2019-12-22 02:34:37 +01:00
|
|
|
module parser
|
|
|
|
|
2019-12-24 18:54:43 +01:00
|
|
|
import (
|
|
|
|
compiler2.ast
|
|
|
|
compiler2.cgen
|
|
|
|
)
|
2019-12-22 02:34:37 +01:00
|
|
|
|
|
|
|
fn test_parser() {
|
2019-12-26 10:02:38 +01:00
|
|
|
//if true { return }
|
2019-12-22 02:34:37 +01:00
|
|
|
//expr := ast.IntegerExpr {val:10}
|
|
|
|
//expr := ast.BinaryExpr{}
|
2019-12-25 13:39:58 +01:00
|
|
|
|
|
|
|
// print using walk
|
2019-12-22 02:34:37 +01:00
|
|
|
expr := parse_expr('3 + 7')
|
2019-12-25 13:39:58 +01:00
|
|
|
println('\n')
|
2019-12-24 18:54:43 +01:00
|
|
|
|
2019-12-25 13:39:58 +01:00
|
|
|
text_expr := [
|
2019-12-26 03:40:18 +01:00
|
|
|
'1 += 2',
|
|
|
|
'1.2 + 3.4',
|
2019-12-25 13:39:58 +01:00
|
|
|
'4 + 4',
|
|
|
|
'1 + 2 * 5',
|
|
|
|
'(2 * 3) / 2',
|
|
|
|
'3 + (7 * 6)',
|
|
|
|
'2 ^ 8 * (7 * 6)',
|
2019-12-26 03:40:18 +01:00
|
|
|
'20 + (10 * 15) / 5', // 50
|
2019-12-26 10:02:38 +01:00
|
|
|
'(2) + (17*2-30) * (5)+2 - (8/2)*4', // 8
|
|
|
|
'2 + "hi"'
|
2019-12-25 13:39:58 +01:00
|
|
|
]
|
2019-12-26 10:02:38 +01:00
|
|
|
|
2019-12-25 13:39:58 +01:00
|
|
|
for s in text_expr {
|
|
|
|
// print using str method
|
|
|
|
x := parse_expr(s)
|
|
|
|
println('source: $s')
|
|
|
|
println('parsed: $x')
|
|
|
|
println('===================')
|
|
|
|
}
|
2019-12-22 02:34:37 +01:00
|
|
|
}
|
|
|
|
|
2019-12-25 13:39:58 +01:00
|
|
|
|
2019-12-26 10:02:38 +01:00
|
|
|
fn test_cgen() {
|
2019-12-24 18:54:43 +01:00
|
|
|
//expr := parse_expr('3 + 7 * 2')
|
2019-12-26 10:02:38 +01:00
|
|
|
//expr2 := parse_stmt('a := 3 + "f"')
|
2019-12-26 11:21:41 +01:00
|
|
|
expr2 := parse_expr('2 + "helo"')
|
2019-12-24 18:54:43 +01:00
|
|
|
program := ast.Program{
|
|
|
|
exprs: [
|
2019-12-26 10:02:38 +01:00
|
|
|
expr2,
|
2019-12-24 18:54:43 +01:00
|
|
|
//parse_expr('2 * 2'),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
cgen.gen(program)
|
|
|
|
//cgen.save()
|
|
|
|
}
|
|
|
|
|