diff --git a/examples/vweb/vweb_example.v b/examples/vweb/vweb_example.v index 9bbdd0ee15..4ddaa9a928 100644 --- a/examples/vweb/vweb_example.v +++ b/examples/vweb/vweb_example.v @@ -27,7 +27,8 @@ pub fn (mut app App) json_endpoint() { pub fn (mut app App) index() { app.cnt++ - $vweb.html() + app.vweb.text('Hello world from vweb') + //$vweb.html() } pub fn (mut app App) reset() { diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 049f020537..1b0598ef87 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -778,8 +778,11 @@ pub mut: } pub struct ComptimeCall { - name string - left Expr +pub: + method_name string + left Expr +pub mut: + sym table.TypeSymbol } pub struct None { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index aab536b47f..9529be3789 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1258,10 +1258,11 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) { } for i, exp_type in expected_types { got_typ := c.unwrap_generic(got_types[i]) - if got_typ.has_flag(.optional) && - (!exp_type.has_flag(.optional) || c.table.type_to_str(got_typ) != c.table.type_to_str(exp_type)) { + if got_typ.has_flag(.optional) && (!exp_type.has_flag(.optional) || c.table.type_to_str(got_typ) != + c.table.type_to_str(exp_type)) { pos := return_stmt.exprs[i].position() - c.error('cannot use `${c.table.type_to_str(got_typ)}` as type `${c.table.type_to_str(exp_type)}` in return argument', pos) + c.error('cannot use `${c.table.type_to_str(got_typ)}` as type `${c.table.type_to_str(exp_type)}` in return argument', + pos) } if !c.check_types(got_typ, exp_type) { got_typ_sym := c.table.get_type_symbol(got_typ) @@ -1825,6 +1826,10 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type { ast.CharLiteral { return table.byte_type } + ast.ComptimeCall { + it.sym = c.table.get_type_symbol(c.unwrap_generic(c.expr(it.left))) + return table.void_type + } ast.ConcatExpr { return c.concat_expr(mut it) } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index e9f663d31c..eafb0063e6 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1430,7 +1430,7 @@ fn (mut g Gen) expr(node ast.Expr) { g.write("'$it.val'") } ast.ComptimeCall { - g.write('/*c*/') + g.comptime_call(it) } ast.ConcatExpr { g.concat_expr(it) @@ -4336,7 +4336,7 @@ fn (mut g Gen) array_init(it ast.ArrayInit) { // `ui.foo(button)` => // `ui__foo(I_ui__Button_to_ui__Widget(` ... -fn (g &Gen) interface_call(typ, interface_type table.Type) { +fn (mut g Gen) interface_call(typ, interface_type table.Type) { interface_styp := g.cc_type(interface_type) styp := g.cc_type(typ) mut cast_fn_name := 'I_${styp}_to_Interface_${interface_styp}' @@ -4349,6 +4349,29 @@ fn (g &Gen) interface_call(typ, interface_type table.Type) { } } +fn (g &Gen) comptime_call(node ast.ComptimeCall) { + 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('\\', '/') diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 4b1247519c..6fd75ca922 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -14,13 +14,13 @@ const ( 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos'] ) -fn (mut p Parser)resolve_vroot(flag string) string { +fn (mut p Parser) resolve_vroot(flag string) string { mcache := vmod.get_cache() vmod_file_location := mcache.get_by_folder(p.file_name_dir) if vmod_file_location.vmod_file.len == 0 { // There was no actual v.mod file found. p.error('To use @VROOT, you need' + ' to have a "v.mod" file in ${p.file_name_dir},' + - ' or in one of its parent folders.') + ' or in one of its parent folders.') } vmod_path := vmod_file_location.vmod_folder return flag.replace('@VROOT', os.real_path(vmod_path)) @@ -220,7 +220,6 @@ fn os_from_string(os string) pref.OS { fn (mut p Parser) comptime_method_call(left ast.Expr) ast.ComptimeCall { p.check(.dollar) method_name := p.check_name() - _ = method_name /* mut j := 0 sym := p.table.get_type_symbol(typ) @@ -256,6 +255,6 @@ fn (mut p Parser) comptime_method_call(left ast.Expr) ast.ComptimeCall { } return ast.ComptimeCall{ left: left - name: method_name + method_name: method_name } } diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 5f3f9d1600..328ec5b789 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -283,9 +283,9 @@ fn handle_conn(conn net.Socket, mut app T) { } // Call the right action - $if debug { + //$if debug { println('action=$action') - } + //} app.$action() /* app.$action() or {