AssignStmt

pull/3253/head
Alexander Medvednikov 2019-12-28 19:16:04 +01:00
parent b5fe40624c
commit 5a6428f1ff
6 changed files with 61 additions and 23 deletions

View File

@ -11,7 +11,7 @@ import (
pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral | FloatLiteral | pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral | FloatLiteral |
Ident Ident
pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt | AssignStmt
// Stand-alone expression in a statement list. // Stand-alone expression in a statement list.
pub struct ExprStmt { pub struct ExprStmt {
pub: pub:
@ -101,7 +101,7 @@ pub:
pub struct BinaryExpr { pub struct BinaryExpr {
pub: pub:
tok_kind token.TokenKind // tok_kind token.TokenKind
// op BinaryOp // op BinaryOp
op token.TokenKind op token.TokenKind
left Expr left Expr
@ -118,18 +118,25 @@ pub:
left Expr left Expr
} }
struct IfExpr { pub struct IfExpr {
tok_kind token.TokenKind tok_kind token.TokenKind
cond Expr cond Expr
body []Stmt body []Stmt
else_ []Stmt else_ []Stmt
} }
struct ReturnStmt { pub struct ReturnStmt {
tok_kind token.TokenKind // or pos tok_kind token.TokenKind // or pos
results []Expr results []Expr
} }
pub struct AssignStmt {
pub:
left Expr
right Expr
op token.TokenKind
}
// string representaiton of expr // string representaiton of expr
pub fn (x Expr) str() string { pub fn (x Expr) str() string {
match x { match x {

View File

@ -32,6 +32,12 @@ pub fn (g mut Gen) writeln(s string) {
fn (g mut Gen) stmt(node ast.Stmt) { fn (g mut Gen) stmt(node ast.Stmt) {
match node { match node {
ast.AssignStmt {
g.expr(it.left)
g.write(' $it.op.str() ')
g.expr(it.right)
g.writeln(';')
}
ast.FnDecl { ast.FnDecl {
g.writeln('$it.typ.name ${it.name}() { ') g.writeln('$it.typ.name ${it.name}() { ')
for stmt in it.stmts { for stmt in it.stmts {

View File

@ -9,5 +9,5 @@ void function2() {
f64 f = 10.1; f64 f = 10.1;
string s = tos3("hi"); string s = tos3("hi");
int m = 10; int m = 10;
m += 10; x += 10;
} }

View File

@ -6,10 +6,10 @@ fn function1() int {
// comment // comment
fn function2() { fn function2() {
x := 0 mut x := 0
f := 10.1 f := 10.1
s := 'hi' s := 'hi'
m := 10 m := 10
m += 10 x += 10
//c := 0 //c := 0
} }

View File

@ -133,10 +133,16 @@ fn (p mut Parser) check_name() string {
} }
pub fn (p mut Parser) stmt() ast.Stmt { pub fn (p mut Parser) stmt() ast.Stmt {
// println('stmt at ' + p.tok.str())
// `x := ...` // `x := ...`
if p.tok.kind == .name && p.peek_tok.kind == .decl_assign { if p.tok.kind == .name {
if p.peek_tok.kind == .decl_assign {
return p.var_decl() return p.var_decl()
} }
else if p.peek_tok.is_assign() {
return p.assign_stmt()
}
}
match p.tok.kind { match p.tok.kind {
.key_module { .key_module {
return p.module_decl() return p.module_decl()
@ -162,18 +168,27 @@ pub fn (p mut Parser) stmt() ast.Stmt {
} }
} }
pub fn (p mut Parser) assign_stmt() ast.AssignStmt {
left_expr,left_type := p.expr(0)
op := p.tok.kind
println('assignn_stmt() ' + op.str())
p.next()
right_expr,right_type := p.expr(0)
return ast.AssignStmt{
left: left_expr
right: right_expr
op: op
}
}
// Implementation of Pratt Precedence // Implementation of Pratt Precedence
pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) { pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
println('expr at ' + p.tok.str()) // println('expr at ' + p.tok.str())
// null denotation (prefix) // null denotation (prefix)
mut node := ast.Expr{} mut node := ast.Expr{}
mut typ := types.void_type mut typ := types.void_type
match p.tok.kind { match p.tok.kind {
.name { .name {
// `x := ...`
// if p.peek_tok.kind == .decl_assign {
// return p.var_decl()
// }
// name expr // name expr
node = ast.Ident{ node = ast.Ident{
name: p.tok.lit name: p.tok.lit
@ -215,6 +230,16 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
if prev_tok.is_right_assoc() { if prev_tok.is_right_assoc() {
mut expr := ast.Expr{} mut expr := ast.Expr{}
expr,t2 = p.expr(prev_tok.precedence() - 1) expr,t2 = p.expr(prev_tok.precedence() - 1)
/*
if prev_tok.is_assign() {
return ast.AssignStmt {
left: node
op: prev_tok.kind
right: expr
}, types.void_type
}
*/
node = ast.BinaryExpr{ node = ast.BinaryExpr{
left: node left: node
// left_type: t1 // left_type: t1

View File

@ -267,8 +267,8 @@ pub fn is_decl(t TokenKind) bool {
return t in [.key_enum, .key_interface, .key_fn, .key_struct, .key_type, .key_const, .key_import_const, .key_pub, .eof] return t in [.key_enum, .key_interface, .key_fn, .key_struct, .key_type, .key_const, .key_import_const, .key_pub, .eof]
} }
fn (t TokenKind) is_assign() bool { pub fn (t Token) is_assign() bool {
return t in assign_tokens return t.kind in assign_tokens
} }
fn (t []TokenKind) contains(val TokenKind) bool { fn (t []TokenKind) contains(val TokenKind) bool {
@ -331,9 +331,9 @@ pub fn (tok Token) precedence() int {
.logical_or { .logical_or {
return 3 return 3
} }
.plus_assign { // /.plus_assign {
return 2 // /return 2
} // /}
else { else {
return lowest_prec return lowest_prec
} }