cgen: fix sum type casts & else if formatting
parent
c2ce06eba7
commit
0609756d36
|
@ -416,12 +416,13 @@ fn (g mut Gen) expr_with_cast(expr ast.Expr, got_type table.Type, exp_type table
|
||||||
if exp_sym.kind == .sum_type {
|
if exp_sym.kind == .sum_type {
|
||||||
sum_info := exp_sym.info as table.SumType
|
sum_info := exp_sym.info as table.SumType
|
||||||
if got_type in sum_info.variants {
|
if got_type in sum_info.variants {
|
||||||
|
got_sym := g.table.get_type_symbol(got_type)
|
||||||
got_styp := g.typ(got_type)
|
got_styp := g.typ(got_type)
|
||||||
exp_styp := g.typ(exp_type)
|
exp_styp := g.typ(exp_type)
|
||||||
got_idx := table.type_idx(got_type)
|
got_idx := table.type_idx(got_type)
|
||||||
g.write('/* sum type cast */ ($exp_styp) {.obj = memdup(&(${got_styp}[]) {')
|
g.write('/* sum type cast */ ($exp_styp) {.obj = memdup(&(${got_styp}[]) {')
|
||||||
g.expr(expr)
|
g.expr(expr)
|
||||||
g.write('}, sizeof($got_styp)), .typ = $got_idx}')
|
g.write('}, sizeof($got_styp)), .typ = $got_idx /* $got_sym.name */}')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -996,8 +997,9 @@ fn (g mut Gen) expr(node ast.Expr) {
|
||||||
ast.Type {
|
ast.Type {
|
||||||
// match sum Type
|
// match sum Type
|
||||||
// g.write('/* Type */')
|
// g.write('/* Type */')
|
||||||
g.write('_type_idx_')
|
type_idx := table.type_idx(it.typ)
|
||||||
g.write(g.typ(it.typ))
|
sym := g.table.get_type_symbol(it.typ)
|
||||||
|
g.write('$type_idx /* $sym.name */')
|
||||||
}
|
}
|
||||||
ast.TypeOf {
|
ast.TypeOf {
|
||||||
g.write('tos3("TYPEOF_TODO")')
|
g.write('tos3("TYPEOF_TODO")')
|
||||||
|
@ -1305,9 +1307,9 @@ fn (g mut Gen) if_expr(node ast.IfExpr) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if i < node.branches.len - 1 || !node.has_else {
|
else if i < node.branches.len - 1 || !node.has_else {
|
||||||
g.writeln('} else if (')
|
g.write('} else if (')
|
||||||
g.expr(branch.cond)
|
g.expr(branch.cond)
|
||||||
g.write(') {')
|
g.writeln(') {')
|
||||||
}
|
}
|
||||||
else if i == node.branches.len - 1 && node.has_else {
|
else if i == node.branches.len - 1 && node.has_else {
|
||||||
if is_guard {
|
if is_guard {
|
||||||
|
@ -1699,7 +1701,7 @@ fn (g mut Gen) write_sorted_types() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g mut Gen) write_types(types []table.TypeSymbol) {
|
fn (g mut Gen) write_types(types []table.TypeSymbol) {
|
||||||
for i, typ in types {
|
for typ in types {
|
||||||
if typ.name.starts_with('C.') {
|
if typ.name.starts_with('C.') {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -1717,11 +1719,9 @@ fn (g mut Gen) write_types(types []table.TypeSymbol) {
|
||||||
// g.definitions.writeln('} $name;\n')
|
// g.definitions.writeln('} $name;\n')
|
||||||
//
|
//
|
||||||
g.definitions.writeln('};\n')
|
g.definitions.writeln('};\n')
|
||||||
g.typedefs.writeln('#define _type_idx_$name $i')
|
|
||||||
}
|
}
|
||||||
// table.Alias, table.SumType { TODO
|
// table.Alias, table.SumType { TODO
|
||||||
table.Alias {
|
table.Alias {
|
||||||
g.typedefs.writeln('#define _type_idx_$name $i')
|
|
||||||
}
|
}
|
||||||
table.Enum {
|
table.Enum {
|
||||||
g.definitions.writeln('typedef enum {')
|
g.definitions.writeln('typedef enum {')
|
||||||
|
@ -1731,7 +1731,6 @@ fn (g mut Gen) write_types(types []table.TypeSymbol) {
|
||||||
g.definitions.writeln('} $name;\n')
|
g.definitions.writeln('} $name;\n')
|
||||||
}
|
}
|
||||||
table.SumType {
|
table.SumType {
|
||||||
g.typedefs.writeln('#define _type_idx_$name $i')
|
|
||||||
g.definitions.writeln('// Sum type')
|
g.definitions.writeln('// Sum type')
|
||||||
g.definitions.writeln('
|
g.definitions.writeln('
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -1739,9 +1738,7 @@ void* obj;
|
||||||
int typ;
|
int typ;
|
||||||
} $name;')
|
} $name;')
|
||||||
}
|
}
|
||||||
else {
|
else {}
|
||||||
g.typedefs.writeln('#define _type_idx_$name $i')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,11 +48,11 @@ void println(string s) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_expr(Expr e) {
|
void handle_expr(Expr e) {
|
||||||
if (e.typ == _type_idx_IfExpr) {
|
if (e.typ == 25 /* IfExpr */) {
|
||||||
IfExpr* it = (IfExpr*)e.obj; // ST it
|
IfExpr* it = (IfExpr*)e.obj; // ST it
|
||||||
println(tos3("if"));
|
println(tos3("if"));
|
||||||
}
|
}
|
||||||
else if (e.typ == _type_idx_IntegerLiteral) {
|
else if (e.typ == 26 /* IntegerLiteral */) {
|
||||||
IntegerLiteral* it = (IntegerLiteral*)e.obj; // ST it
|
IntegerLiteral* it = (IntegerLiteral*)e.obj; // ST it
|
||||||
println(tos3("integer"));
|
println(tos3("integer"));
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ int main(int argc, char** argv) {
|
||||||
Option_int n = get_opt();
|
Option_int n = get_opt();
|
||||||
int a = /*opt*/(*(int*)n.data) + 3;
|
int a = /*opt*/(*(int*)n.data) + 3;
|
||||||
handle_expr(/* sum type cast */ (Expr) {.obj = memdup(&(IfExpr[]) {(IfExpr){
|
handle_expr(/* sum type cast */ (Expr) {.obj = memdup(&(IfExpr[]) {(IfExpr){
|
||||||
0}}, sizeof(IfExpr)), .typ = 25});
|
0}}, sizeof(IfExpr)), .typ = 25 /* IfExpr */});
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue