2019-07-29 18:21:36 +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-10-13 15:37:43 +02:00
|
|
|
module compiler
|
2019-07-29 18:21:36 +02:00
|
|
|
|
2019-08-23 12:43:31 +02:00
|
|
|
import (
|
2019-12-19 21:36:18 +01:00
|
|
|
vweb.tmpl // for `$vweb_html()`
|
2019-08-23 12:43:31 +02:00
|
|
|
os
|
2019-09-26 04:28:43 +02:00
|
|
|
strings
|
2019-12-23 11:09:22 +01:00
|
|
|
filepath
|
2019-08-23 12:43:31 +02:00
|
|
|
)
|
2019-07-29 18:21:36 +02:00
|
|
|
|
|
|
|
fn (p mut Parser) comp_time() {
|
2019-08-23 12:43:31 +02:00
|
|
|
p.check(.dollar)
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.tok == .key_if {
|
2019-08-23 12:43:31 +02:00
|
|
|
p.check(.key_if)
|
|
|
|
p.fspace()
|
|
|
|
not := p.tok == .not
|
2019-07-29 18:21:36 +02:00
|
|
|
if not {
|
2019-08-23 12:43:31 +02:00
|
|
|
p.check(.not)
|
2019-07-29 18:21:36 +02:00
|
|
|
}
|
|
|
|
name := p.check_name()
|
2019-08-23 12:43:31 +02:00
|
|
|
p.fspace()
|
2020-01-16 15:28:23 +01:00
|
|
|
|
2019-09-27 03:00:48 +02:00
|
|
|
if name in supported_platforms {
|
2020-01-16 15:28:23 +01:00
|
|
|
os := os_from_string(name)
|
2019-08-23 12:43:31 +02:00
|
|
|
ifdef_name := os_name_to_ifdef(name)
|
2019-12-03 14:09:37 +01:00
|
|
|
if name == 'mac' {
|
|
|
|
p.warn('use `macos` instead of `mac`')
|
|
|
|
}
|
2019-12-31 19:53:15 +01:00
|
|
|
|
2019-07-29 18:21:36 +02:00
|
|
|
if not {
|
2020-01-05 20:13:35 +01:00
|
|
|
if name == 'linux_or_macos' {
|
|
|
|
p.genln('#if !defined(__linux__) && !defined(__APPLE__)')
|
|
|
|
} else {
|
|
|
|
p.genln('#ifndef $ifdef_name')
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-12-31 19:53:15 +01:00
|
|
|
if name == 'linux_or_macos' {
|
2020-01-05 20:13:35 +01:00
|
|
|
p.genln('#if defined(__linux__) || defined(__APPLE__)')
|
2019-12-31 19:53:15 +01:00
|
|
|
} else {
|
|
|
|
p.genln('#ifdef $ifdef_name')
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
}
|
2020-01-16 15:28:23 +01:00
|
|
|
|
2019-07-29 18:21:36 +02:00
|
|
|
p.check(.lcbr)
|
2019-12-31 19:53:15 +01:00
|
|
|
if ((!not && os != p.os) || (not && os == p.os)) && !name.contains('_or_') &&
|
|
|
|
!p.scanner.is_fmt && !p.pref.output_cross_c {
|
2019-10-11 23:54:25 +02: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 {
|
2019-11-13 19:14:37 +01:00
|
|
|
if p.tok == .key_return {
|
|
|
|
p.returns = true
|
2019-11-30 11:09:05 +01:00
|
|
|
}
|
2019-10-11 23:54:25 +02:00
|
|
|
if p.tok == .lcbr {
|
|
|
|
stack++
|
2019-12-19 21:36:18 +01:00
|
|
|
}
|
|
|
|
else if p.tok == .rcbr {
|
2019-10-11 23:54:25 +02:00
|
|
|
stack--
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-10-11 23:54:25 +02:00
|
|
|
if p.tok == .eof {
|
|
|
|
break
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-10-11 23:54:25 +02:00
|
|
|
if stack <= 0 && p.tok == .rcbr {
|
2019-12-19 21:36:18 +01:00
|
|
|
// p.warn('exiting $stack')
|
2019-10-11 23:54:25 +02:00
|
|
|
p.next()
|
|
|
|
break
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-10-11 23:54:25 +02:00
|
|
|
p.next()
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-12-19 21:36:18 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-10-11 23:54:25 +02:00
|
|
|
p.statements_no_rcbr()
|
|
|
|
}
|
2019-12-19 21:36:18 +01:00
|
|
|
if !(p.tok == .dollar && p.peek() == .key_else) {
|
2019-07-29 18:21:36 +02:00
|
|
|
p.genln('#endif')
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 14:14:28 +01:00
|
|
|
else if name == 'x64' {
|
2020-01-16 15:28:23 +01:00
|
|
|
p.comptime_if_block('TARGET_IS_64BIT', not)
|
2019-11-15 14:14:28 +01:00
|
|
|
}
|
|
|
|
else if name == 'x32' {
|
2020-01-16 15:28:23 +01:00
|
|
|
p.comptime_if_block('TARGET_IS_32BIT', not)
|
2019-11-15 14:14:28 +01:00
|
|
|
}
|
|
|
|
else if name == 'big_endian' {
|
2020-01-16 15:28:23 +01:00
|
|
|
p.comptime_if_block('TARGET_ORDER_IS_BIG', not)
|
2019-11-15 14:14:28 +01:00
|
|
|
}
|
|
|
|
else if name == 'little_endian' {
|
2020-01-16 15:28:23 +01:00
|
|
|
p.comptime_if_block('TARGET_ORDER_IS_LITTLE', not)
|
2019-11-15 14:14:28 +01:00
|
|
|
}
|
2019-08-16 07:50:36 +02:00
|
|
|
else if name == 'debug' {
|
2020-01-16 15:28:23 +01:00
|
|
|
p.comptime_if_block('VDEBUG', not)
|
2019-08-16 07:50:36 +02:00
|
|
|
}
|
2019-12-14 00:28:15 +01:00
|
|
|
else if name == 'prealloc' {
|
2020-01-16 15:28:23 +01:00
|
|
|
p.comptime_if_block('VPREALLOC', not)
|
2019-12-14 00:28:15 +01:00
|
|
|
}
|
2019-10-11 02:55:54 +02:00
|
|
|
else if name == 'tinyc' {
|
2020-01-16 15:28:23 +01:00
|
|
|
p.comptime_if_block('__TINYC__', not)
|
2019-10-11 02:55:54 +02:00
|
|
|
}
|
2019-10-25 17:32:44 +02:00
|
|
|
else if name == 'glibc' {
|
2020-01-16 15:28:23 +01:00
|
|
|
p.comptime_if_block('__GLIBC__', not)
|
2019-11-15 14:14:28 +01:00
|
|
|
}
|
2019-11-04 18:14:34 +01:00
|
|
|
else if name == 'mingw' {
|
2020-01-16 15:28:23 +01:00
|
|
|
p.comptime_if_block('__MINGW32__', not)
|
2019-11-04 18:14:34 +01:00
|
|
|
}
|
|
|
|
else if name == 'msvc' {
|
2020-01-16 15:28:23 +01:00
|
|
|
p.comptime_if_block('_MSC_VER', not)
|
2019-11-04 18:14:34 +01:00
|
|
|
}
|
|
|
|
else if name == 'clang' {
|
2020-01-16 15:28:23 +01:00
|
|
|
p.comptime_if_block('__clang__', not)
|
2019-11-04 18:14:34 +01:00
|
|
|
}
|
2019-12-23 11:09:00 +01:00
|
|
|
else if p.v.compile_defines_all.len > 0 && name in p.v.compile_defines_all {
|
2020-01-16 15:28:23 +01:00
|
|
|
// Support for *optional* custom compile defines, i.e.:
|
|
|
|
//
|
|
|
|
// `[if custom]` => custom should be defined
|
|
|
|
// `$if custom { // stuff }` => custom should be defined
|
|
|
|
// `$if custom ? { // stuff }` => custom may not be defined
|
|
|
|
//
|
|
|
|
// Custom compile defines are given on the CLI, like this:
|
|
|
|
// `v -d custom=0` => means that the custom will be defined,
|
|
|
|
// but that it will be considered false.
|
|
|
|
// `v -d custom=1`, which is equivalent to `v -d custom`,
|
|
|
|
// means that the custom will be defined, and considered true.
|
|
|
|
//
|
|
|
|
// The ? sign, means that `custom` is optional, and when
|
|
|
|
// it is not present at all at the command line, then the
|
|
|
|
// block will just be ignored, instead of erroring.
|
|
|
|
if p.tok == .question {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
p.comptime_if_block('CUSTOM_DEFINE_${name}', not)
|
|
|
|
} else {
|
|
|
|
if p.tok == .question {
|
|
|
|
p.next()
|
|
|
|
p.comptime_if_block('CUSTOM_DEFINE_${name}', not)
|
|
|
|
}else{
|
|
|
|
println('Supported platforms:')
|
|
|
|
println(supported_platforms)
|
|
|
|
p.error('unknown platform `$name`')
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
}
|
2019-08-10 10:26:42 +02:00
|
|
|
if_returns := p.returns
|
|
|
|
p.returns = false
|
2019-12-19 21:36:18 +01:00
|
|
|
// p.gen('/* returns $p.returns */')
|
2019-08-10 10:26:42 +02:00
|
|
|
if p.tok == .dollar && p.peek() == .key_else {
|
2019-11-09 20:05:44 +01:00
|
|
|
p.fspace()
|
2019-08-10 10:26:42 +02:00
|
|
|
p.next()
|
|
|
|
p.next()
|
2019-11-09 20:05:44 +01:00
|
|
|
p.fspace() // spaces before and after $else
|
2019-08-10 10:26:42 +02:00
|
|
|
p.check(.lcbr)
|
|
|
|
p.genln('#else')
|
|
|
|
p.statements_no_rcbr()
|
|
|
|
p.genln('#endif')
|
|
|
|
else_returns := p.returns
|
|
|
|
p.returns = if_returns && else_returns
|
2019-12-19 21:36:18 +01:00
|
|
|
// p.gen('/* returns $p.returns */')
|
|
|
|
}
|
|
|
|
else if p.tok == .key_else {
|
2019-11-13 19:47:05 +01:00
|
|
|
p.error('use `$' + 'else` instead of `else` in comptime if statements')
|
2019-11-30 11:09:05 +01:00
|
|
|
}
|
2019-07-29 18:21:36 +02: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)
|
2019-12-19 21:36:18 +01:00
|
|
|
p.check_name() // fields
|
2019-07-29 18:21:36 +02:00
|
|
|
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)
|
2019-12-19 21:36:18 +01:00
|
|
|
_,val := p.tmp_expr()
|
|
|
|
// p.bool_expression()
|
|
|
|
// val := p.cgen.end_tmp()
|
2019-07-29 18:21:36 +02:00
|
|
|
p.check(.rcbr)
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
else if p.tok == .name && p.lit == 'vweb' {
|
2020-01-05 16:29:33 +01:00
|
|
|
// $vweb.html()
|
|
|
|
// Compile vweb html template to V code, parse that V code and embed the resulting V functions
|
|
|
|
// that returns an html string
|
2019-09-21 00:17:52 +02:00
|
|
|
mut path := p.cur_fn.name + '.html'
|
2019-07-31 03:41:39 +02:00
|
|
|
if p.pref.is_debug {
|
2020-01-05 16:29:33 +01:00
|
|
|
println('>>> compiling vweb HTML template "$path"')
|
2019-08-23 12:43:31 +02:00
|
|
|
}
|
2019-12-04 21:03:12 +01:00
|
|
|
if !os.exists(path) {
|
2019-09-21 00:17:52 +02: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
|
2019-12-23 11:09:22 +01:00
|
|
|
path = filepath.dir(p.scanner.file_path) + '/' + path
|
2019-12-04 21:03:12 +01:00
|
|
|
if !os.exists(path) {
|
2019-09-21 00:17:52 +02:00
|
|
|
p.error('vweb HTML template "$path" not found')
|
|
|
|
}
|
2019-08-23 12:43:31 +02:00
|
|
|
}
|
2019-12-19 21:36:18 +01:00
|
|
|
p.check(.name) // skip `vweb.html()` TODO
|
2019-08-23 12:43:31 +02:00
|
|
|
p.check(.dot)
|
|
|
|
p.check(.name)
|
|
|
|
p.check(.lpar)
|
|
|
|
p.check(.rpar)
|
|
|
|
v_code := tmpl.compile_template(path)
|
2019-12-16 16:58:56 +01:00
|
|
|
if p.pref.is_verbose {
|
2020-01-05 16:29:33 +01:00
|
|
|
println('\n\n')
|
|
|
|
println('>>> vweb template for ${path}:')
|
2019-12-16 16:58:56 +01:00
|
|
|
println(v_code)
|
2020-01-05 16:29:33 +01:00
|
|
|
println('>>> vweb template END')
|
|
|
|
println('\n\n')
|
2019-12-16 16:58:56 +01:00
|
|
|
}
|
2019-11-25 11:48:26 +01:00
|
|
|
is_strings_imorted := p.import_table.known_import('strings')
|
|
|
|
if !is_strings_imorted {
|
|
|
|
p.register_import('strings', 0) // used by v_code
|
|
|
|
}
|
|
|
|
p.import_table.register_used_import('strings')
|
2019-08-23 12:43:31 +02:00
|
|
|
p.genln('/////////////////// tmpl start')
|
2020-01-05 16:29:33 +01:00
|
|
|
p.statements_from_text(v_code, false, path)
|
2019-08-23 12:43:31 +02:00
|
|
|
p.genln('/////////////////// tmpl end')
|
|
|
|
receiver := p.cur_fn.args[0]
|
2019-11-26 07:34:04 +01:00
|
|
|
dot := if receiver.is_mut || receiver.ptr || receiver.typ.ends_with('*') { '->' } else { '.' }
|
2019-12-11 15:32:54 +01:00
|
|
|
p.genln('vweb__Context_html( & $receiver.name /*!*/$dot vweb, tmpl_res)')
|
2019-08-23 12:43:31 +02:00
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
else {
|
2020-01-05 16:29:33 +01:00
|
|
|
p.error('bad comp_time expression')
|
2019-07-29 18:21:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 12:43:31 +02:00
|
|
|
// #include, #flag, #v
|
2019-07-29 18:21:36 +02:00
|
|
|
fn (p mut Parser) chash() {
|
|
|
|
hash := p.lit.trim_space()
|
2019-10-07 00:31:01 +02:00
|
|
|
// println('chsh() file=$p.file hash="$hash"')
|
2019-07-29 18:21:36 +02:00
|
|
|
p.next()
|
2019-12-19 03:41:12 +01:00
|
|
|
p.fgen_nl()
|
2019-07-29 18:21:36 +02:00
|
|
|
if hash.starts_with('flag ') {
|
2019-10-19 10:40:01 +02:00
|
|
|
if p.first_pass() {
|
2019-10-27 08:03:15 +01:00
|
|
|
mut flag := hash[5..]
|
2019-10-18 23:00:47 +02:00
|
|
|
// expand `@VROOT` `@VMOD` to absolute path
|
|
|
|
flag = flag.replace('@VROOT', p.vroot)
|
2019-11-21 13:06:19 +01:00
|
|
|
flag = flag.replace('@VPATH', p.pref.vpath)
|
|
|
|
flag = flag.replace('@VLIB_PATH', p.pref.vlib_path)
|
2019-10-18 23:00:47 +02:00
|
|
|
flag = flag.replace('@VMOD', v_modules_path)
|
2019-12-19 21:36:18 +01:00
|
|
|
// p.log('adding flag "$flag"')
|
2019-12-23 11:09:00 +01:00
|
|
|
_ = p.table.parse_cflag(flag, p.mod, p.v.compile_defines_all ) or {
|
2019-12-19 21:36:18 +01:00
|
|
|
p.error_with_token_index(err, p.cur_tok_index() - 1)
|
2019-10-20 09:19:37 +02:00
|
|
|
return
|
2019-10-19 02:38:02 +02:00
|
|
|
}
|
2019-10-18 23:00:47 +02:00
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if hash.starts_with('include') {
|
2019-10-07 00:31:01 +02:00
|
|
|
if p.first_pass() && !p.is_vh {
|
2019-11-06 02:43:13 +01:00
|
|
|
/*
|
2019-11-05 22:32:15 +01:00
|
|
|
if !p.pref.building_v && !p.fileis('vlib') {
|
|
|
|
p.warn('C #includes will soon be removed from the language' +
|
|
|
|
'\ndefine the C structs and functions in V')
|
2019-11-30 11:09:05 +01:00
|
|
|
}
|
2019-11-06 02:43:13 +01:00
|
|
|
*/
|
2019-08-04 20:39:43 +02:00
|
|
|
if p.file_pcguard.len != 0 {
|
2019-12-19 21:36:18 +01:00
|
|
|
// println('p: $p.file_platform $p.file_pcguard')
|
2019-08-04 20:39:43 +02:00
|
|
|
p.cgen.includes << '$p.file_pcguard\n#$hash\n#endif'
|
|
|
|
return
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
p.cgen.includes << '#$hash'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO remove after ui_mac.m is removed
|
|
|
|
else if hash.contains('embed') {
|
2019-12-19 21:36:18 +01:00
|
|
|
pos := hash.index('embed') or {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file := hash[pos + 5..]
|
|
|
|
// if p.pref.build_mode != .default_mode {
|
|
|
|
p.genln('#include $file')
|
|
|
|
// }
|
2019-07-29 18:21:36 +02:00
|
|
|
}
|
2019-08-23 12:43:31 +02:00
|
|
|
else if hash.contains('define') {
|
|
|
|
// Move defines on top
|
2019-11-13 21:35:14 +01:00
|
|
|
if p.first_pass() {
|
|
|
|
p.cgen.includes << '#$hash'
|
|
|
|
}
|
2019-08-23 12:43:31 +02:00
|
|
|
}
|
2019-12-19 21:36:18 +01:00
|
|
|
// // Don't parse a non-JS V file (`#-js` flag)
|
|
|
|
else if hash == '-js' {
|
2019-09-14 22:48:30 +02:00
|
|
|
$if js {
|
|
|
|
for p.tok != .eof {
|
|
|
|
p.next()
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-09-14 22:48:30 +02:00
|
|
|
} $else {
|
|
|
|
p.next()
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-09-14 22:48:30 +02:00
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
else {
|
2019-09-14 22:48:30 +02:00
|
|
|
$if !js {
|
|
|
|
if !p.can_chash {
|
|
|
|
println('hash="$hash"')
|
2019-12-19 21:36:18 +01:00
|
|
|
if hash.starts_with('include') {
|
|
|
|
println('include')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
}
|
2019-09-14 22:48:30 +02:00
|
|
|
p.error('bad token `#` (embedding C code is no longer supported)')
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
}
|
|
|
|
p.genln(hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 12:43:31 +02:00
|
|
|
// `user.$method()` (`method` is a string)
|
2019-07-29 18:21:36 +02:00
|
|
|
fn (p mut Parser) comptime_method_call(typ Type) {
|
2019-08-23 12:43:31 +02:00
|
|
|
p.cgen.cur_line = ''
|
2019-07-29 18:21:36 +02:00
|
|
|
p.check(.dollar)
|
2019-08-23 12:43:31 +02:00
|
|
|
var := p.check_name()
|
2019-11-25 06:38:00 +01:00
|
|
|
mut j := 0
|
2019-12-06 13:24:53 +01:00
|
|
|
for method in typ.methods {
|
2019-07-29 18:21:36 +02:00
|
|
|
if method.typ != 'void' {
|
2019-08-23 12:43:31 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
receiver := method.args[0]
|
2019-11-25 06:38:00 +01:00
|
|
|
if !p.expr_var.ptr {
|
|
|
|
p.error('`$p.expr_var.name` needs to be a reference')
|
2019-11-30 11:09:05 +01:00
|
|
|
}
|
2019-11-25 06:38:00 +01:00
|
|
|
amp := if receiver.is_mut && !p.expr_var.ptr { '&' } else { '' }
|
|
|
|
if j > 0 {
|
2019-08-02 05:20:18 +02:00
|
|
|
p.gen(' else ')
|
2019-08-23 12:43:31 +02:00
|
|
|
}
|
2019-12-19 21:36:18 +01:00
|
|
|
p.genln('if ( string_eq($var, _STR("$method.name")) ) ' + '${typ.name}_$method.name ($amp $p.expr_var.name);')
|
2019-11-25 06:38:00 +01:00
|
|
|
j++
|
2019-08-23 12:43:31 +02:00
|
|
|
}
|
|
|
|
p.check(.lpar)
|
|
|
|
p.check(.rpar)
|
2019-08-02 04:04:48 +02:00
|
|
|
if p.tok == .key_orelse {
|
2019-08-23 12:43:31 +02:00
|
|
|
p.check(.key_orelse)
|
|
|
|
p.genln('else {')
|
2019-08-02 04:04:48 +02:00
|
|
|
p.check(.lcbr)
|
2019-08-23 12:43:31 +02:00
|
|
|
p.statements()
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
|
2019-08-31 15:38:13 +02:00
|
|
|
fn (p mut Parser) gen_array_str(typ Type) {
|
2019-10-25 21:09:23 +02:00
|
|
|
if typ.has_method('str') {
|
|
|
|
return
|
2019-11-30 11:09:05 +01:00
|
|
|
}
|
2019-09-18 14:37:34 +02:00
|
|
|
p.add_method(typ.name, Fn{
|
|
|
|
name: 'str'
|
2019-08-10 23:02:48 +02:00
|
|
|
typ: 'string'
|
2019-12-19 21:36:18 +01:00
|
|
|
args: [Var{
|
|
|
|
typ: typ.name
|
|
|
|
is_arg: true
|
|
|
|
}]
|
2019-08-23 12:43:31 +02:00
|
|
|
is_method: true
|
2019-08-22 04:00:06 +02:00
|
|
|
is_public: true
|
2019-08-23 12:43:31 +02:00
|
|
|
receiver_typ: typ.name
|
|
|
|
})
|
2019-12-21 23:44:16 +01:00
|
|
|
elm_type := parse_pointer(typ.name[6..])
|
2019-08-31 15:38:13 +02:00
|
|
|
elm_type2 := p.table.find_type(elm_type)
|
2019-10-17 05:52:58 +02:00
|
|
|
is_array := elm_type.starts_with('array_')
|
|
|
|
if is_array {
|
|
|
|
p.gen_array_str(elm_type2)
|
2019-12-19 21:36:18 +01:00
|
|
|
}
|
|
|
|
else if p.typ_to_fmt(elm_type, 0) == '' && !p.table.type_has_method(elm_type2, 'str') {
|
2019-08-22 06:56:29 +02:00
|
|
|
p.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
|
|
|
|
}
|
2019-09-27 13:02:01 +02:00
|
|
|
p.v.vgen_buf.writeln('
|
2019-12-11 01:24:26 +01:00
|
|
|
pub fn (a $typ.name) str() string {
|
2019-09-26 04:28:43 +02:00
|
|
|
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 12:43:31 +02:00
|
|
|
}
|
2019-09-26 04:28:43 +02:00
|
|
|
sb.write("]")
|
|
|
|
return sb.str()
|
2019-08-23 12:43:31 +02:00
|
|
|
}
|
2019-09-26 04:28:43 +02: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'
|
2019-12-19 21:36:18 +01:00
|
|
|
args: [Var{
|
|
|
|
typ: typ.name
|
|
|
|
is_arg: true
|
|
|
|
}]
|
2019-09-26 04:28:43 +02:00
|
|
|
is_method: true
|
|
|
|
is_public: true
|
|
|
|
receiver_typ: typ.name
|
|
|
|
})
|
|
|
|
mut sb := strings.new_builder(typ.fields.len * 20)
|
2019-12-11 01:24:26 +01:00
|
|
|
sb.writeln('pub fn (a $typ.name) str() string {\nreturn')
|
2019-09-26 04:28:43 +02:00
|
|
|
sb.writeln("'{")
|
|
|
|
for field in typ.fields {
|
2019-10-04 04:40:08 +02:00
|
|
|
sb.writeln('\t$field.name: $' + 'a.${field.name}')
|
2019-09-26 04:28:43 +02:00
|
|
|
}
|
2019-10-04 14:48:09 +02:00
|
|
|
sb.writeln("}'")
|
2019-09-26 04:28:43 +02:00
|
|
|
sb.writeln('}')
|
2019-09-27 13:02:01 +02:00
|
|
|
p.v.vgen_buf.writeln(sb.str())
|
2019-09-26 04:28:43 +02:00
|
|
|
// Need to manually add the definition to `fns` so that it stays
|
|
|
|
// at the top of the file.
|
2019-11-02 11:17:56 +01:00
|
|
|
// This function will get parsed by V after the main pass.
|
|
|
|
p.cgen.fns << 'string ${typ.name}_str();'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) gen_varg_str(typ Type) {
|
|
|
|
elm_type := typ.name[5..]
|
|
|
|
elm_type2 := p.table.find_type(elm_type)
|
|
|
|
is_array := elm_type.starts_with('array_')
|
|
|
|
if is_array {
|
|
|
|
p.gen_array_str(elm_type2)
|
2019-12-19 21:36:18 +01:00
|
|
|
}
|
|
|
|
else if elm_type2.cat == .struct_ {
|
2019-11-02 11:17:56 +01:00
|
|
|
p.gen_struct_str(elm_type2)
|
|
|
|
}
|
|
|
|
p.v.vgen_buf.writeln('
|
2019-12-11 01:24:26 +01:00
|
|
|
pub fn (a $typ.name) str() string {
|
2019-11-02 11:17:56 +01:00
|
|
|
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(", ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.write("]")
|
|
|
|
return sb.str()
|
|
|
|
}')
|
2019-09-26 04:28:43 +02:00
|
|
|
p.cgen.fns << 'string ${typ.name}_str();'
|
2019-08-23 12:43:31 +02:00
|
|
|
}
|
2019-10-18 06:58:54 +02: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)
|
2019-11-30 11:09:05 +01:00
|
|
|
|
2019-10-18 06:58:54 +02:00
|
|
|
// 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-12-21 23:44:16 +01:00
|
|
|
val_type := parse_pointer(str_typ[6..])
|
2019-10-18 06:58:54 +02: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
|
2019-12-19 21:36:18 +01:00
|
|
|
p.cgen.set_placeholder(method_ph, '\n$str_typ $tmp = new_array(0, $a .len,sizeof($val_type));\n')
|
2019-10-18 06:58:54 +02:00
|
|
|
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);')
|
2019-12-19 21:36:18 +01:00
|
|
|
// p.genln(') array_push(&$tmp, &((($val_type*)${a}.data)[i]));')
|
|
|
|
// p.genln(') array_push(&$tmp, ${a}.data + i * ${a}.element_size);')
|
2019-10-18 06:58:54 +02:00
|
|
|
p.genln('}')
|
|
|
|
p.gen(tmp) // TODO why does this `gen()` work?
|
|
|
|
p.check(.rpar)
|
|
|
|
p.close_scope()
|
|
|
|
}
|
2019-10-26 13:25:03 +02:00
|
|
|
|
2019-10-26 13:49:36 +02:00
|
|
|
fn (p mut Parser) gen_array_map(str_typ string, method_ph int) string {
|
2019-10-26 13:25:03 +02:00
|
|
|
/*
|
|
|
|
// V
|
|
|
|
a := [1,2,3,4]
|
|
|
|
b := a.map(it * 2)
|
2019-11-30 11:09:05 +01:00
|
|
|
|
2019-10-26 13:25:03 +02:00
|
|
|
// 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-12-21 23:44:16 +01:00
|
|
|
val_type := parse_pointer(str_typ[6..])
|
2019-10-26 13:25:03 +02: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
|
2019-12-19 21:36:18 +01:00
|
|
|
map_type,expr := p.tmp_expr()
|
|
|
|
p.cgen.set_placeholder(method_ph, '\narray $tmp = new_array(0, $a .len, ' + 'sizeof($map_type));\n')
|
2019-10-26 13:25:03 +02:00
|
|
|
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-12-21 23:44:16 +01:00
|
|
|
return 'array_' + stringify_pointer(map_type)
|
2019-10-26 13:25:03 +02:00
|
|
|
}
|
2019-11-04 18:14:34 +01:00
|
|
|
|
2020-01-16 15:28:23 +01:00
|
|
|
fn (p mut Parser) comptime_if_block(name string, not bool) {
|
|
|
|
if not {
|
|
|
|
p.genln('#ifndef $name')
|
|
|
|
}else{
|
|
|
|
p.genln('#ifdef $name')
|
|
|
|
}
|
2019-11-04 18:14:34 +01:00
|
|
|
p.check(.lcbr)
|
|
|
|
p.statements_no_rcbr()
|
2019-12-19 21:36:18 +01:00
|
|
|
if !(p.tok == .dollar && p.peek() == .key_else) {
|
2019-11-04 18:14:34 +01:00
|
|
|
p.genln('#endif')
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 04:16:47 +01:00
|
|
|
|
|
|
|
fn (p mut Parser) gen_enum_flag_methods(typ mut Type) {
|
|
|
|
for method in ['set', 'clear', 'toggle', 'has'] {
|
|
|
|
typ.methods << Fn{
|
2019-12-19 21:36:18 +01:00
|
|
|
name: method
|
2019-12-10 04:16:47 +01:00
|
|
|
typ: if method == 'has' { 'bool' } else { 'void' }
|
2019-12-19 21:36:18 +01:00
|
|
|
args: [Var{
|
|
|
|
typ: typ.name
|
|
|
|
is_mut: true
|
|
|
|
is_arg: true
|
|
|
|
}, Var{
|
|
|
|
typ: typ.name
|
|
|
|
is_arg: true
|
|
|
|
}]
|
2019-12-10 04:16:47 +01:00
|
|
|
is_method: true
|
|
|
|
is_public: true
|
|
|
|
receiver_typ: typ.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.v.vgen_buf.writeln('
|
|
|
|
pub fn (e mut $typ.name) set(flag $typ.name) { *e = int(*e) | (1 << int(flag)) }
|
|
|
|
pub fn (e mut $typ.name) clear(flag $typ.name) { *e = int(*e) &~ (1 << int(flag)) }
|
|
|
|
pub fn (e mut $typ.name) toggle(flag $typ.name) { *e = int(*e) ^ (1 << int(flag)) }
|
2019-12-19 21:36:18 +01:00
|
|
|
pub fn (e &$typ.name) has(flag $typ.name) bool { return int(*e)&(1 << int(flag)) != 0 }')
|
2019-12-10 04:16:47 +01:00
|
|
|
p.cgen.fns << 'void ${typ.name}_set($typ.name *e, $typ.name flag);'
|
|
|
|
p.cgen.fns << 'void ${typ.name}_clear($typ.name *e, $typ.name flag);'
|
|
|
|
p.cgen.fns << 'void ${typ.name}_toggle($typ.name *e, $typ.name flag);'
|
|
|
|
p.cgen.fns << 'bool ${typ.name}_has($typ.name *e, $typ.name flag);'
|
|
|
|
}
|
2019-12-19 21:36:18 +01:00
|
|
|
|