cgen: generate enums earlier

pull/4170/head
Alexander Medvednikov 2020-03-31 20:26:15 +02:00
parent 2b563bc69f
commit a3bd19ce73
3 changed files with 17 additions and 39 deletions

View File

@ -128,7 +128,8 @@ pub:
} }
pub struct InterfaceDecl { pub struct InterfaceDecl {
name string name string
field_names []string
} }
pub struct StructInit { pub struct StructInit {
@ -656,34 +657,6 @@ pub struct None {
pub: pub:
foo int // todo foo int // todo
} }
/*
enum BinaryOp {
sum
difference
product
quotient
remainder
bitwise_and
bitwise_or
bitwise_xor
left_shift
right_shift
equality
inequality
less_than
less_than_or_equal
more_than
more_than_or_equal
in_check
//These are suffixed with `bool` to prevent conflict with the keyword `or`
and_bool
or_bool
}
*/
[inline] [inline]
pub fn expr_is_blank_ident(expr Expr) bool { pub fn expr_is_blank_ident(expr Expr) bool {

View File

@ -323,21 +323,21 @@ fn (g mut Gen) stmt(node ast.Stmt) {
} }
ast.EnumDecl { ast.EnumDecl {
name := it.name.replace('.', '__') name := it.name.replace('.', '__')
g.definitions.writeln('typedef enum {') g.typedefs.writeln('typedef enum {')
for j, val in it.vals { for j, val in it.vals {
if j < it.default_exprs.len { if j < it.default_exprs.len {
g.definitions.write('\t${name}_$val = ') g.typedefs.write('\t${name}_$val = ')
pos := g.out.len pos := g.out.len
g.expr(it.default_exprs[j]) g.expr(it.default_exprs[j])
expr := g.out.after(pos) expr := g.out.after(pos)
g.out.go_back(expr.len) g.out.go_back(expr.len)
g.definitions.writeln('$expr ,') g.typedefs.writeln('$expr ,')
} }
else { else {
g.definitions.writeln('\t${name}_$val, // $j') g.typedefs.writeln('\t${name}_$val, // $j')
} }
} }
g.definitions.writeln('} $name;\n') g.typedefs.writeln('} $name;\n')
} }
ast.ExprStmt { ast.ExprStmt {
g.expr(it.expr) g.expr(it.expr)
@ -1977,7 +1977,7 @@ fn (g mut Gen) write_init_function() {
g.writeln('}') g.writeln('}')
if g.autofree { if g.autofree {
g.writeln('void _vcleanup() {') g.writeln('void _vcleanup() {')
//g.writeln('puts("cleaning up...");') // g.writeln('puts("cleaning up...");')
if g.is_importing_os() { if g.is_importing_os() {
g.writeln('free(_const_os__args.data);') g.writeln('free(_const_os__args.data);')
g.writeln('string_free(_const_os__wd_at_startup);') g.writeln('string_free(_const_os__wd_at_startup);')

View File

@ -1592,9 +1592,11 @@ fn (p mut Parser) interface_decl() ast.InterfaceDecl {
p.next() // `interface` p.next() // `interface`
interface_name := p.check_name() interface_name := p.check_name()
p.check(.lcbr) p.check(.lcbr)
mut field_names := []string
for p.tok.kind != .rcbr && p.tok.kind != .eof { for p.tok.kind != .rcbr && p.tok.kind != .eof {
line_nr := p.tok.line_nr line_nr := p.tok.line_nr
name := p.check_name() name := p.check_name()
field_names << name
p.fn_args() p.fn_args()
if p.tok.kind == .name && p.tok.line_nr == line_nr { if p.tok.kind == .name && p.tok.line_nr == line_nr {
p.parse_type() p.parse_type()
@ -1603,6 +1605,7 @@ fn (p mut Parser) interface_decl() ast.InterfaceDecl {
p.check(.rcbr) p.check(.rcbr)
return ast.InterfaceDecl{ return ast.InterfaceDecl{
name: interface_name name: interface_name
field_names: field_names
} }
} }
@ -1925,11 +1928,13 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
} }
} }
fn (p &Parser) new_true_expr() ast.Expr {
return ast.BoolLiteral{
val: true
}
}
fn verror(s string) { fn verror(s string) {
println(s) println(s)
exit(1) exit(1)
} }
fn (p &Parser) new_true_expr() ast.Expr {
return ast.BoolLiteral{ val: true }
}