parser: change the new_parser() signature

pull/1722/head
Alexander Medvednikov 2019-08-23 13:43:31 +03:00
parent 624a932420
commit 0589decc43
3 changed files with 118 additions and 122 deletions

View File

@ -4,24 +4,24 @@
module main
import (
vweb.tmpl // for `$vweb_html()`
os
)
import (
vweb.tmpl // for `$vweb_html()`
os
)
fn (p mut Parser) comp_time() {
p.check(.dollar)
p.check(.dollar)
if p.tok == .key_if {
p.check(.key_if)
p.fspace()
not := p.tok == .not
p.check(.key_if)
p.fspace()
not := p.tok == .not
if not {
p.check(.not)
p.check(.not)
}
name := p.check_name()
p.fspace()
p.fspace()
if name in SupportedPlatforms {
ifdef_name := os_name_to_ifdef(name)
ifdef_name := os_name_to_ifdef(name)
if not {
p.genln('#ifndef $ifdef_name')
}
@ -85,55 +85,55 @@ fn (p mut Parser) comp_time() {
p.check(.rcbr)
// }
}
// $vweb.html()
// $vweb.html()
// Compile vweb html template to V code, parse that V code and embed the resulting V functions
// that returns an html string
// that returns an html string
else if p.tok == .name && p.lit == 'vweb' {
path := p.cur_fn.name + '.html'
path := p.cur_fn.name + '.html'
if p.pref.is_debug {
println('compiling tmpl $path')
}
println('compiling tmpl $path')
}
if !os.file_exists(path) {
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)
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)
if os.file_exists('.vwebtmpl.v') {
os.rm('.vwebtmpl.v')
}
os.write_file('.vwebtmpl.v', v_code.clone()) // TODO don't need clone, compiler bug
p.genln('')
os.rm('.vwebtmpl.v')
}
os.write_file('.vwebtmpl.v', v_code.clone()) // TODO don't need clone, compiler bug
p.genln('')
// Parse the function and embed resulting C code in current function so that
// all variables are available.
pos := p.cgen.lines.len - 1
mut pp := p.v.new_parser('.vwebtmpl.v', Pass.main)
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()
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)')
}
// 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)')
}
else {
p.error('bad comptime expr')
}
}
// #include, #flag, #v
// #include, #flag, #v
fn (p mut Parser) chash() {
hash := p.lit.trim_space()
// println('chsh() file=$p.file is_sig=${p.is_sig()} hash="$hash"')
@ -146,7 +146,7 @@ fn (p mut Parser) chash() {
if hash.contains('linux') && p.os != .linux {
return
}
else if hash.contains('darwin') && p.os != .mac {
else if hash.contains('darwin') && p.os != .mac {
return
}
else if hash.contains('windows') && (p.os != .windows && p.os != .msvc) {
@ -157,7 +157,7 @@ fn (p mut Parser) chash() {
pos := flag.index(' ')
flag = flag.right(pos)
}
has_vroot := flag.contains('@VROOT')
has_vroot := flag.contains('@VROOT')
flag = flag.trim_space().replace('@VROOT', p.vroot)
if p.table.flags.contains(flag) {
return
@ -169,16 +169,16 @@ fn (p mut Parser) chash() {
return
}
p.log('adding flag "$flag"')
// `@VROOT/thirdparty/glad/glad.o`, make sure it exists, otherwise build it
// `@VROOT/thirdparty/glad/glad.o`, make sure it exists, otherwise build it
if (has_vroot || has_vmod) && flag.contains('.o') {
if p.os == .msvc {
build_thirdparty_obj_file_with_msvc(flag)
}
}
else {
build_thirdparty_obj_file(flag)
}
}
p.table.flags << flag
}
p.table.flags << flag
return
}
if hash.starts_with('include') {
@ -200,14 +200,14 @@ fn (p mut Parser) chash() {
p.genln('#include $file')
}
}
else if hash.contains('define') {
// Move defines on top
else if hash.contains('define') {
// Move defines on top
p.cgen.includes << '#$hash'
}
}
else if hash == 'v' {
println('v script')
//p.v_script = true
}
println('v script')
//p.v_script = true
}
else {
if !p.can_chash {
p.error('bad token `#` (embedding C code is no longer supported)')
@ -216,66 +216,66 @@ fn (p mut Parser) chash() {
}
}
// `user.$method()` (`method` is a string)
// `user.$method()` (`method` is a string)
fn (p mut Parser) comptime_method_call(typ Type) {
p.cgen.cur_line = ''
p.cgen.cur_line = ''
p.check(.dollar)
var := p.check_name()
var := p.check_name()
for i, method in typ.methods {
if method.typ != 'void' {
continue
}
receiver := method.args[0]
amp := if receiver.is_mut { '&' } else { '' }
if i > 0 {
continue
}
receiver := method.args[0]
amp := if receiver.is_mut { '&' } else { '' }
if i > 0 {
p.gen(' else ')
}
p.gen('if ( string_eq($var, _STR("$method.name")) ) ${typ.name}_$method.name($amp $p.expr_var.name);')
}
p.check(.lpar)
p.check(.rpar)
}
p.gen('if ( string_eq($var, _STR("$method.name")) ) ${typ.name}_$method.name($amp $p.expr_var.name);')
}
p.check(.lpar)
p.check(.rpar)
if p.tok == .key_orelse {
p.check(.key_orelse)
p.genln('else {')
p.check(.key_orelse)
p.genln('else {')
p.check(.lcbr)
p.statements()
}
}
p.statements()
}
}
fn (p mut Parser) gen_array_str(typ mut Type) {
typ.add_method(Fn{
name: 'str',
typ: 'string'
args: [Var{typ: typ.name, is_arg:true}]
is_method: true
args: [Var{typ: typ.name, is_arg:true}]
is_method: true
is_public: true
receiver_typ: typ.name
})
t := typ.name
elm_type := t.right(6)
receiver_typ: typ.name
})
t := typ.name
elm_type := t.right(6)
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}')
}
p.cgen.fns << '
string ${t}_str($t a) {
strings__Builder sb = strings__new_builder(a.len * 3);
strings__Builder_write(&sb, tos2("[")) ;
strings__Builder sb = strings__new_builder(a.len * 3);
strings__Builder_write(&sb, tos2("[")) ;
for (int i = 0; i < a.len; i++) {
strings__Builder_write(&sb, ${elm_type}_str( (($elm_type *) a.data)[i]));
strings__Builder_write(&sb, ${elm_type}_str( (($elm_type *) a.data)[i]));
if (i < a.len - 1) {
strings__Builder_write(&sb, tos2(", ")) ;
}
}
strings__Builder_write(&sb, tos2("]")) ;
return strings__Builder_str(sb);
} '
}
strings__Builder_write(&sb, tos2(", ")) ;
}
}
strings__Builder_write(&sb, tos2("]")) ;
return strings__Builder_str(sb);
} '
}
fn (p mut Parser) parse_t() {
}
}

