2019-10-13 16:37:43 +03:00
|
|
|
module compiler
|
2019-09-14 23:48:30 +03:00
|
|
|
|
|
|
|
import strings
|
|
|
|
|
|
|
|
const (
|
|
|
|
dot_ptr = '->'
|
|
|
|
)
|
|
|
|
|
2019-10-01 06:33:03 +03:00
|
|
|
/*
|
|
|
|
fn (p mut Parser) gen_or_else(pos int) string {
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2019-09-14 23:48:30 +03:00
|
|
|
// returns the type of the new variable
|
|
|
|
fn (p mut Parser) gen_var_decl(name string, is_static bool) string {
|
|
|
|
// Generate expression to tmp because we need its type first
|
|
|
|
// `[typ] [name] = bool_expression();`
|
|
|
|
pos := p.cgen.add_placeholder()
|
|
|
|
mut typ := p.bool_expression()
|
2019-09-30 17:11:12 +03:00
|
|
|
if typ.starts_with('...') { typ = typ.right(3) }
|
2019-09-14 23:48:30 +03:00
|
|
|
//p.gen('/*after expr*/')
|
|
|
|
// Option check ? or {
|
|
|
|
or_else := p.tok == .key_orelse
|
|
|
|
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 = ')
|
|
|
|
p.genln(';')
|
2019-10-12 17:53:53 +03:00
|
|
|
if !typ.starts_with('Option_') {
|
|
|
|
p.error('`or` block cannot be applied to non-optional type')
|
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
typ = typ.replace('Option_', '')
|
|
|
|
p.next()
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.genln('if (!$tmp .ok) {')
|
|
|
|
p.register_var(Var {
|
|
|
|
name: 'err'
|
|
|
|
typ: 'string'
|
|
|
|
is_mut: false
|
|
|
|
is_used: true
|
|
|
|
})
|
2019-10-25 22:03:42 +03:00
|
|
|
p.register_var(Var {
|
|
|
|
name: 'errcode'
|
|
|
|
typ: 'int'
|
|
|
|
is_mut: false
|
|
|
|
is_used: true
|
|
|
|
})
|
2019-09-14 23:48:30 +03:00
|
|
|
p.genln('string err = $tmp . error;')
|
2019-10-25 22:03:42 +03:00
|
|
|
p.genln('int errcode = $tmp . ecode;')
|
2019-09-14 23:48:30 +03:00
|
|
|
p.statements()
|
|
|
|
p.genln('$typ $name = *($typ*) $tmp . data;')
|
|
|
|
if !p.returns && p.prev_tok2 != .key_continue && p.prev_tok2 != .key_break {
|
2019-09-18 15:37:34 +03:00
|
|
|
p.error('`or` block must return/exit/continue/break/panic')
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
p.returns = false
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
gen_name := p.table.var_cgen_name(name)
|
|
|
|
mut nt_gen := p.table.cgen_name_type_pair(gen_name, typ)
|
|
|
|
// `foo := C.Foo{}` => `Foo foo;`
|
|
|
|
if !p.is_empty_c_struct_init && !typ.starts_with('['){
|
|
|
|
nt_gen += '='
|
2019-10-06 03:07:02 +03:00
|
|
|
} else if typ.starts_with('[') && typ[ typ.len-1 ] != `*` {
|
|
|
|
// a fixed_array initializer, like `v := [1.1, 2.2]!!`
|
|
|
|
// ... should translate to the following in C `f32 v[2] = {1.1, 2.2};`
|
|
|
|
initializer := p.cgen.cur_line.right(pos)
|
|
|
|
if initializer.len > 0 {
|
|
|
|
p.cgen.resetln(' = {' + initializer.all_after('{') )
|
2019-10-09 15:26:27 +03:00
|
|
|
} else if initializer.len == 0 {
|
|
|
|
p.cgen.resetln(' = { 0 }')
|
2019-10-12 17:53:53 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
2019-10-06 03:07:02 +03:00
|
|
|
|
2019-09-14 23:48:30 +03:00
|
|
|
if is_static {
|
|
|
|
nt_gen = 'static $nt_gen'
|
|
|
|
}
|
|
|
|
p.cgen.set_placeholder(pos, nt_gen)
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) gen_fn_decl(f Fn, typ, str_args string) {
|
2019-10-14 08:41:46 +03:00
|
|
|
dll_export_linkage := if p.pref.ccompiler == 'msvc' && p.attr == 'live' && p.pref.is_so {
|
2019-09-14 23:48:30 +03:00
|
|
|
'__declspec(dllexport) '
|
|
|
|
} else if p.attr == 'inline' {
|
|
|
|
'static inline '
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
}
|
|
|
|
fn_name_cgen := p.table.fn_gen_name(f)
|
|
|
|
//str_args := f.str_args(p.table)
|
|
|
|
p.genln('$dll_export_linkage$typ $fn_name_cgen($str_args) {')
|
|
|
|
}
|
|
|
|
|
2019-10-01 06:33:03 +03:00
|
|
|
// blank identifer assignment `_ = 111`
|
2019-09-25 14:53:44 +03:00
|
|
|
fn (p mut Parser) gen_blank_identifier_assign() {
|
2019-10-01 15:40:11 +03:00
|
|
|
assign_error_tok_idx := p.token_idx
|
2019-09-26 14:09:59 +03:00
|
|
|
p.check_name()
|
|
|
|
p.check_space(.assign)
|
2019-10-01 15:40:11 +03:00
|
|
|
is_indexer := p.peek() == .lsbr
|
2019-10-20 12:24:12 +03:00
|
|
|
is_fn_call, next_expr := p.is_next_expr_fn_call()
|
2019-09-25 14:53:44 +03:00
|
|
|
pos := p.cgen.add_placeholder()
|
|
|
|
mut typ := p.bool_expression()
|
2019-10-19 00:00:47 +03:00
|
|
|
if !is_indexer && !is_fn_call {
|
2019-10-20 12:24:12 +03:00
|
|
|
p.error_with_token_index('assigning `$next_expr` to `_` is redundant', assign_error_tok_idx)
|
2019-10-19 00:00:47 +03:00
|
|
|
}
|
2019-09-25 14:53:44 +03:00
|
|
|
tmp := p.get_tmp()
|
|
|
|
// handle or
|
|
|
|
if p.tok == .key_orelse {
|
|
|
|
p.cgen.set_placeholder(pos, '$typ $tmp = ')
|
|
|
|
p.genln(';')
|
|
|
|
typ = typ.replace('Option_', '')
|
|
|
|
p.next()
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.genln('if (!$tmp .ok) {')
|
|
|
|
p.register_var(Var {
|
|
|
|
name: 'err'
|
|
|
|
typ: 'string'
|
|
|
|
is_mut: false
|
|
|
|
is_used: true
|
|
|
|
})
|
2019-10-25 22:03:42 +03:00
|
|
|
p.register_var(Var {
|
|
|
|
name: 'errcode'
|
|
|
|
typ: 'int'
|
|
|
|
is_mut: false
|
|
|
|
is_used: true
|
|
|
|
})
|
2019-09-25 14:53:44 +03:00
|
|
|
p.genln('string err = $tmp . error;')
|
2019-10-25 22:03:42 +03:00
|
|
|
p.genln('int errcode = $tmp . ecode;')
|
2019-09-25 14:53:44 +03:00
|
|
|
p.statements()
|
|
|
|
p.returns = false
|
2019-10-01 15:40:11 +03:00
|
|
|
} else {
|
|
|
|
if is_fn_call {
|
|
|
|
p.gen(';')
|
|
|
|
} else {
|
|
|
|
p.cgen.resetln('{$typ _ = $p.cgen.cur_line;}')
|
|
|
|
}
|
2019-09-25 14:53:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 23:48:30 +03:00
|
|
|
fn types_to_c(types []Type, table &Table) string {
|
|
|
|
mut sb := strings.new_builder(10)
|
|
|
|
for t in types {
|
2019-09-21 21:38:12 +03:00
|
|
|
if t.cat != .union_ && t.cat != .struct_ && t.cat != .objc_interface {
|
2019-09-14 23:48:30 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
//if is_atomic {
|
|
|
|
//sb.write('_Atomic ')
|
|
|
|
//}
|
2019-09-21 21:38:12 +03:00
|
|
|
if t.cat == .objc_interface {
|
|
|
|
sb.writeln('@interface $t.name : $t.parent { @public')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
kind := if t.cat == .union_ {'union'} else {'struct'}
|
|
|
|
sb.writeln('$kind $t.name {')
|
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
for field in t.fields {
|
|
|
|
sb.write('\t')
|
|
|
|
sb.writeln(table.cgen_name_type_pair(field.name,
|
|
|
|
field.typ) + ';')
|
|
|
|
}
|
|
|
|
sb.writeln('};\n')
|
2019-09-21 21:38:12 +03:00
|
|
|
if t.cat == .objc_interface {
|
|
|
|
sb.writeln('@end')
|
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
return sb.str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) index_get(typ string, fn_ph int, cfg IndexCfg) {
|
|
|
|
// 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()
|
|
|
|
mut index_expr := ''
|
|
|
|
if p.cgen.is_tmp {
|
|
|
|
index_expr = p.cgen.tmp_line.right(fn_ph)
|
|
|
|
p.cgen.resetln(p.cgen.tmp_line.left(fn_ph))
|
|
|
|
} else {
|
|
|
|
index_expr = p.cgen.cur_line.right(fn_ph)
|
|
|
|
p.cgen.resetln(p.cgen.cur_line.left(fn_ph))
|
|
|
|
}
|
|
|
|
// Can't pass integer literal, because map_get() requires a void*
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
tmp_ok := p.get_tmp()
|
|
|
|
if cfg.is_map {
|
|
|
|
p.gen('$tmp')
|
|
|
|
def := type_default(typ)
|
2019-10-07 01:31:01 +03:00
|
|
|
p.cgen.insert_before('$typ $tmp = $def; ' +
|
|
|
|
'bool $tmp_ok = map_get(/*$p.file_name : $p.scanner.line_nr*/$index_expr, & $tmp);')
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
else if cfg.is_arr {
|
|
|
|
if p.pref.translated && !p.builtin_mod {
|
|
|
|
p.gen('$index_expr ]')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if cfg.is_ptr {
|
2019-10-10 01:07:00 +03:00
|
|
|
p.gen('( *($typ*) array_get(* $index_expr) )')
|
2019-09-14 23:48:30 +03:00
|
|
|
} else {
|
2019-10-10 01:07:00 +03:00
|
|
|
p.gen('( *($typ*) array_get($index_expr) )')
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if cfg.is_str && !p.builtin_mod {
|
|
|
|
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 cfg.is_map && typ == 'string' {
|
|
|
|
// p.cgen.insert_before('if (!${tmp}.str) $tmp = tos("", 0);')
|
|
|
|
p.cgen.insert_before('if (!$tmp_ok) $tmp = tos((byte *)"", 0);')
|
|
|
|
}
|
2019-10-12 17:53:53 +03:00
|
|
|
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (table mut Table) fn_gen_name(f &Fn) string {
|
|
|
|
mut name := f.name
|
|
|
|
if f.is_method {
|
|
|
|
name = '${f.receiver_typ}_$f.name'
|
|
|
|
name = name.replace(' ', '')
|
|
|
|
name = name.replace('*', '')
|
|
|
|
name = name.replace('+', 'plus')
|
|
|
|
name = name.replace('-', 'minus')
|
|
|
|
}
|
|
|
|
// Avoid name conflicts (with things like abs(), print() etc).
|
2019-09-18 15:37:34 +03:00
|
|
|
// Generate v_abs(), v_print()
|
2019-09-14 23:48:30 +03:00
|
|
|
// TODO duplicate functionality
|
|
|
|
if f.mod == 'builtin' && f.name in CReserved {
|
|
|
|
return 'v_$name'
|
|
|
|
}
|
|
|
|
// Obfuscate but skip certain names
|
|
|
|
// TODO ugly, fix
|
2019-10-06 16:23:52 +03:00
|
|
|
// NB: the order here is from faster to potentially slower checks
|
2019-10-07 01:31:01 +03:00
|
|
|
if table.obfuscate &&
|
2019-10-06 16:23:52 +03:00
|
|
|
!f.is_c &&
|
2019-10-07 01:31:01 +03:00
|
|
|
f.name != 'main' && f.name != 'WinMain' && f.name != 'main__main' &&
|
2019-10-06 16:23:52 +03:00
|
|
|
f.name != 'gg__vec2' &&
|
2019-10-07 01:31:01 +03:00
|
|
|
f.name != 'build_token_str' &&
|
|
|
|
f.name != 'build_keys' &&
|
|
|
|
f.mod != 'builtin' &&
|
|
|
|
f.mod != 'darwin' &&
|
|
|
|
f.mod != 'os' &&
|
2019-10-06 16:23:52 +03:00
|
|
|
f.mod != 'json' &&
|
2019-10-12 01:17:37 +03:00
|
|
|
!f.name.ends_with('_init') &&
|
2019-10-07 01:31:01 +03:00
|
|
|
!f.name.contains('window_proc') &&
|
|
|
|
!name.ends_with('_str') &&
|
2019-10-06 16:23:52 +03:00
|
|
|
!name.contains('contains') {
|
2019-09-14 23:48:30 +03:00
|
|
|
mut idx := table.obf_ids[name]
|
|
|
|
// No such function yet, register it
|
|
|
|
if idx == 0 {
|
|
|
|
table.fn_cnt++
|
|
|
|
table.obf_ids[name] = table.fn_cnt
|
|
|
|
idx = table.fn_cnt
|
|
|
|
}
|
|
|
|
old := name
|
|
|
|
name = 'f_$idx'
|
|
|
|
println('$old ==> $name')
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2019-10-25 21:57:32 +03:00
|
|
|
fn (p mut Parser) gen_method_call(receiver &Var, receiver_type string,
|
|
|
|
cgen_name string, ftyp string, method_ph int)
|
|
|
|
{
|
2019-09-14 23:48:30 +03:00
|
|
|
//mut cgen_name := p.table.fn_gen_name(f)
|
2019-10-25 21:32:27 +03:00
|
|
|
mut method_call := cgen_name + ' ('
|
2019-09-14 23:48:30 +03:00
|
|
|
// if receiver is key_mut or a ref (&), generate & for the first arg
|
|
|
|
if receiver.ref || (receiver.is_mut && !receiver_type.contains('*')) {
|
|
|
|
method_call += '& /* ? */'
|
|
|
|
}
|
|
|
|
// generate deref (TODO copy pasta later in fn_call_args)
|
|
|
|
if !receiver.is_mut && receiver_type.contains('*') {
|
|
|
|
method_call += '*'
|
|
|
|
}
|
|
|
|
mut cast := ''
|
|
|
|
// Method returns (void*) => cast it to int, string, user etc
|
|
|
|
// number := *(int*)numbers.first()
|
|
|
|
if ftyp == 'void*' {
|
2019-10-03 01:28:24 +03:00
|
|
|
if receiver_type.starts_with('array_') {
|
|
|
|
// array_int => int
|
2019-10-13 13:11:52 +03:00
|
|
|
cast = receiver_type.all_after('array_')
|
2019-10-03 01:28:24 +03:00
|
|
|
cast = '*($cast*) '
|
2019-10-25 21:32:27 +03:00
|
|
|
} else {
|
2019-10-03 01:28:24 +03:00
|
|
|
cast = '(voidptr) '
|
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
2019-10-25 21:57:32 +03:00
|
|
|
p.cgen.set_placeholder(method_ph, '$cast $method_call')
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) gen_array_at(typ_ string, is_arr0 bool, fn_ph int) {
|
|
|
|
mut typ := typ_
|
|
|
|
//p.fgen('[')
|
|
|
|
// array_int a; a[0]
|
|
|
|
// type is "array_int", need "int"
|
|
|
|
// typ = typ.replace('array_', '')
|
|
|
|
if is_arr0 {
|
|
|
|
typ = typ.right(6)
|
|
|
|
}
|
|
|
|
// array a; a.first() voidptr
|
|
|
|
// type is "array", need "void*"
|
|
|
|
if typ == 'array' {
|
|
|
|
typ = 'void*'
|
|
|
|
}
|
|
|
|
// No bounds check in translated from C code
|
|
|
|
if p.pref.translated && !p.builtin_mod {
|
|
|
|
// 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(',')
|
|
|
|
}
|
2019-10-12 17:53:53 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
|
|
|
|
fn (p mut Parser) gen_for_header(i, tmp, var_typ, val string) {
|
|
|
|
p.genln('for (int $i = 0; $i < ${tmp}.len; $i++) {')
|
2019-09-28 19:53:56 +03:00
|
|
|
if val == '_' { return }
|
2019-09-14 23:48:30 +03:00
|
|
|
p.genln('$var_typ $val = (($var_typ *) $tmp . data)[$i];')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) gen_for_str_header(i, tmp, var_typ, val string) {
|
|
|
|
p.genln('array_byte bytes_$tmp = string_bytes( $tmp );')
|
|
|
|
p.genln(';\nfor (int $i = 0; $i < $tmp .len; $i ++) {')
|
2019-09-28 19:53:56 +03:00
|
|
|
if val == '_' { return }
|
2019-09-14 23:48:30 +03:00
|
|
|
p.genln('$var_typ $val = (($var_typ *) bytes_$tmp . data)[$i];')
|
|
|
|
}
|
|
|
|
|
2019-09-17 22:16:16 +03:00
|
|
|
fn (p mut Parser) gen_for_range_header(i, range_end, tmp, var_type, val string) {
|
|
|
|
p.genln(';\nfor (int $i = $tmp; $i < $range_end; $i++) {')
|
2019-09-28 19:53:56 +03:00
|
|
|
if val == '_' { return }
|
2019-09-17 22:16:16 +03:00
|
|
|
p.genln('$var_type $val = $i;')
|
|
|
|
}
|
|
|
|
|
2019-09-14 23:48:30 +03:00
|
|
|
fn (p mut Parser) gen_for_map_header(i, tmp, var_typ, val, typ string) {
|
|
|
|
def := type_default(typ)
|
|
|
|
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];')
|
|
|
|
// TODO don't call map_get() for each key, fetch values while traversing
|
|
|
|
// the tree (replace `map_keys()` above with `map_key_vals()`)
|
2019-09-28 19:53:56 +03:00
|
|
|
if val == '_' { return }
|
2019-09-14 23:48:30 +03:00
|
|
|
p.genln('$var_typ $val = $def; map_get($tmp, $i, & $val);')
|
|
|
|
}
|
|
|
|
|
2019-09-30 17:11:12 +03:00
|
|
|
fn (p mut Parser) gen_for_varg_header(i, varg, var_typ, val string) {
|
|
|
|
p.genln('for (int $i = 0; $i < ${varg}->len; $i++) {')
|
|
|
|
if val == '_' { return }
|
|
|
|
p.genln('$var_typ $val = (($var_typ *) $varg->args)[$i];')
|
|
|
|
}
|
|
|
|
|
2019-09-14 23:48:30 +03:00
|
|
|
fn (p mut Parser) gen_array_init(typ string, no_alloc bool, new_arr_ph int, nr_elems int) {
|
|
|
|
mut new_arr := 'new_array_from_c_array'
|
|
|
|
if no_alloc {
|
|
|
|
new_arr += '_no_alloc'
|
|
|
|
}
|
2019-10-05 15:46:13 +03:00
|
|
|
if nr_elems == 0 {
|
|
|
|
p.gen(' TCCSKIP(0) })')
|
2019-09-14 23:48:30 +03:00
|
|
|
} else {
|
|
|
|
p.gen(' })')
|
|
|
|
}
|
|
|
|
// Need to do this in the second pass, otherwise it goes to the very top of the out.c file
|
2019-10-12 17:53:53 +03:00
|
|
|
if !p.first_pass() {
|
2019-10-05 15:46:13 +03:00
|
|
|
p.cgen.set_placeholder(new_arr_ph,
|
|
|
|
'$new_arr($nr_elems, $nr_elems, sizeof($typ), EMPTY_ARRAY_OF_ELEMS( $typ, $nr_elems ) { ')
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
2019-10-12 17:53:53 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
|
|
|
|
fn (p mut Parser) gen_array_set(typ string, is_ptr, is_map bool,fn_ph, assign_pos int, is_cao bool) {
|
|
|
|
// `a[0] = 7`
|
|
|
|
// curline right now: `a , 0 = 7`
|
|
|
|
mut val := p.cgen.cur_line.right(assign_pos)
|
|
|
|
p.cgen.resetln(p.cgen.cur_line.left(assign_pos))
|
|
|
|
mut cao_tmp := p.cgen.cur_line
|
|
|
|
mut func := ''
|
|
|
|
if is_map {
|
2019-10-10 01:07:00 +03:00
|
|
|
func = 'map_set(&'
|
2019-09-14 23:48:30 +03:00
|
|
|
// CAO on map is a bit more complicated as it loads
|
|
|
|
// the value inside a pointer instead of returning it.
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if is_ptr {
|
|
|
|
func = 'array_set('
|
|
|
|
if is_cao {
|
2019-10-10 01:07:00 +03:00
|
|
|
cao_tmp = '*($p.expected_type *) array_get(*$cao_tmp)'
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
func = 'array_set(&/*q*/'
|
|
|
|
if is_cao {
|
2019-10-10 01:07:00 +03:00
|
|
|
cao_tmp = '*($p.expected_type *) array_get($cao_tmp)'
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.cgen.set_placeholder(fn_ph, func)
|
|
|
|
if is_cao {
|
|
|
|
val = cao_tmp + val.all_before('=') + val.all_after('=')
|
|
|
|
}
|
|
|
|
p.gen(', & ($typ []) { $val })')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// returns true in case of an early return
|
|
|
|
fn (p mut Parser) gen_struct_init(typ string, t Type) bool {
|
|
|
|
// 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 p.is_c_struct_init {
|
|
|
|
if t.cat != .c_typedef {
|
|
|
|
p.cgen.insert_before('struct /*c struct init*/')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO tm struct struct bug
|
|
|
|
if typ == 'tm' {
|
|
|
|
p.cgen.lines[p.cgen.lines.len-1] = ''
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
p.check(.lcbr)
|
|
|
|
ptr := typ.contains('*')
|
|
|
|
// `user := User{foo:bar}` => `User user = (User){ .foo = bar}`
|
|
|
|
if !ptr {
|
|
|
|
if p.is_c_struct_init {
|
|
|
|
// `face := C.FT_Face{}` => `FT_Face face;`
|
|
|
|
if p.tok == .rcbr {
|
|
|
|
p.is_empty_c_struct_init = true
|
|
|
|
p.check(.rcbr)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
p.gen('(struct $typ) {')
|
|
|
|
p.is_c_struct_init = false
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen('($typ) {')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO tmp hack for 0 pointers init
|
|
|
|
// &User{!} ==> 0
|
|
|
|
if p.tok == .not {
|
|
|
|
p.next()
|
|
|
|
p.gen('0')
|
|
|
|
p.check(.rcbr)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
p.gen('($t.name*)memdup(&($t.name) {')
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) gen_struct_field_init(field string) {
|
|
|
|
p.gen('.$field = ')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) gen_empty_map(typ string) {
|
|
|
|
p.gen('new_map(1, sizeof($typ))')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) cast(typ string) {
|
|
|
|
p.next()
|
|
|
|
pos := p.cgen.add_placeholder()
|
|
|
|
if p.tok == .rpar {
|
|
|
|
// skip `)` if it's `(*int)(ptr)`, not `int(a)`
|
|
|
|
p.ptr_cast = true
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
p.check(.lpar)
|
|
|
|
p.expected_type = typ
|
|
|
|
expr_typ := p.bool_expression()
|
|
|
|
// `face := FT_Face(cobj)` => `FT_Face face = *((FT_Face*)cobj);`
|
|
|
|
casting_voidptr_to_value := expr_typ == 'void*' && typ != 'int' &&
|
|
|
|
typ != 'byteptr' && !typ.ends_with('*')
|
|
|
|
p.expected_type = ''
|
|
|
|
// `string(buffer)` => `tos2(buffer)`
|
|
|
|
// `string(buffer, len)` => `tos(buffer, len)`
|
|
|
|
// `string(bytes_array, len)` => `tos(bytes_array.data, len)`
|
|
|
|
is_byteptr := expr_typ == 'byte*' || expr_typ == 'byteptr'
|
|
|
|
is_bytearr := expr_typ == 'array_byte'
|
|
|
|
if typ == 'string' {
|
|
|
|
if is_byteptr || is_bytearr {
|
|
|
|
if p.tok == .comma {
|
|
|
|
p.check(.comma)
|
|
|
|
p.cgen.set_placeholder(pos, 'tos((byte *)')
|
|
|
|
if is_bytearr {
|
|
|
|
p.gen('.data')
|
|
|
|
}
|
|
|
|
p.gen(', ')
|
|
|
|
p.check_types(p.expression(), 'int')
|
|
|
|
} else {
|
|
|
|
if is_bytearr {
|
|
|
|
p.gen('.data')
|
|
|
|
}
|
|
|
|
p.cgen.set_placeholder(pos, 'tos2((byte *)')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// `string(234)` => error
|
|
|
|
else if expr_typ == 'int' {
|
|
|
|
p.error('cannot cast `$expr_typ` to `$typ`, use `str()` method instead')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('cannot cast `$expr_typ` to `$typ`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 []')
|
|
|
|
}
|
|
|
|
else if casting_voidptr_to_value {
|
|
|
|
p.cgen.set_placeholder(pos, '*($typ*)(')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.cgen.set_placeholder(pos, '($typ)(')
|
|
|
|
}
|
|
|
|
p.check(.rpar)
|
|
|
|
p.gen(')')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_default(typ string) string {
|
|
|
|
if typ.starts_with('array_') {
|
|
|
|
return 'new_array(0, 1, sizeof( ${typ.right(6)} ))'
|
|
|
|
}
|
|
|
|
// Always set pointers to 0
|
|
|
|
if typ.ends_with('*') {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
// User struct defined in another module.
|
|
|
|
if typ.contains('__') {
|
|
|
|
return '{0}'
|
|
|
|
}
|
|
|
|
// Default values for other types are not needed because of mandatory initialization
|
2019-10-24 19:19:03 +03:00
|
|
|
match typ {
|
|
|
|
'bool'{ return '0'}
|
|
|
|
'string'{ return 'tos3("")'}
|
|
|
|
'i8'{ return '0'}
|
|
|
|
'i16'{ return '0'}
|
|
|
|
'i64'{ return '0'}
|
|
|
|
'u16'{ return '0'}
|
|
|
|
'u32'{ return '0'}
|
|
|
|
'u64'{ return '0'}
|
|
|
|
'byte'{ return '0'}
|
|
|
|
'int'{ return '0'}
|
|
|
|
'rune'{ return '0'}
|
|
|
|
'f32'{ return '0.0'}
|
|
|
|
'f64'{ return '0.0'}
|
|
|
|
'byteptr'{ return '0'}
|
|
|
|
'voidptr'{ return '0'}
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
return '{0}'
|
|
|
|
}
|
|
|
|
|
2019-09-24 22:30:30 +03:00
|
|
|
fn (p mut Parser) gen_array_push(ph int, typ, expr_type, tmp, elm_type string) {
|
|
|
|
// Two arrays of the same type?
|
2019-09-14 23:48:30 +03:00
|
|
|
push_array := typ == expr_type
|
|
|
|
if push_array {
|
|
|
|
p.cgen.set_placeholder(ph, '_PUSH_MANY(&' )
|
|
|
|
p.gen('), $tmp, $typ)')
|
2019-09-24 22:30:30 +03:00
|
|
|
} else {
|
|
|
|
p.check_types(expr_type, elm_type)
|
2019-09-14 23:48:30 +03:00
|
|
|
// Pass tmp var info to the _PUSH macro
|
|
|
|
// Prepend tmp initialisation and push call
|
|
|
|
// Don't dereference if it's already a mutable array argument (`fn foo(mut []int)`)
|
|
|
|
push_call := if typ.contains('*'){'_PUSH('} else { '_PUSH(&'}
|
|
|
|
p.cgen.set_placeholder(ph, push_call)
|
2019-09-24 22:30:30 +03:00
|
|
|
if elm_type.ends_with('*') {
|
|
|
|
p.gen('), $tmp, ${elm_type.left(elm_type.len - 1)})')
|
2019-09-22 10:32:44 +03:00
|
|
|
} else {
|
2019-09-24 22:30:30 +03:00
|
|
|
p.gen('), $tmp, $elm_type)')
|
2019-09-22 10:32:44 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|