Browse Source

cleanup ast directory

master
Brett Langdon 10 years ago
parent
commit
6de4a22c88
16 changed files with 203 additions and 189 deletions
  1. +0
    -19
      ast/base.go
  2. +0
    -13
      ast/compoundstatement.go
  3. +16
    -0
      ast/expressions.go
  4. +0
    -20
      ast/expressionstatement.go
  5. +0
    -41
      ast/listnode.go
  6. +0
    -6
      ast/node.go
  7. +67
    -0
      ast/nodes.go
  8. +0
    -17
      ast/simplestatement.go
  9. +0
    -23
      ast/smallstatement.go
  10. +32
    -0
      ast/smallstatements.go
  11. +2
    -3
      ast/start.go
  12. +0
    -25
      ast/statement.go
  13. +43
    -0
      ast/statements.go
  14. +0
    -11
      ast/testliststartexpr.go
  15. +18
    -0
      ast/tests.go
  16. +25
    -11
      parser/parser.go

+ 0
- 19
ast/base.go View File

@ -1,19 +0,0 @@
package ast
type BaseNode struct {
ID NodeID
}
func (node *BaseNode) initBaseNode(id NodeID) {
node.ID = id
}
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
}

+ 0
- 13
ast/compoundstatement.go View File

@ -1,13 +0,0 @@
package ast
type CompoundStatement struct {
Statement
}
func NewCompoundStatement() *CompoundStatement {
node := &CompoundStatement{}
node.initBaseNode(SIMPLE_STMT)
return node
}
func (node *CompoundStatement) StatementNode() {}

+ 16
- 0
ast/expressions.go View File

@ -0,0 +1,16 @@
package ast
type ExpressionNode interface {
Node
exprNode()
}
type TestlistStarExpression struct {
ParentNode
}
func NewTestListStarExpression() *TestlistStarExpression {
node := &TestlistStarExpression{}
node.initBaseNode(TESTLIST_STAR_EXPR)
return node
}

+ 0
- 20
ast/expressionstatement.go View File

@ -1,20 +0,0 @@
package ast
type ExpressionStatement struct {
BaseNode
Expression *TestlistStartExpr
}
func NewExpressionStatement() *ExpressionStatement {
node := &ExpressionStatement{}
node.initBaseNode(EXPR_STMT)
return node
}
func (node *ExpressionStatement) SmallStatementNode() {}
func (node *ExpressionStatement) Repr() []interface{} {
out := node.BaseNode.Repr()
out = append(out, node.Expression.Repr())
return out
}

+ 0
- 41
ast/listnode.go View File

@ -1,41 +0,0 @@
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
}

+ 0
- 6
ast/node.go View File

@ -1,6 +0,0 @@
package ast
type Node interface {
Name() string
Repr() []interface{}
}

+ 67
- 0
ast/nodes.go View File

@ -0,0 +1,67 @@
package ast
import "github.com/brettlangdon/gython/token"
type Node interface {
Name() string
Repr() []interface{}
}
type TokenNode struct {
Token *token.Token
}
func NewTokenNode(tok *token.Token) *TokenNode {
return &TokenNode{
Token: tok,
}
}
func (node *TokenNode) Name() string { return token.TokenNames[node.Token.ID] }
func (node *TokenNode) Repr() []interface{} {
parts := make([]interface{}, 0)
parts = append(parts, node.Name())
return append(parts, node.Token.Literal)
}
type BaseNode struct {
ID NodeID
child Node
}
func (node *BaseNode) initBaseNode(id NodeID) { node.ID = id }
func (node *BaseNode) Name() string { return NodeNames[node.ID] }
func (node *BaseNode) Repr() (parts []interface{}) { return append(parts, node.Name()) }
type ParentNode struct {
BaseNode
child Node
}
func (node *ParentNode) SetChild(n Node) { node.child = n }
func (node *ParentNode) Child() Node { return node.child }
func (node *ParentNode) Repr() (parts []interface{}) {
parts = node.BaseNode.Repr()
child := node.Child()
if child != nil {
parts = append(parts, child.Repr())
}
return parts
}
type ListNode struct {
BaseNode
children []Node
}
func (node *ListNode) initListNode() { node.children = make([]Node, 0) }
func (node *ListNode) Length() int { return len(node.children) }
func (node *ListNode) Children() []Node { return node.children }
func (node *ListNode) Append(n Node) { node.children = append(node.children, n) }
func (node *ListNode) Repr() (parts []interface{}) {
parts = node.BaseNode.Repr()
children := node.Children()
for _, child := range children {
parts = append(parts, child.Repr())
}
return parts
}

+ 0
- 17
ast/simplestatement.go View File

@ -1,17 +0,0 @@
package ast
type SimpleStatement struct {
ListNode
}
func NewSimpleStatement() *SimpleStatement {
node := &SimpleStatement{}
node.initBaseNode(SIMPLE_STMT)
return node
}
func (node *SimpleStatement) StatementNode() {}
func (node *SimpleStatement) AppendNode(n *SmallStatement) {
node.ListNode.AppendNode(n)
}

+ 0
- 23
ast/smallstatement.go View File

@ -1,23 +0,0 @@
package ast
type SmallStatementNode interface {
Node
SmallStatementNode()
}
type SmallStatement struct {
BaseNode
Statement SmallStatementNode
}
func NewSmallStatement() *SmallStatement {
node := &SmallStatement{}
node.initBaseNode(SMALL_STMT)
return node
}
func (node *SmallStatement) Repr() []interface{} {
out := node.BaseNode.Repr()
out = append(out, node.Statement.Repr())
return out
}

