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.
|
|
|
|
|
|
|
|
module main
|
|
|
|
|
2019-08-23 12:43:31 +02:00
|
|
|
import (
|
|
|
|
vweb.tmpl // for `$vweb_html()`
|
|
|
|
os
|
|
|
|
)
|
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()
|
2019-07-29 18:21:36 +02:00
|
|
|
if name in SupportedPlatforms {
|
2019-08-23 12:43:31 +02:00
|
|
|
ifdef_name := os_name_to_ifdef(name)
|
2019-07-29 18:21:36 +02:00
|
|
|
if not {
|
|
|
|
p.genln('#ifndef $ifdef_name')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln('#ifdef $ifdef_name')
|
|
|
|
}
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.statements_no_rcbr()
|
|
|
|
if ! (p.tok == .dollar && p.peek() == .key_else) {
|
|
|
|
p.genln('#endif')
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 07:50:36 +02:00
|
|
|
else if name == 'debug' {
|
|
|
|
p.genln('#ifdef VDEBUG')
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.statements_no_rcbr()
|
|
|
|
p.genln('#endif')
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
else {
|
|
|
|
println('Supported platforms:')
|
|
|
|
println(SupportedPlatforms)
|
|
|
|
p.error('unknown platform `$name`')
|
|
|
|
}
|
2019-08-10 10:26:42 +02:00
|
|
|
if_returns := p.returns
|
|
|
|
p.returns = false
|
|
|
|
p.gen('/* returns $p.returns */')
|
|
|
|
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-08-16 07:50:36 +02:00
|
|
|
p.gen('/* returns $p.returns */')
|
2019-08-10 10:26:42 +02: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)
|
|
|
|
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 12:43:31 +02:00
|
|
|
// $vweb.html()
|
2019-07-29 18:21:36 +02:00
|
|
|
// Compile vweb html template to V code, parse that V code and embed the resulting V functions
|
2019-08-23 12:43:31 +02:00
|
|
|
// that returns an html string
|
2019-07-29 18:21:36 +02:00
|
|
|
else if p.tok == .name && p.lit == 'vweb' {
|
2019-08-23 12:43:31 +02:00
|
|
|
path := p.cur_fn.name + '.html'
|
2019-07-31 03:41:39 +02:00
|
|
|
if p.pref.is_debug {
|
2019-08-23 12:43:31 +02:00
|
|
|
println('compiling tmpl $path')
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
if !os.file_exists(path) {
|
2019-08-23 12:43:31 +02:00
|
|
|
p.error('vweb HTML template "$path" not found')
|
|
|
|
}
|
|
|
|
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 03:41:39 +02:00
|
|
|
if os.file_exists('.vwebtmpl.v') {
|
2019-08-23 12:43:31 +02: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 18:21:36 +02:00
|
|
|
// Parse the function and embed resulting C code in current function so that
|
2019-08-23 12:43:31 +02:00
|
|
|
// all variables are available.
|
|
|
|
pos := p.cgen.lines.len - 1
|
|
|
|
mut pp := p.v.new_parser('.vwebtmpl.v')
|
|
|
|
if !p.pref.is_debug {
|
|
|
|
os.rm('.vwebtmpl.v')
|
|
|
|
}
|
|
|
|
pp.is_vweb = true
|
|
|
|
pp.cur_fn = p.cur_fn // give access too all variables in current function
|
|
|
|
pp.parse(.main)
|
|
|
|
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
|
|
|
|
p.cgen.lines = p.cgen.lines.left(pos)
|
|
|
|
p.genln('/////////////////// tmpl start')
|
|
|
|
p.genln(tmpl_fn_body.left(end_pos))
|
|
|
|
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 18:21:36 +02:00
|
|
|
else {
|
|
|
|
p.error('bad comptime expr')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
// println('chsh() file=$p.file is_sig=${p.is_sig()} hash="$hash"')
|
|
|
|
p.next()
|
|
|
|
is_sig := p.is_sig()
|
|
|
|
if hash.starts_with('flag ') {
|
|
|
|
mut flag := hash.right(5)
|
|
|
|
// No the right os? Skip!
|
|
|
|
// mut ok := true
|
|
|
|
if hash.contains('linux') && p.os != .linux {
|
|
|
|
return
|
|
|
|
}
|
2019-08-23 12:43:31 +02:00
|
|
|
else if hash.contains('darwin') && p.os != .mac {
|
2019-07-29 18:21:36 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
else if hash.contains('windows') && (p.os != .windows && p.os != .msvc) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Remove "linux" etc from flag
|
|
|
|
if flag.contains('linux') || flag.contains('darwin') || flag.contains('windows') {
|
|
|
|
pos := flag.index(' ')
|
|
|
|
flag = flag.right(pos)
|
|
|
|
}
|
2019-08-23 12:43:31 +02:00
|
|
|
has_vroot := flag.contains('@VROOT')
|
2019-07-29 18:21:36 +02:00
|
|
|
flag = flag.trim_space().replace('@VROOT', p.vroot)
|
|
|
|
if p.table.flags.contains(flag) {
|
|
|
|
return
|
|
|
|
}
|
2019-08-05 09:42:50 +02:00
|
|
|
// expand `@VMOD/pg/pg.o` to absolute path
|
|
|
|
has_vmod := flag.contains('@VMOD')
|
|
|
|
flag = flag.trim_space().replace('@VMOD', ModPath)
|
|
|
|
if p.table.flags.contains(flag) {
|
|
|
|
return
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
p.log('adding flag "$flag"')
|
2019-08-23 12:43:31 +02:00
|
|
|
// `@VROOT/thirdparty/glad/glad.o`, make sure it exists, otherwise build it
|
2019-08-05 09:42:50 +02:00
|
|
|
if (has_vroot || has_vmod) && flag.contains('.o') {
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.os == .msvc {
|
|
|
|
build_thirdparty_obj_file_with_msvc(flag)
|
2019-08-23 12:43:31 +02:00
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
else {
|
|
|
|
build_thirdparty_obj_file(flag)
|
|
|
|
}
|
2019-08-23 12:43:31 +02:00
|
|
|
}
|
|
|
|
p.table.flags << flag
|
2019-07-29 18:21:36 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if hash.starts_with('include') {
|
|
|
|
if p.first_pass() && !is_sig {
|
2019-08-04 20:39:43 +02: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 18:21:36 +02:00
|
|
|
p.cgen.includes << '#$hash'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO remove after ui_mac.m is removed
|
|
|
|
else if hash.contains('embed') {
|
|
|
|
pos := hash.index('embed') + 5
|
|
|
|
file := hash.right(pos)
|
|
|
|
if p.pref.build_mode != BuildMode.default_mode {
|
|
|
|
p.genln('#include $file')
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 12:43:31 +02:00
|
|
|
else if hash.contains('define') {
|
|
|
|
// Move defines on top
|
2019-07-29 18:21:36 +02:00
|
|
|
p.cgen.includes << '#$hash'
|
2019-08-23 12:43:31 +02:00
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
else if hash == 'v' {
|
2019-08-23 12:43:31 +02:00
|
|
|
println('v script')
|
|
|
|
//p.v_script = true
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
else {
|
|
|
|
if !p.can_chash {
|
|
|
|
p.error('bad token `#` (embedding C code is no longer supported)')
|
|
|
|
}
|
|
|
|
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-08-02 05:20:18 +02:00
|
|
|
for i, 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]
|
|
|
|
amp := if receiver.is_mut { '&' } else { '' }
|
|
|
|
if i > 0 {
|
2019-08-02 05:20:18 +02:00
|
|
|
p.gen(' else ')
|
2019-08-23 12:43:31 +02: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 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-10 23:02:48 +02:00
|
|
|
fn (p mut Parser) gen_array_str(typ mut Type) {
|
|
|
|
typ.add_method(Fn{
|
|
|
|
name: 'str',
|
|
|
|
typ: 'string'
|
2019-08-23 12:43:31 +02:00
|
|
|
args: [Var{typ: typ.name, is_arg:true}]
|
|
|
|
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
|
|
|
|
})
|
|
|
|
t := typ.name
|
|
|
|
elm_type := t.right(6)
|
2019-08-22 06:56:29 +02:00
|
|
|
if p.typ_to_fmt(elm_type, 0) == '' && !p.table.type_has_method(p.table.find_type(elm_type), 'str') {
|
|
|
|
p.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
|
|
|
|
}
|
2019-08-10 23:02:48 +02:00
|
|
|
p.cgen.fns << '
|
|
|
|
string ${t}_str($t a) {
|
2019-08-23 12:43:31 +02:00
|
|
|
strings__Builder sb = strings__new_builder(a.len * 3);
|
|
|
|
strings__Builder_write(&sb, tos2("[")) ;
|
2019-08-10 23:02:48 +02:00
|
|
|
for (int i = 0; i < a.len; i++) {
|
2019-08-23 12:43:31 +02:00
|
|
|
strings__Builder_write(&sb, ${elm_type}_str( (($elm_type *) a.data)[i]));
|
2019-08-10 23:02:48 +02:00
|
|
|
|
|
|
|
if (i < a.len - 1) {
|
2019-08-23 12:43:31 +02:00
|
|
|
strings__Builder_write(&sb, tos2(", ")) ;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strings__Builder_write(&sb, tos2("]")) ;
|
|
|
|
return strings__Builder_str(sb);
|
|
|
|
} '
|
|
|
|
}
|
2019-08-10 23:02:48 +02:00
|
|
|
|
2019-08-13 13:50:19 +02:00
|
|
|
fn (p mut Parser) parse_t() {
|
2019-08-23 12:43:31 +02:00
|
|
|
|
|
|
|
}
|
2019-08-13 13:50:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
|