2019-07-29 19:21:36 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
module compiler
|
2019-07-29 19:21:36 +03:00
|
|
|
|
2019-08-23 13:43:31 +03:00
|
|
|
import (
|
|
|
|
vweb.tmpl // for `$vweb_html()`
|
|
|
|
os
|
2019-09-26 05:28:43 +03:00
|
|
|
strings
|
2019-08-23 13:43:31 +03:00
|
|
|
)
|
2019-07-29 19:21:36 +03:00
|
|
|
|
|
|
|
fn (p mut Parser) comp_time() {
|
2019-08-23 13:43:31 +03:00
|
|
|
p.check(.dollar)
|
2019-07-29 19:21:36 +03:00
|
|
|
if p.tok == .key_if {
|
2019-08-23 13:43:31 +03:00
|
|
|
p.check(.key_if)
|
|
|
|
p.fspace()
|
|
|
|
not := p.tok == .not
|
2019-07-29 19:21:36 +03:00
|
|
|
if not {
|
2019-08-23 13:43:31 +03:00
|
|
|
p.check(.not)
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
|
|
|
name := p.check_name()
|
2019-08-23 13:43:31 +03:00
|
|
|
p.fspace()
|
2019-09-27 04:00:48 +03:00
|
|
|
if name in supported_platforms {
|
2019-08-23 13:43:31 +03:00
|
|
|
ifdef_name := os_name_to_ifdef(name)
|
2019-07-29 19:21:36 +03:00
|
|
|
if not {
|
|
|
|
p.genln('#ifndef $ifdef_name')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln('#ifdef $ifdef_name')
|
|
|
|
}
|
|
|
|
p.check(.lcbr)
|
2019-10-12 00:54:25 +03:00
|
|
|
os := os_from_string(name)
|
2019-10-12 03:49:36 +03:00
|
|
|
if false && p.fileis('runtime.v') && os != p.os {
|
2019-10-12 00:54:25 +03:00
|
|
|
// `$if os {` for a different target, skip everything inside
|
|
|
|
// to avoid compilation errors (like including <windows.h>
|
|
|
|
// on non-Windows systems)
|
|
|
|
mut stack := 1
|
|
|
|
for {
|
|
|
|
if p.tok == .lcbr {
|
|
|
|
stack++
|
|
|
|
} else if p.tok == .rcbr {
|
|
|
|
stack--
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-10-12 00:54:25 +03:00
|
|
|
if p.tok == .eof {
|
|
|
|
break
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-10-12 00:54:25 +03:00
|
|
|
if stack <= 0 && p.tok == .rcbr {
|
|
|
|
//p.warn('exiting $stack')
|
|
|
|
p.next()
|
|
|
|
break
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-10-12 00:54:25 +03:00
|
|
|
p.next()
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-10-12 00:54:25 +03:00
|
|
|
} else {
|
|
|
|
p.statements_no_rcbr()
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
if ! (p.tok == .dollar && p.peek() == .key_else) {
|
|
|
|
p.genln('#endif')
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 08:50:36 +03:00
|
|
|
else if name == 'debug' {
|
|
|
|
p.genln('#ifdef VDEBUG')
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.statements_no_rcbr()
|
|
|
|
p.genln('#endif')
|
|
|
|
}
|
2019-10-11 03:55:54 +03:00
|
|
|
else if name == 'tinyc' {
|
|
|
|
p.genln('#ifdef __TINYC__')
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.statements_no_rcbr()
|
2019-10-14 08:52:54 +03:00
|
|
|
if ! (p.tok == .dollar && p.peek() == .key_else) {
|
|
|
|
p.genln('#endif')
|
|
|
|
}
|
2019-10-11 03:55:54 +03:00
|
|
|
}
|
2019-10-25 18:32:44 +03:00
|
|
|
else if name == 'glibc' {
|
|
|
|
p.genln('#ifdef __GLIBC__')
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.statements_no_rcbr()
|
|
|
|
if ! (p.tok == .dollar && p.peek() == .key_else) {
|
|
|
|
p.genln('#endif')
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
else {
|
|
|
|
println('Supported platforms:')
|
2019-09-27 04:00:48 +03:00
|
|
|
println(supported_platforms)
|
2019-07-29 19:21:36 +03:00
|
|
|
p.error('unknown platform `$name`')
|
|
|
|
}
|
2019-08-10 11:26:42 +03:00
|
|
|
if_returns := p.returns
|
|
|
|
p.returns = false
|
2019-07-08 04:42:36 +03:00
|
|
|
//p.gen('/* returns $p.returns */')
|
2019-08-10 11:26:42 +03:00
|
|
|
if p.tok == .dollar && p.peek() == .key_else {
|
|
|
|
p.next()
|
|
|
|
p.next()
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.genln('#else')
|
|
|
|
p.statements_no_rcbr()
|
|
|
|
p.genln('#endif')
|
|
|
|
else_returns := p.returns
|
|
|
|
p.returns = if_returns && else_returns
|
2019-07-08 04:42:36 +03:00
|
|
|
//p.gen('/* returns $p.returns */')
|
2019-08-10 11:26:42 +03:00
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
|
|
|
else if p.tok == .key_for {
|
|
|
|
p.next()
|
|
|
|
name := p.check_name()
|
|
|
|
if name != 'field' {
|
|
|
|
p.error('for field only')
|
|
|
|
}
|
|
|
|
p.check(.key_in)
|
|
|
|
p.check_name()
|
|
|
|
p.check(.dot)
|
|
|
|
p.check_name()// fields
|
|
|
|
p.check(.lcbr)
|
|
|
|
// for p.tok != .rcbr && p.tok != .eof {
|
|
|
|
res_name := p.check_name()
|
|
|
|
println(res_name)
|
|
|
|
p.check(.dot)
|
|
|
|
p.check(.dollar)
|
|
|
|
p.check(.name)
|
|
|
|
p.check(.assign)
|
|
|
|
p.cgen.start_tmp()
|
|
|
|
p.bool_expression()
|
|
|
|
val := p.cgen.end_tmp()
|
|
|
|
println(val)
|
|
|
|
p.check(.rcbr)
|
|
|
|
// }
|
|
|
|
}
|
2019-08-23 13:43:31 +03:00
|
|
|
// $vweb.html()
|
2019-07-29 19:21:36 +03:00
|
|
|
// Compile vweb html template to V code, parse that V code and embed the resulting V functions
|
2019-08-23 13:43:31 +03:00
|
|
|
// that returns an html string
|
2019-07-29 19:21:36 +03:00
|
|
|
else if p.tok == .name && p.lit == 'vweb' {
|
2019-09-21 01:17:52 +03:00
|
|
|
mut path := p.cur_fn.name + '.html'
|
2019-07-31 04:41:39 +03:00
|
|
|
if p.pref.is_debug {
|
2019-08-23 13:43:31 +03:00
|
|
|
println('compiling tmpl $path')
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
if !os.file_exists(path) {
|
2019-09-21 01:17:52 +03:00
|
|
|
// Can't find the template file in current directory,
|
|
|
|
// try looking next to the vweb program, in case it's run with
|
|
|
|
// v path/to/vweb_app.v
|
|
|
|
path = os.dir(p.scanner.file_path) + '/' + path
|
|
|
|
if !os.file_exists(path) {
|
|
|
|
p.error('vweb HTML template "$path" not found')
|
|
|
|
}
|
2019-08-23 13:43:31 +03:00
|
|
|
}
|
|
|
|
p.check(.name) // skip `vweb.html()` TODO
|
|
|
|
p.check(.dot)
|
|
|
|
p.check(.name)
|
|
|
|
p.check(.lpar)
|
|
|
|
p.check(.rpar)
|
|
|
|
v_code := tmpl.compile_template(path)
|
2019-07-31 04:41:39 +03:00
|
|
|
if os.file_exists('.vwebtmpl.v') {
|
2019-08-23 13:43:31 +03:00
|
|
|
os.rm('.vwebtmpl.v')
|
|
|
|
}
|
|
|
|
os.write_file('.vwebtmpl.v', v_code.clone()) // TODO don't need clone, compiler bug
|
|
|
|
p.genln('')
|
2019-07-29 19:21:36 +03:00
|
|
|
// Parse the function and embed resulting C code in current function so that
|
2019-08-23 13:43:31 +03:00
|
|
|
// all variables are available.
|
|
|
|
pos := p.cgen.lines.len - 1
|
2019-10-04 15:48:09 +03:00
|
|
|
mut pp := p.v.new_parser_from_file('.vwebtmpl.v')
|
2019-08-23 13:43:31 +03:00
|
|
|
if !p.pref.is_debug {
|
|
|
|
os.rm('.vwebtmpl.v')
|
|
|
|
}
|
|
|
|
pp.is_vweb = true
|
2019-09-07 13:44:41 +03:00
|
|
|
pp.set_current_fn( p.cur_fn ) // give access too all variables in current function
|
2019-08-23 13:43:31 +03:00
|
|
|
pp.parse(.main)
|
2019-10-05 09:52:50 +03:00
|
|
|
pp.v.add_parser(pp)
|
2019-08-23 13:43:31 +03:00
|
|
|
tmpl_fn_body := p.cgen.lines.slice(pos + 2, p.cgen.lines.len).join('\n').clone()
|
|
|
|
end_pos := tmpl_fn_body.last_index('Builder_str( sb )') + 19 // TODO
|
2019-10-27 10:03:15 +03:00
|
|
|
p.cgen.lines = p.cgen.lines[..pos]
|
2019-08-23 13:43:31 +03:00
|
|
|
p.genln('/////////////////// tmpl start')
|
2019-10-27 10:03:15 +03:00
|
|
|
p.genln(tmpl_fn_body[..end_pos])
|
2019-08-23 13:43:31 +03:00
|
|
|
p.genln('/////////////////// tmpl end')
|
|
|
|
// `app.vweb.html(index_view())`
|
|
|
|
receiver := p.cur_fn.args[0]
|
|
|
|
dot := if receiver.is_mut { '->' } else { '.' }
|
|
|
|
p.genln('vweb__Context_html($receiver.name $dot vweb, tmpl_res)')
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
else {
|
|
|
|
p.error('bad comptime expr')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 13:43:31 +03:00
|
|
|
// #include, #flag, #v
|
2019-07-29 19:21:36 +03:00
|
|
|
fn (p mut Parser) chash() {
|
|
|
|
hash := p.lit.trim_space()
|
2019-10-07 01:31:01 +03:00
|
|
|
// println('chsh() file=$p.file hash="$hash"')
|
2019-07-29 19:21:36 +03:00
|
|
|
p.next()
|
|
|
|
if hash.starts_with('flag ') {
|
2019-10-19 11:40:01 +03:00
|
|
|
if p.first_pass() {
|
2019-10-27 10:03:15 +03:00
|
|
|
mut flag := hash[5..]
|
2019-10-19 00:00:47 +03:00
|
|
|
// expand `@VROOT` `@VMOD` to absolute path
|
|
|
|
flag = flag.replace('@VROOT', p.vroot)
|
|
|
|
flag = flag.replace('@VMOD', v_modules_path)
|
|
|
|
//p.log('adding flag "$flag"')
|
2019-10-20 12:24:12 +03:00
|
|
|
_ = p.table.parse_cflag(flag, p.mod) or {
|
2019-10-19 03:38:02 +03:00
|
|
|
p.error_with_token_index(err, p.cur_tok_index()-1)
|
2019-10-20 10:19:37 +03:00
|
|
|
return
|
2019-10-19 03:38:02 +03:00
|
|
|
}
|
2019-10-19 00:00:47 +03:00
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if hash.starts_with('include') {
|
2019-10-07 01:31:01 +03:00
|
|
|
if p.first_pass() && !p.is_vh {
|
2019-08-04 21:39:43 +03:00
|
|
|
if p.file_pcguard.len != 0 {
|
|
|
|
//println('p: $p.file_platform $p.file_pcguard')
|
|
|
|
p.cgen.includes << '$p.file_pcguard\n#$hash\n#endif'
|
|
|
|
return
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
p.cgen.includes << '#$hash'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO remove after ui_mac.m is removed
|
|
|
|
else if hash.contains('embed') {
|
|
|
|
pos := hash.index('embed') + 5
|
2019-10-27 10:03:15 +03:00
|
|
|
file := hash[pos..]
|
2019-10-30 15:04:25 +03:00
|
|
|
//if p.pref.build_mode != .default_mode {
|
2019-07-29 19:21:36 +03:00
|
|
|
p.genln('#include $file')
|
2019-10-30 15:04:25 +03:00
|
|
|
//}
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
2019-08-23 13:43:31 +03:00
|
|
|
else if hash.contains('define') {
|
|
|
|
// Move defines on top
|
2019-07-29 19:21:36 +03:00
|
|
|
p.cgen.includes << '#$hash'
|
2019-08-23 13:43:31 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
// Don't parse a non-JS V file (`#-js` flag)
|
|
|
|
else if hash == '-js' {
|
|
|
|
$if js {
|
|
|
|
for p.tok != .eof {
|
|
|
|
p.next()
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
} $else {
|
|
|
|
p.next()
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
else {
|
2019-09-14 23:48:30 +03:00
|
|
|
$if !js {
|
|
|
|
if !p.can_chash {
|
|
|
|
println('hash="$hash"')
|
2019-10-25 18:32:44 +03:00
|
|
|
if hash.starts_with('include') { println("include") } else {}
|
2019-09-14 23:48:30 +03:00
|
|
|
p.error('bad token `#` (embedding C code is no longer supported)')
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
|
|
|
p.genln(hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 13:43:31 +03:00
|
|
|
// `user.$method()` (`method` is a string)
|
2019-07-29 19:21:36 +03:00
|
|
|
fn (p mut Parser) comptime_method_call(typ Type) {
|
2019-08-23 13:43:31 +03:00
|
|
|
p.cgen.cur_line = ''
|
2019-07-29 19:21:36 +03:00
|
|
|
p.check(.dollar)
|
2019-08-23 13:43:31 +03:00
|
|
|
var := p.check_name()
|
2019-08-02 06:20:18 +03:00
|
|
|
for i, method in typ.methods {
|
2019-07-29 19:21:36 +03:00
|
|
|
if method.typ != 'void' {
|
2019-08-23 13:43:31 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
receiver := method.args[0]
|
|
|
|
amp := if receiver.is_mut { '&' } else { '' }
|
|
|
|
if i > 0 {
|
2019-08-02 06:20:18 +03:00
|
|
|
p.gen(' else ')
|
2019-08-23 13:43:31 +03:00
|
|
|
}
|
|
|
|
p.gen('if ( string_eq($var, _STR("$method.name")) ) ${typ.name}_$method.name($amp $p.expr_var.name);')
|
|
|
|
}
|
|
|
|
p.check(.lpar)
|
|
|
|
p.check(.rpar)
|
2019-08-02 05:04:48 +03:00
|
|
|
if p.tok == .key_orelse {
|
2019-08-23 13:43:31 +03:00
|
|
|
p.check(.key_orelse)
|
|
|
|
p.genln('else {')
|
2019-08-02 05:04:48 +03:00
|
|
|
p.check(.lcbr)
|
2019-08-23 13:43:31 +03:00
|
|
|
p.statements()
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
|
2019-08-31 16:38:13 +03:00
|
|
|
fn (p mut Parser) gen_array_str(typ Type) {
|
2019-10-25 22:09:23 +03:00
|
|
|
if typ.has_method('str') {
|
|
|
|
return
|
|
|
|
}
|
2019-09-18 15:37:34 +03:00
|
|
|
p.add_method(typ.name, Fn{
|
|
|
|
name: 'str'
|
2019-08-11 00:02:48 +03:00
|
|
|
typ: 'string'
|
2019-08-23 13:43:31 +03:00
|
|
|
args: [Var{typ: typ.name, is_arg:true}]
|
|
|
|
is_method: true
|
2019-08-22 05:00:06 +03:00
|
|
|
is_public: true
|
2019-08-23 13:43:31 +03:00
|
|
|
receiver_typ: typ.name
|
|
|
|
})
|
2019-10-27 10:03:15 +03:00
|
|
|
elm_type := typ.name[6..]
|
2019-08-31 16:38:13 +03:00
|
|
|
elm_type2 := p.table.find_type(elm_type)
|
2019-10-17 06:52:58 +03:00
|
|
|
is_array := elm_type.starts_with('array_')
|
|
|
|
if is_array {
|
|
|
|
p.gen_array_str(elm_type2)
|
|
|
|
} else if p.typ_to_fmt(elm_type, 0) == '' &&
|
2019-08-31 16:38:13 +03:00
|
|
|
!p.table.type_has_method(elm_type2, 'str') {
|
2019-08-22 07:56:29 +03:00
|
|
|
p.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
|
|
|
|
}
|
2019-09-27 14:02:01 +03:00
|
|
|
p.v.vgen_buf.writeln('
|
2019-09-26 05:28:43 +03:00
|
|
|
fn (a $typ.name) str() string {
|
|
|
|
mut sb := strings.new_builder(a.len * 3)
|
|
|
|
sb.write("[")
|
|
|
|
for i, elm in a {
|
|
|
|
sb.write(elm.str())
|
|
|
|
if i < a.len - 1 {
|
|
|
|
sb.write(", ")
|
|
|
|
}
|
2019-08-23 13:43:31 +03:00
|
|
|
}
|
2019-09-26 05:28:43 +03:00
|
|
|
sb.write("]")
|
|
|
|
return sb.str()
|
2019-08-23 13:43:31 +03:00
|
|
|
}
|
2019-09-26 05:28:43 +03:00
|
|
|
')
|
|
|
|
p.cgen.fns << 'string ${typ.name}_str();'
|
|
|
|
}
|
|
|
|
|
|
|
|
// `Foo { bar: 3, baz: 'hi' }` => '{ bar: 3, baz: "hi" }'
|
|
|
|
fn (p mut Parser) gen_struct_str(typ Type) {
|
|
|
|
p.add_method(typ.name, Fn{
|
|
|
|
name: 'str'
|
|
|
|
typ: 'string'
|
|
|
|
args: [Var{typ: typ.name, is_arg:true}]
|
|
|
|
is_method: true
|
|
|
|
is_public: true
|
|
|
|
receiver_typ: typ.name
|
|
|
|
})
|
2019-10-20 10:19:37 +03:00
|
|
|
|
2019-09-26 05:28:43 +03:00
|
|
|
mut sb := strings.new_builder(typ.fields.len * 20)
|
|
|
|
sb.writeln('fn (a $typ.name) str() string {\nreturn')
|
|
|
|
sb.writeln("'{")
|
|
|
|
for field in typ.fields {
|
2019-10-04 05:40:08 +03:00
|
|
|
sb.writeln('\t$field.name: $' + 'a.${field.name}')
|
2019-09-26 05:28:43 +03:00
|
|
|
}
|
2019-10-04 15:48:09 +03:00
|
|
|
sb.writeln("}'")
|
2019-09-26 05:28:43 +03:00
|
|
|
sb.writeln('}')
|
2019-09-27 14:02:01 +03:00
|
|
|
p.v.vgen_buf.writeln(sb.str())
|
2019-09-26 05:28:43 +03:00
|
|
|
// Need to manually add the definition to `fns` so that it stays
|
|
|
|
// at the top of the file.
|
|
|
|
// This function will get parsee by V after the main pass.
|
|
|
|
p.cgen.fns << 'string ${typ.name}_str();'
|
2019-08-23 13:43:31 +03:00
|
|
|
}
|
2019-10-18 07:58:54 +03:00
|
|
|
|
|
|
|
fn (p mut Parser) gen_array_filter(str_typ string, method_ph int) {
|
|
|
|
/*
|
|
|
|
// V
|
|
|
|
a := [1,2,3,4]
|
|
|
|
b := a.filter(it % 2 == 0)
|
|
|
|
|
|
|
|
// C
|
|
|
|
array_int a = ...;
|
|
|
|
array_int tmp2 = new_array(0, 4, 4);
|
|
|
|
for (int i = 0; i < a.len; i++) {
|
|
|
|
int it = ((int*)a.data)[i];
|
|
|
|
if (it % 2 == 0) array_push(&tmp2, &it);
|
|
|
|
}
|
|
|
|
array_int b = tmp2;
|
|
|
|
*/
|
2019-10-27 10:03:15 +03:00
|
|
|
val_type:=str_typ[6..]
|
2019-10-18 07:58:54 +03:00
|
|
|
p.open_scope()
|
|
|
|
p.register_var(Var{
|
|
|
|
name: 'it'
|
|
|
|
typ: val_type
|
|
|
|
})
|
|
|
|
p.next()
|
|
|
|
p.check(.lpar)
|
|
|
|
p.cgen.resetln('')
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
a := p.expr_var.name
|
|
|
|
p.cgen.set_placeholder(method_ph,'\n$str_typ $tmp = new_array(0, $a .len,sizeof($val_type));\n')
|
|
|
|
p.genln('for (int i = 0; i < ${a}.len; i++) {')
|
|
|
|
p.genln('$val_type it = (($val_type*)${a}.data)[i];')
|
|
|
|
p.gen('if (')
|
|
|
|
p.bool_expression()
|
|
|
|
p.genln(') array_push(&$tmp, &it);')
|
|
|
|
//p.genln(') array_push(&$tmp, &((($val_type*)${a}.data)[i]));')
|
|
|
|
//p.genln(') array_push(&$tmp, ${a}.data + i * ${a}.element_size);')
|
|
|
|
p.genln('}')
|
|
|
|
p.gen(tmp) // TODO why does this `gen()` work?
|
|
|
|
p.check(.rpar)
|
|
|
|
p.close_scope()
|
|
|
|
}
|
2019-10-26 14:25:03 +03:00
|
|
|
|
2019-10-26 14:49:36 +03:00
|
|
|
fn (p mut Parser) gen_array_map(str_typ string, method_ph int) string {
|
2019-10-26 14:25:03 +03:00
|
|
|
/*
|
|
|
|
// V
|
|
|
|
a := [1,2,3,4]
|
|
|
|
b := a.map(it * 2)
|
|
|
|
|
|
|
|
// C
|
|
|
|
array_int a = ...;
|
|
|
|
array_int tmp2 = new_array(0, 4, 4);
|
|
|
|
for (int i = 0; i < a.len; i++) {
|
|
|
|
int it = ((int*)a.data)[i];
|
|
|
|
_PUSH(tmp2, it * 2, tmp3, int)
|
|
|
|
}
|
|
|
|
array_int b = tmp2;
|
|
|
|
*/
|
2019-10-27 10:03:15 +03:00
|
|
|
val_type:=str_typ[6..]
|
2019-10-26 14:25:03 +03:00
|
|
|
p.open_scope()
|
|
|
|
p.register_var(Var{
|
|
|
|
name: 'it'
|
|
|
|
typ: val_type
|
|
|
|
})
|
|
|
|
p.next()
|
|
|
|
p.check(.lpar)
|
|
|
|
p.cgen.resetln('')
|
|
|
|
tmp := p.get_tmp()
|
|
|
|
tmp_elm := p.get_tmp()
|
|
|
|
a := p.expr_var.name
|
|
|
|
map_type, expr := p.tmp_expr()
|
|
|
|
p.cgen.set_placeholder(method_ph,'\narray $tmp = new_array(0, $a .len, ' +
|
|
|
|
'sizeof($map_type));\n')
|
|
|
|
p.genln('for (int i = 0; i < ${a}.len; i++) {')
|
|
|
|
p.genln('$val_type it = (($val_type*)${a}.data)[i];')
|
|
|
|
p.genln('_PUSH(&$tmp, $expr, $tmp_elm, $map_type)')
|
|
|
|
p.genln('}')
|
|
|
|
p.gen(tmp) // TODO why does this `gen()` work?
|
|
|
|
p.check(.rpar)
|
|
|
|
p.close_scope()
|
2019-10-26 14:49:36 +03:00
|
|
|
return 'array_' + map_type
|
2019-10-26 14:25:03 +03:00
|
|
|
}
|