-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.go
More file actions
82 lines (63 loc) · 1.57 KB
/
ast.go
File metadata and controls
82 lines (63 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import "fmt"
// -------------------- AST Types
// Base expression type
type ExprAST interface{}
// Expression type for a number
type NumberExprAST struct{ Val string }
// Expression type for a variable
type VariableExprAST struct{ Name string }
// Expression type for a binary operator
type BinaryExprAST struct {
Op rune
LHS *ExprAST
RHS *ExprAST
}
// Expression type for a function call
type CallExprAST struct {
Callee string
Args []ExprAST
}
// Represent a function prototype
// Captures the name and args of a function
type PrototypeAST struct {
Name string
Args []string
}
// Represents a function itself
type FunctionAST struct {
Prototype *PrototypeAST
Body *ExprAST
}
// -------------------- Parser Logic
// Parser handles all logic related to building/parsing the abstract syntax tree
type Parser struct {
tokens <-chan Token
curTok Token
}
func (p *Parser) logErrorF(format string, args ...interface{}) ExprAST {
fmt.Printf(format, args...)
return nil
}
// Retrieves the next token and assigns it to Parser.curTok
func (p *Parser) getNextToken() {
p.curTok = <-p.tokens
}
func (p *Parser) parseNumberExpr() ExprAST {
val := p.curTok.(NumberToken)
p.getNextToken() // Consume the token
return NumberExprAST{val.string}
}
func (p *Parser) parseParenExpr() ExprAST {
p.getNextToken() // Consume '('
// V := p.parseExpression()
// if !V {
// return nil
// }
val, ok := p.curTok.(IdentifierToken)
if !ok || val.string != ")" {
p.logErrorF("Expected )")
}
p.getNextToken() // Consume ')'
return nil // TODO change this to return V
}