From 9d5a0d9f64801f5584370e87ac91233a8bee5079 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 7 May 2022 00:58:14 +0300 Subject: [PATCH] cgen: fix c2v consts; enum_decl() --- vlib/builtin/cfns.c.v | 3 ++ vlib/v/gen/c/cgen.v | 69 +++++++++++++++++++++++++------------------ 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/vlib/builtin/cfns.c.v b/vlib/builtin/cfns.c.v index 6c76248e10..fa3369a1ab 100644 --- a/vlib/builtin/cfns.c.v +++ b/vlib/builtin/cfns.c.v @@ -71,6 +71,9 @@ fn C.fclose(stream &C.FILE) int fn C.pclose(stream &C.FILE) int +fn C.strrchr(s &char, c int) &char +fn C.strchr(s &char, c int) &char + // process execution, os.process: [trusted] fn C.getpid() int diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 45420005ba..9c155f027f 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -1751,34 +1751,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { g.defer_stmts << defer_stmt } ast.EnumDecl { - enum_name := util.no_dots(node.name) - is_flag := node.is_flag - g.enum_typedefs.writeln('typedef enum {') - mut cur_enum_expr := '' - mut cur_enum_offset := 0 - for i, field in node.fields { - g.enum_typedefs.write_string('\t${enum_name}__$field.name') - if field.has_expr { - g.enum_typedefs.write_string(' = ') - expr_str := g.expr_string(field.expr) - g.enum_typedefs.write_string(expr_str) - cur_enum_expr = expr_str - cur_enum_offset = 0 - } else if is_flag { - g.enum_typedefs.write_string(' = ') - cur_enum_expr = '1 << $i' - g.enum_typedefs.write_string((1 << i).str()) - cur_enum_offset = 0 - } - cur_value := if cur_enum_offset > 0 { - '$cur_enum_expr+$cur_enum_offset' - } else { - cur_enum_expr - } - g.enum_typedefs.writeln(', // $cur_value') - cur_enum_offset++ - } - g.enum_typedefs.writeln('} $enum_name;\n') + g.enum_decl(node) } ast.ExprStmt { g.write_v_source_line_info(node.pos) @@ -3418,6 +3391,37 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) { } } +fn (mut g Gen) enum_decl(node ast.EnumDecl) { + enum_name := util.no_dots(node.name) + is_flag := node.is_flag + g.enum_typedefs.writeln('typedef enum {') + mut cur_enum_expr := '' + mut cur_enum_offset := 0 + for i, field in node.fields { + g.enum_typedefs.write_string('\t${enum_name}__$field.name') + if field.has_expr { + g.enum_typedefs.write_string(' = ') + expr_str := g.expr_string(field.expr) + g.enum_typedefs.write_string(expr_str) + cur_enum_expr = expr_str + cur_enum_offset = 0 + } else if is_flag { + g.enum_typedefs.write_string(' = ') + cur_enum_expr = '1 << $i' + g.enum_typedefs.write_string((1 << i).str()) + cur_enum_offset = 0 + } + cur_value := if cur_enum_offset > 0 { + '$cur_enum_expr+$cur_enum_offset' + } else { + cur_enum_expr + } + g.enum_typedefs.writeln(', // $cur_value') + cur_enum_offset++ + } + g.enum_typedefs.writeln('} $enum_name;\n') +} + fn (mut g Gen) enum_expr(node ast.Expr) { match node { ast.EnumVal { @@ -4490,8 +4494,15 @@ fn (mut g Gen) const_decl_simple_define(name string, val string) { } else { x = '_const_$x' } - g.definitions.write_string('#define $x ') + if g.pref.translated { + g.definitions.write_string('const int $x = ') + } else { + g.definitions.write_string('#define $x ') + } g.definitions.writeln(val) + if g.pref.translated { + g.definitions.write_string(';') + } } fn (mut g Gen) const_decl_init_later(mod string, name string, expr ast.Expr, typ ast.Type, unwrap_option bool) {