Skip to content

Commit

Permalink
Use parens to demarcate expressions in AST parsing
Browse files Browse the repository at this point in the history
Gamelan music currently playing: Pelog Barang - Singa Nebah

co-authored-by: Connor Walsh <[email protected]>
co-authored-by: Octavia <[email protected]>
  • Loading branch information
3 people committed May 10, 2018
1 parent c0b0f17 commit d2c799f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 11 deletions.
59 changes: 59 additions & 0 deletions forest.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,65 @@ func (e *Expr) Eval() (string, error) {
// but is should overwrite the Eval function since it does that differently.
type IfConditional struct{}

/*
SOME NOTES (mr,cw|4.25.2018):
------ define function ------
(# add (x y) (+ x y))
build an expression:
{
Op: ?
Operands: [<sequential list of expressions in function>]
Locals: [local vars or functions]
Args: [x, y]
}
store that expression in Locals
------ eval program --------
(add 4 5)
build an expression for our executed program:
add
/ \
4 5
{ Op: "add"
operands: [4, 5]
}
------
locals
add:
+
/ \
x y -> args
/|\
\./
nnnnnnnnnnn
mm0m0m0m0m0m0m
m0m0m0m0m0m0mm
mmmmmmmmmmmmmm
/// \\\
//// \\\\
//// \\\\
||| ==== ==== |||
|| ( ) ( ) ||
| | |
| | |
\ ( ) /
\ mmmmmmm /
| ----- |
\________/
welcome 2 dockerland
*/

// an Identifier can be a named expression which can either be a variable
// or a function which is indicated by its Type.
type Identifier struct {
Expand Down
38 changes: 31 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *Compterpreter) Parse() error {
// on all program roots.
c.StackTree = &Expr{}

for _, token := range c.Tokens {
for idx, token := range c.Tokens {
if parenCount < 0 {
return SyntaxError("too many close parens", "current token:", string(token.Value))
}
Expand All @@ -69,19 +69,43 @@ func (c *Compterpreter) Parse() error {
case PUNCTUATION:
switch token.Value {
case "(":
// TODO: eventually check a puntaution stack for syntax checking
parenCount++
// push the open paren to the expression stack so we know when an expression
// or array is starting.
exprStack.Push(&Expr{Op: token.Value})

// check to see if the next token is *not* an operator/keyword
if idx + 1 < len(c.Tokens) {
nextToken := c.Tokens[idx+1]

if nextToken.Type != OPERATOR && nextToken.Type != KEYWORD {
// add the implicit list operator to the ops stack since an
// open paren followed by a non-operator or keyword is assumed
// to be an array
opsStack.Push(&Expr{Op: IMPLICIT_LIST_OPERATOR, Arity: -1})
}
}
case ")":
// shit gets real
var opsExpr = opsStack.Pop().(*Expr)
// pop a count of arity items off exprStack
for i := 0; i < opsExpr.Arity; i++ {
for {
// make sure we're not popping nil into exprs
if exprStack.Peek() == nil {
return SyntaxError()
}
opsExpr.Operands = append([]AST{exprStack.Pop()}, opsExpr.Operands...)
}

// pop the expression off the expr stack
exp := exprStack.Pop()

// check whether this popped expression is an open paren
// if it is, stop the loop
if e, ok := exp.(*Expr); ok && e.Op == "(" {
break
}

// add this expression to the expression operands array`
opsExpr.Operands = append([]AST{exp, opsExpr.Operands...)
}

// push modified ops expr onto the expr stack
exprStack.Push(opsExpr)

Expand Down
2 changes: 2 additions & 0 deletions symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
VARIABLE_INITIALIZATION = "≡"
VARIABLE_ASSIGNMENT = "="
EXIT_OPERATOR = "ꙮ"
IMPLICIT_LIST_OPERATOR = "…"
NOOP = "NOOP"

R_PAREN_PUNCTION = "("
Expand Down Expand Up @@ -58,6 +59,7 @@ func PopulateSymbols() *Symbols {
VARIABLE_INITIALIZATION,
VARIABLE_ASSIGNMENT,
EXIT_OPERATOR,
IMPLICIT_LIST_OPERATOR,
NOOP,
},
Keywords: []string{
Expand Down
7 changes: 3 additions & 4 deletions test/test_function.doc
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@


(❦ add (arg1 arg2) (
(≡ a (+ arg1 arg2))
(return a)
(≡ x arg1)
(≡ y arg2)
(+ x (+ y 1))
))

(add 2 3)

0 comments on commit d2c799f

Please sign in to comment.