Browse Source

add Repr(), added ListNode back in, and some more nodes

master
Brett Langdon 10 years ago
parent
commit
426f42c799
11 changed files with 103 additions and 26 deletions
  1. +6
    -0
      ast/base.go
  2. +7
    -0
      ast/expressionstatement.go
  3. +4
    -17
      ast/fileinput.go
  4. +41
    -0
      ast/listnode.go
  5. +1
    -0
      ast/node.go
  6. +3
    -4
      ast/simplestatement.go
  7. +7
    -0
      ast/smallstatement.go
  8. +7
    -0
      ast/statement.go
  9. +11
    -0
      ast/testliststartexpr.go
  10. +1
    -1
      main.go
  11. +15
    -4
      parser/parser.go

+ 6
- 0
ast/base.go View File

@ -11,3 +11,9 @@ func (node *BaseNode) initBaseNode(id NodeID) {
func (node *BaseNode) Name() string {
return NodeNames[node.ID]
}
func (node *BaseNode) Repr() []interface{} {
out := make([]interface{}, 0)
out = append(out, node.Name())
return out
}

+ 7
- 0
ast/expressionstatement.go View File

@ -2,6 +2,7 @@ package ast
type ExpressionStatement struct {
BaseNode
Expression *TestlistStartExpr
}
func NewExpressionStatement() *ExpressionStatement {
@ -11,3 +12,9 @@ func NewExpressionStatement() *ExpressionStatement {
}
func (node *ExpressionStatement) SmallStatementNode() {}
func (node *ExpressionStatement) Repr() []interface{} {
out := node.BaseNode.Repr()
out = append(out, node.Expression.Repr())
return out
}

+ 4
- 17
ast/fileinput.go View File

@ -1,28 +1,15 @@
package ast
import "github.com/brettlangdon/gython/token"
type FileInput struct {
BaseNode
children []interface{}
ListNode
}
func NewFileInput() *FileInput {
node := &FileInput{
children: make([]interface{}, 0),
}
node := &FileInput{}
node.initBaseNode(FILE_INPUT)
node.initListNode()
return node
}
func (node *FileInput) AppendToken(t *token.Token) {
node.children = append(node.children, t)
}
func (node *FileInput) AppendNode(n StatementNode) {
node.children = append(node.children, n)
}
func (node *FileInput) Children() []interface{} {
return node.children
node.ListNode.AppendNode(n)
}

+ 41
- 0
ast/listnode.go View File

@ -0,0 +1,41 @@
package ast
import "github.com/brettlangdon/gython/token"
type ListNode struct {
BaseNode
children []interface{}
}
func (node *ListNode) initListNode() {
node.children = make([]interface{}, 0)
}
func (node *ListNode) AppendToken(t *token.Token) {
node.children = append(node.children, t)
}
func (node *ListNode) AppendNode(n Node) {
node.children = append(node.children, n)
}
func (node *ListNode) Children() []interface{} {
return node.children
}
func (node *ListNode) Length() int {
return len(node.children)
}
func (node *ListNode) Repr() []interface{} {
out := node.BaseNode.Repr()
for _, child := range node.Children() {
switch child.(type) {
case Node:
out = append(out, child.(Node).Repr())
default:
out = append(out, child)
}
}
return out
}

+ 1
- 0
ast/node.go View File

@ -2,4 +2,5 @@ package ast
type Node interface {
Name() string
Repr() []interface{}
}

+ 3
- 4
ast/simplestatement.go View File

@ -1,8 +1,7 @@
package ast
type SimpleStatement struct {
BaseNode
Statements []*SmallStatement
ListNode
}
func NewSimpleStatement() *SimpleStatement {
@ -13,6 +12,6 @@ func NewSimpleStatement() *SimpleStatement {
func (node *SimpleStatement) StatementNode() {}
func (node *SimpleStatement) AppendSmallStatement(n *SmallStatement) {
node.Statements = append(node.Statements, n)
func (node *SimpleStatement) AppendNode(n *SmallStatement) {
node.ListNode.AppendNode(n)
}

+ 7
- 0
ast/smallstatement.go View File

@ -1,6 +1,7 @@
package ast
type SmallStatementNode interface {
Node
SmallStatementNode()
}
@ -14,3 +15,9 @@ func NewSmallStatement() *SmallStatement {
node.initBaseNode(SMALL_STMT)
return node
}
func (node *SmallStatement) Repr() []interface{} {
out := node.BaseNode.Repr()
out = append(out, node.Statement.Repr())
return out
}

+ 7
- 0
ast/statement.go View File

@ -1,6 +1,7 @@
package ast
type StatementNode interface {
Node
StatementNode()
}
@ -16,3 +17,9 @@ func NewStatement() *Statement {
}
func (node *Statement) StatementNode() {}
func (node *Statement) Repr() []interface{} {
out := node.BaseNode.Repr()
out = append(out, node.Statement.Repr())
return out
}

+ 11
- 0
ast/testliststartexpr.go View File

@ -0,0 +1,11 @@
package ast
type TestlistStartExpr struct {
BaseNode
}
func NewTestListStartExpr() *TestlistStartExpr {
node := &TestlistStartExpr{}
node.initBaseNode(TESTLIST_STAR_EXPR)
return node
}

+ 1
- 1
main.go View File

@ -25,5 +25,5 @@ func tokenize() {
func main() {
root, p := parser.ParseReader(os.Stdin)
fmt.Println(p)
fmt.Println(root)
fmt.Println(root.Repr())
}

+ 15
- 4
parser/parser.go View File

@ -50,10 +50,21 @@ func (parser *Parser) parseCompoundStatement() *ast.CompoundStatement {
return compoundStmt
}
// testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
func (parser *Parser) parseTestlistStartExpr() *ast.TestlistStartExpr {
testlistStartExpr := ast.NewTestListStartExpr()
return testlistStartExpr
}
// expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
// ('=' (yield_expr|testlist_star_expr))*)
func (parser *Parser) parseExpressionStatement() *ast.ExpressionStatement {
exprStmt := ast.NewExpressionStatement()
testlistStartExpr := parser.parseTestlistStartExpr()
if testlistStartExpr == nil {
return nil
}
exprStmt.Expression = testlistStartExpr
return exprStmt
}
@ -82,19 +93,19 @@ func (parser *Parser) parseSimpleStatement() *ast.SimpleStatement {
if smallStmt == nil {
break
}
simpleStmt.AppendSmallStatement(smallStmt)
simpleStmt.AppendNode(smallStmt)
next := parser.nextToken()
if next.ID != token.SEMI {
parser.unreadToken(next)
break
}
}
parser.expect(token.NEWLINE)
// no small statements found
if len(simpleStmt.Statements) == 0 {
if simpleStmt.Length() == 0 {
return nil
}
parser.expect(token.NEWLINE)
return simpleStmt
}


Loading…
Cancel
Save