diff options
| author | Marc Vertes <marc.vertes@tendermint.com> | 2023-10-12 10:51:58 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-12 10:51:58 +0200 |
| commit | 37b9da32d3b911091deb254f6cba2a137c471287 (patch) | |
| tree | b4451de0fa0473a937a77d39fd1f8a4f87c8f60d /parser/dot.go | |
| parent | a21b9b12ad865a19ff687645082f9093c4101039 (diff) | |
move to a direct byte code compiler (#8)
* chore: refactor to keep only the new parser and bytecode vm
* scanner: remove Token.value field
* scanner: remove scanner.kind field
* chore: move language specification in lang package
This avoid a cyclic dependency in scanner_test which can now use
the golang/GoSpec language specification for Go.
* clean code
* scanner: export scanner fields
Also parser now generate function calls, including externals.
* chore: fix lint issues
* parser: handle strings
* wip
* parser: implement support for 'if, else, else if' statements
Resolving labels in the compiler still in progress.
* parser: support if statements, improve compiler
* improve handling of functions
* improve support of local variables
* scanner: trim leading and trailing spaces
* fixes to make fibonacci work
* parser: improve README, fix function parameters parsing
Diffstat (limited to 'parser/dot.go')
| -rw-r--r-- | parser/dot.go | 89 |
1 files changed, 0 insertions, 89 deletions
diff --git a/parser/dot.go b/parser/dot.go deleted file mode 100644 index 11d5014..0000000 --- a/parser/dot.go +++ /dev/null @@ -1,89 +0,0 @@ -package parser - -import ( - "bytes" - "fmt" - "io" - "log" - "os/exec" - "strings" -) - -func (*Parser) Adot(nodes []*Node, c string) { - if c == "" { - return - } - n := &Node{Child: nodes} - n.Dot(c, "") -} - -func (n *Node) Dot(c, s string) { - dw, cmd := dotWriter(c) - n.astDot(dw, s) - if cmd == nil { - return - } - if err := cmd.Wait(); err != nil { - log.Fatal(err) - } -} - -func (n *Node) Sdot(s string) string { - var buf bytes.Buffer - n.astDot(&buf, s) - return buf.String() -} - -// TODO: rewrite it using Walk2 -func (n *Node) astDot(out io.Writer, label string) { - fmt.Fprintf(out, "digraph ast { ") - if label != "" { - fmt.Fprintf(out, "labelloc=\"t\"; label=\"%s\";", label) - } - anc := map[*Node]*Node{} - index := map[*Node]int{} - count := 0 - n.Walk(func(nod *Node) bool { - index[nod] = count - count++ - - for _, c := range nod.Child { - anc[c] = nod - } - name := "" - if nod.Token != nil { - name = strings.ReplaceAll(nod.Name(), `"`, `\"`) - } - fmt.Fprintf(out, "%d [label=\"%s\"]; ", index[nod], name) - if anc[nod] != nil { - fmt.Fprintf(out, "%d -> %d; ", index[anc[nod]], index[nod]) - } - return true - }, nil) - fmt.Fprintf(out, "}") - if c, ok := out.(io.Closer); ok { - c.Close() - } -} - -type nopCloser struct { - io.Writer -} - -func (nopCloser) Close() error { return nil } - -func dotWriter(dotCmd string) (io.WriteCloser, *exec.Cmd) { - if dotCmd == "" { - return nopCloser{io.Discard}, nil - } - fields := strings.Fields(dotCmd) - cmd := exec.Command(fields[0], fields[1:]...) - dotin, err := cmd.StdinPipe() - if err != nil { - log.Fatal(err) - } - if err = cmd.Start(); err != nil { - log.Fatal(err) - } - return dotin, cmd -} |
