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

cgen: generate enums earlier

This commit is contained in:
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 {
name string
name string
field_names []string
}
pub struct StructInit {
@ -656,34 +657,6 @@ pub struct None {
pub:
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]
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 {
name := it.name.replace('.', '__')
g.definitions.writeln('typedef enum {')
g.typedefs.writeln('typedef enum {')
for j, val in it.vals {
if j < it.default_exprs.len {
g.definitions.write('\t${name}_$val = ')
g.typedefs.write('\t${name}_$val = ')
pos := g.out.len
g.expr(it.default_exprs[j])
expr := g.out.after(pos)
g.out.go_back(expr.len)
g.definitions.writeln('$expr ,')
g.typedefs.writeln('$expr ,')
}
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 {
g.expr(it.expr)
@ -1977,7 +1977,7 @@ fn (g mut Gen) write_init_function() {
g.writeln('}')
if g.autofree {
g.writeln('void _vcleanup() {')
//g.writeln('puts("cleaning up...");')
// g.writeln('puts("cleaning up...");')
if g.is_importing_os() {
g.writeln('free(_const_os__args.data);')
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`
interface_name := p.check_name()
p.check(.lcbr)
mut field_names := []string
for p.tok.kind != .rcbr && p.tok.kind != .eof {
line_nr := p.tok.line_nr
name := p.check_name()
field_names << name
p.fn_args()
if p.tok.kind == .name && p.tok.line_nr == line_nr {
p.parse_type()
@ -1603,6 +1605,7 @@ fn (p mut Parser) interface_decl() ast.InterfaceDecl {
p.check(.rcbr)
return ast.InterfaceDecl{
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) {
println(s)
exit(1)
}
fn (p &Parser) new_true_expr() ast.Expr {
return ast.BoolLiteral{ val: true }
}