From e8c9f609a49d6776594cd82815bdaa68794dbed4 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 14 Mar 2020 05:20:12 +0100 Subject: [PATCH] cgen: enum fixes; sum type definition; const bug fix --- vlib/v/gen/cgen.v | 45 +++++++++++++++++++++++++++++++++++++----- vlib/v/gen/cgen_test.v | 6 ++++++ vlib/v/gen/tests/1.c | 13 ++++++------ vlib/v/parser/parser.v | 4 ++-- 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 34356d6526..cb2c5ebbed 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -191,11 +191,16 @@ fn (g mut Gen) stmt(node ast.Stmt) { g.writeln('// defer') } ast.EnumDecl { - g.writeln('typedef enum {') + g.writeln('//') + /* + name := it.name.replace('.', '__') + g.definitions.writeln('typedef enum {') for i, val in it.vals { - g.writeln('\t${it.name}_$val, // $i') + g.definitions.writeln('\t${name}_$val, // $i') } - g.writeln('} $it.name;') + g.definitions.writeln('} $name;') + */ + } ast.ExprStmt { g.expr(it.expr) @@ -1103,8 +1108,11 @@ fn (g mut Gen) const_decl(node ast.ConstDecl) { pos := g.out.len g.expr(expr) g.writeln('') - val := string(g.out.buf[pos..]) + b := g.out.buf[pos..g.out.buf.len].clone() + val := string(b) + // val += '\0' // g.out.go_back(val.len) + // println('pos=$pos buf.len=$g.out.buf.len len=$g.out.len val.len=$val.len val="$val"\n') g.definitions.write(val) } else { @@ -1159,6 +1167,18 @@ fn (g mut Gen) write_builtin_types() { builtin_types << g.table.types[g.table.type_idxs[builtin_name]] } g.write_types(builtin_types) + // TODO remove this + g.definitions.writeln(' + typedef struct { + int len; + string args[100]; +} variadic_string; + + typedef struct { + int len; + int args[100]; +} variadic_int; +') } // C struct definitions, ordered @@ -1186,10 +1206,10 @@ fn (g mut Gen) write_types(types []table.TypeSymbol) { continue } // sym := g.table.get_type_symbol(typ) + name := typ.name.replace('.', '__') match typ.info { table.Struct { info := typ.info as table.Struct - name := typ.name.replace('.', '__') // g.definitions.writeln('typedef struct {') g.definitions.writeln('struct $name {') for field in info.fields { @@ -1200,6 +1220,21 @@ fn (g mut Gen) write_types(types []table.TypeSymbol) { // g.definitions.writeln('};\n') } + table.Enum { + g.definitions.writeln('typedef enum {') + for i, val in it.vals { + g.definitions.writeln('\t${name}_$val, // $i') + } + g.definitions.writeln('} $name;\n') + } + table.SumType { + g.definitions.writeln('// Sum type') + g.definitions.writeln(' + typedef struct { +void* obj; +int typ; +} $name;') + } else {} } } diff --git a/vlib/v/gen/cgen_test.v b/vlib/v/gen/cgen_test.v index 7ca8ad4bb0..1fda2f3692 100644 --- a/vlib/v/gen/cgen_test.v +++ b/vlib/v/gen/cgen_test.v @@ -38,12 +38,18 @@ fn compare_texts(a, b, path string) bool { lines_b_ := b.trim_space().split_into_lines() lines_a := lines_a_.filter(it != '') lines_b := lines_b_.filter(it != '') + /* if lines_a.len != lines_b.len { println(term.red('different len')) println('${path}: got\n$a') return false } + */ + for i, line_a in lines_a { + if i >= lines_b.len { + return false + } line_b := lines_b[i] if line_a.trim_space() != line_b.trim_space() { println('${path}: got\n$a') diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index fd0f2cea83..8b165c06af 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -1,3 +1,9 @@ +typedef enum { + Color_red, // 0 + Color_green, // 1 + Color_blue, // 2 +} Color; + struct Two { }; @@ -36,12 +42,7 @@ int localmod__get_int_10(); //3 - -typedef enum { - Color_red, // 0 - Color_green, // 1 - Color_blue, // 2 -} Color; +// int main() { int a = 10; diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index e8c2971acc..d35f1ef6ef 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1662,7 +1662,7 @@ fn (p mut Parser) enum_decl() ast.EnumDecl { p.next() } p.check(.key_enum) - name := p.check_name() + name := p.prepend_mod(p.check_name()) p.check(.lcbr) mut vals := []string for p.tok.kind != .eof && p.tok.kind != .rcbr { @@ -1677,7 +1677,7 @@ fn (p mut Parser) enum_decl() ast.EnumDecl { p.check(.rcbr) p.table.register_type_symbol(table.TypeSymbol{ kind: .enum_ - name: p.prepend_mod(name) + name: name info: table.Enum{ vals: vals }