From 13c5cc552604ce63466e3c5cbce7c1125ac93cf8 Mon Sep 17 00:00:00 2001 From: connorwalsh Date: Mon, 2 Apr 2018 20:25:19 -0400 Subject: [PATCH] add support for additional arithmetic operators Major Changes: * support subtraction, multiplication, division, modulo * print syntax errors when they occur Git issue references: Gamelan music currently playing: Gendhing Prabu Mataram Co-authored-by: Mouse Reeve --- compterpreter.go | 21 ++++++++++++++++++--- memorycell/main.go | 31 +++++++++++++++++++++++++++++++ parser.go | 7 +++++++ test/division.doc | 1 + test/modulo.doc | 1 + test/multiplication.doc | 1 + test/nested_arithmetic.doc | 1 + test/subtraction.doc | 2 ++ 8 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 test/division.doc create mode 100644 test/modulo.doc create mode 100644 test/multiplication.doc create mode 100644 test/nested_arithmetic.doc create mode 100644 test/subtraction.doc diff --git a/compterpreter.go b/compterpreter.go index 0c1a239..4abec78 100644 --- a/compterpreter.go +++ b/compterpreter.go @@ -87,12 +87,27 @@ func (c *Compterpreter) LoadSourceCode() error { } func (c *Compterpreter) Interpret() error { + var ( + err error + ) + // Identifies tokens in the provided .doc code - c.Lex() + err = c.Lex() + if err != nil { + return err + } + // Creates c.StackTree representing the provided .doc code - c.Parse() + err = c.Parse() + if err != nil { + return err + } + // Actually dockerize and evaluate the StackTree - c.Evaluate() + err = c.Evaluate() + if err != nil { + return err + } return nil } diff --git a/memorycell/main.go b/memorycell/main.go index d92e24a..58ef2b9 100644 --- a/memorycell/main.go +++ b/memorycell/main.go @@ -5,6 +5,7 @@ import ( "io" "io/ioutil" "log" + "math" "net/http" "os" "strconv" @@ -91,6 +92,36 @@ func (c *Computation) ExecHandler() string { input2, _ := strconv.Atoi(c.Dependencies[1]) result := strconv.Itoa(input1 + input2) + return result + case dockerlang.SUBTRACTION_OPERATOR: + input1, _ := strconv.Atoi(c.Dependencies[0]) + input2, _ := strconv.Atoi(c.Dependencies[1]) + + result := strconv.Itoa(input1 - input2) + return result + case dockerlang.MULTIPLICATION_OPERATOR: + input1, _ := strconv.Atoi(c.Dependencies[0]) + input2, _ := strconv.Atoi(c.Dependencies[1]) + + result := strconv.Itoa(input1 * input2) + return result + case dockerlang.DIVISION_OPERATOR: + input1, _ := strconv.Atoi(c.Dependencies[0]) + input2, _ := strconv.Atoi(c.Dependencies[1]) + + result := strconv.Itoa(input1 / input2) + return result + case dockerlang.MODULO_OPERATOR: + input1, _ := strconv.ParseFloat(c.Dependencies[0], 32) + input2, _ := strconv.ParseFloat(c.Dependencies[1], 32) + + result := strconv.FormatFloat( + math.Mod(input1, input2), + 'f', + -1, + 32, + ) + return result default: // wtf is this type?????? diff --git a/parser.go b/parser.go index 59e234c..61b2111 100644 --- a/parser.go +++ b/parser.go @@ -1,5 +1,7 @@ package dockerlang +import "fmt" + type Stack struct { Elements []AST } @@ -62,6 +64,7 @@ func (c *Compterpreter) Parse() error { for i := 0; i < opsExpr.Arity; i++ { // make sure we're not popping nil into exprs if exprStack.Peek() == nil { + fmt.Println("1") return DockerlangSyntaxError } opsExpr.Operands = append([]AST{exprStack.Pop()}, opsExpr.Operands...) @@ -80,9 +83,13 @@ func (c *Compterpreter) Parse() error { return DockerlangSyntaxError } if opsStack.Peek() != nil { + fmt.Println(opsStack.Peek()) // oh noooo! return DockerlangSyntaxError } + + fmt.Println(exprStack) + c.StackTree.Operands = []AST{exprStack.Pop().(*Expr)} return nil diff --git a/test/division.doc b/test/division.doc new file mode 100644 index 0000000..1b587f3 --- /dev/null +++ b/test/division.doc @@ -0,0 +1 @@ +(ꙮ (‡ 99 3)) diff --git a/test/modulo.doc b/test/modulo.doc new file mode 100644 index 0000000..2376dcc --- /dev/null +++ b/test/modulo.doc @@ -0,0 +1 @@ +(ꙮ (% 11 2)) diff --git a/test/multiplication.doc b/test/multiplication.doc new file mode 100644 index 0000000..2ab2cc4 --- /dev/null +++ b/test/multiplication.doc @@ -0,0 +1 @@ +(ꙮ (* 3 5)) diff --git a/test/nested_arithmetic.doc b/test/nested_arithmetic.doc new file mode 100644 index 0000000..e23d29b --- /dev/null +++ b/test/nested_arithmetic.doc @@ -0,0 +1 @@ +(ꙮ (+ († (‡ 99 3) 31) (% (* 4 11) 2))) diff --git a/test/subtraction.doc b/test/subtraction.doc new file mode 100644 index 0000000..de9d519 --- /dev/null +++ b/test/subtraction.doc @@ -0,0 +1,2 @@ + +(ꙮ († 5 3))