cgen: lots of fixes
parent
b7e2af8151
commit
4161cfcdb8
|
@ -199,6 +199,7 @@ fn (v mut V) cc() {
|
|||
}
|
||||
if debug_mode {
|
||||
a << debug_options
|
||||
a << ' -ferror-limit=5000 '
|
||||
}
|
||||
if v.pref.is_prod {
|
||||
a << optimization_options
|
||||
|
|
|
@ -375,7 +375,8 @@ pub struct ForCStmt {
|
|||
pub:
|
||||
init Stmt // i := 0;
|
||||
cond Expr // i < 10;
|
||||
inc Stmt // i++;
|
||||
//inc Stmt // i++;
|
||||
inc Expr// i++;
|
||||
stmts []Stmt
|
||||
}
|
||||
|
||||
|
@ -388,7 +389,7 @@ pub struct ReturnStmt {
|
|||
// #include etc
|
||||
pub struct HashStmt {
|
||||
pub:
|
||||
name string
|
||||
val string
|
||||
}
|
||||
|
||||
// filter(), map()
|
||||
|
|
|
@ -444,7 +444,8 @@ fn (c mut Checker) stmt(node ast.Stmt) {
|
|||
ast.ForCStmt {
|
||||
c.stmt(it.init)
|
||||
c.expr(it.cond)
|
||||
c.stmt(it.inc)
|
||||
// c.stmt(it.inc)
|
||||
c.expr(it.inc)
|
||||
for stmt in it.stmts {
|
||||
c.stmt(stmt)
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ pub fn (g mut Gen) init() {
|
|||
for i, mr_typ in info.types {
|
||||
field_type_sym := g.table.get_type_symbol(mr_typ)
|
||||
type_name := field_type_sym.name.replace('.', '__')
|
||||
g.definitions.writeln('\t$type_name arg_${i+1};')
|
||||
g.definitions.writeln('\t$type_name arg${i};')
|
||||
}
|
||||
g.definitions.writeln('} $name;\n')
|
||||
// g.typedefs.writeln('typedef struct $name $name;')
|
||||
|
@ -115,11 +115,12 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
|||
// TODO
|
||||
}
|
||||
ast.Attr {
|
||||
g.writeln('[$it.name]')
|
||||
g.writeln('//[$it.name]')
|
||||
}
|
||||
ast.BranchStmt {
|
||||
// continue or break
|
||||
g.writeln(it.tok.str())
|
||||
g.write(it.tok.kind.str())
|
||||
g.writeln(';')
|
||||
}
|
||||
ast.ConstDecl {
|
||||
for i, field in it.fields {
|
||||
|
@ -132,10 +133,10 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
|||
}
|
||||
ast.CompIf {
|
||||
// TODO
|
||||
g.write('#ifdef ')
|
||||
g.writeln('//#ifdef ')
|
||||
g.expr(it.cond)
|
||||
g.stmts(it.stmts)
|
||||
g.writeln('#endif')
|
||||
g.writeln('//#endif')
|
||||
}
|
||||
ast.DeferStmt {
|
||||
g.writeln('// defer')
|
||||
|
@ -218,7 +219,8 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
|||
// g.write('; ')
|
||||
g.expr(it.cond)
|
||||
g.write('; ')
|
||||
g.stmt(it.inc)
|
||||
//g.stmt(it.inc)
|
||||
g.expr(it.inc)
|
||||
g.writeln(') {')
|
||||
for stmt in it.stmts {
|
||||
g.stmt(stmt)
|
||||
|
@ -226,7 +228,16 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
|||
g.writeln('}')
|
||||
}
|
||||
ast.ForInStmt {
|
||||
g.writeln('for')
|
||||
if it.is_range {
|
||||
i := g.new_tmp_var()
|
||||
g.write('for (int $i = ')
|
||||
g.expr(it.cond)
|
||||
g.write('; $i < ')
|
||||
g.expr(it.high)
|
||||
g.writeln('; $i++) { ')
|
||||
// g.stmts(it.stmts) TODO
|
||||
g.writeln('}')
|
||||
}
|
||||
}
|
||||
ast.ForStmt {
|
||||
g.write('while (')
|
||||
|
@ -245,7 +256,8 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
|||
g.writeln('$it.name:')
|
||||
}
|
||||
ast.HashStmt {
|
||||
g.writeln('#$it.name')
|
||||
// #include etc
|
||||
g.writeln('#$it.val')
|
||||
}
|
||||
ast.Import {}
|
||||
ast.Return {
|
||||
|
@ -303,7 +315,9 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||
match node {
|
||||
ast.ArrayInit {
|
||||
type_sym := g.table.get_type_symbol(it.typ)
|
||||
g.writeln('new_array_from_c_array($it.exprs.len, $it.exprs.len, sizeof($type_sym.name), {\t')
|
||||
elem_sym := g.table.get_type_symbol(it.elem_type)
|
||||
g.write('new_array_from_c_array($it.exprs.len, $it.exprs.len, sizeof($type_sym.name), ')
|
||||
g.writeln('(${elem_sym.name}[]){\t')
|
||||
for expr in it.exprs {
|
||||
g.expr(expr)
|
||||
g.write(', ')
|
||||
|
@ -325,7 +339,11 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||
g.write(it.val.str())
|
||||
}
|
||||
ast.CallExpr {
|
||||
name := it.name.replace('.', '__')
|
||||
mut name := it.name.replace('.', '__')
|
||||
if it.is_c {
|
||||
// Skip "C__"
|
||||
name = name[3..]
|
||||
}
|
||||
g.write('${name}(')
|
||||
g.call_args(it.args)
|
||||
g.write(')')
|
||||
|
@ -367,6 +385,25 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||
tmp = g.new_tmp_var()
|
||||
// g.writeln('$ti.name $tmp;')
|
||||
}
|
||||
// one line ?:
|
||||
// TODO clean this up once `is` is supported
|
||||
if it.stmts.len == 1 && it.else_stmts.len == 1 && type_sym.kind != .void {
|
||||
cond := it.cond
|
||||
stmt1 := it.stmts[0]
|
||||
else_stmt1 := it.else_stmts[0]
|
||||
match stmt1 {
|
||||
ast.ExprStmt {
|
||||
g.expr(cond)
|
||||
g.write(' ? ')
|
||||
expr_stmt := stmt1 as ast.ExprStmt
|
||||
g.expr(expr_stmt.expr)
|
||||
g.write(' : ')
|
||||
g.stmt(else_stmt1)
|
||||
}
|
||||
else {}
|
||||
}
|
||||
}
|
||||
else {
|
||||
g.write('if (')
|
||||
g.expr(it.cond)
|
||||
g.writeln(') {')
|
||||
|
@ -385,6 +422,7 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||
g.writeln('}')
|
||||
}
|
||||
}
|
||||
}
|
||||
ast.IfGuardExpr {
|
||||
g.write('/* guard */')
|
||||
}
|
||||
|
|
|
@ -47,23 +47,22 @@ void foo(int a) {
|
|||
while (true) {
|
||||
}
|
||||
for (int i = 0;
|
||||
i < 10; i++;
|
||||
) {
|
||||
i < 10; i++) {
|
||||
}
|
||||
array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), {
|
||||
array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), (void[]){
|
||||
1, 2, 3,
|
||||
});
|
||||
array_int nums2 = array_slice(nums, 0, 2);
|
||||
int number = nums[0];
|
||||
array_bool bools = new_array_from_c_array(2, 2, sizeof(array_bool), {
|
||||
array_bool bools = new_array_from_c_array(2, 2, sizeof(array_bool), (void[]){
|
||||
true, false,
|
||||
});
|
||||
array_User users = new_array_from_c_array(1, 1, sizeof(array_User), {
|
||||
array_User users = new_array_from_c_array(1, 1, sizeof(array_User), (void[]){
|
||||
(User){
|
||||
},
|
||||
});
|
||||
bool b = bools[0];
|
||||
array_string mystrings = new_array_from_c_array(2, 2, sizeof(array_string), {
|
||||
array_string mystrings = new_array_from_c_array(2, 2, sizeof(array_string), (void[]){
|
||||
tos3("a"), tos3("b"),
|
||||
});
|
||||
string s = mystrings[0];
|
||||
|
@ -116,12 +115,7 @@ multi_return_int_string multi_return() {
|
|||
|
||||
void variadic(variadic_int a) {
|
||||
int x = path_sep;
|
||||
int y = if (true) {
|
||||
1;
|
||||
}
|
||||
else {
|
||||
0;
|
||||
}
|
||||
int y = true ? 1 : 0;
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
@ -65,8 +65,8 @@ void function2() {
|
|||
}
|
||||
|
||||
void init_array() {
|
||||
array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), {
|
||||
1, 2, 3,
|
||||
array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), (void[]){
|
||||
4, 2, 3,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ fn function2() {
|
|||
}
|
||||
|
||||
fn init_array() {
|
||||
nums := [1,2,3]
|
||||
nums := [4,2,3]
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -25,13 +25,13 @@ int main() {
|
|||
string e = tos3("hello");
|
||||
e = testb(111);
|
||||
e = tos3("world");
|
||||
array_int f = new_array_from_c_array(4, 4, sizeof(array_int), {
|
||||
array_int f = new_array_from_c_array(4, 4, sizeof(array_int), (void[]){
|
||||
testa(), 2, 3, 4,
|
||||
});
|
||||
array_string g = new_array_from_c_array(2, 2, sizeof(array_string), {
|
||||
array_string g = new_array_from_c_array(2, 2, sizeof(array_string), (void[]){
|
||||
testb(1), tos3("hello"),
|
||||
});
|
||||
array_Foo arr_foo = new_array_from_c_array(1, 1, sizeof(array_Foo), {
|
||||
array_Foo arr_foo = new_array_from_c_array(1, 1, sizeof(array_Foo), (void[]){
|
||||
a,
|
||||
});
|
||||
Foo af_idx_el = arr_foo[0];
|
||||
|
|
|
@ -1052,7 +1052,8 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
|||
else if p.peek_tok.kind in [.decl_assign, .assign, .semicolon] || p.tok.kind == .semicolon {
|
||||
mut init := ast.Stmt{}
|
||||
mut cond := ast.Expr{}
|
||||
mut inc := ast.Stmt{}
|
||||
//mut inc := ast.Stmt{}
|
||||
mut inc := ast.Expr{}
|
||||
if p.peek_tok.kind in [.assign, .decl_assign] {
|
||||
init = p.var_decl_and_assign_stmt()
|
||||
}
|
||||
|
@ -1074,7 +1075,8 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
|||
}
|
||||
p.check(.semicolon)
|
||||
if p.tok.kind != .lcbr {
|
||||
inc = p.stmt()
|
||||
//inc = p.stmt()
|
||||
inc,_ = p.expr(0)
|
||||
}
|
||||
stmts := p.parse_block()
|
||||
p.close_scope()
|
||||
|
@ -1662,9 +1664,10 @@ fn (p mut Parser) var_decl_and_assign_stmt() ast.Stmt {
|
|||
// pub fn (p mut Parser) assign_stmt() ast.AssignStmt {}
|
||||
// fn (p mut Parser) var_decl() ast.VarDecl {}
|
||||
fn (p mut Parser) hash() ast.HashStmt {
|
||||
val := p.tok.lit
|
||||
p.next()
|
||||
return ast.HashStmt{
|
||||
name: p.tok.lit
|
||||
val: val
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1747,7 +1750,6 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
|
|||
branches << ast.MatchBranch{
|
||||
exprs: exprs
|
||||
stmts: stmts
|
||||
|
||||
}
|
||||
p.close_scope()
|
||||
if p.tok.kind == .rcbr {
|
||||
|
|
Loading…
Reference in New Issue