vweb: return $vweb.html()

pull/5439/head
Alexander Medvednikov 2020-06-20 03:12:35 +02:00
parent e41ddab3b0
commit 1d8d19c977
7 changed files with 32 additions and 26 deletions

View File

@ -28,13 +28,13 @@ pub fn (mut app App) json_endpoint() {
app.vweb.json('{"a": 3}')
}
pub fn (mut app App) index() {
pub fn (mut app App) index() vweb.Result {
app.cnt++
show := true
//app.vweb.text('Hello world from vweb')
hello := 'Hello world from vweb'
numbers := [1,2,3]
$vweb.html()
return $vweb.html()
}
pub fn (mut app App) text() {

View File

@ -518,14 +518,6 @@ pub:
pos token.Position
}
/*
pub struct ReturnStmt {
pub:
tok_kind token.Kind // or pos
results []Expr
pos token.Position
}
*/
// #include etc
pub struct HashStmt {
pub:

View File

@ -1929,7 +1929,8 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
c.nr_warnings += c2.nr_warnings
c.nr_errors += c2.nr_errors
}
return table.void_type
return c.table.find_type_idx('vweb.Result')
// return table.void_type
}
ast.ConcatExpr {
return c.concat_expr(mut node)

View File

@ -2296,6 +2296,14 @@ fn (mut g Gen) return_statement(node ast.Return) {
g.writeln('return 0;')
return
}
if node.exprs.len > 0 {
// skip `retun $vweb.html()`
if node.exprs[0] is ast.ComptimeCall {
g.expr(node.exprs[0])
g.writeln(';')
return
}
}
// got to do a correct check for multireturn
sym := g.table.get_type_symbol(g.fn_decl.return_type)
fn_return_is_multi := sym.kind == .multi_return

View File

@ -23,8 +23,11 @@ fn (g &Gen) comptime_call(node ast.ComptimeCall) {
}
g.writeln('// $' + 'method call. sym="$node.sym.name"')
mut j := 0
result_type := g.table.find_type_idx('vweb.Result')
println('!!!!! $result_type')
for method in node.sym.methods {
if method.return_type != table.void_type {
// if method.return_type != table.void_type {
if method.return_type != result_type {
continue
}
// receiver := method.args[0]
@ -36,7 +39,7 @@ fn (g &Gen) comptime_call(node ast.ComptimeCall) {
g.write(' else ')
}
g.write('if (string_eq($node.method_name, tos_lit("$method.name"))) ')
g.write('${node.sym.name}_$method.name ($amp ')
g.write('${node.sym.name}_${method.name}($amp ')
g.expr(node.left)
g.writeln(');')
j++
@ -46,29 +49,21 @@ fn (g &Gen) comptime_call(node ast.ComptimeCall) {
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)
g.writeln('\n// \$if !$it.val {\n#ifndef ' + ifdef)
} else {
g.writeln('\n// \$if ${it.val} {\n#ifdef ' + ifdef)
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
}
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.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')
g.writeln('\n// } $it.val\n#endif\n')
}

View File

@ -34,6 +34,13 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
// .enum_val
node = p.enum_val()
}
.dollar {
if p.peek_tok.kind == .name {
return p.vweb()
} else {
p.error('unexpected $')
}
}
.chartoken {
node = ast.CharLiteral{
val: p.tok.lit

View File

@ -49,6 +49,8 @@ pub mut:
done bool
}
pub struct Result {}
fn (mut ctx Context) send_response_to_client(mimetype string, res string) bool {
if ctx.done { return false }
ctx.done = true
@ -372,6 +374,7 @@ pub fn (mut ctx Context) handle_static(directory_path string) bool {
return true
}
pub fn (mut ctx Context) serve_static(url, file_path, mime_type string) {
ctx.static_files[url] = file_path
ctx.static_mime_types[url] = mime_type