From f73b38a0d8050f05fd0a7729067e0efaf6ec97cd Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 17 Mar 2020 03:49:15 +0200 Subject: [PATCH] vfmt2: fix literal integer number formatting --- vlib/v/ast/ast.v | 2 +- vlib/v/ast/str.v | 4 ++-- vlib/v/checker/checker.v | 4 ++-- vlib/v/fmt/fmt.v | 2 +- vlib/v/fmt/tests/integer_literal_keep.vv | 4 ++++ vlib/v/gen/cgen.v | 2 +- vlib/v/gen/jsgen.v | 2 +- vlib/v/parser/parser.v | 2 +- 8 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 vlib/v/fmt/tests/integer_literal_keep.vv diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index cf729e90cb..18ea54901d 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -40,7 +40,7 @@ pub: pub struct IntegerLiteral { pub: - val int + val string } pub struct FloatLiteral { diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index 7bc9176936..d6d43c82b0 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -69,9 +69,9 @@ pub fn (x Expr) str() string { */ IntegerLiteral { - return it.val.str() + return it.val } - IntegerLiteral { + StringLiteral { return '"$it.val"' } else { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 9afc131084..183ce50652 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -492,12 +492,12 @@ pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type { mut fixed_size := 1 match array_init.exprs[0] { ast.IntegerLiteral { - fixed_size = it.val + fixed_size = it.val.int() } else { c.error('expecting `int` for fixed size', array_init.pos) } - } + } idx := c.table.find_or_register_array_fixed(array_init.elem_type, fixed_size, 1) array_type := table.new_type(idx) array_init.typ = array_type diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index eaf5e05c51..df8c40d4df 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -433,7 +433,7 @@ fn (f mut Fmt) expr(node ast.Expr) { f.write(']') } ast.IntegerLiteral { - f.write(it.val.str()) + f.write(it.val) } ast.MapInit { f.writeln('{') diff --git a/vlib/v/fmt/tests/integer_literal_keep.vv b/vlib/v/fmt/tests/integer_literal_keep.vv new file mode 100644 index 0000000000..1c4cc43ba7 --- /dev/null +++ b/vlib/v/fmt/tests/integer_literal_keep.vv @@ -0,0 +1,4 @@ +fn main() { + x := 0xdeadbeef + u := 9978654321 +} diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 758bab22a1..f16b1f4965 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -747,7 +747,7 @@ fn (g mut Gen) expr(node ast.Expr) { g.infix_expr(it) } ast.IntegerLiteral { - g.write(it.val.str()) + g.write(it.val.int().str()) } ast.MatchExpr { g.match_expr(it) diff --git a/vlib/v/gen/jsgen.v b/vlib/v/gen/jsgen.v index 23be14a5c9..795a51afb8 100644 --- a/vlib/v/gen/jsgen.v +++ b/vlib/v/gen/jsgen.v @@ -108,7 +108,7 @@ fn (g mut JsGen) expr(node ast.Expr) { // println('cgen expr()') match node { ast.IntegerLiteral { - g.write(it.val.str()) + g.write(it.val) } ast.FloatLiteral { g.write(it.val) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 1de1dcbbd3..e2dfa368fd 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1310,7 +1310,7 @@ fn (p mut Parser) parse_number_literal() ast.Expr { } else { node = ast.IntegerLiteral{ - val: lit.int() + val: lit } } p.next()