2020-04-17 18:16:55 +02:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module parser
|
|
|
|
|
|
|
|
import v.ast
|
2020-07-04 12:44:25 +02:00
|
|
|
import v.table
|
2020-04-17 18:16:55 +02:00
|
|
|
|
2020-04-23 01:16:58 +02:00
|
|
|
fn (mut p Parser) assign_stmt() ast.Stmt {
|
2020-07-17 19:13:22 +02:00
|
|
|
exprs, comments := p.expr_list()
|
|
|
|
return p.partial_assign_stmt(exprs, comments)
|
2020-05-15 23:14:53 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 19:34:05 +01:00
|
|
|
fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) ? {
|
2020-11-25 12:09:40 +01:00
|
|
|
match val {
|
2020-05-24 19:33:53 +02:00
|
|
|
ast.Ident {
|
2020-06-16 13:20:16 +02:00
|
|
|
for expr in exprs {
|
|
|
|
if expr is ast.Ident {
|
2020-07-09 17:14:14 +02:00
|
|
|
if expr.name == val.name {
|
2020-06-27 15:00:28 +02:00
|
|
|
p.error_with_pos('undefined variable: `$val.name`', val.pos)
|
2020-12-04 19:34:05 +01:00
|
|
|
return error('undefined variable: `$val.name`')
|
2020-06-16 13:20:16 +02:00
|
|
|
}
|
2020-05-24 19:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast.InfixExpr {
|
2020-12-04 19:34:05 +01:00
|
|
|
p.check_undefined_variables(exprs, val.left) ?
|
|
|
|
p.check_undefined_variables(exprs, val.right) ?
|
2020-05-25 11:31:04 +02:00
|
|
|
}
|
2020-05-29 17:38:40 +02:00
|
|
|
ast.ParExpr {
|
2020-12-04 19:34:05 +01:00
|
|
|
p.check_undefined_variables(exprs, val.expr) ?
|
2020-05-29 17:38:40 +02:00
|
|
|
}
|
|
|
|
ast.PostfixExpr {
|
2020-12-04 19:34:05 +01:00
|
|
|
p.check_undefined_variables(exprs, val.expr) ?
|
2020-05-29 17:38:40 +02:00
|
|
|
}
|
|
|
|
ast.PrefixExpr {
|
2020-12-04 19:34:05 +01:00
|
|
|
p.check_undefined_variables(exprs, val.right) ?
|
2020-05-29 17:38:40 +02:00
|
|
|
}
|
2020-05-25 11:31:04 +02:00
|
|
|
ast.StringInterLiteral {
|
2020-06-27 15:00:28 +02:00
|
|
|
for expr_ in val.exprs {
|
2020-12-04 19:34:05 +01:00
|
|
|
p.check_undefined_variables(exprs, expr_) ?
|
2020-05-25 11:31:04 +02:00
|
|
|
}
|
2020-05-24 19:33:53 +02:00
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 13:20:16 +02:00
|
|
|
fn (mut p Parser) check_cross_variables(exprs []ast.Expr, val ast.Expr) bool {
|
2020-07-01 16:43:13 +02:00
|
|
|
val_ := val
|
2020-11-25 12:09:40 +01:00
|
|
|
match val_ {
|
2020-07-01 12:11:03 +02:00
|
|
|
ast.Ident {
|
|
|
|
for expr in exprs {
|
2020-06-16 13:20:16 +02:00
|
|
|
if expr is ast.Ident {
|
2020-07-09 17:14:14 +02:00
|
|
|
if expr.name == val_.name {
|
2020-06-27 15:00:28 +02:00
|
|
|
return true
|
|
|
|
}
|
2020-06-16 13:20:16 +02:00
|
|
|
}
|
2020-07-01 12:11:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ast.IndexExpr {
|
|
|
|
for expr in exprs {
|
2020-07-01 16:43:13 +02:00
|
|
|
if expr.str() == val.str() {
|
|
|
|
return true
|
2020-07-01 12:11:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 22:12:59 +02:00
|
|
|
ast.InfixExpr {
|
|
|
|
return p.check_cross_variables(exprs, val_.left) || p.check_cross_variables(exprs, val_.right)
|
|
|
|
}
|
|
|
|
ast.PrefixExpr {
|
|
|
|
return p.check_cross_variables(exprs, val_.right)
|
|
|
|
}
|
|
|
|
ast.PostfixExpr {
|
|
|
|
return p.check_cross_variables(exprs, val_.expr)
|
|
|
|
}
|
2020-07-01 18:43:14 +02:00
|
|
|
ast.SelectorExpr {
|
|
|
|
for expr in exprs {
|
|
|
|
if expr.str() == val.str() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 18:00:51 +02:00
|
|
|
else {}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-17 19:13:22 +02:00
|
|
|
fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comment) ast.Stmt {
|
2020-06-16 13:20:16 +02:00
|
|
|
p.is_stmt_ident = false
|
|
|
|
op := p.tok.kind
|
2020-04-17 18:16:55 +02:00
|
|
|
pos := p.tok.position()
|
2020-06-16 13:20:16 +02:00
|
|
|
p.next()
|
2021-01-15 13:45:26 +01:00
|
|
|
mut right_comments := []ast.Comment{}
|
|
|
|
mut right := []ast.Expr{cap: left.len}
|
|
|
|
if p.tok.kind == .key_go {
|
|
|
|
spos := p.tok.position()
|
|
|
|
p.next()
|
|
|
|
right_comments = p.eat_comments()
|
|
|
|
mut mod := ''
|
|
|
|
mut language := table.Language.v
|
|
|
|
if p.peek_tok.kind == .dot {
|
|
|
|
if p.tok.lit == 'C' {
|
|
|
|
language = table.Language.c
|
|
|
|
p.check_for_impure_v(language, p.tok.position())
|
|
|
|
} else if p.tok.lit == 'JS' {
|
|
|
|
language = table.Language.js
|
|
|
|
p.check_for_impure_v(language, p.tok.position())
|
|
|
|
} else {
|
|
|
|
mod = p.tok.lit
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
call_expr := p.call_expr(language, mod)
|
|
|
|
allpos := spos.extend(p.tok.position())
|
|
|
|
right << ast.GoExpr{
|
|
|
|
go_stmt: ast.GoStmt{
|
|
|
|
call_expr: call_expr
|
|
|
|
pos: allpos
|
|
|
|
}
|
|
|
|
pos: allpos
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
right, right_comments = p.expr_list()
|
|
|
|
}
|
2020-07-17 19:13:22 +02:00
|
|
|
mut comments := []ast.Comment{cap: left_comments.len + right_comments.len}
|
|
|
|
comments << left_comments
|
|
|
|
comments << right_comments
|
2020-12-11 18:20:24 +01:00
|
|
|
end_comments := p.eat_line_end_comments()
|
2020-05-26 18:00:51 +02:00
|
|
|
mut has_cross_var := false
|
2020-06-16 13:20:16 +02:00
|
|
|
if op == .decl_assign {
|
2020-05-24 19:33:53 +02:00
|
|
|
// a, b := a + 1, b
|
2020-06-16 13:20:16 +02:00
|
|
|
for r in right {
|
|
|
|
p.check_undefined_variables(left, r)
|
2020-05-24 19:33:53 +02:00
|
|
|
}
|
2020-06-27 15:00:28 +02:00
|
|
|
} else if left.len > 1 {
|
2020-05-26 18:00:51 +02:00
|
|
|
// a, b = b, a
|
2020-06-16 13:20:16 +02:00
|
|
|
for r in right {
|
|
|
|
has_cross_var = p.check_cross_variables(left, r)
|
2020-07-10 13:59:17 +02:00
|
|
|
if op !in [.assign, .decl_assign] {
|
2020-12-26 18:48:58 +01:00
|
|
|
p.error_with_pos('unexpected $op.str(), expecting := or = or comma', pos)
|
2020-12-04 19:34:05 +01:00
|
|
|
return ast.Stmt{}
|
2020-07-10 13:59:17 +02:00
|
|
|
}
|
2020-07-01 12:11:03 +02:00
|
|
|
if has_cross_var {
|
|
|
|
break
|
|
|
|
}
|
2020-05-26 18:00:51 +02:00
|
|
|
}
|
2020-05-24 19:33:53 +02:00
|
|
|
}
|
2020-10-09 21:20:58 +02:00
|
|
|
mut is_static := false
|
2020-06-16 13:20:16 +02:00
|
|
|
for i, lx in left {
|
2020-11-25 12:09:40 +01:00
|
|
|
match mut lx {
|
2020-06-16 13:20:16 +02:00
|
|
|
ast.Ident {
|
|
|
|
if op == .decl_assign {
|
2020-06-18 22:16:38 +02:00
|
|
|
if p.scope.known_var(lx.name) {
|
|
|
|
p.error_with_pos('redefinition of `$lx.name`', lx.pos)
|
2020-12-04 19:34:05 +01:00
|
|
|
return ast.Stmt{}
|
2020-06-18 22:16:38 +02:00
|
|
|
}
|
2020-07-04 12:44:25 +02:00
|
|
|
mut share := table.ShareType(0)
|
|
|
|
if lx.info is ast.IdentVar {
|
2020-10-09 21:20:58 +02:00
|
|
|
iv := lx.info as ast.IdentVar
|
|
|
|
share = iv.share
|
|
|
|
if iv.is_static {
|
|
|
|
if !p.pref.translated {
|
2020-10-15 22:12:59 +02:00
|
|
|
p.error_with_pos('static variables are supported only in -translated mode',
|
|
|
|
lx.pos)
|
2020-12-04 19:34:05 +01:00
|
|
|
return ast.Stmt{}
|
2020-10-09 21:20:58 +02:00
|
|
|
}
|
|
|
|
is_static = true
|
|
|
|
}
|
2020-07-04 12:44:25 +02:00
|
|
|
}
|
2020-12-04 18:06:53 +01:00
|
|
|
r0 := right[0]
|
2020-07-22 18:10:31 +02:00
|
|
|
mut v := ast.Var{
|
|
|
|
name: lx.name
|
|
|
|
expr: if left.len == right.len { right[i] } else { ast.Expr{} }
|
|
|
|
share: share
|
|
|
|
is_mut: lx.is_mut || p.inside_for
|
|
|
|
pos: lx.pos
|
2020-06-16 13:20:16 +02:00
|
|
|
}
|
2020-12-04 18:06:53 +01:00
|
|
|
if p.pref.autofree {
|
|
|
|
if r0 is ast.CallExpr {
|
|
|
|
// Set correct variable position (after the or block)
|
|
|
|
// so that autofree doesn't free it in cgen before
|
|
|
|
// it's declared. (`Or` variables are declared after the or block).
|
2020-12-04 20:44:33 +01:00
|
|
|
if r0.or_block.pos.pos > 0 && r0.or_block.stmts.len > 0 {
|
2020-12-04 18:06:53 +01:00
|
|
|
v.is_or = true
|
|
|
|
// v.pos = r0.or_block.pos.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 18:10:31 +02:00
|
|
|
obj := ast.ScopeObject(v)
|
|
|
|
lx.obj = obj
|
2020-12-02 14:40:25 +01:00
|
|
|
p.scope.register(obj)
|
2020-06-16 13:20:16 +02:00
|
|
|
}
|
2020-04-17 18:16:55 +02:00
|
|
|
}
|
2020-06-16 13:20:16 +02:00
|
|
|
ast.IndexExpr {
|
2020-06-20 14:30:03 +02:00
|
|
|
if op == .decl_assign {
|
2020-06-27 15:00:28 +02:00
|
|
|
p.error_with_pos('non-name `$lx.left[$lx.index]` on left side of `:=`',
|
|
|
|
lx.pos)
|
2020-12-04 19:34:05 +01:00
|
|
|
return ast.Stmt{}
|
2020-06-20 14:30:03 +02:00
|
|
|
}
|
2020-06-18 22:16:38 +02:00
|
|
|
lx.is_setter = true
|
2020-04-17 18:16:55 +02:00
|
|
|
}
|
2020-06-16 13:20:16 +02:00
|
|
|
ast.ParExpr {}
|
|
|
|
ast.PrefixExpr {}
|
|
|
|
ast.SelectorExpr {
|
|
|
|
if op == .decl_assign {
|
2020-06-27 15:00:28 +02:00
|
|
|
p.error_with_pos('struct fields can only be declared during the initialization',
|
|
|
|
lx.pos)
|
2020-12-04 19:34:05 +01:00
|
|
|
return ast.Stmt{}
|
2020-06-16 13:20:16 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-27 15:00:28 +02:00
|
|
|
else {
|
|
|
|
// TODO: parexpr ( check vars)
|
|
|
|
// else { p.error_with_pos('unexpected `${typeof(lx)}`', lx.position()) }
|
|
|
|
}
|
2020-04-17 18:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-27 15:00:28 +02:00
|
|
|
return ast.AssignStmt{
|
|
|
|
op: op
|
|
|
|
left: left
|
|
|
|
right: right
|
2020-07-17 19:13:22 +02:00
|
|
|
comments: comments
|
2020-12-11 18:20:24 +01:00
|
|
|
end_comments: end_comments
|
2020-06-27 15:00:28 +02:00
|
|
|
pos: pos
|
|
|
|
has_cross_var: has_cross_var
|
|
|
|
is_simple: p.inside_for && p.tok.kind == .lcbr
|
2020-10-09 21:20:58 +02:00
|
|
|
is_static: is_static
|
2020-06-27 15:00:28 +02:00
|
|
|
}
|
2020-04-17 18:16:55 +02:00
|
|
|
}
|