v2: `if x := `; fix `for key, val in`; CastExpr
parent
3bde876097
commit
4f0d505c65
|
@ -11,7 +11,7 @@ import (
|
||||||
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
|
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
|
||||||
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
|
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
|
||||||
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
|
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
|
||||||
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit
|
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | OrExpr
|
||||||
|
|
||||||
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
|
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
|
||||||
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
|
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
|
||||||
|
@ -351,10 +351,11 @@ pub:
|
||||||
|
|
||||||
pub struct ForInStmt {
|
pub struct ForInStmt {
|
||||||
pub:
|
pub:
|
||||||
var string
|
key_var string
|
||||||
cond Expr
|
val_var string
|
||||||
stmts []Stmt
|
cond Expr
|
||||||
pos token.Position
|
stmts []Stmt
|
||||||
|
pos token.Position
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ForCStmt {
|
pub struct ForCStmt {
|
||||||
|
@ -481,6 +482,12 @@ pub:
|
||||||
expr Expr
|
expr Expr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct OrExpr {
|
||||||
|
pub:
|
||||||
|
var_name string
|
||||||
|
expr Expr
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Assoc {
|
pub struct Assoc {
|
||||||
pub:
|
pub:
|
||||||
name string
|
name string
|
||||||
|
|
|
@ -159,7 +159,11 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||||
f.writeln('}\n')
|
f.writeln('}\n')
|
||||||
}
|
}
|
||||||
ast.ForInStmt {
|
ast.ForInStmt {
|
||||||
f.write('for $it.var in ')
|
f.write('for $it.key_var')
|
||||||
|
if it.val_var != '' {
|
||||||
|
f.write(', $it.val_var')
|
||||||
|
}
|
||||||
|
f.write(' in ')
|
||||||
f.expr(it.cond)
|
f.expr(it.cond)
|
||||||
f.writeln(' {')
|
f.writeln(' {')
|
||||||
f.stmts(it.stmts)
|
f.stmts(it.stmts)
|
||||||
|
@ -282,6 +286,11 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
ast.BoolLiteral {
|
ast.BoolLiteral {
|
||||||
f.write(it.val.str())
|
f.write(it.val.str())
|
||||||
}
|
}
|
||||||
|
ast.CastExpr {
|
||||||
|
f.write(f.table.type_to_str(it.typ) + '(')
|
||||||
|
f.expr(it.expr)
|
||||||
|
f.write(')')
|
||||||
|
}
|
||||||
ast.CallExpr {
|
ast.CallExpr {
|
||||||
f.write('${it.name}(')
|
f.write('${it.name}(')
|
||||||
for i, expr in it.args {
|
for i, expr in it.args {
|
||||||
|
@ -387,6 +396,10 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
ast.None {
|
ast.None {
|
||||||
f.write('none')
|
f.write('none')
|
||||||
}
|
}
|
||||||
|
ast.OrExpr {
|
||||||
|
f.write(it.var_name + ' := ')
|
||||||
|
f.expr(it.expr)
|
||||||
|
}
|
||||||
ast.PostfixExpr {
|
ast.PostfixExpr {
|
||||||
f.expr(it.expr)
|
f.expr(it.expr)
|
||||||
f.write(it.op.str())
|
f.write(it.op.str())
|
||||||
|
@ -424,7 +437,9 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
f.write('}')
|
f.write('}')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {}
|
else {
|
||||||
|
println('fmt expr: unhandled node ') // + typeof(node))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1035,9 +1035,10 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
||||||
// `for i in vals`, `for i in start .. end`
|
// `for i in vals`, `for i in start .. end`
|
||||||
else if p.peek_tok.kind in [.key_in, .comma] {
|
else if p.peek_tok.kind in [.key_in, .comma] {
|
||||||
var_name := p.check_name()
|
var_name := p.check_name()
|
||||||
|
mut val_name := ''
|
||||||
if p.tok.kind == .comma {
|
if p.tok.kind == .comma {
|
||||||
p.check(.comma)
|
p.check(.comma)
|
||||||
val_name := p.check_name()
|
val_name = p.check_name()
|
||||||
// p.table.register_var(table.Var{
|
// p.table.register_var(table.Var{
|
||||||
// name: val_name
|
// name: val_name
|
||||||
// typ: table.int_type
|
// typ: table.int_type
|
||||||
|
@ -1092,7 +1093,8 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
pos: p.tok.position()
|
pos: p.tok.position()
|
||||||
cond: cond
|
cond: cond
|
||||||
var: var_name
|
key_var: var_name
|
||||||
|
val_var: val_name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// `for cond {`
|
// `for cond {`
|
||||||
|
@ -1115,10 +1117,22 @@ fn (p mut Parser) if_expr() ast.Expr {
|
||||||
pos := p.tok.position()
|
pos := p.tok.position()
|
||||||
// `if x := opt() {`
|
// `if x := opt() {`
|
||||||
mut cond := ast.Expr{}
|
mut cond := ast.Expr{}
|
||||||
|
mut is_or := false
|
||||||
if p.peek_tok.kind == .decl_assign {
|
if p.peek_tok.kind == .decl_assign {
|
||||||
p.next()
|
is_or = true
|
||||||
|
p.open_scope()
|
||||||
|
var_name := p.check_name()
|
||||||
|
// p.table.register_var(
|
||||||
p.check(.decl_assign)
|
p.check(.decl_assign)
|
||||||
p.expr(0)
|
expr,typ := p.expr(0)
|
||||||
|
p.scope.register_var(ast.VarDecl{
|
||||||
|
name: var_name
|
||||||
|
typ: typ
|
||||||
|
})
|
||||||
|
cond = ast.OrExpr{
|
||||||
|
var_name: var_name
|
||||||
|
expr: expr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cond,_ = p.expr(0)
|
cond,_ = p.expr(0)
|
||||||
|
@ -1138,6 +1152,9 @@ fn (p mut Parser) if_expr() ast.Expr {
|
||||||
else_stmts = p.parse_block()
|
else_stmts = p.parse_block()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if is_or {
|
||||||
|
p.close_scope()
|
||||||
|
}
|
||||||
// mut typ := table.void_type
|
// mut typ := table.void_type
|
||||||
// mut left := ast.Expr{}
|
// mut left := ast.Expr{}
|
||||||
// If the last statement is an expression, return its type
|
// If the last statement is an expression, return its type
|
||||||
|
|
Loading…
Reference in New Issue