From a3bd19ce73408a8923e278def0af36c82c0a33c9 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 31 Mar 2020 20:26:15 +0200 Subject: [PATCH] cgen: generate enums earlier --- vlib/v/ast/ast.v | 31 ++----------------------------- vlib/v/gen/cgen.v | 12 ++++++------ vlib/v/parser/parser.v | 13 +++++++++---- 3 files changed, 17 insertions(+), 39 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 3f6fb63e8b..c03b598f02 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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 { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index ff605fe3e9..df0a717b82 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -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);') diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index a65604f482..8c6d4b377c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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 } -}