v/vlib/v/parser/v_parser_test.v

189 lines
3.5 KiB
V
Raw Normal View History

2019-12-22 02:34:37 +01:00
module parser
2020-06-27 15:00:28 +02:00
// import v.eval
2020-04-26 13:49:31 +02:00
import v.ast
import v.gen
import v.table
import v.checker
import v.pref
import term
2019-12-22 02:34:37 +01:00
fn test_eval() {
/*
inputs := [
//
'2+2',
'struct User { age int }',
'2+3',
'4',
'x := 10',
'x',
'x + 1',
'y := 2',
'x * y', // 20
//
]
expected := [
//
'5',
'4',
'>>',
'10',
'11',
'>>',
'20',
//
]
table := table.new_table()
2020-04-04 02:03:41 +02:00
vpref := &pref.Preferences{}
mut scope := &ast.Scope{
start_pos: 0
parent: 0
}
2020-04-26 16:25:54 +02:00
mut stmts := []ast.Stmt{}
for input in inputs {
stmts << parse_stmt(input, table, scope)
}
file := ast.File{
stmts: stmts
scope: scope
}
2020-04-04 02:03:41 +02:00
mut checker := checker.new_checker(table, vpref)
checker.check(file)
mut ev := eval.Eval{}
s := ev.eval(file, table)
println('eval done')
println(s)
assert s == expected.join('\n')
exit(0)
*/
return
}
2019-12-27 09:35:30 +01:00
fn test_parse_file() {
2019-12-28 14:11:05 +01:00
if true {
return
}
2019-12-27 13:57:49 +01:00
s := '
2019-12-27 18:55:51 +01:00
fn foo() int {
2019-12-27 13:57:49 +01:00
f := 23
return 10+4
}
12 + 3
x := 10
5+7
8+4
'
table := &table.Table{}
2020-03-30 17:21:32 +02:00
vpref := &pref.Preferences{}
2020-06-27 15:00:28 +02:00
gscope := &ast.Scope{
parent: 0
}
prog := parse_file(s, table, .skip_comments, vpref, gscope)
2020-04-04 02:03:41 +02:00
mut checker := checker.new_checker(table, vpref)
checker.check(prog)
2020-03-30 17:21:32 +02:00
res := gen.cgen([prog], table, vpref)
2019-12-27 09:35:30 +01:00
println(res)
}
2019-12-22 02:34:37 +01:00
2020-01-06 16:13:12 +01:00
fn test_one() {
2020-03-21 11:55:49 +01:00
if true {
return
}
2020-01-06 16:13:12 +01:00
println('\n\ntest_one()')
2020-06-27 15:00:28 +02:00
input := ['a := 10', 'b := -a', 'c := 20']
2020-01-06 16:13:12 +01:00
expected := 'int a = 10;int b = -a;int c = 20;'
table := table.new_table()
2020-03-30 17:21:32 +02:00
vpref := &pref.Preferences{}
scope := &ast.Scope{
start_pos: 0
parent: 0
}
2020-04-26 16:25:54 +02:00
mut e := []ast.Stmt{}
2020-01-06 16:13:12 +01:00
for line in input {
e << parse_stmt(line, table, scope)
2020-01-06 16:13:12 +01:00
}
program := ast.File{
stmts: e
scope: scope
global_scope: scope
2020-01-06 16:13:12 +01:00
}
2020-04-04 02:03:41 +02:00
mut checker := checker.new_checker(table, vpref)
checker.check(program)
2020-03-30 17:21:32 +02:00
res := gen.cgen([program], table, vpref).replace('\n', '').trim_space().after('#endif')
println(res)
2020-01-06 16:13:12 +01:00
ok := expected == res
println(res)
assert ok
2020-06-27 15:00:28 +02:00
if !ok {
}
2020-01-06 16:13:12 +01:00
// exit(0)
}
fn test_parse_expr() {
2020-03-21 11:55:49 +01:00
if true {
return
}
input := ['1 == 1', '234234', '2 * 8 + 3', 'a := 3', 'a++', 'b := 4 + 2', 'neg := -a', 'a + a',
'bo := 2 + 3 == 5', '2 + 1', 'q := 1', 'q + 777', '2 + 3', '2+2*4', 'x := 10', 'mut aa := 12',
'ab := 10 + 3 * 9', 's := "hi"', 'x = 11', 'a += 10', '1.2 + 3.4', '4 + 4', '1 + 2 * 5', '-a+1',
'2+2',
]
expecting := ['1 == 1;', '234234;', '2 * 8 + 3;', 'int a = 3;', 'a++;', 'int b = 4 + 2;', 'int neg = -a;',
'a + a;', 'bool bo = 2 + 3 == 5;', '2 + 1;', 'int q = 1;', 'q + 777;', '2 + 3;', '2 + 2 * 4;',
'int x = 10;', 'int aa = 12;', 'int ab = 10 + 3 * 9;', 'string s = tos3("hi");', 'x = 11;', 'a += 10;',
'1.2 + 3.4;', '4 + 4;', '1 + 2 * 5;', '-a + 1;', '2 + 2;']
2020-04-26 16:25:54 +02:00
mut e := []ast.Stmt{}
table := table.new_table()
2020-03-30 17:21:32 +02:00
vpref := &pref.Preferences{}
2020-04-04 02:03:41 +02:00
mut checker := checker.new_checker(table, vpref)
2020-03-30 17:21:32 +02:00
scope := &ast.Scope{
start_pos: 0
parent: 0
}
for s in input {
println('\n\nst="$s"')
e << parse_stmt(s, table, scope)
2019-12-28 14:11:05 +01:00
}
2019-12-30 12:10:46 +01:00
program := ast.File{
2019-12-28 14:11:05 +01:00
stmts: e
scope: scope
global_scope: scope
2019-12-24 18:54:43 +01:00
}
checker.check(program)
2020-03-30 17:21:32 +02:00
res := gen.cgen([program], table, vpref).after('#endif')
println('========')
println(res)
println('========')
2019-12-27 09:35:30 +01:00
lines := res.trim_space().split_into_lines()
mut i := 0
for line in lines {
if line == '' {
continue
}
2020-01-06 16:13:12 +01:00
if line != expecting[i] {
println('V:"$line" expecting:"${expecting[i]}"')
}
2019-12-27 09:35:30 +01:00
assert line == expecting[i]
2020-01-06 16:13:12 +01:00
println(term.green('$i OK'))
println(line)
println('')
i++
2019-12-27 09:35:30 +01:00
if i >= expecting.len {
break
}
}
2019-12-24 18:54:43 +01:00
}
2019-12-27 10:03:29 +01:00
/*
2020-06-27 15:00:28 +02:00
table := &table.Table{}
for s in text_expr {
// print using str method
x := parse_expr(s, table)
println('source: $s')
println('parsed: $x')
println('===================')
}
2019-12-27 10:03:29 +01:00
*/