2019-06-23 04:21:30 +02: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-06-22 20:20:28 +02:00
|
|
|
module main
|
|
|
|
|
2019-07-29 18:21:36 +02:00
|
|
|
import (
|
|
|
|
os
|
|
|
|
rand
|
|
|
|
strings
|
2019-08-17 21:19:37 +02:00
|
|
|
)
|
2019-06-22 20:20:28 +02:00
|
|
|
|
|
|
|
struct Var {
|
|
|
|
mut:
|
|
|
|
typ string
|
|
|
|
name string
|
|
|
|
is_arg bool
|
|
|
|
is_const bool
|
|
|
|
args []Var // function args
|
|
|
|
attr string // [json] etc
|
|
|
|
is_mut bool
|
2019-08-17 21:19:37 +02:00
|
|
|
is_alloc bool
|
2019-06-22 20:20:28 +02:00
|
|
|
ptr bool
|
|
|
|
ref bool
|
|
|
|
parent_fn string // Variables can only be defined in functions
|
2019-08-17 21:19:37 +02:00
|
|
|
mod string // module where this var is stored
|
2019-06-22 20:20:28 +02:00
|
|
|
line_nr int
|
|
|
|
access_mod AccessMod
|
|
|
|
is_global bool // __global (translated from C only)
|
|
|
|
is_used bool
|
2019-08-17 21:19:37 +02:00
|
|
|
is_changed bool
|
2019-06-22 20:20:28 +02:00
|
|
|
scope_level int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Parser {
|
|
|
|
file_path string // "/home/user/hello.v"
|
|
|
|
file_name string // "hello.v"
|
2019-08-04 20:39:43 +02:00
|
|
|
file_platform string // ".v", "_win.v", "_nix.v", "_mac.v", "_lin.v" ...
|
2019-08-17 21:19:37 +02:00
|
|
|
file_pcguard string // When p.file_pcguard != '', it contains a
|
|
|
|
// C ifdef guard clause that must be put before
|
2019-08-04 20:39:43 +02:00
|
|
|
// the #include directives in the parsed .v file
|
2019-06-22 20:20:28 +02:00
|
|
|
mut:
|
2019-08-17 21:19:37 +02:00
|
|
|
v *V
|
2019-06-22 20:20:28 +02:00
|
|
|
scanner *Scanner
|
|
|
|
// tokens []Token // TODO cache all tokens, right now they have to be scanned twice
|
|
|
|
token_idx int
|
|
|
|
tok Token
|
|
|
|
prev_tok Token
|
|
|
|
prev_tok2 Token // TODO remove these once the tokens are cached
|
|
|
|
lit string
|
|
|
|
cgen *CGen
|
|
|
|
table *Table
|
2019-07-12 07:37:54 +02:00
|
|
|
import_table *FileImportTable // Holds imports for just the file being parsed
|
2019-08-17 21:19:37 +02:00
|
|
|
pass Pass
|
|
|
|
os OS
|
2019-07-07 22:30:15 +02:00
|
|
|
mod string
|
2019-06-22 20:20:28 +02:00
|
|
|
inside_const bool
|
|
|
|
expr_var Var
|
|
|
|
assigned_type string
|
2019-07-07 22:30:15 +02:00
|
|
|
expected_type string
|
2019-06-22 20:20:28 +02:00
|
|
|
tmp_cnt int
|
|
|
|
is_script bool
|
2019-06-30 22:03:17 +02:00
|
|
|
pref *Preferences // Setting and Preferences shared from V struct
|
2019-08-05 09:49:52 +02:00
|
|
|
builtin_mod bool
|
2019-06-22 20:20:28 +02:00
|
|
|
vh_lines []string
|
|
|
|
inside_if_expr bool
|
|
|
|
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
|
|
|
|
cur_fn *Fn
|
|
|
|
returns bool
|
2019-06-25 14:24:12 +02:00
|
|
|
vroot string
|
2019-06-27 17:48:49 +02:00
|
|
|
is_c_struct_init bool
|
2019-08-20 13:34:29 +02:00
|
|
|
is_empty_c_struct_init bool
|
|
|
|
is_c_fn_call bool
|
2019-06-27 19:02:47 +02:00
|
|
|
can_chash bool
|
2019-08-17 21:19:37 +02: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
|
|
|
|
building_v bool
|
|
|
|
is_alloc bool // Whether current expression resulted in an allocation
|
|
|
|
cur_gen_type string // "App" to replace "T" in current generic function
|
|
|
|
is_vweb bool
|
|
|
|
is_sql bool
|
|
|
|
sql_i int // $1 $2 $3
|
2019-08-20 16:32:39 +02:00
|
|
|
sql_params []string // ("select * from users where id = $1", ***"100"***)
|
2019-08-20 17:05:56 +02:00
|
|
|
sql_types []string // int, string and so on; see sql_params
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
EmptyFn = &Fn { }
|
2019-08-17 21:19:37 +02:00
|
|
|
MainFn= &Fn{name:'main'}
|
2019-06-22 20:20:28 +02:00
|
|
|
)
|
|
|
|
|
2019-07-10 09:02:04 +02:00
|
|
|
const (
|
|
|
|
MaxModuleDepth = 4
|
2019-08-17 21:19:37 +02:00
|
|
|
)
|
2019-07-10 09:02:04 +02:00
|
|
|
|
2019-07-29 18:21:36 +02:00
|
|
|
fn (v mut V) new_parser(path string, pass Pass) Parser {
|
|
|
|
v.log('new_parser("$path")')
|
2019-08-04 20:39:43 +02:00
|
|
|
v.cgen.pass = pass
|
|
|
|
|
|
|
|
mut path_pcguard := ''
|
|
|
|
mut path_platform := '.v'
|
|
|
|
for path_ending in ['_lin.v', '_mac.v', '_win.v', '_nix.v'] {
|
|
|
|
if path.ends_with(path_ending) {
|
|
|
|
path_platform = path_ending
|
|
|
|
path_pcguard = platform_postfix_to_ifdefguard( path_ending )
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
mut p := Parser {
|
2019-08-17 21:19:37 +02:00
|
|
|
v: v
|
2019-06-22 20:20:28 +02:00
|
|
|
file_path: path
|
|
|
|
file_name: path.all_after('/')
|
2019-08-04 20:39:43 +02:00
|
|
|
file_platform: path_platform
|
|
|
|
file_pcguard: path_pcguard
|
2019-06-22 20:20:28 +02:00
|
|
|
scanner: new_scanner(path)
|
2019-07-29 18:21:36 +02:00
|
|
|
table: v.table
|
2019-07-12 07:37:54 +02:00
|
|
|
import_table: new_file_import_table(path)
|
2019-06-22 20:20:28 +02:00
|
|
|
cur_fn: EmptyFn
|
2019-07-29 18:21:36 +02:00
|
|
|
cgen: v.cgen
|
|
|
|
is_script: (v.pref.is_script && path == v.dir)
|
|
|
|
pref: v.pref
|
|
|
|
os: v.os
|
2019-08-17 21:19:37 +02:00
|
|
|
pass: pass
|
2019-07-29 18:21:36 +02:00
|
|
|
vroot: v.vroot
|
2019-08-17 21:19:37 +02:00
|
|
|
building_v: !v.pref.is_repl && (path.contains('compiler/') ||
|
|
|
|
path.contains('v/vlib'))
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-26 16:45:16 +02:00
|
|
|
|
2019-07-29 18:21:36 +02:00
|
|
|
v.cgen.line_directives = v.pref.is_debuggable
|
|
|
|
v.cgen.file = path
|
2019-07-26 16:45:16 +02:00
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
// p.scanner.debug_tokens()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) next() {
|
|
|
|
p.prev_tok2 = p.prev_tok
|
|
|
|
p.prev_tok = p.tok
|
2019-08-17 21:19:37 +02:00
|
|
|
p.scanner.prev_tok = p.tok
|
2019-06-22 20:20:28 +02:00
|
|
|
res := p.scanner.scan()
|
|
|
|
p.tok = res.tok
|
|
|
|
p.lit = res.lit
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p &Parser) log(s string) {
|
2019-08-17 21:19:37 +02:00
|
|
|
/*
|
2019-06-30 22:03:17 +02:00
|
|
|
if !p.pref.is_verbose {
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
println(s)
|
2019-08-17 21:19:37 +02:00
|
|
|
*/
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) parse() {
|
2019-07-29 18:21:36 +02:00
|
|
|
p.log('\nparse() run=$p.pass file=$p.file_name tok=${p.strtok()}')// , "script_file=", script_file)
|
2019-06-22 20:20:28 +02:00
|
|
|
// `module main` is not required if it's a single file program
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.is_script || p.pref.is_test {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.mod = 'main'
|
2019-06-22 20:20:28 +02:00
|
|
|
// User may still specify `module main`
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .key_module {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
p.fgen('module ')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.mod = p.check_name()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.key_module)
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fspace()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.mod = p.check_name()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.fgenln('\n')
|
2019-08-05 09:49:52 +02:00
|
|
|
p.builtin_mod = p.mod == 'builtin'
|
2019-08-20 13:34:29 +02:00
|
|
|
p.can_chash = p.mod=='ui' || p.mod == 'darwin'// TODO tmp remove
|
2019-06-22 20:20:28 +02:00
|
|
|
// Import pass - the first and the smallest pass that only analyzes imports
|
2019-07-12 07:37:54 +02:00
|
|
|
// fully qualify the module name, eg base64 to encoding.base64
|
|
|
|
fq_mod := p.table.qualify_module(p.mod, p.file_path)
|
2019-07-21 17:53:35 +02:00
|
|
|
p.import_table.module_name = fq_mod
|
2019-08-05 09:49:52 +02:00
|
|
|
p.table.register_module(fq_mod)
|
2019-07-13 15:11:32 +02:00
|
|
|
// replace "." with "_dot_" in module name for C variable names
|
|
|
|
p.mod = fq_mod.replace('.', '_dot_')
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.pass == .imports {
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok == .key_import && p.peek() != .key_const {
|
2019-07-16 17:59:07 +02:00
|
|
|
p.imports()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-04 17:36:53 +02:00
|
|
|
if p.table.imports.contains('builtin') {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error('module `builtin` cannot be imported')
|
2019-08-05 09:49:52 +02:00
|
|
|
}
|
|
|
|
// save file import table
|
|
|
|
p.table.file_imports << *p.import_table
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Go through every top level token or throw a compilation error if a non-top level token is met
|
|
|
|
for {
|
|
|
|
switch p.tok {
|
2019-08-20 13:34:29 +02:00
|
|
|
case .key_import:
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.peek() == .key_const {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.const_decl()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO remove imported consts from the language
|
2019-07-16 17:59:07 +02:00
|
|
|
p.imports()
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.tok != .key_import {
|
|
|
|
p.fgenln('')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_enum:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .name {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen('enum ')
|
|
|
|
name := p.check_name()
|
|
|
|
p.fgen(' ')
|
|
|
|
p.enum_decl(name)
|
|
|
|
}
|
|
|
|
// 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-30 22:03:17 +02:00
|
|
|
else if p.pref.translated {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.enum_decl('int')
|
|
|
|
}
|
|
|
|
else {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.name)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_pub:
|
|
|
|
if p.peek() == .func {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fn_decl()
|
2019-07-07 22:30:15 +02:00
|
|
|
} else if p.peek() == .key_struct {
|
2019-06-26 20:23:10 +02:00
|
|
|
p.error('structs can\'t be declared public *yet*')
|
|
|
|
// TODO public structs
|
|
|
|
} else {
|
|
|
|
p.error('wrong pub keyword usage')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.func:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fn_decl()
|
2019-07-16 12:17:17 +02:00
|
|
|
case Token.key_type:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.type_decl()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.lsbr:
|
2019-07-07 21:46:21 +02:00
|
|
|
// `[` can only mean an [attribute] before a function
|
|
|
|
// or a struct definition
|
2019-08-17 21:19:37 +02:00
|
|
|
p.attribute()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_struct, Token.key_interface, Token.key_union, Token.lsbr:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.struct_decl()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_const:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.const_decl()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.hash:
|
2019-06-22 20:20:28 +02:00
|
|
|
// insert C code, TODO this is going to be removed ASAP
|
|
|
|
// some libraries (like UI) still have lots of C code
|
|
|
|
// # puts("hello");
|
|
|
|
p.chash()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.dollar:
|
2019-06-22 20:20:28 +02:00
|
|
|
// $if, $else
|
|
|
|
p.comp_time()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_global:
|
2019-08-17 21:19:37 +02:00
|
|
|
if !p.pref.translated && !p.pref.is_live &&
|
2019-08-19 23:08:45 +02:00
|
|
|
!p.builtin_mod && !p.building_v && !os.getwd().contains('/volt') {
|
2019-07-21 12:43:47 +02:00
|
|
|
p.error('__global is only allowed in translated code')
|
2019-06-22 20:20:28 +02: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 22:30:15 +02:00
|
|
|
if p.tok == .assign {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
// p.gen(' = ')
|
|
|
|
g += ' = '
|
|
|
|
p.cgen.start_tmp()
|
|
|
|
p.bool_expression()
|
|
|
|
// g += '<<< ' + p.cgen.end_tmp() + '>>>'
|
|
|
|
g += p.cgen.end_tmp()
|
|
|
|
}
|
|
|
|
// p.genln('; // global')
|
2019-08-17 21:19:37 +02:00
|
|
|
g += '; // global'
|
2019-06-22 20:20:28 +02:00
|
|
|
p.cgen.consts << g
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.eof:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.log('end of parse()')
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.is_script && !p.pref.is_test {
|
2019-06-28 20:50:29 +02:00
|
|
|
p.cur_fn = MainFn
|
|
|
|
p.check_unused_variables()
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
if false && !p.first_pass() && p.fileis('main.v') {
|
2019-07-03 21:07:42 +02:00
|
|
|
out := os.create('/var/tmp/fmt.v') or {
|
2019-08-17 21:19:37 +02:00
|
|
|
panic('failed to create fmt.v')
|
|
|
|
}
|
2019-06-30 16:11:55 +02:00
|
|
|
out.writeln(p.scanner.fmt_out.str())
|
2019-06-22 20:20:28 +02:00
|
|
|
out.close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// no `fn main`, add this "global" statement to cgen.fn_main
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.is_script && !p.pref.is_test {
|
2019-06-28 12:41:09 +02:00
|
|
|
// cur_fn is empty since there was no fn main declared
|
2019-08-17 21:19:37 +02:00
|
|
|
// we need to set it to save and find variables
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.first_pass() {
|
2019-06-28 12:41:09 +02:00
|
|
|
if p.cur_fn.name == '' {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.cur_fn = MainFn
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-06-28 12:41:09 +02:00
|
|
|
if p.cur_fn.name == '' {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.cur_fn = MainFn
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.pref.is_repl {
|
2019-06-28 17:15:52 +02:00
|
|
|
p.cur_fn.clear_vars()
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-07-01 19:55:47 +02:00
|
|
|
mut start := p.cgen.lines.len
|
2019-06-22 20:20:28 +02:00
|
|
|
p.statement(true)
|
2019-07-01 19:55:47 +02:00
|
|
|
if p.cgen.lines[start - 1] != '' && p.cgen.fn_main != '' {
|
|
|
|
start--
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
p.genln('')
|
2019-06-22 20:20:28 +02:00
|
|
|
end := p.cgen.lines.len
|
|
|
|
lines := p.cgen.lines.slice(start, end)
|
2019-08-17 21:19:37 +02:00
|
|
|
//mut line := p.cgen.fn_main + lines.join('\n')
|
|
|
|
//line = line.trim_space()
|
2019-06-22 20:20:28 +02:00
|
|
|
p.cgen.fn_main = p.cgen.fn_main + lines.join('\n')
|
2019-07-18 20:21:23 +02:00
|
|
|
p.cgen.resetln('')
|
2019-06-22 20:20:28 +02:00
|
|
|
for i := start; i < end; i++ {
|
|
|
|
p.cgen.lines[i] = ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('unexpected token `${p.strtok()}`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 17:59:07 +02:00
|
|
|
fn (p mut Parser) imports() {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.key_import)
|
2019-06-22 20:20:28 +02:00
|
|
|
// `import ()`
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .lpar {
|
|
|
|
p.check(.lpar)
|
|
|
|
for p.tok != .rpar && p.tok != .eof {
|
2019-07-30 03:49:47 +02:00
|
|
|
p.import_statement()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rpar)
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// `import foo`
|
2019-07-30 03:49:47 +02:00
|
|
|
p.import_statement()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) import_statement() {
|
|
|
|
if p.tok != .name {
|
|
|
|
p.error('bad import format')
|
|
|
|
}
|
|
|
|
if p.peek() == .number && p.scanner.text[p.scanner.pos + 1] == `.` {
|
|
|
|
p.error('bad import format. module/submodule names cannot begin with a number.')
|
|
|
|
}
|
2019-08-05 09:49:52 +02:00
|
|
|
mut mod := p.check_name().trim_space()
|
|
|
|
mut mod_alias := mod
|
2019-07-30 03:49:47 +02:00
|
|
|
// submodule support
|
|
|
|
mut depth := 1
|
|
|
|
for p.tok == .dot {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.dot)
|
2019-07-30 03:49:47 +02:00
|
|
|
submodule := p.check_name()
|
|
|
|
mod_alias = submodule
|
2019-08-05 09:49:52 +02:00
|
|
|
mod += '.' + submodule
|
2019-07-30 03:49:47 +02:00
|
|
|
depth++
|
2019-08-17 21:19:37 +02:00
|
|
|
if depth > MaxModuleDepth {
|
|
|
|
p.error('module depth of $MaxModuleDepth exceeded: $mod')
|
2019-07-30 03:49:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// aliasing (import encoding.base64 as b64)
|
|
|
|
if p.tok == .key_as && p.peek() == .name {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.key_as)
|
2019-07-30 03:49:47 +02:00
|
|
|
mod_alias = p.check_name()
|
|
|
|
}
|
|
|
|
// add import to file scope import table
|
2019-08-05 09:49:52 +02:00
|
|
|
p.import_table.register_alias(mod_alias, mod)
|
2019-07-30 03:49:47 +02:00
|
|
|
// Make sure there are no duplicate imports
|
2019-08-05 09:49:52 +02:00
|
|
|
if p.table.imports.contains(mod) {
|
2019-07-30 03:49:47 +02:00
|
|
|
return
|
|
|
|
}
|
2019-08-05 09:49:52 +02:00
|
|
|
p.log('adding import $mod')
|
|
|
|
p.table.imports << mod
|
|
|
|
p.table.register_module(mod)
|
2019-07-30 03:49:47 +02:00
|
|
|
|
2019-08-05 09:49:52 +02:00
|
|
|
p.fgenln(' ' + mod)
|
2019-07-23 23:12:55 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
fn (p mut Parser) const_decl() {
|
2019-08-22 22:19:31 +02:00
|
|
|
if p.tok == .key_import {
|
|
|
|
p.error('`import const` was removed from the language, ' +
|
|
|
|
'use `foo(C.CONST_NAME)` instead')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-22 22:19:31 +02:00
|
|
|
p.inside_const = true
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.key_const)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fspace()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lpar)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgenln('')
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fmt_inc()
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok == .name {
|
2019-06-22 20:20:28 +02:00
|
|
|
// `Age = 20`
|
|
|
|
mut name := p.check_name()
|
2019-07-29 18:21:36 +02:00
|
|
|
//if ! (name[0] >= `A` && name[0] <= `Z`) {
|
|
|
|
//p.error('const name must be capitalized')
|
|
|
|
//}
|
2019-08-22 22:19:31 +02:00
|
|
|
name = p.prepend_mod(name)
|
|
|
|
p.check_space(.assign)
|
|
|
|
typ := p.expression()
|
|
|
|
if p.first_pass() && p.table.known_const(name) {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('redefinition of `$name`')
|
|
|
|
}
|
2019-08-22 22:19:31 +02:00
|
|
|
p.table.register_const(name, typ, p.mod)
|
|
|
|
if p.pass == .main {
|
2019-06-22 20:20:28 +02: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
|
|
|
|
if is_compile_time_const(p.cgen.cur_line) {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.cgen.consts << '#define $name $p.cgen.cur_line'
|
2019-07-18 20:21:23 +02:00
|
|
|
p.cgen.resetln('')
|
2019-06-22 20:20:28 +02: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 20:21:23 +02:00
|
|
|
p.cgen.resetln('')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.fgenln('')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fmt_dec()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rpar)
|
2019-06-22 20:20:28 +02: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 12:17:17 +02:00
|
|
|
p.check(.key_type)
|
2019-06-22 20:20:28 +02:00
|
|
|
name := p.check_name()
|
2019-08-10 13:08:59 +02: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-06-22 20:20:28 +02:00
|
|
|
parent := p.get_type()
|
|
|
|
nt_pair := p.table.cgen_name_type_pair(name, parent)
|
|
|
|
// 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"
|
|
|
|
_struct := if !parent.contains('[') && !parent.starts_with('fn ') && !p.table.known_type(parent){'struct'} else { ''}
|
|
|
|
p.gen_typedef('typedef $_struct $nt_pair; // type alias name="$name" parent="$parent"')
|
2019-07-10 14:18:21 +02:00
|
|
|
p.register_type_with_parent(name, parent)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 21:53:25 +02: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-07-29 18:21:36 +02:00
|
|
|
p.log('is interface. field=$field_name run=$p.pass')
|
2019-07-03 21:53:25 +02:00
|
|
|
p.fn_args(mut method)
|
|
|
|
if p.scanner.has_gone_over_line_end() {
|
|
|
|
method.typ = 'void'
|
|
|
|
} else {
|
|
|
|
method.typ = p.get_type()// method return type
|
|
|
|
p.fspace()
|
|
|
|
p.fgenln('')
|
|
|
|
}
|
|
|
|
return method
|
|
|
|
}
|
|
|
|
|
2019-08-20 13:34:29 +02:00
|
|
|
fn key_to_type_cat(tok Token) TypeCategory {
|
|
|
|
switch tok {
|
|
|
|
case Token.key_interface: return TypeCategory.interface_
|
|
|
|
case Token.key_struct: return TypeCategory.struct_
|
|
|
|
case Token.key_union: return TypeCategory.union_
|
|
|
|
//Token.key_ => return .interface_
|
|
|
|
}
|
|
|
|
panic('')
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
// also unions and interfaces
|
|
|
|
fn (p mut Parser) struct_decl() {
|
2019-08-20 13:34:29 +02:00
|
|
|
// V can generate Objective C for integration with Cocoa
|
|
|
|
// `[interface:ParentInterface]`
|
|
|
|
is_objc := p.attr.starts_with('interface')
|
|
|
|
objc_parent := if is_objc { p.attr.right(10) } else { '' }
|
|
|
|
// interface, union, struct
|
2019-08-17 21:19:37 +02:00
|
|
|
is_interface := p.tok == .key_interface
|
2019-07-07 22:30:15 +02:00
|
|
|
is_union := p.tok == .key_union
|
|
|
|
is_struct := p.tok == .key_struct
|
2019-08-20 13:34:29 +02:00
|
|
|
mut cat := key_to_type_cat(p.tok)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(p.tok.str() + ' ')
|
|
|
|
// Get type name
|
|
|
|
p.next()
|
|
|
|
mut name := p.check_name()
|
2019-06-30 22:03:17 +02:00
|
|
|
if name.contains('_') && !p.pref.translated {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('type names cannot contain `_`')
|
|
|
|
}
|
|
|
|
if is_interface && !name.ends_with('er') {
|
|
|
|
p.error('interface names temporarily have to end with `er` (e.g. `Speaker`, `Reader`)')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
is_c := name == 'C' && p.tok == .dot
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_c {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.dot)
|
2019-06-22 20:20:28 +02:00
|
|
|
name = p.check_name()
|
2019-08-20 13:34:29 +02:00
|
|
|
cat = .c_struct
|
|
|
|
if p.attr == 'typedef' {
|
|
|
|
cat = .c_typedef
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-04 09:36:21 +02:00
|
|
|
if !is_c && !good_type_name(name) {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error('bad struct name, e.g. use `HttpRequest` instead of `HTTPRequest`')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// Specify full type name
|
2019-08-05 09:49:52 +02:00
|
|
|
if !is_c && !p.builtin_mod && p.mod != 'main' {
|
|
|
|
name = p.prepend_mod(name)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.pass == .decl && p.table.known_type(name) {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('`$name` redeclared')
|
|
|
|
}
|
|
|
|
// Generate type definitions
|
|
|
|
if is_objc {
|
|
|
|
p.gen_type('@interface $name : $objc_parent { @public')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// type alias is generated later
|
|
|
|
if !is_c {
|
2019-07-16 17:59:07 +02:00
|
|
|
kind := if is_union {'union'} else {'struct'}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen_typedef('typedef $kind $name $name;')
|
2019-07-10 13:27:35 +02:00
|
|
|
p.gen_type('$kind $name {')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Register the type
|
|
|
|
mut typ := p.table.find_type(name)
|
|
|
|
mut is_ph := false
|
|
|
|
if typ.is_placeholder {
|
|
|
|
is_ph = true
|
|
|
|
typ.name = name
|
2019-07-07 22:30:15 +02:00
|
|
|
typ.mod = p.mod
|
2019-06-22 20:20:28 +02:00
|
|
|
typ.is_c = is_c
|
|
|
|
typ.is_placeholder = false
|
2019-08-20 13:34:29 +02:00
|
|
|
typ.cat = cat
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
typ = &Type {
|
|
|
|
name: name
|
2019-07-07 22:30:15 +02:00
|
|
|
mod: p.mod
|
2019-06-22 20:20:28 +02:00
|
|
|
is_c: is_c
|
2019-08-20 13:34:29 +02:00
|
|
|
cat: cat
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Struct `C.Foo` declaration, no body
|
2019-07-07 22:30:15 +02:00
|
|
|
if is_c && is_struct && p.tok != .lcbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.table.register_type2(typ)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.fgen(' ')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
// Struct fields
|
|
|
|
mut is_pub := false
|
|
|
|
mut is_mut := false
|
|
|
|
mut names := []string// to avoid dup names TODO alloc perf
|
2019-08-17 21:19:37 +02:00
|
|
|
/*
|
|
|
|
mut fmt_max_len := 0
|
2019-07-16 17:59:07 +02:00
|
|
|
for field in typ.fields {
|
|
|
|
if field.name.len > max_len {
|
2019-08-17 21:19:37 +02:00
|
|
|
fmt_max_len = field.name.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println('fmt max len = $max_len nrfields=$typ.fields.len pass=$p.pass')
|
|
|
|
*/
|
2019-07-23 23:23:13 +02:00
|
|
|
mut did_gen_something := false
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok != .rcbr {
|
|
|
|
if p.tok == .key_pub {
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_pub {
|
|
|
|
p.error('structs can only have one `pub:`, all public fields have to be grouped')
|
|
|
|
}
|
|
|
|
is_pub = true
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fmt_dec()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.key_pub)
|
|
|
|
if p.tok != .key_mut {
|
|
|
|
p.check(.colon)
|
2019-06-24 23:34:39 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fmt_inc()
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgenln('')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .key_mut {
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_mut {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.error('structs can only have one `mut:`, all private key_mut fields have to be grouped')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
is_mut = true
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fmt_dec()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.key_mut)
|
|
|
|
if p.tok != .key_mut {
|
|
|
|
p.check(.colon)
|
2019-06-24 23:34:39 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fmt_inc()
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgenln('')
|
|
|
|
}
|
|
|
|
// if is_pub {
|
|
|
|
// }
|
|
|
|
// (mut) user *User
|
2019-07-07 22:30:15 +02:00
|
|
|
// if p.tok == .plus {
|
2019-06-22 20:20:28 +02:00
|
|
|
// p.next()
|
|
|
|
// }
|
|
|
|
// Check dups
|
|
|
|
field_name := p.check_name()
|
|
|
|
if field_name in names {
|
|
|
|
p.error('duplicate field `$field_name`')
|
|
|
|
}
|
2019-08-04 09:36:21 +02:00
|
|
|
if !is_c && p.mod != 'os' && contains_capital(field_name) {
|
2019-08-04 09:16:44 +02:00
|
|
|
p.error('struct fields cannot contain uppercase letters, use snake_case instead')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
names << field_name
|
|
|
|
// We are in an interface?
|
|
|
|
// `run() string` => run is a method, not a struct field
|
|
|
|
if is_interface {
|
2019-07-03 21:53:25 +02:00
|
|
|
typ.add_method(p.interface_method(field_name, name))
|
2019-06-22 20:20:28 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// `pub` access mod
|
2019-07-07 22:30:15 +02:00
|
|
|
access_mod := if is_pub{AccessMod.public} else { AccessMod.private}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(' ')
|
|
|
|
field_type := p.get_type()
|
2019-07-07 22:30:15 +02:00
|
|
|
is_atomic := p.tok == .key_atomic
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_atomic {
|
|
|
|
p.next()
|
|
|
|
p.gen_type('_Atomic ')
|
|
|
|
}
|
|
|
|
if !is_c {
|
|
|
|
p.gen_type(p.table.cgen_name_type_pair(field_name, field_type) + ';')
|
|
|
|
}
|
|
|
|
// [ATTR]
|
|
|
|
mut attr := ''
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .lsbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
attr = p.check_name()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rsbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-14 16:38:28 +02:00
|
|
|
if attr == 'raw' && field_type != 'string' {
|
|
|
|
p.error('struct field with attribute "raw" should be of type "string" but got "$field_type"')
|
|
|
|
}
|
2019-07-23 23:23:13 +02:00
|
|
|
did_gen_something = true
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
typ.add_field(field_name, field_type, is_mut, attr, access_mod)
|
|
|
|
p.fgenln('')
|
|
|
|
}
|
2019-07-23 23:23:13 +02:00
|
|
|
|
2019-07-29 18:21:36 +02:00
|
|
|
if !is_ph && p.first_pass() {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.table.register_type2(typ)
|
2019-08-17 21:19:37 +02:00
|
|
|
//println('registering 1 nrfields=$typ.fields.len')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-23 23:23:13 +02:00
|
|
|
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
if !is_c {
|
2019-07-24 17:46:41 +02:00
|
|
|
if !did_gen_something {
|
|
|
|
p.gen_type('EMPTY_STRUCT_DECLARATION };')
|
2019-07-23 23:23:13 +02:00
|
|
|
p.fgenln('')
|
|
|
|
} else {
|
|
|
|
p.gen_type('}; ')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
if is_objc {
|
|
|
|
p.gen_type('@end')
|
|
|
|
}
|
|
|
|
p.fgenln('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) enum_decl(_enum_name string) {
|
|
|
|
mut enum_name := _enum_name
|
|
|
|
// Specify full type name
|
2019-08-05 09:49:52 +02:00
|
|
|
if !p.builtin_mod && p.mod != 'main' {
|
|
|
|
enum_name = p.prepend_mod(enum_name)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Skip empty enums
|
|
|
|
if enum_name != 'int' {
|
2019-07-10 13:27:35 +02:00
|
|
|
p.cgen.typedefs << 'typedef int $enum_name;'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
mut val := 0
|
2019-08-17 21:19:37 +02:00
|
|
|
mut fields := []string
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok == .name {
|
2019-06-22 20:20:28 +02:00
|
|
|
field := p.check_name()
|
2019-08-17 21:19:37 +02:00
|
|
|
fields << field
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgenln('')
|
2019-07-16 17:59:07 +02:00
|
|
|
name := '${p.mod}__${enum_name}_$field'
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.pass == .main {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.cgen.consts << '#define $name $val'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .comma {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
}
|
2019-08-22 22:19:31 +02:00
|
|
|
p.table.register_const(name, enum_name, p.mod)
|
2019-06-22 20:20:28 +02:00
|
|
|
val++
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.table.register_type2(&Type {
|
|
|
|
name: enum_name
|
|
|
|
mod: p.mod
|
|
|
|
parent: 'int'
|
2019-08-20 13:34:29 +02:00
|
|
|
cat: TypeCategory.enum_
|
2019-08-17 21:19:37 +02:00
|
|
|
enum_vals: fields
|
2019-07-07 22:30:15 +02:00
|
|
|
})
|
|
|
|
p.check(.rcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgenln('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
// check_name checks for a name token and returns its literal
|
|
|
|
fn (p mut Parser) check_name() string {
|
|
|
|
name := p.lit
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.name)
|
2019-06-22 20:20:28 +02:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) check_string() string {
|
|
|
|
s := p.lit
|
2019-07-16 17:59:07 +02:00
|
|
|
p.check(.str)
|
2019-06-22 20:20:28 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p &Parser) strtok() string {
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .name {
|
2019-06-22 20:20:28 +02:00
|
|
|
return p.lit
|
|
|
|
}
|
2019-07-16 17:59:07 +02:00
|
|
|
if p.tok == .str {
|
2019-06-22 20:20:28 +02:00
|
|
|
return '"$p.lit"'
|
|
|
|
}
|
|
|
|
res := p.tok.str()
|
|
|
|
if res == '' {
|
|
|
|
n := int(p.tok)
|
|
|
|
return n.str()
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-07-14 16:30:04 +02:00
|
|
|
// same as check(), but adds a space to the formatter output
|
2019-06-22 20:20:28 +02:00
|
|
|
// TODO bad name
|
|
|
|
fn (p mut Parser) check_space(expected Token) {
|
|
|
|
p.fspace()
|
|
|
|
p.check(expected)
|
|
|
|
p.fspace()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) check(expected Token) {
|
|
|
|
if p.tok != expected {
|
|
|
|
println('check()')
|
2019-07-25 13:16:17 +02:00
|
|
|
s := 'expected `${expected.str()}` but got `${p.strtok()}`'
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
println('next token = `${p.strtok()}`')
|
|
|
|
print_backtrace()
|
|
|
|
p.error(s)
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if expected == .rcbr {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fmt_dec()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.fgen(p.strtok())
|
|
|
|
// vfmt: increase indentation on `{` unless it's `{}`
|
2019-08-09 20:19:37 +02:00
|
|
|
if expected == .lcbr && p.scanner.pos + 1 < p.scanner.text.len && p.scanner.text[p.scanner.pos + 1] != `}` {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgenln('')
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fmt_inc()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.next()
|
2019-07-16 17:59:07 +02:00
|
|
|
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.scanner.line_comment != '' {
|
2019-07-16 17:59:07 +02:00
|
|
|
//p.fgenln('// ! "$p.scanner.line_comment"')
|
2019-08-17 21:19:37 +02:00
|
|
|
//p.scanner.line_comment = ''
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) error(s string) {
|
|
|
|
// Dump all vars and types for debugging
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.pref.is_debug {
|
2019-06-22 20:20:28 +02:00
|
|
|
// os.write_to_file('/var/tmp/lang.types', '')//pes(p.table.types))
|
|
|
|
// os.write_to_file('/var/tmp/lang.vars', q.J(p.table.vars))
|
2019-08-17 21:19:37 +02:00
|
|
|
os.write_file('fns.txt', p.table.debug_fns())
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.pref.is_verbose || p.pref.is_debug {
|
2019-08-08 06:55:08 +02:00
|
|
|
println('pass=$p.pass fn=`$p.cur_fn.name`\n')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.cgen.save()
|
2019-06-26 01:03:41 +02:00
|
|
|
// V git pull hint
|
|
|
|
cur_path := os.getwd()
|
2019-08-10 23:02:48 +02:00
|
|
|
if !p.pref.is_repl && !p.pref.is_test && ( p.file_path.contains('v/compiler') || cur_path.contains('v/compiler') ){
|
2019-06-26 14:53:55 +02:00
|
|
|
println('\n=========================')
|
2019-08-17 21:19:37 +02:00
|
|
|
println('It looks like you are building V. It is being frequently updated every day.')
|
2019-06-26 14:53:55 +02:00
|
|
|
println('If you didn\'t modify the compiler\'s code, most likely there was a change that ')
|
|
|
|
println('lead to this error.')
|
2019-06-28 12:41:09 +02:00
|
|
|
println('\nRun `git pull && make`, that will most likely fix it.')
|
|
|
|
//println('\nIf this doesn\'t help, re-install V from source or download a precompiled' + ' binary from\nhttps://vlang.io.')
|
|
|
|
println('\nIf this doesn\'t help, please create a GitHub issue.')
|
2019-06-26 14:53:55 +02:00
|
|
|
println('=========================\n')
|
2019-06-26 01:03:41 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// p.scanner.debug_tokens()
|
|
|
|
// Print `[]int` instead of `array_int` in errors
|
2019-08-17 21:19:37 +02:00
|
|
|
p.scanner.error(s.replace('array_', '[]').replace('__', '.').replace('Option_', '?'))
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 18:21:36 +02:00
|
|
|
fn (p &Parser) first_pass() bool {
|
|
|
|
return p.pass == .decl
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO return Type instead of string?
|
|
|
|
fn (p mut Parser) get_type() string {
|
|
|
|
debug := p.fileis('fn_test') && false
|
|
|
|
mut mul := false
|
|
|
|
mut nr_muls := 0
|
2019-06-28 14:19:46 +02:00
|
|
|
mut typ := ''
|
2019-06-22 20:20:28 +02:00
|
|
|
// fn type
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .func {
|
2019-06-22 20:20:28 +02:00
|
|
|
if debug {
|
2019-07-07 22:30:15 +02:00
|
|
|
println('\nget_type() .key_goT FN TYP line=$p.scanner.line_nr')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-05 09:49:52 +02:00
|
|
|
mut f := Fn{name: '_', mod: p.mod}
|
2019-06-22 20:20:28 +02: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 {
|
|
|
|
if debug {
|
|
|
|
println('same line getting type')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.tok == .name {
|
2019-07-16 14:31:08 +02:00
|
|
|
f.typ = p.get_type()
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-07-16 14:31:08 +02:00
|
|
|
f.typ = 'void'
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02: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 22:30:15 +02:00
|
|
|
mod: p.mod
|
2019-06-22 20:20:28 +02:00
|
|
|
func: f
|
|
|
|
}
|
|
|
|
p.table.register_type2(fn_typ)
|
|
|
|
return f.typ_str()
|
|
|
|
}
|
|
|
|
// arrays ([]int)
|
|
|
|
mut is_arr := false
|
|
|
|
mut is_arr2 := false// [][]int TODO remove this and allow unlimited levels of arrays
|
2019-07-07 22:30:15 +02:00
|
|
|
is_question := p.tok == .question
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_question {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.question)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .lsbr {
|
|
|
|
p.check(.lsbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
// [10]int
|
2019-07-24 00:06:48 +02:00
|
|
|
if p.tok == .number {
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = '[$p.lit]'
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
is_arr = true
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rsbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
// [10][3]int
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .lsbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
2019-07-24 00:06:48 +02:00
|
|
|
if p.tok == .number {
|
2019-06-22 20:20:28 +02:00
|
|
|
typ += '[$p.lit]'
|
2019-07-24 00:06:48 +02:00
|
|
|
p.check(.number)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
is_arr2 = true
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rsbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
// map[string]int
|
2019-08-05 09:49:52 +02:00
|
|
|
if !p.builtin_mod && p.tok == .name && p.lit == 'map' {
|
2019-06-30 22:44:15 +02:00
|
|
|
p.next()
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.lsbr)
|
|
|
|
key_type := p.check_name()
|
2019-06-30 22:44:15 +02:00
|
|
|
if key_type != 'string' {
|
2019-08-17 21:19:37 +02: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 22:44:15 +02:00
|
|
|
p.register_map(typ)
|
2019-08-17 21:19:37 +02:00
|
|
|
return typ
|
|
|
|
}
|
|
|
|
//
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok == .mul {
|
2019-06-22 20:20:28 +02:00
|
|
|
mul = true
|
|
|
|
nr_muls++
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.mul)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .amp {
|
2019-06-22 20:20:28 +02:00
|
|
|
mul = true
|
|
|
|
nr_muls++
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.amp)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
typ += p.lit
|
|
|
|
if !p.is_struct_init {
|
2019-08-20 13:34:29 +02:00
|
|
|
// Otherwise we get `foo := FooFoo{` because `Foo` was already
|
|
|
|
// generated in name_expr()
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(p.lit)
|
|
|
|
}
|
|
|
|
// C.Struct import
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.lit == 'C' && p.peek() == .dot {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.dot)
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = p.lit
|
|
|
|
}
|
|
|
|
else {
|
2019-08-05 09:49:52 +02:00
|
|
|
// Module specified? (e.g. gx.Image)
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.peek() == .dot {
|
2019-08-08 14:35:53 +02: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('.') {
|
|
|
|
typ = mod.replace('.', '_dot_')
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.dot)
|
2019-06-22 20:20:28 +02:00
|
|
|
typ += '__$p.lit'
|
|
|
|
}
|
|
|
|
mut t := p.table.find_type(typ)
|
2019-08-05 09:49:52 +02:00
|
|
|
// "typ" not found? try "mod__typ"
|
|
|
|
if t.name == '' && !p.builtin_mod {
|
2019-07-29 18:21:36 +02:00
|
|
|
// && !p.first_pass() {
|
2019-07-07 22:30:15 +02:00
|
|
|
if !typ.contains('array_') && p.mod != 'main' && !typ.contains('__') &&
|
2019-08-17 21:19:37 +02:00
|
|
|
!typ.starts_with('[') {
|
2019-08-05 09:49:52 +02:00
|
|
|
typ = p.prepend_mod(typ)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
t = p.table.find_type(typ)
|
2019-07-29 18:21:36 +02:00
|
|
|
if t.name == '' && !p.pref.translated && !p.first_pass() && !typ.starts_with('[') {
|
2019-06-22 20:20:28 +02:00
|
|
|
println('get_type() bad type')
|
|
|
|
// println('all registered types:')
|
|
|
|
// for q in p.table.types {
|
|
|
|
// println(q.name)
|
|
|
|
// }
|
|
|
|
p.error('unknown type `$typ`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if typ == 'void' {
|
|
|
|
p.error('unknown type `$typ`')
|
|
|
|
}
|
|
|
|
if mul {
|
2019-07-03 22:11:27 +02:00
|
|
|
typ += strings.repeat(`*`, nr_muls)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Register an []array type
|
|
|
|
if is_arr2 {
|
|
|
|
typ = 'array_array_$typ'
|
|
|
|
p.register_array(typ)
|
|
|
|
}
|
|
|
|
else if is_arr {
|
|
|
|
typ = 'array_$typ'
|
2019-07-29 18:21:36 +02:00
|
|
|
// p.log('ARR TYPE="$typ" run=$p.pass')
|
2019-06-22 20:20:28 +02:00
|
|
|
// We come across "[]User" etc ?
|
|
|
|
p.register_array(typ)
|
|
|
|
}
|
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .question || is_question {
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = 'Option_$typ'
|
|
|
|
p.table.register_type_with_parent(typ, 'Option')
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .question {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Because the code uses * to see if it's a pointer
|
|
|
|
if typ == 'byteptr' {
|
|
|
|
return 'byte*'
|
|
|
|
}
|
|
|
|
if typ == 'voidptr' {
|
2019-08-05 09:49:52 +02:00
|
|
|
//if !p.builtin_mod && p.mod != 'os' && p.mod != 'gx' && p.mod != 'gg' && !p.pref.translated {
|
2019-06-22 20:20:28 +02: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 22:30:15 +02:00
|
|
|
if p.tok == .name {
|
2019-06-22 20:20:28 +02:00
|
|
|
println(p.lit)
|
|
|
|
return
|
|
|
|
}
|
2019-07-16 17:59:07 +02:00
|
|
|
if p.tok == .str {
|
2019-06-22 20:20:28 +02:00
|
|
|
println('"$p.lit"')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println(p.tok.str())
|
|
|
|
}
|
|
|
|
|
|
|
|
// statements() returns the type of the last statement
|
|
|
|
fn (p mut Parser) statements() string {
|
|
|
|
p.log('statements()')
|
2019-07-24 02:35:25 +02:00
|
|
|
typ := p.statements_no_rcbr()
|
2019-06-22 20:20:28 +02:00
|
|
|
if !p.inside_if_expr {
|
|
|
|
p.genln('}')
|
|
|
|
}
|
2019-07-24 02:35:25 +02:00
|
|
|
//if p.fileis('if_expr') {
|
|
|
|
//println('statements() ret=$typ line=$p.scanner.line_nr')
|
|
|
|
//}
|
2019-06-22 20:20:28 +02:00
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2019-07-24 02:35:25 +02:00
|
|
|
fn (p mut Parser) statements_no_rcbr() string {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.cur_fn.open_scope()
|
|
|
|
if !p.inside_if_expr {
|
|
|
|
p.genln('')
|
|
|
|
}
|
|
|
|
mut i := 0
|
|
|
|
mut last_st_typ := ''
|
2019-08-17 21:19:37 +02:00
|
|
|
for p.tok != .rcbr && p.tok != .eof && p.tok != .key_case &&
|
2019-07-17 01:43:59 +02:00
|
|
|
p.tok != .key_default && p.peek() != .arrow {
|
2019-06-22 20:20:28 +02:00
|
|
|
// println(p.tok.str())
|
|
|
|
// p.print_tok()
|
|
|
|
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`')
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 01:43:59 +02:00
|
|
|
if p.tok != .key_case && p.tok != .key_default && p.peek() != .arrow {
|
2019-06-22 20:20:28 +02:00
|
|
|
// p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-07-07 22:30:15 +02:00
|
|
|
// p.check(.rcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
//p.fmt_dec()
|
2019-06-22 20:20:28 +02:00
|
|
|
// println('close scope line=$p.scanner.line_nr')
|
2019-07-21 12:43:47 +02:00
|
|
|
p.close_scope()
|
2019-06-22 20:20:28 +02:00
|
|
|
return last_st_typ
|
|
|
|
}
|
|
|
|
|
2019-07-21 12:43:47 +02:00
|
|
|
fn (p mut Parser) close_scope() {
|
|
|
|
// println('close_scope level=$f.scope_level var_idx=$f.var_idx')
|
|
|
|
// Move back `var_idx` (pointer to the end of the array) till we reach the previous scope level.
|
|
|
|
// This effectivly deletes (closes) current scope.
|
|
|
|
mut i := p.cur_fn.var_idx - 1
|
2019-08-02 05:20:18 +02:00
|
|
|
for ; i >= 0; i-- {
|
2019-07-21 12:43:47 +02:00
|
|
|
v := p.cur_fn.local_vars[i]
|
|
|
|
if v.scope_level != p.cur_fn.scope_level {
|
|
|
|
// println('breaking. "$v.name" v.scope_level=$v.scope_level')
|
|
|
|
break
|
|
|
|
}
|
2019-08-05 10:42:58 +02:00
|
|
|
if false && !p.building_v && !v.is_mut && v.is_alloc {
|
2019-08-17 21:19:37 +02:00
|
|
|
if v.typ.starts_with('array_') {
|
|
|
|
p.genln('v_array_free($v.name); // close_scope free')
|
|
|
|
}
|
|
|
|
else if v.typ == 'string' {
|
|
|
|
p.genln('v_string_free($v.name); // close_scope free')
|
|
|
|
}
|
2019-07-31 19:21:49 +02:00
|
|
|
else if v.ptr {
|
2019-08-17 21:19:37 +02:00
|
|
|
//p.genln('free($v.name); // close_scope free')
|
|
|
|
}
|
2019-08-07 17:51:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-21 15:05:47 +02:00
|
|
|
|
2019-08-07 17:51:21 +02: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 12:43:47 +02:00
|
|
|
}
|
2019-08-07 17:51:21 +02:00
|
|
|
|
|
|
|
p.cur_fn.close_scope()
|
2019-07-21 12:43:47 +02:00
|
|
|
p.cur_fn.var_idx = i + 1
|
|
|
|
// println('close_scope new var_idx=$f.var_idx\n')
|
2019-08-07 17:51:21 +02:00
|
|
|
}
|
2019-07-21 12:43:47 +02:00
|
|
|
|
2019-06-22 20:20:28 +02: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) vh_genln(s string) {
|
|
|
|
p.vh_lines << s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) statement(add_semi bool) string {
|
2019-08-12 16:21:56 +02:00
|
|
|
if p.returns && !p.is_vweb {
|
2019-08-10 10:26:42 +02:00
|
|
|
p.error('unreachable code')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.cgen.is_tmp = false
|
|
|
|
tok := p.tok
|
|
|
|
mut q := ''
|
|
|
|
switch tok {
|
2019-07-07 22:30:15 +02:00
|
|
|
case .name:
|
2019-06-22 20:20:28 +02:00
|
|
|
next := p.peek()
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.pref.is_verbose {
|
2019-06-22 20:20:28 +02:00
|
|
|
println(next.str())
|
|
|
|
}
|
|
|
|
// goto_label:
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.peek() == .colon {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fmt_dec()
|
|
|
|
label := p.check_name()
|
|
|
|
p.fmt_inc()
|
|
|
|
p.genln(label + ':')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.colon)
|
2019-06-22 20:20:28 +02:00
|
|
|
return ''
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
// `a := 777`
|
2019-07-07 22:30:15 +02:00
|
|
|
else if p.peek() == .decl_assign {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.var_decl()
|
|
|
|
}
|
|
|
|
else {
|
2019-08-09 08:17:31 +02: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 13:34:29 +02:00
|
|
|
// `a + 3`, `a(7)`, or just `a`
|
2019-06-22 20:20:28 +02:00
|
|
|
q = p.bool_expression()
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_goto:
|
|
|
|
p.check(.key_goto)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(' ')
|
|
|
|
label := p.check_name()
|
|
|
|
p.genln('goto $label;')
|
|
|
|
return ''
|
2019-07-15 22:09:34 +02:00
|
|
|
case Token.key_defer:
|
|
|
|
p.defer_st()
|
|
|
|
return ''
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.hash:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.chash()
|
|
|
|
return ''
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.dollar:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.comp_time()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_if:
|
2019-07-12 06:54:01 +02:00
|
|
|
p.if_st(false, 0)
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_for:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.for_st()
|
2019-08-17 21:19:37 +02:00
|
|
|
case Token.key_switch, Token.key_match:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.switch_statement()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_mut, Token.key_static:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.var_decl()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_return:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.return_st()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.lcbr:// {} block
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.lcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.genln('{')
|
|
|
|
p.statements()
|
|
|
|
return ''
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_continue:
|
2019-06-22 20:20:28 +02:00
|
|
|
if p.for_expr_cnt == 0 {
|
|
|
|
p.error('`continue` statement outside `for`')
|
|
|
|
}
|
|
|
|
p.genln('continue')
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.key_continue)
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_break:
|
2019-06-22 20:20:28 +02:00
|
|
|
if p.for_expr_cnt == 0 {
|
|
|
|
p.error('`break` statement outside `for`')
|
|
|
|
}
|
|
|
|
p.genln('break')
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.key_break)
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_go:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.go_statement()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_assert:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.assert_statement()
|
|
|
|
default:
|
|
|
|
// An expression as a statement
|
|
|
|
typ := p.expression()
|
|
|
|
if p.inside_if_expr {
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln('; ')
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
// ? : uses , as statement separators
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.inside_if_expr && p.tok != .rcbr {
|
2019-06-22 20:20:28 +02: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) {
|
|
|
|
p.log('assign_statement() name=$v.name tok=')
|
2019-08-17 21:19:37 +02:00
|
|
|
is_vid := p.fileis('vid') // TODO remove
|
2019-06-22 20:20:28 +02:00
|
|
|
tok := p.tok
|
2019-08-07 08:19:27 +02:00
|
|
|
//if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{
|
2019-08-07 13:00:19 +02:00
|
|
|
if !v.is_mut && !p.pref.translated && !v.is_global && !is_vid {
|
2019-08-08 06:55:08 +02: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 21:19:37 +02:00
|
|
|
fn ($v.name mut $v.typ) $p.cur_fn.name (...) {
|
|
|
|
')
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 06:55:08 +02:00
|
|
|
p.error('`$v.name` is immutable.')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-25 13:16:17 +02:00
|
|
|
if !v.is_changed {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.cur_fn.mark_var_changed(v)
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
is_str := v.typ == 'string'
|
|
|
|
switch tok {
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.assign:
|
2019-08-20 13:34:29 +02:00
|
|
|
if !is_map && !p.is_empty_c_struct_init {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(' = ')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.plus_assign:
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_str {
|
|
|
|
p.gen('= string_add($v.name, ')// TODO can't do `foo.bar += '!'`
|
|
|
|
}
|
2019-07-03 13:20:43 +02:00
|
|
|
else {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(' += ')
|
|
|
|
}
|
2019-07-03 13:20:43 +02:00
|
|
|
default: p.gen(' ' + p.tok.str() + ' ')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-03 13:20:43 +02:00
|
|
|
p.fspace()
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fgen(tok.str())
|
2019-07-03 13:20:43 +02:00
|
|
|
p.fspace()
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
pos := p.cgen.cur_line.len
|
|
|
|
expr_type := p.bool_expression()
|
|
|
|
// Allow `num = 4` where `num` is an `?int`
|
|
|
|
if p.assigned_type.starts_with('Option_') && expr_type == p.assigned_type.right('Option_'.len) {
|
|
|
|
println('allowing option asss')
|
|
|
|
expr := p.cgen.cur_line.right(pos)
|
|
|
|
left := p.cgen.cur_line.left(pos)
|
2019-08-17 21:19:37 +02:00
|
|
|
typ := expr_type.replace('Option_', '')
|
2019-07-03 21:07:42 +02:00
|
|
|
//p.cgen.cur_line = left + 'opt_ok($expr)'
|
2019-08-12 17:54:28 +02:00
|
|
|
p.cgen.resetln(left + 'opt_ok($expr, sizeof($typ))')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-05 09:49:52 +02:00
|
|
|
else if !p.builtin_mod && !p.check_types_no_throw(expr_type, p.assigned_type) {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.scanner.line_nr--
|
|
|
|
p.error('cannot use type `$expr_type` as type `$p.assigned_type` in assignment')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if is_str && tok == .plus_assign {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(')')
|
|
|
|
}
|
|
|
|
// p.assigned_var = ''
|
|
|
|
p.assigned_type = ''
|
|
|
|
if !v.is_used {
|
|
|
|
p.cur_fn.mark_var_used(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) var_decl() {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.is_alloc = false
|
2019-07-07 22:30:15 +02: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 20:20:28 +02:00
|
|
|
p.fspace()
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .key_static {
|
|
|
|
p.check(.key_static)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fspace()
|
|
|
|
}
|
|
|
|
// println('var decl tok=${p.strtok()} ismut=$is_mut')
|
|
|
|
name := p.check_name()
|
2019-08-17 21:19:37 +02:00
|
|
|
p.var_decl_name = name
|
2019-06-22 20:20:28 +02:00
|
|
|
// Don't allow declaring a variable with the same name. Even in a child scope
|
|
|
|
// (shadowing is not allowed)
|
2019-08-05 09:49:52 +02:00
|
|
|
if !p.builtin_mod && p.cur_fn.known_var(name) {
|
2019-06-22 20:20:28 +02:00
|
|
|
v := p.cur_fn.find_var(name)
|
|
|
|
p.error('redefinition of `$name`')
|
|
|
|
}
|
2019-07-15 22:44:26 +02:00
|
|
|
if name.len > 1 && contains_capital(name) {
|
|
|
|
p.error('variable names cannot contain uppercase letters, use snake_case instead')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check_space(.decl_assign) // :=
|
2019-06-22 20:20:28 +02:00
|
|
|
// Generate expression to tmp because we need its type first
|
2019-07-07 22:30:15 +02:00
|
|
|
// [TYP .name =] bool_expression()
|
2019-06-22 20:20:28 +02:00
|
|
|
pos := p.cgen.add_placeholder()
|
|
|
|
mut typ := p.bool_expression()
|
|
|
|
// Option check ? or {
|
2019-08-17 21:19:37 +02:00
|
|
|
or_else := p.tok == .key_orelse
|
2019-06-22 20:20:28 +02:00
|
|
|
tmp := p.get_tmp()
|
|
|
|
if or_else {
|
|
|
|
// Option_User tmp = get_user(1);
|
|
|
|
// if (!tmp.ok) { or_statement }
|
|
|
|
// User user = *(User*)tmp.data;
|
|
|
|
// p.assigned_var = ''
|
|
|
|
p.cgen.set_placeholder(pos, '$typ $tmp = ')
|
2019-07-26 16:45:16 +02:00
|
|
|
p.genln(';')
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = typ.replace('Option_', '')
|
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.genln('if (!$tmp .ok) {')
|
2019-07-08 04:16:20 +02:00
|
|
|
p.register_var(Var {
|
|
|
|
name: 'err'
|
|
|
|
typ: 'string'
|
|
|
|
is_mut: false
|
|
|
|
is_used: true
|
|
|
|
})
|
|
|
|
p.genln('string err = $tmp . error;')
|
2019-06-22 20:20:28 +02:00
|
|
|
p.statements()
|
|
|
|
p.genln('$typ $name = *($typ*) $tmp . data;')
|
2019-07-07 22:30:15 +02:00
|
|
|
if !p.returns && p.prev_tok2 != .key_continue && p.prev_tok2 != .key_break {
|
2019-07-04 01:56:18 +02:00
|
|
|
p.error('`or` block must return/continue/break/panic')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-10 10:26:42 +02:00
|
|
|
p.returns = false
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.register_var(Var {
|
|
|
|
name: name
|
|
|
|
typ: typ
|
|
|
|
is_mut: is_mut
|
2019-08-17 21:19:37 +02:00
|
|
|
is_alloc: p.is_alloc
|
2019-06-22 20:20:28 +02:00
|
|
|
})
|
|
|
|
if !or_else {
|
|
|
|
gen_name := p.table.var_cgen_name(name)
|
2019-08-20 13:34:29 +02:00
|
|
|
mut nt_gen := p.table.cgen_name_type_pair(gen_name, typ)
|
|
|
|
// `foo := C.Foo{}` => `Foo foo;`
|
|
|
|
if !p.is_empty_c_struct_init {
|
|
|
|
nt_gen += '='
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_static {
|
|
|
|
nt_gen = 'static $nt_gen'
|
|
|
|
}
|
|
|
|
p.cgen.set_placeholder(pos, nt_gen)
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
p.var_decl_name = ''
|
2019-08-20 13:34:29 +02:00
|
|
|
p.is_empty_c_struct_init = false
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 10:18:31 +02: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 21:19:37 +02:00
|
|
|
)
|
2019-08-04 10:18:31 +02:00
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
fn (p mut Parser) bool_expression() string {
|
|
|
|
tok := p.tok
|
|
|
|
typ := p.bterm()
|
2019-08-17 21:19:37 +02:00
|
|
|
mut got_and := false // to catch `a && b || c` in one expression without ()
|
|
|
|
mut got_or := false
|
2019-07-14 11:01:32 +02:00
|
|
|
for p.tok == .and || p.tok == .logical_or {
|
2019-08-04 10:18:31 +02:00
|
|
|
if p.tok == .and {
|
2019-08-17 21:19:37 +02:00
|
|
|
got_and = true
|
|
|
|
if got_or { p.error(and_or_error) }
|
|
|
|
}
|
2019-08-04 10:18:31 +02:00
|
|
|
if p.tok == .logical_or {
|
2019-08-17 21:19:37 +02:00
|
|
|
got_or = true
|
|
|
|
if got_and { p.error(and_or_error) }
|
|
|
|
}
|
2019-08-09 18:10:59 +02:00
|
|
|
if p.is_sql {
|
|
|
|
if p.tok == .and {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.gen(' and ')
|
|
|
|
}
|
2019-08-09 18:10:59 +02:00
|
|
|
else if p.tok == .logical_or {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.gen(' or ')
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-09 18:10:59 +02:00
|
|
|
p.gen(' ${p.tok.str()} ')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
|
|
|
p.check_space(p.tok)
|
2019-06-22 20:20:28 +02: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 14:19:46 +02:00
|
|
|
mut typ := p.expression()
|
2019-08-17 21:19:37 +02:00
|
|
|
p.expected_type = typ
|
|
|
|
is_str := typ=='string' && !p.is_sql
|
2019-06-22 20:20:28 +02:00
|
|
|
tok := p.tok
|
2019-07-07 22:30:15 +02:00
|
|
|
// if tok in [ .eq, .gt, .lt, .le, .ge, .ne] {
|
2019-08-20 13:34:29 +02:00
|
|
|
if tok == .eq || tok == .gt || tok == .lt || tok == .le || tok == .ge || tok == .ne {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(' ${p.tok.str()} ')
|
|
|
|
if is_str {
|
|
|
|
p.gen(',')
|
|
|
|
}
|
2019-08-09 18:10:59 +02:00
|
|
|
else if p.is_sql && tok == .eq {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.gen('=')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
else {
|
|
|
|
p.gen(tok.str())
|
|
|
|
}
|
|
|
|
p.next()
|
2019-08-17 21:19:37 +02:00
|
|
|
// `id == user.id` => `id == $1`, `user.id`
|
2019-08-13 13:50:19 +02:00
|
|
|
if p.is_sql {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.sql_i++
|
|
|
|
p.gen('$' + p.sql_i.str())
|
|
|
|
p.cgen.start_cut()
|
2019-08-13 13:50:19 +02:00
|
|
|
p.check_types(p.expression(), typ)
|
2019-08-20 17:05:56 +02: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 21:19:37 +02:00
|
|
|
} else {
|
2019-08-13 13:50:19 +02:00
|
|
|
p.check_types(p.expression(), typ)
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = 'bool'
|
2019-08-17 21:19:37 +02:00
|
|
|
if is_str { //&& !p.is_sql {
|
2019-06-22 22:12:38 +02:00
|
|
|
p.gen(')')
|
2019-06-22 20:20:28 +02:00
|
|
|
switch tok {
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.eq: p.cgen.set_placeholder(ph, 'string_eq(')
|
|
|
|
case Token.ne: p.cgen.set_placeholder(ph, 'string_ne(')
|
|
|
|
case Token.le: p.cgen.set_placeholder(ph, 'string_le(')
|
|
|
|
case Token.ge: p.cgen.set_placeholder(ph, 'string_ge(')
|
|
|
|
case Token.gt: p.cgen.set_placeholder(ph, 'string_gt(')
|
|
|
|
case Token.lt: p.cgen.set_placeholder(ph, 'string_lt(')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
/*
|
2019-07-17 10:54:24 +02:00
|
|
|
Token.eq => p.cgen.set_placeholder(ph, 'string_eq(')
|
|
|
|
Token.ne => p.cgen.set_placeholder(ph, 'string_ne(')
|
|
|
|
Token.le => p.cgen.set_placeholder(ph, 'string_le(')
|
|
|
|
Token.ge => p.cgen.set_placeholder(ph, 'string_ge(')
|
|
|
|
Token.gt => p.cgen.set_placeholder(ph, 'string_gt(')
|
|
|
|
Token.lt => p.cgen.set_placeholder(ph, 'string_lt(')
|
2019-08-17 21:19:37 +02:00
|
|
|
*/
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2019-08-17 21:19:37 +02:00
|
|
|
// also called on *, &, @, . (enum)
|
2019-06-22 20:20:28 +02:00
|
|
|
fn (p mut Parser) name_expr() string {
|
2019-07-06 02:22:29 +02:00
|
|
|
ph := p.cgen.add_placeholder()
|
2019-06-22 20:20:28 +02:00
|
|
|
// amp
|
2019-07-07 22:30:15 +02:00
|
|
|
ptr := p.tok == .amp
|
|
|
|
deref := p.tok == .mul
|
2019-06-22 20:20:28 +02:00
|
|
|
if ptr || deref {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
if deref {
|
2019-08-05 09:49:52 +02:00
|
|
|
if p.pref.is_play && !p.builtin_mod {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('dereferencing is temporarily disabled on the playground, will be fixed soon')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mut name := p.lit
|
|
|
|
p.fgen(name)
|
|
|
|
// known_type := p.table.known_type(name)
|
|
|
|
orig_name := name
|
2019-08-17 21:19:37 +02:00
|
|
|
is_c := name == 'C' && p.peek() == .dot
|
2019-06-22 20:20:28 +02:00
|
|
|
mut is_c_struct_init := is_c && ptr// a := &C.mycstruct{}
|
|
|
|
if is_c {
|
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.dot)
|
2019-06-22 20:20:28 +02:00
|
|
|
name = p.lit
|
|
|
|
p.fgen(name)
|
|
|
|
// Currently struct init is set to true only we have `&C.Foo{}`, handle `C.Foo{}`:
|
2019-07-07 22:30:15 +02:00
|
|
|
if !is_c_struct_init && p.peek() == .lcbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
is_c_struct_init = true
|
|
|
|
}
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
// enum value? (`color == .green`)
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .dot {
|
2019-08-17 21:19:37 +02:00
|
|
|
//println('got enum dot val $p.left_type pass=$p.pass $p.scanner.line_nr left=$p.left_type')
|
|
|
|
T := p.find_type(p.expected_type)
|
2019-08-20 13:34:29 +02:00
|
|
|
if T.cat == .enum_ {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.dot)
|
|
|
|
val := p.check_name()
|
|
|
|
// Make sure this enum value exists
|
2019-07-07 22:30:15 +02:00
|
|
|
if !T.has_enum_val(val) {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error('enum `$T.name` does not have value `$val`')
|
|
|
|
}
|
|
|
|
p.gen(T.mod + '__' + p.expected_type + '_' + val)
|
|
|
|
}
|
|
|
|
return p.expected_type
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// //////////////////////////
|
|
|
|
// module ?
|
|
|
|
// Allow shadowing (gg = gg.newcontext(); gg.draw_triangle())
|
2019-08-05 09:49:52 +02:00
|
|
|
if ((name == p.mod && p.table.known_mod(name)) || p.import_table.known_alias(name))
|
2019-07-12 07:37:54 +02:00
|
|
|
&& !p.cur_fn.known_var(name) && !is_c {
|
2019-08-05 09:49:52 +02:00
|
|
|
mut mod := name
|
2019-07-12 07:37:54 +02:00
|
|
|
// must be aliased module
|
|
|
|
if name != p.mod && p.import_table.known_alias(name) {
|
2019-07-13 15:11:32 +02:00
|
|
|
// we replaced "." with "_dot_" in p.mod for C variable names, do same here.
|
2019-08-05 09:49:52 +02:00
|
|
|
mod = p.import_table.resolve_alias(name).replace('.', '_dot_')
|
2019-07-12 07:37:54 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.dot)
|
2019-06-22 20:20:28 +02:00
|
|
|
name = p.lit
|
|
|
|
p.fgen(name)
|
2019-08-05 09:49:52 +02:00
|
|
|
name = prepend_mod(mod, name)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else if !p.table.known_type(name) && !p.cur_fn.known_var(name) &&
|
|
|
|
!p.table.known_fn(name) && !p.table.known_const(name) && !is_c {
|
2019-08-05 09:49:52 +02:00
|
|
|
name = p.prepend_mod(name)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Variable
|
2019-07-19 13:27:44 +02:00
|
|
|
mut v := p.cur_fn.find_var(name)
|
2019-08-17 21:19:37 +02:00
|
|
|
// A hack to allow `newvar := Foo{ field: newvar }`
|
|
|
|
// Declare the variable so that it can be used in the initialization
|
2019-07-19 13:27:44 +02:00
|
|
|
if name == 'main__' + p.var_decl_name {
|
|
|
|
v.name = p.var_decl_name
|
2019-08-17 21:19:37 +02:00
|
|
|
v.typ = 'voidptr'
|
|
|
|
v.is_mut = true
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
if v.name.len != 0 {
|
|
|
|
if ptr {
|
|
|
|
p.gen('& /*vvar*/ ')
|
|
|
|
}
|
|
|
|
else if deref {
|
|
|
|
p.gen('*')
|
|
|
|
}
|
|
|
|
mut typ := p.var_expr(v)
|
|
|
|
// *var
|
|
|
|
if 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 ptr {
|
|
|
|
typ += '*'
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
// if known_type || is_c_struct_init || (p.first_pass() && p.peek() == .lcbr) {
|
2019-06-22 20:20:28 +02:00
|
|
|
// known type? int(4.5) or Color.green (enum)
|
|
|
|
if p.table.known_type(name) {
|
|
|
|
// float(5), byte(0), (*int)(ptr) etc
|
2019-07-07 22:30:15 +02:00
|
|
|
if !is_c && ( p.peek() == .lpar || (deref && p.peek() == .rpar) ) {
|
2019-06-22 20:20:28 +02:00
|
|
|
if deref {
|
|
|
|
name += '*'
|
|
|
|
}
|
|
|
|
else if ptr {
|
|
|
|
name += '*'
|
|
|
|
}
|
2019-06-27 12:27:46 +02:00
|
|
|
p.gen('(')
|
2019-06-22 20:20:28 +02:00
|
|
|
mut typ := p.cast(name)
|
|
|
|
p.gen(')')
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok == .dot {
|
2019-07-06 02:22:29 +02:00
|
|
|
typ = p.dot(typ, ph)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
// Color.green
|
2019-07-07 22:30:15 +02:00
|
|
|
else if p.peek() == .dot {
|
2019-06-22 20:20:28 +02:00
|
|
|
enum_type := p.table.find_type(name)
|
2019-08-20 13:34:29 +02:00
|
|
|
if enum_type.cat != .enum_ {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('`$name` is not an enum')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.dot)
|
2019-06-22 20:20:28 +02:00
|
|
|
val := p.lit
|
|
|
|
// println('enum val $val')
|
2019-07-14 00:10:39 +02:00
|
|
|
p.gen(enum_type.mod + '__' + enum_type.name + '_' + val)// `color = main__Color_green`
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
return enum_type.name
|
|
|
|
}
|
2019-08-20 13:34:29 +02:00
|
|
|
// struct initialization
|
2019-07-07 22:30:15 +02:00
|
|
|
else if p.peek() == .lcbr {
|
2019-07-29 18:21:36 +02:00
|
|
|
if ptr {
|
|
|
|
name += '*' // `&User{}` => type `User*`
|
|
|
|
}
|
|
|
|
if name == 'T' {
|
2019-08-17 21:19:37 +02:00
|
|
|
name = p.cur_gen_type
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
return p.struct_init(name, is_c_struct_init)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_c {
|
2019-08-17 21:19:37 +02:00
|
|
|
// C const (`C.GLFW_KEY_LEFT`)
|
2019-08-06 05:54:47 +02:00
|
|
|
if p.peek() != .lpar {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.gen(name)
|
|
|
|
p.next()
|
|
|
|
return 'int'
|
|
|
|
}
|
2019-08-06 05:54:47 +02:00
|
|
|
// C fn
|
2019-06-22 20:20:28 +02:00
|
|
|
f := Fn {
|
|
|
|
name: name// .replace('c_', '')
|
|
|
|
is_c: true
|
|
|
|
}
|
2019-08-20 13:34:29 +02:00
|
|
|
p.is_c_fn_call = true
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fn_call(f, 0, '', '')
|
2019-08-20 13:34:29 +02:00
|
|
|
p.is_c_fn_call = false
|
2019-06-22 20:20:28 +02:00
|
|
|
// 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)
|
|
|
|
// Not Found? Return 'void*'
|
|
|
|
if cfn.name == '' {
|
2019-08-20 13:34:29 +02:00
|
|
|
//return 'cvoid' //'void*'
|
2019-06-22 20:20:28 +02:00
|
|
|
return 'void*'
|
|
|
|
}
|
|
|
|
return cfn.typ
|
|
|
|
}
|
|
|
|
// Constant
|
2019-07-25 13:16:17 +02:00
|
|
|
c := p.table.find_const(name)
|
2019-06-22 20:20:28 +02:00
|
|
|
if c.name != '' && ptr && !c.is_global {
|
|
|
|
p.error('cannot take the address of constant `$c.name`')
|
|
|
|
}
|
|
|
|
if c.name.len != 0 {
|
|
|
|
if ptr {
|
|
|
|
// c.ptr = true
|
|
|
|
p.gen('& /*const*/ ')
|
|
|
|
}
|
|
|
|
p.log('calling var expr')
|
|
|
|
mut typ := p.var_expr(c)
|
|
|
|
if ptr {
|
|
|
|
typ += '*'
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
// Function (not method btw, methods are handled in dot())
|
2019-07-17 10:54:24 +02:00
|
|
|
mut f := p.table.find_fn(name)
|
2019-06-22 20:20:28 +02:00
|
|
|
if f.name == '' {
|
2019-08-17 21:19:37 +02:00
|
|
|
// We are in a second pass, that means this function was not defined, throw an error.
|
2019-07-29 18:21:36 +02:00
|
|
|
if !p.first_pass() {
|
2019-08-17 21:19:37 +02:00
|
|
|
// V script? Try os module.
|
|
|
|
if p.v_script {
|
|
|
|
name = name.replace('main__', 'os__')
|
|
|
|
f = p.table.find_fn(name)
|
|
|
|
}
|
|
|
|
if f.name == '' {
|
2019-08-05 09:49:52 +02: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) {
|
2019-07-17 10:54:24 +02:00
|
|
|
name = name.replace('__', '.').replace('_dot_', '.')
|
|
|
|
p.error('undefined: `$name`')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('undefined: `$orig_name`')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-07-17 10:54:24 +02:00
|
|
|
p.next()
|
2019-08-17 21:19:37 +02:00
|
|
|
// First pass, the function can be defined later.
|
2019-07-17 10:54:24 +02:00
|
|
|
return 'void'
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// no () after func, so func is an argument, just gen its name
|
|
|
|
// TODO verify this and handle errors
|
2019-08-17 21:19:37 +02:00
|
|
|
peek := p.peek()
|
2019-07-29 18:21:36 +02:00
|
|
|
if peek != .lpar && peek != .lt {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(p.table.cgen_name(f))
|
|
|
|
p.next()
|
|
|
|
return 'void*'
|
|
|
|
}
|
|
|
|
// TODO bring back
|
|
|
|
if f.typ == 'void' && !p.inside_if_expr {
|
|
|
|
// p.error('`$f.name` used as value')
|
|
|
|
}
|
|
|
|
p.log('calling function')
|
|
|
|
p.fn_call(f, 0, '', '')
|
|
|
|
// dot after a function call: `get_user().age`
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .dot {
|
2019-06-22 20:20:28 +02:00
|
|
|
mut typ := ''
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok == .dot {
|
2019-06-22 20:20:28 +02:00
|
|
|
// println('dot #$dc')
|
2019-07-06 02:22:29 +02:00
|
|
|
typ = p.dot(f.typ, ph)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
p.log('end of name_expr')
|
|
|
|
return f.typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) var_expr(v Var) string {
|
|
|
|
p.log('\nvar_expr() v.name="$v.name" v.typ="$v.typ"')
|
|
|
|
// println('var expr is_tmp=$p.cgen.is_tmp\n')
|
2019-08-17 21:19:37 +02:00
|
|
|
p.cur_fn.mark_var_used(v)
|
2019-06-22 20:20:28 +02: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 21:19:37 +02:00
|
|
|
// Function pointer?
|
2019-06-22 20:20:28 +02:00
|
|
|
if typ.starts_with('fn ') {
|
2019-07-16 15:29:27 +02:00
|
|
|
//println('CALLING FN PTR')
|
|
|
|
//p.print_tok()
|
2019-06-22 20:20:28 +02:00
|
|
|
T := p.table.find_type(typ)
|
|
|
|
p.gen('(')
|
2019-07-29 18:21:36 +02:00
|
|
|
p.fn_call_args(mut T.func)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(')')
|
|
|
|
typ = T.func.typ
|
|
|
|
}
|
|
|
|
// users[0].name
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .lsbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = p.index_expr(typ, fn_ph)
|
|
|
|
}
|
|
|
|
// a.b.c().d chain
|
|
|
|
// mut dc := 0
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok ==.dot {
|
2019-08-09 18:10:59 +02:00
|
|
|
if p.peek() == .key_select {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.next()
|
|
|
|
return p.select_query(fn_ph)
|
|
|
|
}
|
2019-08-12 11:06:17 +02:00
|
|
|
if typ == 'pg__DB' && !p.fileis('pg.v') && p.peek() == .name {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.next()
|
|
|
|
p.insert_query(fn_ph)
|
|
|
|
return 'void'
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// println('dot #$dc')
|
|
|
|
typ = p.dot(typ, fn_ph)
|
|
|
|
p.log('typ after dot=$typ')
|
|
|
|
// print('tok after dot()')
|
|
|
|
// p.print_tok()
|
|
|
|
// dc++
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .lsbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
// typ = p.index_expr(typ, fn_ph, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// a++ and a--
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .inc || p.tok == .dec {
|
2019-07-05 02:44:22 +02:00
|
|
|
if !v.is_mut && !v.is_arg && !p.pref.translated {
|
2019-07-10 14:18:21 +02:00
|
|
|
p.error('`$v.name` is immutable')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-25 13:16:17 +02:00
|
|
|
if !v.is_changed {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.cur_fn.mark_var_changed(v)
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
if typ != 'int' {
|
2019-06-30 22:03:17 +02:00
|
|
|
if !p.pref.translated && !is_number_type(typ) {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('cannot ++/-- value of type `$typ`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.gen(p.tok.str())
|
|
|
|
p.fgen(p.tok.str())
|
2019-08-17 21:19:37 +02:00
|
|
|
p.next()// ++/--
|
|
|
|
// allow `a := c++` in translated code
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.pref.translated {
|
2019-07-02 00:00:27 +02:00
|
|
|
//return p.index_expr(typ, fn_ph)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 'void'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
typ = p.index_expr(typ, fn_ph)
|
2019-08-17 21:19:37 +02:00
|
|
|
// TODO hack to allow `foo.bar[0] = 2`
|
|
|
|
if p.tok == .dot {
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok == .dot {
|
2019-07-02 00:00:27 +02:00
|
|
|
typ = p.dot(typ, fn_ph)
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-07-02 00:00:27 +02:00
|
|
|
typ = p.index_expr(typ, fn_ph)
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2019-08-17 21:19:37 +02:00
|
|
|
// for debugging only
|
2019-06-22 20:20:28 +02:00
|
|
|
fn (p &Parser) fileis(s string) bool {
|
|
|
|
return p.scanner.file_path.contains(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// user.name => `str_typ` is `User`
|
|
|
|
// user.company.name => `str_typ` is `Company`
|
|
|
|
fn (p mut Parser) dot(str_typ string, method_ph int) string {
|
2019-08-17 21:19:37 +02:00
|
|
|
//if p.fileis('orm_test') {
|
|
|
|
//println('ORM dot $str_typ')
|
|
|
|
//}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.dot)
|
2019-07-29 18:21:36 +02:00
|
|
|
typ := p.find_type(str_typ)
|
|
|
|
if typ.name.len == 0 {
|
|
|
|
p.error('dot(): cannot find type `$str_typ`')
|
|
|
|
}
|
|
|
|
if p.tok == .dollar {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.comptime_method_call(typ)
|
|
|
|
return 'void'
|
|
|
|
}
|
2019-07-25 13:16:17 +02:00
|
|
|
field_name := p.lit
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fgen(field_name)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.log('dot() field_name=$field_name typ=$str_typ')
|
2019-07-02 00:00:27 +02:00
|
|
|
//if p.fileis('main.v') {
|
2019-08-17 21:19:37 +02:00
|
|
|
//println('dot() field_name=$field_name typ=$str_typ prev_tok=${prev_tok.str()}')
|
2019-07-02 00:00:27 +02:00
|
|
|
//}
|
2019-06-22 20:20:28 +02:00
|
|
|
has_field := p.table.type_has_field(typ, field_name)
|
2019-08-10 23:02:48 +02:00
|
|
|
mut has_method := p.table.type_has_method(typ, field_name)
|
2019-08-17 21:19:37 +02:00
|
|
|
// generate `.str()`
|
|
|
|
if !has_method && field_name == 'str' && typ.name.starts_with('array_') {
|
|
|
|
p.gen_array_str(mut typ)
|
|
|
|
has_method = true
|
|
|
|
}
|
2019-08-20 13:34:29 +02:00
|
|
|
if !typ.is_c && !p.is_c_fn_call && !has_field && !has_method && !p.first_pass() {
|
2019-06-22 20:20:28 +02:00
|
|
|
if typ.name.starts_with('Option_') {
|
2019-08-17 21:19:37 +02:00
|
|
|
opt_type := typ.name.right(7)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('unhandled option type: $opt_type?')
|
|
|
|
}
|
2019-08-05 10:42:58 +02: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"')
|
|
|
|
p.error('type `$typ.name` has no field or method `$field_name`')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
mut dot := '.'
|
2019-08-20 13:34:29 +02:00
|
|
|
if str_typ.ends_with('*') || str_typ == 'FT_Face' { // TODO fix C ptr typedefs
|
2019-06-22 20:20:28 +02:00
|
|
|
dot = '->'
|
|
|
|
}
|
|
|
|
// field
|
|
|
|
if has_field {
|
|
|
|
field := p.table.find_field(typ, field_name)
|
|
|
|
// Is the next token `=`, `+=` etc? (Are we modifying the field?)
|
|
|
|
next := p.peek()
|
2019-07-07 22:30:15 +02:00
|
|
|
modifying := next.is_assign() || next == .inc || next == .dec
|
2019-08-07 13:00:19 +02:00
|
|
|
is_vi := p.fileis('vid')
|
2019-08-05 09:49:52 +02:00
|
|
|
if !p.builtin_mod && !p.pref.translated && modifying && !field.is_mut && !is_vi {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error('cannot modify immutable field `$field_name` (type `$typ.name`)\n' +
|
2019-08-08 06:55:08 +02:00
|
|
|
'declare the field with `mut:`
|
|
|
|
|
|
|
|
struct $typ.name {
|
|
|
|
mut:
|
2019-08-17 21:19:37 +02:00
|
|
|
$field_name $field.typ
|
|
|
|
}
|
|
|
|
')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-05 09:49:52 +02:00
|
|
|
if !p.builtin_mod && p.mod != typ.mod {
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-05 09:49:52 +02:00
|
|
|
// if p.pref.is_play && field.access_mod ==.private && !p.builtin_mod && p.mod != typ.mod {
|
2019-06-22 20:20:28 +02:00
|
|
|
// Don't allow `arr.data`
|
2019-08-05 09:49:52 +02:00
|
|
|
if field.access_mod == .private && !p.builtin_mod && !p.pref.translated && p.mod != typ.mod {
|
2019-06-22 20:20:28 +02:00
|
|
|
// println('$typ.name :: $field.name ')
|
|
|
|
// println(field.access_mod)
|
|
|
|
p.error('cannot refer to unexported field `$field_name` (type `$typ.name`)')
|
|
|
|
}
|
2019-08-05 09:49:52 +02:00
|
|
|
// if field.access_mod ==.public && p.peek() == .assign && !p.builtin_mod && p.mod != typ.mod {
|
2019-06-22 20:20:28 +02:00
|
|
|
// Don't allow `str.len = 0`
|
2019-08-05 09:49:52 +02:00
|
|
|
if field.access_mod == .public && !p.builtin_mod && p.mod != typ.mod {
|
2019-06-30 22:03:17 +02:00
|
|
|
if !field.is_mut && !p.pref.translated && modifying {
|
2019-07-10 14:18:21 +02:00
|
|
|
p.error('cannot modify public immutable field `$field_name` (type `$typ.name`)')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.gen(dot + field_name)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
return field.typ
|
|
|
|
}
|
|
|
|
// method
|
2019-07-25 13:16:17 +02:00
|
|
|
method := p.table.find_method(typ, field_name)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fn_call(method, method_ph, '', str_typ)
|
2019-08-17 21:19:37 +02:00
|
|
|
// Methods returning `array` should return `array_string`
|
2019-06-22 20:20:28 +02:00
|
|
|
if method.typ == 'array' && typ.name.starts_with('array_') {
|
|
|
|
return typ.name
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
// Array methods returning `voidptr` (like `last()`) should return element type
|
2019-06-22 20:20:28 +02:00
|
|
|
if method.typ == 'void*' && typ.name.starts_with('array_') {
|
|
|
|
return typ.name.right(6)
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
//if false && p.tok == .lsbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
// if is_indexer {
|
2019-07-02 00:00:27 +02:00
|
|
|
//return p.index_expr(method.typ, method_ph)
|
|
|
|
//}
|
2019-06-22 20:20:28 +02:00
|
|
|
return method.typ
|
|
|
|
}
|
|
|
|
|
2019-08-07 08:19:27 +02:00
|
|
|
fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
|
2019-08-17 21:19:37 +02:00
|
|
|
mut typ := typ_
|
2019-06-22 20:20:28 +02:00
|
|
|
// a[0]
|
|
|
|
v := p.expr_var
|
2019-07-24 13:04:57 +02:00
|
|
|
//if p.fileis('fn_test.v') {
|
|
|
|
//println('index expr typ=$typ')
|
2019-08-17 21:19:37 +02:00
|
|
|
//println(v.name)
|
2019-07-24 13:04:57 +02:00
|
|
|
//}
|
2019-06-22 20:20:28 +02: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-07-07 22:30:15 +02:00
|
|
|
is_indexer := p.tok == .lsbr
|
2019-06-22 20:20:28 +02:00
|
|
|
mut close_bracket := false
|
|
|
|
if is_indexer {
|
|
|
|
is_fixed_arr := typ[0] == `[`
|
|
|
|
if !is_str && !is_arr && !is_map && !is_ptr && !is_fixed_arr {
|
|
|
|
p.error('Cant [] non-array/string/map. Got type "$typ"')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lsbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
// Get element type (set `typ` to it)
|
|
|
|
if is_str {
|
|
|
|
typ = 'byte'
|
|
|
|
p.fgen('[')
|
2019-08-05 09:49:52 +02:00
|
|
|
// Direct faster access to .str[i] in builtin modules
|
|
|
|
if p.builtin_mod {
|
2019-06-22 20:20:28 +02: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)
|
|
|
|
typ = typ.right(pos)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
typ = typ.all_after(']')
|
|
|
|
}
|
|
|
|
p.gen('[')
|
|
|
|
close_bracket = true
|
|
|
|
}
|
|
|
|
else if is_ptr {
|
|
|
|
// typ = 'byte'
|
|
|
|
typ = typ.replace('*', '')
|
|
|
|
// modify(mut []string) fix
|
|
|
|
if !is_arr {
|
|
|
|
p.gen('[/*ptr*/')
|
|
|
|
close_bracket = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_arr {
|
2019-07-03 13:20:43 +02:00
|
|
|
//p.fgen('[')
|
2019-06-22 20:20:28 +02:00
|
|
|
// array_int a; a[0]
|
|
|
|
// type is "array_int", need "int"
|
|
|
|
// typ = typ.replace('array_', '')
|
|
|
|
if is_arr0 {
|
|
|
|
if p.fileis('int_test') {
|
|
|
|
println('\nRRRR0 $typ')
|
|
|
|
}
|
|
|
|
typ = typ.right(6)
|
|
|
|
if p.fileis('int_test') {
|
|
|
|
println('RRRR $typ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// array a; a.first() voidptr
|
|
|
|
// type is "array", need "void*"
|
|
|
|
if typ == 'array' {
|
|
|
|
typ = 'void*'
|
|
|
|
}
|
|
|
|
// No bounds check in translated from C code
|
2019-08-05 09:49:52 +02:00
|
|
|
if p.pref.translated && !p.builtin_mod{
|
2019-06-22 20:20:28 +02:00
|
|
|
// Cast void* to typ*: add (typ*) to the beginning of the assignment :
|
|
|
|
// ((int*)a.data = ...
|
|
|
|
p.cgen.set_placeholder(fn_ph, '(($typ*)(')
|
|
|
|
p.gen('.data))[')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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 [ ]
|
|
|
|
if is_arr {
|
2019-08-18 18:26:02 +02:00
|
|
|
index_pos := p.cgen.cur_line.len
|
2019-06-22 20:20:28 +02:00
|
|
|
T := p.table.find_type(p.expression())
|
2019-08-12 01:58:08 +02:00
|
|
|
// Allows only i8-64 and u8-64 to be used when accessing an array
|
|
|
|
if T.parent != 'int' && T.parent != 'u32' {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.check_types(T.name, 'int')
|
|
|
|
}
|
2019-08-18 18:26:02 +02:00
|
|
|
if p.cgen.cur_line.right(index_pos).replace(' ', '').int() < 0 {
|
|
|
|
p.error('cannot access negative array index')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-08-13 19:13:39 +02: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 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rsbr)
|
2019-08-05 09:49:52 +02:00
|
|
|
// if (is_str && p.builtin_mod) || is_ptr || is_fixed_arr && ! (is_ptr && is_arr) {
|
2019-06-22 20:20:28 +02:00
|
|
|
if close_bracket {
|
|
|
|
p.gen(']/*r$typ $v.is_mut*/')
|
|
|
|
}
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
// TODO move this from index_expr()
|
2019-06-22 20:20:28 +02:00
|
|
|
// TODO if p.tok in ...
|
2019-07-07 22:30:15 +02:00
|
|
|
// if p.tok in [.assign, .plus_assign, .minus_assign]
|
2019-08-09 18:10:59 +02:00
|
|
|
if (p.tok == .assign && !p.is_sql) || p.tok == .plus_assign || p.tok == .minus_assign ||
|
2019-07-07 22:30:15 +02:00
|
|
|
p.tok == .mult_assign || p.tok == .div_assign || p.tok == .xor_assign || p.tok == .mod_assign ||
|
|
|
|
p.tok == .or_assign || p.tok == .and_assign || p.tok == .righ_shift_assign ||
|
|
|
|
p.tok == .left_shift_assign {
|
2019-08-05 09:49:52 +02:00
|
|
|
if is_indexer && is_str && !p.builtin_mod {
|
2019-07-10 14:18:21 +02:00
|
|
|
p.error('strings are immutable')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
assign_pos := p.cgen.cur_line.len
|
2019-08-22 03:27:57 +02:00
|
|
|
is_cao := p.tok != .assign
|
2019-06-22 20:20:28 +02:00
|
|
|
p.assigned_type = typ
|
2019-07-07 22:30:15 +02:00
|
|
|
p.expected_type = typ
|
2019-06-22 20:20:28 +02:00
|
|
|
p.assign_statement(v, fn_ph, is_indexer && (is_map || is_arr))
|
|
|
|
// m[key] = val
|
|
|
|
if is_indexer && (is_map || is_arr) {
|
|
|
|
// a[0] = 7
|
|
|
|
// curline right now: "a , 0 = 7"
|
|
|
|
// println('222 "$p.cgen.cur_line"')
|
|
|
|
// Cant have &7, so need a tmp
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
tmp_val := p.cgen.cur_line.right(assign_pos)
|
2019-07-18 20:21:23 +02:00
|
|
|
p.cgen.resetln(p.cgen.cur_line.left(assign_pos))
|
2019-06-22 20:20:28 +02:00
|
|
|
// val := p.cgen.end_tmp()
|
2019-08-22 03:27:57 +02:00
|
|
|
mut cao_tmp := p.cgen.cur_line
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_map {
|
|
|
|
p.cgen.set_placeholder(fn_ph, 'map__set(&')
|
2019-08-22 03:27:57 +02:00
|
|
|
// CAO on map is a bit more complicated as it loads
|
|
|
|
// the value inside a pointer instead of returning it.
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if is_ptr {
|
|
|
|
p.cgen.set_placeholder(fn_ph, 'array_set(')
|
2019-08-22 03:27:57 +02:00
|
|
|
if is_cao {
|
|
|
|
cao_tmp = '*($p.expected_type *) array__get(*$cao_tmp)'
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.cgen.set_placeholder(fn_ph, 'array_set(&/*q*/')
|
2019-08-22 03:27:57 +02:00
|
|
|
if is_cao {
|
|
|
|
cao_tmp = '*($p.expected_type *) array__get($cao_tmp)'
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p.gen(', & $tmp)')
|
2019-08-22 03:27:57 +02:00
|
|
|
if !is_cao {
|
|
|
|
p.cgen.insert_before('$typ $tmp = $tmp_val;')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.cgen.insert_before('$typ $tmp = $cao_tmp ' + tmp_val.all_before('=') + tmp_val.all_after('=') + ';')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
2019-06-30 22:03:17 +02:00
|
|
|
// else if p.pref.is_verbose && p.assigned_var != '' {
|
2019-06-22 20:20:28 +02:00
|
|
|
// p.error('didnt assign')
|
|
|
|
// }
|
|
|
|
// m[key]. no =, just a getter
|
2019-08-05 09:49:52 +02:00
|
|
|
else if (is_map || is_arr || (is_str && !p.builtin_mod)) && is_indexer {
|
2019-06-22 20:20:28 +02:00
|
|
|
// Erase var name we generated earlier: "int a = m, 0"
|
|
|
|
// "m, 0" gets killed since we need to start from scratch. It's messy.
|
|
|
|
// "m, 0" is an index expression, save it before deleting and insert later in map_get()
|
|
|
|
index_expr := p.cgen.cur_line.right(fn_ph)
|
2019-07-18 20:21:23 +02:00
|
|
|
p.cgen.resetln(p.cgen.cur_line.left(fn_ph))
|
2019-06-22 20:20:28 +02:00
|
|
|
// Can't pass integer literal, because map_get() requires a void*
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
tmp_ok := p.get_tmp()
|
|
|
|
if is_map {
|
|
|
|
p.gen('$tmp')
|
2019-07-23 23:23:13 +02:00
|
|
|
mut def := type_default(typ)
|
2019-07-24 17:46:41 +02:00
|
|
|
if def == 'STRUCT_DEFAULT_VALUE' {
|
2019-07-23 23:23:13 +02:00
|
|
|
def = '{0}'
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.cgen.insert_before('$typ $tmp = $def; bool $tmp_ok = map_get($index_expr, & $tmp);')
|
|
|
|
}
|
|
|
|
else if is_arr {
|
2019-08-05 09:49:52 +02:00
|
|
|
if p.pref.translated && !p.builtin_mod {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('$index_expr ]')
|
|
|
|
}
|
|
|
|
else {
|
2019-08-17 21:19:37 +02:00
|
|
|
if is_ptr {
|
2019-07-24 13:04:57 +02:00
|
|
|
p.gen('( *($typ*) array__get(* $index_expr) )')
|
2019-08-17 21:19:37 +02:00
|
|
|
} else {
|
2019-07-24 13:04:57 +02:00
|
|
|
p.gen('( *($typ*) array__get($index_expr) )')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-05 09:49:52 +02:00
|
|
|
else if is_str && !p.builtin_mod {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('string_at($index_expr)')
|
|
|
|
}
|
|
|
|
// Zero the string after map_get() if it's nil, numbers are automatically 0
|
|
|
|
// This is ugly, but what can I do without generics?
|
|
|
|
// TODO what about user types?
|
|
|
|
if is_map && typ == 'string' {
|
|
|
|
// p.cgen.insert_before('if (!${tmp}.str) $tmp = tos("", 0);')
|
|
|
|
p.cgen.insert_before('if (!$tmp_ok) $tmp = tos("", 0);')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// else if is_arr && is_indexer{}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns resulting type
|
|
|
|
fn (p mut Parser) expression() string {
|
|
|
|
if p.scanner.file_path.contains('test_test') {
|
2019-07-29 18:21:36 +02:00
|
|
|
println('expression() pass=$p.pass tok=')
|
2019-06-22 20:20:28 +02:00
|
|
|
p.print_tok()
|
|
|
|
}
|
|
|
|
p.cgen('/* expr start*/')
|
|
|
|
ph := p.cgen.add_placeholder()
|
|
|
|
mut typ := p.term()
|
2019-08-17 21:19:37 +02:00
|
|
|
is_str := typ=='string'
|
2019-06-22 20:20:28 +02:00
|
|
|
// a << b ==> array2_push(&a, b)
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .left_shift {
|
2019-06-22 20:20:28 +02: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()
|
|
|
|
tmp_typ := typ.right(6)// skip "array_"
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check_space(.left_shift)
|
2019-06-22 20:20:28 +02:00
|
|
|
// Get the value we are pushing
|
|
|
|
p.gen(', (')
|
2019-08-20 13:34:29 +02:00
|
|
|
// Immutable? Can we push?
|
2019-06-30 22:03:17 +02:00
|
|
|
if !p.expr_var.is_mut && !p.pref.translated {
|
2019-07-10 14:18:21 +02:00
|
|
|
p.error('`$p.expr_var.name` is immutable (can\'t <<)')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-25 13:16:17 +02:00
|
|
|
if !p.expr_var.is_changed {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.cur_fn.mark_var_changed(p.expr_var)
|
|
|
|
}
|
|
|
|
expr_type := p.expression()
|
|
|
|
// Two arrays of the same type?
|
2019-07-04 01:56:18 +02:00
|
|
|
push_array := typ == expr_type
|
2019-08-17 21:19:37 +02:00
|
|
|
if push_array {
|
|
|
|
p.cgen.set_placeholder(ph, '_PUSH_MANY(&' )
|
2019-07-04 01:56:18 +02:00
|
|
|
p.gen('), $tmp, $typ)')
|
2019-08-17 21:19:37 +02:00
|
|
|
} else {
|
2019-07-04 01:56:18 +02:00
|
|
|
p.check_types(expr_type, tmp_typ)
|
|
|
|
// Pass tmp var info to the _PUSH macro
|
|
|
|
// Prepend tmp initialisation and push call
|
2019-08-20 13:34:29 +02:00
|
|
|
// Don't dereference if it's already a mutable array argument (`fn foo(mut []int)`)
|
2019-08-17 21:19:37 +02:00
|
|
|
push_call := if typ.contains('*'){'_PUSH('} else { '_PUSH(&'}
|
2019-07-04 01:56:18 +02:00
|
|
|
p.cgen.set_placeholder(ph, push_call)
|
|
|
|
p.gen('), $tmp, $tmp_typ)')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
return 'void'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.next()
|
|
|
|
p.gen(' << ')
|
|
|
|
p.check_types(p.expression(), typ)
|
|
|
|
return 'int'
|
|
|
|
}
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
// `a in [1, 2, 3]`
|
|
|
|
// `key in map`
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .key_in {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(' ')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.key_in)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(' ')
|
|
|
|
p.gen(', ')
|
|
|
|
arr_typ := p.expression()
|
2019-08-17 21:19:37 +02:00
|
|
|
is_map := arr_typ.starts_with('map_')
|
|
|
|
if !arr_typ.starts_with('array_') && !is_map {
|
2019-07-23 22:57:06 +02:00
|
|
|
p.error('`in` requires an array/map')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
T := p.table.find_type(arr_typ)
|
2019-07-23 22:57:06 +02:00
|
|
|
if !is_map && !T.has_method('contains') {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('$arr_typ has no method `contains`')
|
|
|
|
}
|
2019-07-23 22:57:06 +02:00
|
|
|
// `typ` is element's type
|
|
|
|
if is_map {
|
|
|
|
p.cgen.set_placeholder(ph, '_IN_MAP( ')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-07-23 22:57:06 +02:00
|
|
|
p.cgen.set_placeholder(ph, '_IN($typ, ')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(')')
|
|
|
|
return 'bool'
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .righ_shift {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
p.gen(' >> ')
|
|
|
|
p.check_types(p.expression(), typ)
|
|
|
|
return 'int'
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.tok == .dot {
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok == .dot {
|
2019-07-06 02:22:29 +02:00
|
|
|
typ = p.dot(typ, ph)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// + - |
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok == .plus || p.tok == .minus || p.tok == .pipe || p.tok == .amp || p.tok == .xor {
|
|
|
|
// for p.tok in [.plus, .minus, .pipe, .amp, .xor] {
|
2019-06-22 20:20:28 +02:00
|
|
|
tok_op := p.tok
|
|
|
|
is_num := typ == 'void*' || typ == 'byte*' || is_number_type(typ)
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check_space(p.tok)
|
2019-07-07 22:30:15 +02:00
|
|
|
if is_str && tok_op == .plus {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.cgen.set_placeholder(ph, 'string_add(')
|
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
// 3 + 4
|
|
|
|
else if is_num {
|
2019-07-24 17:46:41 +02:00
|
|
|
if typ == 'void*' {
|
2019-07-23 23:23:13 +02: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 20:20:28 +02:00
|
|
|
p.gen(tok_op.str())
|
|
|
|
}
|
|
|
|
// Vec + Vec
|
|
|
|
else {
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.pref.translated {
|
2019-06-22 20:20:28 +02: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-07-07 22:30:15 +02:00
|
|
|
if is_str && tok_op == .plus {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(')')
|
|
|
|
}
|
|
|
|
// Make sure operators are used with correct types
|
2019-06-30 22:03:17 +02:00
|
|
|
if !p.pref.translated && !is_str && !is_num {
|
2019-06-22 20:20:28 +02:00
|
|
|
T := p.table.find_type(typ)
|
2019-07-07 22:30:15 +02:00
|
|
|
if tok_op == .plus {
|
2019-06-22 20:20:28 +02: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 22:30:15 +02:00
|
|
|
else if tok_op == .minus {
|
2019-06-22 20:20:28 +02: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 13:04:57 +02:00
|
|
|
//if p.fileis('fn_test') {
|
|
|
|
//println('\nterm() $line_nr')
|
|
|
|
//}
|
2019-06-22 20:20:28 +02:00
|
|
|
typ := p.unary()
|
2019-07-24 13:04:57 +02:00
|
|
|
//if p.fileis('fn_test') {
|
|
|
|
//println('2: $line_nr')
|
|
|
|
//}
|
2019-06-22 20:20:28 +02:00
|
|
|
// `*` on a newline? Can't be multiplication, only dereference
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .mul && line_nr != p.scanner.line_nr {
|
2019-06-22 20:20:28 +02:00
|
|
|
return typ
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok == .mul || p.tok == .div || p.tok == .mod {
|
2019-06-22 20:20:28 +02:00
|
|
|
tok := p.tok
|
2019-07-07 22:30:15 +02:00
|
|
|
is_div := tok == .div
|
|
|
|
is_mod := tok == .mod
|
|
|
|
// is_mul := tok == .mod
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
p.gen(tok.str())// + ' /*op2*/ ')
|
|
|
|
p.fgen(' ' + tok.str() + ' ')
|
2019-08-11 13:47:34 +02:00
|
|
|
if (is_div || is_mod) && p.tok == .number && p.lit == '0' {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('division by zero')
|
|
|
|
}
|
|
|
|
if is_mod && (is_float_type(typ) || !is_number_type(typ)) {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.error('operator .mod requires integer types')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.check_types(p.unary(), typ)
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) unary() string {
|
|
|
|
mut typ := ''
|
|
|
|
tok := p.tok
|
|
|
|
switch tok {
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.not:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('!')
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.not)
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = 'bool'
|
|
|
|
p.bool_expression()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.bit_not:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('~')
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.bit_not)
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = p.bool_expression()
|
|
|
|
default:
|
|
|
|
typ = p.factor()
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) factor() string {
|
|
|
|
mut typ := ''
|
|
|
|
tok := p.tok
|
|
|
|
switch tok {
|
2019-07-24 00:06:48 +02:00
|
|
|
case .number:
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = 'int'
|
2019-06-26 21:44:08 +02:00
|
|
|
// Check if float (`1.0`, `1e+3`) but not if is hexa
|
2019-08-17 21:19:37 +02:00
|
|
|
if (p.lit.contains('.') || (p.lit.contains('e') || p.lit.contains('E'))) &&
|
2019-07-18 10:55:39 +02:00
|
|
|
!(p.lit[0] == `0` && (p.lit[1] == `x` || p.lit[1] == `X`)) {
|
2019-06-25 21:36:44 +02:00
|
|
|
typ = 'f32'
|
2019-08-17 21:19:37 +02:00
|
|
|
// typ = 'f64' // TODO
|
2019-07-18 10:55:39 +02:00
|
|
|
} else {
|
|
|
|
v_u64 := p.lit.u64()
|
|
|
|
if u64(u32(v_u64)) < v_u64 {
|
|
|
|
typ = 'u64'
|
|
|
|
}
|
2019-06-25 21:36:44 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02: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 21:36:44 +02:00
|
|
|
p.gen(p.lit)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(p.lit)
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.minus:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('-')
|
|
|
|
p.fgen('-')
|
|
|
|
p.next()
|
|
|
|
return p.factor()
|
|
|
|
// Variable
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_sizeof:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('sizeof(')
|
|
|
|
p.fgen('sizeof(')
|
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lpar)
|
2019-06-22 20:20:28 +02:00
|
|
|
mut sizeof_typ := p.get_type()
|
|
|
|
if sizeof_typ.ends_with('*') {
|
|
|
|
// Move * from the end to the beginning, as C requires
|
|
|
|
sizeof_typ = '*' + sizeof_typ.left(sizeof_typ.len - 1)
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rpar)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('$sizeof_typ)')
|
|
|
|
p.fgen('$sizeof_typ)')
|
|
|
|
return 'int'
|
2019-08-17 21:19:37 +02:00
|
|
|
case Token.amp, Token.dot, Token.mul:
|
|
|
|
// (dot is for enum vals: `.green`)
|
2019-06-22 20:20:28 +02:00
|
|
|
return p.name_expr()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.name:
|
2019-06-22 20:20:28 +02:00
|
|
|
// map[string]int
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.lit == 'map' && p.peek() == .lsbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
return p.map_init()
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.lit == 'json' && p.peek() == .dot {
|
2019-08-16 17:32:32 +02:00
|
|
|
if !('json' in p.table.imports) {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error('undefined: `json`, use `import json`')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
return p.js_decode()
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
//if p.fileis('orm_test') {
|
|
|
|
//println('ORM name: $p.lit')
|
|
|
|
//}
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = p.name_expr()
|
|
|
|
return typ
|
2019-08-17 21:19:37 +02:00
|
|
|
case Token.key_default:
|
2019-06-22 20:20:28 +02: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-07-07 22:30:15 +02:00
|
|
|
case Token.lpar:
|
2019-07-10 13:27:35 +02:00
|
|
|
//p.gen('(/*lpar*/')
|
|
|
|
p.gen('(')
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.lpar)
|
2019-06-22 20:20:28 +02: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 22:30:15 +02:00
|
|
|
p.check(.rpar)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.ptr_cast = false
|
|
|
|
p.gen(')')
|
|
|
|
return typ
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.chartoken:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.char_expr()
|
|
|
|
typ = 'byte'
|
|
|
|
return typ
|
2019-07-16 17:59:07 +02:00
|
|
|
case Token.str:
|
2019-06-22 20:20:28 +02:00
|
|
|
p.string_expr()
|
|
|
|
typ = 'string'
|
|
|
|
return typ
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_false:
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = 'bool'
|
|
|
|
p.gen('0')
|
|
|
|
p.fgen('false')
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_true:
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = 'bool'
|
|
|
|
p.gen('1')
|
|
|
|
p.fgen('true')
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.lsbr:
|
2019-06-22 20:20:28 +02: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-07-07 22:30:15 +02:00
|
|
|
case Token.lcbr:
|
2019-08-17 21:19:37 +02:00
|
|
|
// `m := { 'one': 1 }`
|
2019-08-03 09:44:08 +02:00
|
|
|
if p.peek() == .str {
|
2019-08-17 21:19:37 +02:00
|
|
|
return p.map_init()
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// { user | name :'new name' }
|
|
|
|
return p.assoc()
|
2019-07-07 22:30:15 +02:00
|
|
|
case Token.key_if:
|
2019-07-12 06:54:01 +02:00
|
|
|
typ = p.if_st(true, 0)
|
2019-06-22 20:20:28 +02:00
|
|
|
return typ
|
|
|
|
default:
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.pref.is_verbose || p.pref.is_debug {
|
2019-08-17 19:21:59 +02:00
|
|
|
next := p.peek()
|
|
|
|
println('prev=${p.prev_tok.str()}')
|
|
|
|
println('next=${next.str()}')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('unexpected token: `${p.tok.str()}`')
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
if !p.cur_fn.known_var(name) {
|
|
|
|
p.error('unknown variable `$name`')
|
|
|
|
}
|
|
|
|
var := p.cur_fn.find_var(name)
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.pipe)
|
2019-06-22 20:20:28 +02: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 22:30:15 +02:00
|
|
|
for p.tok != .rcbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
field := p.check_name()
|
|
|
|
fields << field
|
|
|
|
p.gen('.$field = ')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.colon)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.bool_expression()
|
|
|
|
p.gen(',')
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok != .rcbr {
|
|
|
|
p.check(.comma)
|
2019-06-22 20:20:28 +02: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 22:30:15 +02:00
|
|
|
p.check(.rcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('}')
|
|
|
|
return var.typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) char_expr() {
|
|
|
|
p.gen('\'$p.lit\'')
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-07 08:19:27 +02:00
|
|
|
fn format_str(_str string) string {
|
|
|
|
mut str := _str.replace('"', '\\"')
|
2019-07-04 21:51:59 +02:00
|
|
|
$if windows {
|
|
|
|
str = str.replace('\r\n', '\\n')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-07-04 21:51:59 +02:00
|
|
|
str = str.replace('\n', '\\n')
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
fn (p mut Parser) string_expr() {
|
|
|
|
str := p.lit
|
2019-07-16 17:59:07 +02:00
|
|
|
// No ${}, just return a simple string
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.peek() != .dollar {
|
2019-07-16 17:59:07 +02:00
|
|
|
p.fgen('\'$str\'')
|
2019-06-22 20:20:28 +02:00
|
|
|
f := format_str(str)
|
2019-08-17 21:19:37 +02:00
|
|
|
// `C.puts('hi')` => `puts("hi");`
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.calling_c || (p.pref.translated && p.mod == 'main') {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('"$f"')
|
|
|
|
}
|
2019-08-09 18:10:59 +02:00
|
|
|
else if p.is_sql {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.gen('\'$str\'')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
else {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.gen('tos2((byte*)"$f")')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// tmp := p.get_tmp()
|
2019-08-17 21:19:37 +02:00
|
|
|
p.is_alloc = true // $ interpolation means there's allocation
|
2019-06-22 20:20:28 +02:00
|
|
|
mut args := '"'
|
|
|
|
mut format := '"'
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fgen('\'')
|
|
|
|
mut complex_inter := false // for vfmt
|
2019-07-16 17:59:07 +02:00
|
|
|
for p.tok == .str {
|
2019-06-22 20:20:28 +02:00
|
|
|
// Add the string between %d's
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fgen(p.lit)
|
2019-07-15 14:05:11 +02:00
|
|
|
p.lit = p.lit.replace('%', '%%')
|
2019-06-22 20:20:28 +02:00
|
|
|
format += format_str(p.lit)
|
|
|
|
p.next()// skip $
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok != .dollar {
|
2019-06-22 20:20:28 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
// Handle .dollar
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.dollar)
|
2019-07-16 17:59:07 +02:00
|
|
|
// If there's no string after current token, it means we are in
|
2019-08-17 21:19:37 +02:00
|
|
|
// a complex expression (`${...}`)
|
2019-07-16 17:59:07 +02:00
|
|
|
if p.peek() != .str {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fgen('{')
|
|
|
|
complex_inter = true
|
|
|
|
}
|
2019-06-22 20:20:28 +02: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 21:19:37 +02:00
|
|
|
if typ == 'bool' {
|
2019-08-09 18:10:59 +02:00
|
|
|
//args += '.len, ${val}.str'
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// Custom format? ${t.hour:02d}
|
2019-07-07 22:30:15 +02:00
|
|
|
custom := p.tok == .colon
|
2019-06-22 20:20:28 +02:00
|
|
|
if custom {
|
|
|
|
format += '%'
|
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .dot {
|
2019-06-22 20:20:28 +02:00
|
|
|
format += '.'
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
format += p.lit// 02
|
|
|
|
p.next()
|
|
|
|
format += p.lit// f
|
|
|
|
// println('custom str F=$format')
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
else {
|
2019-08-17 21:19:37 +02:00
|
|
|
f := p.typ_to_fmt(typ, 0)
|
|
|
|
if f == '' {
|
2019-08-22 06:56:29 +02:00
|
|
|
is_array := typ.starts_with('array_')
|
|
|
|
has_str_method := p.table.type_has_method(p.table.find_type(typ), 'str')
|
|
|
|
if is_array || has_str_method {
|
|
|
|
if is_array && !has_str_method {
|
|
|
|
p.gen_array_str(mut p.table.find_type(typ))
|
|
|
|
}
|
|
|
|
args = args.all_before_last(val) + '${typ}_str(${val}).len, ${typ}_str(${val}).str'
|
|
|
|
format += '%.*s '
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('unhandled sprintf format "$typ" ')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
|
|
|
format += f
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-16 17:59:07 +02:00
|
|
|
if complex_inter {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fgen('}')
|
|
|
|
}
|
|
|
|
p.fgen('\'')
|
2019-06-22 20:20:28 +02:00
|
|
|
// println("hello %d", num) optimization.
|
|
|
|
if p.cgen.nogen {
|
|
|
|
return
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
// println: don't allocate a new string, just print it.
|
2019-07-31 09:51:24 +02:00
|
|
|
$if !windows {
|
|
|
|
cur_line := p.cgen.cur_line.trim_space()
|
|
|
|
if cur_line.contains('println (') && p.tok != .plus &&
|
|
|
|
!cur_line.contains('string_add') && !cur_line.contains('eprintln') {
|
|
|
|
p.cgen.resetln(cur_line.replace('println (', 'printf('))
|
|
|
|
p.gen('$format\\n$args')
|
|
|
|
return
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// '$age'! means the user wants this to be a tmp string (uses global buffer, no allocation,
|
|
|
|
// won't be used again)
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .not {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.not)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('_STR_TMP($format$args)')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Otherwise do ugly len counting + allocation + sprintf
|
|
|
|
p.gen('_STR($format$args)')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// m := map[string]int{}
|
2019-08-17 21:19:37 +02:00
|
|
|
// m := { 'one': 1 }
|
2019-06-22 20:20:28 +02:00
|
|
|
fn (p mut Parser) map_init() string {
|
2019-08-17 21:19:37 +02: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 09:44:08 +02:00
|
|
|
if p.tok == .lcbr {
|
|
|
|
p.check(.lcbr)
|
2019-08-17 21:19:37 +02:00
|
|
|
mut i := 0
|
|
|
|
for {
|
|
|
|
key := p.lit
|
|
|
|
keys_gen += 'tos2("$key"), '
|
|
|
|
p.check(.str)
|
|
|
|
p.check(.colon)
|
|
|
|
p.cgen.start_tmp()
|
|
|
|
t := p.bool_expression()
|
|
|
|
if i == 0 {
|
|
|
|
val_type = t
|
|
|
|
}
|
|
|
|
i++
|
2019-08-03 09:44:08 +02: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 21:19:37 +02:00
|
|
|
val_expr := p.cgen.end_tmp()
|
|
|
|
vals_gen += '$val_expr, '
|
2019-08-03 09:44:08 +02:00
|
|
|
if p.tok == .rcbr {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.rcbr)
|
|
|
|
break
|
|
|
|
}
|
2019-08-03 09:44:08 +02:00
|
|
|
if p.tok == .comma {
|
|
|
|
p.check(.comma)
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-03 10:26:36 +02:00
|
|
|
p.gen('new_map_init($i, sizeof($val_type), ' +
|
2019-08-17 21:19:37 +02:00
|
|
|
'(string[]){ $keys_gen }, ($val_type []){ $vals_gen } )')
|
|
|
|
typ := 'map_$val_type'
|
|
|
|
return typ
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lsbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
key_type := p.check_name()
|
|
|
|
if key_type != 'string' {
|
|
|
|
p.error('only string key maps allowed for now')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rsbr)
|
2019-08-16 17:44:35 +02: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 12:33:18 +02:00
|
|
|
typ := 'map_$val_type'
|
|
|
|
p.register_map(typ)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('new_map(1, sizeof($val_type))')
|
2019-08-16 17:44:35 +02:00
|
|
|
if p.tok == .lcbr {
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.check(.rcbr)
|
|
|
|
println('warning: $p.file_name:$p.scanner.line_nr ' +
|
2019-08-17 21:19:37 +02:00
|
|
|
'initializaing maps no longer requires `{}`')
|
|
|
|
}
|
2019-07-15 12:33:18 +02:00
|
|
|
return typ
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-08-17 21:19:37 +02:00
|
|
|
// `nums := [1, 2, 3]`
|
2019-06-22 20:20:28 +02:00
|
|
|
fn (p mut Parser) array_init() string {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.is_alloc = true
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lsbr)
|
2019-08-17 21:19:37 +02: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-08-04 03:59:12 +02:00
|
|
|
if p.tok == .name {
|
2019-08-17 21:19:37 +02:00
|
|
|
c := p.table.find_const(p.prepend_mod(p.lit))
|
2019-08-04 03:59:12 +02:00
|
|
|
if c.name != '' && c.typ == 'int' && p.peek() == .rsbr && !p.inside_const {
|
2019-08-17 21:19:37 +02:00
|
|
|
is_integer = true
|
|
|
|
is_const_len = true
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
lit := p.lit
|
|
|
|
mut typ := ''
|
|
|
|
new_arr_ph := p.cgen.add_placeholder()
|
|
|
|
mut i := 0
|
|
|
|
pos := p.cgen.cur_line.len// remember cur line to fetch first number in cgen for [0; 10]
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok != .rsbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
val_typ := p.bool_expression()
|
|
|
|
// Get type of the first expression
|
|
|
|
if i == 0 {
|
|
|
|
typ = val_typ
|
|
|
|
// fixed width array initialization? (`arr := [20]byte`)
|
2019-07-07 22:30:15 +02:00
|
|
|
if is_integer && p.tok == .rsbr && p.peek() == .name {
|
2019-06-22 20:20:28 +02:00
|
|
|
nextc := p.scanner.text[p.scanner.pos + 1]
|
|
|
|
// TODO whitespace hack
|
|
|
|
// Make sure there's no space in `[10]byte`
|
|
|
|
if !nextc.is_space() {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rsbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
name := p.check_name()
|
|
|
|
if p.table.known_type(name) {
|
2019-07-18 20:21:23 +02:00
|
|
|
p.cgen.resetln('')
|
2019-07-24 17:46:41 +02:00
|
|
|
p.gen('STRUCT_DEFAULT_VALUE')
|
2019-08-17 21:19:37 +02:00
|
|
|
if is_const_len {
|
|
|
|
return '[${p.mod}__$lit]$name'
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
return '[$lit]$name'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('bad type `$name`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 22:30:15 +02:00
|
|
|
if p.tok != .rsbr && p.tok != .semicolon {
|
2019-07-03 13:20:43 +02:00
|
|
|
p.gen(', ')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.comma)
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fspace()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
// Repeat (a = [0;5] )
|
2019-07-07 22:30:15 +02:00
|
|
|
if i == 1 && p.tok == .semicolon {
|
|
|
|
p.check_space(.semicolon)
|
2019-06-22 20:20:28 +02:00
|
|
|
val := p.cgen.cur_line.right(pos)
|
|
|
|
// p.cgen.cur_line = ''
|
2019-07-18 20:21:23 +02:00
|
|
|
p.cgen.resetln(p.cgen.cur_line.left(pos))
|
2019-06-22 20:20:28 +02:00
|
|
|
// Special case for zero
|
|
|
|
if false && val.trim_space() == '0' {
|
|
|
|
p.gen('array_repeat( & V_ZERO, ')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tmp := p.get_tmp()
|
2019-06-22 22:12:38 +02:00
|
|
|
p.cgen.insert_before('$typ $tmp = $val;')
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('array_repeat(&$tmp, ')
|
|
|
|
}
|
|
|
|
p.check_types(p.bool_expression(), 'int')
|
|
|
|
p.gen(', sizeof($typ) )')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rsbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
return 'array_$typ'
|
|
|
|
}
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rsbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
// type after `]`? (e.g. "[]string")
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok != .name && i == 0 {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('specify array type: `[]typ` instead of `[]`')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .name && i == 0 {
|
2019-06-22 20:20:28 +02:00
|
|
|
// vals.len == 0 {
|
|
|
|
typ = p.get_type()
|
2019-07-07 22:30:15 +02:00
|
|
|
// println('.key_goT TYP after [] $typ')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// ! after array => no malloc and no copy
|
2019-08-17 21:19:37 +02:00
|
|
|
no_alloc := p.tok == .not
|
2019-06-23 13:17:33 +02:00
|
|
|
if no_alloc {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
// [1,2,3]!! => [3]int{1,2,3}
|
2019-08-17 21:19:37 +02:00
|
|
|
is_fixed_size := p.tok == .not
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_fixed_size {
|
|
|
|
p.next()
|
|
|
|
p.gen(' }')
|
2019-07-29 18:21:36 +02:00
|
|
|
if !p.first_pass() {
|
2019-06-22 20:20:28 +02: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 {
|
|
|
|
p.cgen.set_placeholder(new_arr_ph, '{ ')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.cgen.set_placeholder(new_arr_ph, '($typ[]) { ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '[$i]$typ'
|
|
|
|
}
|
|
|
|
// if ptr {
|
|
|
|
// typ += '_ptr"
|
|
|
|
// }
|
2019-06-22 22:12:38 +02:00
|
|
|
mut new_arr := 'new_array_from_c_array'
|
2019-06-23 13:17:33 +02:00
|
|
|
if no_alloc {
|
|
|
|
new_arr += '_no_alloc'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.gen(' })')
|
|
|
|
// p.gen('$new_arr($vals.len, $vals.len, sizeof($typ), ($typ[]) $c_arr );')
|
2019-07-29 18:21:36 +02:00
|
|
|
// TODO why need !first_pass()?? Otherwise it goes to the very top of the out.c file
|
|
|
|
if !p.first_pass() {
|
2019-07-24 17:46:41 +02:00
|
|
|
if i == 0 {
|
|
|
|
p.cgen.set_placeholder(new_arr_ph, '$new_arr($i, $i, sizeof($typ), ($typ[]) {EMPTY_STRUCT_INIT ')
|
2019-07-23 23:23:13 +02:00
|
|
|
} else {
|
|
|
|
p.cgen.set_placeholder(new_arr_ph, '$new_arr($i, $i, sizeof($typ), ($typ[]) { ')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
typ = 'array_$typ'
|
|
|
|
p.register_array(typ)
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2019-07-29 18:21:36 +02:00
|
|
|
fn (p mut Parser) struct_init(typ string, is_c_struct_init bool) string {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.is_struct_init = true
|
2019-08-20 13:34:29 +02:00
|
|
|
t := p.table.find_type(typ)
|
|
|
|
// TODO hack. If it's a C type, we may need to add struct before declaration:
|
|
|
|
// a := &C.A{} ==> struct A* a = malloc(sizeof(struct A));
|
|
|
|
if is_c_struct_init { // && t.cat != .c_typedef {
|
|
|
|
p.is_c_struct_init = true
|
|
|
|
if t.cat != .c_typedef {
|
|
|
|
p.cgen.insert_before('struct /*c struct init*/')
|
|
|
|
}
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
p.next()
|
2019-06-22 20:20:28 +02:00
|
|
|
p.scanner.fmt_out.cut(typ.len)
|
|
|
|
ptr := typ.contains('*')
|
2019-06-27 19:02:47 +02:00
|
|
|
// TODO tm struct struct bug
|
|
|
|
if typ == 'tm' {
|
|
|
|
p.cgen.lines[p.cgen.lines.len-1] = ''
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lcbr)
|
2019-08-20 13:34:29 +02:00
|
|
|
// `user := User{foo:bar}` => `User user = (User){ .foo = bar}`
|
2019-06-22 20:20:28 +02:00
|
|
|
if !ptr {
|
2019-06-27 17:48:49 +02:00
|
|
|
if p.is_c_struct_init {
|
2019-08-20 13:34:29 +02:00
|
|
|
// `face := C.FT_Face{}` => `FT_Face face;`
|
|
|
|
if p.tok == .rcbr {
|
|
|
|
p.is_empty_c_struct_init = true
|
|
|
|
p.check(.rcbr)
|
|
|
|
return typ
|
|
|
|
}
|
2019-06-27 19:02:47 +02:00
|
|
|
p.gen('(struct $typ) {')
|
2019-06-27 17:48:49 +02:00
|
|
|
p.is_c_struct_init = false
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-06-27 19:02:47 +02:00
|
|
|
p.gen('($typ) {')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO tmp hack for 0 pointers init
|
|
|
|
// &User{!} ==> 0
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .not {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
p.gen('0')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
return typ
|
|
|
|
}
|
|
|
|
no_star := typ.replace('*', '')
|
|
|
|
p.gen('ALLOC_INIT($no_star, {')
|
|
|
|
}
|
2019-07-23 23:23:13 +02:00
|
|
|
mut did_gen_something := false
|
2019-06-22 20:20:28 +02:00
|
|
|
// Loop thru all struct init keys and assign values
|
|
|
|
// u := User{age:20, name:'bob'}
|
|
|
|
// Remember which fields were set, so that we dont have to zero them later
|
|
|
|
mut inited_fields := []string
|
|
|
|
peek := p.peek()
|
2019-07-07 22:30:15 +02:00
|
|
|
if peek == .colon || p.tok == .rcbr {
|
|
|
|
for p.tok != .rcbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
field := p.check_name()
|
|
|
|
if !t.has_field(field) {
|
|
|
|
p.error('`$t.name` has no field `$field`')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
f := t.find_field(field)
|
2019-06-22 20:20:28 +02:00
|
|
|
inited_fields << field
|
|
|
|
p.gen('.$field = ')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.colon)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fspace()
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check_types(p.bool_expression(), f.typ)
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .comma {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok != .rcbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
p.fgenln('')
|
2019-07-23 23:23:13 +02:00
|
|
|
did_gen_something = true
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// If we already set some fields, need to prepend a comma
|
|
|
|
if t.fields.len != inited_fields.len && inited_fields.len > 0 {
|
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
// Zero values: init all fields (ints to 0, strings to '' etc)
|
|
|
|
for i, field in t.fields {
|
|
|
|
// println('### field.name')
|
|
|
|
// Skip if this field has already been assigned to
|
|
|
|
if inited_fields.contains(field.name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
field_typ := field.typ
|
2019-08-05 09:49:52 +02:00
|
|
|
if !p.builtin_mod && field_typ.ends_with('*') && field_typ.contains('Cfg') {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('pointer field `${typ}.${field.name}` must be initialized')
|
|
|
|
}
|
2019-08-14 11:35:51 +02:00
|
|
|
// init map fields
|
|
|
|
if field_typ.starts_with('map_') {
|
|
|
|
p.gen('.$field.name = new_map(1, sizeof( ${field_typ.right(4)} ))')
|
|
|
|
inited_fields << field.name
|
|
|
|
if i != t.fields.len - 1 {
|
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
did_gen_something = true
|
|
|
|
continue
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
def_val := type_default(field_typ)
|
2019-07-24 17:46:41 +02:00
|
|
|
if def_val != '' && def_val != 'STRUCT_DEFAULT_VALUE' {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('.$field.name = $def_val')
|
|
|
|
if i != t.fields.len - 1 {
|
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
}
|
2019-07-23 23:23:13 +02:00
|
|
|
did_gen_something = true
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Point{3,4} syntax
|
|
|
|
else {
|
|
|
|
mut T := p.table.find_type(typ)
|
|
|
|
// Aliases (TODO Hack, implement proper aliases)
|
|
|
|
if T.fields.len == 0 && T.parent != '' {
|
|
|
|
T = p.table.find_type(T.parent)
|
|
|
|
}
|
|
|
|
for i, ffield in T.fields {
|
|
|
|
expr_typ := p.bool_expression()
|
|
|
|
if !p.check_types_no_throw(expr_typ, ffield.typ) {
|
|
|
|
p.error('field value #${i+1} `$ffield.name` has type `$ffield.typ`, got `$expr_typ` ')
|
|
|
|
}
|
|
|
|
if i < T.fields.len - 1 {
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok != .comma {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('too few values in `$typ` literal (${i+1} instead of $T.fields.len)')
|
|
|
|
}
|
|
|
|
p.gen(',')
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Allow `user := User{1,2,3,}`
|
|
|
|
// The final comma will be removed by vfmt, since we are not calling `p.fgen()`
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .comma {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok != .rcbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('too many fields initialized: `$typ` has $T.fields.len field(s)')
|
|
|
|
}
|
2019-07-23 23:23:13 +02:00
|
|
|
did_gen_something = true
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-23 23:23:13 +02:00
|
|
|
|
2019-07-24 17:46:41 +02:00
|
|
|
if !did_gen_something {
|
|
|
|
p.gen('EMPTY_STRUCT_INIT')
|
2019-07-23 23:23:13 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('}')
|
|
|
|
if ptr {
|
|
|
|
p.gen(')')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.is_struct_init = false
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
// `f32(3)`
|
|
|
|
// tok is `f32` or `)` if `(*int)(ptr)`
|
|
|
|
fn (p mut Parser) cast(typ string) string {
|
|
|
|
p.next()
|
2019-06-27 12:27:46 +02:00
|
|
|
pos := p.cgen.add_placeholder()
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .rpar {
|
2019-06-22 20:20:28 +02:00
|
|
|
// skip `)` if it's `(*int)(ptr)`, not `int(a)`
|
|
|
|
p.ptr_cast = true
|
|
|
|
p.next()
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lpar)
|
2019-08-17 21:19:37 +02:00
|
|
|
p.expected_type = typ
|
2019-06-22 20:20:28 +02:00
|
|
|
expr_typ := p.bool_expression()
|
2019-08-20 13:34:29 +02:00
|
|
|
// `face := FT_Face(cobj)` => `FT_Face face = *((FT_Face*)cobj);`
|
|
|
|
casting_voidptr_to_value := expr_typ == 'void*' && typ != 'int' &&
|
|
|
|
typ != 'byteptr' && !typ.ends_with('*')
|
2019-08-17 21:19:37 +02:00
|
|
|
p.expected_type = ''
|
2019-06-27 12:27:46 +02:00
|
|
|
// `string(buffer)` => `tos2(buffer)`
|
2019-07-18 04:42:00 +02:00
|
|
|
// `string(buffer, len)` => `tos(buffer, len)`
|
2019-07-20 22:03:38 +02:00
|
|
|
// `string(bytes_array, len)` => `tos(bytes_array.data, len)`
|
2019-08-17 21:19:37 +02:00
|
|
|
is_byteptr := expr_typ == 'byte*' || expr_typ == 'byteptr'
|
|
|
|
is_bytearr := expr_typ == 'array_byte'
|
2019-07-21 00:04:34 +02:00
|
|
|
if typ == 'string' {
|
2019-08-17 21:19:37 +02:00
|
|
|
if is_byteptr || is_bytearr {
|
|
|
|
if p.tok == .comma {
|
|
|
|
p.check(.comma)
|
2019-07-21 00:04:34 +02:00
|
|
|
p.cgen.set_placeholder(pos, 'tos(')
|
|
|
|
if is_bytearr {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.gen('.data')
|
|
|
|
}
|
|
|
|
p.gen(', ')
|
|
|
|
p.check_types(p.expression(), 'int')
|
|
|
|
} else {
|
2019-07-21 00:04:34 +02:00
|
|
|
if is_bytearr {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.gen('.data')
|
|
|
|
}
|
2019-07-21 00:04:34 +02:00
|
|
|
p.cgen.set_placeholder(pos, 'tos2(')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-21 00:04:34 +02:00
|
|
|
// `string(234)` => error
|
|
|
|
else if expr_typ == 'int' {
|
|
|
|
p.error('cannot cast `$expr_typ` to `$typ`, use `str()` method instead')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-07-21 00:04:34 +02:00
|
|
|
else {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error('cannot cast `$expr_typ` to `$typ`')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-13 18:28:52 +02:00
|
|
|
else if typ == 'byte' && expr_typ == 'string' {
|
|
|
|
p.error('cannot cast `$expr_typ` to `$typ`, use backquotes `` to create a `$typ` or access the value of an index of `$expr_typ` using []')
|
|
|
|
}
|
2019-08-20 13:34:29 +02:00
|
|
|
else if casting_voidptr_to_value {
|
|
|
|
p.cgen.set_placeholder(pos, '*($typ*)(')
|
|
|
|
}
|
2019-06-27 12:27:46 +02:00
|
|
|
else {
|
|
|
|
p.cgen.set_placeholder(pos, '($typ)(')
|
|
|
|
}
|
2019-07-18 04:42:00 +02:00
|
|
|
p.check(.rpar)
|
2019-06-27 12:27:46 +02:00
|
|
|
p.gen(')')
|
2019-06-22 20:20:28 +02:00
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-12 06:54:01 +02:00
|
|
|
fn (p mut Parser) if_st(is_expr bool, elif_depth int) string {
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_expr {
|
2019-08-09 18:10:59 +02:00
|
|
|
//if p.fileis('if_expr') {
|
|
|
|
//println('IF EXPR')
|
|
|
|
//}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.inside_if_expr = true
|
|
|
|
p.gen('(')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen('if (')
|
|
|
|
p.fgen('if ')
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
p.check_types(p.bool_expression(), 'bool')
|
|
|
|
if is_expr {
|
|
|
|
p.gen(') ? (')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln(') {')
|
|
|
|
}
|
|
|
|
p.fgen(' ')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
mut typ := ''
|
|
|
|
// if { if hack
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .key_if && p.inside_if_expr {
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = p.factor()
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
typ = p.statements()
|
|
|
|
}
|
2019-08-08 09:49:56 +02:00
|
|
|
if_returns := p.returns
|
|
|
|
p.returns = false
|
2019-06-22 20:20:28 +02:00
|
|
|
// println('IF TYp=$typ')
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .key_else {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fgenln('')
|
|
|
|
p.check(.key_else)
|
|
|
|
p.fspace()
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .key_if {
|
2019-07-12 05:43:21 +02:00
|
|
|
if is_expr {
|
|
|
|
p.gen(') : (')
|
2019-08-09 08:17:31 +02: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 05:43:21 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen(' else ')
|
2019-08-09 08:17:31 +02:00
|
|
|
nested := p.if_st(is_expr, 0)
|
|
|
|
nested_returns := p.returns
|
|
|
|
p.returns = if_returns && nested_returns
|
|
|
|
return nested
|
2019-07-12 05:43:21 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// return ''
|
|
|
|
}
|
|
|
|
if is_expr {
|
|
|
|
p.gen(') : (')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln(' else { ')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
// statements() returns the type of the last statement
|
|
|
|
typ = p.statements()
|
|
|
|
p.inside_if_expr = false
|
|
|
|
if is_expr {
|
2019-07-12 06:54:01 +02:00
|
|
|
p.gen(strings.repeat(`)`, elif_depth + 1))
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-09 08:17:31 +02:00
|
|
|
else_returns := p.returns
|
|
|
|
p.returns = if_returns && else_returns
|
2019-06-22 20:20:28 +02: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 22:30:15 +02:00
|
|
|
p.check(.key_for)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(' ')
|
|
|
|
p.for_expr_cnt++
|
|
|
|
next_tok := p.peek()
|
2019-07-12 07:23:16 +02:00
|
|
|
//debug := p.scanner.file_path.contains('r_draw')
|
2019-06-22 20:20:28 +02:00
|
|
|
p.cur_fn.open_scope()
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .lcbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
// Infinite loop
|
|
|
|
p.gen('while (1) {')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
else if p.tok == .key_mut {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('`mut` is not required in for loops')
|
|
|
|
}
|
|
|
|
// for i := 0; i < 10; i++ {
|
2019-07-07 22:30:15 +02:00
|
|
|
else if next_tok == .decl_assign || next_tok == .assign || p.tok == .semicolon {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.genln('for (')
|
2019-07-07 22:30:15 +02:00
|
|
|
if next_tok == .decl_assign {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.var_decl()
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
else if p.tok != .semicolon {
|
2019-06-22 20:20:28 +02:00
|
|
|
// allow `for ;; i++ {`
|
|
|
|
// Allow `for i = 0; i < ...`
|
|
|
|
p.statement(false)
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.semicolon)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(' ; ')
|
|
|
|
p.fgen(' ')
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok != .semicolon {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.bool_expression()
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.semicolon)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(' ; ')
|
|
|
|
p.fgen(' ')
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok != .lcbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.statement(false)
|
|
|
|
}
|
|
|
|
p.genln(') { ')
|
|
|
|
}
|
|
|
|
// for i, val in array
|
2019-07-07 22:30:15 +02:00
|
|
|
else if p.peek() == .comma {
|
2019-06-22 20:20:28 +02:00
|
|
|
// for i, val in array { ==>
|
2019-08-17 21:19:37 +02:00
|
|
|
//
|
2019-06-22 20:20:28 +02:00
|
|
|
// array_int tmp = array;
|
|
|
|
// for (int i = 0; i < tmp.len; i++) {
|
|
|
|
// int val = tmp[i];
|
|
|
|
i := p.check_name()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.comma)
|
2019-06-22 20:20:28 +02:00
|
|
|
val := p.check_name()
|
|
|
|
p.fgen(' ')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.key_in)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(' ')
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
p.cgen.start_tmp()
|
|
|
|
typ := p.bool_expression()
|
2019-08-17 21:19:37 +02:00
|
|
|
is_arr := typ.starts_with('array_')
|
|
|
|
is_map := typ.starts_with('map_')
|
|
|
|
is_str := typ == 'string'
|
|
|
|
if !is_arr && !is_str && !is_map {
|
|
|
|
p.error('cannot range over type `$typ`')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
expr := p.cgen.end_tmp()
|
|
|
|
p.genln('$typ $tmp = $expr ;')
|
2019-08-17 21:19:37 +02:00
|
|
|
pad := if is_arr { 6 } else { 4 }
|
2019-07-12 07:23:16 +02:00
|
|
|
var_typ := typ.right(pad)
|
2019-06-22 20:20:28 +02:00
|
|
|
// typ = strings.Replace(typ, "_ptr", "*", -1)
|
|
|
|
// Register temp var
|
|
|
|
val_var := Var {
|
|
|
|
name: val
|
|
|
|
typ: var_typ
|
|
|
|
ptr: typ.contains('*')
|
|
|
|
}
|
|
|
|
p.register_var(val_var)
|
2019-08-17 21:19:37 +02:00
|
|
|
if is_arr || is_str {
|
2019-07-12 07:23:16 +02:00
|
|
|
i_var := Var {
|
|
|
|
name: i
|
|
|
|
typ: 'int'
|
|
|
|
// parent_fn: p.cur_fn
|
|
|
|
is_mut: true
|
2019-08-17 21:19:37 +02:00
|
|
|
is_changed: true
|
2019-07-12 07:23:16 +02:00
|
|
|
}
|
|
|
|
p.register_var(i_var)
|
|
|
|
p.genln(';\nfor (int $i = 0; $i < $tmp .len; $i ++) {')
|
|
|
|
p.genln('$var_typ $val = (($var_typ *) $tmp . data)[$i];')
|
|
|
|
}
|
|
|
|
else if is_map {
|
|
|
|
i_var := Var {
|
|
|
|
name: i
|
|
|
|
typ: 'string'
|
|
|
|
is_mut: true
|
2019-08-17 21:19:37 +02:00
|
|
|
is_changed: true
|
2019-07-12 07:23:16 +02:00
|
|
|
}
|
|
|
|
p.register_var(i_var)
|
2019-08-17 21:19:37 +02:00
|
|
|
p.genln('array_string keys_$tmp = map_keys(& $tmp ); ')
|
|
|
|
p.genln('for (int l = 0; l < keys_$tmp .len; l++) {')
|
|
|
|
p.genln(' string $i = ((string*)keys_$tmp .data)[l];')
|
|
|
|
//p.genln(' string $i = *(string*) ( array__get(keys_$tmp, l) );')
|
2019-07-23 23:23:13 +02:00
|
|
|
mut def := type_default(typ)
|
2019-07-24 17:46:41 +02:00
|
|
|
if def == 'STRUCT_DEFAULT_VALUE' {
|
2019-07-23 23:23:13 +02:00
|
|
|
def = '{0}'
|
|
|
|
}
|
2019-07-14 11:01:32 +02:00
|
|
|
// TODO don't call map_get() for each key, fetch values while traversing
|
2019-08-17 21:19:37 +02:00
|
|
|
// the tree (replace `map_keys()` above with `map_key_vals()`)
|
|
|
|
p.genln('$var_typ $val = $def; map_get($tmp, $i, & $val);')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// `for val in vals`
|
2019-07-07 22:30:15 +02:00
|
|
|
else if p.peek() == .key_in {
|
2019-06-22 20:20:28 +02:00
|
|
|
val := p.check_name()
|
|
|
|
p.fgen(' ')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.key_in)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fspace()
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
p.cgen.start_tmp()
|
|
|
|
typ := p.bool_expression()
|
|
|
|
expr := p.cgen.end_tmp()
|
2019-07-07 22:30:15 +02:00
|
|
|
is_range := p.tok == .dotdot
|
2019-06-22 20:20:28 +02:00
|
|
|
mut range_end := ''
|
|
|
|
if is_range {
|
|
|
|
p.check_types(typ, 'int')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check_space(.dotdot)
|
2019-06-22 20:20:28 +02: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'
|
|
|
|
if !is_arr && !is_str && !is_range {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error('cannot range over type `$typ`')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.genln('$typ $tmp = $expr;')
|
|
|
|
// TODO var_type := if...
|
|
|
|
mut var_type := ''
|
|
|
|
if is_arr {
|
|
|
|
var_type = typ.right(6)// all after `array_`
|
|
|
|
}
|
|
|
|
else if is_str {
|
|
|
|
var_type = 'byte'
|
|
|
|
}
|
|
|
|
else if is_range {
|
|
|
|
var_type = 'int'
|
|
|
|
}
|
|
|
|
// println('for typ=$typ vartyp=$var_typ')
|
|
|
|
// Register temp var
|
|
|
|
val_var := Var {
|
|
|
|
name: val
|
|
|
|
typ: var_type
|
|
|
|
ptr: typ.contains('*')
|
2019-08-17 21:19:37 +02:00
|
|
|
is_changed: true
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.register_var(val_var)
|
|
|
|
i := p.get_tmp()
|
|
|
|
if is_range {
|
|
|
|
p.genln(';\nfor (int $i = $tmp; $i < $range_end; $i++) {')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln(';\nfor (int $i = 0; $i < $tmp .len; $i ++) {')
|
|
|
|
}
|
|
|
|
if is_arr {
|
|
|
|
p.genln('$var_type $val = (($var_type *) ${tmp}.data)[$i];')
|
|
|
|
}
|
|
|
|
else if is_str {
|
|
|
|
p.genln('$var_type $val = (($var_type *) ${tmp}.str)[$i];')
|
|
|
|
}
|
|
|
|
else if is_range {
|
|
|
|
p.genln('$var_type $val = $i;')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// `for a < b {`
|
|
|
|
p.gen('while (')
|
|
|
|
p.check_types(p.bool_expression(), 'bool')
|
|
|
|
p.genln(') {')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fspace()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lcbr)
|
2019-08-17 21:19:37 +02:00
|
|
|
p.genln('')
|
2019-06-22 20:20:28 +02:00
|
|
|
p.statements()
|
2019-07-21 12:43:47 +02:00
|
|
|
p.close_scope()
|
2019-06-22 20:20:28 +02:00
|
|
|
p.for_expr_cnt--
|
2019-08-10 10:26:42 +02:00
|
|
|
p.returns = false // TODO handle loops that are guaranteed to return
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) switch_statement() {
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.tok == .key_switch {
|
|
|
|
p.check(.key_switch)
|
2019-07-17 01:43:59 +02:00
|
|
|
} else {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.key_match)
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.cgen.start_tmp()
|
|
|
|
typ := p.bool_expression()
|
|
|
|
expr := p.cgen.end_tmp()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
mut i := 0
|
2019-08-10 10:26:42 +02:00
|
|
|
mut all_cases_return := true
|
2019-08-17 21:19:37 +02:00
|
|
|
for p.tok == .key_case || p.tok == .key_default || p.peek() == .arrow || p.tok == .key_else {
|
2019-08-10 10:26:42 +02:00
|
|
|
p.returns = false
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.tok == .key_default || p.tok == .key_else {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.genln('else { // default:')
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.tok == .key_default {
|
|
|
|
p.check(.key_default)
|
2019-07-17 02:47:45 +02:00
|
|
|
p.check(.colon)
|
2019-08-17 21:19:37 +02:00
|
|
|
} else {
|
|
|
|
p.check(.key_else)
|
2019-07-17 02:47:45 +02:00
|
|
|
p.check(.arrow)
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.statements()
|
2019-08-10 10:26:42 +02:00
|
|
|
p.returns = all_cases_return && p.returns
|
|
|
|
return
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
if i > 0 {
|
|
|
|
p.gen('else ')
|
|
|
|
}
|
|
|
|
p.gen('if (')
|
|
|
|
// Multiple checks separated by comma
|
|
|
|
mut got_comma := false
|
|
|
|
for {
|
|
|
|
if got_comma {
|
|
|
|
p.gen(') || ')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
if typ == 'string' {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('string_eq($expr, ')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen('($expr == ')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.tok == .key_case || p.tok == .key_default {
|
|
|
|
p.check(p.tok)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.bool_expression()
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok != .comma {
|
2019-06-22 20:20:28 +02:00
|
|
|
break
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.comma)
|
2019-06-22 20:20:28 +02:00
|
|
|
got_comma = true
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.tok == .colon {
|
2019-07-17 01:43:59 +02:00
|
|
|
p.check(.colon)
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.check(.arrow)
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(')) {')
|
|
|
|
p.genln('/* case */')
|
|
|
|
p.statements()
|
2019-08-10 10:26:42 +02:00
|
|
|
all_cases_return = all_cases_return && p.returns
|
2019-06-22 20:20:28 +02:00
|
|
|
i++
|
|
|
|
}
|
2019-08-10 10:26:42 +02:00
|
|
|
p.returns = false // only get here when no default, so return is not guaranteed
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) assert_statement() {
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.first_pass() {
|
2019-08-17 21:19:37 +02:00
|
|
|
return
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.key_assert)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fspace()
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
p.gen('bool $tmp = ')
|
|
|
|
p.check_types(p.bool_expression(), 'bool')
|
|
|
|
// TODO print "expected: got" for failed tests
|
2019-07-31 19:01:52 +02:00
|
|
|
filename := p.file_path.replace('\\', '\\\\')
|
2019-08-17 21:19:37 +02:00
|
|
|
p.genln(';\n
|
2019-07-31 19:01:52 +02:00
|
|
|
if (!$tmp) {
|
|
|
|
println(tos2("\\x1B[31mFAILED: $p.cur_fn.name() in $filename:$p.scanner.line_nr\\x1B[0m"));
|
2019-08-17 21:19:37 +02:00
|
|
|
g_test_ok = 0 ;
|
2019-06-22 20:20:28 +02:00
|
|
|
// TODO
|
2019-08-17 21:19:37 +02:00
|
|
|
// Maybe print all vars in a test function if it fails?
|
|
|
|
}
|
|
|
|
else {
|
2019-07-21 17:59:25 +02:00
|
|
|
//puts("\\x1B[32mPASSED: $p.cur_fn.name()\\x1B[0m");
|
2019-06-22 20:20:28 +02:00
|
|
|
}')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) return_st() {
|
2019-07-15 16:15:34 +02:00
|
|
|
p.check(.key_return)
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fgen(' ')
|
2019-06-22 20:20:28 +02:00
|
|
|
fn_returns := p.cur_fn.typ != 'void'
|
|
|
|
if fn_returns {
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .rcbr {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('`$p.cur_fn.name` needs to return `$p.cur_fn.typ`')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ph := p.cgen.add_placeholder()
|
|
|
|
expr_type := p.bool_expression()
|
|
|
|
// Automatically wrap an object inside an option if the function returns an option
|
|
|
|
if p.cur_fn.typ.ends_with(expr_type) && p.cur_fn.typ.starts_with('Option_') {
|
2019-07-15 16:15:34 +02:00
|
|
|
tmp := p.get_tmp()
|
|
|
|
ret := p.cgen.cur_line.right(ph)
|
2019-08-17 21:19:37 +02:00
|
|
|
typ := expr_type.replace('Option_', '')
|
2019-08-12 17:54:28 +02:00
|
|
|
p.cgen.cur_line = '$expr_type $tmp = OPTION_CAST($typ)($ret);'
|
2019-07-24 17:46:41 +02:00
|
|
|
p.cgen.resetln('$expr_type $tmp = OPTION_CAST($expr_type)($ret);')
|
2019-08-12 17:54:28 +02:00
|
|
|
p.gen('return opt_ok(&$tmp, sizeof($typ))')
|
2019-07-15 16:15:34 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret := p.cgen.cur_line.right(ph)
|
2019-08-07 17:51:21 +02:00
|
|
|
|
|
|
|
// @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 total_text := ''
|
|
|
|
|
|
|
|
for text in p.cur_fn.defer_text {
|
|
|
|
if text != '' {
|
|
|
|
// In reverse order
|
|
|
|
total_text = text + total_text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if total_text == '' || expr_type == 'void*' {
|
2019-08-18 18:45:48 +02:00
|
|
|
if expr_type == '${p.cur_fn.typ}*' {
|
|
|
|
p.cgen.resetln('return *$ret')
|
|
|
|
} else {
|
|
|
|
p.cgen.resetln('return $ret')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
} else {
|
|
|
|
tmp := p.get_tmp()
|
2019-07-26 16:45:16 +02:00
|
|
|
p.cgen.resetln('$expr_type $tmp = $ret;\n')
|
2019-08-07 17:51:21 +02:00
|
|
|
p.genln(total_text)
|
2019-08-17 21:19:37 +02:00
|
|
|
p.genln('return $tmp;')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.check_types(expr_type, p.cur_fn.typ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Don't allow `return val` in functions that don't return anything
|
2019-08-17 21:19:37 +02:00
|
|
|
if !p.is_vweb && (p.tok == .name || p.tok == .number || p.tok == .str) {
|
2019-07-29 16:35:56 +02:00
|
|
|
p.error('function `$p.cur_fn.name` should not return a value')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-15 16:32:35 +02:00
|
|
|
|
|
|
|
if p.cur_fn.name == 'main' {
|
|
|
|
p.gen('return 0')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen('return')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.returns = true
|
|
|
|
}
|
|
|
|
|
2019-08-05 09:49:52 +02:00
|
|
|
fn prepend_mod(mod, name string) string {
|
|
|
|
return '${mod}__${name}'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-08-05 09:49:52 +02:00
|
|
|
fn (p &Parser) prepend_mod(name string) string {
|
|
|
|
return prepend_mod(p.mod, name)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) go_statement() {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.key_go)
|
2019-06-22 20:20:28 +02:00
|
|
|
// TODO copypasta of name_expr() ?
|
|
|
|
// Method
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.peek() == .dot {
|
2019-06-22 20:20:28 +02:00
|
|
|
var_name := p.lit
|
|
|
|
v := p.cur_fn.find_var(var_name)
|
|
|
|
p.cur_fn.mark_var_used(v)
|
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.dot)
|
2019-06-22 20:20:28 +02:00
|
|
|
typ := p.table.find_type(v.typ)
|
2019-07-25 13:16:17 +02:00
|
|
|
method := p.table.find_method(typ, p.lit)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.async_fn_call(method, 0, var_name, v.typ)
|
|
|
|
}
|
|
|
|
// Normal function
|
|
|
|
else {
|
|
|
|
f := p.table.find_fn(p.lit)
|
|
|
|
if f.name == 'println' {
|
|
|
|
p.error('`go` cannot be used with `println`')
|
|
|
|
}
|
|
|
|
// println(f.name)
|
|
|
|
p.async_fn_call(f, 0, '', '')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 02:44:22 +02:00
|
|
|
fn (p mut Parser) register_var(v Var) {
|
2019-06-22 20:20:28 +02:00
|
|
|
if v.line_nr == 0 {
|
2019-08-07 08:19:27 +02:00
|
|
|
//v.line_nr = p.scanner.line_nr
|
2019-08-17 21:19:37 +02:00
|
|
|
p.cur_fn.register_var({ v | line_nr: p.scanner.line_nr })
|
|
|
|
} else {
|
2019-08-07 08:19:27 +02:00
|
|
|
p.cur_fn.register_var(v)
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// user:=jsdecode(User, user_json_string)
|
|
|
|
fn (p mut Parser) js_decode() string {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.name)// json
|
|
|
|
p.check(.dot)
|
2019-06-22 20:20:28 +02:00
|
|
|
op := p.check_name()
|
|
|
|
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 22:30:15 +02:00
|
|
|
p.check(.lpar)
|
2019-06-22 20:20:28 +02:00
|
|
|
typ := p.get_type()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.comma)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.cgen.start_tmp()
|
|
|
|
p.check_types(p.bool_expression(), 'string')
|
|
|
|
expr := p.cgen.end_tmp()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rpar)
|
2019-06-22 20:20:28 +02: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 13:28:59 +02:00
|
|
|
decl += '$tmp . $field.name = OPTION_CAST($field.typ) $def_val;\n'
|
2019-06-22 20:20:28 +02: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;'
|
|
|
|
p.table.register_type(opt_type)
|
|
|
|
return opt_type
|
|
|
|
}
|
|
|
|
else if op == 'encode' {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lpar)
|
2019-06-22 20:20:28 +02: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 22:30:15 +02:00
|
|
|
p.check(.rpar)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen('json__json_print(json__jsencode_$typ($expr))')
|
|
|
|
return 'string'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('bad json op "$op"')
|
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2019-08-17 21:19:37 +02:00
|
|
|
/*
|
2019-06-27 19:02:47 +02:00
|
|
|
fn (p &Parser) building_v() bool {
|
|
|
|
cur_dir := os.getwd()
|
2019-08-17 21:19:37 +02:00
|
|
|
return p.file_path.contains('v/compiler') || cur_dir.contains('v/compiler')
|
2019-06-27 19:02:47 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
*/
|
2019-06-27 19:02:47 +02:00
|
|
|
|
2019-07-07 21:46:21 +02:00
|
|
|
fn (p mut Parser) attribute() {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lsbr)
|
2019-08-20 13:34:29 +02:00
|
|
|
if p.tok == .key_interface {
|
|
|
|
p.check(.key_interface)
|
|
|
|
p.check(.colon)
|
|
|
|
p.attr = 'interface:' + p.check_name()
|
|
|
|
} else {
|
|
|
|
p.attr = p.check_name()
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.rsbr)
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .func {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fn_decl()
|
|
|
|
p.attr = ''
|
|
|
|
return
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
else if p.tok == .key_struct {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.struct_decl()
|
|
|
|
p.attr = ''
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.error('bad attribute usage')
|
|
|
|
}
|
2019-07-07 21:46:21 +02:00
|
|
|
|
2019-07-15 22:09:34 +02:00
|
|
|
fn (p mut Parser) defer_st() {
|
|
|
|
p.check(.key_defer)
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check(.lcbr)
|
2019-07-30 18:05:06 +02:00
|
|
|
|
2019-08-17 21:19:37 +02:00
|
|
|
pos := p.cgen.lines.len
|
2019-07-30 18:05:06 +02:00
|
|
|
|
|
|
|
// Save everything inside the defer block to `defer_text`.
|
|
|
|
// It will be inserted before every `return`
|
2019-08-07 17:51:21 +02: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 21:19:37 +02:00
|
|
|
p.genln('{')
|
|
|
|
p.statements()
|
2019-08-07 17:51:21 +02:00
|
|
|
p.cur_fn.defer_text.last() = p.cgen.lines.right(pos).join('\n') + p.cur_fn.defer_text.last()
|
2019-07-30 18:05:06 +02:00
|
|
|
|
|
|
|
// Rollback p.cgen.lines
|
|
|
|
p.cgen.lines = p.cgen.lines.left(pos)
|
|
|
|
p.cgen.resetln('')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-07-15 22:09:34 +02:00
|
|
|
|