First past and printer function are totally ready
authorOscar J Rodriguez <jrpc@google.com>
Sat, 18 Mar 2023 09:13:41 +0000 (02:13 -0700)
committerOscar J Rodriguez <jrpc@google.com>
Sat, 18 Mar 2023 09:13:41 +0000 (02:13 -0700)
compiler.go
go.mod
go.sum [new file with mode: 0644]

index 4ad5e55090da421ad08fc1a97dd652d970657464..6cd94fd9c70a8003efb97fbc030955e9a4228291 100644 (file)
@@ -29,14 +29,16 @@ type AST struct {
 func main() {
        //a := &AST{Op: imm, N: 5
        //b := &AST{Op: plus, A: a, B: &AST{Op: arg, N: 0}}
-       input := "[ a b ] a*a + b*b"
+       input := "[ a b ] (a*a) + (b*b)"
        //value := []rune(input)
 
        variables, program := extractVariables(input)
 
        fmt.Println(variables, program)
-       var Tree AST
-       firstPass(program, &Tree)
+       Tree := AST{}
+       firstPass(variables, program, &Tree)
+       fmt.Println(Tree)
+       printer(&Tree)
        //si es una letra y el stack esta sin setear pon en el A del stack un AST arg
        //si es una operacion setea la op en el stack
        //si es un abrir parentesis apunta al lado que este disponible del AST
@@ -47,53 +49,81 @@ func main() {
 
 }
 
+func printer(tree *AST) {
+       switch {
+       case tree.Op == imm:
+               fmt.Print(tree.Value - 48)
+       case tree.Op == arg:
+               fmt.Printf("%c", tree.Value)
+       default:
+               fmt.Print("(")
+               switch tree.Op {
+               case min:
+                       fmt.Print("-")
+               case plus:
+                       fmt.Print("+")
+               case div:
+                       fmt.Print("/")
+               case mul:
+                       fmt.Print("*")
+               }
+               fmt.Print(",")
+               printer(tree.Left)
+               fmt.Print(",")
+               printer(tree.Right)
+               fmt.Print(")")
+
+       }
+}
+
 func firstPass(variables, program []rune, node *AST) {
+       pass := node
        switch program[0] {
        case '-':
                node.Op = min
-               firstPass(variables, program[1:], node)
        case '+':
                node.Op = plus
-               firstPass(variables, program[1:], node)
        case '*':
                node.Op = mul
-               firstPass(variables, program[1:], node)
        case '/':
                node.Op = div
-               firstPass(variables, program[1:], node)
        case '(':
-               if node.Left != nil {
-                       firstPass(variables, program[1:], node.Left)
+               if node.Left == nil {
+                       node.Left = &AST{}
+                       node.Left.Parent = node
+                       pass = node.Left
                } else {
-                       firstPass(variables, program[1:], node.Right)
+                       node.Right = &AST{}
+                       node.Right.Parent = node
+                       pass = node.Right
                }
        case ')':
-               return
+               pass = node.Parent
 
        default:
                if program[0] > 47 && program[0] < 58 {
                        var zeroOp op
                        if node.Op == zeroOp {
                                node.Left = &AST{Op: imm, Value: int(program[0]) - 48}
-                               firstPass(variables, program[1:], node)
                                //a := &AST{Op: imm, N: 5
                        } else {
                                node.Right = &AST{Op: imm, Value: int(program[0]) - 48}
-                               firstPass(variables, program[1:], node)
                        }
                } else if slices.Contains(variables, program[0]) {
-                       var zeroOp op
-                       if node.Op == zeroOp {
-                               node.Left = &AST{Op: imm, Value: int(program[0]) - 48}
-                               firstPass(variables, program[1:], node)
+                       //var zeroOp op
+                       if node.Op != 2 && node.Op != 3 && node.Op != 4 && node.Op != 5 {
+                               node.Left = &AST{Op: arg, Value: int(program[0])}
                                //a := &AST{Op: imm, N: 5
                        } else {
-                               node.Right = &AST{Op: imm, Value: int(program[0]) - 48}
-                               firstPass(variables, program[1:], node)
+                               node.Right = &AST{Op: arg, Value: int(program[0])}
                        }
 
                }
 
+       }
+       if len(program) > 1 {
+               firstPass(variables, program[1:], pass)
+
        }
        return
 
diff --git a/go.mod b/go.mod
index e73107b26ee7ecbdfe9f5b707012d27ae7375298..02248a43a4cd4e0b7298b8502ec45ebd25ca7f83 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
 module TinyThreePassCompiler
 
 go 1.19
+
+require golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0
diff --git a/go.sum b/go.sum
new file mode 100644 (file)
index 0000000..a06f679
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
+golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 h1:pVgRXcIictcr+lBQIFeiwuwtDIs4eL21OuM9nyAADmo=
+golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=