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 name string
is_pub bool is_pub bool
vals []string vals []string
default_exprs []Expr
} }
pub struct AliasTypeDecl { pub struct AliasTypeDecl {

View File

@ -322,16 +322,22 @@ fn (g mut Gen) stmt(node ast.Stmt) {
g.defer_stmts << defer_stmt g.defer_stmts << defer_stmt
} }
ast.EnumDecl { ast.EnumDecl {
g.writeln('//')
/*
name := it.name.replace('.', '__') name := it.name.replace('.', '__')
g.definitions.writeln('typedef enum {') g.definitions.writeln('typedef enum {')
for i, val in it.vals { for j, val in it.vals {
g.definitions.writeln('\t${name}_$val, // $i') 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 { ast.ExprStmt {
g.expr(it.expr) 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.SumType { TODO
table.Alias {} 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 { table.SumType {
g.definitions.writeln('// Sum type') g.definitions.writeln('// Sum type')
g.definitions.writeln(' 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"); }') g.definitions.writeln('string ${styp}_str($styp* x) { return tos3("TODO_str"); }')
} }
if g.autofree && !table.type_is_optional(typ) { if g.autofree && !table.type_is_optional(typ) {
// Create a temporary variable so that the value can be freed
tmp := g.new_tmp_var() tmp := g.new_tmp_var()
// tmps << tmp // tmps << tmp
g.write('string $tmp = ${styp}_str(') g.write('string $tmp = ${styp}_str(')
g.expr(node.args[0].expr) g.expr(node.args[0].expr)
g.writeln('); println($tmp); string_free($tmp); //MEM2 $styp') 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 { else {
// `println(int_str(10))` // `println(int_str(10))`
// sym := g.table.get_type_symbol(node.args[0].typ) // 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()) name := p.prepend_mod(p.check_name())
p.check(.lcbr) p.check(.lcbr)
mut vals := []string mut vals := []string
mut default_exprs := []ast.Expr
for p.tok.kind != .eof && p.tok.kind != .rcbr { for p.tok.kind != .eof && p.tok.kind != .rcbr {
val := p.check_name() val := p.check_name()
vals << val vals << val
// p.warn('enum val $val') // p.warn('enum val $val')
if p.tok.kind == .assign { if p.tok.kind == .assign {
p.next() p.next()
p.expr(0) default_exprs << p.expr(0)
} }
} }
p.check(.rcbr) p.check(.rcbr)
@ -1835,6 +1836,7 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
name: name name: name
is_pub: is_pub is_pub: is_pub
vals: vals vals: vals
default_exprs: default_exprs
} }
} }

View File

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