2019-06-23 05:21:30 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
module compiler
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2019-07-29 19:21:36 +03:00
|
|
|
import (
|
|
|
|
os
|
|
|
|
strings
|
2019-09-26 05:28:43 +03:00
|
|
|
)
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
struct Parser {
|
2019-10-25 16:34:12 +03:00
|
|
|
file_path string // if parsing file will be path eg, "/home/user/hello.v"
|
2019-06-22 21:20:28 +03:00
|
|
|
file_name string // "hello.v"
|
2019-10-23 08:18:44 +03:00
|
|
|
file_platform string // ".v", "_windows.v", "_nix.v", "_darwin.v", "_linux.v" ...
|
2019-09-24 22:30:30 +03:00
|
|
|
// When p.file_pcguard != '', it contains a
|
|
|
|
// C ifdef guard clause that must be put before
|
|
|
|
// the #include directives in the parsed .v file
|
|
|
|
file_pcguard string
|
2019-09-01 22:51:16 +03:00
|
|
|
v &V
|
2019-09-19 17:25:00 +03:00
|
|
|
pref &Preferences // Preferences shared from V struct
|
|
|
|
mut:
|
2019-09-01 22:51:16 +03:00
|
|
|
scanner &Scanner
|
2019-10-09 01:05:34 +03:00
|
|
|
tokens []Token
|
2019-06-22 21:20:28 +03:00
|
|
|
token_idx int
|
2019-10-09 01:05:34 +03:00
|
|
|
tok TokenKind
|
|
|
|
prev_tok TokenKind
|
|
|
|
prev_tok2 TokenKind // TODO remove these once the tokens are cached
|
2019-06-22 21:20:28 +03:00
|
|
|
lit string
|
2019-09-01 22:51:16 +03:00
|
|
|
cgen &CGen
|
|
|
|
table &Table
|
2019-10-25 16:34:12 +03:00
|
|
|
import_table ImportTable // Holds imports for just the file being parsed
|
2019-08-17 22:19:37 +03:00
|
|
|
pass Pass
|
|
|
|
os OS
|
2019-06-22 21:20:28 +03:00
|
|
|
inside_const bool
|
|
|
|
expr_var Var
|
2019-08-30 20:19:06 +03:00
|
|
|
has_immutable_field bool
|
|
|
|
first_immutable_field Var
|
2019-09-24 22:30:30 +03:00
|
|
|
assigned_type string // non-empty if we are in an assignment expression
|
2019-07-07 23:30:15 +03:00
|
|
|
expected_type string
|
2019-06-22 21:20:28 +03:00
|
|
|
tmp_cnt int
|
2019-08-05 10:49:52 +03:00
|
|
|
builtin_mod bool
|
2019-06-22 21:20:28 +03:00
|
|
|
inside_if_expr bool
|
2019-08-27 00:39:11 +03:00
|
|
|
inside_unwrapping_match_statement bool
|
2019-09-09 03:29:24 +03:00
|
|
|
inside_return_expr bool
|
2019-10-20 19:59:53 +03:00
|
|
|
inside_unsafe bool
|
2019-06-22 21:20:28 +03:00
|
|
|
is_struct_init bool
|
|
|
|
if_expr_cnt int
|
|
|
|
for_expr_cnt int // to detect whether `continue` can be used
|
|
|
|
ptr_cast bool
|
|
|
|
calling_c bool
|
2019-09-09 16:22:39 +03:00
|
|
|
cur_fn Fn
|
2019-09-23 20:34:08 +03:00
|
|
|
local_vars []Var // local function variables
|
2019-10-20 10:19:37 +03:00
|
|
|
var_idx int
|
|
|
|
returns bool
|
|
|
|
vroot string
|
2019-06-27 18:48:49 +03:00
|
|
|
is_c_struct_init bool
|
2019-08-20 14:34:29 +03:00
|
|
|
is_empty_c_struct_init bool
|
|
|
|
is_c_fn_call bool
|
2019-06-27 20:02:47 +03:00
|
|
|
can_chash bool
|
2019-08-17 22:19:37 +03:00
|
|
|
attr string
|
|
|
|
v_script bool // "V bash", import all os functions into global space
|
|
|
|
var_decl_name string // To allow declaring the variable so that it can be used in the struct initialization
|
|
|
|
is_alloc bool // Whether current expression resulted in an allocation
|
2019-10-07 01:31:01 +03:00
|
|
|
is_const_literal bool // `1`, `2.0` etc, so that `u64_var == 0` works
|
2019-10-20 10:19:37 +03:00
|
|
|
in_dispatch bool // dispatching generic instance?
|
2019-10-25 16:34:12 +03:00
|
|
|
is_vgen bool
|
2019-08-17 22:19:37 +03:00
|
|
|
is_vweb bool
|
|
|
|
is_sql bool
|
2019-09-14 23:48:30 +03:00
|
|
|
is_js bool
|
2019-08-17 22:19:37 +03:00
|
|
|
sql_i int // $1 $2 $3
|
2019-08-20 17:32:39 +03:00
|
|
|
sql_params []string // ("select * from users where id = $1", ***"100"***)
|
2019-08-20 18:05:56 +03:00
|
|
|
sql_types []string // int, string and so on; see sql_params
|
2019-10-07 01:31:01 +03:00
|
|
|
is_vh bool // parsing .vh file (for example `const (a int)` is allowed)
|
2019-10-21 14:14:28 +03:00
|
|
|
pub:
|
|
|
|
mod string
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:02:04 +03:00
|
|
|
const (
|
|
|
|
MaxModuleDepth = 4
|
2019-08-17 22:19:37 +03:00
|
|
|
)
|
2019-07-10 10:02:04 +03:00
|
|
|
|
2019-09-28 04:56:05 +03:00
|
|
|
// new parser from string. unique id specified in `id`.
|
|
|
|
// tip: use a hashing function to auto generate `id` from `text` eg. sha1.hexhash(text)
|
2019-10-25 16:34:12 +03:00
|
|
|
fn (v mut V) new_parser_from_string(text string) Parser {
|
|
|
|
mut p := v.new_parser(new_scanner(text))
|
2019-09-27 14:02:01 +03:00
|
|
|
p.scan_tokens()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2019-10-12 00:04:42 +03:00
|
|
|
fn (v mut V) reset_cgen_file_line_parameters(){
|
|
|
|
v.cgen.line = 0
|
|
|
|
v.cgen.file = ''
|
2019-10-12 13:54:01 +03:00
|
|
|
v.cgen.line_directives = v.pref.is_vlines
|
2019-10-12 00:04:42 +03:00
|
|
|
}
|
|
|
|
|
2019-10-04 15:48:09 +03:00
|
|
|
fn (v mut V) new_parser_from_file(path string) Parser {
|
2019-10-12 00:04:42 +03:00
|
|
|
v.reset_cgen_file_line_parameters()
|
2019-08-23 13:43:31 +03:00
|
|
|
//println('new_parser("$path")')
|
2019-08-04 21:39:43 +03:00
|
|
|
mut path_pcguard := ''
|
|
|
|
mut path_platform := '.v'
|
2019-10-23 08:18:44 +03:00
|
|
|
for path_ending in ['_lin.v', '_mac.v', '_win.v', '_nix.v', '_linux.v',
|
|
|
|
'_darwin.v', '_windows.v'] {
|
2019-08-04 21:39:43 +03:00
|
|
|
if path.ends_with(path_ending) {
|
2019-10-23 08:18:44 +03:00
|
|
|
if path_ending == '_mac.v' {
|
|
|
|
p := path_ending.replace('_mac.v', '_darwin.v')
|
|
|
|
println('warning: use "$p" file name instead of "$path"')
|
|
|
|
}
|
|
|
|
if path_ending == '_lin.v' {
|
|
|
|
p := path_ending.replace('_lin.v', '_linux.v')
|
|
|
|
println('warning: use "$p" file name instead of "$path"')
|
|
|
|
}
|
|
|
|
if path_ending == '_win.v' {
|
|
|
|
p := path_ending.replace('_win.v', '_windows.v')
|
|
|
|
println('warning: use "$p" file name instead of "$path"')
|
|
|
|
}
|
2019-08-04 21:39:43 +03:00
|
|
|
path_platform = path_ending
|
|
|
|
path_pcguard = platform_postfix_to_ifdefguard( path_ending )
|
|
|
|
break
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-08-04 21:39:43 +03:00
|
|
|
}
|
2019-09-23 13:42:20 +03:00
|
|
|
|
2019-10-25 16:34:12 +03:00
|
|
|
mut p := v.new_parser(new_scanner_file(path))
|
2019-09-27 14:02:01 +03:00
|
|
|
p = { p|
|
2019-10-25 16:34:12 +03:00
|
|
|
file_path: path,
|
2019-10-12 22:31:05 +03:00
|
|
|
file_name: path.all_after(os.path_separator),
|
2019-09-27 14:02:01 +03:00
|
|
|
file_platform: path_platform,
|
|
|
|
file_pcguard: path_pcguard,
|
2019-10-25 16:34:12 +03:00
|
|
|
is_vh: path.ends_with('.vh'),
|
|
|
|
v_script: path.ends_with('.vsh')
|
|
|
|
}
|
|
|
|
if p.v_script {
|
|
|
|
println('new_parser: V script')
|
2019-09-27 14:02:01 +03:00
|
|
|
}
|
2019-10-01 15:39:19 +03:00
|
|
|
if p.pref.building_v {
|
2019-10-09 23:38:33 +03:00
|
|
|
p.scanner.should_print_relative_paths_on_error = true
|
2019-10-01 15:39:19 +03:00
|
|
|
}
|
2019-10-23 08:18:44 +03:00
|
|
|
//if p.pref.generating_vh {
|
|
|
|
// Keep newlines
|
|
|
|
//p.scanner.is_vh = true
|
|
|
|
//}
|
2019-09-27 14:02:01 +03:00
|
|
|
p.scan_tokens()
|
|
|
|
//p.scanner.debug_tokens()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2019-09-28 04:56:05 +03:00
|
|
|
// creates a new parser. most likely you will want to use
|
|
|
|
// `new_parser_file` or `new_parser_string` instead.
|
2019-10-25 16:34:12 +03:00
|
|
|
fn (v mut V) new_parser(scanner &Scanner) Parser {
|
2019-10-12 00:04:42 +03:00
|
|
|
v.reset_cgen_file_line_parameters()
|
2019-06-22 21:20:28 +03:00
|
|
|
mut p := Parser {
|
2019-09-27 14:02:01 +03:00
|
|
|
scanner: scanner
|
2019-08-17 22:19:37 +03:00
|
|
|
v: v
|
2019-07-29 19:21:36 +03:00
|
|
|
table: v.table
|
2019-06-22 21:20:28 +03:00
|
|
|
cur_fn: EmptyFn
|
2019-07-29 19:21:36 +03:00
|
|
|
cgen: v.cgen
|
|
|
|
pref: v.pref
|
|
|
|
os: v.os
|
|
|
|
vroot: v.vroot
|
2019-09-23 20:34:08 +03:00
|
|
|
local_vars: [Var{}].repeat(MaxLocalVars)
|
2019-10-25 16:34:12 +03:00
|
|
|
import_table: new_import_table()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
$if js {
|
|
|
|
p.is_js = true
|
|
|
|
}
|
2019-09-10 13:08:43 +03:00
|
|
|
if p.pref.is_repl {
|
|
|
|
p.scanner.should_print_line_on_error = false
|
2019-09-29 04:33:23 +03:00
|
|
|
p.scanner.should_print_errors_in_color = false
|
2019-10-01 15:39:19 +03:00
|
|
|
p.scanner.should_print_relative_paths_on_error = true
|
2019-09-10 13:08:43 +03:00
|
|
|
}
|
2019-09-27 14:02:01 +03:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) scan_tokens() {
|
2019-09-27 04:00:48 +03:00
|
|
|
for {
|
2019-09-27 14:02:01 +03:00
|
|
|
res := p.scanner.scan()
|
2019-10-09 01:05:34 +03:00
|
|
|
p.tokens << Token{
|
2019-09-27 14:02:01 +03:00
|
|
|
tok: res.tok
|
|
|
|
lit: res.lit
|
|
|
|
line_nr: p.scanner.line_nr
|
2019-09-29 04:54:12 +03:00
|
|
|
col: p.scanner.pos - p.scanner.last_nl_pos
|
2019-09-27 14:02:01 +03:00
|
|
|
}
|
|
|
|
if res.tok == .eof {
|
|
|
|
break
|
|
|
|
}
|
2019-09-27 04:00:48 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-09-09 16:22:39 +03:00
|
|
|
fn (p mut Parser) set_current_fn(f Fn) {
|
2019-09-07 13:44:41 +03:00
|
|
|
p.cur_fn = f
|
2019-09-09 16:22:39 +03:00
|
|
|
//p.cur_fn = p.table.fns[f.name]
|
2019-09-07 13:44:41 +03:00
|
|
|
p.scanner.fn_name = '${f.mod}.${f.name}'
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (p mut Parser) next() {
|
2019-09-27 04:00:48 +03:00
|
|
|
p.prev_tok2 = p.prev_tok
|
|
|
|
p.prev_tok = p.tok
|
|
|
|
p.scanner.prev_tok = p.tok
|
|
|
|
if p.token_idx >= p.tokens.len {
|
2019-10-30 15:04:25 +03:00
|
|
|
p.tok = .eof
|
2019-09-27 14:02:01 +03:00
|
|
|
p.lit = ''
|
|
|
|
return
|
2019-09-27 04:00:48 +03:00
|
|
|
}
|
|
|
|
res := p.tokens[p.token_idx]
|
|
|
|
p.token_idx++
|
|
|
|
p.tok = res.tok
|
|
|
|
p.lit = res.lit
|
|
|
|
p.scanner.line_nr = res.line_nr
|
2019-10-12 00:04:42 +03:00
|
|
|
p.cgen.line = res.line_nr
|
2019-09-27 05:27:57 +03:00
|
|
|
}
|
2019-09-27 04:00:48 +03:00
|
|
|
|
2019-10-09 01:05:34 +03:00
|
|
|
fn (p & Parser) peek() TokenKind {
|
2019-09-27 04:00:48 +03:00
|
|
|
if p.token_idx >= p.tokens.len - 2 {
|
2019-10-30 15:04:25 +03:00
|
|
|
return .eof
|
2019-09-27 04:00:48 +03:00
|
|
|
}
|
|
|
|
tok := p.tokens[p.token_idx]
|
|
|
|
return tok.tok
|
|
|
|
}
|
|
|
|
|
2019-09-27 20:59:12 +03:00
|
|
|
// TODO remove dups
|
2019-10-09 01:05:34 +03:00
|
|
|
[inline] fn (p &Parser) prev_token() Token {
|
2019-09-27 20:59:12 +03:00
|
|
|
return p.tokens[p.token_idx - 2]
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-10-09 01:05:34 +03:00
|
|
|
[inline] fn (p &Parser) cur_tok() Token {
|
2019-09-27 20:59:12 +03:00
|
|
|
return p.tokens[p.token_idx - 1]
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-10-09 01:05:34 +03:00
|
|
|
[inline] fn (p &Parser) peek_token() Token {
|
2019-09-27 05:56:38 +03:00
|
|
|
if p.token_idx >= p.tokens.len - 2 {
|
2019-10-09 01:05:34 +03:00
|
|
|
return Token{ tok:TokenKind.eof }
|
2019-09-27 05:56:38 +03:00
|
|
|
}
|
2019-09-29 20:37:39 +03:00
|
|
|
return p.tokens[p.token_idx]
|
2019-09-27 05:56:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (p &Parser) log(s string) {
|
2019-08-17 22:19:37 +03:00
|
|
|
/*
|
2019-06-30 23:03:17 +03:00
|
|
|
if !p.pref.is_verbose {
|
2019-06-22 21:20:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
println(s)
|
2019-08-17 22:19:37 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-10-12 22:01:50 +03:00
|
|
|
fn (p mut Parser) parse(pass Pass) {
|
2019-10-12 00:04:42 +03:00
|
|
|
p.cgen.line = 0
|
2019-10-25 16:34:12 +03:00
|
|
|
p.cgen.file = cescaped_path(os.realpath(p.file_path))
|
2019-10-12 00:04:42 +03:00
|
|
|
/////////////////////////////////////
|
2019-08-23 13:43:31 +03:00
|
|
|
p.pass = pass
|
2019-09-27 04:00:48 +03:00
|
|
|
p.token_idx = 0
|
|
|
|
p.next()
|
2019-09-23 23:18:55 +03:00
|
|
|
//p.log('\nparse() run=$p.pass file=$p.file_name tok=${p.strtok()}')// , "script_file=", script_file)
|
2019-06-22 21:20:28 +03:00
|
|
|
// `module main` is not required if it's a single file program
|
2019-10-13 03:05:11 +03:00
|
|
|
if p.pref.is_script || p.pref.is_test {
|
2019-06-22 21:20:28 +03:00
|
|
|
// User may still specify `module main`
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .key_module {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
p.fgen('module ')
|
2019-10-13 03:05:11 +03:00
|
|
|
p.mod = p.check_name()
|
2019-10-12 01:17:37 +03:00
|
|
|
} else {
|
|
|
|
p.mod = 'main'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.key_module)
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fspace()
|
2019-10-13 03:05:11 +03:00
|
|
|
p.mod = p.check_name()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-07 01:31:01 +03:00
|
|
|
//
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-10-07 01:31:01 +03:00
|
|
|
p.cgen.nogen = false
|
|
|
|
if p.pref.build_mode == .build_module && p.mod != p.v.mod {
|
|
|
|
//println('skipping $p.mod (v.mod = $p.v.mod)')
|
|
|
|
p.cgen.nogen = true
|
|
|
|
//defer { p.cgen.nogen = false }
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgenln('\n')
|
2019-08-05 10:49:52 +03:00
|
|
|
p.builtin_mod = p.mod == 'builtin'
|
2019-08-20 14:34:29 +03:00
|
|
|
p.can_chash = p.mod=='ui' || p.mod == 'darwin'// TODO tmp remove
|
2019-06-22 21:20:28 +03:00
|
|
|
// Import pass - the first and the smallest pass that only analyzes imports
|
2019-10-13 03:05:11 +03:00
|
|
|
// if we are a building module get the full module name from v.mod
|
2019-10-15 04:46:40 +03:00
|
|
|
fq_mod := if p.pref.build_mode == .build_module && p.v.mod.ends_with(p.mod) {
|
2019-10-13 03:05:11 +03:00
|
|
|
p.v.mod
|
|
|
|
}
|
2019-10-12 03:38:56 +03:00
|
|
|
// fully qualify the module name, eg base64 to encoding.base64
|
2019-10-13 03:05:11 +03:00
|
|
|
else {
|
2019-10-25 16:34:12 +03:00
|
|
|
p.table.qualify_module(p.mod, p.file_path)
|
2019-10-13 03:05:11 +03:00
|
|
|
}
|
2019-08-05 10:49:52 +03:00
|
|
|
p.table.register_module(fq_mod)
|
2019-10-13 03:05:11 +03:00
|
|
|
p.mod = fq_mod
|
2019-10-12 01:17:37 +03:00
|
|
|
|
2019-07-29 19:21:36 +03:00
|
|
|
if p.pass == .imports {
|
2019-07-07 23:30:15 +03:00
|
|
|
for p.tok == .key_import && p.peek() != .key_const {
|
2019-07-16 18:59:07 +03:00
|
|
|
p.imports()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-11 15:07:18 +03:00
|
|
|
if 'builtin' in p.table.imports {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.error('module `builtin` cannot be imported')
|
2019-08-05 10:49:52 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Go through every top level token or throw a compilation error if a non-top level token is met
|
|
|
|
for {
|
2019-10-24 15:44:46 +03:00
|
|
|
match p.tok {
|
|
|
|
.key_import {
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.peek() == .key_const {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.const_decl()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO remove imported consts from the language
|
2019-07-16 18:59:07 +03:00
|
|
|
p.imports()
|
2019-08-17 22:19:37 +03:00
|
|
|
if p.tok != .key_import {
|
|
|
|
p.fgenln('')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
.key_enum {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .name {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgen('enum ')
|
|
|
|
name := p.check_name()
|
|
|
|
p.fgen(' ')
|
|
|
|
p.enum_decl(name)
|
|
|
|
}
|
2019-06-30 23:03:17 +03:00
|
|
|
else if p.pref.translated {
|
2019-10-24 13:19:27 +03:00
|
|
|
// enum without a name, only allowed in code,
|
|
|
|
// translated from C. it's a very bad practice
|
|
|
|
// in C as well, but is used unfortunately
|
|
|
|
// (for example, by DOOM). such fields are
|
|
|
|
// basically int consts
|
2019-06-22 21:20:28 +03:00
|
|
|
p.enum_decl('int')
|
|
|
|
}
|
|
|
|
else {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.name)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
.key_pub {
|
2019-10-24 12:19:05 +03:00
|
|
|
next := p.peek()
|
|
|
|
match next {
|
|
|
|
.key_fn { p.fn_decl() }
|
|
|
|
.key_const { p.const_decl() }
|
2019-10-27 02:23:52 +03:00
|
|
|
.key_struct, .key_union, .key_interface { p.struct_decl() }
|
2019-10-24 13:19:27 +03:00
|
|
|
.key_enum { p.enum_decl('') }
|
2019-10-24 12:19:05 +03:00
|
|
|
else {
|
|
|
|
p.error('wrong pub keyword usage')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
.key_fn {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fn_decl()
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
.key_type {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.type_decl()
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
.lsbr {
|
2019-07-07 22:46:21 +03:00
|
|
|
// `[` can only mean an [attribute] before a function
|
|
|
|
// or a struct definition
|
2019-08-17 22:19:37 +03:00
|
|
|
p.attribute()
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
.key_struct, .key_interface, .key_union, .lsbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.struct_decl()
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
.key_const {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.const_decl()
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
.hash {
|
|
|
|
// insert C code (only for ui module)
|
2019-06-22 21:20:28 +03:00
|
|
|
// # puts("hello");
|
|
|
|
p.chash()
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
.dollar {
|
2019-06-22 21:20:28 +03:00
|
|
|
// $if, $else
|
|
|
|
p.comp_time()
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
.key_global {
|
2019-08-17 22:19:37 +03:00
|
|
|
if !p.pref.translated && !p.pref.is_live &&
|
2019-10-30 15:04:25 +03:00
|
|
|
!p.builtin_mod && !p.pref.building_v &&
|
|
|
|
p.mod != 'ui' && !os.getwd().contains('/volt') {
|
2019-07-21 13:43:47 +03:00
|
|
|
p.error('__global is only allowed in translated code')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
name := p.check_name()
|
|
|
|
typ := p.get_type()
|
|
|
|
p.register_global(name, typ)
|
|
|
|
// p.genln(p.table.cgen_name_type_pair(name, typ))
|
|
|
|
mut g := p.table.cgen_name_type_pair(name, typ)
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .assign {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
g += ' = '
|
2019-10-07 01:31:01 +03:00
|
|
|
_, expr := p.tmp_expr()
|
|
|
|
g += expr
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// p.genln('; // global')
|
2019-08-17 22:19:37 +03:00
|
|
|
g += '; // global'
|
2019-10-07 01:31:01 +03:00
|
|
|
if !p.cgen.nogen {
|
|
|
|
p.cgen.consts << g
|
|
|
|
}
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
.eof {
|
2019-09-23 23:18:55 +03:00
|
|
|
//p.log('end of parse()')
|
2019-09-24 13:14:04 +03:00
|
|
|
// TODO: check why this was added? everything seems to work
|
|
|
|
// without it, and it's already happening in fn_decl
|
2019-10-13 03:05:11 +03:00
|
|
|
// if p.pref.is_script && !p.pref.is_test {
|
2019-09-24 06:18:06 +03:00
|
|
|
// p.set_current_fn( MainFn )
|
|
|
|
// p.check_unused_variables()
|
|
|
|
// }
|
2019-09-23 15:11:05 +03:00
|
|
|
if !p.first_pass() && !p.pref.is_repl {
|
|
|
|
p.check_unused_imports()
|
2019-06-28 21:50:29 +03:00
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
if false && !p.first_pass() && p.fileis('main.v') {
|
2019-07-03 22:07:42 +03:00
|
|
|
out := os.create('/var/tmp/fmt.v') or {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('failed to create fmt.v')
|
2019-08-29 03:30:17 +03:00
|
|
|
return
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-30 17:11:55 +03:00
|
|
|
out.writeln(p.scanner.fmt_out.str())
|
2019-06-22 21:20:28 +03:00
|
|
|
out.close()
|
|
|
|
}
|
|
|
|
return
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-06-22 21:20:28 +03:00
|
|
|
// no `fn main`, add this "global" statement to cgen.fn_main
|
2019-10-13 03:05:11 +03:00
|
|
|
if p.pref.is_script && !p.pref.is_test {
|
2019-06-28 13:41:09 +03:00
|
|
|
// cur_fn is empty since there was no fn main declared
|
2019-08-17 22:19:37 +03:00
|
|
|
// we need to set it to save and find variables
|
2019-06-28 13:41:09 +03:00
|
|
|
if p.cur_fn.name == '' {
|
2019-09-07 13:44:41 +03:00
|
|
|
p.set_current_fn( MainFn )
|
2019-06-30 23:03:17 +03:00
|
|
|
if p.pref.is_repl {
|
2019-10-13 17:57:18 +03:00
|
|
|
if p.first_pass() {
|
|
|
|
return
|
|
|
|
}
|
2019-09-23 20:34:08 +03:00
|
|
|
p.clear_vars()
|
2019-06-28 18:15:52 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-07-01 20:55:47 +03:00
|
|
|
mut start := p.cgen.lines.len
|
2019-06-22 21:20:28 +03:00
|
|
|
p.statement(true)
|
2019-10-13 17:57:18 +03:00
|
|
|
if start > 0 && p.cgen.lines[start - 1] != '' && p.cgen.fn_main != '' {
|
2019-07-01 20:55:47 +03:00
|
|
|
start--
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
p.genln('')
|
2019-06-22 21:20:28 +03:00
|
|
|
end := p.cgen.lines.len
|
|
|
|
lines := p.cgen.lines.slice(start, end)
|
2019-08-17 22:19:37 +03:00
|
|
|
//mut line := p.cgen.fn_main + lines.join('\n')
|
|
|
|
//line = line.trim_space()
|
2019-06-22 21:20:28 +03:00
|
|
|
p.cgen.fn_main = p.cgen.fn_main + lines.join('\n')
|
2019-07-18 21:21:23 +03:00
|
|
|
p.cgen.resetln('')
|
2019-06-22 21:20:28 +03:00
|
|
|
for i := start; i < end; i++ {
|
|
|
|
p.cgen.lines[i] = ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('unexpected token `${p.strtok()}`')
|
|
|
|
}
|
2019-10-24 15:44:46 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 18:59:07 +03:00
|
|
|
fn (p mut Parser) imports() {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.key_import)
|
2019-06-22 21:20:28 +03:00
|
|
|
// `import ()`
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .lpar {
|
|
|
|
p.check(.lpar)
|
|
|
|
for p.tok != .rpar && p.tok != .eof {
|
2019-07-30 04:49:47 +03:00
|
|
|
p.import_statement()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rpar)
|
2019-06-22 21:20:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// `import foo`
|
2019-07-30 04:49:47 +03:00
|
|
|
p.import_statement()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) import_statement() {
|
|
|
|
if p.tok != .name {
|
|
|
|
p.error('bad import format')
|
|
|
|
}
|
2019-10-24 19:44:49 +03:00
|
|
|
if p.peek() == .number {
|
2019-09-25 17:59:50 +03:00
|
|
|
p.error('bad import format. module/submodule names cannot begin with a number')
|
2019-07-30 04:49:47 +03:00
|
|
|
}
|
2019-10-08 02:32:12 +03:00
|
|
|
import_tok_idx := p.token_idx-1
|
2019-08-05 10:49:52 +03:00
|
|
|
mut mod := p.check_name().trim_space()
|
|
|
|
mut mod_alias := mod
|
2019-07-30 04:49:47 +03:00
|
|
|
// submodule support
|
|
|
|
mut depth := 1
|
|
|
|
for p.tok == .dot {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.dot)
|
2019-07-30 04:49:47 +03:00
|
|
|
submodule := p.check_name()
|
|
|
|
mod_alias = submodule
|
2019-08-05 10:49:52 +03:00
|
|
|
mod += '.' + submodule
|
2019-07-30 04:49:47 +03:00
|
|
|
depth++
|
2019-08-17 22:19:37 +03:00
|
|
|
if depth > MaxModuleDepth {
|
|
|
|
p.error('module depth of $MaxModuleDepth exceeded: $mod')
|
2019-07-30 04:49:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// aliasing (import encoding.base64 as b64)
|
|
|
|
if p.tok == .key_as && p.peek() == .name {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.key_as)
|
2019-07-30 04:49:47 +03:00
|
|
|
mod_alias = p.check_name()
|
|
|
|
}
|
|
|
|
// add import to file scope import table
|
2019-10-25 16:34:12 +03:00
|
|
|
p.register_import_alias(mod_alias, mod, import_tok_idx)
|
2019-07-30 04:49:47 +03:00
|
|
|
// Make sure there are no duplicate imports
|
2019-09-11 15:07:18 +03:00
|
|
|
if mod in p.table.imports {
|
2019-07-30 04:49:47 +03:00
|
|
|
return
|
|
|
|
}
|
2019-09-23 23:18:55 +03:00
|
|
|
//p.log('adding import $mod')
|
2019-08-05 10:49:52 +03:00
|
|
|
p.table.imports << mod
|
|
|
|
p.table.register_module(mod)
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-08-05 10:49:52 +03:00
|
|
|
p.fgenln(' ' + mod)
|
2019-07-24 00:12:55 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (p mut Parser) const_decl() {
|
2019-10-24 12:19:05 +03:00
|
|
|
is_pub := p.tok == .key_pub
|
|
|
|
if is_pub {
|
|
|
|
p.next()
|
|
|
|
}
|
2019-08-22 23:19:31 +03:00
|
|
|
p.inside_const = true
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.key_const)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fspace()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lpar)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgenln('')
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fmt_inc()
|
2019-07-07 23:30:15 +03:00
|
|
|
for p.tok == .name {
|
2019-10-07 01:31:01 +03:00
|
|
|
if p.lit == '_' && p.peek() == .assign && !p.cgen.nogen {
|
2019-09-28 13:59:54 +03:00
|
|
|
p.gen_blank_identifier_assign()
|
2019-10-07 01:31:01 +03:00
|
|
|
//if !p.cgen.nogen {
|
2019-09-28 13:59:54 +03:00
|
|
|
p.cgen.consts_init << p.cgen.cur_line.trim_space()
|
|
|
|
p.cgen.resetln('')
|
2019-10-07 01:31:01 +03:00
|
|
|
//}
|
2019-09-28 13:59:54 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-10-07 01:31:01 +03:00
|
|
|
mut name := p.check_name() // `Age = 20`
|
2019-10-12 22:38:06 +03:00
|
|
|
//if !p.pref.building_v && p.mod != 'os' && contains_capital(name) {
|
|
|
|
//p.warn('const names cannot contain uppercase letters, use snake_case instead')
|
|
|
|
//}
|
2019-08-22 23:19:31 +03:00
|
|
|
name = p.prepend_mod(name)
|
2019-10-07 01:31:01 +03:00
|
|
|
mut typ := ''
|
|
|
|
if p.is_vh {
|
|
|
|
// .vh files don't have const values, just types: `const (a int)`
|
2019-10-23 13:03:14 +03:00
|
|
|
if p.tok == .assign {
|
|
|
|
p.next()
|
|
|
|
typ = p.expression()
|
|
|
|
} else {
|
|
|
|
typ = p.get_type()
|
|
|
|
}
|
2019-10-24 12:19:05 +03:00
|
|
|
p.table.register_const(name, typ, p.mod, is_pub)
|
2019-10-10 02:59:33 +03:00
|
|
|
p.cgen.consts << ('extern ' +
|
|
|
|
p.table.cgen_name_type_pair(name, typ)) + ';'
|
2019-10-07 01:31:01 +03:00
|
|
|
continue // Don't generate C code when building a .vh file
|
|
|
|
} else {
|
|
|
|
p.check_space(.assign)
|
|
|
|
typ = p.expression()
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-08-22 23:19:31 +03:00
|
|
|
if p.first_pass() && p.table.known_const(name) {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.error('redefinition of `$name`')
|
|
|
|
}
|
2019-10-04 15:48:09 +03:00
|
|
|
if p.first_pass() {
|
2019-10-24 12:19:05 +03:00
|
|
|
p.table.register_const(name, typ, p.mod, is_pub)
|
2019-10-04 15:48:09 +03:00
|
|
|
}
|
2019-10-21 08:57:29 +03:00
|
|
|
// Check to see if this constant exists, and is void. If so, try and get the type again:
|
2019-10-21 22:04:58 +03:00
|
|
|
if my_const := p.v.table.find_const(name) {
|
2019-10-21 08:57:29 +03:00
|
|
|
if my_const.typ == 'void' {
|
|
|
|
for i, v in p.v.table.consts {
|
|
|
|
if v.name == name {
|
|
|
|
p.v.table.consts[i].typ = typ
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 01:31:01 +03:00
|
|
|
if p.pass == .main && !p.cgen.nogen {
|
2019-06-22 21:20:28 +03:00
|
|
|
// TODO hack
|
|
|
|
// cur_line has const's value right now. if it's just a number, then optimize generation:
|
|
|
|
// output a #define so that we don't pollute the binary with unnecessary global vars
|
2019-10-10 02:59:33 +03:00
|
|
|
// Do not do this when building a module, otherwise the consts
|
|
|
|
// will not be accessible.
|
|
|
|
if p.pref.build_mode != .build_module && is_compile_time_const(p.cgen.cur_line) {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.cgen.consts << '#define $name $p.cgen.cur_line'
|
2019-07-18 21:21:23 +03:00
|
|
|
p.cgen.resetln('')
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgenln('')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if typ.starts_with('[') {
|
|
|
|
p.cgen.consts << p.table.cgen_name_type_pair(name, typ) +
|
|
|
|
' = $p.cgen.cur_line;'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.cgen.consts << p.table.cgen_name_type_pair(name, typ) + ';'
|
|
|
|
p.cgen.consts_init << '$name = $p.cgen.cur_line;'
|
|
|
|
}
|
2019-07-18 21:21:23 +03:00
|
|
|
p.cgen.resetln('')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
p.fgenln('')
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fmt_dec()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rpar)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgenln('\n')
|
|
|
|
p.inside_const = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// `type myint int`
|
|
|
|
// `type onclickfn fn(voidptr) int`
|
|
|
|
fn (p mut Parser) type_decl() {
|
2019-07-16 13:17:17 +03:00
|
|
|
p.check(.key_type)
|
2019-06-22 21:20:28 +03:00
|
|
|
name := p.check_name()
|
2019-08-10 14:08:59 +03:00
|
|
|
// V used to have 'type Foo struct', many Go users might use this syntax
|
|
|
|
if p.tok == .key_struct {
|
|
|
|
p.error('use `struct $name {` instead of `type $name struct {`')
|
|
|
|
}
|
2019-09-02 14:17:20 +03:00
|
|
|
parent := p.get_type2()
|
|
|
|
nt_pair := p.table.cgen_name_type_pair(name, parent.name)
|
2019-06-22 21:20:28 +03:00
|
|
|
// TODO dirty C typedef hacks for DOOM
|
|
|
|
// Unknown type probably means it's a struct, and it's used before the struct is defined,
|
|
|
|
// so specify "struct"
|
2019-09-02 14:17:20 +03:00
|
|
|
_struct := if parent.cat != .array && parent.cat != .func &&
|
|
|
|
!p.table.known_type(parent.name) {
|
|
|
|
'struct'
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
}
|
|
|
|
p.gen_typedef('typedef $_struct $nt_pair; //type alias name="$name" parent=`$parent.name`')
|
2019-10-12 04:09:37 +03:00
|
|
|
p.table.register_type2(Type{
|
|
|
|
name: name
|
|
|
|
parent: parent.name
|
|
|
|
mod: p.mod
|
|
|
|
cat: TypeCategory.alias
|
|
|
|
})
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-09-27 20:59:12 +03:00
|
|
|
// current token is `(`
|
2019-07-03 22:53:25 +03:00
|
|
|
fn (p mut Parser) interface_method(field_name, receiver string) &Fn {
|
|
|
|
mut method := &Fn {
|
|
|
|
name: field_name
|
|
|
|
is_interface: true
|
|
|
|
is_method: true
|
|
|
|
receiver_typ: receiver
|
|
|
|
}
|
2019-09-23 23:18:55 +03:00
|
|
|
//p.log('is interface. field=$field_name run=$p.pass')
|
2019-07-03 22:53:25 +03:00
|
|
|
p.fn_args(mut method)
|
2019-09-27 20:59:12 +03:00
|
|
|
prev_tok := p.prev_token()
|
|
|
|
cur_tok := p.cur_tok()
|
|
|
|
// No type on the same line, this method doesn't return a type, process next
|
|
|
|
if prev_tok.line_nr != cur_tok.line_nr {
|
2019-07-03 22:53:25 +03:00
|
|
|
method.typ = 'void'
|
|
|
|
} else {
|
|
|
|
method.typ = p.get_type()// method return type
|
|
|
|
p.fspace()
|
|
|
|
p.fgenln('')
|
|
|
|
}
|
|
|
|
return method
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:05:34 +03:00
|
|
|
fn key_to_type_cat(tok TokenKind) TypeCategory {
|
2019-10-24 19:19:03 +03:00
|
|
|
match tok {
|
2019-10-24 19:44:49 +03:00
|
|
|
.key_interface { return .interface_ }
|
|
|
|
.key_struct { return .struct_ }
|
|
|
|
.key_union { return .union_ }
|
|
|
|
//TokenKind.key_ => return .interface_
|
2019-08-20 14:34:29 +03:00
|
|
|
}
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('Unknown token: $tok')
|
2019-10-24 19:44:49 +03:00
|
|
|
return .builtin
|
2019-08-20 14:34:29 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// check_name checks for a name token and returns its literal
|
|
|
|
fn (p mut Parser) check_name() string {
|
|
|
|
name := p.lit
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.name)
|
2019-06-22 21:20:28 +03:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) check_string() string {
|
|
|
|
s := p.lit
|
2019-07-16 18:59:07 +03:00
|
|
|
p.check(.str)
|
2019-06-22 21:20:28 +03:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p &Parser) strtok() string {
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .name {
|
2019-06-22 21:20:28 +03:00
|
|
|
return p.lit
|
|
|
|
}
|
2019-07-16 18:59:07 +03:00
|
|
|
if p.tok == .str {
|
2019-06-22 21:20:28 +03:00
|
|
|
return '"$p.lit"'
|
|
|
|
}
|
|
|
|
res := p.tok.str()
|
|
|
|
if res == '' {
|
|
|
|
n := int(p.tok)
|
|
|
|
return n.str()
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:30:04 +03:00
|
|
|
// same as check(), but adds a space to the formatter output
|
2019-06-22 21:20:28 +03:00
|
|
|
// TODO bad name
|
2019-10-09 01:05:34 +03:00
|
|
|
fn (p mut Parser) check_space(expected TokenKind) {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fspace()
|
|
|
|
p.check(expected)
|
|
|
|
p.fspace()
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:05:34 +03:00
|
|
|
fn (p mut Parser) check(expected TokenKind) {
|
2019-06-22 21:20:28 +03:00
|
|
|
if p.tok != expected {
|
|
|
|
println('check()')
|
2019-07-25 14:16:17 +03:00
|
|
|
s := 'expected `${expected.str()}` but got `${p.strtok()}`'
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
println('next token = `${p.strtok()}`')
|
|
|
|
print_backtrace()
|
|
|
|
p.error(s)
|
|
|
|
}
|
2019-09-26 05:28:43 +03:00
|
|
|
/*
|
2019-07-07 23:30:15 +03:00
|
|
|
if expected == .rcbr {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fmt_dec()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
p.fgen(p.strtok())
|
|
|
|
// vfmt: increase indentation on `{` unless it's `{}`
|
2019-09-26 05:28:43 +03:00
|
|
|
// TODO
|
2019-08-09 21:19:37 +03:00
|
|
|
if expected == .lcbr && p.scanner.pos + 1 < p.scanner.text.len && p.scanner.text[p.scanner.pos + 1] != `}` {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgenln('')
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fmt_inc()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-26 05:28:43 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
2019-07-16 18:59:07 +03:00
|
|
|
|
2019-10-24 19:44:49 +03:00
|
|
|
//if p.scanner.line_comment != '' {
|
|
|
|
//p.fgenln('// ! "$p.scanner.line_comment"')
|
|
|
|
//p.scanner.line_comment = ''
|
|
|
|
//}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-04 15:48:09 +03:00
|
|
|
[inline]
|
2019-07-29 19:21:36 +03:00
|
|
|
fn (p &Parser) first_pass() bool {
|
|
|
|
return p.pass == .decl
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO return Type instead of string?
|
|
|
|
fn (p mut Parser) get_type() string {
|
|
|
|
mut mul := false
|
|
|
|
mut nr_muls := 0
|
2019-06-28 15:19:46 +03:00
|
|
|
mut typ := ''
|
2019-09-23 05:45:19 +03:00
|
|
|
// multiple returns
|
|
|
|
if p.tok == .lpar {
|
2019-09-23 13:39:35 +03:00
|
|
|
// if p.inside_tuple {p.error('unexpected (')}
|
2019-09-23 20:34:08 +03:00
|
|
|
// p.inside_tuple = true
|
|
|
|
p.check(.lpar)
|
|
|
|
mut types := []string
|
2019-09-23 05:45:19 +03:00
|
|
|
for {
|
2019-09-23 13:39:35 +03:00
|
|
|
types << p.get_type()
|
2019-09-23 05:45:19 +03:00
|
|
|
if p.tok != .comma {
|
2019-09-23 20:34:08 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
p.check(.comma)
|
2019-09-23 05:45:19 +03:00
|
|
|
}
|
2019-09-23 20:34:08 +03:00
|
|
|
p.check(.rpar)
|
|
|
|
// p.inside_tuple = false
|
2019-10-09 20:55:36 +03:00
|
|
|
typ = p.register_multi_return_stuct(types)
|
|
|
|
return typ
|
2019-09-23 05:45:19 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// fn type
|
2019-10-21 14:21:30 +03:00
|
|
|
if p.tok == .key_fn {
|
2019-08-05 10:49:52 +03:00
|
|
|
mut f := Fn{name: '_', mod: p.mod}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
line_nr := p.scanner.line_nr
|
|
|
|
p.fn_args(mut f)
|
|
|
|
// Same line, it's a return type
|
|
|
|
if p.scanner.line_nr == line_nr {
|
2019-10-30 15:04:25 +03:00
|
|
|
if p.tok in [.name, .mul, .amp, .lsbr, .question, .lpar] {
|
2019-07-16 15:31:08 +03:00
|
|
|
f.typ = p.get_type()
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-07-16 15:31:08 +03:00
|
|
|
f.typ = 'void'
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// println('fn return typ=$f.typ')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f.typ = 'void'
|
|
|
|
}
|
|
|
|
// Register anon fn type
|
|
|
|
fn_typ := Type {
|
|
|
|
name: f.typ_str()// 'fn (int, int) string'
|
2019-07-07 23:30:15 +03:00
|
|
|
mod: p.mod
|
2019-06-22 21:20:28 +03:00
|
|
|
func: f
|
|
|
|
}
|
|
|
|
p.table.register_type2(fn_typ)
|
|
|
|
return f.typ_str()
|
|
|
|
}
|
|
|
|
// arrays ([]int)
|
2019-10-17 06:52:58 +03:00
|
|
|
mut arr_level := 0
|
2019-07-07 23:30:15 +03:00
|
|
|
is_question := p.tok == .question
|
2019-06-22 21:20:28 +03:00
|
|
|
if is_question {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.question)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-17 06:52:58 +03:00
|
|
|
for p.tok == .lsbr {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lsbr)
|
2019-06-22 21:20:28 +03:00
|
|
|
// [10]int
|
2019-07-24 01:06:48 +03:00
|
|
|
if p.tok == .number {
|
2019-10-17 06:52:58 +03:00
|
|
|
typ += '[$p.lit]'
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
else {
|
2019-10-17 06:52:58 +03:00
|
|
|
arr_level++
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rsbr)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
// map[string]int
|
2019-08-05 10:49:52 +03:00
|
|
|
if !p.builtin_mod && p.tok == .name && p.lit == 'map' {
|
2019-06-30 23:44:15 +03:00
|
|
|
p.next()
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.lsbr)
|
|
|
|
key_type := p.check_name()
|
2019-06-30 23:44:15 +03:00
|
|
|
if key_type != 'string' {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.error('maps only support string keys for now')
|
|
|
|
}
|
|
|
|
p.check(.rsbr)
|
|
|
|
val_type := p.get_type()// p.check_name()
|
|
|
|
typ = 'map_$val_type'
|
2019-06-30 23:44:15 +03:00
|
|
|
p.register_map(typ)
|
2019-08-17 22:19:37 +03:00
|
|
|
return typ
|
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
|
|
|
|
// ptr/ref
|
2019-09-09 16:22:39 +03:00
|
|
|
mut warn := false
|
2019-07-07 23:30:15 +03:00
|
|
|
for p.tok == .mul {
|
2019-09-01 22:51:16 +03:00
|
|
|
if p.first_pass() {
|
2019-09-09 16:22:39 +03:00
|
|
|
warn = true
|
2019-09-01 22:51:16 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
mul = true
|
|
|
|
nr_muls++
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.mul)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .amp {
|
2019-06-22 21:20:28 +03:00
|
|
|
mul = true
|
|
|
|
nr_muls++
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.amp)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
|
|
|
|
// Generic type check
|
|
|
|
ti := p.cur_fn.dispatch_of.inst
|
|
|
|
if p.lit in ti.keys() {
|
|
|
|
typ += ti[p.lit]
|
|
|
|
// println('cur dispatch: $p.lit => $typ')
|
|
|
|
} else {
|
|
|
|
typ += p.lit
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
if !p.is_struct_init {
|
2019-08-20 14:34:29 +03:00
|
|
|
// Otherwise we get `foo := FooFoo{` because `Foo` was already
|
|
|
|
// generated in name_expr()
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgen(p.lit)
|
|
|
|
}
|
|
|
|
// C.Struct import
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.lit == 'C' && p.peek() == .dot {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.dot)
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = p.lit
|
|
|
|
}
|
|
|
|
else {
|
2019-09-24 22:30:30 +03:00
|
|
|
if warn && p.mod != 'ui' {
|
2019-09-09 16:22:39 +03:00
|
|
|
p.warn('use `&Foo` instead of `*Foo`')
|
|
|
|
}
|
2019-08-05 10:49:52 +03:00
|
|
|
// Module specified? (e.g. gx.Image)
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.peek() == .dot {
|
2019-08-08 15:35:53 +03:00
|
|
|
// try resolve full submodule
|
|
|
|
if !p.builtin_mod && p.import_table.known_alias(typ) {
|
|
|
|
mod := p.import_table.resolve_alias(typ)
|
|
|
|
if mod.contains('.') {
|
2019-10-12 01:17:37 +03:00
|
|
|
typ = mod_gen_name(mod)
|
2019-08-08 15:35:53 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.dot)
|
2019-06-22 21:20:28 +03:00
|
|
|
typ += '__$p.lit'
|
|
|
|
}
|
|
|
|
mut t := p.table.find_type(typ)
|
2019-08-05 10:49:52 +03:00
|
|
|
// "typ" not found? try "mod__typ"
|
|
|
|
if t.name == '' && !p.builtin_mod {
|
2019-07-29 19:21:36 +03:00
|
|
|
// && !p.first_pass() {
|
2019-07-07 23:30:15 +03:00
|
|
|
if !typ.contains('array_') && p.mod != 'main' && !typ.contains('__') &&
|
2019-08-17 22:19:37 +03:00
|
|
|
!typ.starts_with('[') {
|
2019-08-05 10:49:52 +03:00
|
|
|
typ = p.prepend_mod(typ)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
t = p.table.find_type(typ)
|
2019-07-29 19:21:36 +03:00
|
|
|
if t.name == '' && !p.pref.translated && !p.first_pass() && !typ.starts_with('[') {
|
2019-06-22 21:20:28 +03:00
|
|
|
println('get_type() bad type')
|
|
|
|
// println('all registered types:')
|
|
|
|
// for q in p.table.types {
|
|
|
|
// println(q.name)
|
|
|
|
// }
|
|
|
|
p.error('unknown type `$typ`')
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 19:44:49 +03:00
|
|
|
else if !t.is_public && t.mod != p.mod && t.name != '' && !p.first_pass() {
|
2019-10-24 13:19:27 +03:00
|
|
|
p.error('type `$t.name` is private')
|
2019-10-24 12:47:21 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
if typ == 'void' {
|
|
|
|
p.error('unknown type `$typ`')
|
|
|
|
}
|
|
|
|
if mul {
|
2019-07-03 23:11:27 +03:00
|
|
|
typ += strings.repeat(`*`, nr_muls)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// Register an []array type
|
2019-10-17 06:52:58 +03:00
|
|
|
if arr_level > 0 {
|
2019-07-29 19:21:36 +03:00
|
|
|
// p.log('ARR TYPE="$typ" run=$p.pass')
|
2019-06-22 21:20:28 +03:00
|
|
|
// We come across "[]User" etc ?
|
2019-10-17 06:52:58 +03:00
|
|
|
for i := 0; i < arr_level; i++ {
|
|
|
|
typ = 'array_$typ'
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.register_array(typ)
|
|
|
|
}
|
|
|
|
p.next()
|
2019-09-23 23:17:06 +03:00
|
|
|
if is_question {
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = 'Option_$typ'
|
|
|
|
p.table.register_type_with_parent(typ, 'Option')
|
|
|
|
}
|
|
|
|
// Because the code uses * to see if it's a pointer
|
|
|
|
if typ == 'byteptr' {
|
|
|
|
return 'byte*'
|
|
|
|
}
|
|
|
|
if typ == 'voidptr' {
|
2019-08-05 10:49:52 +03:00
|
|
|
//if !p.builtin_mod && p.mod != 'os' && p.mod != 'gx' && p.mod != 'gg' && !p.pref.translated {
|
2019-06-22 21:20:28 +03:00
|
|
|
//p.error('voidptr can only be used in unsafe code')
|
|
|
|
//}
|
|
|
|
return 'void*'
|
|
|
|
}
|
|
|
|
if typ.last_index('__') > typ.index('__') {
|
|
|
|
p.error('2 __ in gettype(): typ="$typ"')
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p &Parser) print_tok() {
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .name {
|
2019-06-22 21:20:28 +03:00
|
|
|
println(p.lit)
|
|
|
|
return
|
|
|
|
}
|
2019-07-16 18:59:07 +03:00
|
|
|
if p.tok == .str {
|
2019-06-22 21:20:28 +03:00
|
|
|
println('"$p.lit"')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println(p.tok.str())
|
|
|
|
}
|
|
|
|
|
|
|
|
// statements() returns the type of the last statement
|
|
|
|
fn (p mut Parser) statements() string {
|
2019-09-23 23:18:55 +03:00
|
|
|
//p.log('statements()')
|
2019-07-24 03:35:25 +03:00
|
|
|
typ := p.statements_no_rcbr()
|
2019-06-22 21:20:28 +03:00
|
|
|
if !p.inside_if_expr {
|
|
|
|
p.genln('}')
|
|
|
|
}
|
2019-07-24 03:35:25 +03:00
|
|
|
//if p.fileis('if_expr') {
|
|
|
|
//println('statements() ret=$typ line=$p.scanner.line_nr')
|
|
|
|
//}
|
2019-06-22 21:20:28 +03:00
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2019-07-24 03:35:25 +03:00
|
|
|
fn (p mut Parser) statements_no_rcbr() string {
|
2019-09-09 16:22:39 +03:00
|
|
|
p.open_scope()
|
2019-08-27 00:39:11 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
if !p.inside_if_expr {
|
|
|
|
p.genln('')
|
|
|
|
}
|
|
|
|
mut i := 0
|
|
|
|
mut last_st_typ := ''
|
2019-10-27 14:05:50 +03:00
|
|
|
for p.tok != .rcbr && p.tok != .eof {
|
2019-10-20 10:19:37 +03:00
|
|
|
// println('stm: '+p.tok.str()+', next: '+p.peek().str())
|
2019-06-22 21:20:28 +03:00
|
|
|
last_st_typ = p.statement(true)
|
|
|
|
// println('last st typ=$last_st_typ')
|
|
|
|
if !p.inside_if_expr {
|
|
|
|
p.genln('')// // end st tok= ${p.strtok()}')
|
|
|
|
p.fgenln('')
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
if i > 50000 {
|
|
|
|
p.cgen.save()
|
|
|
|
p.error('more than 50 000 statements in function `$p.cur_fn.name`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// p.next()
|
2019-10-27 14:05:50 +03:00
|
|
|
p.check(.rcbr)
|
2019-08-17 22:19:37 +03:00
|
|
|
//p.fmt_dec()
|
2019-08-27 00:39:11 +03:00
|
|
|
|
2019-07-21 13:43:47 +03:00
|
|
|
p.close_scope()
|
2019-06-22 21:20:28 +03:00
|
|
|
return last_st_typ
|
|
|
|
}
|
|
|
|
|
2019-07-21 13:43:47 +03:00
|
|
|
fn (p mut Parser) close_scope() {
|
|
|
|
// println('close_scope level=$f.scope_level var_idx=$f.var_idx')
|
2019-09-08 18:53:40 +03:00
|
|
|
// Move back `var_idx` (pointer to the end of the array) till we reach
|
|
|
|
// the previous scope level. This effectivly deletes (closes) current
|
|
|
|
// scope.
|
2019-09-23 20:34:08 +03:00
|
|
|
mut i := p.var_idx - 1
|
2019-08-02 06:20:18 +03:00
|
|
|
for ; i >= 0; i-- {
|
2019-09-23 20:34:08 +03:00
|
|
|
v := p.local_vars[i]
|
2019-09-27 18:55:30 +03:00
|
|
|
if v.scope_level != p.cur_fn.scope_level {
|
2019-07-21 13:43:47 +03:00
|
|
|
break
|
|
|
|
}
|
2019-09-14 18:54:55 +03:00
|
|
|
// Clean up memory, only do this if -autofree was passed for now
|
2019-09-24 22:30:30 +03:00
|
|
|
if p.pref.autofree && v.is_alloc { // && !p.pref.is_test {
|
2019-09-09 16:22:39 +03:00
|
|
|
mut free_fn := 'free'
|
2019-08-17 22:19:37 +03:00
|
|
|
if v.typ.starts_with('array_') {
|
2019-09-09 16:22:39 +03:00
|
|
|
free_fn = 'v_array_free'
|
|
|
|
} else if v.typ == 'string' {
|
|
|
|
free_fn = 'v_string_free'
|
2019-09-24 00:40:34 +03:00
|
|
|
//if p.fileis('str.v') {
|
|
|
|
//println('freeing str $v.name')
|
|
|
|
//}
|
2019-10-02 15:31:03 +03:00
|
|
|
continue
|
2019-09-09 16:22:39 +03:00
|
|
|
} else if v.ptr || v.typ.ends_with('*') {
|
|
|
|
free_fn = 'v_ptr_free'
|
|
|
|
//continue
|
|
|
|
} else {
|
|
|
|
continue
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-09-09 16:22:39 +03:00
|
|
|
if p.returns {
|
2019-09-24 22:30:30 +03:00
|
|
|
// Don't free a variable that's being returned
|
2019-09-09 16:22:39 +03:00
|
|
|
if !v.is_returned && v.typ != 'FILE*' { //!v.is_c {
|
|
|
|
prev_line := p.cgen.lines[p.cgen.lines.len-2]
|
|
|
|
p.cgen.lines[p.cgen.lines.len-2] =
|
|
|
|
'$free_fn($v.name); /* :) close_scope free $v.typ */' + prev_line
|
2019-09-08 18:53:40 +03:00
|
|
|
}
|
2019-09-09 16:22:39 +03:00
|
|
|
} else {
|
|
|
|
p.genln('$free_fn($v.name); // close_scope free')
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-09-13 16:27:01 +03:00
|
|
|
}
|
2019-08-07 18:51:21 +03:00
|
|
|
}
|
|
|
|
if p.cur_fn.defer_text.last() != '' {
|
|
|
|
p.genln(p.cur_fn.defer_text.last())
|
|
|
|
//p.cur_fn.defer_text[f] = ''
|
2019-07-21 13:43:47 +03:00
|
|
|
}
|
2019-09-09 16:22:39 +03:00
|
|
|
p.cur_fn.scope_level--
|
2019-10-27 10:03:15 +03:00
|
|
|
p.cur_fn.defer_text = p.cur_fn.defer_text[..p.cur_fn.scope_level + 1]
|
2019-09-23 20:34:08 +03:00
|
|
|
p.var_idx = i + 1
|
2019-07-21 13:43:47 +03:00
|
|
|
// println('close_scope new var_idx=$f.var_idx\n')
|
2019-08-07 18:51:21 +03:00
|
|
|
}
|
2019-07-21 13:43:47 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (p mut Parser) genln(s string) {
|
|
|
|
p.cgen.genln(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) gen(s string) {
|
|
|
|
p.cgen.gen(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate V header from V source
|
|
|
|
fn (p mut Parser) statement(add_semi bool) string {
|
2019-10-24 15:41:29 +03:00
|
|
|
p.expected_type = ''
|
2019-08-12 17:21:56 +03:00
|
|
|
if p.returns && !p.is_vweb {
|
2019-08-10 11:26:42 +03:00
|
|
|
p.error('unreachable code')
|
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
// if !p.in_dispatch {
|
|
|
|
p.cgen.is_tmp = false
|
|
|
|
// }
|
2019-06-22 21:20:28 +03:00
|
|
|
tok := p.tok
|
|
|
|
mut q := ''
|
2019-10-24 16:02:24 +03:00
|
|
|
match tok {
|
|
|
|
.name {
|
2019-06-22 21:20:28 +03:00
|
|
|
next := p.peek()
|
2019-10-07 01:09:11 +03:00
|
|
|
//if p.pref.is_verbose {
|
|
|
|
//println(next.str())
|
|
|
|
//}
|
2019-06-22 21:20:28 +03:00
|
|
|
// goto_label:
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.peek() == .colon {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fmt_dec()
|
|
|
|
label := p.check_name()
|
|
|
|
p.fmt_inc()
|
2019-10-25 18:53:45 +03:00
|
|
|
p.genln(label + ': ;')
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.colon)
|
2019-06-22 21:20:28 +03:00
|
|
|
return ''
|
|
|
|
}
|
2019-09-23 20:34:08 +03:00
|
|
|
// `a := 777`
|
2019-09-23 05:45:19 +03:00
|
|
|
else if p.peek() == .decl_assign || p.peek() == .comma {
|
2019-09-23 23:18:55 +03:00
|
|
|
//p.log('var decl')
|
2019-06-22 21:20:28 +03:00
|
|
|
p.var_decl()
|
|
|
|
}
|
2019-09-25 14:53:44 +03:00
|
|
|
// `_ = 777`
|
|
|
|
else if p.lit == '_' && p.peek() == .assign {
|
|
|
|
p.gen_blank_identifier_assign()
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
else {
|
2019-08-09 09:17:31 +03:00
|
|
|
// panic and exit count as returns since they stop the function
|
|
|
|
if p.lit == 'panic' || p.lit == 'exit' {
|
|
|
|
p.returns = true
|
|
|
|
}
|
2019-08-20 14:34:29 +03:00
|
|
|
// `a + 3`, `a(7)`, or just `a`
|
2019-06-22 21:20:28 +03:00
|
|
|
q = p.bool_expression()
|
|
|
|
}
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_goto {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.key_goto)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgen(' ')
|
|
|
|
label := p.check_name()
|
|
|
|
p.genln('goto $label;')
|
|
|
|
return ''
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_defer {
|
2019-07-15 23:09:34 +03:00
|
|
|
p.defer_st()
|
|
|
|
return ''
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.hash {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.chash()
|
|
|
|
return ''
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_unsafe {
|
2019-10-20 19:59:53 +03:00
|
|
|
p.next()
|
|
|
|
p.inside_unsafe = true
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.genln('{')
|
|
|
|
p.statements()
|
|
|
|
p.inside_unsafe = false
|
|
|
|
//p.check(.rcbr)
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.dollar {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.comp_time()
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_if {
|
2019-07-12 07:54:01 +03:00
|
|
|
p.if_st(false, 0)
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_for {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.for_st()
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_switch {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.switch_statement()
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_match {
|
2019-08-27 00:39:11 +03:00
|
|
|
p.match_statement(false)
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_mut, .key_static {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.var_decl()
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_return {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.return_st()
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.lcbr {// {} block
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.lcbr)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.genln('{')
|
|
|
|
p.statements()
|
|
|
|
return ''
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_continue {
|
2019-06-22 21:20:28 +03:00
|
|
|
if p.for_expr_cnt == 0 {
|
|
|
|
p.error('`continue` statement outside `for`')
|
|
|
|
}
|
|
|
|
p.genln('continue')
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.key_continue)
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_break {
|
2019-06-22 21:20:28 +03:00
|
|
|
if p.for_expr_cnt == 0 {
|
|
|
|
p.error('`break` statement outside `for`')
|
|
|
|
}
|
|
|
|
p.genln('break')
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.key_break)
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_go {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.go_statement()
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.key_assert {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.assert_statement()
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-06-22 21:20:28 +03:00
|
|
|
// An expression as a statement
|
|
|
|
typ := p.expression()
|
|
|
|
if p.inside_if_expr {
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln('; ')
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// ? : uses , as statement separators
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.inside_if_expr && p.tok != .rcbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(', ')
|
|
|
|
}
|
|
|
|
if add_semi && !p.inside_if_expr {
|
|
|
|
p.genln(';')
|
|
|
|
}
|
|
|
|
return q
|
|
|
|
// p.cgen.end_statement()
|
|
|
|
}
|
|
|
|
|
|
|
|
// is_map: are we in map assignment? (m[key] = val) if yes, dont generate '='
|
|
|
|
// this can be `user = ...` or `user.field = ...`, in both cases `v` is `user`
|
|
|
|
fn (p mut Parser) assign_statement(v Var, ph int, is_map bool) {
|
2019-09-29 20:37:39 +03:00
|
|
|
errtok := p.cur_tok_index()
|
2019-09-09 03:29:24 +03:00
|
|
|
//p.log('assign_statement() name=$v.name tok=')
|
2019-08-17 22:19:37 +03:00
|
|
|
is_vid := p.fileis('vid') // TODO remove
|
2019-06-22 21:20:28 +03:00
|
|
|
tok := p.tok
|
2019-08-07 09:19:27 +03:00
|
|
|
//if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{
|
2019-08-07 14:00:19 +03:00
|
|
|
if !v.is_mut && !p.pref.translated && !v.is_global && !is_vid {
|
2019-08-08 07:55:08 +03:00
|
|
|
if v.is_arg {
|
|
|
|
if p.cur_fn.args.len > 0 && p.cur_fn.args[0].name == v.name {
|
|
|
|
println('make the receiver `$v.name` mutable:
|
2019-08-17 22:19:37 +03:00
|
|
|
fn ($v.name mut $v.typ) $p.cur_fn.name (...) {
|
|
|
|
')
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 17:59:50 +03:00
|
|
|
p.error('`$v.name` is immutable')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-25 14:16:17 +03:00
|
|
|
if !v.is_changed {
|
2019-09-09 16:22:39 +03:00
|
|
|
p.mark_var_changed(v)
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
is_str := v.typ == 'string'
|
2019-09-20 19:07:38 +03:00
|
|
|
is_ustr := v.typ == 'ustring'
|
2019-10-24 16:02:24 +03:00
|
|
|
match tok {
|
|
|
|
.assign {
|
2019-08-20 14:34:29 +03:00
|
|
|
if !is_map && !p.is_empty_c_struct_init {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(' = ')
|
|
|
|
}
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
.plus_assign {
|
2019-09-14 23:48:30 +03:00
|
|
|
if is_str && !p.is_js {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('= string_add($v.name, ')// TODO can't do `foo.bar += '!'`
|
|
|
|
}
|
2019-09-20 19:07:38 +03:00
|
|
|
else if is_ustr {
|
|
|
|
p.gen('= ustring_add($v.name, ')
|
|
|
|
}
|
2019-07-03 14:20:43 +03:00
|
|
|
else {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(' += ')
|
|
|
|
}
|
2019-10-24 16:02:24 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen(' ' + p.tok.str() + ' ')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-03 14:20:43 +03:00
|
|
|
p.fspace()
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fgen(tok.str())
|
2019-07-03 14:20:43 +03:00
|
|
|
p.fspace()
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
pos := p.cgen.cur_line.len
|
|
|
|
expr_type := p.bool_expression()
|
2019-09-09 03:29:24 +03:00
|
|
|
//if p.expected_type.starts_with('array_') {
|
|
|
|
//p.warn('expecting array got $expr_type')
|
2019-10-12 22:01:50 +03:00
|
|
|
//}
|
2019-06-22 21:20:28 +03:00
|
|
|
// Allow `num = 4` where `num` is an `?int`
|
2019-09-17 22:41:58 +03:00
|
|
|
if p.assigned_type.starts_with('Option_') &&
|
2019-10-27 10:03:15 +03:00
|
|
|
expr_type == p.assigned_type['Option_'.len..] {
|
|
|
|
expr := p.cgen.cur_line[pos..]
|
|
|
|
left := p.cgen.cur_line[..pos]
|
2019-08-17 22:19:37 +03:00
|
|
|
typ := expr_type.replace('Option_', '')
|
2019-08-12 18:54:28 +03:00
|
|
|
p.cgen.resetln(left + 'opt_ok($expr, sizeof($typ))')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-06 04:27:29 +03:00
|
|
|
else if expr_type[0]==`[` {
|
2019-10-06 03:07:02 +03:00
|
|
|
// assignment to a fixed_array `mut a:=[3]int a=[1,2,3]!!`
|
2019-10-27 10:03:15 +03:00
|
|
|
expr := p.cgen.cur_line[pos..].all_after('{').all_before('}')
|
|
|
|
left := p.cgen.cur_line[..pos].all_before('=')
|
|
|
|
cline_pos := p.cgen.cur_line[pos..]
|
2019-10-06 03:07:02 +03:00
|
|
|
etype := cline_pos.all_before(' {')
|
|
|
|
if p.assigned_type != p.expected_type {
|
|
|
|
p.error_with_token_index( 'incompatible types: $p.assigned_type != $p.expected_type', errtok)
|
|
|
|
}
|
2019-10-21 14:14:28 +03:00
|
|
|
p.cgen.resetln('memcpy( (& $left), ($etype{$expr}), sizeof( $left ) );')
|
2019-10-06 03:07:02 +03:00
|
|
|
}
|
2019-08-05 10:49:52 +03:00
|
|
|
else if !p.builtin_mod && !p.check_types_no_throw(expr_type, p.assigned_type) {
|
2019-09-29 20:37:39 +03:00
|
|
|
p.error_with_token_index( 'cannot use type `$expr_type` as type `$p.assigned_type` in assignment', errtok)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-20 19:07:38 +03:00
|
|
|
if (is_str || is_ustr) && tok == .plus_assign && !p.is_js {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(')')
|
|
|
|
}
|
|
|
|
// p.assigned_var = ''
|
|
|
|
p.assigned_type = ''
|
|
|
|
if !v.is_used {
|
2019-09-09 16:22:39 +03:00
|
|
|
p.mark_var_used(v)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) var_decl() {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.is_alloc = false
|
2019-07-07 23:30:15 +03:00
|
|
|
is_mut := p.tok == .key_mut || p.prev_tok == .key_for
|
|
|
|
is_static := p.tok == .key_static
|
|
|
|
if p.tok == .key_mut {
|
|
|
|
p.check(.key_mut)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fspace()
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .key_static {
|
|
|
|
p.check(.key_static)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fspace()
|
|
|
|
}
|
2019-10-07 13:19:44 +03:00
|
|
|
|
|
|
|
mut var_token_idxs := [p.cur_tok_index()]
|
|
|
|
mut var_mut := [is_mut] // add first var mut
|
|
|
|
mut var_names := [p.check_name()] // add first variable
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-10-07 13:19:44 +03:00
|
|
|
p.scanner.validate_var_name(var_names[0])
|
2019-10-08 19:26:07 +03:00
|
|
|
mut new_vars := 0
|
|
|
|
if var_names[0] != '_' && !p.known_var(var_names[0]) {
|
|
|
|
new_vars++
|
|
|
|
}
|
2019-10-05 19:17:09 +03:00
|
|
|
// more than 1 vars (multiple returns)
|
2019-09-23 05:45:19 +03:00
|
|
|
for p.tok == .comma {
|
|
|
|
p.check(.comma)
|
2019-10-07 13:19:44 +03:00
|
|
|
if p.tok == .key_mut {
|
|
|
|
p.check(.key_mut)
|
|
|
|
var_mut << true
|
|
|
|
} else {
|
|
|
|
var_mut << false
|
|
|
|
}
|
|
|
|
var_token_idxs << p.cur_tok_index()
|
|
|
|
var_name := p.check_name()
|
|
|
|
p.scanner.validate_var_name(var_name)
|
2019-10-08 19:26:07 +03:00
|
|
|
if var_name != '_' && !p.known_var(var_name) {
|
|
|
|
new_vars++
|
|
|
|
}
|
2019-10-07 13:19:44 +03:00
|
|
|
var_names << var_name
|
2019-10-05 19:17:09 +03:00
|
|
|
}
|
|
|
|
is_assign := p.tok == .assign
|
|
|
|
is_decl_assign := p.tok == .decl_assign
|
|
|
|
if is_assign {
|
|
|
|
p.check_space(.assign) // =
|
|
|
|
} else if is_decl_assign {
|
|
|
|
p.check_space(.decl_assign) // :=
|
|
|
|
} else {
|
|
|
|
p.error('expected `=` or `:=`')
|
2019-07-15 23:44:26 +03:00
|
|
|
}
|
2019-10-08 19:26:07 +03:00
|
|
|
// all vars on left of `:=` already defined (or `_`)
|
2019-10-09 20:54:52 +03:00
|
|
|
if is_decl_assign && var_names.len == 1 && var_names[0] == '_' {
|
|
|
|
p.error_with_token_index('use `=` instead of `:=`', var_token_idxs.last())
|
2019-10-08 19:26:07 +03:00
|
|
|
}
|
2019-10-07 13:19:44 +03:00
|
|
|
p.var_decl_name = if var_names.len > 1 { '_V_mret_'+var_names.join('_') } else { var_names[0] }
|
2019-10-05 19:17:09 +03:00
|
|
|
t := p.gen_var_decl(p.var_decl_name, is_static)
|
2019-10-07 13:19:44 +03:00
|
|
|
mut var_types := [t]
|
2019-10-05 19:17:09 +03:00
|
|
|
// multiple returns types
|
2019-10-07 13:19:44 +03:00
|
|
|
if var_names.len > 1 {
|
|
|
|
var_types = t.replace('_V_MulRet_', '').replace('_PTR_', '*').split('_V_')
|
|
|
|
}
|
|
|
|
// mismatched number of return & assignment vars
|
|
|
|
if var_names.len != var_types.len {
|
|
|
|
mr_fn := p.cgen.cur_line.find_between('=', '(').trim_space()
|
|
|
|
p.error_with_token_index('assignment mismatch: ${var_names.len} variables but `$mr_fn` returns $var_types.len values', var_token_idxs.last())
|
2019-09-23 05:45:19 +03:00
|
|
|
}
|
2019-10-07 13:19:44 +03:00
|
|
|
for i, var_name in var_names {
|
|
|
|
var_token_idx := var_token_idxs[i]
|
|
|
|
var_is_mut := var_mut[i]
|
|
|
|
var_type := var_types[i]
|
|
|
|
known_var := p.known_var(var_name)
|
2019-10-08 19:26:07 +03:00
|
|
|
if var_name == '_' {
|
|
|
|
if var_is_mut {
|
|
|
|
p.error_with_token_index('`mut` has no effect here', var_token_idx-1)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-10-07 13:19:44 +03:00
|
|
|
// println('var decl tok=${p.strtok()} name=type=$var_name type=$var_type ismut=$var_is_mut')
|
|
|
|
// var decl, already exists (shadowing is not allowed)
|
2019-09-23 05:45:19 +03:00
|
|
|
// Don't allow declaring a variable with the same name. Even in a child scope
|
2019-10-07 13:19:44 +03:00
|
|
|
// if var_names.len == 1 && !p.builtin_mod && known_var {
|
|
|
|
if is_decl_assign && known_var {
|
|
|
|
p.error_with_token_index('redefinition of `$var_name`', var_token_idx)
|
|
|
|
}
|
|
|
|
// mut specified with assignment
|
2019-10-24 14:25:03 +03:00
|
|
|
//if /*is_assign && implicit*/ known_var && var_is_mut {
|
|
|
|
if known_var && var_is_mut {
|
2019-10-07 13:19:44 +03:00
|
|
|
p.error_with_token_index('cannot specify mutability for existing var `$var_name`, only for new vars', var_token_idx)
|
2019-09-23 05:45:19 +03:00
|
|
|
}
|
2019-10-05 19:17:09 +03:00
|
|
|
// assignment, but var does not exist
|
2019-10-07 13:19:44 +03:00
|
|
|
if is_assign && !known_var {
|
|
|
|
suggested := p.find_misspelled_local_var(var_name, 50)
|
2019-10-05 19:17:09 +03:00
|
|
|
if suggested != '' {
|
2019-10-07 13:19:44 +03:00
|
|
|
p.error_with_token_index('undefined: `$var_name`. did you mean:$suggested', var_token_idx)
|
2019-10-05 19:17:09 +03:00
|
|
|
}
|
2019-10-07 13:19:44 +03:00
|
|
|
p.error_with_token_index('undefined: `$var_name`.', var_token_idx)
|
2019-10-05 19:17:09 +03:00
|
|
|
}
|
2019-10-07 13:19:44 +03:00
|
|
|
if var_name.len > 1 && contains_capital(var_name) {
|
2019-09-29 20:37:39 +03:00
|
|
|
p.error_with_token_index('variable names cannot contain uppercase letters, use snake_case instead', var_token_idx)
|
2019-09-23 05:45:19 +03:00
|
|
|
}
|
2019-10-05 19:17:09 +03:00
|
|
|
// multiple return
|
2019-10-07 13:19:44 +03:00
|
|
|
if var_names.len > 1 {
|
2019-09-25 14:53:44 +03:00
|
|
|
p.gen(';\n')
|
2019-10-05 19:17:09 +03:00
|
|
|
// assigment
|
2019-10-07 13:19:44 +03:00
|
|
|
// if !p.builtin_mod && known_var {
|
|
|
|
if known_var {
|
|
|
|
v := p.find_var(var_name) or {
|
|
|
|
p.error_with_token_index('cannot find `$var_name`', var_token_idx)
|
2019-10-05 19:17:09 +03:00
|
|
|
break
|
|
|
|
}
|
2019-10-07 13:19:44 +03:00
|
|
|
p.check_types_with_token_index(var_type, v.typ, var_token_idx)
|
2019-10-05 19:17:09 +03:00
|
|
|
if !v.is_mut {
|
|
|
|
p.error_with_token_index('`$v.name` is immutable', var_token_idx)
|
|
|
|
}
|
2019-10-07 13:19:44 +03:00
|
|
|
p.mark_var_used(v)
|
2019-10-05 19:17:09 +03:00
|
|
|
p.mark_var_changed(v)
|
2019-10-07 13:19:44 +03:00
|
|
|
p.gen('$var_name = ${p.var_decl_name}.var_$i')
|
2019-10-05 19:17:09 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// decleration
|
2019-10-07 13:19:44 +03:00
|
|
|
p.gen('$var_type $var_name = ${p.var_decl_name}.var_$i')
|
2019-09-25 14:53:44 +03:00
|
|
|
}
|
2019-09-23 05:45:19 +03:00
|
|
|
p.register_var(Var {
|
2019-10-07 13:19:44 +03:00
|
|
|
name: var_name
|
|
|
|
typ: var_type
|
|
|
|
is_mut: var_is_mut
|
|
|
|
is_alloc: p.is_alloc || var_type.starts_with('array_')
|
2019-09-29 20:37:39 +03:00
|
|
|
line_nr: p.tokens[ var_token_idx ].line_nr
|
|
|
|
token_idx: var_token_idx
|
2019-09-23 05:45:19 +03:00
|
|
|
})
|
2019-09-24 00:40:34 +03:00
|
|
|
//if p.fileis('str.v') {
|
|
|
|
//if p.is_alloc { println('REG VAR IS ALLOC $name') }
|
|
|
|
//}
|
2019-09-23 05:45:19 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
p.var_decl_name = ''
|
2019-08-20 14:34:29 +03:00
|
|
|
p.is_empty_c_struct_init = false
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-04 11:18:31 +03:00
|
|
|
const (
|
|
|
|
and_or_error = 'use `()` to make the boolean expression clear\n' +
|
|
|
|
'for example: `(a && b) || c` instead of `a && b || c`'
|
2019-08-17 22:19:37 +03:00
|
|
|
)
|
2019-08-04 11:18:31 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (p mut Parser) bool_expression() string {
|
|
|
|
tok := p.tok
|
|
|
|
typ := p.bterm()
|
2019-08-17 22:19:37 +03:00
|
|
|
mut got_and := false // to catch `a && b || c` in one expression without ()
|
|
|
|
mut got_or := false
|
2019-07-14 12:01:32 +03:00
|
|
|
for p.tok == .and || p.tok == .logical_or {
|
2019-08-04 11:18:31 +03:00
|
|
|
if p.tok == .and {
|
2019-08-17 22:19:37 +03:00
|
|
|
got_and = true
|
|
|
|
if got_or { p.error(and_or_error) }
|
|
|
|
}
|
2019-08-04 11:18:31 +03:00
|
|
|
if p.tok == .logical_or {
|
2019-08-17 22:19:37 +03:00
|
|
|
got_or = true
|
|
|
|
if got_and { p.error(and_or_error) }
|
|
|
|
}
|
2019-08-09 19:10:59 +03:00
|
|
|
if p.is_sql {
|
|
|
|
if p.tok == .and {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.gen(' and ')
|
|
|
|
}
|
2019-08-09 19:10:59 +03:00
|
|
|
else if p.tok == .logical_or {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.gen(' or ')
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-09 19:10:59 +03:00
|
|
|
p.gen(' ${p.tok.str()} ')
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
|
|
|
p.check_space(p.tok)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.check_types(p.bterm(), typ)
|
|
|
|
}
|
|
|
|
if typ == '' {
|
|
|
|
println('curline:')
|
|
|
|
println(p.cgen.cur_line)
|
|
|
|
println(tok.str())
|
|
|
|
p.error('expr() returns empty type')
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) bterm() string {
|
|
|
|
ph := p.cgen.add_placeholder()
|
2019-06-28 15:19:46 +03:00
|
|
|
mut typ := p.expression()
|
2019-08-17 22:19:37 +03:00
|
|
|
p.expected_type = typ
|
|
|
|
is_str := typ=='string' && !p.is_sql
|
2019-09-20 19:07:38 +03:00
|
|
|
is_ustr := typ=='ustring'
|
2019-10-15 04:46:40 +03:00
|
|
|
is_float := typ[0] == `f` && (typ in ['f64', 'f32']) &&
|
|
|
|
!(p.cur_fn.name in ['f64_abs', 'f32_abs']) &&
|
|
|
|
!(p.cur_fn.name == 'eq')
|
2019-10-28 10:26:42 +03:00
|
|
|
is_array := typ.contains('array_')
|
2019-09-29 18:27:53 +03:00
|
|
|
expr_type := typ
|
2019-06-22 21:20:28 +03:00
|
|
|
tok := p.tok
|
2019-10-15 04:46:40 +03:00
|
|
|
if tok in [.eq, .gt, .lt, .le, .ge, .ne] {
|
2019-10-28 10:26:42 +03:00
|
|
|
//TODO: remove when array comparing is supported
|
|
|
|
if is_array {
|
|
|
|
p.error('Array comparing is not supported yet')
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgen(' ${p.tok.str()} ')
|
2019-10-01 22:38:36 +03:00
|
|
|
if (is_float || is_str || is_ustr) && !p.is_js {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(',')
|
|
|
|
}
|
2019-08-09 19:10:59 +03:00
|
|
|
else if p.is_sql && tok == .eq {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.gen('=')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
else {
|
|
|
|
p.gen(tok.str())
|
|
|
|
}
|
|
|
|
p.next()
|
2019-08-17 22:19:37 +03:00
|
|
|
// `id == user.id` => `id == $1`, `user.id`
|
2019-08-13 14:50:19 +03:00
|
|
|
if p.is_sql {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.sql_i++
|
|
|
|
p.gen('$' + p.sql_i.str())
|
|
|
|
p.cgen.start_cut()
|
2019-08-13 14:50:19 +03:00
|
|
|
p.check_types(p.expression(), typ)
|
2019-08-20 18:05:56 +03:00
|
|
|
sql_param := p.cgen.cut()
|
|
|
|
p.sql_params << sql_param
|
|
|
|
p.sql_types << typ
|
|
|
|
//println('*** sql type: $typ | param: $sql_param')
|
2019-08-17 22:19:37 +03:00
|
|
|
} else {
|
2019-08-13 14:50:19 +03:00
|
|
|
p.check_types(p.expression(), typ)
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = 'bool'
|
2019-09-14 23:48:30 +03:00
|
|
|
if is_str && !p.is_js { //&& !p.is_sql {
|
2019-06-22 23:12:38 +03:00
|
|
|
p.gen(')')
|
2019-10-24 16:02:24 +03:00
|
|
|
match tok {
|
|
|
|
.eq { p.cgen.set_placeholder(ph, 'string_eq(') }
|
|
|
|
.ne { p.cgen.set_placeholder(ph, 'string_ne(') }
|
|
|
|
.le { p.cgen.set_placeholder(ph, 'string_le(') }
|
|
|
|
.ge { p.cgen.set_placeholder(ph, 'string_ge(') }
|
|
|
|
.gt { p.cgen.set_placeholder(ph, 'string_gt(') }
|
|
|
|
.lt { p.cgen.set_placeholder(ph, 'string_lt(') }
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-20 19:07:38 +03:00
|
|
|
if is_ustr {
|
|
|
|
p.gen(')')
|
2019-10-24 16:02:24 +03:00
|
|
|
match tok {
|
|
|
|
.eq { p.cgen.set_placeholder(ph, 'ustring_eq(') }
|
|
|
|
.ne { p.cgen.set_placeholder(ph, 'ustring_ne(') }
|
|
|
|
.le { p.cgen.set_placeholder(ph, 'ustring_le(') }
|
|
|
|
.ge { p.cgen.set_placeholder(ph, 'ustring_ge(') }
|
|
|
|
.gt { p.cgen.set_placeholder(ph, 'ustring_gt(') }
|
|
|
|
.lt { p.cgen.set_placeholder(ph, 'ustring_lt(') }
|
2019-09-20 19:07:38 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-15 04:46:40 +03:00
|
|
|
if is_float && p.cur_fn.name != 'f32_abs' && p.cur_fn.name != 'f64_abs' {
|
2019-09-29 18:27:53 +03:00
|
|
|
p.gen(')')
|
2019-10-24 16:02:24 +03:00
|
|
|
match tok {
|
|
|
|
.eq { p.cgen.set_placeholder(ph, '${expr_type}_eq(') }
|
|
|
|
.ne { p.cgen.set_placeholder(ph, '${expr_type}_ne(') }
|
|
|
|
.le { p.cgen.set_placeholder(ph, '${expr_type}_le(') }
|
|
|
|
.ge { p.cgen.set_placeholder(ph, '${expr_type}_ge(') }
|
|
|
|
.gt { p.cgen.set_placeholder(ph, '${expr_type}_gt(') }
|
|
|
|
.lt { p.cgen.set_placeholder(ph, '${expr_type}_lt(') }
|
2019-10-01 22:38:36 +03:00
|
|
|
}
|
2019-09-29 18:27:53 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2019-08-17 22:19:37 +03:00
|
|
|
// also called on *, &, @, . (enum)
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (p mut Parser) name_expr() string {
|
2019-10-11 06:36:46 +03:00
|
|
|
//println('n')
|
2019-08-30 20:19:06 +03:00
|
|
|
p.has_immutable_field = false
|
2019-09-23 02:14:10 +03:00
|
|
|
p.is_const_literal = false
|
2019-07-06 03:22:29 +03:00
|
|
|
ph := p.cgen.add_placeholder()
|
2019-06-22 21:20:28 +03:00
|
|
|
// amp
|
2019-07-07 23:30:15 +03:00
|
|
|
ptr := p.tok == .amp
|
|
|
|
deref := p.tok == .mul
|
2019-06-22 21:20:28 +03:00
|
|
|
if ptr || deref {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
mut name := p.lit
|
2019-10-06 03:56:08 +03:00
|
|
|
// Raw string (`s := r'hello \n ')
|
|
|
|
if name == 'r' && p.peek() == .str {
|
|
|
|
p.string_expr()
|
|
|
|
return 'string'
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgen(name)
|
|
|
|
// known_type := p.table.known_type(name)
|
|
|
|
orig_name := name
|
2019-08-17 22:19:37 +03:00
|
|
|
is_c := name == 'C' && p.peek() == .dot
|
2019-10-20 10:19:37 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
if is_c {
|
2019-10-20 10:19:37 +03:00
|
|
|
p.check(.name)
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.dot)
|
2019-06-22 21:20:28 +03:00
|
|
|
name = p.lit
|
2019-10-20 10:19:37 +03:00
|
|
|
// C struct initialization
|
|
|
|
if p.peek() == .lcbr && p.table.known_type(name) {
|
|
|
|
return p.get_struct_type(name, true, ptr)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
// C function
|
|
|
|
if p.peek() == .lpar {
|
|
|
|
return p.get_c_func_type(name)
|
|
|
|
}
|
|
|
|
// C const (`C.GLFW_KEY_LEFT`)
|
|
|
|
p.gen(name)
|
|
|
|
p.next()
|
|
|
|
return 'int'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
|
2019-08-17 22:19:37 +03:00
|
|
|
// enum value? (`color == .green`)
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .dot {
|
2019-10-20 10:19:37 +03:00
|
|
|
if p.table.known_type(p.expected_type) {
|
|
|
|
p.check_enum_member_access()
|
|
|
|
// println("found enum value: $p.expected_type")
|
|
|
|
return p.expected_type
|
|
|
|
} else {
|
|
|
|
p.error("unknown enum: `$p.expected_type`")
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
|
2019-10-01 06:33:03 +03:00
|
|
|
// Variable, checked before modules, so module shadowing is allowed.
|
|
|
|
// (`gg = gg.newcontext(); gg.draw_rect(...)`)
|
2019-10-20 10:19:37 +03:00
|
|
|
if p.known_var_check_new_var(name) {
|
|
|
|
rtyp := p.get_var_type(name, ptr, deref)
|
|
|
|
return rtyp
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
|
2019-10-01 06:33:03 +03:00
|
|
|
// Module?
|
2019-09-27 04:00:48 +03:00
|
|
|
if p.peek() == .dot && ((name == p.mod && p.table.known_mod(name)) ||
|
2019-10-01 06:33:03 +03:00
|
|
|
p.import_table.known_alias(name)) && !is_c {
|
2019-08-05 10:49:52 +03:00
|
|
|
mut mod := name
|
2019-07-12 08:37:54 +03:00
|
|
|
// must be aliased module
|
|
|
|
if name != p.mod && p.import_table.known_alias(name) {
|
2019-09-23 13:42:20 +03:00
|
|
|
p.import_table.register_used_import(name)
|
2019-10-13 03:05:11 +03:00
|
|
|
mod = p.import_table.resolve_alias(name)
|
2019-07-12 08:37:54 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.dot)
|
2019-06-22 21:20:28 +03:00
|
|
|
name = p.lit
|
|
|
|
p.fgen(name)
|
2019-10-13 03:05:11 +03:00
|
|
|
name = prepend_mod(mod_gen_name(mod), name)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
|
2019-10-01 06:33:03 +03:00
|
|
|
// Unknown name, try prepending the module name to it
|
|
|
|
// TODO perf
|
|
|
|
else if !p.table.known_type(name) &&
|
2019-09-27 04:00:48 +03:00
|
|
|
!p.table.known_fn(name) && !p.table.known_const(name) && !is_c
|
|
|
|
{
|
2019-08-05 10:49:52 +03:00
|
|
|
name = p.prepend_mod(name)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-10-20 10:19:37 +03:00
|
|
|
// re-check
|
|
|
|
if p.known_var_check_new_var(name) {
|
|
|
|
return p.get_var_type(name, ptr, deref)
|
2019-10-03 02:41:48 +03:00
|
|
|
}
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-07-29 19:21:36 +03:00
|
|
|
// if known_type || is_c_struct_init || (p.first_pass() && p.peek() == .lcbr) {
|
2019-06-22 21:20:28 +03:00
|
|
|
// known type? int(4.5) or Color.green (enum)
|
|
|
|
if p.table.known_type(name) {
|
2019-10-20 10:19:37 +03:00
|
|
|
// cast expression: float(5), byte(0), (*int)(ptr) etc
|
2019-07-07 23:30:15 +03:00
|
|
|
if !is_c && ( p.peek() == .lpar || (deref && p.peek() == .rpar) ) {
|
2019-06-22 21:20:28 +03:00
|
|
|
if deref {
|
|
|
|
name += '*'
|
|
|
|
}
|
|
|
|
else if ptr {
|
|
|
|
name += '*'
|
|
|
|
}
|
2019-06-27 13:27:46 +03:00
|
|
|
p.gen('(')
|
2019-09-14 23:48:30 +03:00
|
|
|
mut typ := name
|
2019-10-25 21:32:27 +03:00
|
|
|
if typ in p.cur_fn.dispatch_of.inst.keys() {
|
|
|
|
typ = p.cur_fn.dispatch_of.inst[typ]
|
|
|
|
}
|
|
|
|
p.cast(typ)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(')')
|
2019-07-07 23:30:15 +03:00
|
|
|
for p.tok == .dot {
|
2019-07-06 03:22:29 +03:00
|
|
|
typ = p.dot(typ, ph)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
// Color.green
|
2019-07-07 23:30:15 +03:00
|
|
|
else if p.peek() == .dot {
|
2019-06-22 21:20:28 +03:00
|
|
|
enum_type := p.table.find_type(name)
|
2019-08-20 14:34:29 +03:00
|
|
|
if enum_type.cat != .enum_ {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.error('`$name` is not an enum')
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.dot)
|
2019-06-22 21:20:28 +03:00
|
|
|
val := p.lit
|
2019-10-20 19:59:53 +03:00
|
|
|
if !enum_type.has_enum_val(val) {
|
|
|
|
p.error('enum `$enum_type.name` does not have value `$val`')
|
|
|
|
}
|
2019-10-30 15:04:25 +03:00
|
|
|
if p.expected_type == enum_type.name {
|
2019-10-30 16:21:57 +03:00
|
|
|
// `if color == .red` is enough
|
|
|
|
// no need in `if color == Color.red`
|
|
|
|
p.error('`${enum_type.name}.$val` is unnecessary, use `.$val`')
|
2019-10-30 15:04:25 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// println('enum val $val')
|
2019-10-13 03:05:11 +03:00
|
|
|
p.gen(mod_gen_name(enum_type.mod) + '__' + enum_type.name + '_' + val)// `color = main__Color_green`
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
return enum_type.name
|
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
// normal struct init (non-C)
|
2019-07-07 23:30:15 +03:00
|
|
|
else if p.peek() == .lcbr {
|
2019-10-20 10:19:37 +03:00
|
|
|
return p.get_struct_type(name, false, ptr)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// Constant
|
2019-10-20 10:19:37 +03:00
|
|
|
if p.table.known_const(name) {
|
|
|
|
return p.get_const_type(name, ptr)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
|
2019-10-24 12:36:57 +03:00
|
|
|
// Function (not method, methods are handled in `.dot()`)
|
2019-10-15 18:08:46 +03:00
|
|
|
mut f := p.table.find_fn_is_script(name, p.v_script) or {
|
2019-10-20 10:19:37 +03:00
|
|
|
return p.get_undefined_fn_type(name, orig_name)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// no () after func, so func is an argument, just gen its name
|
|
|
|
// TODO verify this and handle errors
|
2019-08-17 22:19:37 +03:00
|
|
|
peek := p.peek()
|
2019-07-29 19:21:36 +03:00
|
|
|
if peek != .lpar && peek != .lt {
|
2019-09-05 12:09:41 +03:00
|
|
|
// Register anon fn type
|
|
|
|
fn_typ := Type {
|
|
|
|
name: f.typ_str()// 'fn (int, int) string'
|
|
|
|
mod: p.mod
|
|
|
|
func: f
|
|
|
|
}
|
|
|
|
p.table.register_type2(fn_typ)
|
2019-09-14 23:48:30 +03:00
|
|
|
p.gen(p.table.fn_gen_name(f))
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
2019-09-05 12:09:41 +03:00
|
|
|
return f.typ_str() //'void*'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// TODO bring back
|
|
|
|
if f.typ == 'void' && !p.inside_if_expr {
|
|
|
|
// p.error('`$f.name` used as value')
|
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
|
|
|
|
// println('call to fn $f.name of type $f.typ')
|
|
|
|
// TODO replace the following dirty hacks (needs ptr access to fn table)
|
|
|
|
new_f := f
|
|
|
|
p.fn_call(mut new_f, 0, '', '')
|
|
|
|
if f.is_generic {
|
|
|
|
f2 := p.table.find_fn(f.name) or {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
// println('after call of generic instance $new_f.name(${new_f.str_args(p.table)}) $new_f.typ')
|
|
|
|
// println(' from $f2.name(${f2.str_args(p.table)}) $f2.typ : $f2.type_inst')
|
|
|
|
}
|
|
|
|
f = new_f
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// dot after a function call: `get_user().age`
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .dot {
|
2019-06-22 21:20:28 +03:00
|
|
|
mut typ := ''
|
2019-07-07 23:30:15 +03:00
|
|
|
for p.tok == .dot {
|
2019-06-22 21:20:28 +03:00
|
|
|
// println('dot #$dc')
|
2019-07-06 03:22:29 +03:00
|
|
|
typ = p.dot(f.typ, ph)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
2019-09-23 23:18:55 +03:00
|
|
|
//p.log('end of name_expr')
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-09-09 16:22:39 +03:00
|
|
|
if f.typ.ends_with('*') {
|
|
|
|
p.is_alloc = true
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return f.typ
|
|
|
|
}
|
|
|
|
|
2019-10-20 10:19:37 +03:00
|
|
|
fn (p mut Parser) get_struct_type(name_ string, is_c bool, is_ptr bool) string {
|
|
|
|
mut name := name_
|
|
|
|
if is_ptr {
|
|
|
|
name += '*' // `&User{}` => type `User*`
|
|
|
|
}
|
|
|
|
if name in reserved_type_param_names {
|
|
|
|
p.warn('name `$name` is reserved for type parameters')
|
|
|
|
}
|
|
|
|
p.is_c_struct_init = is_c
|
|
|
|
return p.struct_init(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) get_var_type(name string, is_ptr bool, is_deref bool) string {
|
|
|
|
v := p.find_var_check_new_var(name) or { return "" }
|
|
|
|
if name == '_' {
|
|
|
|
p.error('cannot use `_` as value')
|
|
|
|
}
|
|
|
|
if is_ptr {
|
|
|
|
p.gen('&')
|
|
|
|
}
|
|
|
|
else if is_deref {
|
|
|
|
p.gen('*')
|
|
|
|
}
|
|
|
|
if p.pref.autofree && v.typ == 'string' && v.is_arg &&
|
|
|
|
p.assigned_type == 'string' {
|
|
|
|
p.warn('setting moved ' + v.typ)
|
|
|
|
p.mark_arg_moved(v)
|
|
|
|
}
|
|
|
|
mut typ := p.var_expr(v)
|
|
|
|
// *var
|
|
|
|
if is_deref {
|
|
|
|
if !typ.contains('*') && !typ.ends_with('ptr') {
|
|
|
|
println('name="$name", t=$v.typ')
|
|
|
|
p.error('dereferencing requires a pointer, but got `$typ`')
|
|
|
|
}
|
|
|
|
typ = typ.replace('ptr', '')// TODO
|
|
|
|
typ = typ.replace('*', '')// TODO
|
|
|
|
}
|
|
|
|
// &var
|
|
|
|
else if is_ptr {
|
|
|
|
typ += '*'
|
|
|
|
}
|
|
|
|
if p.inside_return_expr {
|
|
|
|
//println('marking $v.name returned')
|
|
|
|
p.mark_var_returned(v)
|
|
|
|
// v.is_returned = true // TODO modifying a local variable
|
|
|
|
// that's not used afterwards, this should be a compilation
|
|
|
|
// error
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) get_const_type(name string, is_ptr bool) string {
|
|
|
|
c := p.table.find_const(name) or { return "" }
|
|
|
|
if is_ptr && !c.is_global {
|
|
|
|
p.error('cannot take the address of constant `$c.name`')
|
|
|
|
} else if is_ptr && c.is_global {
|
|
|
|
// c.ptr = true
|
|
|
|
p.gen('& /*const*/ ')
|
|
|
|
}
|
2019-10-24 12:36:57 +03:00
|
|
|
if !c.is_public && c.mod != p.mod {
|
|
|
|
p.warn('constant `$c.name` is private')
|
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
mut typ := p.var_expr(c)
|
|
|
|
if is_ptr {
|
|
|
|
typ += '*'
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) get_c_func_type(name string) string {
|
2019-10-24 14:51:22 +03:00
|
|
|
//p.warn(name + ' ' + p.expected_type)
|
|
|
|
//}
|
2019-10-20 10:19:37 +03:00
|
|
|
f := Fn {
|
|
|
|
name: name
|
|
|
|
is_c: true
|
|
|
|
}
|
|
|
|
p.is_c_fn_call = true
|
|
|
|
p.fn_call(mut f, 0, '', '')
|
|
|
|
p.is_c_fn_call = false
|
|
|
|
// Try looking it up. Maybe its defined with "C.fn_name() fn_type",
|
|
|
|
// then we know what type it returns
|
|
|
|
cfn := p.table.find_fn(name) or {
|
|
|
|
// Not Found? Return 'void*'
|
|
|
|
//return 'cvoid' //'void*'
|
2019-10-24 15:41:29 +03:00
|
|
|
//if p.expected_type != '' && p.expected_type != 'void' {
|
|
|
|
//p.warn('\n e=$p.expected_type define imported C function with ' +
|
|
|
|
//'`fn C.$name([args]) [return_type]`\n')
|
|
|
|
//}
|
2019-10-20 10:19:37 +03:00
|
|
|
return 'void*'
|
|
|
|
}
|
|
|
|
// println("C fn $name has type $cfn.typ")
|
|
|
|
return cfn.typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) get_undefined_fn_type(name string, orig_name string) string {
|
|
|
|
if p.first_pass() {
|
|
|
|
p.next()
|
|
|
|
// First pass, the function can be defined later.
|
|
|
|
return 'void'
|
|
|
|
} else {
|
|
|
|
// We are in the second pass, that means this function was not defined, throw an error.
|
|
|
|
|
|
|
|
// V script? Try os module.
|
|
|
|
// TODO
|
|
|
|
if p.v_script {
|
|
|
|
//name = name.replace('main__', 'os__')
|
|
|
|
//f = p.table.find_fn(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for misspelled function / variable / module
|
2019-10-25 10:26:14 +03:00
|
|
|
name_dotted := mod_gen_name_rev(name.replace('__', '.'))
|
|
|
|
suggested := p.identify_typo(name)
|
|
|
|
if suggested.len != 0 {
|
|
|
|
p.error('undefined: `$name_dotted`. did you mean:\n$suggested\n')
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If orig_name is a mod, then printing undefined: `mod` tells us nothing
|
|
|
|
// if p.table.known_mod(orig_name) {
|
|
|
|
if p.table.known_mod(orig_name) || p.import_table.known_alias(orig_name) {
|
|
|
|
m_name := mod_gen_name_rev(name.replace('__', '.'))
|
|
|
|
p.error('undefined function: `$m_name` (in module `$orig_name`)')
|
|
|
|
} else if orig_name in reserved_type_param_names {
|
|
|
|
p.error('the letter `$orig_name` is reserved for type parameters')
|
|
|
|
} else {
|
2019-10-20 20:47:24 +03:00
|
|
|
p.error('undefined: `$orig_name`')
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
|
|
|
return 'void'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (p mut Parser) var_expr(v Var) string {
|
2019-09-23 23:18:55 +03:00
|
|
|
//p.log('\nvar_expr() v.name="$v.name" v.typ="$v.typ"')
|
2019-06-22 21:20:28 +03:00
|
|
|
// println('var expr is_tmp=$p.cgen.is_tmp\n')
|
2019-09-23 22:47:09 +03:00
|
|
|
if !v.is_const {
|
|
|
|
p.mark_var_used(v)
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
fn_ph := p.cgen.add_placeholder()
|
|
|
|
p.expr_var = v
|
|
|
|
p.gen(p.table.var_cgen_name(v.name))
|
|
|
|
p.next()
|
|
|
|
mut typ := v.typ
|
2019-08-17 22:19:37 +03:00
|
|
|
// Function pointer?
|
2019-09-01 01:40:53 +03:00
|
|
|
if typ.starts_with('fn ') && p.tok == .lpar {
|
2019-06-22 21:20:28 +03:00
|
|
|
T := p.table.find_type(typ)
|
|
|
|
p.gen('(')
|
2019-07-29 19:21:36 +03:00
|
|
|
p.fn_call_args(mut T.func)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(')')
|
|
|
|
typ = T.func.typ
|
|
|
|
}
|
|
|
|
// users[0].name
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .lsbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = p.index_expr(typ, fn_ph)
|
|
|
|
}
|
|
|
|
// a.b.c().d chain
|
|
|
|
// mut dc := 0
|
2019-07-07 23:30:15 +03:00
|
|
|
for p.tok ==.dot {
|
2019-08-09 19:10:59 +03:00
|
|
|
if p.peek() == .key_select {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.next()
|
|
|
|
return p.select_query(fn_ph)
|
|
|
|
}
|
2019-08-12 12:06:17 +03:00
|
|
|
if typ == 'pg__DB' && !p.fileis('pg.v') && p.peek() == .name {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.next()
|
|
|
|
p.insert_query(fn_ph)
|
|
|
|
return 'void'
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// println('dot #$dc')
|
|
|
|
typ = p.dot(typ, fn_ph)
|
2019-09-23 23:18:55 +03:00
|
|
|
//p.log('typ after dot=$typ')
|
2019-06-22 21:20:28 +03:00
|
|
|
// print('tok after dot()')
|
|
|
|
// p.print_tok()
|
|
|
|
// dc++
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .lsbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
// typ = p.index_expr(typ, fn_ph, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// a++ and a--
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .inc || p.tok == .dec {
|
2019-07-05 03:44:22 +03:00
|
|
|
if !v.is_mut && !v.is_arg && !p.pref.translated {
|
2019-07-10 15:18:21 +03:00
|
|
|
p.error('`$v.name` is immutable')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-25 14:16:17 +03:00
|
|
|
if !v.is_changed {
|
2019-09-09 16:22:39 +03:00
|
|
|
p.mark_var_changed(v)
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
if typ != 'int' {
|
2019-06-30 23:03:17 +03:00
|
|
|
if !p.pref.translated && !is_number_type(typ) {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.error('cannot ++/-- value of type `$typ`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.gen(p.tok.str())
|
|
|
|
p.fgen(p.tok.str())
|
2019-08-17 22:19:37 +03:00
|
|
|
p.next()// ++/--
|
|
|
|
// allow `a := c++` in translated code
|
2019-06-30 23:03:17 +03:00
|
|
|
if p.pref.translated {
|
2019-07-02 01:00:27 +03:00
|
|
|
//return p.index_expr(typ, fn_ph)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 'void'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
typ = p.index_expr(typ, fn_ph)
|
2019-08-17 22:19:37 +03:00
|
|
|
// TODO hack to allow `foo.bar[0] = 2`
|
|
|
|
if p.tok == .dot {
|
2019-07-07 23:30:15 +03:00
|
|
|
for p.tok == .dot {
|
2019-07-02 01:00:27 +03:00
|
|
|
typ = p.dot(typ, fn_ph)
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-07-02 01:00:27 +03:00
|
|
|
typ = p.index_expr(typ, fn_ph)
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
// user.name => `str_typ` is `User`
|
|
|
|
// user.company.name => `str_typ` is `Company`
|
2019-09-30 13:46:50 +03:00
|
|
|
fn (p mut Parser) dot(str_typ_ string, method_ph int) string {
|
2019-08-17 22:19:37 +03:00
|
|
|
//if p.fileis('orm_test') {
|
|
|
|
//println('ORM dot $str_typ')
|
|
|
|
//}
|
2019-09-30 13:46:50 +03:00
|
|
|
mut str_typ := str_typ_
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.dot)
|
2019-10-01 06:33:03 +03:00
|
|
|
is_variadic_arg := str_typ.starts_with('...')
|
2019-10-27 10:03:15 +03:00
|
|
|
if is_variadic_arg { str_typ = str_typ[3..] }
|
2019-09-30 17:11:12 +03:00
|
|
|
mut typ := p.find_type(str_typ)
|
2019-07-29 19:21:36 +03:00
|
|
|
if typ.name.len == 0 {
|
|
|
|
p.error('dot(): cannot find type `$str_typ`')
|
|
|
|
}
|
|
|
|
if p.tok == .dollar {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.comptime_method_call(typ)
|
|
|
|
return 'void'
|
|
|
|
}
|
2019-07-25 14:16:17 +03:00
|
|
|
field_name := p.lit
|
2019-10-17 10:44:20 +03:00
|
|
|
if field_name == 'filter' && str_typ.starts_with('array_') {
|
2019-10-18 07:58:54 +03:00
|
|
|
p.gen_array_filter(str_typ, method_ph)
|
2019-10-17 10:44:20 +03:00
|
|
|
return str_typ
|
|
|
|
}
|
2019-10-26 14:25:03 +03:00
|
|
|
else if field_name == 'map' && str_typ.starts_with('array_') {
|
2019-10-26 14:49:36 +03:00
|
|
|
return p.gen_array_map(str_typ, method_ph)
|
2019-10-26 14:25:03 +03:00
|
|
|
}
|
2019-10-17 10:44:20 +03:00
|
|
|
|
2019-10-06 03:07:02 +03:00
|
|
|
fname_tidx := p.cur_tok_index()
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fgen(field_name)
|
2019-09-18 15:37:34 +03:00
|
|
|
//p.log('dot() field_name=$field_name typ=$str_typ')
|
2019-07-02 01:00:27 +03:00
|
|
|
//if p.fileis('main.v') {
|
2019-08-17 22:19:37 +03:00
|
|
|
//println('dot() field_name=$field_name typ=$str_typ prev_tok=${prev_tok.str()}')
|
2019-07-02 01:00:27 +03:00
|
|
|
//}
|
2019-08-29 22:19:37 +03:00
|
|
|
has_field := p.table.type_has_field(typ, p.table.var_cgen_name(field_name))
|
2019-08-11 00:02:48 +03:00
|
|
|
mut has_method := p.table.type_has_method(typ, field_name)
|
2019-09-30 17:11:12 +03:00
|
|
|
if is_variadic_arg && field_name == 'len' {
|
2019-09-30 13:46:50 +03:00
|
|
|
p.gen('->$field_name')
|
|
|
|
p.next()
|
|
|
|
return 'int'
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
// generate `.str()`
|
|
|
|
if !has_method && field_name == 'str' && typ.name.starts_with('array_') {
|
2019-08-31 16:38:13 +03:00
|
|
|
p.gen_array_str(typ)
|
2019-08-17 22:19:37 +03:00
|
|
|
has_method = true
|
|
|
|
}
|
2019-08-20 14:34:29 +03:00
|
|
|
if !typ.is_c && !p.is_c_fn_call && !has_field && !has_method && !p.first_pass() {
|
2019-06-22 21:20:28 +03:00
|
|
|
if typ.name.starts_with('Option_') {
|
2019-10-27 10:03:15 +03:00
|
|
|
opt_type := typ.name[7..]
|
2019-09-18 15:37:34 +03:00
|
|
|
p.error('unhandled option type: `?$opt_type`')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-05 11:42:58 +03:00
|
|
|
//println('error in dot():')
|
|
|
|
//println('fields:')
|
|
|
|
//for field in typ.fields {
|
|
|
|
//println(field.name)
|
|
|
|
//}
|
|
|
|
//println('methods:')
|
|
|
|
//for field in typ.methods {
|
|
|
|
//println(field.name)
|
|
|
|
//}
|
|
|
|
//println('str_typ=="$str_typ"')
|
2019-10-06 03:07:02 +03:00
|
|
|
p.error_with_token_index('type `$typ.name` has no field or method `$field_name`', fname_tidx)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
mut dot := '.'
|
2019-08-20 14:34:29 +03:00
|
|
|
if str_typ.ends_with('*') || str_typ == 'FT_Face' { // TODO fix C ptr typedefs
|
2019-09-14 23:48:30 +03:00
|
|
|
dot = dot_ptr
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// field
|
|
|
|
if has_field {
|
2019-08-29 22:19:37 +03:00
|
|
|
struct_field := if typ.name != 'Option' { p.table.var_cgen_name(field_name) } else { field_name }
|
2019-09-28 20:42:29 +03:00
|
|
|
field := p.table.find_field(typ, struct_field) or {
|
2019-10-06 03:07:02 +03:00
|
|
|
p.error_with_token_index('missing field: $struct_field in type $typ.name', fname_tidx)
|
2019-09-28 20:42:29 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2019-08-30 20:19:06 +03:00
|
|
|
if !field.is_mut && !p.has_immutable_field {
|
|
|
|
p.has_immutable_field = true
|
|
|
|
p.first_immutable_field = field
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// Is the next token `=`, `+=` etc? (Are we modifying the field?)
|
|
|
|
next := p.peek()
|
2019-08-30 20:19:06 +03:00
|
|
|
modifying := next.is_assign() || next == .inc || next == .dec ||
|
|
|
|
(field.typ.starts_with('array_') && next == .left_shift)
|
2019-10-13 01:50:15 +03:00
|
|
|
if !p.builtin_mod && !p.pref.translated && modifying &&
|
|
|
|
p.has_immutable_field {
|
2019-08-30 20:19:06 +03:00
|
|
|
f := p.first_immutable_field
|
2019-10-06 03:07:02 +03:00
|
|
|
p.error_with_token_index('cannot modify immutable field `$f.name` (type `$f.parent_fn`)\n' +
|
2019-08-30 20:19:06 +03:00
|
|
|
'declare the field with `mut:`
|
|
|
|
struct $f.parent_fn {
|
2019-08-08 07:55:08 +03:00
|
|
|
mut:
|
2019-08-30 20:19:06 +03:00
|
|
|
$f.name $f.typ
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-10-06 03:07:02 +03:00
|
|
|
', fname_tidx)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// Don't allow `arr.data`
|
2019-10-25 16:34:12 +03:00
|
|
|
if field.access_mod == .private && !p.builtin_mod && !p.pref.translated && p.mod != typ.mod && !p.is_vgen {
|
2019-06-22 21:20:28 +03:00
|
|
|
// println('$typ.name :: $field.name ')
|
|
|
|
// println(field.access_mod)
|
2019-10-14 10:15:21 +03:00
|
|
|
p.error_with_token_index('cannot refer to unexported field `$struct_field` (type `$typ.name`)\n' +
|
|
|
|
'declare the field with `pub:`
|
|
|
|
struct $typ.name {
|
|
|
|
pub:
|
|
|
|
$struct_field $field.typ
|
|
|
|
}
|
|
|
|
', fname_tidx)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-29 22:19:37 +03:00
|
|
|
p.gen(dot + struct_field)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
return field.typ
|
|
|
|
}
|
|
|
|
// method
|
2019-09-18 15:37:34 +03:00
|
|
|
method := p.table.find_method(typ, field_name) or {
|
2019-10-06 03:07:02 +03:00
|
|
|
p.error_with_token_index('could not find method `$field_name`', fname_tidx) // should never happen
|
2019-09-18 15:37:34 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2019-10-20 10:19:37 +03:00
|
|
|
p.fn_call(mut method, method_ph, '', str_typ)
|
2019-10-27 02:36:43 +03:00
|
|
|
// Methods returning `array` should return `array_string` etc
|
2019-06-22 21:20:28 +03:00
|
|
|
if method.typ == 'array' && typ.name.starts_with('array_') {
|
|
|
|
return typ.name
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
// Array methods returning `voidptr` (like `last()`) should return element type
|
2019-06-22 21:20:28 +03:00
|
|
|
if method.typ == 'void*' && typ.name.starts_with('array_') {
|
2019-10-27 10:03:15 +03:00
|
|
|
return typ.name[6..]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
//if false && p.tok == .lsbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
// if is_indexer {
|
2019-07-02 01:00:27 +03:00
|
|
|
//return p.index_expr(method.typ, method_ph)
|
|
|
|
//}
|
2019-09-09 16:22:39 +03:00
|
|
|
if method.typ.ends_with('*') {
|
|
|
|
p.is_alloc = true
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return method.typ
|
|
|
|
}
|
|
|
|
|
2019-09-14 23:48:30 +03:00
|
|
|
enum IndexType {
|
2019-09-17 22:41:58 +03:00
|
|
|
noindex
|
2019-09-14 23:48:30 +03:00
|
|
|
str
|
|
|
|
map
|
|
|
|
array
|
|
|
|
array0
|
|
|
|
fixed_array
|
|
|
|
ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_index_type(typ string) IndexType {
|
2019-10-06 03:56:08 +03:00
|
|
|
if typ.starts_with('map_') { return .map }
|
|
|
|
if typ == 'string' { return .str }
|
|
|
|
if typ.starts_with('array_') || typ == 'array' { return .array }
|
2019-09-14 23:48:30 +03:00
|
|
|
if typ == 'byte*' || typ == 'byteptr' || typ.contains('*') {
|
2019-10-06 03:56:08 +03:00
|
|
|
return .ptr
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
2019-10-06 03:56:08 +03:00
|
|
|
if typ[0] == `[` { return .fixed_array }
|
|
|
|
return .noindex
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 09:19:27 +03:00
|
|
|
fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
|
2019-08-17 22:19:37 +03:00
|
|
|
mut typ := typ_
|
2019-06-22 21:20:28 +03:00
|
|
|
// a[0]
|
|
|
|
v := p.expr_var
|
2019-07-24 14:04:57 +03:00
|
|
|
//if p.fileis('fn_test.v') {
|
|
|
|
//println('index expr typ=$typ')
|
2019-08-17 22:19:37 +03:00
|
|
|
//println(v.name)
|
2019-07-24 14:04:57 +03:00
|
|
|
//}
|
2019-09-30 13:46:50 +03:00
|
|
|
is_variadic_arg := typ.starts_with('...')
|
2019-06-22 21:20:28 +03:00
|
|
|
is_map := typ.starts_with('map_')
|
|
|
|
is_str := typ == 'string'
|
|
|
|
is_arr0 := typ.starts_with('array_')
|
|
|
|
is_arr := is_arr0 || typ == 'array'
|
|
|
|
is_ptr := typ == 'byte*' || typ == 'byteptr' || typ.contains('*')
|
2019-10-26 21:58:26 +03:00
|
|
|
mut is_slice := false
|
2019-07-07 23:30:15 +03:00
|
|
|
is_indexer := p.tok == .lsbr
|
2019-06-22 21:20:28 +03:00
|
|
|
mut close_bracket := false
|
2019-10-01 14:48:19 +03:00
|
|
|
index_error_tok_pos := p.token_idx
|
2019-06-22 21:20:28 +03:00
|
|
|
if is_indexer {
|
|
|
|
is_fixed_arr := typ[0] == `[`
|
2019-09-30 13:46:50 +03:00
|
|
|
if !is_str && !is_arr && !is_map && !is_ptr && !is_fixed_arr && !is_variadic_arg {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.error('Cant [] non-array/string/map. Got type "$typ"')
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lsbr)
|
2019-06-22 21:20:28 +03:00
|
|
|
// Get element type (set `typ` to it)
|
|
|
|
if is_str {
|
|
|
|
typ = 'byte'
|
|
|
|
p.fgen('[')
|
2019-08-05 10:49:52 +03:00
|
|
|
// Direct faster access to .str[i] in builtin modules
|
|
|
|
if p.builtin_mod {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('.str[')
|
|
|
|
close_bracket = true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Bounds check everywhere else
|
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_fixed_arr {
|
|
|
|
// `[10]int` => `int`, `[10][3]int` => `[3]int`
|
|
|
|
if typ.contains('][') {
|
|
|
|
pos := typ.index_after('[', 1)
|
2019-10-27 10:03:15 +03:00
|
|
|
typ = typ[pos..]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
typ = typ.all_after(']')
|
|
|
|
}
|
|
|
|
p.gen('[')
|
|
|
|
close_bracket = true
|
|
|
|
}
|
2019-09-30 13:46:50 +03:00
|
|
|
else if is_ptr && !is_variadic_arg {
|
2019-06-22 21:20:28 +03:00
|
|
|
// typ = 'byte'
|
|
|
|
typ = typ.replace('*', '')
|
|
|
|
// modify(mut []string) fix
|
|
|
|
if !is_arr {
|
|
|
|
p.gen('[/*ptr*/')
|
|
|
|
close_bracket = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_arr {
|
|
|
|
if is_arr0 {
|
2019-10-27 10:03:15 +03:00
|
|
|
typ = typ[6..]
|
2019-09-27 14:02:01 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
p.gen_array_at(typ, is_arr0, fn_ph)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// map is tricky
|
|
|
|
// need to replace "m[key] = val" with "tmp = val; map_set(&m, key, &tmp)"
|
|
|
|
// need to replace "m[key]" with "tmp = val; map_get(&m, key, &tmp)"
|
|
|
|
// can only do that later once we know whether there's an "=" or not
|
|
|
|
if is_map {
|
|
|
|
typ = typ.replace('map_', '')
|
|
|
|
if typ == 'map' {
|
|
|
|
typ = 'void*'
|
|
|
|
}
|
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
// expression inside [ ]
|
2019-10-15 11:02:30 +03:00
|
|
|
if is_arr || is_str {
|
2019-10-27 09:36:04 +03:00
|
|
|
// [2..
|
|
|
|
if p.tok != .dotdot {
|
|
|
|
index_pos := p.cgen.cur_line.len
|
|
|
|
T := p.table.find_type(p.expression())
|
|
|
|
// Allows only i8-64 and byte-64 to be used when accessing an array
|
|
|
|
if T.parent != 'int' && T.parent != 'u32' {
|
|
|
|
p.check_types(T.name, 'int')
|
|
|
|
}
|
2019-10-27 10:03:15 +03:00
|
|
|
if p.cgen.cur_line[index_pos..].replace(' ', '').int() < 0 {
|
2019-10-27 09:36:04 +03:00
|
|
|
p.error('cannot access negative array index')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-27 09:36:04 +03:00
|
|
|
// [..
|
|
|
|
else {
|
|
|
|
p.gen('0')
|
2019-08-18 19:26:02 +03:00
|
|
|
}
|
2019-10-26 21:58:26 +03:00
|
|
|
if p.tok == .dotdot {
|
|
|
|
if is_arr {
|
|
|
|
typ = 'array_' + typ
|
|
|
|
} else if is_str {
|
|
|
|
typ = 'string'
|
|
|
|
} else {
|
|
|
|
p.error('slicing is supported by arrays and strings only')
|
|
|
|
}
|
|
|
|
is_slice = true
|
|
|
|
p.next()
|
|
|
|
p.gen(',')
|
2019-10-27 09:36:04 +03:00
|
|
|
// ..4]
|
|
|
|
if p.tok != .rsbr {
|
|
|
|
p.check_types(p.expression(), 'int')
|
|
|
|
p.gen(', false')
|
|
|
|
}
|
|
|
|
// ..]
|
|
|
|
else {
|
|
|
|
p.gen('-1, true')
|
|
|
|
}
|
2019-10-26 21:58:26 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-08-13 20:13:39 +03:00
|
|
|
T := p.table.find_type(p.expression())
|
|
|
|
// TODO: Get the key type of the map instead of only string.
|
|
|
|
if is_map && T.parent != 'string' {
|
|
|
|
p.check_types(T.name, 'string')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rsbr)
|
2019-08-05 10:49:52 +03:00
|
|
|
// if (is_str && p.builtin_mod) || is_ptr || is_fixed_arr && ! (is_ptr && is_arr) {
|
2019-06-22 21:20:28 +03:00
|
|
|
if close_bracket {
|
|
|
|
p.gen(']/*r$typ $v.is_mut*/')
|
|
|
|
}
|
2019-08-30 12:16:39 +03:00
|
|
|
p.expr_var = v
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-30 13:46:50 +03:00
|
|
|
// accessing variadiac args
|
2019-09-30 17:11:12 +03:00
|
|
|
if is_variadic_arg {
|
|
|
|
// TODO: why was this here?
|
|
|
|
// if p.calling_c {
|
|
|
|
// p.error('you cannot currently pass varg to a C function.')
|
|
|
|
// }
|
|
|
|
if is_indexer {
|
|
|
|
l := p.cgen.cur_line.trim_space()
|
|
|
|
index_val := l.right(l.last_index(' ')).trim_space()
|
2019-10-27 10:03:15 +03:00
|
|
|
p.cgen.resetln(l[..fn_ph])
|
2019-09-30 17:11:12 +03:00
|
|
|
p.table.varg_access << VargAccess{
|
|
|
|
fn_name: p.cur_fn.name,
|
2019-10-01 14:48:19 +03:00
|
|
|
tok_idx: index_error_tok_pos,
|
2019-09-30 17:11:12 +03:00
|
|
|
index: index_val.int()
|
|
|
|
}
|
|
|
|
p.cgen.set_placeholder(fn_ph, '${v.name}->args[$index_val]')
|
|
|
|
return typ
|
|
|
|
}
|
2019-09-30 13:46:50 +03:00
|
|
|
}
|
2019-10-26 21:58:26 +03:00
|
|
|
// `m[key] = val`
|
2019-08-17 22:19:37 +03:00
|
|
|
// TODO move this from index_expr()
|
2019-09-24 22:30:30 +03:00
|
|
|
if (p.tok == .assign && !p.is_sql) || p.tok.is_assign() {
|
2019-08-05 10:49:52 +03:00
|
|
|
if is_indexer && is_str && !p.builtin_mod {
|
2019-07-10 15:18:21 +03:00
|
|
|
p.error('strings are immutable')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
p.assigned_type = typ
|
2019-07-07 23:30:15 +03:00
|
|
|
p.expected_type = typ
|
2019-09-14 23:48:30 +03:00
|
|
|
assign_pos := p.cgen.cur_line.len
|
2019-09-27 18:55:30 +03:00
|
|
|
is_cao := p.tok != .assign
|
2019-06-22 21:20:28 +03:00
|
|
|
p.assign_statement(v, fn_ph, is_indexer && (is_map || is_arr))
|
2019-09-14 23:48:30 +03:00
|
|
|
// `m[key] = val`
|
2019-06-22 21:20:28 +03:00
|
|
|
if is_indexer && (is_map || is_arr) {
|
2019-09-14 23:48:30 +03:00
|
|
|
p.gen_array_set(typ, is_ptr, is_map, fn_ph, assign_pos, is_cao)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
2019-06-30 23:03:17 +03:00
|
|
|
// else if p.pref.is_verbose && p.assigned_var != '' {
|
2019-06-22 21:20:28 +03:00
|
|
|
// p.error('didnt assign')
|
|
|
|
// }
|
2019-10-26 21:58:26 +03:00
|
|
|
// `m[key]`. no =, just a getter
|
2019-08-05 10:49:52 +03:00
|
|
|
else if (is_map || is_arr || (is_str && !p.builtin_mod)) && is_indexer {
|
2019-10-26 21:58:26 +03:00
|
|
|
p.index_get(typ, fn_ph, IndexConfig{
|
2019-09-14 23:48:30 +03:00
|
|
|
is_arr: is_arr
|
|
|
|
is_map: is_map
|
|
|
|
is_ptr: is_ptr
|
|
|
|
is_str: is_str
|
2019-10-26 21:58:26 +03:00
|
|
|
is_slice: is_slice
|
2019-09-14 23:48:30 +03:00
|
|
|
})
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// else if is_arr && is_indexer{}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2019-10-26 21:58:26 +03:00
|
|
|
struct IndexConfig {
|
2019-09-14 23:48:30 +03:00
|
|
|
is_map bool
|
|
|
|
is_str bool
|
|
|
|
is_ptr bool
|
|
|
|
is_arr bool
|
|
|
|
is_arr0 bool
|
2019-10-26 21:58:26 +03:00
|
|
|
is_slice bool
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
2019-10-20 10:19:37 +03:00
|
|
|
// for debugging only
|
|
|
|
fn (p &Parser) fileis(s string) bool {
|
|
|
|
return p.scanner.file_path.contains(s)
|
|
|
|
}
|
|
|
|
|
2019-09-21 18:21:45 +03:00
|
|
|
// in and dot have higher priority than `!`
|
|
|
|
fn (p mut Parser) indot_expr() string {
|
|
|
|
ph := p.cgen.add_placeholder()
|
|
|
|
mut typ := p.term()
|
|
|
|
if p.tok == .dot {
|
|
|
|
for p.tok == .dot {
|
|
|
|
typ = p.dot(typ, ph)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// `a in [1, 2, 3]`
|
|
|
|
// `key in map`
|
|
|
|
if p.tok == .key_in {
|
|
|
|
p.fgen(' ')
|
|
|
|
p.check(.key_in)
|
2019-10-24 14:56:02 +03:00
|
|
|
p.expected_type = typ // this allows `foo in [.val1, .val2, .val3]`
|
2019-10-06 04:27:29 +03:00
|
|
|
if p.tok == .lsbr {
|
|
|
|
// a in [1,2,3] optimization => `a == 1 || a == 2 || a == 3`
|
|
|
|
// avoids an allocation
|
|
|
|
p.in_optimization(typ, ph)
|
|
|
|
return 'bool'
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-09-21 18:21:45 +03:00
|
|
|
p.fgen(' ')
|
|
|
|
p.gen('), ')
|
|
|
|
arr_typ := p.expression()
|
|
|
|
is_map := arr_typ.starts_with('map_')
|
|
|
|
if !arr_typ.starts_with('array_') && !is_map {
|
|
|
|
p.error('`in` requires an array/map')
|
|
|
|
}
|
|
|
|
T := p.table.find_type(arr_typ)
|
|
|
|
if !is_map && !T.has_method('contains') {
|
|
|
|
p.error('$arr_typ has no method `contains`')
|
|
|
|
}
|
|
|
|
// `typ` is element's type
|
|
|
|
if is_map {
|
|
|
|
p.cgen.set_placeholder(ph, '_IN_MAP( (')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.cgen.set_placeholder(ph, '_IN($typ, (')
|
|
|
|
}
|
|
|
|
p.gen(')')
|
|
|
|
return 'bool'
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// returns resulting type
|
|
|
|
fn (p mut Parser) expression() string {
|
2019-09-23 02:14:10 +03:00
|
|
|
p.is_const_literal = true
|
|
|
|
//if p.scanner.file_path.contains('test_test') {
|
|
|
|
//println('expression() pass=$p.pass tok=')
|
|
|
|
//p.print_tok()
|
|
|
|
//}
|
2019-06-22 21:20:28 +03:00
|
|
|
ph := p.cgen.add_placeholder()
|
2019-09-21 18:21:45 +03:00
|
|
|
mut typ := p.indot_expr()
|
2019-08-17 22:19:37 +03:00
|
|
|
is_str := typ=='string'
|
2019-09-20 19:07:38 +03:00
|
|
|
is_ustr := typ=='ustring'
|
2019-09-14 23:48:30 +03:00
|
|
|
// `a << b` ==> `array_push(&a, b)`
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .left_shift {
|
2019-06-22 21:20:28 +03:00
|
|
|
if typ.contains('array_') {
|
|
|
|
// Can't pass integer literal, because push requires a void*
|
|
|
|
// a << 7 => int tmp = 7; array_push(&a, &tmp);
|
|
|
|
// _PUSH(&a, expression(), tmp, string)
|
|
|
|
tmp := p.get_tmp()
|
2019-10-27 10:03:15 +03:00
|
|
|
tmp_typ := typ[6..]// skip "array_"
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check_space(.left_shift)
|
2019-06-22 21:20:28 +03:00
|
|
|
// Get the value we are pushing
|
|
|
|
p.gen(', (')
|
2019-08-20 14:34:29 +03:00
|
|
|
// Immutable? Can we push?
|
2019-06-30 23:03:17 +03:00
|
|
|
if !p.expr_var.is_mut && !p.pref.translated {
|
2019-07-10 15:18:21 +03:00
|
|
|
p.error('`$p.expr_var.name` is immutable (can\'t <<)')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-29 17:02:28 +03:00
|
|
|
if p.expr_var.is_arg && p.expr_var.typ.starts_with('array_') {
|
|
|
|
p.error("for now it's not possible to append an element to "+
|
|
|
|
'a mutable array argument `$p.expr_var.name`')
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-07-25 14:16:17 +03:00
|
|
|
if !p.expr_var.is_changed {
|
2019-09-09 16:22:39 +03:00
|
|
|
p.mark_var_changed(p.expr_var)
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-09-24 22:30:30 +03:00
|
|
|
p.gen('/*typ = $typ tmp_typ=$tmp_typ*/')
|
|
|
|
ph_clone := p.cgen.add_placeholder()
|
2019-08-17 22:19:37 +03:00
|
|
|
expr_type := p.expression()
|
2019-09-24 22:30:30 +03:00
|
|
|
// Need to clone the string when appending it to an array?
|
|
|
|
if p.pref.autofree && typ == 'array_string' && expr_type == 'string' {
|
|
|
|
p.cgen.set_placeholder(ph_clone, 'string_clone(')
|
|
|
|
p.gen(')')
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
p.gen_array_push(ph, typ, expr_type, tmp, tmp_typ)
|
2019-06-22 21:20:28 +03:00
|
|
|
return 'void'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.next()
|
|
|
|
p.gen(' << ')
|
|
|
|
p.check_types(p.expression(), typ)
|
|
|
|
return 'int'
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .righ_shift {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
p.gen(' >> ')
|
|
|
|
p.check_types(p.expression(), typ)
|
|
|
|
return 'int'
|
|
|
|
}
|
2019-09-15 19:07:40 +03:00
|
|
|
// + - | ^
|
2019-10-30 15:04:25 +03:00
|
|
|
for p.tok in [.plus, .minus, .pipe, .amp, .xor] {
|
2019-06-22 21:20:28 +03:00
|
|
|
tok_op := p.tok
|
2019-09-15 19:07:40 +03:00
|
|
|
if typ == 'bool' {
|
|
|
|
p.error('operator ${p.tok.str()} not defined on bool ')
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
is_num := typ == 'void*' || typ == 'byte*' || is_number_type(typ)
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check_space(p.tok)
|
2019-09-14 23:48:30 +03:00
|
|
|
if is_str && tok_op == .plus && !p.is_js {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.cgen.set_placeholder(ph, 'string_add(')
|
|
|
|
p.gen(',')
|
|
|
|
}
|
2019-09-20 19:07:38 +03:00
|
|
|
else if is_ustr && tok_op == .plus {
|
|
|
|
p.cgen.set_placeholder(ph, 'ustring_add(')
|
|
|
|
p.gen(',')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// 3 + 4
|
2019-09-14 23:48:30 +03:00
|
|
|
else if is_num || p.is_js {
|
2019-07-24 18:46:41 +03:00
|
|
|
if typ == 'void*' {
|
2019-07-24 00:23:13 +03:00
|
|
|
// Msvc errors on void* pointer arithmatic
|
|
|
|
// ... So cast to byte* and then do the add
|
|
|
|
p.cgen.set_placeholder(ph, '(byte*)')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(tok_op.str())
|
|
|
|
}
|
|
|
|
// Vec + Vec
|
|
|
|
else {
|
2019-06-30 23:03:17 +03:00
|
|
|
if p.pref.translated {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(tok_op.str() + ' /*doom hack*/')// TODO hack to fix DOOM's angle_t
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.check_types(p.term(), typ)
|
2019-09-20 19:07:38 +03:00
|
|
|
if (is_str || is_ustr) && tok_op == .plus && !p.is_js {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(')')
|
|
|
|
}
|
|
|
|
// Make sure operators are used with correct types
|
2019-09-20 19:07:38 +03:00
|
|
|
if !p.pref.translated && !is_str && !is_ustr && !is_num {
|
2019-06-22 21:20:28 +03:00
|
|
|
T := p.table.find_type(typ)
|
2019-07-07 23:30:15 +03:00
|
|
|
if tok_op == .plus {
|
2019-06-22 21:20:28 +03:00
|
|
|
if T.has_method('+') {
|
|
|
|
p.cgen.set_placeholder(ph, typ + '_plus(')
|
|
|
|
p.gen(')')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('operator + not defined on `$typ`')
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
else if tok_op == .minus {
|
2019-06-22 21:20:28 +03:00
|
|
|
if T.has_method('-') {
|
|
|
|
p.cgen.set_placeholder(ph, '${typ}_minus(')
|
|
|
|
p.gen(')')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('operator - not defined on `$typ`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) term() string {
|
|
|
|
line_nr := p.scanner.line_nr
|
2019-07-24 14:04:57 +03:00
|
|
|
//if p.fileis('fn_test') {
|
|
|
|
//println('\nterm() $line_nr')
|
|
|
|
//}
|
2019-06-22 21:20:28 +03:00
|
|
|
typ := p.unary()
|
2019-07-24 14:04:57 +03:00
|
|
|
//if p.fileis('fn_test') {
|
|
|
|
//println('2: $line_nr')
|
|
|
|
//}
|
2019-06-22 21:20:28 +03:00
|
|
|
// `*` on a newline? Can't be multiplication, only dereference
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .mul && line_nr != p.scanner.line_nr {
|
2019-06-22 21:20:28 +03:00
|
|
|
return typ
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
for p.tok == .mul || p.tok == .div || p.tok == .mod {
|
2019-06-22 21:20:28 +03:00
|
|
|
tok := p.tok
|
2019-07-07 23:30:15 +03:00
|
|
|
is_div := tok == .div
|
|
|
|
is_mod := tok == .mod
|
|
|
|
// is_mul := tok == .mod
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
p.gen(tok.str())// + ' /*op2*/ ')
|
|
|
|
p.fgen(' ' + tok.str() + ' ')
|
2019-08-11 14:47:34 +03:00
|
|
|
if (is_div || is_mod) && p.tok == .number && p.lit == '0' {
|
2019-09-28 19:52:52 +03:00
|
|
|
p.error('division or modulo by zero')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
if is_mod && (is_float_type(typ) || !is_number_type(typ)) {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.error('operator .mod requires integer types')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
p.check_types(p.unary(), typ)
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) unary() string {
|
|
|
|
mut typ := ''
|
|
|
|
tok := p.tok
|
2019-10-24 19:19:03 +03:00
|
|
|
match tok {
|
|
|
|
.not {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('!')
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.not)
|
2019-09-21 18:21:45 +03:00
|
|
|
// typ should be bool type
|
|
|
|
typ = p.indot_expr()
|
|
|
|
if typ != 'bool' {
|
|
|
|
p.error('operator ! requires bool type, not `$typ`')
|
|
|
|
}
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.bit_not {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('~')
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.bit_not)
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = p.bool_expression()
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = p.factor()
|
|
|
|
}
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) factor() string {
|
|
|
|
mut typ := ''
|
|
|
|
tok := p.tok
|
2019-10-24 19:19:03 +03:00
|
|
|
match tok {
|
|
|
|
.key_none {
|
2019-09-17 22:41:58 +03:00
|
|
|
if !p.expected_type.starts_with('Option_') {
|
|
|
|
p.error('need "$p.expected_type" got none')
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-09-17 22:41:58 +03:00
|
|
|
p.gen('opt_none()')
|
|
|
|
p.check(.key_none)
|
|
|
|
return p.expected_type
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.number {
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = 'int'
|
2019-06-26 22:44:08 +03:00
|
|
|
// Check if float (`1.0`, `1e+3`) but not if is hexa
|
2019-08-17 22:19:37 +03:00
|
|
|
if (p.lit.contains('.') || (p.lit.contains('e') || p.lit.contains('E'))) &&
|
2019-07-18 11:55:39 +03:00
|
|
|
!(p.lit[0] == `0` && (p.lit[1] == `x` || p.lit[1] == `X`)) {
|
2019-06-25 22:36:44 +03:00
|
|
|
typ = 'f32'
|
2019-08-17 22:19:37 +03:00
|
|
|
// typ = 'f64' // TODO
|
2019-07-18 11:55:39 +03:00
|
|
|
} else {
|
|
|
|
v_u64 := p.lit.u64()
|
|
|
|
if u64(u32(v_u64)) < v_u64 {
|
|
|
|
typ = 'u64'
|
|
|
|
}
|
2019-06-25 22:36:44 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
if p.expected_type != '' && !is_valid_int_const(p.lit, p.expected_type) {
|
|
|
|
p.error('constant `$p.lit` overflows `$p.expected_type`')
|
|
|
|
}
|
2019-06-25 22:36:44 +03:00
|
|
|
p.gen(p.lit)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgen(p.lit)
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.minus {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('-')
|
|
|
|
p.fgen('-')
|
|
|
|
p.next()
|
|
|
|
return p.factor()
|
|
|
|
// Variable
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.key_sizeof {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('sizeof(')
|
|
|
|
p.fgen('sizeof(')
|
|
|
|
p.next()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lpar)
|
2019-09-21 18:21:45 +03:00
|
|
|
mut sizeof_typ := p.get_type()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rpar)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('$sizeof_typ)')
|
|
|
|
p.fgen('$sizeof_typ)')
|
|
|
|
return 'int'
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.amp, .dot, .mul {
|
2019-08-17 22:19:37 +03:00
|
|
|
// (dot is for enum vals: `.green`)
|
2019-06-22 21:20:28 +03:00
|
|
|
return p.name_expr()
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.name {
|
2019-06-22 21:20:28 +03:00
|
|
|
// map[string]int
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.lit == 'map' && p.peek() == .lsbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
return p.map_init()
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.lit == 'json' && p.peek() == .dot {
|
2019-08-16 18:32:32 +03:00
|
|
|
if !('json' in p.table.imports) {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.error('undefined: `json`, use `import json`')
|
|
|
|
}
|
2019-09-23 13:42:20 +03:00
|
|
|
p.import_table.register_used_import('json')
|
2019-06-22 21:20:28 +03:00
|
|
|
return p.js_decode()
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
//if p.fileis('orm_test') {
|
|
|
|
//println('ORM name: $p.lit')
|
|
|
|
//}
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = p.name_expr()
|
|
|
|
return typ
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
2019-10-27 14:05:50 +03:00
|
|
|
/*
|
2019-10-24 19:19:03 +03:00
|
|
|
.key_default {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
p.next()
|
|
|
|
name := p.check_name()
|
|
|
|
if name != 'T' {
|
|
|
|
p.error('default needs T')
|
|
|
|
}
|
|
|
|
p.gen('default(T)')
|
|
|
|
p.next()
|
|
|
|
return 'T'
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
2019-10-27 14:05:50 +03:00
|
|
|
*/
|
2019-10-24 19:19:03 +03:00
|
|
|
.lpar {
|
2019-07-10 14:27:35 +03:00
|
|
|
//p.gen('(/*lpar*/')
|
|
|
|
p.gen('(')
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.lpar)
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = p.bool_expression()
|
|
|
|
// Hack. If this `)` referes to a ptr cast `(*int__)__`, it was already checked
|
|
|
|
// TODO: fix parser so that it doesn't think it's a par expression when it sees `(` in
|
|
|
|
// __(__*int)(
|
|
|
|
if !p.ptr_cast {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rpar)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
p.ptr_cast = false
|
|
|
|
p.gen(')')
|
|
|
|
return typ
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.chartoken {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.char_expr()
|
|
|
|
typ = 'byte'
|
|
|
|
return typ
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.str {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.string_expr()
|
|
|
|
typ = 'string'
|
|
|
|
return typ
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.key_false {
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = 'bool'
|
|
|
|
p.gen('0')
|
|
|
|
p.fgen('false')
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.key_true {
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = 'bool'
|
|
|
|
p.gen('1')
|
|
|
|
p.fgen('true')
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.lsbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
// `[1,2,3]` or `[]` or `[20]byte`
|
|
|
|
// TODO have to return because arrayInit does next()
|
|
|
|
// everything should do next()
|
|
|
|
return p.array_init()
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.lcbr {
|
2019-08-17 22:19:37 +03:00
|
|
|
// `m := { 'one': 1 }`
|
2019-08-03 10:44:08 +03:00
|
|
|
if p.peek() == .str {
|
2019-08-17 22:19:37 +03:00
|
|
|
return p.map_init()
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// { user | name :'new name' }
|
|
|
|
return p.assoc()
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.key_if {
|
2019-07-12 07:54:01 +03:00
|
|
|
typ = p.if_st(true, 0)
|
2019-06-22 21:20:28 +03:00
|
|
|
return typ
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
.key_match {
|
2019-08-27 00:39:11 +03:00
|
|
|
typ = p.match_statement(true)
|
|
|
|
return typ
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-08-17 22:19:37 +03:00
|
|
|
if p.pref.is_verbose || p.pref.is_debug {
|
2019-08-17 20:21:59 +03:00
|
|
|
next := p.peek()
|
|
|
|
println('prev=${p.prev_tok.str()}')
|
|
|
|
println('next=${next.str()}')
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.error('unexpected token: `${p.tok.str()}`')
|
|
|
|
}
|
2019-10-24 19:19:03 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()// TODO everything should next()
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
// { user | name: 'new name' }
|
|
|
|
fn (p mut Parser) assoc() string {
|
|
|
|
// println('assoc()')
|
|
|
|
p.next()
|
|
|
|
name := p.check_name()
|
2019-09-23 20:34:08 +03:00
|
|
|
var := p.find_var(name) or {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.error('unknown variable `$name`')
|
2019-09-17 22:41:58 +03:00
|
|
|
exit(1)
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-10-25 10:27:43 +03:00
|
|
|
p.mark_var_used(var)
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.pipe)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('($var.typ){')
|
|
|
|
mut fields := []string// track the fields user is setting, the rest will be copied from the old object
|
2019-07-07 23:30:15 +03:00
|
|
|
for p.tok != .rcbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
field := p.check_name()
|
|
|
|
fields << field
|
|
|
|
p.gen('.$field = ')
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.colon)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.bool_expression()
|
|
|
|
p.gen(',')
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok != .rcbr {
|
|
|
|
p.check(.comma)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Copy the rest of the fields
|
|
|
|
T := p.table.find_type(var.typ)
|
|
|
|
for ffield in T.fields {
|
|
|
|
f := ffield.name
|
|
|
|
if f in fields {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.gen('.$f = $name . $f,')
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rcbr)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('}')
|
|
|
|
return var.typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) char_expr() {
|
|
|
|
p.gen('\'$p.lit\'')
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-07 09:19:27 +03:00
|
|
|
fn format_str(_str string) string {
|
2019-10-07 01:09:11 +03:00
|
|
|
// TODO don't call replace 3 times for every string, do this in scanner.v
|
2019-08-07 09:19:27 +03:00
|
|
|
mut str := _str.replace('"', '\\"')
|
2019-10-12 04:48:26 +03:00
|
|
|
str = str.replace('\r\n', '\\n')
|
2019-07-04 22:51:59 +03:00
|
|
|
str = str.replace('\n', '\\n')
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (p mut Parser) string_expr() {
|
2019-10-06 03:56:08 +03:00
|
|
|
is_raw := p.tok == .name && p.lit == 'r'
|
|
|
|
if is_raw {
|
|
|
|
p.next()
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
str := p.lit
|
2019-07-16 18:59:07 +03:00
|
|
|
// No ${}, just return a simple string
|
2019-10-06 03:56:08 +03:00
|
|
|
if p.peek() != .dollar || is_raw {
|
|
|
|
p.fgen("'$str'")
|
2019-10-12 00:04:42 +03:00
|
|
|
f := if is_raw { cescaped_path(str) } else { format_str(str) }
|
2019-08-17 22:19:37 +03:00
|
|
|
// `C.puts('hi')` => `puts("hi");`
|
2019-09-13 02:41:51 +03:00
|
|
|
/*
|
|
|
|
Calling a C function sometimes requires a call to a string method
|
|
|
|
C.fun('ssss'.to_wide()) => fun(string_to_wide(tos2((byte*)('ssss'))))
|
|
|
|
*/
|
|
|
|
if (p.calling_c && p.peek() != .dot) || (p.pref.translated && p.mod == 'main') {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('"$f"')
|
|
|
|
}
|
2019-08-09 19:10:59 +03:00
|
|
|
else if p.is_sql {
|
2019-10-06 03:56:08 +03:00
|
|
|
p.gen("'$str'")
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
else if p.is_js {
|
2019-10-25 11:24:34 +03:00
|
|
|
p.gen('tos("$f")')
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
else {
|
2019-10-07 01:31:01 +03:00
|
|
|
p.gen('tos3("$f")')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
return
|
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
$if js {
|
|
|
|
p.error('js backend does not support string formatting yet')
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
p.is_alloc = true // $ interpolation means there's allocation
|
2019-06-22 21:20:28 +03:00
|
|
|
mut args := '"'
|
|
|
|
mut format := '"'
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fgen('\'')
|
|
|
|
mut complex_inter := false // for vfmt
|
2019-07-16 18:59:07 +03:00
|
|
|
for p.tok == .str {
|
2019-06-22 21:20:28 +03:00
|
|
|
// Add the string between %d's
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fgen(p.lit)
|
2019-07-15 15:05:11 +03:00
|
|
|
p.lit = p.lit.replace('%', '%%')
|
2019-06-22 21:20:28 +03:00
|
|
|
format += format_str(p.lit)
|
|
|
|
p.next()// skip $
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok != .dollar {
|
2019-06-22 21:20:28 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
// Handle .dollar
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.dollar)
|
2019-07-16 18:59:07 +03:00
|
|
|
// If there's no string after current token, it means we are in
|
2019-08-17 22:19:37 +03:00
|
|
|
// a complex expression (`${...}`)
|
2019-07-16 18:59:07 +03:00
|
|
|
if p.peek() != .str {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fgen('{')
|
|
|
|
complex_inter = true
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// Get bool expr inside a temp var
|
|
|
|
p.cgen.start_tmp()
|
|
|
|
typ := p.bool_expression()
|
|
|
|
mut val := p.cgen.end_tmp()
|
|
|
|
val = val.trim_space()
|
|
|
|
args += ', $val'
|
|
|
|
if typ == 'string' {
|
|
|
|
// args += '.str'
|
|
|
|
// printf("%.*s", a.len, a.str) syntax
|
|
|
|
args += '.len, ${val}.str'
|
|
|
|
}
|
|
|
|
if typ == 'ustring' {
|
|
|
|
args += '.len, ${val}.s.str'
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
if typ == 'bool' {
|
2019-08-09 19:10:59 +03:00
|
|
|
//args += '.len, ${val}.str'
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// Custom format? ${t.hour:02d}
|
2019-07-07 23:30:15 +03:00
|
|
|
custom := p.tok == .colon
|
2019-06-22 21:20:28 +03:00
|
|
|
if custom {
|
2019-09-07 13:44:41 +03:00
|
|
|
mut cformat := ''
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .dot {
|
2019-09-07 13:44:41 +03:00
|
|
|
cformat += '.'
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
if p.tok == .minus { // support for left aligned formatting
|
|
|
|
cformat += '-'
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
}
|
2019-09-07 13:44:41 +03:00
|
|
|
cformat += p.lit// 02
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
2019-09-07 13:44:41 +03:00
|
|
|
fspec := p.lit // f
|
|
|
|
cformat += fspec
|
|
|
|
if fspec == 's' {
|
|
|
|
//println('custom str F=$cformat | format_specifier: "$fspec" | typ: $typ ')
|
|
|
|
if typ != 'string' {
|
2019-09-25 17:59:50 +03:00
|
|
|
p.error('only V strings can be formatted with a :${cformat} format, but you have given "${val}", which has type ${typ}')
|
2019-09-07 13:44:41 +03:00
|
|
|
}
|
|
|
|
args = args.all_before_last('${val}.len, ${val}.str') + '${val}.str'
|
|
|
|
}
|
|
|
|
format += '%$cformat'
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
else {
|
2019-08-17 22:19:37 +03:00
|
|
|
f := p.typ_to_fmt(typ, 0)
|
|
|
|
if f == '' {
|
2019-08-22 07:56:29 +03:00
|
|
|
is_array := typ.starts_with('array_')
|
2019-08-31 16:38:13 +03:00
|
|
|
typ2 := p.table.find_type(typ)
|
|
|
|
has_str_method := p.table.type_has_method(typ2, 'str')
|
2019-08-22 07:56:29 +03:00
|
|
|
if is_array || has_str_method {
|
|
|
|
if is_array && !has_str_method {
|
2019-08-31 16:38:13 +03:00
|
|
|
p.gen_array_str(typ2)
|
2019-08-22 07:56:29 +03:00
|
|
|
}
|
2019-10-17 07:02:02 +03:00
|
|
|
tmp_var := p.get_tmp()
|
|
|
|
p.cgen.insert_before('string $tmp_var = ${typ}_str(${val});')
|
|
|
|
args = args.all_before_last(val) + '${tmp_var}.len, ${tmp_var}.str'
|
2019-08-22 07:56:29 +03:00
|
|
|
format += '%.*s '
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('unhandled sprintf format "$typ" ')
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
|
|
|
format += f
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-07 13:44:41 +03:00
|
|
|
//println('interpolation format is: |${format}| args are: |${args}| ')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-16 18:59:07 +03:00
|
|
|
if complex_inter {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fgen('}')
|
|
|
|
}
|
|
|
|
p.fgen('\'')
|
2019-06-22 21:20:28 +03:00
|
|
|
// println("hello %d", num) optimization.
|
|
|
|
if p.cgen.nogen {
|
|
|
|
return
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
// println: don't allocate a new string, just print it.
|
2019-07-31 10:51:24 +03:00
|
|
|
$if !windows {
|
|
|
|
cur_line := p.cgen.cur_line.trim_space()
|
2019-08-29 21:48:16 +03:00
|
|
|
if cur_line == 'println (' && p.tok != .plus {
|
2019-07-31 10:51:24 +03:00
|
|
|
p.cgen.resetln(cur_line.replace('println (', 'printf('))
|
|
|
|
p.gen('$format\\n$args')
|
|
|
|
return
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// '$age'! means the user wants this to be a tmp string (uses global buffer, no allocation,
|
|
|
|
// won't be used again)
|
2019-10-21 14:21:30 +03:00
|
|
|
// TODO remove this hack, do this automatically
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .not {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.not)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('_STR_TMP($format$args)')
|
|
|
|
}
|
|
|
|
else {
|
2019-09-24 00:40:34 +03:00
|
|
|
// Otherwise do len counting + allocation + sprintf
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('_STR($format$args)')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// m := map[string]int{}
|
2019-08-17 22:19:37 +03:00
|
|
|
// m := { 'one': 1 }
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (p mut Parser) map_init() string {
|
2019-08-17 22:19:37 +03:00
|
|
|
// m := { 'one': 1, 'two': 2 }
|
|
|
|
mut keys_gen := '' // (string[]){tos2("one"), tos2("two")}
|
|
|
|
mut vals_gen := '' // (int[]){1, 2}
|
|
|
|
mut val_type := '' // 'int'
|
2019-08-03 10:44:08 +03:00
|
|
|
if p.tok == .lcbr {
|
|
|
|
p.check(.lcbr)
|
2019-08-17 22:19:37 +03:00
|
|
|
mut i := 0
|
|
|
|
for {
|
|
|
|
key := p.lit
|
2019-10-10 22:27:22 +03:00
|
|
|
keys_gen += 'tos3("$key"), '
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.str)
|
|
|
|
p.check(.colon)
|
|
|
|
p.cgen.start_tmp()
|
|
|
|
t := p.bool_expression()
|
|
|
|
if i == 0 {
|
|
|
|
val_type = t
|
|
|
|
}
|
|
|
|
i++
|
2019-08-03 10:44:08 +03:00
|
|
|
if val_type != t {
|
|
|
|
if !p.check_types_no_throw(val_type, t) {
|
|
|
|
p.error('bad map element type `$val_type` instead of `$t`')
|
|
|
|
}
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
val_expr := p.cgen.end_tmp()
|
|
|
|
vals_gen += '$val_expr, '
|
2019-08-03 10:44:08 +03:00
|
|
|
if p.tok == .rcbr {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.rcbr)
|
|
|
|
break
|
|
|
|
}
|
2019-08-03 10:44:08 +03:00
|
|
|
if p.tok == .comma {
|
|
|
|
p.check(.comma)
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-03 11:26:36 +03:00
|
|
|
p.gen('new_map_init($i, sizeof($val_type), ' +
|
2019-09-22 17:24:15 +03:00
|
|
|
'(string[$i]){ $keys_gen }, ($val_type [$i]){ $vals_gen } )')
|
2019-08-17 22:19:37 +03:00
|
|
|
typ := 'map_$val_type'
|
2019-08-26 17:42:18 +03:00
|
|
|
p.register_map(typ)
|
2019-08-17 22:19:37 +03:00
|
|
|
return typ
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lsbr)
|
2019-06-22 21:20:28 +03:00
|
|
|
key_type := p.check_name()
|
|
|
|
if key_type != 'string' {
|
|
|
|
p.error('only string key maps allowed for now')
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rsbr)
|
2019-08-16 18:44:35 +03:00
|
|
|
val_type = p.get_type()/// p.check_name()
|
|
|
|
//if !p.table.known_type(val_type) {
|
|
|
|
//p.error('map init unknown type "$val_type"')
|
|
|
|
//}
|
2019-07-15 13:33:18 +03:00
|
|
|
typ := 'map_$val_type'
|
|
|
|
p.register_map(typ)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('new_map(1, sizeof($val_type))')
|
2019-08-16 18:44:35 +03:00
|
|
|
if p.tok == .lcbr {
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.check(.rcbr)
|
|
|
|
println('warning: $p.file_name:$p.scanner.line_nr ' +
|
2019-08-17 22:19:37 +03:00
|
|
|
'initializaing maps no longer requires `{}`')
|
|
|
|
}
|
2019-07-15 13:33:18 +03:00
|
|
|
return typ
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-17 22:19:37 +03:00
|
|
|
// `nums := [1, 2, 3]`
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (p mut Parser) array_init() string {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.is_alloc = true
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lsbr)
|
2019-08-17 22:19:37 +03:00
|
|
|
mut is_integer := p.tok == .number // for `[10]int`
|
|
|
|
// fixed length arrays with a const len: `nums := [N]int`, same as `[10]int` basically
|
|
|
|
mut is_const_len := false
|
2019-09-19 14:19:44 +03:00
|
|
|
if p.tok == .name && !p.inside_const {
|
|
|
|
const_name := p.prepend_mod(p.lit)
|
|
|
|
if p.table.known_const(const_name) {
|
|
|
|
c := p.table.find_const(const_name) or {
|
|
|
|
//p.error('unknown const `$p.lit`')
|
|
|
|
exit(1)
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-09-19 14:19:44 +03:00
|
|
|
if c.typ == 'int' && p.peek() == .rsbr { //&& !p.inside_const {
|
|
|
|
is_integer = true
|
|
|
|
is_const_len = true
|
|
|
|
} else {
|
|
|
|
p.error('bad fixed size array const `$p.lit`')
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
lit := p.lit
|
|
|
|
mut typ := ''
|
|
|
|
new_arr_ph := p.cgen.add_placeholder()
|
|
|
|
mut i := 0
|
2019-09-14 23:48:30 +03:00
|
|
|
pos := p.cgen.cur_line.len// remember cur line to fetch first number in cgen for [0; 10]
|
2019-07-07 23:30:15 +03:00
|
|
|
for p.tok != .rsbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
val_typ := p.bool_expression()
|
2019-09-14 23:48:30 +03:00
|
|
|
// Get the type of the first expression
|
2019-06-22 21:20:28 +03:00
|
|
|
if i == 0 {
|
|
|
|
typ = val_typ
|
|
|
|
// fixed width array initialization? (`arr := [20]byte`)
|
2019-09-28 13:50:30 +03:00
|
|
|
if is_integer && p.tok == .rsbr && p.peek() == .name &&
|
|
|
|
p.cur_tok().line_nr == p.peek_token().line_nr {
|
|
|
|
// there is no space between `[10]` and `byte`
|
|
|
|
if p.cur_tok().col + p.peek_token().lit.len == p.peek_token().col {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rsbr)
|
2019-08-31 03:28:20 +03:00
|
|
|
array_elem_typ := p.get_type()
|
2019-09-14 23:48:30 +03:00
|
|
|
if !p.table.known_type(array_elem_typ) {
|
2019-08-31 03:28:20 +03:00
|
|
|
p.error('bad type `$array_elem_typ`')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
p.cgen.resetln('')
|
|
|
|
//p.gen('{0}')
|
|
|
|
p.is_alloc = false
|
|
|
|
if is_const_len {
|
2019-10-13 03:05:11 +03:00
|
|
|
return '[${mod_gen_name(p.mod)}__$lit]$array_elem_typ'
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
return '[$lit]$array_elem_typ'
|
2019-09-28 13:50:30 +03:00
|
|
|
} else {
|
|
|
|
p.check(.rsbr)
|
|
|
|
typ = p.get_type()
|
|
|
|
p.error('no space allowed between [$lit] and $typ')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if val_typ != typ {
|
|
|
|
if !p.check_types_no_throw(val_typ, typ) {
|
|
|
|
p.error('bad array element type `$val_typ` instead of `$typ`')
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok != .rsbr && p.tok != .semicolon {
|
2019-07-03 14:20:43 +03:00
|
|
|
p.gen(', ')
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.comma)
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fspace()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
// Repeat (a = [0;5] )
|
2019-07-07 23:30:15 +03:00
|
|
|
if i == 1 && p.tok == .semicolon {
|
2019-09-14 23:48:30 +03:00
|
|
|
p.warn('`[0 ; len]` syntax was removed. Use `[0].repeat(len)` instead')
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check_space(.semicolon)
|
2019-10-27 10:03:15 +03:00
|
|
|
val := p.cgen.cur_line[pos..]
|
|
|
|
p.cgen.resetln(p.cgen.cur_line[..pos])
|
2019-09-14 23:48:30 +03:00
|
|
|
p.gen('array_repeat_old(& ($typ[]){ $val }, ')
|
2019-06-22 21:20:28 +03:00
|
|
|
p.check_types(p.bool_expression(), 'int')
|
|
|
|
p.gen(', sizeof($typ) )')
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rsbr)
|
2019-06-22 21:20:28 +03:00
|
|
|
return 'array_$typ'
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rsbr)
|
2019-06-22 21:20:28 +03:00
|
|
|
// type after `]`? (e.g. "[]string")
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok != .name && i == 0 {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.error('specify array type: `[]typ` instead of `[]`')
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .name && i == 0 {
|
2019-06-22 21:20:28 +03:00
|
|
|
// vals.len == 0 {
|
|
|
|
typ = p.get_type()
|
|
|
|
}
|
|
|
|
// ! after array => no malloc and no copy
|
2019-08-17 22:19:37 +03:00
|
|
|
no_alloc := p.tok == .not
|
2019-06-23 14:17:33 +03:00
|
|
|
if no_alloc {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
|
|
|
}
|
2019-08-31 00:59:21 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// [1,2,3]!! => [3]int{1,2,3}
|
2019-08-17 22:19:37 +03:00
|
|
|
is_fixed_size := p.tok == .not
|
2019-06-22 21:20:28 +03:00
|
|
|
if is_fixed_size {
|
|
|
|
p.next()
|
|
|
|
p.gen(' }')
|
2019-07-29 19:21:36 +03:00
|
|
|
if !p.first_pass() {
|
2019-06-22 21:20:28 +03:00
|
|
|
// If we are defining a const array, we don't need to specify the type:
|
|
|
|
// `a = {1,2,3}`, not `a = (int[]) {1,2,3}`
|
|
|
|
if p.inside_const {
|
2019-08-31 00:59:21 +03:00
|
|
|
p.cgen.set_placeholder(new_arr_ph, '{')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-08-31 00:59:21 +03:00
|
|
|
p.cgen.set_placeholder(new_arr_ph, '($typ[]) {')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return '[$i]$typ'
|
|
|
|
}
|
|
|
|
// if ptr {
|
|
|
|
// typ += '_ptr"
|
|
|
|
// }
|
2019-09-14 23:48:30 +03:00
|
|
|
p.gen_array_init(typ, no_alloc, new_arr_ph, i)
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = 'array_$typ'
|
|
|
|
p.register_array(typ)
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
// `f32(3)`
|
|
|
|
// tok is `f32` or `)` if `(*int)(ptr)`
|
|
|
|
|
|
|
|
fn (p mut Parser) get_tmp() string {
|
|
|
|
p.tmp_cnt++
|
|
|
|
return 'tmp$p.tmp_cnt'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) get_tmp_counter() int {
|
|
|
|
p.tmp_cnt++
|
|
|
|
return p.tmp_cnt
|
|
|
|
}
|
|
|
|
|
2019-10-01 06:33:03 +03:00
|
|
|
// returns expression's type, and entire expression's string representation)
|
|
|
|
fn (p mut Parser) tmp_expr() (string, string) {
|
|
|
|
p.cgen.start_tmp()
|
|
|
|
typ := p.bool_expression()
|
|
|
|
val := p.cgen.end_tmp()
|
|
|
|
return typ, val
|
|
|
|
}
|
|
|
|
|
2019-07-12 07:54:01 +03:00
|
|
|
fn (p mut Parser) if_st(is_expr bool, elif_depth int) string {
|
2019-06-22 21:20:28 +03:00
|
|
|
if is_expr {
|
2019-08-09 19:10:59 +03:00
|
|
|
//if p.fileis('if_expr') {
|
|
|
|
//println('IF EXPR')
|
|
|
|
//}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.inside_if_expr = true
|
|
|
|
p.gen('(')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen('if (')
|
|
|
|
p.fgen('if ')
|
|
|
|
}
|
|
|
|
p.next()
|
2019-10-01 06:33:03 +03:00
|
|
|
// `if a := opt() { }` syntax
|
|
|
|
if p.tok == .name && p.peek() == .decl_assign {
|
|
|
|
option_tmp := p.get_tmp()
|
|
|
|
var_name := p.lit
|
|
|
|
p.next()
|
|
|
|
p.check(.decl_assign)
|
|
|
|
option_type, expr := p.tmp_expr()// := p.bool_expression()
|
2019-10-27 10:03:15 +03:00
|
|
|
typ := option_type[7..]
|
2019-10-01 06:33:03 +03:00
|
|
|
// Option_User tmp = get_user(1);
|
|
|
|
// if (tmp.ok) {
|
|
|
|
// User user = *(User*)tmp.data;
|
|
|
|
// [statements]
|
|
|
|
// }
|
|
|
|
p.cgen.insert_before('$option_type $option_tmp = $expr; ')
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.genln(option_tmp + '.ok) {')
|
|
|
|
p.genln('$typ $var_name = *($typ*) $option_tmp . data;')
|
|
|
|
p.register_var(Var {
|
|
|
|
name: var_name
|
|
|
|
typ: typ
|
|
|
|
is_mut: false // TODO
|
|
|
|
//is_alloc: p.is_alloc || typ.starts_with('array_')
|
|
|
|
//line_nr: p.tokens[ var_token_idx ].line_nr
|
|
|
|
//token_idx: var_token_idx
|
|
|
|
})
|
|
|
|
p.statements()
|
|
|
|
return 'void'
|
|
|
|
} else {
|
|
|
|
p.check_types(p.bool_expression(), 'bool')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
if is_expr {
|
|
|
|
p.gen(') ? (')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln(') {')
|
|
|
|
}
|
|
|
|
p.fgen(' ')
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lcbr)
|
2019-06-22 21:20:28 +03:00
|
|
|
mut typ := ''
|
|
|
|
// if { if hack
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .key_if && p.inside_if_expr {
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = p.factor()
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
typ = p.statements()
|
|
|
|
}
|
2019-08-08 10:49:56 +03:00
|
|
|
if_returns := p.returns
|
|
|
|
p.returns = false
|
2019-06-22 21:20:28 +03:00
|
|
|
// println('IF TYp=$typ')
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .key_else {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fgenln('')
|
|
|
|
p.check(.key_else)
|
|
|
|
p.fspace()
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .key_if {
|
2019-07-12 06:43:21 +03:00
|
|
|
if is_expr {
|
|
|
|
p.gen(') : (')
|
2019-08-09 09:17:31 +03:00
|
|
|
nested := p.if_st(is_expr, elif_depth + 1)
|
|
|
|
nested_returns := p.returns
|
|
|
|
p.returns = if_returns && nested_returns
|
|
|
|
return nested
|
2019-07-12 06:43:21 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen(' else ')
|
2019-08-09 09:17:31 +03:00
|
|
|
nested := p.if_st(is_expr, 0)
|
|
|
|
nested_returns := p.returns
|
|
|
|
p.returns = if_returns && nested_returns
|
|
|
|
return nested
|
2019-07-12 06:43:21 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// return ''
|
|
|
|
}
|
|
|
|
if is_expr {
|
|
|
|
p.gen(') : (')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln(' else { ')
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lcbr)
|
2019-06-22 21:20:28 +03:00
|
|
|
// statements() returns the type of the last statement
|
2019-09-04 21:46:25 +03:00
|
|
|
first_typ := typ
|
2019-06-22 21:20:28 +03:00
|
|
|
typ = p.statements()
|
|
|
|
p.inside_if_expr = false
|
|
|
|
if is_expr {
|
2019-09-04 21:46:25 +03:00
|
|
|
p.check_types(first_typ, typ)
|
2019-07-12 07:54:01 +03:00
|
|
|
p.gen(strings.repeat(`)`, elif_depth + 1))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-09 09:17:31 +03:00
|
|
|
else_returns := p.returns
|
|
|
|
p.returns = if_returns && else_returns
|
2019-06-22 21:20:28 +03:00
|
|
|
return typ
|
|
|
|
}
|
|
|
|
p.inside_if_expr = false
|
|
|
|
if p.fileis('test_test') {
|
|
|
|
println('if ret typ="$typ" line=$p.scanner.line_nr')
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) for_st() {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.key_for)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgen(' ')
|
|
|
|
p.for_expr_cnt++
|
|
|
|
next_tok := p.peek()
|
2019-07-12 08:23:16 +03:00
|
|
|
//debug := p.scanner.file_path.contains('r_draw')
|
2019-09-09 16:22:39 +03:00
|
|
|
p.open_scope()
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .lcbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
// Infinite loop
|
|
|
|
p.gen('while (1) {')
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
else if p.tok == .key_mut {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.error('`mut` is not required in for loops')
|
|
|
|
}
|
|
|
|
// for i := 0; i < 10; i++ {
|
2019-07-07 23:30:15 +03:00
|
|
|
else if next_tok == .decl_assign || next_tok == .assign || p.tok == .semicolon {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.genln('for (')
|
2019-07-07 23:30:15 +03:00
|
|
|
if next_tok == .decl_assign {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.var_decl()
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
else if p.tok != .semicolon {
|
2019-06-22 21:20:28 +03:00
|
|
|
// allow `for ;; i++ {`
|
|
|
|
// Allow `for i = 0; i < ...`
|
|
|
|
p.statement(false)
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.semicolon)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(' ; ')
|
|
|
|
p.fgen(' ')
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok != .semicolon {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.bool_expression()
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.semicolon)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen(' ; ')
|
|
|
|
p.fgen(' ')
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok != .lcbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.statement(false)
|
|
|
|
}
|
|
|
|
p.genln(') { ')
|
|
|
|
}
|
|
|
|
// for i, val in array
|
2019-07-07 23:30:15 +03:00
|
|
|
else if p.peek() == .comma {
|
2019-09-14 23:48:30 +03:00
|
|
|
/*
|
|
|
|
`for i, val in array {`
|
|
|
|
==>
|
|
|
|
```
|
|
|
|
array_int tmp = array;
|
|
|
|
for (int i = 0; i < tmp.len; i++) {
|
|
|
|
int val = tmp[i];
|
|
|
|
```
|
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
i := p.check_name()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.comma)
|
2019-06-22 21:20:28 +03:00
|
|
|
val := p.check_name()
|
2019-09-25 17:59:50 +03:00
|
|
|
if i == '_' && val == '_' {
|
|
|
|
p.error('no new variables on the left side of `in`')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgen(' ')
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.key_in)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fgen(' ')
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
p.cgen.start_tmp()
|
2019-09-30 17:11:12 +03:00
|
|
|
mut typ := p.bool_expression()
|
2019-08-17 22:19:37 +03:00
|
|
|
is_arr := typ.starts_with('array_')
|
|
|
|
is_map := typ.starts_with('map_')
|
|
|
|
is_str := typ == 'string'
|
2019-09-30 17:11:12 +03:00
|
|
|
is_variadic_arg := typ.starts_with('...')
|
2019-10-27 10:03:15 +03:00
|
|
|
if is_variadic_arg { typ = typ[3..] }
|
2019-09-30 17:11:12 +03:00
|
|
|
if !is_arr && !is_str && !is_map && !is_variadic_arg {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.error('cannot range over type `$typ`')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
expr := p.cgen.end_tmp()
|
2019-09-30 17:11:12 +03:00
|
|
|
if !is_variadic_arg {
|
|
|
|
if p.is_js {
|
|
|
|
p.genln('var $tmp = $expr;')
|
|
|
|
} else {
|
|
|
|
p.genln('$typ $tmp = $expr;')
|
|
|
|
}
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
pad := if is_arr { 6 } else { 4 }
|
2019-09-30 17:11:12 +03:00
|
|
|
var_typ := if is_str { 'byte' }
|
|
|
|
else if is_variadic_arg { typ }
|
2019-10-27 10:03:15 +03:00
|
|
|
else { typ[pad..] }
|
2019-06-22 21:20:28 +03:00
|
|
|
// typ = strings.Replace(typ, "_ptr", "*", -1)
|
2019-09-28 19:53:56 +03:00
|
|
|
mut i_var_type := 'int'
|
2019-10-13 16:58:54 +03:00
|
|
|
if is_variadic_arg {
|
|
|
|
p.gen_for_varg_header(i, expr, typ, val)
|
|
|
|
}
|
|
|
|
else if is_arr {
|
2019-09-14 23:48:30 +03:00
|
|
|
p.gen_for_header(i, tmp, var_typ, val)
|
2019-07-12 08:23:16 +03:00
|
|
|
}
|
|
|
|
else if is_map {
|
2019-09-28 19:53:56 +03:00
|
|
|
i_var_type = 'string'
|
2019-09-14 23:48:30 +03:00
|
|
|
p.gen_for_map_header(i, tmp, var_typ, val, typ)
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-08-29 19:46:03 +03:00
|
|
|
else if is_str {
|
2019-09-28 19:53:56 +03:00
|
|
|
i_var_type = 'byte'
|
|
|
|
p.gen_for_str_header(i, tmp, var_typ, val)
|
|
|
|
}
|
|
|
|
// Register temp vars
|
|
|
|
if i != '_' {
|
|
|
|
p.register_var(Var {
|
2019-08-29 19:46:03 +03:00
|
|
|
name: i
|
2019-09-28 19:53:56 +03:00
|
|
|
typ: i_var_type
|
2019-08-29 19:46:03 +03:00
|
|
|
is_mut: true
|
|
|
|
is_changed: true
|
2019-09-28 19:53:56 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if val != '_' {
|
|
|
|
p.register_var(Var {
|
|
|
|
name: val
|
|
|
|
typ: var_typ
|
|
|
|
ptr: typ.contains('*')
|
|
|
|
})
|
2019-08-29 19:46:03 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// `for val in vals`
|
2019-07-07 23:30:15 +03:00
|
|
|
else if p.peek() == .key_in {
|
2019-06-22 21:20:28 +03:00
|
|
|
val := p.check_name()
|
|
|
|
p.fgen(' ')
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.key_in)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fspace()
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
p.cgen.start_tmp()
|
2019-09-30 17:11:12 +03:00
|
|
|
mut typ := p.bool_expression()
|
2019-06-22 21:20:28 +03:00
|
|
|
expr := p.cgen.end_tmp()
|
2019-07-07 23:30:15 +03:00
|
|
|
is_range := p.tok == .dotdot
|
2019-09-30 17:11:12 +03:00
|
|
|
is_variadic_arg := typ.starts_with('...')
|
2019-10-27 10:03:15 +03:00
|
|
|
if is_variadic_arg { typ = typ[3..] }
|
2019-06-22 21:20:28 +03:00
|
|
|
mut range_end := ''
|
|
|
|
if is_range {
|
|
|
|
p.check_types(typ, 'int')
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check_space(.dotdot)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.cgen.start_tmp()
|
|
|
|
p.check_types(p.bool_expression(), 'int')
|
|
|
|
range_end = p.cgen.end_tmp()
|
|
|
|
}
|
|
|
|
is_arr := typ.contains('array')
|
|
|
|
is_str := typ == 'string'
|
2019-09-30 17:11:12 +03:00
|
|
|
if !is_arr && !is_str && !is_range && !is_variadic_arg {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.error('cannot range over type `$typ`')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-30 17:11:12 +03:00
|
|
|
if !is_variadic_arg {
|
|
|
|
if p.is_js {
|
|
|
|
p.genln('var $tmp = $expr;')
|
|
|
|
} else {
|
|
|
|
p.genln('$typ $tmp = $expr;')
|
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// TODO var_type := if...
|
2019-09-30 17:11:12 +03:00
|
|
|
i := p.get_tmp()
|
|
|
|
mut var_type := typ
|
2019-10-13 16:58:54 +03:00
|
|
|
if is_variadic_arg {
|
|
|
|
p.gen_for_varg_header(i, expr, typ, val)
|
|
|
|
}
|
|
|
|
else if is_range {
|
|
|
|
var_type = 'int'
|
|
|
|
p.gen_for_range_header(i, range_end, tmp, var_type, val)
|
|
|
|
}
|
|
|
|
else if is_arr {
|
2019-10-27 10:03:15 +03:00
|
|
|
var_type = typ[6..]// all after `array_`
|
2019-09-30 17:11:12 +03:00
|
|
|
p.gen_for_header(i, tmp, var_type, val)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
else if is_str {
|
|
|
|
var_type = 'byte'
|
2019-09-30 17:11:12 +03:00
|
|
|
p.gen_for_str_header(i, tmp, var_type, val)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// println('for typ=$typ vartyp=$var_typ')
|
|
|
|
// Register temp var
|
2019-09-28 19:53:56 +03:00
|
|
|
if val != '_' {
|
|
|
|
p.register_var(Var {
|
|
|
|
name: val
|
|
|
|
typ: var_type
|
|
|
|
ptr: typ.contains('*')
|
|
|
|
is_changed: true
|
2019-10-15 18:08:46 +03:00
|
|
|
is_mut: false
|
|
|
|
is_for_var: true
|
2019-09-28 19:53:56 +03:00
|
|
|
})
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-28 19:53:56 +03:00
|
|
|
} else {
|
2019-06-22 21:20:28 +03:00
|
|
|
// `for a < b {`
|
|
|
|
p.gen('while (')
|
|
|
|
p.check_types(p.bool_expression(), 'bool')
|
|
|
|
p.genln(') {')
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fspace()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lcbr)
|
2019-08-17 22:19:37 +03:00
|
|
|
p.genln('')
|
2019-06-22 21:20:28 +03:00
|
|
|
p.statements()
|
2019-07-21 13:43:47 +03:00
|
|
|
p.close_scope()
|
2019-06-22 21:20:28 +03:00
|
|
|
p.for_expr_cnt--
|
2019-08-10 11:26:42 +03:00
|
|
|
p.returns = false // TODO handle loops that are guaranteed to return
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) switch_statement() {
|
2019-10-27 13:03:40 +03:00
|
|
|
p.error('`switch` statement has been removed, use `match` instead:\n' +
|
2019-10-24 19:19:03 +03:00
|
|
|
'https://vlang.io/docs#match')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-28 17:35:44 +03:00
|
|
|
// Returns typ if used as expession
|
2019-08-27 00:39:11 +03:00
|
|
|
fn (p mut Parser) match_statement(is_expr bool) string {
|
|
|
|
p.check(.key_match)
|
|
|
|
p.cgen.start_tmp()
|
|
|
|
typ := p.bool_expression()
|
|
|
|
expr := p.cgen.end_tmp()
|
|
|
|
|
|
|
|
// is it safe to use p.cgen.insert_before ???
|
|
|
|
tmp_var := p.get_tmp()
|
|
|
|
p.cgen.insert_before('$typ $tmp_var = $expr;')
|
|
|
|
|
|
|
|
p.check(.lcbr)
|
|
|
|
mut i := 0
|
|
|
|
mut all_cases_return := true
|
|
|
|
|
2019-08-28 17:35:44 +03:00
|
|
|
// stores typ of resulting variable
|
2019-08-27 00:39:11 +03:00
|
|
|
mut res_typ := ''
|
|
|
|
|
|
|
|
defer {
|
|
|
|
p.check(.rcbr)
|
|
|
|
}
|
|
|
|
|
|
|
|
for p.tok != .rcbr {
|
2019-08-28 17:35:44 +03:00
|
|
|
if p.tok == .key_else {
|
|
|
|
p.check(.key_else)
|
2019-10-21 14:21:30 +03:00
|
|
|
if p.tok == .arrow {
|
2019-10-23 00:56:25 +03:00
|
|
|
p.warn(match_arrow_warning)
|
2019-10-21 14:21:30 +03:00
|
|
|
p.check(.arrow)
|
|
|
|
}
|
2019-08-27 00:39:11 +03:00
|
|
|
|
|
|
|
// unwrap match if there is only else
|
|
|
|
if i == 0 {
|
|
|
|
if is_expr {
|
|
|
|
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
|
|
|
|
|
|
|
// allow braces is else
|
|
|
|
got_brace := p.tok == .lcbr
|
|
|
|
if got_brace {
|
|
|
|
p.check(.lcbr)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.gen('( ')
|
|
|
|
|
|
|
|
res_typ = p.bool_expression()
|
|
|
|
|
|
|
|
p.gen(' )')
|
|
|
|
|
|
|
|
// allow braces in else
|
|
|
|
if got_brace {
|
|
|
|
p.check(.rcbr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res_typ
|
|
|
|
} else {
|
2019-09-04 19:48:01 +03:00
|
|
|
p.returns = false
|
|
|
|
p.check(.lcbr)
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-09-04 19:48:01 +03:00
|
|
|
p.genln('{ ')
|
|
|
|
p.statements()
|
2019-08-27 00:39:11 +03:00
|
|
|
p.returns = all_cases_return && p.returns
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_expr {
|
|
|
|
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
|
|
|
p.gen(':(')
|
|
|
|
|
|
|
|
// allow braces is else
|
|
|
|
got_brace := p.tok == .lcbr
|
|
|
|
if got_brace {
|
|
|
|
p.check(.lcbr)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.check_types(p.bool_expression(), res_typ)
|
|
|
|
|
|
|
|
// allow braces in else
|
|
|
|
if got_brace {
|
|
|
|
p.check(.rcbr)
|
|
|
|
}
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-08-27 00:39:11 +03:00
|
|
|
p.gen(strings.repeat(`)`, i+1))
|
|
|
|
|
|
|
|
return res_typ
|
|
|
|
} else {
|
2019-09-04 19:48:01 +03:00
|
|
|
p.returns = false
|
2019-08-27 00:39:11 +03:00
|
|
|
p.genln('else // default:')
|
2019-09-04 19:48:01 +03:00
|
|
|
|
|
|
|
p.check(.lcbr)
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-09-04 19:48:01 +03:00
|
|
|
p.genln('{ ')
|
|
|
|
p.statements()
|
|
|
|
|
2019-08-27 00:39:11 +03:00
|
|
|
p.returns = all_cases_return && p.returns
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 0 {
|
|
|
|
if is_expr {
|
|
|
|
p.gen(': (')
|
|
|
|
} else {
|
|
|
|
p.gen('else ')
|
|
|
|
}
|
|
|
|
} else if is_expr {
|
|
|
|
p.gen('(')
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_expr {
|
|
|
|
p.gen('(')
|
|
|
|
} else {
|
|
|
|
p.gen('if (')
|
|
|
|
}
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-10-18 20:17:23 +03:00
|
|
|
ph := p.cgen.add_placeholder()
|
|
|
|
|
2019-08-27 00:39:11 +03:00
|
|
|
// Multiple checks separated by comma
|
|
|
|
mut got_comma := false
|
|
|
|
|
|
|
|
for {
|
|
|
|
if got_comma {
|
|
|
|
p.gen(') || (')
|
|
|
|
}
|
|
|
|
|
2019-10-18 20:17:23 +03:00
|
|
|
mut got_string := false
|
|
|
|
|
2019-08-28 17:35:44 +03:00
|
|
|
if typ == 'string' {
|
2019-10-18 20:17:23 +03:00
|
|
|
got_string = true
|
2019-08-27 00:39:11 +03:00
|
|
|
p.gen('string_eq($tmp_var, ')
|
|
|
|
}
|
|
|
|
else {
|
2019-10-18 20:17:23 +03:00
|
|
|
p.gen('$tmp_var == ')
|
2019-08-27 00:39:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
p.expected_type = typ
|
|
|
|
p.check_types(p.bool_expression(), typ)
|
|
|
|
p.expected_type = ''
|
|
|
|
|
2019-10-18 20:17:23 +03:00
|
|
|
if got_string {
|
|
|
|
p.gen(')')
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:39:11 +03:00
|
|
|
if p.tok != .comma {
|
2019-09-04 19:47:37 +03:00
|
|
|
if got_comma {
|
|
|
|
p.gen(') ')
|
2019-10-18 20:17:23 +03:00
|
|
|
p.cgen.set_placeholder(ph, '(')
|
2019-09-04 19:47:37 +03:00
|
|
|
}
|
2019-08-27 00:39:11 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
p.check(.comma)
|
|
|
|
got_comma = true
|
|
|
|
}
|
2019-10-18 20:17:23 +03:00
|
|
|
p.gen(')')
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-10-23 00:56:25 +03:00
|
|
|
if p.tok == .arrow {
|
|
|
|
p.warn(match_arrow_warning)
|
|
|
|
p.check(.arrow)
|
|
|
|
}
|
2019-10-12 22:01:50 +03:00
|
|
|
|
|
|
|
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
2019-08-27 00:39:11 +03:00
|
|
|
if is_expr {
|
|
|
|
p.gen('? (')
|
|
|
|
|
|
|
|
// braces are required for now
|
|
|
|
p.check(.lcbr)
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-08-27 00:39:11 +03:00
|
|
|
if i == 0 {
|
|
|
|
// on the first iteration we set value of res_typ
|
|
|
|
res_typ = p.bool_expression()
|
|
|
|
} else {
|
|
|
|
// later on we check that the value is of res_typ type
|
|
|
|
p.check_types(p.bool_expression(), res_typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
// braces are required for now
|
|
|
|
p.check(.rcbr)
|
|
|
|
|
|
|
|
p.gen(')')
|
|
|
|
}
|
|
|
|
else {
|
2019-09-04 19:48:01 +03:00
|
|
|
p.returns = false
|
|
|
|
p.check(.lcbr)
|
2019-10-12 22:01:50 +03:00
|
|
|
|
2019-09-04 19:48:01 +03:00
|
|
|
p.genln('{ ')
|
|
|
|
p.statements()
|
|
|
|
|
|
|
|
all_cases_return = all_cases_return && p.returns
|
2019-08-27 00:39:11 +03:00
|
|
|
// p.gen(')')
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_expr {
|
|
|
|
// we get here if no else found, ternary requires "else" branch
|
|
|
|
p.error('Match expession requires "else"')
|
|
|
|
}
|
|
|
|
|
|
|
|
p.returns = false // only get here when no default, so return is not guaranteed
|
|
|
|
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (p mut Parser) assert_statement() {
|
2019-07-29 19:21:36 +03:00
|
|
|
if p.first_pass() {
|
2019-08-17 22:19:37 +03:00
|
|
|
return
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.key_assert)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.fspace()
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
p.gen('bool $tmp = ')
|
|
|
|
p.check_types(p.bool_expression(), 'bool')
|
2019-10-30 12:15:33 +03:00
|
|
|
nline := p.scanner.line_nr
|
2019-06-22 21:20:28 +03:00
|
|
|
// TODO print "expected: got" for failed tests
|
2019-10-25 16:34:12 +03:00
|
|
|
filename := cescaped_path(p.file_path)
|
2019-10-30 12:15:33 +03:00
|
|
|
cfname:=p.cur_fn.name.replace('main__', '')
|
|
|
|
sourceline := p.scanner.line( nline - 1 ).replace('"', '\'')
|
2019-10-07 08:51:26 +03:00
|
|
|
|
2019-10-30 12:15:33 +03:00
|
|
|
if !p.pref.is_test {
|
|
|
|
// an assert used in a normal v program. no fancy formatting
|
|
|
|
p.genln(';\n
|
|
|
|
/// sline: "$sourceline"
|
|
|
|
if (!$tmp) {
|
|
|
|
g_test_fails++;
|
|
|
|
eprintln(tos3("${filename}:${p.scanner.line_nr}: FAILED: ${cfname}()"));
|
|
|
|
eprintln(tos3("Source: $sourceline"));
|
|
|
|
v_panic(tos3("An assertion failed."));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
g_test_oks++;
|
|
|
|
}
|
|
|
|
')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.genln(';\n
|
2019-07-31 20:01:52 +03:00
|
|
|
if (!$tmp) {
|
2019-10-07 08:51:26 +03:00
|
|
|
g_test_fails++;
|
2019-10-30 15:04:25 +03:00
|
|
|
main__cb_assertion_failed(
|
|
|
|
tos3("$filename"),
|
|
|
|
$p.scanner.line_nr,
|
2019-10-30 12:15:33 +03:00
|
|
|
tos3("$sourceline"),
|
|
|
|
tos3("$p.cur_fn.name()")
|
|
|
|
);
|
2019-10-10 20:24:36 +03:00
|
|
|
return;
|
2019-10-07 08:51:26 +03:00
|
|
|
// TODO
|
|
|
|
// Maybe print all vars in a test function if it fails?
|
|
|
|
} else {
|
|
|
|
g_test_oks++;
|
2019-10-30 15:04:25 +03:00
|
|
|
main__cb_assertion_ok(
|
|
|
|
tos3("$filename"),
|
|
|
|
$p.scanner.line_nr,
|
2019-10-30 12:15:33 +03:00
|
|
|
tos3("$sourceline"),
|
|
|
|
tos3("$p.cur_fn.name()")
|
|
|
|
);
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-10-07 08:51:26 +03:00
|
|
|
|
|
|
|
')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) return_st() {
|
2019-07-15 17:15:34 +03:00
|
|
|
p.check(.key_return)
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fgen(' ')
|
2019-10-12 22:01:50 +03:00
|
|
|
deferred_text := p.get_deferred_text()
|
2019-06-22 21:20:28 +03:00
|
|
|
fn_returns := p.cur_fn.typ != 'void'
|
|
|
|
if fn_returns {
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.tok == .rcbr {
|
2019-06-22 21:20:28 +03:00
|
|
|
p.error('`$p.cur_fn.name` needs to return `$p.cur_fn.typ`')
|
|
|
|
}
|
2019-10-12 22:01:50 +03:00
|
|
|
ph := p.cgen.add_placeholder()
|
|
|
|
p.inside_return_expr = true
|
|
|
|
is_none := p.tok == .key_none
|
|
|
|
p.expected_type = p.cur_fn.typ
|
|
|
|
mut expr_type := p.bool_expression()
|
2019-10-20 10:19:37 +03:00
|
|
|
// println('$p.cur_fn.name returns type $expr_type, should be $p.cur_fn.typ')
|
2019-10-12 22:01:50 +03:00
|
|
|
mut types := []string
|
2019-10-27 10:03:15 +03:00
|
|
|
mut mr_values := [p.cgen.cur_line[ph..].trim_space()]
|
2019-10-12 22:01:50 +03:00
|
|
|
types << expr_type
|
|
|
|
for p.tok == .comma {
|
|
|
|
p.check(.comma)
|
|
|
|
p.cgen.start_tmp()
|
|
|
|
types << p.bool_expression()
|
|
|
|
mr_values << p.cgen.end_tmp().trim_space()
|
|
|
|
}
|
|
|
|
mut cur_fn_typ_chk := p.cur_fn.typ
|
|
|
|
// multiple returns
|
|
|
|
if types.len > 1 {
|
|
|
|
expr_type = types.join(',')
|
|
|
|
cur_fn_typ_chk = cur_fn_typ_chk.replace('_V_MulRet_', '').replace('_PTR_', '*').replace('_V_', ',')
|
|
|
|
mut ret_fields := ''
|
|
|
|
for ret_val_idx, ret_val in mr_values {
|
|
|
|
if ret_val_idx > 0 {
|
|
|
|
ret_fields += ','
|
2019-09-23 05:45:19 +03:00
|
|
|
}
|
2019-10-12 22:01:50 +03:00
|
|
|
ret_fields += '.var_$ret_val_idx=${ret_val}'
|
2019-07-15 17:15:34 +03:00
|
|
|
}
|
2019-10-12 22:01:50 +03:00
|
|
|
p.cgen.resetln('($p.cur_fn.typ){$ret_fields}')
|
|
|
|
}
|
|
|
|
p.inside_return_expr = false
|
|
|
|
// Automatically wrap an object inside an option if the function
|
|
|
|
// returns an option:
|
|
|
|
// `return val` => `return opt_ok(val)`
|
|
|
|
if p.cur_fn.typ.ends_with(expr_type) && !is_none &&
|
|
|
|
p.cur_fn.typ.starts_with('Option_') {
|
|
|
|
tmp := p.get_tmp()
|
2019-10-27 10:03:15 +03:00
|
|
|
ret := p.cgen.cur_line[ph..]
|
2019-10-12 22:01:50 +03:00
|
|
|
typ := expr_type.replace('Option_', '')
|
|
|
|
p.cgen.resetln('$expr_type $tmp = OPTION_CAST($expr_type)($ret);')
|
|
|
|
p.genln(deferred_text)
|
|
|
|
p.gen('return opt_ok(&$tmp, sizeof($typ))')
|
|
|
|
}
|
|
|
|
else {
|
2019-10-27 10:03:15 +03:00
|
|
|
ret := p.cgen.cur_line[ph..]
|
2019-08-07 18:51:21 +03:00
|
|
|
|
2019-10-12 22:01:50 +03:00
|
|
|
if deferred_text == '' || expr_type == 'void*' {
|
|
|
|
// no defer{} necessary?
|
|
|
|
if expr_type == '${p.cur_fn.typ}*' {
|
|
|
|
p.cgen.resetln('return *$ret')
|
|
|
|
} else {
|
|
|
|
p.cgen.resetln('return $ret')
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-10-12 22:01:50 +03:00
|
|
|
} else {
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
p.cgen.resetln('$expr_type $tmp = $ret;\n')
|
|
|
|
p.genln(deferred_text)
|
|
|
|
p.genln('return $tmp;')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-12 22:01:50 +03:00
|
|
|
p.check_types(expr_type, cur_fn_typ_chk)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Don't allow `return val` in functions that don't return anything
|
2019-08-17 22:19:37 +03:00
|
|
|
if !p.is_vweb && (p.tok == .name || p.tok == .number || p.tok == .str) {
|
2019-09-29 20:37:39 +03:00
|
|
|
p.error_with_token_index('function `$p.cur_fn.name` should not return a value', p.cur_fn.fn_name_token_idx)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-15 17:32:35 +03:00
|
|
|
|
2019-10-12 22:01:50 +03:00
|
|
|
p.genln(deferred_text)
|
2019-07-15 17:32:35 +03:00
|
|
|
if p.cur_fn.name == 'main' {
|
|
|
|
p.gen('return 0')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen('return')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
p.returns = true
|
|
|
|
}
|
|
|
|
|
2019-10-12 22:01:50 +03:00
|
|
|
fn (p Parser) get_deferred_text() string {
|
|
|
|
// @emily33901: Scoped defer
|
|
|
|
// Check all of our defer texts to see if there is one at a higher scope level
|
|
|
|
// The one for our current scope would be the last so any before that need to be
|
|
|
|
// added.
|
|
|
|
mut deferred_text := ''
|
|
|
|
for text in p.cur_fn.defer_text {
|
|
|
|
if text != '' {
|
|
|
|
// In reverse order
|
|
|
|
deferred_text = text + deferred_text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return deferred_text
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:49:52 +03:00
|
|
|
fn prepend_mod(mod, name string) string {
|
|
|
|
return '${mod}__${name}'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-05 10:49:52 +03:00
|
|
|
fn (p &Parser) prepend_mod(name string) string {
|
2019-10-13 03:05:11 +03:00
|
|
|
return prepend_mod(mod_gen_name(p.mod), name)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) go_statement() {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.key_go)
|
2019-09-29 20:37:39 +03:00
|
|
|
mut gotoken_idx := p.cur_tok_index()
|
2019-06-22 21:20:28 +03:00
|
|
|
// TODO copypasta of name_expr() ?
|
2019-07-07 23:30:15 +03:00
|
|
|
if p.peek() == .dot {
|
2019-09-28 20:42:29 +03:00
|
|
|
// Method
|
2019-06-22 21:20:28 +03:00
|
|
|
var_name := p.lit
|
2019-09-28 20:42:29 +03:00
|
|
|
v := p.find_var(var_name) or {
|
|
|
|
return
|
|
|
|
}
|
2019-09-09 16:22:39 +03:00
|
|
|
p.mark_var_used(v)
|
2019-09-29 20:37:39 +03:00
|
|
|
gotoken_idx = p.cur_tok_index()
|
2019-06-22 21:20:28 +03:00
|
|
|
p.next()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.dot)
|
2019-06-22 21:20:28 +03:00
|
|
|
typ := p.table.find_type(v.typ)
|
2019-09-28 20:42:29 +03:00
|
|
|
method := p.table.find_method(typ, p.lit) or {
|
2019-09-29 20:37:39 +03:00
|
|
|
p.error_with_token_index('go method missing $var_name', gotoken_idx)
|
2019-09-28 20:42:29 +03:00
|
|
|
return
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
p.async_fn_call(method, 0, var_name, v.typ)
|
|
|
|
}
|
|
|
|
else {
|
2019-09-28 20:42:29 +03:00
|
|
|
f_name := p.lit
|
|
|
|
// Normal function
|
|
|
|
f := p.table.find_fn(p.prepend_mod(f_name)) or {
|
|
|
|
println( p.table.debug_fns() )
|
2019-09-29 20:37:39 +03:00
|
|
|
p.error_with_token_index('can not find function $f_name', gotoken_idx)
|
2019-09-28 20:42:29 +03:00
|
|
|
return
|
|
|
|
}
|
2019-09-19 17:25:00 +03:00
|
|
|
if f.name == 'println' || f.name == 'print' {
|
2019-09-29 20:37:39 +03:00
|
|
|
p.error_with_token_index('`go` cannot be used with `println`', gotoken_idx)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
p.async_fn_call(f, 0, '', '')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 20:34:08 +03:00
|
|
|
/*
|
2019-07-05 03:44:22 +03:00
|
|
|
fn (p mut Parser) register_var(v Var) {
|
2019-06-22 21:20:28 +03:00
|
|
|
if v.line_nr == 0 {
|
2019-09-20 13:42:37 +03:00
|
|
|
spos := p.scanner.get_scanner_pos()
|
2019-09-23 20:34:08 +03:00
|
|
|
p.register_var({ v | scanner_pos: spos, line_nr: spos.line_nr })
|
2019-08-17 22:19:37 +03:00
|
|
|
} else {
|
2019-09-23 20:34:08 +03:00
|
|
|
p.register_var(v)
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-23 20:34:08 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
|
|
|
|
// user:=jsdecode(User, user_json_string)
|
|
|
|
fn (p mut Parser) js_decode() string {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.name)// json
|
|
|
|
p.check(.dot)
|
2019-06-22 21:20:28 +03:00
|
|
|
op := p.check_name()
|
2019-10-12 22:01:50 +03:00
|
|
|
op_token_idx := p.cur_tok_index()
|
2019-06-22 21:20:28 +03:00
|
|
|
if op == 'decode' {
|
|
|
|
// User tmp2; tmp2.foo = 0; tmp2.bar = 0;// I forgot to zero vals before => huge bug
|
|
|
|
// Option_User tmp3 = jsdecode_User(json_parse( s), &tmp2); ;
|
|
|
|
// if (!tmp3 .ok) {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// User u = *(User*) tmp3 . data; // TODO remove this (generated in or {} block handler)
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lpar)
|
2019-06-22 21:20:28 +03:00
|
|
|
typ := p.get_type()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.comma)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.cgen.start_tmp()
|
|
|
|
p.check_types(p.bool_expression(), 'string')
|
|
|
|
expr := p.cgen.end_tmp()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rpar)
|
2019-06-22 21:20:28 +03:00
|
|
|
tmp := p.get_tmp()
|
|
|
|
cjson_tmp := p.get_tmp()
|
|
|
|
mut decl := '$typ $tmp; '
|
|
|
|
// Init the struct
|
|
|
|
T := p.table.find_type(typ)
|
|
|
|
for field in T.fields {
|
|
|
|
def_val := type_default(field.typ)
|
|
|
|
if def_val != '' {
|
2019-08-04 14:28:59 +03:00
|
|
|
decl += '$tmp . $field.name = OPTION_CAST($field.typ) $def_val;\n'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p.gen_json_for_type(T)
|
|
|
|
decl += 'cJSON* $cjson_tmp = json__json_parse($expr);'
|
|
|
|
p.cgen.insert_before(decl)
|
|
|
|
// p.gen('jsdecode_$typ(json_parse($expr), &$tmp);')
|
|
|
|
p.gen('json__jsdecode_$typ($cjson_tmp, &$tmp); cJSON_Delete($cjson_tmp);')
|
|
|
|
opt_type := 'Option_$typ'
|
|
|
|
p.cgen.typedefs << 'typedef Option $opt_type;'
|
2019-10-24 12:47:21 +03:00
|
|
|
p.table.register_builtin(opt_type)
|
2019-06-22 21:20:28 +03:00
|
|
|
return opt_type
|
|
|
|
}
|
|
|
|
else if op == 'encode' {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lpar)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.cgen.start_tmp()
|
|
|
|
typ := p.bool_expression()
|
|
|
|
T := p.table.find_type(typ)
|
|
|
|
p.gen_json_for_type(T)
|
|
|
|
expr := p.cgen.end_tmp()
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.rpar)
|
2019-06-22 21:20:28 +03:00
|
|
|
p.gen('json__json_print(json__jsencode_$typ($expr))')
|
|
|
|
return 'string'
|
|
|
|
}
|
|
|
|
else {
|
2019-09-29 20:37:39 +03:00
|
|
|
p.error_with_token_index('bad json op "$op"', op_token_idx)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2019-07-07 22:46:21 +03:00
|
|
|
fn (p mut Parser) attribute() {
|
2019-07-07 23:30:15 +03:00
|
|
|
p.check(.lsbr)
|
2019-10-27 02:03:06 +03:00
|
|
|
if p.tok == .key_if {
|
|
|
|
// [if vfmt]
|
|
|
|
p.next()
|
|
|
|
p.attr = 'if ' + p.check_name()
|
|
|
|
} else {
|
|
|
|
p.attr = p.check_name()
|
|
|
|
}
|
2019-09-29 20:37:39 +03:00
|
|
|
attr_token_idx := p.cur_tok_index()
|
2019-09-21 21:38:12 +03:00
|
|
|
if p.tok == .colon {
|
2019-08-20 14:34:29 +03:00
|
|
|
p.check(.colon)
|
2019-09-21 21:38:12 +03:00
|
|
|
p.attr = p.attr + ':' + p.check_name()
|
2019-10-12 22:01:50 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.rsbr)
|
2019-10-21 14:21:30 +03:00
|
|
|
if p.tok == .key_fn || (p.tok == .key_pub && p.peek() == .key_fn) {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.fn_decl()
|
|
|
|
p.attr = ''
|
|
|
|
return
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
else if p.tok == .key_struct {
|
2019-08-17 22:19:37 +03:00
|
|
|
p.struct_decl()
|
|
|
|
p.attr = ''
|
|
|
|
return
|
|
|
|
}
|
2019-09-29 20:37:39 +03:00
|
|
|
p.error_with_token_index('bad attribute usage', attr_token_idx)
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-07-07 22:46:21 +03:00
|
|
|
|
2019-07-15 23:09:34 +03:00
|
|
|
fn (p mut Parser) defer_st() {
|
|
|
|
p.check(.key_defer)
|
2019-08-17 22:19:37 +03:00
|
|
|
p.check(.lcbr)
|
2019-07-30 19:05:06 +03:00
|
|
|
|
2019-08-17 22:19:37 +03:00
|
|
|
pos := p.cgen.lines.len
|
2019-07-30 19:05:06 +03:00
|
|
|
|
|
|
|
// Save everything inside the defer block to `defer_text`.
|
|
|
|
// It will be inserted before every `return`
|
2019-08-07 18:51:21 +03:00
|
|
|
|
|
|
|
// Emily: TODO: all variables that are used in this defer statement need to be evaluated when the block
|
|
|
|
// is defined otherwise they could change over the course of the function
|
|
|
|
// (make temps out of them)
|
|
|
|
|
2019-08-17 22:19:37 +03:00
|
|
|
p.genln('{')
|
|
|
|
p.statements()
|
2019-10-27 10:03:15 +03:00
|
|
|
p.cur_fn.defer_text.last() = p.cgen.lines[pos..].join('\n') + p.cur_fn.defer_text.last()
|
2019-07-30 19:05:06 +03:00
|
|
|
|
|
|
|
// Rollback p.cgen.lines
|
2019-10-27 10:03:15 +03:00
|
|
|
p.cgen.lines = p.cgen.lines[..pos]
|
2019-07-30 19:05:06 +03:00
|
|
|
p.cgen.resetln('')
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-07-15 23:09:34 +03:00
|
|
|
|
2019-09-23 13:42:20 +03:00
|
|
|
fn (p mut Parser) check_and_register_used_imported_type(typ_name string) {
|
|
|
|
us_idx := typ_name.index('__')
|
|
|
|
if us_idx != -1 {
|
2019-10-27 10:03:15 +03:00
|
|
|
arg_mod := typ_name[..us_idx]
|
2019-09-23 13:42:20 +03:00
|
|
|
if p.import_table.known_alias(arg_mod) {
|
|
|
|
p.import_table.register_used_import(arg_mod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) check_unused_imports() {
|
2019-09-26 05:28:43 +03:00
|
|
|
// Don't run in the generated V file with `.str()`
|
2019-10-25 16:34:12 +03:00
|
|
|
if p.is_vgen {
|
2019-09-26 05:28:43 +03:00
|
|
|
return
|
2019-09-27 14:02:01 +03:00
|
|
|
}
|
2019-09-23 13:42:20 +03:00
|
|
|
mut output := ''
|
|
|
|
for alias, mod in p.import_table.imports {
|
|
|
|
if !p.import_table.is_used_import(alias) {
|
|
|
|
mod_alias := if alias == mod { alias } else { '$alias ($mod)' }
|
|
|
|
output += '\n * $mod_alias'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if output == '' { return }
|
2019-09-29 20:37:39 +03:00
|
|
|
// the imports are usually at the start of the file
|
|
|
|
p.production_error_with_token_index( 'the following imports were never used: $output', 0 )
|
2019-09-23 13:42:20 +03:00
|
|
|
}
|
2019-10-20 12:24:12 +03:00
|
|
|
|
|
|
|
fn (p mut Parser) is_next_expr_fn_call() (bool, string) {
|
|
|
|
mut next_expr := p.lit
|
2019-10-20 19:59:53 +03:00
|
|
|
mut is_fn_call := p.peek() == .lpar
|
2019-10-20 12:24:12 +03:00
|
|
|
if !is_fn_call {
|
|
|
|
mut i := p.token_idx+1
|
|
|
|
for (p.tokens[i].tok == .dot || p.tokens[i].tok == .name) &&
|
|
|
|
p.tokens[i].lit != '_' && i < p.tokens.len {
|
|
|
|
next_expr += if p.tokens[i].tok == .dot { '.' } else { p.tokens[i].lit }
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
is_fn_call = p.tokens[i].tok == .lpar
|
|
|
|
}
|
|
|
|
return is_fn_call, next_expr
|
|
|
|
}
|