1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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() {
app.cnt++
$vweb.html()
app.vweb.text('Hello world from vweb')
//$vweb.html()
}
pub fn (mut app App) reset() {

View File

@ -778,8 +778,11 @@ pub mut:
}
pub struct ComptimeCall {
name string
pub:
method_name string
left Expr
pub mut:
sym table.TypeSymbol
}
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 {
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)
}

View File

@ -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('\\', '/')

View File

@ -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
}
}

View File

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