From d38ad5ba3ee3677c4d00213fc93497f9031cba08 Mon Sep 17 00:00:00 2001
From: Marc Vertes Yaegi is an
interpreter of the Go language written in Go. This project was started
in Traefik-Labs initially to provide a simple and practical embedded
@@ -36,7 +41,7 @@ overview. Let’s see what happens inside yaegi when one executes the following
line: The following figure 1 displays the main steps of evaluation:Overview of architecture
interp.Eval(`print("hello", 2+3)`)interp.Eval(`print("hello", 2+3)`)
interpreter or other language tool, and the function to use it
(extracted from here).
// node defines a node of a (abstract syntax) tree.
-type node struct {
- // Node children
- child []*node
- // Node metadata
- ...
-}
-
-// walk traverses AST n in depth first order, invoking in function
-// at node entry and out function at node exit.
-func (n *node) walk(in func(n *node) bool, out func(n *node)) {
- if in != nil && !in(n) {
- return
- }
- for _, child := range n.child {
- child.Walk(in, out)
- }
- if out != nil {
- out(n)
- }
-}// node defines a node of a (abstract syntax) tree.
+type node struct {
+ // Node children
+ child []*node
+ // Node metadata
+ ...
+}
+
+// walk traverses AST n in depth first order, invoking in function
+// at node entry and out function at node exit.
+func (n *node) walk(in func(n *node) bool, out func(n *node)) {
+ if in != nil && !in(n) {
+ return
+ }
+ for _, child := range n.child {
+ child.Walk(in, out)
+ }
+ if out != nil {
+ out(n)
+ }
+}The above code is deceptively simple. As in many complex systems, an important part of the signification is carried by the relationships between the elements and the patterns they form. It’s easier to understand it by displaying the corresponding graph and consider the system as a whole. We can do that using a simple example:
-a := 3
-if a > 2 {
- print("ok")
-}
-print("bye")a := 3
+if a > 2 {
+ print("ok")
+}
+print("bye")The corresponding AST is:
const (
- prefix = "/usr"
- path = prefix + "/local/bin"
-)
-var a [len(prefix+path) + 2]intconst (
+ prefix = "/usr"
+ path = prefix + "/local/bin"
+)
+var a [len(prefix+path) + 2]intA paradox is that the compiler needs an interpreter to perform the
type analysis! Indeed, in the example above, [16]int
(because len(prefix+path) + 2 = 16) is a specific type in
@@ -384,7 +389,8 @@ in a next article:
P.S. Thanks to @lejatorn for his feedback and suggestions on this post.
-]]> +