+ 32
- 0
ast/smallstatements.go View File

@ -0,0 +1,32 @@
package ast
type SmallStatementNode interface {
Node
smallStmtNode()
}
type SmallStatement struct {
ParentNode
}
func NewSmallStatement() *SmallStatement {
node := &SmallStatement{}
node.initBaseNode(SMALL_STMT)
return node
}
func (node *SmallStatement) SetChild(n SmallStatementNode) { node.ParentNode.SetChild(n) }
type ExpressionStatement struct {
ParentNode
Expression *TestlistStarExpression
}
func NewExpressionStatement() *ExpressionStatement {
node := &ExpressionStatement{}
node.initBaseNode(EXPR_STMT)
return node
}
func (node *ExpressionStatement) smallStmtNode() {}
func (node *ExpressionStatement) SetChild(n *TestlistStarExpression) { node.ParentNode.SetChild(n) }

ast/fileinput.go → ast/start.go View File


+ 0
- 25
ast/statement.go View File

@ -1,25 +0,0 @@
package ast
type StatementNode interface {
Node
StatementNode()
}
type Statement struct {
BaseNode
Statement StatementNode
}
func NewStatement() *Statement {
node := &Statement{}
node.initBaseNode(STMT)
return node
}
func (node *Statement) StatementNode() {}
func (node *Statement) Repr() []interface{} {
out := node.BaseNode.Repr()
out = append(out, node.Statement.Repr())
return out
}

+ 43
- 0
ast/statements.go View File

@ -0,0 +1,43 @@
package ast
type StatementNode interface {
Node
stmtNode()
}
type Statement struct {
ParentNode
}
func NewStatement() *Statement {
node := &Statement{}
node.initBaseNode(STMT)
return node
}
func (node *Statement) SetChild(n StatementNode) { node.ParentNode.SetChild(n) }
type SimpleStatement struct {
ListNode
}
func NewSimpleStatement() *SimpleStatement {
node := &SimpleStatement{}
node.initBaseNode(SIMPLE_STMT)
node.initListNode()
return node
}
func (node *SimpleStatement) stmtNode() {}
func (node *SimpleStatement) Append(n *SmallStatement) { node.ListNode.Append(n) }
type CompoundStatement struct {
BaseNode
}
func NewCompoundStatement() *CompoundStatement {
node := &CompoundStatement{}
node.initBaseNode(COMPOUND_STMT)
return node
}
func (node *CompoundStatement) stmtNode() {}

+ 0
- 11
ast/testliststartexpr.go View File

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

+ 18
- 0
ast/tests.go View File

@ -0,0 +1,18 @@
package ast
type TestNode interface {
Node
test()
}
type Test struct {
ParentNode
}
func NewTest() *Test {
node := &Test{}
node.initBaseNode(TEST)
return node
}
func (node *Test) SetChild(n TestNode) { node.ParentNode.SetChild(n) }

+ 25
- 11
parser/parser.go View File

@ -50,21 +50,34 @@ func (parser *Parser) parseCompoundStatement() *ast.CompoundStatement {
return compoundStmt
}
// test: or_test ['if' or_test 'else' test] | lambdef
func (parser *Parser) parseTest() *ast.Test {
test := ast.NewTest()
return test
}
// testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
func (parser *Parser) parseTestlistStartExpr() *ast.TestlistStartExpr {
testlistStartExpr := ast.NewTestListStartExpr()
return testlistStartExpr
func (parser *Parser) parseTestlistStarExpression() *ast.TestlistStarExpression {
testlistStarExpression := ast.NewTestListStarExpression()
var expr ast.Node
expr = parser.parseTest()
if expr == nil {
return nil
}
testlistStarExpression.SetChild(expr)
return testlistStarExpression
}
// 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 {
testlistStarExpression := parser.parseTestlistStarExpression()
if testlistStarExpression == nil {
return nil
}
exprStmt.Expression = testlistStartExpr
exprStmt.SetChild(testlistStarExpression)
return exprStmt
}
@ -76,7 +89,7 @@ func (parser *Parser) parseSmallStatment() *ast.SmallStatement {
var stmt ast.SmallStatementNode
stmt = parser.parseExpressionStatement()
if stmt != nil {
smallStmt.Statement = stmt
smallStmt.SetChild(stmt)
}
if stmt == nil {
@ -93,7 +106,7 @@ func (parser *Parser) parseSimpleStatement() *ast.SimpleStatement {
if smallStmt == nil {
break
}
simpleStmt.AppendNode(smallStmt)
simpleStmt.Append(smallStmt)
next := parser.nextToken()
if next.ID != token.SEMI {
parser.unreadToken(next)
@ -122,7 +135,7 @@ func (parser *Parser) parseStatement() *ast.Statement {
}
stmt := ast.NewStatement()
stmt.Statement = next
stmt.SetChild(next)
return stmt
}
@ -132,7 +145,8 @@ func (parser *Parser) parseFileInput() *ast.FileInput {
for parser.tokenizer.State() == errorcode.E_OK {
next := parser.nextToken()
if next.ID == token.NEWLINE {
root.AppendToken(next)
// root.Append(ast.NewTokenNode(next))
continue
} else if next.ID == token.ENDMARKER {
// Unread, so we can read in the expected value later
parser.unreadToken(next)
@ -143,7 +157,7 @@ func (parser *Parser) parseFileInput() *ast.FileInput {
if stmt == nil {
break
}
root.AppendNode(stmt)
root.Append(stmt)
break
}
}


Loading…
Cancel
Save