remove ScalarExpr

pull/3222/head
Alexander Medvednikov 2019-12-26 13:21:41 +03:00
parent 9b37fc7310
commit 6363118aa9
4 changed files with 24 additions and 26 deletions

View File

@ -10,11 +10,11 @@ import (
struct Foo {}
pub type Expr = Foo | IfExpr | BinaryExpr | ScalarExpr | UnaryExpr |
StringLiteral | IntegerExpr
pub type Expr = Foo | IfExpr | BinaryExpr | UnaryExpr |
StringLiteral | IntegerLiteral
pub type Stmt = Foo | VarDecl
pub struct IntegerExpr {
pub struct IntegerLiteral {
pub:
val int
}
@ -66,16 +66,6 @@ pub:
right Expr
}
pub struct ScalarExpr {
pub:
token token.Token
// op BinaryOp
// op token.Token
typ token.Token
val string
left Expr
}
pub struct UnaryExpr {
pub:
// token token.Token
@ -102,9 +92,9 @@ pub fn (x Expr) str() string {
BinaryExpr {
return '(${it.left.str()}$it.op.str()${it.right.str()})'
}
ScalarExpr {
return '${it.left.str()}$it.val'
}
//ScalarExpr {
//return '${it.left.str()}$it.val'
//}
UnaryExpr {
return '${it.left.str()}$it.op.str()'
}

View File

@ -44,15 +44,10 @@ const (
fn (g mut Gen) expr(node ast.Expr) Type {
//println('cgen expr()')
match node {
ast.IntegerExpr {
ast.IntegerLiteral {
g.write(it.val.str())
return int_type
}
ast.ScalarExpr {
g.expr(it.left)
g.write(' $it.val ')
}
ast.UnaryExpr {
g.expr(it.left)
g.write(' $it.op ')

View File

@ -64,10 +64,18 @@ pub fn (p mut Parser) expr(rbp int) ast.Expr {
else {
// TODO: fix bug. note odd conditon instead of else if (same below)
if tok.is_scalar() {
node = ast.ScalarExpr{
val: lit
typ: tok
if tok == .str {
node = ast.StringLiteral {
val: lit
}
} if tok == .number {
node = ast.IntegerLiteral {
val: lit.int()
}
}
//else {
//verror('bad scalar token')
//}
}
if !tok.is_scalar() && tok.is_unary() {
node = ast.UnaryExpr{
@ -123,3 +131,8 @@ fn (p mut Parser) stmt() ast.Stmt {
return ast.VarDecl{}
}
fn verror(s string) {
println(s)
exit(1)
}

View File

@ -40,7 +40,7 @@ fn test_parser() {
fn test_cgen() {
//expr := parse_expr('3 + 7 * 2')
//expr2 := parse_stmt('a := 3 + "f"')
expr2 := parse_expr('2 +3 ')//"helo"')
expr2 := parse_expr('2 + "helo"')
program := ast.Program{
exprs: [
expr2,