From 91269961d03944a45eb65d42fd9e5b0583ef7e4d Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 6 Mar 2020 13:43:22 +0100 Subject: [PATCH] cgen: array types --- vlib/v/ast/ast.v | 22 +++++++++++----------- vlib/v/gen/cgen.v | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 3ef0e4354f..8ed066ab97 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -8,15 +8,15 @@ import ( v.table ) -pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral | -FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | -AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | -CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | +pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral | +FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | +AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | +CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | ConcatExpr | Type | AsCast -pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | -ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | -HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | +pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | +ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | +HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | LineComment | MultiLineComment | AssertStmt | UnsafeStmt // pub type Type = StructType | ArrayType // pub struct StructType { @@ -176,8 +176,8 @@ mut: pub struct Return { pub: - pos token.Position - exprs []Expr + pos token.Position + exprs []Expr } /* @@ -376,8 +376,8 @@ pub struct ForCStmt { pub: init Stmt // i := 0; cond Expr // i < 10; - //inc Stmt // i++; - inc Expr// i++; + // inc Stmt // i++; + inc Expr // i++; stmts []Stmt } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 62c8d4ec0b..67aed482ac 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -39,11 +39,27 @@ pub fn (g mut Gen) init() { g.definitions.writeln('#include ') // int64_t etc g.definitions.writeln(c_builtin_types) g.definitions.writeln(c_headers) + g.write_array_types() g.write_sorted_types() g.write_multi_return_types() g.definitions.writeln('// end of definitions #endif') } +// V type to C type +pub fn (g &Gen) typ(t string) string { + return t.replace_each(['.', '__']) +} + +pub fn (g mut Gen) write_array_types() { + for typ in g.table.types { + if typ.kind != .array { + continue + } + styp := typ.name.replace('.', '__') + g.definitions.writeln('typedef array $styp;') + } +} + pub fn (g mut Gen) write_multi_return_types() { g.definitions.writeln('// multi return structs') for typ in g.table.types {