v2: process `or` block for `CallExpr`

pull/3882/head
Alexey 2020-02-29 17:03:32 +03:00 committed by GitHub
parent efff66ada7
commit ee0a0afb25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 13 deletions

View File

@ -8,14 +8,14 @@ import (
v.table v.table
) )
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 | IfGuardExpr | ParExpr | OrExpr CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | 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 |
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
LineComment | MultiLineComment | AssertStmt | UnsafeStmt LineComment | MultiLineComment | AssertStmt | UnsafeStmt
pub type Type = StructType | ArrayType pub type Type = StructType | ArrayType
@ -154,6 +154,7 @@ mut:
args []Expr args []Expr
is_c bool is_c bool
muts []bool muts []bool
or_block OrExpr
} }
pub struct MethodCallExpr { pub struct MethodCallExpr {

View File

@ -324,16 +324,24 @@ fn (f mut Fmt) expr(node ast.Expr) {
} }
ast.CallExpr { ast.CallExpr {
f.write('${it.name}(') f.write('${it.name}(')
for i, expr in it.args { for i, arg in it.args {
if it.muts[i] { if it.muts[i] {
f.write('mut ') f.write('mut ')
} }
f.expr(expr) if i > 0 {
if i != it.args.len - 1 { f.wrap_long_line()
}
f.expr(arg)
if i < it.args.len - 1 {
f.write(', ') f.write(', ')
} }
} }
f.write(')') f.write(')')
if it.or_block.stmts.len > 0 {
f.writeln(' or {')
f.stmts(it.or_block.stmts)
f.write('}')
}
} }
ast.CharLiteral { ast.CharLiteral {
f.write('`$it.val`') f.write('`$it.val`')

View File

@ -138,3 +138,17 @@ fn unsafe_fn() {
malloc(2) malloc(2)
} }
} }
fn fn_with_or() int {
fn_with_optional() or {
return 10
}
return 20
}
fn (f Foo) method_with_or() int {
f.fn_with_optional() or {
return 10
}
return 20
}

View File

@ -138,3 +138,17 @@ fn fn_with_multi_return() (int,string) {
fn unsafe_fn() { fn unsafe_fn() {
unsafe { malloc(2) } unsafe { malloc(2) }
} }
fn fn_with_or() int {
fn_with_optional() or {
return 10
}
return 20
}
fn (f Foo) method_with_or() int {
f.fn_with_optional() or {
return 10
}
return 20
}

View File

@ -14,6 +14,11 @@ pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr {
fn_name := if is_c {'C.$name' } else if mod.len > 0 { '${mod}.$name' } else { name } fn_name := if is_c {'C.$name' } else if mod.len > 0 { '${mod}.$name' } else { name }
p.check(.lpar) p.check(.lpar)
args, muts := p.call_args() args, muts := p.call_args()
mut or_stmts := []ast.Stmt
if p.tok.kind == .key_orelse {
p.next()
or_stmts = p.parse_block()
}
node := ast.CallExpr{ node := ast.CallExpr{
name: fn_name name: fn_name
args: args args: args
@ -22,10 +27,9 @@ pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr {
pos: tok.position() pos: tok.position()
is_c: is_c is_c: is_c
} or_block: ast.OrExpr{
if p.tok.kind == .key_orelse { stmts: or_stmts
p.next() }
p.parse_block()
} }
return node return node
} }