handle floats

pull/3229/head
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 {} struct Foo {}
pub type Expr = Foo | IfExpr | BinaryExpr | UnaryExpr | pub type Expr = Foo | IfExpr | BinaryExpr | UnaryExpr |
StringLiteral | IntegerLiteral | VarDecl StringLiteral | IntegerLiteral | FloatLiteral | VarDecl
pub type Stmt = Foo | Foo //VarDecl pub type Stmt = Foo | Foo //VarDecl
@ -21,6 +21,12 @@ pub:
val int val int
} }
pub struct FloatLiteral {
pub:
//val f64
val string
}
pub struct StringLiteral { pub struct StringLiteral {
pub: pub:
val string val string

View File

@ -47,6 +47,9 @@ fn (g mut Gen) expr(node ast.Expr) {
ast.IntegerLiteral { ast.IntegerLiteral {
g.write(it.val.str()) g.write(it.val.str())
} }
ast.FloatLiteral {
g.write(it.val)
}
ast.UnaryExpr { ast.UnaryExpr {
g.expr(it.left) g.expr(it.left)
g.write(' $it.op ') g.write(' $it.op ')

View File

@ -127,11 +127,19 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
typ = types.string_type typ = types.string_type
} }
if tok == .number { if tok == .number {
if lit.contains('.') {
node = ast.FloatLiteral{
//val: lit.f64()
val: lit
}
typ = types.int_type
} else {
node = ast.IntegerLiteral{ node = ast.IntegerLiteral{
val: lit.int() val: lit.int()
} }
typ = types.int_type typ = types.int_type
} }
}
// else { // else {
// verror('bad scalar token') // verror('bad scalar token')
// } // }

View File

@ -6,34 +6,6 @@ import (
v.table 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() { fn test_parse_file() {
s := '12 + 3 s := '12 + 3
x := 10 x := 10
@ -44,7 +16,6 @@ fn test_parse_file() {
prog := parse_file(s, table) prog := parse_file(s, table)
res := cgen.gen(prog) res := cgen.gen(prog)
println(res) println(res)
println('done')
} }
@ -59,17 +30,16 @@ fn test_parse_expr() {
's := "hi"', 's := "hi"',
'1 += 2', '1 += 2',
//'1.2 + 3.4', '1.2 + 3.4',
/*
'4 + 4', '4 + 4',
'1 + 2 * 5', '1 + 2 * 5',
/*
'(2 * 3) / 2', '(2 * 3) / 2',
'3 + (7 * 6)', '3 + (7 * 6)',
'2 ^ 8 * (7 * 6)', '2 ^ 8 * (7 * 6)',
'20 + (10 * 15) / 5', // 50 '20 + (10 * 15) / 5', // 50
'(2) + (17*2-30) * (5)+2 - (8/2)*4', // 8 '(2) + (17*2-30) * (5)+2 - (8/2)*4', // 8
'2 + "hi"', //'2 + "hi"',
'x := 10',
*/ */
] ]
expecting := [ expecting := [
@ -81,10 +51,10 @@ fn test_parse_expr() {
'int ab = 10 + 3 * 9;', 'int ab = 10 + 3 * 9;',
'string s = tos3("hi");', 'string s = tos3("hi");',
'1 += 2', '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 mut e := []ast.Expr
table := &table.Table{} table := &table.Table{}
for s in input { for s in input {
@ -108,6 +78,15 @@ fn test_parse_expr() {
break 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('===================')
*/