v.eval: fix hardcoded math ops in infix_gen.v, add interpret_test.v

pull/13012/head
Delyan Angelov 2022-01-02 10:33:21 +02:00
parent 1521d08e84
commit 41078bc438
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
7 changed files with 711 additions and 641 deletions

View File

@ -22,9 +22,9 @@ fn(e Eval)infix_expr(left Object,right Object,op token.Kind,expecting ast.Type)O
}
math_ops = {
'plus': '+'
'minus': '*'
'mul': '+'
'div': '+'
'minus': '-'
'mul': '*'
'div': '/'
'right_shift': '>>'
'left_shift': '<<'
}
@ -77,7 +77,7 @@ fn main() {
continue
}
unsafe_start, unsafe_end := if op in ['<<', '>>'] { 'unsafe{', '}' } else { '', '' }
b.write_string('$ct2{if expecting in ast.signed_integer_type_idxs{return Int{i64(left.val)+i64(right.val),i8(e.type_to_size(expecting))}}else if expecting in ast.unsigned_integer_type_idxs{return Uint{u64(left.val)+u64(right.val),i8(e.type_to_size(expecting))}}else if expecting==ast.int_literal_type_idx{${unsafe_start}return i64(i64(left.val)${op}i64(right.val))$unsafe_end}')
b.write_string('$ct2{if expecting in ast.signed_integer_type_idxs{return Int{i64(left.val)${op}i64(right.val),i8(e.type_to_size(expecting))}}else if expecting in ast.unsigned_integer_type_idxs{return Uint{u64(left.val)${op}u64(right.val),i8(e.type_to_size(expecting))}}else if expecting==ast.int_literal_type_idx{${unsafe_start}return i64(i64(left.val)${op}i64(right.val))$unsafe_end}')
if op !in ['<<', '>>'] {
b.write_string('else if expecting in ast.float_type_idxs{return Float{f64(left.val)${op}f64(right.val), i8(e.type_to_size(expecting))}}else if expecting==ast.float_literal_type_idx{return f64(f64(left.val)${op}f64(right.val))}')
}
@ -88,7 +88,7 @@ fn main() {
continue
}
unsafe_start, unsafe_end := if op in ['<<', '>>'] { 'unsafe{', '}' } else { '', '' }
b.write_string('$lt2{if expecting in ast.signed_integer_type_idxs{return Int{i64(left.val)+i64(right),i8(e.type_to_size(expecting))}}else if expecting in ast.unsigned_integer_type_idxs{return Uint{u64(left.val)+u64(right),i8(e.type_to_size(expecting))}}else if expecting==ast.int_literal_type_idx{${unsafe_start}return i64(i64(left.val)${op}i64(right))$unsafe_end}')
b.write_string('$lt2{if expecting in ast.signed_integer_type_idxs{return Int{i64(left.val)${op}i64(right),i8(e.type_to_size(expecting))}}else if expecting in ast.unsigned_integer_type_idxs{return Uint{u64(left.val)${op}u64(right),i8(e.type_to_size(expecting))}}else if expecting==ast.int_literal_type_idx{${unsafe_start}return i64(i64(left.val)${op}i64(right))$unsafe_end}')
if op !in ['<<', '>>'] {
b.write_string('else if expecting in ast.float_type_idxs{return Float{f64(left.val)${op}f64(right), i8(e.type_to_size(expecting))}}else if expecting==ast.float_literal_type_idx{return f64(f64(left.val)${op}f64(right))}')
}
@ -106,7 +106,7 @@ fn main() {
continue
}
unsafe_start, unsafe_end := if op in ['<<', '>>'] { 'unsafe{', '}' } else { '', '' }
b.write_string('$ct2{if expecting in ast.signed_integer_type_idxs{return Int{i64(left)+i64(right.val),i8(e.type_to_size(expecting))}}else if expecting in ast.unsigned_integer_type_idxs{return Uint{u64(left)+u64(right.val),i8(e.type_to_size(expecting))}}else if expecting==ast.int_literal_type_idx{${unsafe_start}return i64(i64(left)${op}i64(right.val))$unsafe_end}')
b.write_string('$ct2{if expecting in ast.signed_integer_type_idxs{return Int{i64(left)${op}i64(right.val),i8(e.type_to_size(expecting))}}else if expecting in ast.unsigned_integer_type_idxs{return Uint{u64(left)${op}u64(right.val),i8(e.type_to_size(expecting))}}else if expecting==ast.int_literal_type_idx{${unsafe_start}return i64(i64(left)${op}i64(right.val))$unsafe_end}')
if op !in ['<<', '>>'] {
b.write_string('else if expecting in ast.float_type_idxs{return Float{f64(left)${op}f64(right.val), i8(e.type_to_size(expecting))}}else if expecting==ast.float_literal_type_idx{return f64(f64(left)${op}f64(right.val))}')
}
@ -117,7 +117,7 @@ fn main() {
continue
}
unsafe_start, unsafe_end := if op in ['<<', '>>'] { 'unsafe{', '}' } else { '', '' }
b.write_string('$lt2{if expecting in ast.signed_integer_type_idxs{return Int{i64(left)+i64(right),i8(e.type_to_size(expecting))}}else if expecting in ast.unsigned_integer_type_idxs{return Uint{u64(left)+u64(right),i8(e.type_to_size(expecting))}}else if expecting==ast.int_literal_type_idx{${unsafe_start}return i64(i64(left)${op}i64(right))$unsafe_end}')
b.write_string('$lt2{if expecting in ast.signed_integer_type_idxs{return Int{i64(left)${op}i64(right),i8(e.type_to_size(expecting))}}else if expecting in ast.unsigned_integer_type_idxs{return Uint{u64(left)${op}u64(right),i8(e.type_to_size(expecting))}}else if expecting==ast.int_literal_type_idx{${unsafe_start}return i64(i64(left)${op}i64(right))$unsafe_end}')
if op !in ['<<', '>>'] {
b.write_string('else if expecting in ast.float_type_idxs{return Float{f64(left)${op}f64(right), i8(e.type_to_size(expecting))}}else if expecting==ast.float_literal_type_idx{return f64(f64(left)${op}f64(right))}')
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,64 @@
import os
import benchmark
import term
const is_verbose = os.getenv('VTEST_SHOW_CMD') != ''
fn test_interpret() ? {
mut bench := benchmark.new_benchmark()
vexe := os.getenv('VEXE')
vroot := os.dir(vexe)
os.chdir(vroot) ?
dir := os.join_path(vroot, 'vlib/v/eval/testdata')
files := os.ls(dir) ?
//
tests := files.filter(it.ends_with('.vv'))
if tests.len == 0 {
println('no interpreter tests found')
assert false
}
bench.set_total_expected_steps(tests.len)
for test in tests {
test_name_without_postfix := test.replace('.vv', '')
bench.step()
full_test_path := os.real_path(os.join_path(dir, test))
test_file_name := os.file_name(test)
relative_test_path := full_test_path.replace(vroot + '/', '')
cmd := '"$vexe" interpret "$full_test_path"'
if is_verbose {
println(cmd)
}
res := os.execute(cmd)
if res.exit_code != 0 {
bench.fail()
eprintln(bench.step_message_fail('$full_test_path failed to run'))
eprintln(res.output)
continue
}
mut expected := os.read_file('$dir/${test_name_without_postfix}.out') ?
expected = normalise_line_endings(expected)
mut found := normalise_line_endings(res.output)
found = found.trim_space()
if expected != found {
println(term.red('FAIL'))
println('========================================================\n')
println('============ expected len=$expected.len: "$expected"')
println('============ found len=$found.len: "$found"')
println('========================================================\n')
bench.fail()
continue
}
bench.ok()
eprintln(bench.step_message_ok(relative_test_path))
}
bench.stop()
eprintln(term.h_divider('-'))
eprintln(bench.total_message('native'))
if bench.nfail > 0 {
exit(1)
}
}
fn normalise_line_endings(s string) string {
return s.trim_right('\r\n').replace('\r\n', '\n')
}

1
vlib/v/eval/testdata/hello.out vendored 100644
View File

@ -0,0 +1 @@
Hello, World!

1
vlib/v/eval/testdata/hello.vv vendored 100644
View File

@ -0,0 +1 @@
println('Hello, World!')

View File

@ -0,0 +1 @@
a: 5 | res: 50

View File

@ -0,0 +1,3 @@
a := 5
println('a: $a | res: ${a*10}')