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

all: more match updates

This commit is contained in:
Alexander Medvednikov 2020-06-19 11:46:08 +02:00
parent 274ecb71fa
commit d04554f0b7
6 changed files with 138 additions and 144 deletions

View File

@ -369,9 +369,9 @@ pub mut:
} }
pub fn (i &Ident) var_info() IdentVar { pub fn (i &Ident) var_info() IdentVar {
match i.info { match i.info as info {
IdentVar { IdentVar {
return it return info
} }
else { else {
// return IdentVar{} // return IdentVar{}
@ -826,7 +826,7 @@ pub struct SqlInsertExpr {
[inline] [inline]
pub fn (expr Expr) is_blank_ident() bool { pub fn (expr Expr) is_blank_ident() bool {
match expr { match expr {
Ident { return it.kind == .blank_ident } Ident { return expr.kind == .blank_ident }
else { return false } else { return false }
} }
} }
@ -835,91 +835,91 @@ pub fn (expr Expr) position() token.Position {
// all uncommented have to be implemented // all uncommented have to be implemented
match mut expr { match mut expr {
ArrayInit { ArrayInit {
return it.pos return expr.pos
} }
AsCast { AsCast {
return it.pos return expr.pos
} }
// ast.Ident { } // ast.Ident { }
CastExpr { CastExpr {
return it.pos return expr.pos
} }
Assoc { Assoc {
return it.pos return expr.pos
} }
BoolLiteral { BoolLiteral {
return it.pos return expr.pos
} }
CallExpr { CallExpr {
return it.pos return expr.pos
} }
CharLiteral { CharLiteral {
return it.pos return expr.pos
} }
EnumVal { EnumVal {
return it.pos return expr.pos
} }
FloatLiteral { FloatLiteral {
return it.pos return expr.pos
} }
Ident { Ident {
return it.pos return expr.pos
} }
IfExpr { IfExpr {
return it.pos return expr.pos
} }
// ast.IfGuardExpr { } // ast.IfGuardExpr { }
IndexExpr { IndexExpr {
return it.pos return expr.pos
} }
InfixExpr { InfixExpr {
left_pos := it.left.position() left_pos := expr.left.position()
right_pos := it.right.position() right_pos := expr.right.position()
if left_pos.pos == 0 || right_pos.pos == 0 { if left_pos.pos == 0 || right_pos.pos == 0 {
return it.pos return expr.pos
} }
return token.Position{ return token.Position{
line_nr: it.pos.line_nr line_nr: expr.pos.line_nr
pos: left_pos.pos pos: left_pos.pos
len: right_pos.pos - left_pos.pos + right_pos.len len: right_pos.pos - left_pos.pos + right_pos.len
} }
} }
IntegerLiteral { IntegerLiteral {
return it.pos return expr.pos
} }
MapInit { MapInit {
return it.pos return expr.pos
} }
MatchExpr { MatchExpr {
return it.pos return expr.pos
} }
None { None {
return it.pos return expr.pos
} }
PostfixExpr { PostfixExpr {
return it.pos return expr.pos
} }
// ast.None { } // ast.None { }
PrefixExpr { PrefixExpr {
return it.pos return expr.pos
} }
// ast.ParExpr { } // ast.ParExpr { }
SelectorExpr { SelectorExpr {
return it.pos return expr.pos
} }
// ast.SizeOf { } // ast.SizeOf { }
StringLiteral { StringLiteral {
return it.pos return expr.pos
} }
StringInterLiteral { StringInterLiteral {
return it.pos return expr.pos
} }
// ast.Type { } // ast.Type { }
StructInit { StructInit {
return it.pos return expr.pos
} }
Likely { Likely {
return it.pos return expr.pos
} }
// ast.TypeOf { } // ast.TypeOf { }
else { else {
@ -930,8 +930,8 @@ pub fn (expr Expr) position() token.Position {
pub fn (stmt Stmt) position() token.Position { pub fn (stmt Stmt) position() token.Position {
match stmt { match stmt {
AssertStmt { return it.pos } AssertStmt { return stmt.pos }
AssignStmt { return it.pos } AssignStmt { return stmt.pos }
/* /*
// Attr { // Attr {
// } // }
@ -940,19 +940,19 @@ pub fn (stmt Stmt) position() token.Position {
// BranchStmt { // BranchStmt {
// } // }
*/ */
Comment { return it.pos } Comment { return stmt.pos }
CompIf { return it.pos } CompIf { return stmt.pos }
ConstDecl { return it.pos } ConstDecl { return stmt.pos }
/* /*
// DeferStmt { // DeferStmt {
// } // }
*/ */
EnumDecl { return it.pos } EnumDecl { return stmt.pos }
ExprStmt { return it.pos } ExprStmt { return stmt.pos }
FnDecl { return it.pos } FnDecl { return stmt.pos }
ForCStmt { return it.pos } ForCStmt { return stmt.pos }
ForInStmt { return it.pos } ForInStmt { return stmt.pos }
ForStmt { return it.pos } ForStmt { return stmt.pos }
/* /*
// GlobalDecl { // GlobalDecl {
// } // }
@ -965,15 +965,15 @@ pub fn (stmt Stmt) position() token.Position {
// HashStmt { // HashStmt {
// } // }
*/ */
Import { return it.pos } Import { return stmt.pos }
/* /*
// InterfaceDecl { // InterfaceDecl {
// } // }
// Module { // Module {
// } // }
*/ */
Return { return it.pos } Return { return stmt.pos }
StructDecl { return it.pos } StructDecl { return stmt.pos }
/* /*
// TypeDecl { // TypeDecl {
// } // }

View File

@ -1012,6 +1012,12 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
if it.cond is ast.Ident { if it.cond is ast.Ident {
ident := it.cond as ast.Ident ident := it.cond as ast.Ident
f.it_name = ident.name f.it_name = ident.name
} else if it.cond is ast.SelectorExpr {
// `x.y as z`
// if ident.name != it.var_name && it.var_name != '' {
if it.var_name != '' {
f.write(' as $it.var_name')
}
} }
f.writeln(' {') f.writeln(' {')
f.indent++ f.indent++

View File

@ -3676,12 +3676,10 @@ fn (g Gen) is_importing_os() bool {
fn (mut g Gen) go_stmt(node ast.GoStmt) { fn (mut g Gen) go_stmt(node ast.GoStmt) {
tmp := g.new_tmp_var() tmp := g.new_tmp_var()
// x := node.call_expr as ast.CallEpxr // TODO expr := node.call_expr as ast.CallExpr
match node.call_expr { mut name := expr.name // .replace('.', '__')
ast.CallExpr { if expr.is_method {
mut name := it.name // .replace('.', '__') receiver_sym := g.table.get_type_symbol(expr.receiver_type)
if it.is_method {
receiver_sym := g.table.get_type_symbol(it.receiver_type)
name = receiver_sym.name + '_' + name name = receiver_sym.name + '_' + name
} }
name = name.replace('.', '__') name = name.replace('.', '__')
@ -3690,12 +3688,12 @@ fn (mut g Gen) go_stmt(node ast.GoStmt) {
wrapper_fn_name := name + '_thread_wrapper' wrapper_fn_name := name + '_thread_wrapper'
arg_tmp_var := 'arg_' + tmp arg_tmp_var := 'arg_' + tmp
g.writeln('$wrapper_struct_name *$arg_tmp_var = malloc(sizeof(thread_arg_$name));') g.writeln('$wrapper_struct_name *$arg_tmp_var = malloc(sizeof(thread_arg_$name));')
if it.is_method { if expr.is_method {
g.write('$arg_tmp_var->arg0 = ') g.write('$arg_tmp_var->arg0 = ')
g.expr(it.left) g.expr(expr.left)
g.writeln(';') g.writeln(';')
} }
for i, arg in it.args { for i, arg in expr.args {
g.write('$arg_tmp_var->arg${i+1} = ') g.write('$arg_tmp_var->arg${i+1} = ')
g.expr(arg.expr) g.expr(arg.expr)
g.writeln(';') g.writeln(';')
@ -3712,14 +3710,14 @@ fn (mut g Gen) go_stmt(node ast.GoStmt) {
return return
} }
g.type_definitions.writeln('\ntypedef struct $wrapper_struct_name {') g.type_definitions.writeln('\ntypedef struct $wrapper_struct_name {')
if it.is_method { if expr.is_method {
styp := g.typ(it.receiver_type) styp := g.typ(expr.receiver_type)
g.type_definitions.writeln('\t$styp arg0;') g.type_definitions.writeln('\t$styp arg0;')
} }
if it.args.len == 0 { if expr.args.len == 0 {
g.type_definitions.writeln('EMPTY_STRUCT_DECLARATION;') g.type_definitions.writeln('EMPTY_STRUCT_DECLARATION;')
} else { } else {
for i, arg in it.args { for i, arg in expr.args {
styp := g.typ(arg.typ) styp := g.typ(arg.typ)
g.type_definitions.writeln('\t$styp arg${i+1};') g.type_definitions.writeln('\t$styp arg${i+1};')
} }
@ -3728,15 +3726,15 @@ fn (mut g Gen) go_stmt(node ast.GoStmt) {
g.type_definitions.writeln('void* ${wrapper_fn_name}($wrapper_struct_name *arg);') g.type_definitions.writeln('void* ${wrapper_fn_name}($wrapper_struct_name *arg);')
g.gowrappers.writeln('void* ${wrapper_fn_name}($wrapper_struct_name *arg) {') g.gowrappers.writeln('void* ${wrapper_fn_name}($wrapper_struct_name *arg) {')
g.gowrappers.write('\t${name}(') g.gowrappers.write('\t${name}(')
if it.is_method { if expr.is_method {
g.gowrappers.write('arg->arg0') g.gowrappers.write('arg->arg0')
if it.args.len > 0 { if expr.args.len > 0 {
g.gowrappers.write(', ') g.gowrappers.write(', ')
} }
} }
for i in 0 .. it.args.len { for i in 0 .. expr.args.len {
g.gowrappers.write('arg->arg${i+1}') g.gowrappers.write('arg->arg${i+1}')
if i < it.args.len - 1 { if i < expr.args.len - 1 {
g.gowrappers.write(', ') g.gowrappers.write(', ')
} }
} }
@ -3745,9 +3743,6 @@ fn (mut g Gen) go_stmt(node ast.GoStmt) {
g.gowrappers.writeln('}') g.gowrappers.writeln('}')
g.threaded_fns << name g.threaded_fns << name
} }
else {}
}
}
fn (mut g Gen) as_cast(node ast.AsCast) { fn (mut g Gen) as_cast(node ast.AsCast) {
// Make sure the sum type can be cast to this type (the types // Make sure the sum type can be cast to this type (the types

View File

@ -277,11 +277,11 @@ pub fn (mut g JsGen) typ(t table.Type) string {
// TODO: Implement interfaces // TODO: Implement interfaces
styp = 'interface' styp = 'interface'
} }
}
/* else { /* else {
println('jsgen.typ: Unhandled type $t') println('jsgen.typ: Unhandled type $t')
styp = sym.name styp = sym.name
} */ } */
}
if styp.starts_with('JS.') { return styp[3..] } if styp.starts_with('JS.') { return styp[3..] }
return styp return styp
} }
@ -510,11 +510,6 @@ fn (mut g JsGen) stmt(node ast.Stmt) {
ast.UnsafeStmt { ast.UnsafeStmt {
g.stmts(it.stmts) g.stmts(it.stmts)
} }
/*
else {
verror('jsgen.stmt(): bad node ${typeof(node)}')
}
*/
} }
} }
@ -639,11 +634,6 @@ fn (mut g JsGen) expr(node ast.Expr) {
ast.ComptimeCall { ast.ComptimeCall {
// TODO // TODO
} }
/*
else {
println(term.red('jsgen.expr(): unhandled node "${typeof(node)}"'))
}
*/
} }
} }

View File

@ -463,9 +463,9 @@ pub fn (mut g Gen) gen_print_from_expr(expr ast.Expr, newline bool) {
match expr { match expr {
ast.StringLiteral { ast.StringLiteral {
if newline { if newline {
g.gen_print(it.val + '\n') g.gen_print(expr.val + '\n')
} else { } else {
g.gen_print(it.val) g.gen_print(expr.val)
} }
} }
else {} else {}
@ -559,11 +559,11 @@ pub fn (mut g Gen) call_fn(node ast.CallExpr) {
match expr { match expr {
ast.IntegerLiteral { ast.IntegerLiteral {
// `foo(2)` => `mov edi,0x2` // `foo(2)` => `mov edi,0x2`
g.mov(fn_arg_registers[i], it.val.int()) g.mov(fn_arg_registers[i], expr.val.int())
} }
ast.Ident { ast.Ident {
// `foo(x)` => `mov edi,DWORD PTR [rbp-0x8]` // `foo(x)` => `mov edi,DWORD PTR [rbp-0x8]`
var_offset := g.get_var_offset(it.name) var_offset := g.get_var_offset(expr.name)
println('i=$i fn name= $name offset=$var_offset') println('i=$i fn name= $name offset=$var_offset')
println(int(fn_arg_registers[i])) println(int(fn_arg_registers[i]))
g.mov_var_to_reg(fn_arg_registers[i], var_offset) g.mov_var_to_reg(fn_arg_registers[i], var_offset)
@ -584,20 +584,20 @@ pub fn (mut g Gen) call_fn(node ast.CallExpr) {
fn (mut g Gen) stmt(node ast.Stmt) { fn (mut g Gen) stmt(node ast.Stmt) {
match node { match node {
ast.AssignStmt { ast.AssignStmt {
g.assign_stmt(it) g.assign_stmt(node)
} }
ast.ConstDecl {} ast.ConstDecl {}
ast.ExprStmt { ast.ExprStmt {
g.expr(it.expr) g.expr(node.expr)
} }
ast.FnDecl { ast.FnDecl {
g.fn_decl(it) g.fn_decl(node)
} }
ast.ForStmt { ast.ForStmt {
g.for_stmt(it) g.for_stmt(node)
} }
ast.HashStmt { ast.HashStmt {
words := it.val.split(' ') words := node.val.split(' ')
for word in words { for word in words {
if word.len != 2 { if word.len != 2 {
verror('opcodes format: xx xx xx xx') verror('opcodes format: xx xx xx xx')
@ -615,7 +615,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
} }
ast.StructDecl {} ast.StructDecl {}
ast.UnsafeStmt { ast.UnsafeStmt {
g.stmts(it.stmts) g.stmts(node.stmts)
} }
else { else {
println('x64.stmt(): bad node: ' + typeof(node)) println('x64.stmt(): bad node: ' + typeof(node))
@ -631,22 +631,22 @@ fn (mut g Gen) expr(node ast.Expr) {
ast.ArrayInit {} ast.ArrayInit {}
ast.BoolLiteral {} ast.BoolLiteral {}
ast.CallExpr { ast.CallExpr {
if it.name in ['println', 'print', 'eprintln', 'eprint'] { if node.name in ['println', 'print', 'eprintln', 'eprint'] {
expr := it.args[0].expr expr := node.args[0].expr
g.gen_print_from_expr(expr, it.name in ['println', 'eprintln']) g.gen_print_from_expr(expr, node.name in ['println', 'eprintln'])
return return
} }
g.call_fn(it) g.call_fn(node)
} }
ast.FloatLiteral {} ast.FloatLiteral {}
ast.Ident {} ast.Ident {}
ast.IfExpr { ast.IfExpr {
g.if_expr(it) g.if_expr(node)
} }
ast.InfixExpr {} ast.InfixExpr {}
ast.IntegerLiteral {} ast.IntegerLiteral {}
ast.PostfixExpr { ast.PostfixExpr {
g.postfix_expr(it) g.postfix_expr(node)
} }
ast.StringLiteral {} ast.StringLiteral {}
ast.StructInit {} ast.StructInit {}
@ -700,10 +700,10 @@ fn (mut g Gen) assign_stmt(node ast.AssignStmt) {
// ident := left as ast.Ident // ident := left as ast.Ident
match right { match right {
ast.IntegerLiteral { ast.IntegerLiteral {
g.allocate_var(name, 4, it.val.int()) g.allocate_var(name, 4, right.val.int())
} }
ast.InfixExpr { ast.InfixExpr {
g.infix_expr(it) g.infix_expr(right)
g.allocate_var(name, 4, 0) g.allocate_var(name, 4, 0)
// `mov DWORD PTR [rbp-0x8],eax` // `mov DWORD PTR [rbp-0x8],eax`
offset := g.get_var_offset(name) offset := g.get_var_offset(name)

View File

@ -793,6 +793,9 @@ pub fn (mut p Parser) parse_ident(language table.Language) ast.Ident {
} }
} }
} }
if p.inside_match_body && name == 'it' {
// p.warn('it')
}
if p.expr_mod.len > 0 { if p.expr_mod.len > 0 {
name = '${p.expr_mod}.$name' name = '${p.expr_mod}.$name'
} }