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

View File

@ -44,15 +44,10 @@ const (
fn (g mut Gen) expr(node ast.Expr) Type { fn (g mut Gen) expr(node ast.Expr) Type {
//println('cgen expr()') //println('cgen expr()')
match node { match node {
ast.IntegerExpr { ast.IntegerLiteral {
g.write(it.val.str()) g.write(it.val.str())
return int_type return int_type
} }
ast.ScalarExpr {
g.expr(it.left)
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

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

View File

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