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

handle floats

This commit is contained in:
Alexander Medvednikov 2019-12-27 10:03:29 +01:00
parent 1af274a714
commit d27c5eb345
4 changed files with 37 additions and 41 deletions

View File

@ -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

View File

@ -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 ')

View File

@ -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')

View File

@ -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('===================')
*/