Skip to content

Commit

Permalink
add support for additional arithmetic operators
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
connorwalsh and mouse-reeve committed Apr 3, 2018
1 parent d99b691 commit 13c5cc5
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 3 deletions.
21 changes: 18 additions & 3 deletions compterpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions memorycell/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"
"log"
"math"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -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??????
Expand Down
7 changes: 7 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dockerlang

import "fmt"

type Stack struct {
Elements []AST
}
Expand Down Expand Up @@ -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...)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/division.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ꙮ (‡ 99 3))
1 change: 1 addition & 0 deletions test/modulo.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ꙮ (% 11 2))
1 change: 1 addition & 0 deletions test/multiplication.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ꙮ (* 3 5))
1 change: 1 addition & 0 deletions test/nested_arithmetic.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ꙮ (+ († (‡ 99 3) 31) (% (* 4 11) 2)))
2 changes: 2 additions & 0 deletions test/subtraction.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

(ꙮ († 5 3))

0 comments on commit 13c5cc5

Please sign in to comment.