From 37b9da32d3b911091deb254f6cba2a137c471287 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Thu, 12 Oct 2023 10:51:58 +0200 Subject: 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 --- parser/dot.go | 89 ----------------------------------------------------------- 1 file changed, 89 deletions(-) delete mode 100644 parser/dot.go (limited to 'parser/dot.go') 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 -} -- cgit v1.2.3