cgen: $action (ComptimeCall); vweb: hello world works!

pull/5237/head
Alexander Medvednikov 2020-06-06 10:05:26 +02:00
parent 0a3486b4c5
commit 7815c5b108
6 changed files with 45 additions and 14 deletions

View File

@ -27,7 +27,8 @@ pub fn (mut app App) json_endpoint() {
pub fn (mut app App) index() { pub fn (mut app App) index() {
app.cnt++ app.cnt++
$vweb.html() app.vweb.text('Hello world from vweb')
//$vweb.html()
} }
pub fn (mut app App) reset() { pub fn (mut app App) reset() {

View File

@ -778,8 +778,11 @@ pub mut:
} }
pub struct ComptimeCall { pub struct ComptimeCall {
name string pub:
method_name string
left Expr left Expr
pub mut:
sym table.TypeSymbol
} }
pub struct None { pub struct None {

View File

@ -1258,10 +1258,11 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) {
} }
for i, exp_type in expected_types { for i, exp_type in expected_types {
got_typ := c.unwrap_generic(got_types[i]) got_typ := c.unwrap_generic(got_types[i])
if got_typ.has_flag(.optional) && if got_typ.has_flag(.optional) && (!exp_type.has_flag(.optional) || c.table.type_to_str(got_typ) !=
(!exp_type.has_flag(.optional) || c.table.type_to_str(got_typ) != c.table.type_to_str(exp_type)) { c.table.type_to_str(exp_type)) {
pos := return_stmt.exprs[i].position() 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) { if !c.check_types(got_typ, exp_type) {
got_typ_sym := c.table.get_type_symbol(got_typ) 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 { ast.CharLiteral {
return table.byte_type 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 { ast.ConcatExpr {
return c.concat_expr(mut it) return c.concat_expr(mut it)
} }

View File

@ -1430,7 +1430,7 @@ fn (mut g Gen) expr(node ast.Expr) {
g.write("'$it.val'") g.write("'$it.val'")
} }
ast.ComptimeCall { ast.ComptimeCall {
g.write('/*c*/') g.comptime_call(it)
} }
ast.ConcatExpr { ast.ConcatExpr {
g.concat_expr(it) g.concat_expr(it)
@ -4336,7 +4336,7 @@ fn (mut g Gen) array_init(it ast.ArrayInit) {
// `ui.foo(button)` => // `ui.foo(button)` =>
// `ui__foo(I_ui__Button_to_ui__Widget(` ... // `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) interface_styp := g.cc_type(interface_type)
styp := g.cc_type(typ) styp := g.cc_type(typ)
mut cast_fn_name := 'I_${styp}_to_Interface_${interface_styp}' 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) { 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('\\', '/')

View File

@ -14,7 +14,7 @@ const (
'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos'] '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() mcache := vmod.get_cache()
vmod_file_location := mcache.get_by_folder(p.file_name_dir) vmod_file_location := mcache.get_by_folder(p.file_name_dir)
if vmod_file_location.vmod_file.len == 0 { if vmod_file_location.vmod_file.len == 0 {
@ -220,7 +220,6 @@ fn os_from_string(os string) pref.OS {
fn (mut p Parser) comptime_method_call(left ast.Expr) ast.ComptimeCall { fn (mut p Parser) comptime_method_call(left ast.Expr) ast.ComptimeCall {
p.check(.dollar) p.check(.dollar)
method_name := p.check_name() method_name := p.check_name()
_ = method_name
/* /*
mut j := 0 mut j := 0
sym := p.table.get_type_symbol(typ) 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{ return ast.ComptimeCall{
left: left left: left
name: method_name method_name: method_name
} }
} }

View File

@ -283,9 +283,9 @@ fn handle_conn<T>(conn net.Socket, mut app T) {
} }
// Call the right action // Call the right action
$if debug { //$if debug {
println('action=$action') println('action=$action')
} //}
app.$action() app.$action()
/* /*
app.$action() or { app.$action() or {