cgen: enum default values

pull/4170/head
Alexander Medvednikov 2020-03-31 19:43:11 +02:00
parent 43f72246aa
commit 2fe0e80569
4 changed files with 53 additions and 39 deletions

View File

@ -477,6 +477,7 @@ pub:
name string
is_pub bool
vals []string
default_exprs []Expr
}
pub struct AliasTypeDecl {

View File

@ -322,16 +322,22 @@ fn (g mut Gen) stmt(node ast.Stmt) {
g.defer_stmts << defer_stmt
}
ast.EnumDecl {
g.writeln('//')
/*
name := it.name.replace('.', '__')
g.definitions.writeln('typedef enum {')
for i, val in it.vals {
g.definitions.writeln('\t${name}_$val, // $i')
for j, val in it.vals {
if j < it.default_exprs.len {
g.definitions.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.definitions.writeln('} $name;')
*/
else {
g.definitions.writeln('\t${name}_$val, // $j')
}
}
g.definitions.writeln('} $name;\n')
}
ast.ExprStmt {
g.expr(it.expr)
@ -2070,13 +2076,6 @@ fn (g mut Gen) write_types(types []table.TypeSymbol) {
}
// table.Alias, table.SumType { TODO
table.Alias {}
table.Enum {
g.definitions.writeln('typedef enum {')
for j, val in it.vals {
g.definitions.writeln('\t${name}_$val, // $j')
}
g.definitions.writeln('} $name;\n')
}
table.SumType {
g.definitions.writeln('// Sum type')
g.definitions.writeln('
@ -2359,12 +2358,18 @@ fn (g mut Gen) fn_call(node ast.CallExpr) {
g.definitions.writeln('string ${styp}_str($styp* x) { return tos3("TODO_str"); }')
}
if g.autofree && !table.type_is_optional(typ) {
// Create a temporary variable so that the value can be freed
tmp := g.new_tmp_var()
// tmps << tmp
g.write('string $tmp = ${styp}_str(')
g.expr(node.args[0].expr)
g.writeln('); println($tmp); string_free($tmp); //MEM2 $styp')
}
else if sym.kind == .enum_ {
g.write('println(int_str(')
g.expr(node.args[0].expr)
g.write('))')
}
else {
// `println(int_str(10))`
// sym := g.table.get_type_symbol(node.args[0].typ)

View File

@ -1814,13 +1814,14 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
name := p.prepend_mod(p.check_name())
p.check(.lcbr)
mut vals := []string
mut default_exprs := []ast.Expr
for p.tok.kind != .eof && p.tok.kind != .rcbr {
val := p.check_name()
vals << val
// p.warn('enum val $val')
if p.tok.kind == .assign {
p.next()
p.expr(0)
default_exprs << p.expr(0)
}
}
p.check(.rcbr)
@ -1835,6 +1836,7 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
name: name
is_pub: is_pub
vals: vals
default_exprs: default_exprs
}
}

View File

@ -23,11 +23,9 @@ fn test_enum() {
assert Color.red == .red
assert Color.blue == .blue
assert Color.green == .green
assert Color.red != .blue
assert Color.red != .green
assert Color.blue != .green
mut color := Color.red
assert color == .red
color = .green
@ -47,9 +45,15 @@ fn test_match() {
color := Color.green
num := 3
match color {
.red { assert false }
.green { assert true }
else { assert false }
.red {
assert false
}
.green {
assert true
}
else {
assert false
}
}
println(color)
assert num == 3
@ -70,7 +74,7 @@ fn test_nums() {
assert d == -10
}
/*
enum Expr {
BoolExpr(bool)
IntExpr(int)
@ -98,6 +102,8 @@ fn test_typed_enum() {
}
*/
}
*/
/*
fn test_typed_enum() {