1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix octal and hex int literals

This commit is contained in:
Alexander Medvednikov 2020-03-26 17:14:24 +01:00
parent ed3e0c43bc
commit 75c2ef7947
4 changed files with 17 additions and 16 deletions

View File

@ -99,9 +99,7 @@ fn test_sort() {
} }
fn test_split_nth() { fn test_split_nth() {
a := "1,2,3" a := "1,2,3"
println(a)
assert (a.split(',').len == 3) assert (a.split(',').len == 3)
assert (a.split_nth(',', -1).len == 3) assert (a.split_nth(',', -1).len == 3)
assert (a.split_nth(',', 0).len == 3) assert (a.split_nth(',', 0).len == 3)
@ -109,7 +107,6 @@ fn test_split_nth() {
assert (a.split_nth(',', 2).len == 2) assert (a.split_nth(',', 2).len == 2)
assert (a.split_nth(',', 10).len == 3) assert (a.split_nth(',', 10).len == 3)
b := "1::2::3" b := "1::2::3"
println(b)
assert (b.split('::').len == 3) assert (b.split('::').len == 3)
assert (b.split_nth('::', -1).len == 3) assert (b.split_nth('::', -1).len == 3)
assert (b.split_nth('::', 0).len == 3) assert (b.split_nth('::', 0).len == 3)
@ -117,19 +114,16 @@ fn test_split_nth() {
assert (b.split_nth('::', 2).len == 2) assert (b.split_nth('::', 2).len == 2)
assert (b.split_nth('::', 10).len == 3) assert (b.split_nth('::', 10).len == 3)
c := "ABCDEF" c := "ABCDEF"
println(c)
println(c.split('').len) println(c.split('').len)
assert (c.split('').len == 6) assert (c.split('').len == 6)
assert (c.split_nth('', 3).len == 3) assert (c.split_nth('', 3).len == 3)
assert (c.split_nth('BC', -1).len == 2) assert (c.split_nth('BC', -1).len == 2)
d := "," d := ","
println(d)
assert (d.split(',').len == 2) assert (d.split(',').len == 2)
assert (d.split_nth('', 3).len == 1) assert (d.split_nth('', 3).len == 1)
assert (d.split_nth(',', -1).len == 2) assert (d.split_nth(',', -1).len == 2)
assert (d.split_nth(',', 3).len == 2) assert (d.split_nth(',', 3).len == 2)
e := ",,,0,,,,,a,,b," e := ",,,0,,,,,a,,b,"
println(e)
// assert (e.split(',,').len == 5) // assert (e.split(',,').len == 5)
// assert (e.split_nth(',,', 3).len == 2) // assert (e.split_nth(',,', 3).len == 2)
assert (e.split_nth(',', -1).len == 12) assert (e.split_nth(',', -1).len == 12)

View File

@ -1,5 +1,6 @@
fn test_utf8_char_len() { fn test_utf8_char_len() {
assert utf8_char_len(`a`) == 1 assert utf8_char_len(`a`) == 1
println(utf8_char_len(`a`))
s := 'п' s := 'п'
assert utf8_char_len(s[0]) == 2 assert utf8_char_len(s[0]) == 2
} }

View File

@ -13,7 +13,7 @@ struct CC {
mut: mut:
b BB b BB
nums []int nums []int
aarr []A aarr []AA
num int num int
} }
@ -82,7 +82,7 @@ fn test_struct_levels() {
assert c.b.a.nums[1] == 2 assert c.b.a.nums[1] == 2
c.b.a.nums [0] = 7 c.b.a.nums [0] = 7
assert c.b.a.nums[0] == 7 assert c.b.a.nums[0] == 7
c.aarr << A{val:8} c.aarr << AA{val:8}
assert c.aarr.len == 1 assert c.aarr.len == 1
assert c.aarr[0].val == 8 assert c.aarr[0].val == 8
c.num = 20 c.num = 20

View File

@ -270,14 +270,14 @@ fn (g mut Gen) stmt(node ast.Stmt) {
match node { match node {
ast.AssertStmt { ast.AssertStmt {
g.writeln('// assert') g.writeln('// assert')
g.write('if (!(') g.write('if ((')
g.expr(it.expr) g.expr(it.expr)
g.writeln(')) {') g.writeln(')) {')
g.writeln('g_test_fails++;')
g.writeln('puts("FAILED $g.fn_decl.name $it.pos.line_nr");')
g.writeln('} else {')
g.writeln('g_test_oks++;') g.writeln('g_test_oks++;')
// g.writeln('puts("OK $g.fn_decl.name");') // g.writeln('puts("OK $g.fn_decl.name");')
g.writeln('} else {')
g.writeln('g_test_fails++;')
g.writeln('puts("FAILED $g.fn_decl.name $it.pos.line_nr");')
g.writeln('}') g.writeln('}')
} }
ast.AssignStmt { ast.AssignStmt {
@ -952,7 +952,13 @@ fn (g mut Gen) expr(node ast.Expr) {
g.infix_expr(it) g.infix_expr(it)
} }
ast.IntegerLiteral { ast.IntegerLiteral {
g.write(it.val.int().str()) if it.val.starts_with('0o') {
g.write('0')
g.write(it.val[2..])
}
else {
g.write(it.val) // .int().str())
}
} }
ast.MatchExpr { ast.MatchExpr {
g.match_expr(it) g.match_expr(it)