Tests for generation from AST
This commit is contained in:
parent
22d61691f1
commit
a2aad0e4b9
|
|
@ -39,10 +39,6 @@ type BinaryExpr struct {
|
||||||
right Expr
|
right Expr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b BinaryExpr) Kind() string {
|
|
||||||
return "BinaryExpr"
|
|
||||||
}
|
|
||||||
|
|
||||||
type UnaryExpr struct {
|
type UnaryExpr struct {
|
||||||
startToken Token
|
startToken Token
|
||||||
expr Expr
|
expr Expr
|
||||||
|
|
@ -70,6 +66,15 @@ type BlockExpr struct {
|
||||||
expressions []Expr
|
expressions []Expr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e Identifier) Kind() string { return "Identifier" }
|
||||||
|
func (e AssignmentExpr) Kind() string { return "AssignmentExpr" }
|
||||||
|
func (e BinaryExpr) Kind() string { return "BinaryExpr" }
|
||||||
|
func (e UnaryExpr) Kind() string { return "UnaryExpr" }
|
||||||
|
func (e IfExpr) Kind() string { return "IfExpr" }
|
||||||
|
func (e ElseIfExpr) Kind() string { return "ElseIfExpr" }
|
||||||
|
func (e ElseExpr) Kind() string { return "ElseExpr" }
|
||||||
|
func (e BlockExpr) Kind() string { return "BlockExpr" }
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EOF = "EOF"
|
EOF = "EOF"
|
||||||
INDENT = "INDENT"
|
INDENT = "INDENT"
|
||||||
|
|
@ -120,6 +125,18 @@ var textOperators = map[string]bool{
|
||||||
"not": true,
|
"not": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generate(program []Expr) string {
|
||||||
|
var output string
|
||||||
|
|
||||||
|
for i := 0; i < len(program); i++ {
|
||||||
|
expr := program[i]
|
||||||
|
|
||||||
|
fmt.Println(expr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
func parse(tokens []Token) []Expr {
|
func parse(tokens []Token) []Expr {
|
||||||
var program []Expr
|
var program []Expr
|
||||||
|
|
||||||
|
|
@ -186,9 +203,6 @@ func parse(tokens []Token) []Expr {
|
||||||
condTokens := getCondition()
|
condTokens := getCondition()
|
||||||
blockTokens := getBlock()
|
blockTokens := getBlock()
|
||||||
|
|
||||||
fmt.Println("cond tokens: ", condTokens)
|
|
||||||
fmt.Println("block tokens:", blockTokens)
|
|
||||||
|
|
||||||
addExpr(IfExpr{
|
addExpr(IfExpr{
|
||||||
startToken: token,
|
startToken: token,
|
||||||
condition: parse(condTokens),
|
condition: parse(condTokens),
|
||||||
|
|
@ -201,9 +215,6 @@ func parse(tokens []Token) []Expr {
|
||||||
condTokens := getCondition()
|
condTokens := getCondition()
|
||||||
blockTokens := getBlock()
|
blockTokens := getBlock()
|
||||||
|
|
||||||
fmt.Println("cond tokens: ", condTokens)
|
|
||||||
fmt.Println("block tokens:", blockTokens)
|
|
||||||
|
|
||||||
addExpr(ElseIfExpr{
|
addExpr(ElseIfExpr{
|
||||||
startToken: token,
|
startToken: token,
|
||||||
condition: parse(condTokens),
|
condition: parse(condTokens),
|
||||||
|
|
@ -218,8 +229,6 @@ func parse(tokens []Token) []Expr {
|
||||||
|
|
||||||
blockTokens := getBlock()
|
blockTokens := getBlock()
|
||||||
|
|
||||||
fmt.Println("block tokens:", blockTokens)
|
|
||||||
|
|
||||||
addExpr(ElseExpr{
|
addExpr(ElseExpr{
|
||||||
startToken: token,
|
startToken: token,
|
||||||
block: BlockExpr{
|
block: BlockExpr{
|
||||||
|
|
@ -460,9 +469,9 @@ func main() {
|
||||||
fmt.Printf("%-15s │ %-22s │ %s\n", toPrint...)
|
fmt.Printf("%-15s │ %-22s │ %s\n", toPrint...)
|
||||||
}
|
}
|
||||||
|
|
||||||
parse(tokens)
|
program := parse(tokens)
|
||||||
|
|
||||||
// out := generate(tokens)
|
out := generate(program)
|
||||||
|
|
||||||
// fmt.Println(out)
|
fmt.Println(out)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue