diff --git a/examples/vweb/vweb_example.v b/examples/vweb/vweb_example.v index 158ef62461..8b34242d05 100644 --- a/examples/vweb/vweb_example.v +++ b/examples/vweb/vweb_example.v @@ -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() { diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index f12428f98d..152928ef27 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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: diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 9f2f7e3638..abe4d7c28c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 27af60e22f..3848d9eda6 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -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 diff --git a/vlib/v/gen/comptime.v b/vlib/v/gen/comptime.v index c40114437f..e3d91e3b19 100644 --- a/vlib/v/gen/comptime.v +++ b/vlib/v/gen/comptime.v @@ -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') } diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index c6f034d4f5..457d6e70c4 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -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 diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 2ddb4297ee..00c2b4be3c 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -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