parser: change the new_parser() signature
parent
624a932420
commit
0589decc43
|
@ -4,24 +4,24 @@
|
||||||
|
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
vweb.tmpl // for `$vweb_html()`
|
vweb.tmpl // for `$vweb_html()`
|
||||||
os
|
os
|
||||||
)
|
)
|
||||||
|
|
||||||
fn (p mut Parser) comp_time() {
|
fn (p mut Parser) comp_time() {
|
||||||
p.check(.dollar)
|
p.check(.dollar)
|
||||||
if p.tok == .key_if {
|
if p.tok == .key_if {
|
||||||
p.check(.key_if)
|
p.check(.key_if)
|
||||||
p.fspace()
|
p.fspace()
|
||||||
not := p.tok == .not
|
not := p.tok == .not
|
||||||
if not {
|
if not {
|
||||||
p.check(.not)
|
p.check(.not)
|
||||||
}
|
}
|
||||||
name := p.check_name()
|
name := p.check_name()
|
||||||
p.fspace()
|
p.fspace()
|
||||||
if name in SupportedPlatforms {
|
if name in SupportedPlatforms {
|
||||||
ifdef_name := os_name_to_ifdef(name)
|
ifdef_name := os_name_to_ifdef(name)
|
||||||
if not {
|
if not {
|
||||||
p.genln('#ifndef $ifdef_name')
|
p.genln('#ifndef $ifdef_name')
|
||||||
}
|
}
|
||||||
|
@ -85,55 +85,55 @@ fn (p mut Parser) comp_time() {
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
// $vweb.html()
|
// $vweb.html()
|
||||||
// Compile vweb html template to V code, parse that V code and embed the resulting V functions
|
// 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' {
|
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 {
|
if p.pref.is_debug {
|
||||||
println('compiling tmpl $path')
|
println('compiling tmpl $path')
|
||||||
}
|
}
|
||||||
if !os.file_exists(path) {
|
if !os.file_exists(path) {
|
||||||
p.error('vweb HTML template "$path" not found')
|
p.error('vweb HTML template "$path" not found')
|
||||||
}
|
}
|
||||||
p.check(.name) // skip `vweb.html()` TODO
|
p.check(.name) // skip `vweb.html()` TODO
|
||||||
p.check(.dot)
|
p.check(.dot)
|
||||||
p.check(.name)
|
p.check(.name)
|
||||||
p.check(.lpar)
|
p.check(.lpar)
|
||||||
p.check(.rpar)
|
p.check(.rpar)
|
||||||
v_code := tmpl.compile_template(path)
|
v_code := tmpl.compile_template(path)
|
||||||
if os.file_exists('.vwebtmpl.v') {
|
if os.file_exists('.vwebtmpl.v') {
|
||||||
os.rm('.vwebtmpl.v')
|
os.rm('.vwebtmpl.v')
|
||||||
}
|
}
|
||||||
os.write_file('.vwebtmpl.v', v_code.clone()) // TODO don't need clone, compiler bug
|
os.write_file('.vwebtmpl.v', v_code.clone()) // TODO don't need clone, compiler bug
|
||||||
p.genln('')
|
p.genln('')
|
||||||
// Parse the function and embed resulting C code in current function so that
|
// Parse the function and embed resulting C code in current function so that
|
||||||
// all variables are available.
|
// all variables are available.
|
||||||
pos := p.cgen.lines.len - 1
|
pos := p.cgen.lines.len - 1
|
||||||
mut pp := p.v.new_parser('.vwebtmpl.v', Pass.main)
|
mut pp := p.v.new_parser('.vwebtmpl.v')
|
||||||
if !p.pref.is_debug {
|
if !p.pref.is_debug {
|
||||||
os.rm('.vwebtmpl.v')
|
os.rm('.vwebtmpl.v')
|
||||||
}
|
}
|
||||||
pp.is_vweb = true
|
pp.is_vweb = true
|
||||||
pp.cur_fn = p.cur_fn // give access too all variables in current function
|
pp.cur_fn = p.cur_fn // give access too all variables in current function
|
||||||
pp.parse()
|
pp.parse(.main)
|
||||||
tmpl_fn_body := p.cgen.lines.slice(pos + 2, p.cgen.lines.len).join('\n').clone()
|
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
|
end_pos := tmpl_fn_body.last_index('Builder_str( sb )') + 19 // TODO
|
||||||
p.cgen.lines = p.cgen.lines.left(pos)
|
p.cgen.lines = p.cgen.lines.left(pos)
|
||||||
p.genln('/////////////////// tmpl start')
|
p.genln('/////////////////// tmpl start')
|
||||||
p.genln(tmpl_fn_body.left(end_pos))
|
p.genln(tmpl_fn_body.left(end_pos))
|
||||||
p.genln('/////////////////// tmpl end')
|
p.genln('/////////////////// tmpl end')
|
||||||
// `app.vweb.html(index_view())`
|
// `app.vweb.html(index_view())`
|
||||||
receiver := p.cur_fn.args[0]
|
receiver := p.cur_fn.args[0]
|
||||||
dot := if receiver.is_mut { '->' } else { '.' }
|
dot := if receiver.is_mut { '->' } else { '.' }
|
||||||
p.genln('vweb__Context_html($receiver.name $dot vweb, tmpl_res)')
|
p.genln('vweb__Context_html($receiver.name $dot vweb, tmpl_res)')
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
p.error('bad comptime expr')
|
p.error('bad comptime expr')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// #include, #flag, #v
|
// #include, #flag, #v
|
||||||
fn (p mut Parser) chash() {
|
fn (p mut Parser) chash() {
|
||||||
hash := p.lit.trim_space()
|
hash := p.lit.trim_space()
|
||||||
// println('chsh() file=$p.file is_sig=${p.is_sig()} hash="$hash"')
|
// 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 {
|
if hash.contains('linux') && p.os != .linux {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
else if hash.contains('darwin') && p.os != .mac {
|
else if hash.contains('darwin') && p.os != .mac {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
else if hash.contains('windows') && (p.os != .windows && p.os != .msvc) {
|
else if hash.contains('windows') && (p.os != .windows && p.os != .msvc) {
|
||||||
|
@ -157,7 +157,7 @@ fn (p mut Parser) chash() {
|
||||||
pos := flag.index(' ')
|
pos := flag.index(' ')
|
||||||
flag = flag.right(pos)
|
flag = flag.right(pos)
|
||||||
}
|
}
|
||||||
has_vroot := flag.contains('@VROOT')
|
has_vroot := flag.contains('@VROOT')
|
||||||
flag = flag.trim_space().replace('@VROOT', p.vroot)
|
flag = flag.trim_space().replace('@VROOT', p.vroot)
|
||||||
if p.table.flags.contains(flag) {
|
if p.table.flags.contains(flag) {
|
||||||
return
|
return
|
||||||
|
@ -169,16 +169,16 @@ fn (p mut Parser) chash() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p.log('adding flag "$flag"')
|
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 (has_vroot || has_vmod) && flag.contains('.o') {
|
||||||
if p.os == .msvc {
|
if p.os == .msvc {
|
||||||
build_thirdparty_obj_file_with_msvc(flag)
|
build_thirdparty_obj_file_with_msvc(flag)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
build_thirdparty_obj_file(flag)
|
build_thirdparty_obj_file(flag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.table.flags << flag
|
p.table.flags << flag
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if hash.starts_with('include') {
|
if hash.starts_with('include') {
|
||||||
|
@ -200,14 +200,14 @@ fn (p mut Parser) chash() {
|
||||||
p.genln('#include $file')
|
p.genln('#include $file')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if hash.contains('define') {
|
else if hash.contains('define') {
|
||||||
// Move defines on top
|
// Move defines on top
|
||||||
p.cgen.includes << '#$hash'
|
p.cgen.includes << '#$hash'
|
||||||
}
|
}
|
||||||
else if hash == 'v' {
|
else if hash == 'v' {
|
||||||
println('v script')
|
println('v script')
|
||||||
//p.v_script = true
|
//p.v_script = true
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if !p.can_chash {
|
if !p.can_chash {
|
||||||
p.error('bad token `#` (embedding C code is no longer supported)')
|
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) {
|
fn (p mut Parser) comptime_method_call(typ Type) {
|
||||||
p.cgen.cur_line = ''
|
p.cgen.cur_line = ''
|
||||||
p.check(.dollar)
|
p.check(.dollar)
|
||||||
var := p.check_name()
|
var := p.check_name()
|
||||||
for i, method in typ.methods {
|
for i, method in typ.methods {
|
||||||
if method.typ != 'void' {
|
if method.typ != 'void' {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
receiver := method.args[0]
|
receiver := method.args[0]
|
||||||
amp := if receiver.is_mut { '&' } else { '' }
|
amp := if receiver.is_mut { '&' } else { '' }
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
p.gen(' else ')
|
p.gen(' else ')
|
||||||
}
|
}
|
||||||
p.gen('if ( string_eq($var, _STR("$method.name")) ) ${typ.name}_$method.name($amp $p.expr_var.name);')
|
p.gen('if ( string_eq($var, _STR("$method.name")) ) ${typ.name}_$method.name($amp $p.expr_var.name);')
|
||||||
}
|
}
|
||||||
p.check(.lpar)
|
p.check(.lpar)
|
||||||
p.check(.rpar)
|
p.check(.rpar)
|
||||||
if p.tok == .key_orelse {
|
if p.tok == .key_orelse {
|
||||||
p.check(.key_orelse)
|
p.check(.key_orelse)
|
||||||
p.genln('else {')
|
p.genln('else {')
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
p.statements()
|
p.statements()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) gen_array_str(typ mut Type) {
|
fn (p mut Parser) gen_array_str(typ mut Type) {
|
||||||
typ.add_method(Fn{
|
typ.add_method(Fn{
|
||||||
name: 'str',
|
name: 'str',
|
||||||
typ: 'string'
|
typ: 'string'
|
||||||
args: [Var{typ: typ.name, is_arg:true}]
|
args: [Var{typ: typ.name, is_arg:true}]
|
||||||
is_method: true
|
is_method: true
|
||||||
is_public: true
|
is_public: true
|
||||||
receiver_typ: typ.name
|
receiver_typ: typ.name
|
||||||
})
|
})
|
||||||
t := typ.name
|
t := typ.name
|
||||||
elm_type := t.right(6)
|
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') {
|
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.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
|
||||||
}
|
}
|
||||||
p.cgen.fns << '
|
p.cgen.fns << '
|
||||||
string ${t}_str($t a) {
|
string ${t}_str($t a) {
|
||||||
strings__Builder sb = strings__new_builder(a.len * 3);
|
strings__Builder sb = strings__new_builder(a.len * 3);
|
||||||
strings__Builder_write(&sb, tos2("[")) ;
|
strings__Builder_write(&sb, tos2("[")) ;
|
||||||
for (int i = 0; i < a.len; i++) {
|
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) {
|
if (i < a.len - 1) {
|
||||||
strings__Builder_write(&sb, tos2(", ")) ;
|
strings__Builder_write(&sb, tos2(", ")) ;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
strings__Builder_write(&sb, tos2("]")) ;
|
strings__Builder_write(&sb, tos2("]")) ;
|
||||||
return strings__Builder_str(sb);
|
return strings__Builder_str(sb);
|
||||||
} '
|
} '
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) parse_t() {
|
fn (p mut Parser) parse_t() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -67,6 +67,7 @@ mut:
|
||||||
out_name string // "program.exe"
|
out_name string // "program.exe"
|
||||||
vroot string
|
vroot string
|
||||||
mod string // module being built with -lib
|
mod string // module being built with -lib
|
||||||
|
//parsers []Parser
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Preferences {
|
struct Preferences {
|
||||||
|
@ -222,6 +223,10 @@ fn (v mut V) compile() {
|
||||||
|
|
||||||
mut cgen := v.cgen
|
mut cgen := v.cgen
|
||||||
cgen.genln('// Generated by V')
|
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()
|
v.add_v_files_to_compile()
|
||||||
if v.pref.is_verbose {
|
if v.pref.is_verbose {
|
||||||
println('all .v files:')
|
println('all .v files:')
|
||||||
|
@ -229,8 +234,8 @@ fn (v mut V) compile() {
|
||||||
}
|
}
|
||||||
// First pass (declarations)
|
// First pass (declarations)
|
||||||
for file in v.files {
|
for file in v.files {
|
||||||
mut p := v.new_parser(file, Pass.decl)
|
mut p := v.new_parser(file)
|
||||||
p.parse()
|
p.parse(.decl)
|
||||||
}
|
}
|
||||||
// Main pass
|
// Main pass
|
||||||
cgen.pass = Pass.main
|
cgen.pass = Pass.main
|
||||||
|
@ -243,7 +248,7 @@ fn (v mut V) compile() {
|
||||||
|
|
||||||
cgen.genln(CommonCHeaders)
|
cgen.genln(CommonCHeaders)
|
||||||
|
|
||||||
v.generate_hotcode_reloading_declarations()
|
v.generate_hotcode_reloading_declarations()
|
||||||
|
|
||||||
imports_json := v.table.imports.contains('json')
|
imports_json := v.table.imports.contains('json')
|
||||||
// TODO remove global UI hack
|
// TODO remove global UI hack
|
||||||
|
@ -282,8 +287,8 @@ fn (v mut V) compile() {
|
||||||
cgen.genln('this line will be replaced with definitions')
|
cgen.genln('this line will be replaced with definitions')
|
||||||
defs_pos := cgen.lines.len - 1
|
defs_pos := cgen.lines.len - 1
|
||||||
for file in v.files {
|
for file in v.files {
|
||||||
mut p := v.new_parser(file, Pass.main)
|
mut p := v.new_parser(file)
|
||||||
p.parse()
|
p.parse(.main)
|
||||||
// p.g.gen_x64()
|
// p.g.gen_x64()
|
||||||
// Format all files (don't format automatically generated vlib headers)
|
// Format all files (don't format automatically generated vlib headers)
|
||||||
if !v.pref.nofmt && !file.contains('/vlib/') {
|
if !v.pref.nofmt && !file.contains('/vlib/') {
|
||||||
|
@ -522,13 +527,13 @@ fn (v mut V) add_v_files_to_compile() {
|
||||||
}
|
}
|
||||||
// Parse builtin imports
|
// Parse builtin imports
|
||||||
for file in v.files {
|
for file in v.files {
|
||||||
mut p := v.new_parser(file, Pass.imports)
|
mut p := v.new_parser(file)
|
||||||
p.parse()
|
p.parse(.imports)
|
||||||
}
|
}
|
||||||
// Parse user imports
|
// Parse user imports
|
||||||
for file in user_files {
|
for file in user_files {
|
||||||
mut p := v.new_parser(file, Pass.imports)
|
mut p := v.new_parser(file)
|
||||||
p.parse()
|
p.parse(.imports)
|
||||||
}
|
}
|
||||||
// Parse lib imports
|
// Parse lib imports
|
||||||
/*
|
/*
|
||||||
|
@ -563,8 +568,8 @@ fn (v mut V) add_v_files_to_compile() {
|
||||||
}
|
}
|
||||||
// Add all imports referenced by these libs
|
// Add all imports referenced by these libs
|
||||||
for file in vfiles {
|
for file in vfiles {
|
||||||
mut p := v.new_parser(file, Pass.imports)
|
mut p := v.new_parser(file)
|
||||||
p.parse()
|
p.parse(.imports)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if v.pref.is_verbose {
|
if v.pref.is_verbose {
|
||||||
|
|
|
@ -98,10 +98,8 @@ const (
|
||||||
MaxModuleDepth = 4
|
MaxModuleDepth = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
fn (v mut V) new_parser(path string, pass Pass) Parser {
|
fn (v mut V) new_parser(path string) Parser {
|
||||||
v.log('new_parser("$path")')
|
//println('new_parser("$path")')
|
||||||
v.cgen.pass = pass
|
|
||||||
|
|
||||||
mut path_pcguard := ''
|
mut path_pcguard := ''
|
||||||
mut path_platform := '.v'
|
mut path_platform := '.v'
|
||||||
for path_ending in ['_lin.v', '_mac.v', '_win.v', '_nix.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)
|
is_script: (v.pref.is_script && path == v.dir)
|
||||||
pref: v.pref
|
pref: v.pref
|
||||||
os: v.os
|
os: v.os
|
||||||
pass: pass
|
|
||||||
vroot: v.vroot
|
vroot: v.vroot
|
||||||
building_v: !v.pref.is_repl && (path.contains('compiler/') ||
|
building_v: !v.pref.is_repl && (path.contains('compiler/') ||
|
||||||
path.contains('v/vlib'))
|
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)
|
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
|
// `module main` is not required if it's a single file program
|
||||||
if p.is_script || p.pref.is_test {
|
if p.is_script || p.pref.is_test {
|
||||||
|
@ -3528,13 +3526,6 @@ fn (p mut Parser) js_decode() string {
|
||||||
return ''
|
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() {
|
fn (p mut Parser) attribute() {
|
||||||
p.check(.lsbr)
|
p.check(.lsbr)
|
||||||
if p.tok == .key_interface {
|
if p.tok == .key_interface {
|
||||||
|
|
Loading…
Reference in New Issue