os: fix get_raw_line() + minor v2 fixes

pull/3871/head
Alexander Medvednikov 2020-02-28 14:05:20 +01:00
parent c4b9ef388f
commit 7f5a15372f
6 changed files with 31 additions and 13 deletions

View File

@ -24,7 +24,7 @@ bmark.stop() // call when you want to finalize the benchmark
println( bmark.total_message('remarks about the benchmark') ) println( bmark.total_message('remarks about the benchmark') )
``` ```
benchmark.start() and b.measure() are convenience methods, benchmark.start() and b.measure() are convenience methods,
intended to be used in combination. Their goal is to make intended to be used in combination. Their goal is to make
benchmarking of small snippets of code as *short*, easy to benchmarking of small snippets of code as *short*, easy to
write, and then to read and analyze the results, as possible. write, and then to read and analyze the results, as possible.

View File

@ -704,14 +704,15 @@ pub fn get_raw_line() string {
} }
} $else { } $else {
max := size_t(0) max := size_t(0)
mut buf := byteptr(0) mut buf := charptr(0)
nr_chars := C.getline(&charptr(buf), &max, stdin) nr_chars := C.getline(&buf, &max, stdin)
defer { unsafe{ free(buf) } } //defer { unsafe{ free(buf) } }
if nr_chars == 0 || nr_chars == -1 { if nr_chars == 0 || nr_chars == -1 {
return '' return ''
} }
res := tos_clone( buf ) return tos3(buf)
return res //res := tos_clone(buf)
//return res
} }
} }

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
max_nr_errors = 30 max_nr_errors = 50
) )
pub struct Checker { pub struct Checker {

View File

@ -15,6 +15,7 @@ pub struct Eval {
mut: mut:
checker checker.Checker checker checker.Checker
vars map[string]Var vars map[string]Var
table &table.Table
} }
pub struct Var { pub struct Var {
@ -22,6 +23,7 @@ pub struct Var {
} }
pub fn (e mut Eval) eval(file ast.File, table &table.Table) string { pub fn (e mut Eval) eval(file ast.File, table &table.Table) string {
e.table = table
mut res := '' mut res := ''
e.checker = checker.new_checker(table) e.checker = checker.new_checker(table)
for stmt in file.stmts { for stmt in file.stmts {
@ -61,6 +63,9 @@ fn (e mut Eval) stmt(node ast.Stmt) string {
print_object(o) print_object(o)
return o.str() return o.str()
} }
// ast.StructDecl {
// println('s decl')
// }
ast.VarDecl { ast.VarDecl {
e.vars[it.name] = Var{ e.vars[it.name] = Var{
value: e.expr(it.expr) value: e.expr(it.expr)

View File

@ -762,7 +762,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
p.check(.rcbr) p.check(.rcbr)
} }
else { else {
p.error('pexpr(): bad token `$p.tok.str()`') p.error('parser: expr(): bad token `$p.tok.str()`')
} }
} }
// Infix // Infix

View File

@ -10,8 +10,11 @@ import (
) )
fn test_eval() { fn test_eval() {
/*
inputs := [ inputs := [
// //
'2+2',
'struct User { age int }',
'2+3', '2+3',
'4', '4',
'x := 10', 'x := 10',
@ -32,9 +35,11 @@ fn test_eval() {
'20', '20',
// //
] ]
/*
table := table.new_table() table := table.new_table()
mut scope := &ast.Scope{start_pos: 0, parent: 0} mut scope := &ast.Scope{
start_pos: 0
parent: 0
}
mut stmts := []ast.Stmt mut stmts := []ast.Stmt
for input in inputs { for input in inputs {
stmts << parse_stmt(input, table, scope) stmts << parse_stmt(input, table, scope)
@ -50,8 +55,9 @@ fn test_eval() {
println('eval done') println('eval done')
println(s) println(s)
assert s == expected.join('\n') assert s == expected.join('\n')
// exit(0) exit(0)
*/ */
return
} }
fn test_parse_file() { fn test_parse_file() {
@ -87,7 +93,10 @@ fn test_one() {
] ]
expected := 'int a = 10;int b = -a;int c = 20;' expected := 'int a = 10;int b = -a;int c = 20;'
table := table.new_table() table := table.new_table()
mut scope := &ast.Scope{start_pos: 0, parent: 0} mut scope := &ast.Scope{
start_pos: 0
parent: 0
}
mut e := []ast.Stmt mut e := []ast.Stmt
for line in input { for line in input {
e << parse_stmt(line, table, scope) e << parse_stmt(line, table, scope)
@ -179,7 +188,10 @@ fn test_parse_expr() {
mut e := []ast.Stmt mut e := []ast.Stmt
table := table.new_table() table := table.new_table()
mut checker := checker.new_checker(table) mut checker := checker.new_checker(table)
mut scope := &ast.Scope{start_pos: 0, parent: 0} mut scope := &ast.Scope{
start_pos: 0
parent: 0
}
for s in input { for s in input {
println('\n\nst="$s"') println('\n\nst="$s"')
e << parse_stmt(s, table, scope) e << parse_stmt(s, table, scope)