Skip to content

Commit

Permalink
lex integer/float nodes properly (needs tests), stab at stack collaps…
Browse files Browse the repository at this point in the history
…ing for order of ops but it is too simplistic
  • Loading branch information
jmoiron committed May 30, 2014
1 parent 6241024 commit 785c5cb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
26 changes: 26 additions & 0 deletions neo/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ func lexInsideBlock(l *lexer) stateFn {
switch {
case isSpace(r):
return lexSpace
case isNumeric(r):
return lexNumber
case isAlphaNumeric(r):
return lexIdentifier
case r == '"':
Expand Down Expand Up @@ -514,6 +516,26 @@ func lexIdentifier(l *lexer) stateFn {
}
}

func lexNumber(l *lexer) stateFn {
tokType := tokenInteger
for {
switch r := l.next(); {
case isNumeric(r):
// abosrb
case r == '.':
if tokType != tokenFloat {
tokType = tokenFloat
} else {
l.errorf("two dots in numeric token")
}
default:
l.backup()
l.emit(tokType)
return lexInsideBlock
}
}
}

// Called at the end of a string
func (l *lexer) emitString() {
l.backup()
Expand Down Expand Up @@ -567,3 +589,7 @@ func isEndOfLine(r rune) bool {
func isAlphaNumeric(r rune) bool {
return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r)
}

func isNumeric(r rune) bool {
return unicode.IsDigit(r)
}
30 changes: 30 additions & 0 deletions neo/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"runtime"
"strings"

"github.com/davecgh/go-spew/spew"
)

// Important to jigo, as to most languages, is the idea of an expression.
Expand Down Expand Up @@ -203,6 +205,29 @@ func (t *Tree) expect(i itemType) (token item) {
return t.nextNonSpace()
}

// Collapse N nodes into one
func (t *Tree) collapse(stack *nodeStack) Node {
if stack.len() < 2 {
return stack.pop()
}
rhs := stack.pop()
lhs := stack.pop()
switch lhs.(type) {
case *AddExpr:
lhs := lhs.(*AddExpr)
lhs.rhs = rhs
return lhs
case *MulExpr:
lhs := lhs.(*MulExpr)
lhs.rhs = rhs
return lhs
default:
stack.push(lhs)
stack.push(rhs)
return nil
}
}

// Parsing.

// New allocates a new parse tree with the given name.
Expand Down Expand Up @@ -402,6 +427,11 @@ func (t *Tree) parseExpr(stack *nodeStack, terminator itemType) Node {
if unary.typ != 0 && stack.len() > 0 {
// apply the unary to the expression
}
if stack.len() > 1 {
fmt.Printf("Stack: ")
spew.Dump(stack.Nodes)
return t.collapse(stack)
}
token = t.peekNonSpace()
switch token.typ {
case terminator:
Expand Down
7 changes: 7 additions & 0 deletions neo/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ func spewTree(n Node, indent string) {
fmt.Printf("%s +\n", indent)
spewTree(n.rhs, indent+" ")
fmt.Printf("%s}\n", indent)
case *MulExpr:
n := n.(*MulExpr)
fmt.Printf("%s(MulExpr) {\n", indent)
spewTree(n.lhs, indent+" ")
fmt.Printf("%s *\n", indent)
spewTree(n.rhs, indent+" ")
fmt.Printf("%s}\n", indent)
default:
fmt.Printf(indent)
spew.Dump(n)
Expand Down

0 comments on commit 785c5cb

Please sign in to comment.