assert: fix line position, pretty print float literals and casts
parent
3bad02abdd
commit
ef19aa1de6
|
@ -626,6 +626,7 @@ pub:
|
||||||
expr Expr // `buf`
|
expr Expr // `buf`
|
||||||
arg Expr // `n` in `string(buf, n)`
|
arg Expr // `n` in `string(buf, n)`
|
||||||
typ table.Type // `string`
|
typ table.Type // `string`
|
||||||
|
typname string
|
||||||
mut:
|
mut:
|
||||||
expr_type table.Type // `byteptr`
|
expr_type table.Type // `byteptr`
|
||||||
has_arg bool
|
has_arg bool
|
||||||
|
|
|
@ -72,7 +72,7 @@ pub fn (x Expr) str() string {
|
||||||
return it.name
|
return it.name
|
||||||
}
|
}
|
||||||
InfixExpr {
|
InfixExpr {
|
||||||
return '(${it.left.str()} $it.op.str() ${it.right.str()})'
|
return '${it.left.str()} $it.op.str() ${it.right.str()}'
|
||||||
}
|
}
|
||||||
PrefixExpr {
|
PrefixExpr {
|
||||||
return it.op.str() + it.right.str()
|
return it.op.str() + it.right.str()
|
||||||
|
@ -80,6 +80,9 @@ pub fn (x Expr) str() string {
|
||||||
IntegerLiteral {
|
IntegerLiteral {
|
||||||
return it.val
|
return it.val
|
||||||
}
|
}
|
||||||
|
FloatLiteral {
|
||||||
|
return it.val
|
||||||
|
}
|
||||||
StringLiteral {
|
StringLiteral {
|
||||||
return '"$it.val"'
|
return '"$it.val"'
|
||||||
}
|
}
|
||||||
|
@ -113,6 +116,9 @@ pub fn (x Expr) str() string {
|
||||||
IndexExpr {
|
IndexExpr {
|
||||||
return '${it.left.str()}[${it.index.str()}]'
|
return '${it.left.str()}[${it.index.str()}]'
|
||||||
}
|
}
|
||||||
|
CastExpr {
|
||||||
|
return '${it.typname}(${it.expr.str()})'
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
return '[unhandled expr type ${typeof(x)}]'
|
return '[unhandled expr type ${typeof(x)}]'
|
||||||
}
|
}
|
||||||
|
|
|
@ -676,7 +676,11 @@ fn (c mut Checker) stmt(node ast.Stmt) {
|
||||||
// c.expected_type = table.void_type
|
// c.expected_type = table.void_type
|
||||||
match mut node {
|
match mut node {
|
||||||
ast.AssertStmt {
|
ast.AssertStmt {
|
||||||
c.expr(it.expr)
|
assert_type := c.expr(it.expr)
|
||||||
|
if assert_type != table.bool_type_idx {
|
||||||
|
atype_name := c.table.get_type_symbol(assert_type).name
|
||||||
|
c.error('assert can be used only with `bool` expressions, but found `${atype_name}` instead', it.pos)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ast.AssignStmt {
|
ast.AssignStmt {
|
||||||
c.assign_stmt(mut it)
|
c.assign_stmt(mut it)
|
||||||
|
@ -875,6 +879,7 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
|
||||||
if it.has_arg {
|
if it.has_arg {
|
||||||
c.expr(it.arg)
|
c.expr(it.arg)
|
||||||
}
|
}
|
||||||
|
it.typname = c.table.get_type_symbol(it.typ).name
|
||||||
return it.typ
|
return it.typ
|
||||||
}
|
}
|
||||||
ast.CallExpr {
|
ast.CallExpr {
|
||||||
|
|
|
@ -611,11 +611,10 @@ fn (g mut Gen) gen_assert_stmt(a ast.AssertStmt) {
|
||||||
if g.is_test {
|
if g.is_test {
|
||||||
g.writeln('{')
|
g.writeln('{')
|
||||||
g.writeln(' g_test_oks++;')
|
g.writeln(' g_test_oks++;')
|
||||||
g.writeln(' cb_assertion_ok( _STR("${mod_path}"), ${a.pos.line_nr}, _STR("assert ${s_assertion}"), _STR("${g.fn_decl.name}()") );')
|
g.writeln(' cb_assertion_ok( _STR("${mod_path}"), ${a.pos.line_nr+1}, _STR("assert ${s_assertion}"), _STR("${g.fn_decl.name}()") );')
|
||||||
// g.writeln(' println(_STR("OK ${mod_path}:${a.pos.line_nr}: fn ${g.fn_decl.name}(): assert $s_assertion"));')
|
|
||||||
g.writeln('}else{')
|
g.writeln('}else{')
|
||||||
g.writeln(' g_test_fails++;')
|
g.writeln(' g_test_fails++;')
|
||||||
g.writeln(' cb_assertion_failed( _STR("${mod_path}"), ${a.pos.line_nr}, _STR("assert ${s_assertion}"), _STR("${g.fn_decl.name}()") );')
|
g.writeln(' cb_assertion_failed( _STR("${mod_path}"), ${a.pos.line_nr+1}, _STR("assert ${s_assertion}"), _STR("${g.fn_decl.name}()") );')
|
||||||
g.writeln(' exit(1);')
|
g.writeln(' exit(1);')
|
||||||
g.writeln(' // TODO')
|
g.writeln(' // TODO')
|
||||||
g.writeln(' // Maybe print all vars in a test function if it fails?')
|
g.writeln(' // Maybe print all vars in a test function if it fails?')
|
||||||
|
@ -623,7 +622,7 @@ fn (g mut Gen) gen_assert_stmt(a ast.AssertStmt) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
g.writeln('{}else{')
|
g.writeln('{}else{')
|
||||||
g.writeln(' eprintln(_STR("${mod_path}:${a.pos.line_nr}: FAIL: fn ${g.fn_decl.name}(): assert $s_assertion"));')
|
g.writeln(' eprintln(_STR("${mod_path}:${a.pos.line_nr+1}: FAIL: fn ${g.fn_decl.name}(): assert $s_assertion"));')
|
||||||
g.writeln(' exit(1);')
|
g.writeln(' exit(1);')
|
||||||
g.writeln('}')
|
g.writeln('}')
|
||||||
}
|
}
|
||||||
|
|
|
@ -354,10 +354,11 @@ pub fn (p mut Parser) stmt() ast.Stmt {
|
||||||
}
|
}
|
||||||
.key_assert {
|
.key_assert {
|
||||||
p.next()
|
p.next()
|
||||||
|
assert_pos := p.tok.position()
|
||||||
expr := p.expr(0)
|
expr := p.expr(0)
|
||||||
return ast.AssertStmt{
|
return ast.AssertStmt{
|
||||||
expr: expr
|
expr: expr
|
||||||
pos: p.tok.position()
|
pos: assert_pos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.key_mut, .key_static {
|
.key_mut, .key_static {
|
||||||
|
|
|
@ -724,6 +724,10 @@ pub fn (s mut Scanner) scan() token.Token {
|
||||||
s.ignore_line()
|
s.ignore_line()
|
||||||
s.line_comment = s.text[start + 1..s.pos]
|
s.line_comment = s.text[start + 1..s.pos]
|
||||||
comment := s.line_comment.trim_space()
|
comment := s.line_comment.trim_space()
|
||||||
|
s.pos--
|
||||||
|
// fix line_nr, \n was read, and the comment is marked
|
||||||
|
// on the next line
|
||||||
|
s.line_nr--
|
||||||
if s.comments_mode == .parse_comments {
|
if s.comments_mode == .parse_comments {
|
||||||
// Find out if this comment is on its own line (for vfmt)
|
// Find out if this comment is on its own line (for vfmt)
|
||||||
mut is_separate_line_comment := true
|
mut is_separate_line_comment := true
|
||||||
|
@ -735,10 +739,6 @@ pub fn (s mut Scanner) scan() token.Token {
|
||||||
if is_separate_line_comment {
|
if is_separate_line_comment {
|
||||||
comment = '|' + comment
|
comment = '|' + comment
|
||||||
}
|
}
|
||||||
s.pos--
|
|
||||||
// fix line_nr, \n was read, and the comment is marked
|
|
||||||
// on the next line
|
|
||||||
s.line_nr--
|
|
||||||
return s.new_token(.comment, comment)
|
return s.new_token(.comment, comment)
|
||||||
}
|
}
|
||||||
// s.fgenln('// ${s.prev_tok.str()} "$s.line_comment"')
|
// s.fgenln('// ${s.prev_tok.str()} "$s.line_comment"')
|
||||||
|
|
|
@ -39,5 +39,5 @@ fn test_scan() {
|
||||||
assert e == 120.0
|
assert e == 120.0
|
||||||
assert 1.23e+10 == 1.23e10
|
assert 1.23e+10 == 1.23e10
|
||||||
assert 1.23e+10 == 1.23e0010
|
assert 1.23e+10 == 1.23e0010
|
||||||
assert -1.23e+10 == 1.23e0010 * -1.0
|
assert (-1.23e+10) == (1.23e0010 * -1.0)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue