From d27c5eb3458e4bdcd339bbb275849eb3217c0846 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 27 Dec 2019 10:03:29 +0100 Subject: [PATCH] handle floats --- vlib/v/ast/ast.v | 8 +++++- vlib/v/cgen/cgen.v | 3 +++ vlib/v/parser/parser.v | 14 +++++++--- vlib/v/parser/parser_test.v | 53 +++++++++++-------------------------- 4 files changed, 37 insertions(+), 41 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index a3646f1b3e..643e24491c 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -12,7 +12,7 @@ import ( struct Foo {} pub type Expr = Foo | IfExpr | BinaryExpr | UnaryExpr | - StringLiteral | IntegerLiteral | VarDecl + StringLiteral | IntegerLiteral | FloatLiteral | VarDecl pub type Stmt = Foo | Foo //VarDecl @@ -21,6 +21,12 @@ pub: val int } +pub struct FloatLiteral { +pub: + //val f64 + val string +} + pub struct StringLiteral { pub: val string diff --git a/vlib/v/cgen/cgen.v b/vlib/v/cgen/cgen.v index c93f3c75db..22b2f62331 100644 --- a/vlib/v/cgen/cgen.v +++ b/vlib/v/cgen/cgen.v @@ -47,6 +47,9 @@ fn (g mut Gen) expr(node ast.Expr) { ast.IntegerLiteral { g.write(it.val.str()) } + ast.FloatLiteral { + g.write(it.val) + } ast.UnaryExpr { g.expr(it.left) g.write(' $it.op ') diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 94e0045e31..5cc7238574 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -127,10 +127,18 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) { typ = types.string_type } if tok == .number { - node = ast.IntegerLiteral{ - val: lit.int() + if lit.contains('.') { + node = ast.FloatLiteral{ + //val: lit.f64() + val: lit + } + typ = types.int_type + } else { + node = ast.IntegerLiteral{ + val: lit.int() + } + typ = types.int_type } - typ = types.int_type } // else { // verror('bad scalar token') diff --git a/vlib/v/parser/parser_test.v b/vlib/v/parser/parser_test.v index 635abca15f..706d8f4ccc 100644 --- a/vlib/v/parser/parser_test.v +++ b/vlib/v/parser/parser_test.v @@ -6,34 +6,6 @@ import ( v.table ) -/* -fn test_parser() { - if true { return } - text_expr := [ - '1 += 2', - '1.2 + 3.4', - '4 + 4', - '1 + 2 * 5', - '(2 * 3) / 2', - '3 + (7 * 6)', - '2 ^ 8 * (7 * 6)', - '20 + (10 * 15) / 5', // 50 - '(2) + (17*2-30) * (5)+2 - (8/2)*4', // 8 - '2 + "hi"', - 'x := 10', - ] - - table := &table.Table{} - for s in text_expr { - // print using str method - x := parse_expr(s, table) - println('source: $s') - println('parsed: $x') - println('===================') - } -} - -*/ fn test_parse_file() { s := '12 + 3 x := 10 @@ -44,7 +16,6 @@ fn test_parse_file() { prog := parse_file(s, table) res := cgen.gen(prog) println(res) - println('done') } @@ -59,17 +30,16 @@ fn test_parse_expr() { 's := "hi"', '1 += 2', - //'1.2 + 3.4', - /* + '1.2 + 3.4', '4 + 4', '1 + 2 * 5', + /* '(2 * 3) / 2', '3 + (7 * 6)', '2 ^ 8 * (7 * 6)', '20 + (10 * 15) / 5', // 50 '(2) + (17*2-30) * (5)+2 - (8/2)*4', // 8 - '2 + "hi"', - 'x := 10', + //'2 + "hi"', */ ] expecting := [ @@ -81,10 +51,10 @@ fn test_parse_expr() { 'int ab = 10 + 3 * 9;', 'string s = tos3("hi");', '1 += 2', - //'1.2 + 3.4', + '1.2 + 3.4', + '4 + 4', + '1 + 2 * 5', ] - //expr := parse_expr('3 + 7 * 2') - //expr2 := parse_stmt('a := 3 + "f"') mut e := []ast.Expr table := &table.Table{} for s in input { @@ -108,6 +78,15 @@ fn test_parse_expr() { break } } - //cgen.save() } +/* + table := &table.Table{} + for s in text_expr { + // print using str method + x := parse_expr(s, table) + println('source: $s') + println('parsed: $x') + println('===================') +*/ +