View File

@ -67,6 +67,7 @@ mut:
out_name string // "program.exe"
vroot string
mod string // module being built with -lib
//parsers []Parser
}
struct Preferences {
@ -222,6 +223,10 @@ fn (v mut V) compile() {
mut cgen := v.cgen
cgen.genln('// Generated by V')
// Add builtin parsers
for i, file in v.files {
// v.parsers << v.new_parser(file)
}
v.add_v_files_to_compile()
if v.pref.is_verbose {
println('all .v files:')
@ -229,8 +234,8 @@ fn (v mut V) compile() {
}
// First pass (declarations)
for file in v.files {
mut p := v.new_parser(file, Pass.decl)
p.parse()
mut p := v.new_parser(file)
p.parse(.decl)
}
// Main pass
cgen.pass = Pass.main
@ -243,7 +248,7 @@ fn (v mut V) compile() {
cgen.genln(CommonCHeaders)
v.generate_hotcode_reloading_declarations()
v.generate_hotcode_reloading_declarations()
imports_json := v.table.imports.contains('json')
// TODO remove global UI hack
@ -282,8 +287,8 @@ fn (v mut V) compile() {
cgen.genln('this line will be replaced with definitions')
defs_pos := cgen.lines.len - 1
for file in v.files {
mut p := v.new_parser(file, Pass.main)
p.parse()
mut p := v.new_parser(file)
p.parse(.main)
// p.g.gen_x64()
// Format all files (don't format automatically generated vlib headers)
if !v.pref.nofmt && !file.contains('/vlib/') {
@ -522,13 +527,13 @@ fn (v mut V) add_v_files_to_compile() {
}
// Parse builtin imports
for file in v.files {
mut p := v.new_parser(file, Pass.imports)
p.parse()
mut p := v.new_parser(file)
p.parse(.imports)
}
// Parse user imports
for file in user_files {
mut p := v.new_parser(file, Pass.imports)
p.parse()
mut p := v.new_parser(file)
p.parse(.imports)
}
// Parse lib imports
/*
@ -563,8 +568,8 @@ fn (v mut V) add_v_files_to_compile() {
}
// Add all imports referenced by these libs
for file in vfiles {
mut p := v.new_parser(file, Pass.imports)
p.parse()
mut p := v.new_parser(file)
p.parse(.imports)
}
}
if v.pref.is_verbose {

View File

@ -98,10 +98,8 @@ const (
MaxModuleDepth = 4
)
fn (v mut V) new_parser(path string, pass Pass) Parser {
v.log('new_parser("$path")')
v.cgen.pass = pass
fn (v mut V) new_parser(path string) Parser {
//println('new_parser("$path")')
mut path_pcguard := ''
mut path_platform := '.v'
for path_ending in ['_lin.v', '_mac.v', '_win.v', '_nix.v'] {
@ -126,7 +124,6 @@ fn (v mut V) new_parser(path string, pass Pass) Parser {
is_script: (v.pref.is_script && path == v.dir)
pref: v.pref
os: v.os
pass: pass
vroot: v.vroot
building_v: !v.pref.is_repl && (path.contains('compiler/') ||
path.contains('v/vlib'))
@ -159,7 +156,8 @@ fn (p &Parser) log(s string) {
*/
}
fn (p mut Parser) parse() {
fn (p mut Parser) parse(pass Pass) {
p.pass = pass
p.log('\nparse() run=$p.pass file=$p.file_name tok=${p.strtok()}')// , "script_file=", script_file)
// `module main` is not required if it's a single file program
if p.is_script || p.pref.is_test {
@ -3528,13 +3526,6 @@ fn (p mut Parser) js_decode() string {
return ''
}
/*
fn (p &Parser) building_v() bool {
cur_dir := os.getwd()
return p.file_path.contains('v/compiler') || cur_dir.contains('v/compiler')
}
*/
fn (p mut Parser) attribute() {
p.check(.lsbr)
if p.tok == .key_interface {