mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: remaining nodes; match type fix; v2.c is now generated
This commit is contained in:
parent
8458ea40f0
commit
37453945d0
@ -831,7 +831,7 @@ fn (p mut Parser) factor() string {
|
|||||||
is_sum_type := type_of_var in p.table.sum_types
|
is_sum_type := type_of_var in p.table.sum_types
|
||||||
if is_sum_type && vname.len > 0 {
|
if is_sum_type && vname.len > 0 {
|
||||||
// TODO: make this work for arbitrary sumtype expressions, not just simple vars
|
// TODO: make this work for arbitrary sumtype expressions, not just simple vars
|
||||||
p.gen('tos3(__SumTypeNames__${type_of_var}[${vname}.typ - 1])')
|
p.gen('${vname}.typ == 0 ? tos3("typeof(): typ == 0") : tos3(__SumTypeNames__${type_of_var}[${vname}.typ - 1])')
|
||||||
}else{
|
}else{
|
||||||
p.gen('tos3("$type_of_var")')
|
p.gen('tos3("$type_of_var")')
|
||||||
}
|
}
|
||||||
|
@ -340,7 +340,7 @@ pub fn (v mut V) compile2() {
|
|||||||
println(v.files)
|
println(v.files)
|
||||||
}
|
}
|
||||||
mut b := v.new_v2()
|
mut b := v.new_v2()
|
||||||
b.build_c(v.files, v.pref.out_name)
|
b.build_c(v.files, v.out_name_c)// v.pref.out_name + '.c')
|
||||||
v.cc()
|
v.cc()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,10 +5,12 @@ module wyhash
|
|||||||
|
|
||||||
pub fn rand_u64(seed &u64) u64 {
|
pub fn rand_u64(seed &u64) u64 {
|
||||||
mut seed0 := seed
|
mut seed0 := seed
|
||||||
|
// QTODO
|
||||||
unsafe{
|
unsafe{
|
||||||
mut seed1 := *seed0
|
mut seed1 := *seed0
|
||||||
seed1+=wyp0
|
seed1+=wyp0
|
||||||
*seed0 = seed1
|
*seed0 = seed1
|
||||||
return wymum(seed1^wyp1, seed1)
|
return wymum(seed1^wyp1, seed1)
|
||||||
}
|
}
|
||||||
|
//return 0
|
||||||
}
|
}
|
||||||
|
@ -18,15 +18,11 @@ pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprS
|
|||||||
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
|
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
|
||||||
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
|
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
|
||||||
LineComment | MultiLineComment | AssertStmt | UnsafeStmt
|
LineComment | MultiLineComment | AssertStmt | UnsafeStmt
|
||||||
|
|
||||||
// pub type Type = StructType | ArrayType
|
// pub type Type = StructType | ArrayType
|
||||||
|
|
||||||
// pub struct StructType {
|
// pub struct StructType {
|
||||||
// fields []Field
|
// fields []Field
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// pub struct ArrayType {}
|
// pub struct ArrayType {}
|
||||||
|
|
||||||
pub struct Type {
|
pub struct Type {
|
||||||
pub:
|
pub:
|
||||||
typ table.Type
|
typ table.Type
|
||||||
@ -338,7 +334,7 @@ pub:
|
|||||||
branches []MatchBranch
|
branches []MatchBranch
|
||||||
pos token.Position
|
pos token.Position
|
||||||
mut:
|
mut:
|
||||||
typ table.Type
|
expr_type table.Type // type of `x` in `match x {`
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MatchBranch {
|
pub struct MatchBranch {
|
||||||
@ -348,7 +344,6 @@ pub:
|
|||||||
pos token.Position
|
pos token.Position
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct CompIf {
|
pub struct CompIf {
|
||||||
pub:
|
pub:
|
||||||
cond Expr
|
cond Expr
|
||||||
|
@ -42,10 +42,15 @@ pub fn (b mut Builder) gen_c(v_files []string) string {
|
|||||||
if b.checker.nr_errors > 0 {
|
if b.checker.nr_errors > 0 {
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
return gen.cgen(b.parsed_files, b.table)
|
// println('starting cgen...')
|
||||||
|
res := gen.cgen(b.parsed_files, b.table)
|
||||||
|
println('cgen done')
|
||||||
|
// println(res)
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (b mut Builder) build_c(v_files []string, out_file string) {
|
pub fn (b mut Builder) build_c(v_files []string, out_file string) {
|
||||||
|
println('build_c($out_file)')
|
||||||
os.write_file(out_file, b.gen_c(v_files))
|
os.write_file(out_file, b.gen_c(v_files))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -466,10 +466,10 @@ fn (c mut Checker) stmt(node ast.Stmt) {
|
|||||||
typ := c.expr(it.expr)
|
typ := c.expr(it.expr)
|
||||||
it.typ = typ
|
it.typ = typ
|
||||||
}
|
}
|
||||||
else {
|
else {}
|
||||||
// println('checker.stmt(): unhandled node')
|
// println('checker.stmt(): unhandled node')
|
||||||
// println('checker.stmt(): unhandled node (${typeof(node)})')
|
// println('checker.stmt(): unhandled node (${typeof(node)})')
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -577,12 +577,11 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
else {
|
else {}
|
||||||
// println('checker.expr(): unhandled node')
|
// println('checker.expr(): unhandled node')
|
||||||
// TODO: find nil string bug triggered with typeof
|
// TODO: find nil string bug triggered with typeof
|
||||||
// println('checker.expr(): unhandled node (${typeof(node)})')
|
// println('checker.expr(): unhandled node (${typeof(node)})')
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return table.void_type
|
return table.void_type
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -670,12 +669,15 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
|
pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
|
||||||
t := c.expr(node.cond)
|
expr_type := c.expr(node.cond)
|
||||||
c.expected_type = t
|
if expr_type == 0 {
|
||||||
|
c.error('match 0 expr type', node.pos)
|
||||||
|
}
|
||||||
|
c.expected_type = expr_type
|
||||||
mut ret_type := table.void_type
|
mut ret_type := table.void_type
|
||||||
for branch in node.branches {
|
for branch in node.branches {
|
||||||
for expr in branch.exprs {
|
for expr in branch.exprs {
|
||||||
c.expected_type = t
|
c.expected_type = expr_type
|
||||||
typ := c.expr(expr)
|
typ := c.expr(expr)
|
||||||
typ_sym := c.table.get_type_symbol(typ)
|
typ_sym := c.table.get_type_symbol(typ)
|
||||||
// TODO:
|
// TODO:
|
||||||
@ -698,7 +700,8 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
node.typ = t
|
node.expr_type = expr_type
|
||||||
|
// println('!m $expr_type')
|
||||||
return ret_type
|
return ret_type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,10 +76,10 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
|||||||
// }
|
// }
|
||||||
// g.writeln(';')
|
// g.writeln(';')
|
||||||
// }
|
// }
|
||||||
println('assign')
|
g.write('') // /*assign*/')
|
||||||
}
|
}
|
||||||
ast.AssertStmt {
|
ast.AssertStmt {
|
||||||
println('// assert')
|
g.write('// assert')
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
ast.Attr {
|
ast.Attr {
|
||||||
@ -105,6 +105,9 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
|||||||
g.stmts(it.stmts)
|
g.stmts(it.stmts)
|
||||||
g.writeln('#endif')
|
g.writeln('#endif')
|
||||||
}
|
}
|
||||||
|
ast.DeferStmt {
|
||||||
|
g.writeln('// defer')
|
||||||
|
}
|
||||||
ast.EnumDecl {
|
ast.EnumDecl {
|
||||||
g.writeln('typedef enum {')
|
g.writeln('typedef enum {')
|
||||||
for i, val in it.vals {
|
for i, val in it.vals {
|
||||||
@ -193,6 +196,9 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
|||||||
// TODO
|
// TODO
|
||||||
g.writeln('__global')
|
g.writeln('__global')
|
||||||
}
|
}
|
||||||
|
ast.GotoLabel {
|
||||||
|
g.writeln('$it.name:')
|
||||||
|
}
|
||||||
ast.HashStmt {
|
ast.HashStmt {
|
||||||
g.writeln('#$it.name')
|
g.writeln('#$it.name')
|
||||||
}
|
}
|
||||||
@ -227,6 +233,9 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
|||||||
}
|
}
|
||||||
g.writeln('} $it.name;')
|
g.writeln('} $it.name;')
|
||||||
}
|
}
|
||||||
|
ast.TypeDecl {
|
||||||
|
g.writeln('// type')
|
||||||
|
}
|
||||||
ast.UnsafeStmt {
|
ast.UnsafeStmt {
|
||||||
g.stmts(it.stmts)
|
g.stmts(it.stmts)
|
||||||
}
|
}
|
||||||
@ -243,7 +252,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn (g mut Gen) expr(node ast.Expr) {
|
fn (g mut Gen) expr(node ast.Expr) {
|
||||||
// println('cgen expr()')
|
// println('cgen expr() line_nr=$node.pos.line_nr')
|
||||||
match node {
|
match node {
|
||||||
ast.ArrayInit {
|
ast.ArrayInit {
|
||||||
type_sym := g.table.get_type_symbol(it.typ)
|
type_sym := g.table.get_type_symbol(it.typ)
|
||||||
@ -254,11 +263,17 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||||||
}
|
}
|
||||||
g.write('\n})')
|
g.write('\n})')
|
||||||
}
|
}
|
||||||
|
ast.AsCast {
|
||||||
|
g.write('/* as */')
|
||||||
|
}
|
||||||
ast.AssignExpr {
|
ast.AssignExpr {
|
||||||
g.expr(it.left)
|
g.expr(it.left)
|
||||||
g.write(' $it.op.str() ')
|
g.write(' $it.op.str() ')
|
||||||
g.expr(it.val)
|
g.expr(it.val)
|
||||||
}
|
}
|
||||||
|
ast.Assoc {
|
||||||
|
g.write('/* assoc */')
|
||||||
|
}
|
||||||
ast.BoolLiteral {
|
ast.BoolLiteral {
|
||||||
g.write(it.val.str())
|
g.write(it.val.str())
|
||||||
}
|
}
|
||||||
@ -310,10 +325,8 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||||||
g.writeln(') {')
|
g.writeln(') {')
|
||||||
for i, stmt in it.stmts {
|
for i, stmt in it.stmts {
|
||||||
// Assign ret value
|
// Assign ret value
|
||||||
if i == it.stmts.len - 1 && type_sym.kind != .void {
|
if i == it.stmts.len - 1 && type_sym.kind != .void {}
|
||||||
// g.writeln('$tmp =')
|
// g.writeln('$tmp =')
|
||||||
println(1)
|
|
||||||
}
|
|
||||||
g.stmt(stmt)
|
g.stmt(stmt)
|
||||||
}
|
}
|
||||||
g.writeln('}')
|
g.writeln('}')
|
||||||
@ -325,6 +338,9 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||||||
g.writeln('}')
|
g.writeln('}')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ast.IfGuardExpr {
|
||||||
|
g.write('/* guard */')
|
||||||
|
}
|
||||||
ast.IndexExpr {
|
ast.IndexExpr {
|
||||||
g.index_expr(it)
|
g.index_expr(it)
|
||||||
}
|
}
|
||||||
@ -343,7 +359,13 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||||||
g.write(it.val.str())
|
g.write(it.val.str())
|
||||||
}
|
}
|
||||||
ast.MatchExpr {
|
ast.MatchExpr {
|
||||||
type_sym := g.table.get_type_symbol(it.typ)
|
// println('match expr typ=$it.expr_type')
|
||||||
|
// TODO
|
||||||
|
if it.expr_type == 0 {
|
||||||
|
g.writeln('// match 0')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
type_sym := g.table.get_type_symbol(it.expr_type)
|
||||||
mut tmp := ''
|
mut tmp := ''
|
||||||
if type_sym.kind != .void {
|
if type_sym.kind != .void {
|
||||||
tmp = g.new_tmp_var()
|
tmp = g.new_tmp_var()
|
||||||
@ -356,7 +378,7 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||||||
for i, expr in branch.exprs {
|
for i, expr in branch.exprs {
|
||||||
g.write('$tmp == ')
|
g.write('$tmp == ')
|
||||||
g.expr(expr)
|
g.expr(expr)
|
||||||
if i < branch.exprs.len-1 {
|
if i < branch.exprs.len - 1 {
|
||||||
g.write(' || ')
|
g.write(' || ')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -376,6 +398,9 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||||||
g.call_args(it.args)
|
g.call_args(it.args)
|
||||||
g.write(')')
|
g.write(')')
|
||||||
}
|
}
|
||||||
|
ast.None {
|
||||||
|
g.write('0')
|
||||||
|
}
|
||||||
ast.ParExpr {
|
ast.ParExpr {
|
||||||
g.write('(')
|
g.write('(')
|
||||||
g.expr(it.expr)
|
g.expr(it.expr)
|
||||||
@ -425,6 +450,9 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||||||
g.write('.')
|
g.write('.')
|
||||||
g.write(it.field)
|
g.write(it.field)
|
||||||
}
|
}
|
||||||
|
ast.Type {
|
||||||
|
g.write('/* Type */')
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
// #printf("node=%d\n", node.typ);
|
// #printf("node=%d\n", node.typ);
|
||||||
println(term.red('cgen.expr(): bad node ' + typeof(node)))
|
println(term.red('cgen.expr(): bad node ' + typeof(node)))
|
||||||
@ -467,6 +495,6 @@ fn (g mut Gen) call_args(args []ast.Expr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn verror(s string) {
|
fn verror(s string) {
|
||||||
println(s)
|
println('cgen error: $s')
|
||||||
exit(1)
|
// exit(1)
|
||||||
}
|
}
|
||||||
|
@ -182,6 +182,7 @@ pub fn (t &Table) find_type(name string) ?TypeSymbol {
|
|||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (t &Table) get_type_symbol(typ Type) &TypeSymbol {
|
pub fn (t &Table) get_type_symbol(typ Type) &TypeSymbol {
|
||||||
|
// println('get_type_symbol $typ')
|
||||||
idx := type_idx(typ)
|
idx := type_idx(typ)
|
||||||
if idx > 0 {
|
if idx > 0 {
|
||||||
return &t.types[idx]
|
return &t.types[idx]
|
||||||
@ -400,8 +401,7 @@ pub fn (t &Table) check(got, expected Type) bool {
|
|||||||
}
|
}
|
||||||
// TODO: actually check for & handle pointers with name_expr
|
// TODO: actually check for & handle pointers with name_expr
|
||||||
// see hack in checker IndexExpr line #691
|
// see hack in checker IndexExpr line #691
|
||||||
if (got_type_sym.kind == .byte && exp_type_sym.kind == .byteptr) ||
|
if (got_type_sym.kind == .byte && exp_type_sym.kind == .byteptr) || (exp_type_sym.kind == .byte && got_type_sym.kind == .byteptr) {
|
||||||
(exp_type_sym.kind == .byte && got_type_sym.kind == .byteptr) {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
// TODO
|
// TODO
|
||||||
@ -424,8 +424,7 @@ pub fn (t &Table) check(got, expected Type) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
// type alias
|
// type alias
|
||||||
if (got_type_sym.kind == .alias && got_type_sym.parent_idx == exp_idx) ||
|
if (got_type_sym.kind == .alias && got_type_sym.parent_idx == exp_idx) || (exp_type_sym.kind == .alias && exp_type_sym.parent_idx == got_idx) {
|
||||||
(exp_type_sym.kind == .alias && exp_type_sym.parent_idx == got_idx) {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
// sum type
|
// sum type
|
||||||
|
Loading…
Reference in New Issue
Block a user