vweb: fix unused tmpl warning; remove duplicate code

pull/5284/head
Alexander Medvednikov 2020-06-08 13:30:17 +02:00
parent 808975fc86
commit 1bbfc271c5
4 changed files with 70 additions and 118 deletions

View File

@ -3636,36 +3636,6 @@ fn (g Gen) is_importing_os() bool {
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) {
tmp := g.new_tmp_var()
// 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) {
paline := pos.line_nr + 1
pafile := g.fn_decl.file.replace('\\', '/')

View File

@ -3,47 +3,72 @@
// that can be found in the LICENSE file.
module gen
import vweb.tmpl
import os
import v.ast
import v.table
// $vweb.html()
fn (mut g Gen) vweb_html() {
// Compile vweb html template to V code, parse that V code and embed the resulting V functions
// that returns an html string
mut path := g.cur_fn.name + '.html'
println('html path=$path')
if g.pref.is_debug {
println('>>> compiling vweb HTML template "$path"')
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
}
if !os.exists(path) {
// 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
// path = os.dir(g.scanner.file_path) + '/' + path
// if !os.exists(path) {
verror('vweb HTML template "$path" not found')
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++
}
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')
}

View File

@ -75,7 +75,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.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)
mut p := Parser{
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
// 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)
// println('parse_file("$path")')
// text := os.read_file(path) or {
// panic(err)
@ -273,7 +272,7 @@ pub fn (mut p Parser) close_scope() {
for _, obj in p.scope.objects {
match obj {
ast.Var {
if !it.is_used && !it.name.starts_with('__') {
if !it.is_used && it.name[0] != `_` {
if p.pref.is_prod {
p.error_with_pos('unused variable: `$it.name`', it.pos)
} else {
@ -738,15 +737,10 @@ fn (mut p Parser) parse_multi_expr() ast.Stmt {
mut idents := []ast.Ident{}
for c in collected {
match c {
ast.Ident {
idents << it
}
ast.SelectorExpr {
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())
}
ast.Ident { idents << it }
ast.SelectorExpr { 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)
@ -1650,7 +1644,7 @@ fn (mut p Parser) rewind_scanner_to_current_token_in_new_mode() {
p.peek_tok3 = no_token
for {
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 {
break
}

View File

@ -100,7 +100,7 @@ pub fn compile_template(content string) string {
}
}
s.writeln(str_end)
s.writeln('tmpl_res := sb.str() ')
s.writeln('_tmpl_res := sb.str() ')
s.writeln('}')
s.writeln('// === end of vweb html template ===')
return s.str()