cgen: use the new match syntax
parent
d4b17fb0c3
commit
78b32892ac
File diff suppressed because it is too large
Load Diff
|
@ -11,11 +11,11 @@ import v.util
|
||||||
pub fn (mut p Parser) call_expr(language table.Language, mod string) ast.CallExpr {
|
pub fn (mut p Parser) call_expr(language table.Language, mod string) ast.CallExpr {
|
||||||
first_pos := p.tok.position()
|
first_pos := p.tok.position()
|
||||||
fn_name := if language == .c {
|
fn_name := if language == .c {
|
||||||
'C.${p.check_name()}'
|
'C.$p.check_name()'
|
||||||
} else if language == .js {
|
} else if language == .js {
|
||||||
'JS.${p.check_js_name()}'
|
'JS.$p.check_js_name()'
|
||||||
} else if mod.len > 0 {
|
} else if mod.len > 0 {
|
||||||
'${mod}.${p.check_name()}'
|
'${mod}.$p.check_name()'
|
||||||
} else {
|
} else {
|
||||||
p.check_name()
|
p.check_name()
|
||||||
}
|
}
|
||||||
|
@ -181,11 +181,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
||||||
mut name := ''
|
mut name := ''
|
||||||
if p.tok.kind == .name {
|
if p.tok.kind == .name {
|
||||||
// TODO high order fn
|
// TODO high order fn
|
||||||
name = if language == .js {
|
name = if language == .js { p.check_js_name() } else { p.check_name() }
|
||||||
p.check_js_name()
|
|
||||||
} else {
|
|
||||||
p.check_name()
|
|
||||||
}
|
|
||||||
if language == .v && !p.pref.translated && util.contains_capital(name) {
|
if language == .v && !p.pref.translated && util.contains_capital(name) {
|
||||||
p.error('function names cannot contain uppercase letters, use snake_case instead')
|
p.error('function names cannot contain uppercase letters, use snake_case instead')
|
||||||
}
|
}
|
||||||
|
@ -491,7 +487,7 @@ fn have_fn_main(stmts []ast.Stmt) bool {
|
||||||
for stmt in stmts {
|
for stmt in stmts {
|
||||||
match stmt {
|
match stmt {
|
||||||
ast.FnDecl {
|
ast.FnDecl {
|
||||||
if it.name == 'main' {
|
if stmt.name == 'main' {
|
||||||
has_main_fn = true
|
has_main_fn = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -280,11 +280,11 @@ pub fn (mut p Parser) close_scope() {
|
||||||
for _, obj in p.scope.objects {
|
for _, obj in p.scope.objects {
|
||||||
match obj {
|
match obj {
|
||||||
ast.Var {
|
ast.Var {
|
||||||
if !it.is_used && it.name[0] != `_` {
|
if !obj.is_used && obj.name[0] != `_` {
|
||||||
if p.pref.is_prod {
|
if p.pref.is_prod {
|
||||||
p.error_with_pos('unused variable: `$it.name`', it.pos)
|
p.error_with_pos('unused variable: `$obj.name`', obj.pos)
|
||||||
} else {
|
} else {
|
||||||
p.warn_with_pos('unused variable: `$it.name`', it.pos)
|
p.warn_with_pos('unused variable: `$obj.name`', obj.pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
@ -356,7 +356,7 @@ fn (mut p Parser) check(expected token.Kind) {
|
||||||
// p.next()
|
// p.next()
|
||||||
// }
|
// }
|
||||||
if p.tok.kind != expected {
|
if p.tok.kind != expected {
|
||||||
p.error('unexpected `${p.tok.kind.str()}`, expecting `${expected.str()}`')
|
p.error('unexpected `$p.tok.kind.str()`, expecting `$expected.str()`')
|
||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
|
@ -525,12 +525,10 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
|
||||||
return ast.GotoLabel{
|
return ast.GotoLabel{
|
||||||
name: name
|
name: name
|
||||||
}
|
}
|
||||||
}
|
} else if p.peek_tok.kind == .name {
|
||||||
else if p.peek_tok.kind == .name {
|
|
||||||
p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position())
|
p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position())
|
||||||
}
|
} else if !p.inside_if_expr && !p.inside_match_body && !p.inside_or_expr &&
|
||||||
else if !p.inside_if_expr && !p.inside_match_body && !p.inside_or_expr && p.peek_tok.kind in
|
p.peek_tok.kind in [.rcbr, .eof] {
|
||||||
[.rcbr, .eof] {
|
|
||||||
p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position())
|
p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -608,7 +606,6 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn (mut p Parser) expr_list() []ast.Expr {
|
fn (mut p Parser) expr_list() []ast.Expr {
|
||||||
mut exprs := []ast.Expr{}
|
mut exprs := []ast.Expr{}
|
||||||
for {
|
for {
|
||||||
|
@ -641,7 +638,7 @@ fn (mut p Parser) attributes(is_top_stmt bool) []ast.Attr {
|
||||||
p.next()
|
p.next()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
p.error('unexpected `${p.tok.kind.str()}`, expecting `${expected.str()}`')
|
p.error('unexpected `$p.tok.kind.str()`, expecting `$expected.str()`')
|
||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
|
@ -752,10 +749,8 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt {
|
||||||
left0 := left[0]
|
left0 := left[0]
|
||||||
if p.tok.kind in [.assign, .decl_assign] || p.tok.kind.is_assign() {
|
if p.tok.kind in [.assign, .decl_assign] || p.tok.kind.is_assign() {
|
||||||
return p.partial_assign_stmt(left)
|
return p.partial_assign_stmt(left)
|
||||||
}
|
} else if is_top_level && tok.kind !in [.key_if, .key_match] && left0 !is ast.CallExpr &&
|
||||||
else if is_top_level && tok.kind !in [.key_if, .key_match] &&
|
left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr && (left0 as ast.InfixExpr).op == .left_shift) &&
|
||||||
left0 !is ast.CallExpr && left0 !is ast.PostfixExpr &&
|
|
||||||
!(left0 is ast.InfixExpr && (left0 as ast.InfixExpr).op == .left_shift) &&
|
|
||||||
left0 !is ast.ComptimeCall {
|
left0 !is ast.ComptimeCall {
|
||||||
p.error_with_pos('expression evaluated but not used', left0.position())
|
p.error_with_pos('expression evaluated but not used', left0.position())
|
||||||
}
|
}
|
||||||
|
@ -860,7 +855,7 @@ pub fn (mut p Parser) name_expr() ast.Expr {
|
||||||
match mut obj {
|
match mut obj {
|
||||||
ast.Var {
|
ast.Var {
|
||||||
known_var = true
|
known_var = true
|
||||||
it.is_used = true
|
obj.is_used = true
|
||||||
}
|
}
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
|
@ -1141,11 +1136,7 @@ fn (mut p Parser) string_expr() ast.Expr {
|
||||||
node = ast.StringLiteral{
|
node = ast.StringLiteral{
|
||||||
val: val
|
val: val
|
||||||
is_raw: is_raw
|
is_raw: is_raw
|
||||||
language: if is_cstr {
|
language: if is_cstr { table.Language.c } else { table.Language.v }
|
||||||
table.Language.c
|
|
||||||
} else {
|
|
||||||
table.Language.v
|
|
||||||
}
|
|
||||||
pos: pos
|
pos: pos
|
||||||
}
|
}
|
||||||
return node
|
return node
|
||||||
|
@ -1222,7 +1213,7 @@ fn (mut p Parser) string_expr() ast.Expr {
|
||||||
node = ast.StringInterLiteral{
|
node = ast.StringInterLiteral{
|
||||||
vals: vals
|
vals: vals
|
||||||
exprs: exprs
|
exprs: exprs
|
||||||
need_fmts: has_fmts // prelimery - until checker finds out if really needed
|
need_fmts: has_fmts
|
||||||
fwidths: fwidths
|
fwidths: fwidths
|
||||||
precisions: precisions
|
precisions: precisions
|
||||||
pluss: visible_pluss
|
pluss: visible_pluss
|
||||||
|
@ -1231,6 +1222,7 @@ fn (mut p Parser) string_expr() ast.Expr {
|
||||||
fmt_poss: fposs
|
fmt_poss: fposs
|
||||||
pos: pos
|
pos: pos
|
||||||
}
|
}
|
||||||
|
// need_fmts: prelimery - until checker finds out if really needed
|
||||||
p.inside_str_interp = false
|
p.inside_str_interp = false
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|
|
@ -191,8 +191,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
|
||||||
op: tok.kind
|
op: tok.kind
|
||||||
pos: pos
|
pos: pos
|
||||||
}
|
}
|
||||||
}
|
} else if p.tok.kind.is_infix() {
|
||||||
else if p.tok.kind.is_infix() {
|
|
||||||
// return early for deref assign `*x = 2` goes to prefix expr
|
// return early for deref assign `*x = 2` goes to prefix expr
|
||||||
if p.tok.kind == .mul && p.tok.line_nr != p.prev_tok.line_nr && p.peek_tok2.kind ==
|
if p.tok.kind == .mul && p.tok.line_nr != p.prev_tok.line_nr && p.peek_tok2.kind ==
|
||||||
.assign {
|
.assign {
|
||||||
|
|
Loading…
Reference in New Issue