vweb: fix unused tmpl warning; remove duplicate code
parent
808975fc86
commit
1bbfc271c5
|
@ -3636,36 +3636,6 @@ fn (g Gen) is_importing_os() bool {
|
||||||
return 'os' in g.table.imports
|
return 'os' in g.table.imports
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut g Gen) comp_if(it ast.CompIf) {
|
|
||||||
ifdef := g.comp_if_to_ifdef(it.val, it.is_opt)
|
|
||||||
if it.is_not {
|
|
||||||
g.writeln('\n// \$if !${it.val} {\n#ifndef ' + ifdef)
|
|
||||||
} else {
|
|
||||||
g.writeln('\n// \$if ${it.val} {\n#ifdef ' + ifdef)
|
|
||||||
}
|
|
||||||
// NOTE: g.defer_ifdef is needed for defers called witin an ifdef
|
|
||||||
// in v1 this code would be completely excluded
|
|
||||||
g.defer_ifdef = if it.is_not {
|
|
||||||
'\n#ifndef ' + ifdef
|
|
||||||
} else {
|
|
||||||
'\n#ifdef ' + ifdef
|
|
||||||
}
|
|
||||||
// println('comp if stmts $g.file.path:$it.pos.line_nr')
|
|
||||||
g.stmts(it.stmts)
|
|
||||||
g.defer_ifdef = ''
|
|
||||||
if it.has_else {
|
|
||||||
g.writeln('\n#else')
|
|
||||||
g.defer_ifdef = if it.is_not {
|
|
||||||
'\n#ifdef ' + ifdef
|
|
||||||
} else {
|
|
||||||
'\n#ifndef ' + ifdef
|
|
||||||
}
|
|
||||||
g.stmts(it.else_stmts)
|
|
||||||
g.defer_ifdef = ''
|
|
||||||
}
|
|
||||||
g.writeln('\n// } ${it.val}\n#endif\n')
|
|
||||||
}
|
|
||||||
|
|
||||||
fn (mut g Gen) go_stmt(node ast.GoStmt) {
|
fn (mut g Gen) go_stmt(node ast.GoStmt) {
|
||||||
tmp := g.new_tmp_var()
|
tmp := g.new_tmp_var()
|
||||||
// x := node.call_expr as ast.CallEpxr // TODO
|
// x := node.call_expr as ast.CallEpxr // TODO
|
||||||
|
@ -4349,43 +4319,6 @@ fn (mut g Gen) interface_call(typ, interface_type table.Type) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g &Gen) comptime_call(node ast.ComptimeCall) {
|
|
||||||
if node.is_vweb {
|
|
||||||
for stmt in node.vweb_tmpl.stmts {
|
|
||||||
if stmt is ast.FnDecl {
|
|
||||||
fn_decl := stmt as ast.FnDecl
|
|
||||||
// insert stmts from vweb_tmpl fn
|
|
||||||
if fn_decl.name == 'vweb_tmpl' {
|
|
||||||
g.stmts(fn_decl.stmts)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
g.writeln('vweb__Context_html(&app-> vweb, tmpl_res)')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
g.writeln('// $' + 'method call. sym="$node.sym.name"')
|
|
||||||
mut j := 0
|
|
||||||
for method in node.sym.methods {
|
|
||||||
if method.return_type != table.void_type {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// receiver := method.args[0]
|
|
||||||
// if !p.expr_var.ptr {
|
|
||||||
// p.error('`$p.expr_var.name` needs to be a reference')
|
|
||||||
// }
|
|
||||||
amp := '' // if receiver.is_mut && !p.expr_var.ptr { '&' } else { '' }
|
|
||||||
if j > 0 {
|
|
||||||
g.write(' else ')
|
|
||||||
}
|
|
||||||
g.write('if (string_eq($node.method_name, tos_lit("$method.name"))) ')
|
|
||||||
g.write('${node.sym.name}_$method.name ($amp ')
|
|
||||||
g.expr(node.left)
|
|
||||||
g.writeln(');')
|
|
||||||
j++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn (mut g Gen) panic_debug_info(pos token.Position) (int, string, string, string) {
|
fn (mut g Gen) panic_debug_info(pos token.Position) (int, string, string, string) {
|
||||||
paline := pos.line_nr + 1
|
paline := pos.line_nr + 1
|
||||||
pafile := g.fn_decl.file.replace('\\', '/')
|
pafile := g.fn_decl.file.replace('\\', '/')
|
||||||
|
|
|
@ -3,47 +3,72 @@
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
module gen
|
module gen
|
||||||
|
|
||||||
import vweb.tmpl
|
import v.ast
|
||||||
import os
|
import v.table
|
||||||
|
|
||||||
// $vweb.html()
|
fn (g &Gen) comptime_call(node ast.ComptimeCall) {
|
||||||
fn (mut g Gen) vweb_html() {
|
if node.is_vweb {
|
||||||
// Compile vweb html template to V code, parse that V code and embed the resulting V functions
|
for stmt in node.vweb_tmpl.stmts {
|
||||||
// that returns an html string
|
if stmt is ast.FnDecl {
|
||||||
mut path := g.cur_fn.name + '.html'
|
fn_decl := stmt as ast.FnDecl
|
||||||
println('html path=$path')
|
// insert stmts from vweb_tmpl fn
|
||||||
if g.pref.is_debug {
|
if fn_decl.name == 'vweb_tmpl' {
|
||||||
println('>>> compiling vweb HTML template "$path"')
|
g.stmts(fn_decl.stmts)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g.writeln('vweb__Context_html(&app->vweb, _tmpl_res)')
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if !os.exists(path) {
|
g.writeln('// $' + 'method call. sym="$node.sym.name"')
|
||||||
// Can't find the template file in current directory,
|
mut j := 0
|
||||||
// try looking next to the vweb program, in case it's run with
|
for method in node.sym.methods {
|
||||||
// v path/to/vweb_app.v
|
if method.return_type != table.void_type {
|
||||||
// path = os.dir(g.scanner.file_path) + '/' + path
|
continue
|
||||||
// if !os.exists(path) {
|
}
|
||||||
verror('vweb HTML template "$path" not found')
|
// receiver := method.args[0]
|
||||||
|
// if !p.expr_var.ptr {
|
||||||
|
// p.error('`$p.expr_var.name` needs to be a reference')
|
||||||
// }
|
// }
|
||||||
|
amp := '' // if receiver.is_mut && !p.expr_var.ptr { '&' } else { '' }
|
||||||
|
if j > 0 {
|
||||||
|
g.write(' else ')
|
||||||
|
}
|
||||||
|
g.write('if (string_eq($node.method_name, tos_lit("$method.name"))) ')
|
||||||
|
g.write('${node.sym.name}_$method.name ($amp ')
|
||||||
|
g.expr(node.left)
|
||||||
|
g.writeln(');')
|
||||||
|
j++
|
||||||
}
|
}
|
||||||
v_code := tmpl.compile_template(path)
|
|
||||||
if g.pref.is_verbose {
|
|
||||||
println('\n\n')
|
|
||||||
println('>>> vweb template for ${path}:')
|
|
||||||
println(v_code)
|
|
||||||
println('>>> vweb template END')
|
|
||||||
println('\n\n')
|
|
||||||
}
|
|
||||||
// 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')
|
|
||||||
g.writeln('/////////////////// tmpl start')
|
|
||||||
// g.statements_from_text(v_code, false, path)
|
|
||||||
g.writeln('/////////////////// tmpl end')
|
|
||||||
receiver := g.cur_fn.args[0]
|
|
||||||
dot := '.' // if receiver.is_mut || receiver.ptr || receiver.typ.ends_with('*') { '->' } else { '.' }
|
|
||||||
g.writeln('vweb__Context_html(&$receiver.name /*!*/$dot vweb, tmpl_res)')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fooo() {
|
fn (mut g Gen) comp_if(it ast.CompIf) {
|
||||||
|
ifdef := g.comp_if_to_ifdef(it.val, it.is_opt)
|
||||||
|
if it.is_not {
|
||||||
|
g.writeln('\n// \$if !${it.val} {\n#ifndef ' + ifdef)
|
||||||
|
} else {
|
||||||
|
g.writeln('\n// \$if ${it.val} {\n#ifdef ' + ifdef)
|
||||||
|
}
|
||||||
|
// NOTE: g.defer_ifdef is needed for defers called witin an ifdef
|
||||||
|
// in v1 this code would be completely excluded
|
||||||
|
g.defer_ifdef = if it.is_not {
|
||||||
|
'\n#ifndef ' + ifdef
|
||||||
|
} else {
|
||||||
|
'\n#ifdef ' + ifdef
|
||||||
|
}
|
||||||
|
// println('comp if stmts $g.file.path:$it.pos.line_nr')
|
||||||
|
g.stmts(it.stmts)
|
||||||
|
g.defer_ifdef = ''
|
||||||
|
if it.has_else {
|
||||||
|
g.writeln('\n#else')
|
||||||
|
g.defer_ifdef = if it.is_not {
|
||||||
|
'\n#ifdef ' + ifdef
|
||||||
|
} else {
|
||||||
|
'\n#ifndef ' + ifdef
|
||||||
|
}
|
||||||
|
g.stmts(it.else_stmts)
|
||||||
|
g.defer_ifdef = ''
|
||||||
|
}
|
||||||
|
g.writeln('\n// } ${it.val}\n#endif\n')
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
|
||||||
return p.stmt()
|
return p.stmt()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_text(text string, b_table &table.Table, scope &ast.Scope, global_scope &ast.Scope) ast.File {
|
pub fn parse_text(text string, b_table &table.Table, scope, global_scope &ast.Scope) ast.File {
|
||||||
s := scanner.new_scanner(text, .skip_comments)
|
s := scanner.new_scanner(text, .skip_comments)
|
||||||
mut p := Parser{
|
mut p := Parser{
|
||||||
scanner: s
|
scanner: s
|
||||||
|
@ -94,7 +94,6 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme
|
||||||
// the parser gives feedback to the scanner about toplevel statements, so that the scanner can skip
|
// the parser gives feedback to the scanner about toplevel statements, so that the scanner can skip
|
||||||
// all the tricky inner comments. This is needed because we do not have a good general solution
|
// all the tricky inner comments. This is needed because we do not have a good general solution
|
||||||
// for handling them, and should be removed when we do (the general solution is also needed for vfmt)
|
// for handling them, and should be removed when we do (the general solution is also needed for vfmt)
|
||||||
|
|
||||||
// println('parse_file("$path")')
|
// println('parse_file("$path")')
|
||||||
// text := os.read_file(path) or {
|
// text := os.read_file(path) or {
|
||||||
// panic(err)
|
// panic(err)
|
||||||
|
@ -273,7 +272,7 @@ pub fn (mut p Parser) close_scope() {
|
||||||
for _, obj in p.scope.objects {
|
for _, obj in p.scope.objects {
|
||||||
match obj {
|
match obj {
|
||||||
ast.Var {
|
ast.Var {
|
||||||
if !it.is_used && !it.name.starts_with('__') {
|
if !it.is_used && it.name[0] != `_` {
|
||||||
if p.pref.is_prod {
|
if p.pref.is_prod {
|
||||||
p.error_with_pos('unused variable: `$it.name`', it.pos)
|
p.error_with_pos('unused variable: `$it.name`', it.pos)
|
||||||
} else {
|
} else {
|
||||||
|
@ -738,15 +737,10 @@ fn (mut p Parser) parse_multi_expr() ast.Stmt {
|
||||||
mut idents := []ast.Ident{}
|
mut idents := []ast.Ident{}
|
||||||
for c in collected {
|
for c in collected {
|
||||||
match c {
|
match c {
|
||||||
ast.Ident {
|
ast.Ident { idents << it }
|
||||||
idents << it
|
ast.SelectorExpr { p.error_with_pos('struct fields can only be declared during the initialization',
|
||||||
}
|
it.pos) }
|
||||||
ast.SelectorExpr {
|
else { p.error_with_pos('unexpected `${typeof(c)}`', c.position()) }
|
||||||
p.error_with_pos('struct fields can only be declared during the initialization', it.pos)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
p.error_with_pos('unexpected `${typeof(c)}`', c.position())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return p.partial_assign_stmt(idents)
|
return p.partial_assign_stmt(idents)
|
||||||
|
@ -1650,7 +1644,7 @@ fn (mut p Parser) rewind_scanner_to_current_token_in_new_mode() {
|
||||||
p.peek_tok3 = no_token
|
p.peek_tok3 = no_token
|
||||||
for {
|
for {
|
||||||
p.next()
|
p.next()
|
||||||
//eprintln('rewinding to ${p.tok.tidx:5} | goal: ${tidx:5}')
|
// eprintln('rewinding to ${p.tok.tidx:5} | goal: ${tidx:5}')
|
||||||
if tidx == p.tok.tidx {
|
if tidx == p.tok.tidx {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,7 +100,7 @@ pub fn compile_template(content string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.writeln(str_end)
|
s.writeln(str_end)
|
||||||
s.writeln('tmpl_res := sb.str() ')
|
s.writeln('_tmpl_res := sb.str() ')
|
||||||
s.writeln('}')
|
s.writeln('}')
|
||||||
s.writeln('// === end of vweb html template ===')
|
s.writeln('// === end of vweb html template ===')
|
||||||
return s.str()
|
return s.str()
|
||||||
|
|
Loading…
Reference in New Issue