Skip to content

Commit

Permalink
Start adding statement parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aszecsei committed Oct 10, 2018
1 parent f553452 commit 56669da
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 15 deletions.
13 changes: 7 additions & 6 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,10 @@ func (e *ImportStatement) Pos() token.Pos { return e.Import }
var _ Stmt = (*ImportStatement)(nil) // ImportStatement implements Stmt

type ImportList struct {
Open token.Pos
Names []*ImportIdent
Close token.Pos
}

func (i *ImportList) Pos() token.Pos { return i.Open }
func (i *ImportList) Pos() token.Pos { return i.Names[0].Pos() }

var _ Node = (*ImportList)(nil) // ImportList implements Node

Expand Down Expand Up @@ -377,11 +375,12 @@ func (n *NamedType) Pos() token.Pos { return n.Name.NamePos }
var _ Type = (*NamedType)(nil)

type Expr interface {
Node
Stmt
exprNode()
}

type Expression struct {
Statement
}

func (e *Expression) exprNode() {}
Expand Down Expand Up @@ -439,8 +438,10 @@ type BasicLiteral struct {
Lit string
}

func (b *BasicLiteral) Pos() token.Pos { return b.LitPos }
func (b *BasicLiteral) exprNode() {}
func (b *BasicLiteral) Pos() token.Pos { return b.LitPos }
func (b *BasicLiteral) exprNode() {}
func (b *BasicLiteral) blockElementNode() {}
func (b *BasicLiteral) statementNode() {}

type IntegerLiteral struct {
BasicLiteral
Expand Down
16 changes: 8 additions & 8 deletions docs/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,16 @@
| "in"
| "is"

<adding operator> ::= "+"
| "-"
| "|"
| "^"
<adding operator> ::= "+" [ "=" ]
| "-" [ "=" ]
| "|" [ "=" ]
| "^" [ "=" ]
| "||"

<multiplying operator> ::= "*"
| "/"
| "%"
| "&"
<multiplying operator> ::= "*" [ "=" ]
| "/" [ "=" ]
| "%" [ "=" ]
| "&" [ "=" ]
| "&&"

<factor> ::= [ "-" | "!" ] <primary expression>
Expand Down
120 changes: 119 additions & 1 deletion parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func isDeclarationStarter(t token.TokenType) bool {
func (p *parser) parseBlock() *ast.Block {
elems := make([]ast.BlockElement, 0)
begin := p.scanner.CurrentLexeme().Position
for p.scanner.CurrentLexeme().Type != token.EOF {
for p.scanner.CurrentLexeme().Type != token.EOF || p.scanner.CurrentLexeme().Type != token.RCURLYB {
cur := p.scanner.CurrentLexeme()
var elem ast.BlockElement
if isDeclarationStarter(cur.Type) {
Expand Down Expand Up @@ -373,17 +373,135 @@ func (p *parser) parseEnumValueList() *ast.EnumValueList {
}

func (p *parser) parseStatement() ast.Stmt {
switch p.scanner.CurrentLexeme().Type {
case token.IMPORT:
return p.parseImport()
case token.LCURLYB:
return p.parseInnerBlock()
case token.IF:
return p.parseIf()
case token.FOR:
return p.parseFor()
case token.WHILE:
return p.parseWhile()
case token.DO:
return p.parseDoWhile()
case token.BREAK:
return p.parseJumpStatement()
case token.CONTINUE:
return p.parseJumpStatement()
case token.RETURN:
return p.parseJumpStatement()
default:
return p.parseExpression()
}
}

func (p *parser) parseImport() *ast.ImportStatement {
importPos := p.expect(token.IMPORT)
imports := p.parseImportList()
fromPos := p.expect(token.FROM)
path := p.parseStringLiteral()

return &ast.ImportStatement{
Import: importPos,
ImportList: imports,
From: fromPos,
Path: path,
}
}

func (p *parser) parseImportList() *ast.ImportList {
vals := make([]*ast.ImportIdent, 0)
v := p.parseImportIdent()
vals = append(vals, v)
for p.scanner.CurrentLexeme().Type == token.COMMA {
p.expect(token.COMMA)
x := p.parseImportIdent()
vals = append(vals, x)
}

return &ast.ImportList{
Names: vals,
}
}

func (p *parser) parseImportIdent() *ast.ImportIdent {
old := p.parseIdentifier()
asPos := token.NoPos
var newName *ast.Ident
newName = nil
if old.Name == "*" || p.scanner.CurrentLexeme().Type == token.AS {
asPos = p.expect(token.AS)
newName = p.parseIdentifier()
}
return &ast.ImportIdent{
OldName: old,
As: asPos,
NewName: newName,
}
}

func (p *parser) parseInnerBlock() *ast.InnerBlock {
open := p.expect(token.LCURLYB)
block := p.parseBlock()
close := p.expect(token.RCURLYB)

return &ast.InnerBlock{
Open: open,
Block: block,
Close: close,
}
}

func (p *parser) parseIf() *ast.If {
// TODO
return nil
}

func (p *parser) parseFor() *ast.ForLoop {
// TODO
return nil
}

func (p *parser) parseWhile() *ast.WhileLoop {
// TODO
return nil
}

func (p *parser) parseDoWhile() *ast.DoWhileLoop {
// TODO
return nil
}

func (p *parser) parseJumpStatement() *ast.JumpStatement {
// TODO
return nil
}

func (p *parser) parseExpression() ast.Expr {
// TODO
return nil
}

func (p *parser) parseType() ast.Type {
// TODO
return nil
}

func (p *parser) parseStringLiteral() *ast.StringLiteral {
lit := p.scanner.CurrentLexeme().Literal
pos := p.expect(token.STRING)
return &ast.StringLiteral{
BasicLiteral: ast.BasicLiteral{
LitPos: pos,
Kind: token.STRING,
Lit: lit,
},
Value: lit,
}
}

func (p *parser) parseIdentifier() *ast.Ident {
name := p.scanner.CurrentLexeme().Literal
return &ast.Ident{
Expand Down
2 changes: 2 additions & 0 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const (
FROM
FOR
WHILE
DO
IF
ELSE
BREAK
Expand Down Expand Up @@ -186,6 +187,7 @@ var tok_strings = map[TokenType]string{
FROM: "from",
FOR: "for",
WHILE: "while",
DO: "do",
IF: "if",
ELSE: "else",
BREAK: "break",
Expand Down

0 comments on commit 56669da

Please sign in to comment.