2019-12-30 08:30:24 +01:00
|
|
|
module gen
|
2019-12-24 18:54:43 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
strings
|
2019-12-27 05:43:17 +01:00
|
|
|
v.ast
|
2020-01-02 20:09:15 +01:00
|
|
|
v.table
|
2020-03-05 23:27:21 +01:00
|
|
|
v.depgraph
|
2019-12-29 08:51:55 +01:00
|
|
|
term
|
2019-12-24 18:54:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
struct Gen {
|
2020-03-07 16:39:15 +01:00
|
|
|
out strings.Builder
|
|
|
|
typedefs strings.Builder
|
|
|
|
definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file)
|
|
|
|
table &table.Table
|
2020-01-07 12:10:07 +01:00
|
|
|
mut:
|
2020-03-07 16:39:15 +01:00
|
|
|
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
|
|
|
|
tmp_count int
|
2020-03-14 13:15:07 +01:00
|
|
|
varaidic_args map[string]int
|
2020-03-07 16:39:15 +01:00
|
|
|
is_c_call bool // e.g. `C.printf("v")`
|
2020-03-11 23:22:40 +01:00
|
|
|
is_assign_expr bool // inside left part of assign expr (for array_set(), etc)
|
2020-03-07 16:39:15 +01:00
|
|
|
is_array_set bool
|
2020-03-10 23:21:26 +01:00
|
|
|
is_amp bool // for `&Foo{}` to merge PrefixExpr `&` and StructInit `Foo{}`; also for `&byte(0)` etc
|
2020-03-15 08:18:42 +01:00
|
|
|
optionals []string // to avoid duplicates TODO perf, use map
|
2020-03-18 08:41:49 +01:00
|
|
|
inside_ternary bool // ?: comma separated statements on a single line
|
2019-12-24 18:54:43 +01:00
|
|
|
}
|
|
|
|
|
2020-01-02 20:09:15 +01:00
|
|
|
pub fn cgen(files []ast.File, table &table.Table) string {
|
2020-03-04 17:08:28 +01:00
|
|
|
println('start cgen2')
|
2019-12-28 11:02:06 +01:00
|
|
|
mut g := Gen{
|
|
|
|
out: strings.new_builder(100)
|
2020-03-04 17:08:28 +01:00
|
|
|
typedefs: strings.new_builder(100)
|
2020-01-02 08:30:15 +01:00
|
|
|
definitions: strings.new_builder(100)
|
2020-01-02 20:09:15 +01:00
|
|
|
table: table
|
2020-01-07 12:10:07 +01:00
|
|
|
fn_decl: 0
|
2019-12-28 11:02:06 +01:00
|
|
|
}
|
2020-03-04 17:08:28 +01:00
|
|
|
g.init()
|
2019-12-30 12:10:46 +01:00
|
|
|
for file in files {
|
2020-02-17 22:50:04 +01:00
|
|
|
g.stmts(file.stmts)
|
2019-12-24 18:54:43 +01:00
|
|
|
}
|
2020-03-14 13:15:07 +01:00
|
|
|
g.write_variadic_types()
|
2020-03-04 17:08:28 +01:00
|
|
|
return g.typedefs.str() + g.definitions.str() + g.out.str()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (g mut Gen) init() {
|
|
|
|
g.definitions.writeln('// Generated by the V compiler')
|
|
|
|
g.definitions.writeln('#include <inttypes.h>') // int64_t etc
|
|
|
|
g.definitions.writeln(c_builtin_types)
|
|
|
|
g.definitions.writeln(c_headers)
|
2020-03-10 23:21:26 +01:00
|
|
|
g.write_builtin_types()
|
2020-03-12 07:56:44 +01:00
|
|
|
g.write_typedef_types()
|
2020-03-05 23:27:21 +01:00
|
|
|
g.write_sorted_types()
|
|
|
|
g.write_multi_return_types()
|
2020-03-06 12:06:41 +01:00
|
|
|
g.definitions.writeln('// end of definitions #endif')
|
2020-03-05 23:27:21 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 13:43:22 +01:00
|
|
|
// V type to C type
|
2020-03-13 05:57:51 +01:00
|
|
|
pub fn (g mut Gen) typ(t table.Type) string {
|
2020-03-06 22:36:51 +01:00
|
|
|
nr_muls := table.type_nr_muls(t)
|
|
|
|
sym := g.table.get_type_symbol(t)
|
|
|
|
mut styp := sym.name.replace_each(['.', '__'])
|
|
|
|
if nr_muls > 0 {
|
|
|
|
styp += strings.repeat(`*`, nr_muls)
|
|
|
|
}
|
2020-03-13 01:43:30 +01:00
|
|
|
if styp.starts_with('C__') {
|
|
|
|
styp = styp[3..]
|
|
|
|
}
|
2020-03-15 00:46:08 +01:00
|
|
|
if styp in ['stat', 'dirent*', 'tm'] {
|
2020-03-13 01:43:30 +01:00
|
|
|
// TODO perf and other C structs
|
2020-03-13 12:22:36 +01:00
|
|
|
styp = 'struct $styp'
|
2020-03-13 01:43:30 +01:00
|
|
|
}
|
2020-03-13 05:57:51 +01:00
|
|
|
if table.type_is_optional(t) {
|
|
|
|
styp = 'Option_' + styp
|
2020-03-15 08:18:42 +01:00
|
|
|
if !(styp in g.optionals) {
|
|
|
|
g.definitions.writeln('typedef Option $styp;')
|
|
|
|
g.optionals << styp
|
|
|
|
}
|
2020-03-13 05:57:51 +01:00
|
|
|
}
|
2020-03-06 22:36:51 +01:00
|
|
|
return styp
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
pub fn (g &Gen) styp(t string) string {
|
2020-03-06 13:43:22 +01:00
|
|
|
return t.replace_each(['.', '__'])
|
|
|
|
}
|
2020-03-06 22:36:51 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-16 10:53:28 +01:00
|
|
|
//
|
2020-03-12 07:56:44 +01:00
|
|
|
pub fn (g mut Gen) write_typedef_types() {
|
2020-03-06 13:43:22 +01:00
|
|
|
for typ in g.table.types {
|
2020-03-12 07:56:44 +01:00
|
|
|
match typ.kind {
|
|
|
|
.alias {
|
|
|
|
parent := &g.table.types[typ.parent_idx]
|
|
|
|
styp := typ.name.replace('.', '__')
|
|
|
|
parent_styp := parent.name.replace('.', '__')
|
|
|
|
g.definitions.writeln('typedef $parent_styp $styp;')
|
|
|
|
}
|
|
|
|
.array {
|
|
|
|
styp := typ.name.replace('.', '__')
|
|
|
|
g.definitions.writeln('typedef array $styp;')
|
|
|
|
}
|
|
|
|
.array_fixed {
|
|
|
|
styp := typ.name.replace('.', '__')
|
|
|
|
// array_fixed_char_300 => char x[300]
|
|
|
|
mut fixed := styp[12..]
|
|
|
|
len := styp.after('_')
|
|
|
|
fixed = fixed[..fixed.len - len.len - 1]
|
|
|
|
g.definitions.writeln('typedef $fixed $styp [$len];')
|
|
|
|
}
|
|
|
|
.map {
|
|
|
|
styp := typ.name.replace('.', '__')
|
|
|
|
g.definitions.writeln('typedef map $styp;')
|
|
|
|
}
|
2020-03-16 10:12:03 +01:00
|
|
|
.function {
|
|
|
|
info := typ.info as table.FnType
|
|
|
|
func := info.func
|
|
|
|
if !info.has_decl && !info.is_anon {
|
|
|
|
fn_name := func.name.replace('.', '__')
|
|
|
|
g.definitions.write('typedef ${g.typ(func.return_type)} (*$fn_name)(')
|
2020-03-18 08:41:49 +01:00
|
|
|
for i, arg in func.args {
|
2020-03-16 10:12:03 +01:00
|
|
|
g.definitions.write(g.typ(arg.typ))
|
|
|
|
if i < func.args.len - 1 {
|
|
|
|
g.definitions.write(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.definitions.writeln(');')
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 07:56:44 +01:00
|
|
|
else {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-12 09:11:41 +01:00
|
|
|
}
|
2020-03-06 13:43:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 23:27:21 +01:00
|
|
|
pub fn (g mut Gen) write_multi_return_types() {
|
2020-03-04 20:17:29 +01:00
|
|
|
g.definitions.writeln('// multi return structs')
|
|
|
|
for typ in g.table.types {
|
|
|
|
// sym := g.table.get_type_symbol(typ)
|
|
|
|
if typ.kind != .multi_return {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := typ.name.replace('.', '__')
|
|
|
|
info := typ.info as table.MultiReturn
|
|
|
|
g.definitions.writeln('typedef struct {')
|
|
|
|
// TODO copy pasta StructDecl
|
|
|
|
// for field in struct_info.fields {
|
|
|
|
for i, mr_typ in info.types {
|
2020-03-07 01:05:19 +01:00
|
|
|
type_name := g.typ(mr_typ)
|
2020-03-05 00:43:02 +01:00
|
|
|
g.definitions.writeln('\t$type_name arg${i};')
|
2020-03-04 20:17:29 +01:00
|
|
|
}
|
|
|
|
g.definitions.writeln('} $name;\n')
|
|
|
|
// g.typedefs.writeln('typedef struct $name $name;')
|
|
|
|
}
|
2019-12-24 18:54:43 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 13:15:07 +01:00
|
|
|
pub fn (g mut Gen) write_variadic_types() {
|
|
|
|
if g.varaidic_args.size > 0 {
|
|
|
|
g.definitions.writeln('// variadic structs')
|
|
|
|
}
|
|
|
|
for type_str, arg_len in g.varaidic_args {
|
|
|
|
typ := table.Type(type_str.int())
|
|
|
|
type_name := g.typ(typ)
|
|
|
|
struct_name := 'varg_' + type_name.replace('*', '_ptr')
|
|
|
|
g.definitions.writeln('struct $struct_name {')
|
|
|
|
g.definitions.writeln('\tint len;')
|
|
|
|
g.definitions.writeln('\t$type_name args[$arg_len];')
|
|
|
|
g.definitions.writeln('};\n')
|
|
|
|
g.typedefs.writeln('typedef struct $struct_name $struct_name;')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 11:02:06 +01:00
|
|
|
pub fn (g &Gen) save() {}
|
2019-12-24 18:54:43 +01:00
|
|
|
|
|
|
|
pub fn (g mut Gen) write(s string) {
|
|
|
|
g.out.write(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (g mut Gen) writeln(s string) {
|
|
|
|
g.out.writeln(s)
|
|
|
|
}
|
|
|
|
|
2020-02-18 03:17:21 +01:00
|
|
|
pub fn (g mut Gen) new_tmp_var() string {
|
|
|
|
g.tmp_count++
|
|
|
|
return 'tmp$g.tmp_count'
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (g mut Gen) reset_tmp_count() {
|
|
|
|
g.tmp_count = 0
|
|
|
|
}
|
|
|
|
|
2020-02-07 14:49:14 +01:00
|
|
|
fn (g mut Gen) stmts(stmts []ast.Stmt) {
|
|
|
|
for stmt in stmts {
|
|
|
|
g.stmt(stmt)
|
2020-03-18 08:41:49 +01:00
|
|
|
if !g.inside_ternary {
|
|
|
|
g.writeln('')
|
|
|
|
}
|
2020-02-07 14:49:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 14:11:05 +01:00
|
|
|
fn (g mut Gen) stmt(node ast.Stmt) {
|
2020-01-06 16:13:12 +01:00
|
|
|
// println('cgen.stmt()')
|
|
|
|
// g.writeln('//// stmt start')
|
2019-12-28 14:11:05 +01:00
|
|
|
match node {
|
2020-03-02 18:43:41 +01:00
|
|
|
ast.AssignStmt {
|
2020-03-10 12:01:37 +01:00
|
|
|
g.gen_assign_stmt(it)
|
2020-03-02 18:43:41 +01:00
|
|
|
}
|
|
|
|
ast.AssertStmt {
|
2020-03-11 03:52:01 +01:00
|
|
|
g.writeln('// assert')
|
2020-03-02 18:43:41 +01:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
ast.Attr {
|
2020-03-05 00:43:02 +01:00
|
|
|
g.writeln('//[$it.name]')
|
2020-03-02 18:43:41 +01:00
|
|
|
}
|
|
|
|
ast.BranchStmt {
|
|
|
|
// continue or break
|
2020-03-05 00:43:02 +01:00
|
|
|
g.write(it.tok.kind.str())
|
|
|
|
g.writeln(';')
|
2020-03-02 18:43:41 +01:00
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
ast.ConstDecl {
|
2020-03-06 14:03:35 +01:00
|
|
|
g.const_decl(it)
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
2020-03-02 18:43:41 +01:00
|
|
|
ast.CompIf {
|
|
|
|
// TODO
|
2020-03-05 00:43:02 +01:00
|
|
|
g.writeln('//#ifdef ')
|
2020-03-02 18:43:41 +01:00
|
|
|
g.expr(it.cond)
|
|
|
|
g.stmts(it.stmts)
|
2020-03-05 00:43:02 +01:00
|
|
|
g.writeln('//#endif')
|
2020-03-02 18:43:41 +01:00
|
|
|
}
|
2020-03-04 15:48:43 +01:00
|
|
|
ast.DeferStmt {
|
|
|
|
g.writeln('// defer')
|
|
|
|
}
|
2020-02-25 11:52:41 +01:00
|
|
|
ast.EnumDecl {
|
2020-03-14 05:20:12 +01:00
|
|
|
g.writeln('//')
|
|
|
|
/*
|
|
|
|
name := it.name.replace('.', '__')
|
|
|
|
g.definitions.writeln('typedef enum {')
|
2020-02-25 11:52:41 +01:00
|
|
|
for i, val in it.vals {
|
2020-03-14 05:20:12 +01:00
|
|
|
g.definitions.writeln('\t${name}_$val, // $i')
|
2020-02-25 11:52:41 +01:00
|
|
|
}
|
2020-03-14 05:20:12 +01:00
|
|
|
g.definitions.writeln('} $name;')
|
|
|
|
*/
|
|
|
|
|
2020-02-25 11:52:41 +01:00
|
|
|
}
|
2020-03-02 18:43:41 +01:00
|
|
|
ast.ExprStmt {
|
|
|
|
g.expr(it.expr)
|
|
|
|
match it.expr {
|
|
|
|
// no ; after an if expression
|
|
|
|
ast.IfExpr {}
|
|
|
|
else {
|
2020-03-18 08:41:49 +01:00
|
|
|
if !g.inside_ternary {
|
|
|
|
g.writeln(';')
|
|
|
|
}
|
2020-03-02 18:43:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-28 14:11:05 +01:00
|
|
|
ast.FnDecl {
|
2020-02-03 11:13:36 +01:00
|
|
|
g.fn_decl = it // &it
|
2020-03-06 17:05:58 +01:00
|
|
|
g.gen_fn_decl(it)
|
2019-12-28 14:11:05 +01:00
|
|
|
}
|
2020-03-02 18:43:41 +01:00
|
|
|
ast.ForCStmt {
|
|
|
|
g.write('for (')
|
2020-03-11 02:44:30 +01:00
|
|
|
if !it.has_init {
|
|
|
|
g.write('; ')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.stmt(it.init)
|
|
|
|
}
|
2020-03-02 18:43:41 +01:00
|
|
|
g.expr(it.cond)
|
|
|
|
g.write('; ')
|
2020-03-05 01:20:36 +01:00
|
|
|
// g.stmt(it.inc)
|
2020-03-05 00:43:02 +01:00
|
|
|
g.expr(it.inc)
|
2020-03-02 18:43:41 +01:00
|
|
|
g.writeln(') {')
|
2020-03-18 13:18:18 +01:00
|
|
|
g.stmts(it.stmts)
|
2020-03-02 18:43:41 +01:00
|
|
|
g.writeln('}')
|
|
|
|
}
|
|
|
|
ast.ForInStmt {
|
2020-03-05 00:43:02 +01:00
|
|
|
if it.is_range {
|
|
|
|
i := g.new_tmp_var()
|
|
|
|
g.write('for (int $i = ')
|
|
|
|
g.expr(it.cond)
|
|
|
|
g.write('; $i < ')
|
|
|
|
g.expr(it.high)
|
|
|
|
g.writeln('; $i++) { ')
|
2020-03-18 14:50:21 +01:00
|
|
|
g.writeln('int $it.val_var = $i;')
|
2020-03-18 13:18:18 +01:00
|
|
|
g.stmts(it.stmts)
|
2020-03-05 00:43:02 +01:00
|
|
|
g.writeln('}')
|
|
|
|
}
|
2020-03-18 16:07:52 +01:00
|
|
|
// TODO:
|
2020-03-18 14:50:21 +01:00
|
|
|
else {}
|
2020-03-02 18:43:41 +01:00
|
|
|
}
|
|
|
|
ast.ForStmt {
|
|
|
|
g.write('while (')
|
2020-03-10 23:21:26 +01:00
|
|
|
if it.is_inf {
|
|
|
|
g.write('1')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.expr(it.cond)
|
|
|
|
}
|
2020-03-07 06:02:32 +01:00
|
|
|
g.writeln(') {')
|
2020-03-18 13:18:18 +01:00
|
|
|
g.stmts(it.stmts)
|
2020-03-02 18:43:41 +01:00
|
|
|
g.writeln('}')
|
|
|
|
}
|
|
|
|
ast.GlobalDecl {
|
2020-03-06 22:36:51 +01:00
|
|
|
styp := g.typ(it.typ)
|
2020-03-06 16:31:40 +01:00
|
|
|
g.definitions.writeln('$styp $it.name; // global')
|
2020-03-02 18:43:41 +01:00
|
|
|
}
|
2020-03-04 15:48:43 +01:00
|
|
|
ast.GotoLabel {
|
|
|
|
g.writeln('$it.name:')
|
|
|
|
}
|
2020-03-02 18:43:41 +01:00
|
|
|
ast.HashStmt {
|
2020-03-05 00:43:02 +01:00
|
|
|
// #include etc
|
2020-03-15 00:46:08 +01:00
|
|
|
typ := it.val.all_before(' ')
|
2020-03-15 01:36:17 +01:00
|
|
|
if typ in ['include', 'define'] {
|
2020-03-15 00:46:08 +01:00
|
|
|
g.definitions.writeln('#$it.val')
|
|
|
|
}
|
2020-03-02 18:43:41 +01:00
|
|
|
}
|
|
|
|
ast.Import {}
|
2019-12-28 14:11:05 +01:00
|
|
|
ast.Return {
|
2020-03-13 05:57:51 +01:00
|
|
|
g.return_statement(it)
|
2019-12-28 14:11:05 +01:00
|
|
|
}
|
2019-12-30 06:16:59 +01:00
|
|
|
ast.StructDecl {
|
2020-03-04 20:17:29 +01:00
|
|
|
name := it.name.replace('.', '__')
|
2020-03-05 23:27:21 +01:00
|
|
|
// g.writeln('typedef struct {')
|
|
|
|
// for field in it.fields {
|
|
|
|
// field_type_sym := g.table.get_type_symbol(field.typ)
|
|
|
|
// g.writeln('\t$field_type_sym.name $field.name;')
|
|
|
|
// }
|
|
|
|
// g.writeln('} $name;')
|
2020-03-13 01:43:30 +01:00
|
|
|
if !it.is_c {
|
|
|
|
g.typedefs.writeln('typedef struct $name $name;')
|
|
|
|
}
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
2020-03-04 15:48:43 +01:00
|
|
|
ast.TypeDecl {
|
2020-03-16 03:19:26 +01:00
|
|
|
g.writeln('// TypeDecl')
|
2020-03-04 15:48:43 +01:00
|
|
|
}
|
2020-03-02 18:43:41 +01:00
|
|
|
ast.UnsafeStmt {
|
|
|
|
g.stmts(it.stmts)
|
|
|
|
}
|
2019-12-28 14:11:05 +01:00
|
|
|
else {
|
2020-02-25 11:52:41 +01:00
|
|
|
verror('cgen.stmt(): unhandled node ' + typeof(node))
|
2019-12-28 14:11:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 15:13:55 +01:00
|
|
|
// use instead of expr() when you need to cast to sum type (can add other casts also)
|
|
|
|
fn (g mut Gen) expr_with_cast(got_type table.Type, exp_type table.Type, expr ast.Expr) {
|
|
|
|
// cast to sum type
|
2020-03-18 08:41:49 +01:00
|
|
|
if exp_type != table.void_type && exp_type != 0 && got_type != 0 {
|
2020-03-16 07:42:45 +01:00
|
|
|
exp_sym := g.table.get_type_symbol(exp_type)
|
|
|
|
if exp_sym.kind == .sum_type {
|
|
|
|
sum_info := exp_sym.info as table.SumType
|
|
|
|
if got_type in sum_info.variants {
|
2020-03-17 15:13:55 +01:00
|
|
|
got_styp := g.typ(got_type)
|
|
|
|
exp_styp := g.typ(exp_type)
|
|
|
|
got_idx := table.type_idx(got_type)
|
2020-03-17 16:40:41 +01:00
|
|
|
g.write('/* sum type cast */ ($exp_styp) {.obj = memdup(&(${got_styp}[]) {')
|
2020-03-17 15:13:55 +01:00
|
|
|
g.expr(expr)
|
2020-03-18 10:42:56 +01:00
|
|
|
g.write('}, sizeof($got_styp)), .typ = $got_idx}')
|
|
|
|
return
|
2020-03-16 07:42:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 15:13:55 +01:00
|
|
|
// no cast
|
2020-03-16 07:42:45 +01:00
|
|
|
g.expr(expr)
|
|
|
|
}
|
|
|
|
|
2020-03-10 12:01:37 +01:00
|
|
|
fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
|
|
|
// multi return
|
2020-03-13 01:43:30 +01:00
|
|
|
// g.write('/*assign*/')
|
2020-03-10 12:01:37 +01:00
|
|
|
if assign_stmt.left.len > assign_stmt.right.len {
|
|
|
|
mut return_type := table.void_type
|
|
|
|
match assign_stmt.right[0] {
|
|
|
|
ast.CallExpr {
|
2020-03-11 10:48:45 +01:00
|
|
|
return_type = it.return_type
|
2020-03-10 12:01:37 +01:00
|
|
|
}
|
|
|
|
ast.MethodCallExpr {
|
2020-03-11 10:48:45 +01:00
|
|
|
return_type = it.return_type
|
2020-03-10 12:01:37 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic('expected call')
|
|
|
|
}
|
2020-03-10 23:21:26 +01:00
|
|
|
}
|
2020-03-10 12:01:37 +01:00
|
|
|
mr_var_name := 'mr_$assign_stmt.pos.pos'
|
2020-03-10 23:21:26 +01:00
|
|
|
mr_typ_str := g.typ(return_type)
|
|
|
|
g.write('$mr_typ_str $mr_var_name = ')
|
2020-03-10 12:01:37 +01:00
|
|
|
g.expr(assign_stmt.right[0])
|
|
|
|
g.writeln(';')
|
|
|
|
for i, ident in assign_stmt.left {
|
2020-03-10 13:35:25 +01:00
|
|
|
if ident.kind == .blank_ident {
|
2020-03-11 01:31:24 +01:00
|
|
|
continue
|
2020-03-10 13:35:25 +01:00
|
|
|
}
|
2020-03-11 01:31:24 +01:00
|
|
|
ident_var_info := ident.var_info()
|
|
|
|
styp := g.typ(ident_var_info.typ)
|
|
|
|
if assign_stmt.op == .decl_assign {
|
|
|
|
g.write('$styp ')
|
2020-03-10 13:35:25 +01:00
|
|
|
}
|
2020-03-11 01:31:24 +01:00
|
|
|
g.expr(ident)
|
|
|
|
g.writeln(' = ${mr_var_name}.arg$i;')
|
2020-03-10 12:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// `a := 1` | `a,b := 1,2`
|
|
|
|
else {
|
|
|
|
for i, ident in assign_stmt.left {
|
|
|
|
val := assign_stmt.right[i]
|
|
|
|
ident_var_info := ident.var_info()
|
2020-03-10 23:21:26 +01:00
|
|
|
styp := g.typ(ident_var_info.typ)
|
2020-03-10 13:35:25 +01:00
|
|
|
if ident.kind == .blank_ident {
|
2020-03-18 01:19:23 +01:00
|
|
|
if ast.expr_is_call(val) {
|
2020-03-10 23:21:26 +01:00
|
|
|
g.expr(val)
|
2020-03-10 13:35:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-03-10 23:21:26 +01:00
|
|
|
g.write('{$styp _ = ')
|
2020-03-10 13:35:25 +01:00
|
|
|
g.expr(val)
|
2020-03-18 09:56:19 +01:00
|
|
|
g.writeln(';}')
|
2020-03-10 13:35:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-03-12 10:07:42 +01:00
|
|
|
mut is_fixed_array_init := false
|
|
|
|
match val {
|
|
|
|
ast.ArrayInit {
|
|
|
|
is_fixed_array_init = g.table.get_type_symbol(it.typ).kind == .array_fixed
|
|
|
|
}
|
|
|
|
else {}
|
2020-03-12 20:26:29 +01:00
|
|
|
}
|
2020-03-17 15:13:55 +01:00
|
|
|
is_decl := assign_stmt.op == .decl_assign
|
|
|
|
if is_decl {
|
2020-03-11 01:31:24 +01:00
|
|
|
g.write('$styp ')
|
|
|
|
}
|
|
|
|
g.expr(ident)
|
2020-03-12 10:07:42 +01:00
|
|
|
if !is_fixed_array_init {
|
|
|
|
g.write(' = ')
|
2020-03-17 15:13:55 +01:00
|
|
|
if !is_decl {
|
|
|
|
g.expr_with_cast(assign_stmt.left_types[i], ident_var_info.typ, val)
|
2020-03-18 08:41:49 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-03-16 07:42:45 +01:00
|
|
|
g.expr(val)
|
|
|
|
}
|
2020-03-12 10:07:42 +01:00
|
|
|
}
|
2020-03-10 13:35:25 +01:00
|
|
|
}
|
2020-03-10 12:01:37 +01:00
|
|
|
g.writeln(';')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 17:05:58 +01:00
|
|
|
fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
|
|
|
|
if it.is_c || it.name == 'malloc' || it.no_body {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
g.reset_tmp_count()
|
|
|
|
is_main := it.name == 'main'
|
|
|
|
if is_main {
|
|
|
|
g.write('int ${it.name}(')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mut name := it.name
|
|
|
|
if it.is_method {
|
|
|
|
name = g.table.get_type_symbol(it.receiver.typ).name + '_' + name
|
|
|
|
}
|
|
|
|
name = name.replace('.', '__')
|
2020-03-10 14:40:30 +01:00
|
|
|
if name.starts_with('_op_') {
|
|
|
|
name = op_to_fn_name(name)
|
|
|
|
}
|
2020-03-13 01:43:30 +01:00
|
|
|
if name == 'exit' {
|
|
|
|
name = 'v_exit'
|
|
|
|
}
|
2020-03-11 16:10:46 +01:00
|
|
|
// type_name := g.table.type_to_str(it.return_type)
|
|
|
|
type_name := g.typ(it.return_type)
|
2020-03-06 17:05:58 +01:00
|
|
|
g.write('$type_name ${name}(')
|
|
|
|
g.definitions.write('$type_name ${name}(')
|
|
|
|
}
|
|
|
|
// Receiver is the first argument
|
2020-03-10 23:21:26 +01:00
|
|
|
/*
|
2020-03-06 17:05:58 +01:00
|
|
|
if it.is_method {
|
2020-03-07 22:37:03 +01:00
|
|
|
mut styp := g.typ(it.receiver.typ)
|
|
|
|
// if table.type_nr_muls(it.receiver.typ) > 0 {
|
2020-03-08 19:38:27 +01:00
|
|
|
// if it.rec_mut {
|
|
|
|
// styp += '*'
|
|
|
|
// }
|
2020-03-10 23:21:26 +01:00
|
|
|
g.write('$styp $it.receiver.name ')
|
2020-03-07 04:45:35 +01:00
|
|
|
// TODO mut
|
2020-03-06 17:05:58 +01:00
|
|
|
g.definitions.write('$styp $it.receiver.name')
|
|
|
|
if it.args.len > 0 {
|
|
|
|
g.write(', ')
|
|
|
|
g.definitions.write(', ')
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 23:21:26 +01:00
|
|
|
*/
|
2020-03-06 17:05:58 +01:00
|
|
|
//
|
2020-03-14 23:21:36 +01:00
|
|
|
g.fn_args(it.args, it.is_variadic)
|
|
|
|
g.writeln(') { ')
|
|
|
|
if !is_main {
|
|
|
|
g.definitions.writeln(');')
|
|
|
|
}
|
|
|
|
for stmt in it.stmts {
|
|
|
|
// g.write('\t')
|
|
|
|
g.stmt(stmt)
|
|
|
|
}
|
|
|
|
if is_main {
|
|
|
|
g.writeln('return 0;')
|
|
|
|
}
|
|
|
|
g.writeln('}')
|
|
|
|
g.fn_decl = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut Gen) fn_args(args []table.Arg, is_variadic bool) {
|
|
|
|
no_names := args.len > 0 && args[0].name == 'arg_1'
|
|
|
|
for i, arg in args {
|
2020-03-06 17:05:58 +01:00
|
|
|
arg_type_sym := g.table.get_type_symbol(arg.typ)
|
2020-03-15 00:46:08 +01:00
|
|
|
mut arg_type_name := g.typ(arg.typ) // arg_type_sym.name.replace('.', '__')
|
2020-03-14 23:21:36 +01:00
|
|
|
is_varg := i == args.len - 1 && is_variadic
|
2020-03-14 13:15:07 +01:00
|
|
|
if is_varg {
|
|
|
|
g.varaidic_args[int(arg.typ).str()] = 0
|
|
|
|
arg_type_name = 'varg_' + g.typ(arg.typ).replace('*', '_ptr')
|
2020-03-06 17:05:58 +01:00
|
|
|
}
|
2020-03-12 04:34:00 +01:00
|
|
|
if arg_type_sym.kind == .function {
|
2020-03-16 10:12:03 +01:00
|
|
|
info := arg_type_sym.info as table.FnType
|
|
|
|
func := info.func
|
2020-03-12 04:34:00 +01:00
|
|
|
g.write('${g.typ(func.return_type)} (*$arg.name)(')
|
|
|
|
g.definitions.write('${g.typ(func.return_type)} (*$arg.name)(')
|
2020-03-14 23:21:36 +01:00
|
|
|
g.fn_args(func.args, func.is_variadic)
|
2020-03-12 04:34:00 +01:00
|
|
|
g.write(')')
|
|
|
|
g.definitions.write(')')
|
|
|
|
}
|
|
|
|
else if no_names {
|
2020-03-06 17:05:58 +01:00
|
|
|
g.write(arg_type_name)
|
|
|
|
g.definitions.write(arg_type_name)
|
|
|
|
}
|
|
|
|
else {
|
2020-03-11 21:11:27 +01:00
|
|
|
mut nr_muls := table.type_nr_muls(arg.typ)
|
2020-03-15 00:46:08 +01:00
|
|
|
s := arg_type_name + ' ' + arg.name
|
2020-03-11 21:11:27 +01:00
|
|
|
if arg.is_mut {
|
|
|
|
// mut arg needs one *
|
|
|
|
nr_muls = 1
|
|
|
|
}
|
2020-03-15 00:46:08 +01:00
|
|
|
// if nr_muls > 0 && !is_varg {
|
|
|
|
// s = arg_type_name + strings.repeat(`*`, nr_muls) + ' ' + arg.name
|
|
|
|
// }
|
2020-03-06 20:22:58 +01:00
|
|
|
g.write(s)
|
|
|
|
g.definitions.write(s)
|
2020-03-06 17:05:58 +01:00
|
|
|
}
|
2020-03-14 23:21:36 +01:00
|
|
|
if i < args.len - 1 {
|
2020-03-06 17:05:58 +01:00
|
|
|
g.write(', ')
|
|
|
|
g.definitions.write(', ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:27:35 +01:00
|
|
|
fn (g mut Gen) expr(node ast.Expr) {
|
2020-03-04 15:48:43 +01:00
|
|
|
// println('cgen expr() line_nr=$node.pos.line_nr')
|
2019-12-24 18:54:43 +01:00
|
|
|
match node {
|
2020-02-17 22:50:04 +01:00
|
|
|
ast.ArrayInit {
|
|
|
|
type_sym := g.table.get_type_symbol(it.typ)
|
2020-03-12 09:29:52 +01:00
|
|
|
if type_sym.kind != .array_fixed {
|
|
|
|
elem_sym := g.table.get_type_symbol(it.elem_type)
|
2020-03-15 07:42:45 +01:00
|
|
|
elem_type_str := g.typ(it.elem_type)
|
|
|
|
g.write('new_array_from_c_array($it.exprs.len, $it.exprs.len, sizeof($elem_type_str), ')
|
|
|
|
g.writeln('($elem_type_str[]){\t')
|
2020-03-12 09:29:52 +01:00
|
|
|
for expr in it.exprs {
|
|
|
|
g.expr(expr)
|
|
|
|
g.write(', ')
|
|
|
|
}
|
|
|
|
g.write('\n})')
|
2020-02-17 22:50:04 +01:00
|
|
|
}
|
2020-03-12 09:29:52 +01:00
|
|
|
else {}
|
2020-02-17 22:50:04 +01:00
|
|
|
}
|
2020-03-04 15:48:43 +01:00
|
|
|
ast.AsCast {
|
2020-03-18 13:55:46 +01:00
|
|
|
styp := g.typ(it.typ)
|
|
|
|
expr_type_sym := g.table.get_type_symbol(it.expr_type)
|
|
|
|
if expr_type_sym.kind == .sum_type {
|
|
|
|
g.write('/* as */ *($styp*)')
|
|
|
|
g.expr(it.expr)
|
|
|
|
g.write('.obj')
|
|
|
|
}
|
2020-03-04 15:48:43 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
ast.AssignExpr {
|
2020-03-18 01:19:23 +01:00
|
|
|
if ast.expr_is_blank_ident(it.left) {
|
|
|
|
if ast.expr_is_call(it.val) {
|
2020-03-18 00:41:04 +01:00
|
|
|
g.expr(it.val)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.write('{${g.typ(it.left_type)} _ = ')
|
|
|
|
g.expr(it.val)
|
2020-03-18 09:56:19 +01:00
|
|
|
g.writeln(';}')
|
2020-03-18 00:41:04 +01:00
|
|
|
}
|
2020-03-07 16:39:15 +01:00
|
|
|
}
|
2020-03-18 00:41:04 +01:00
|
|
|
else {
|
|
|
|
g.is_assign_expr = true
|
|
|
|
mut str_add := false
|
|
|
|
if it.left_type == table.string_type_idx && it.op == .plus_assign {
|
|
|
|
// str += str2 => `str = string_add(str, str2)`
|
|
|
|
g.expr(it.left)
|
|
|
|
g.write(' = string_add(')
|
|
|
|
str_add = true
|
|
|
|
}
|
|
|
|
g.expr(it.left)
|
|
|
|
// arr[i] = val => `array_set(arr, i, val)`, not `array_get(arr, i) = val`
|
|
|
|
if !g.is_array_set && !str_add {
|
|
|
|
g.write(' $it.op.str() ')
|
|
|
|
}
|
|
|
|
else if str_add {
|
|
|
|
g.write(', ')
|
|
|
|
}
|
|
|
|
g.is_assign_expr = false
|
|
|
|
g.expr_with_cast(it.right_type, it.left_type, it.val)
|
|
|
|
if g.is_array_set {
|
|
|
|
g.write(' })')
|
|
|
|
g.is_array_set = false
|
|
|
|
}
|
|
|
|
else if str_add {
|
|
|
|
g.write(')')
|
|
|
|
}
|
2020-03-13 01:43:30 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-03-04 15:48:43 +01:00
|
|
|
ast.Assoc {
|
|
|
|
g.write('/* assoc */')
|
|
|
|
}
|
2020-02-17 22:50:04 +01:00
|
|
|
ast.BoolLiteral {
|
|
|
|
g.write(it.val.str())
|
|
|
|
}
|
2019-12-29 07:24:17 +01:00
|
|
|
ast.CallExpr {
|
2020-03-05 00:43:02 +01:00
|
|
|
mut name := it.name.replace('.', '__')
|
2020-03-13 01:43:30 +01:00
|
|
|
if name == 'exit' {
|
|
|
|
name = 'v_exit'
|
|
|
|
}
|
2020-03-05 00:43:02 +01:00
|
|
|
if it.is_c {
|
|
|
|
// Skip "C__"
|
2020-03-07 01:05:19 +01:00
|
|
|
g.is_c_call = true
|
2020-03-05 00:43:02 +01:00
|
|
|
name = name[3..]
|
|
|
|
}
|
2020-03-01 21:56:07 +01:00
|
|
|
g.write('${name}(')
|
2020-03-14 11:11:43 +01:00
|
|
|
if name == 'println' && it.args[0].typ != table.string_type_idx {
|
2020-03-11 23:22:40 +01:00
|
|
|
// `println(int_str(10))`
|
2020-03-14 11:11:43 +01:00
|
|
|
sym := g.table.get_type_symbol(it.args[0].typ)
|
2020-03-11 23:22:40 +01:00
|
|
|
g.write('${sym.name}_str(')
|
2020-03-14 11:11:43 +01:00
|
|
|
g.expr(it.args[0].expr)
|
2020-03-11 23:22:40 +01:00
|
|
|
g.write('))')
|
|
|
|
}
|
|
|
|
else {
|
2020-03-14 11:11:43 +01:00
|
|
|
g.call_args(it.args)
|
2020-03-11 23:22:40 +01:00
|
|
|
g.write(')')
|
|
|
|
}
|
2020-03-07 01:05:19 +01:00
|
|
|
g.is_c_call = false
|
2020-03-01 13:07:51 +01:00
|
|
|
}
|
2020-03-03 18:37:38 +01:00
|
|
|
ast.CastExpr {
|
2020-03-10 23:21:26 +01:00
|
|
|
// g.write('/*cast*/')
|
|
|
|
if g.is_amp {
|
|
|
|
// &Foo(0) => ((Foo*)0)
|
|
|
|
g.out.go_back(1)
|
|
|
|
}
|
2020-03-07 16:23:10 +01:00
|
|
|
if it.typ == table.string_type_idx {
|
2020-03-11 19:00:51 +01:00
|
|
|
// `tos(str, len)`, `tos2(str)`
|
2020-03-10 23:21:26 +01:00
|
|
|
if it.has_arg {
|
|
|
|
g.write('tos(')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.write('tos2(')
|
|
|
|
}
|
2020-03-07 16:23:10 +01:00
|
|
|
g.expr(it.expr)
|
2020-03-11 19:00:51 +01:00
|
|
|
sym := g.table.get_type_symbol(it.expr_type)
|
|
|
|
if sym.kind == .array {
|
|
|
|
// if we are casting an array, we need to add `.data`
|
|
|
|
g.write('.data')
|
|
|
|
}
|
2020-03-07 16:23:10 +01:00
|
|
|
if it.has_arg {
|
2020-03-11 19:00:51 +01:00
|
|
|
// len argument
|
|
|
|
g.write(', ')
|
2020-03-07 16:23:10 +01:00
|
|
|
g.expr(it.arg)
|
|
|
|
}
|
|
|
|
g.write(')')
|
|
|
|
}
|
|
|
|
else {
|
2020-03-10 23:21:26 +01:00
|
|
|
// styp := g.table.type_to_str(it.typ)
|
|
|
|
styp := g.typ(it.typ)
|
|
|
|
// g.write('($styp)(')
|
|
|
|
g.write('(($styp)(')
|
|
|
|
// if g.is_amp {
|
|
|
|
// g.write('*')
|
|
|
|
// }
|
|
|
|
// g.write(')(')
|
2020-03-07 16:23:10 +01:00
|
|
|
g.expr(it.expr)
|
2020-03-10 23:21:26 +01:00
|
|
|
g.write('))')
|
2020-03-07 16:23:10 +01:00
|
|
|
}
|
2019-12-29 07:24:17 +01:00
|
|
|
}
|
2020-03-03 18:37:38 +01:00
|
|
|
ast.CharLiteral {
|
|
|
|
g.write("'$it.val'")
|
|
|
|
}
|
|
|
|
ast.EnumVal {
|
2020-03-15 00:46:08 +01:00
|
|
|
// g.write('/*EnumVal*/${it.mod}${it.enum_name}_$it.val')
|
2020-03-15 02:51:31 +01:00
|
|
|
g.write(g.typ(it.typ))
|
|
|
|
g.write('_$it.val')
|
2020-03-03 18:37:38 +01:00
|
|
|
}
|
|
|
|
ast.FloatLiteral {
|
|
|
|
g.write(it.val)
|
|
|
|
}
|
2019-12-28 14:11:05 +01:00
|
|
|
ast.Ident {
|
2020-03-13 05:57:51 +01:00
|
|
|
g.ident(it)
|
2019-12-24 18:54:43 +01:00
|
|
|
}
|
2019-12-29 08:51:55 +01:00
|
|
|
ast.IfExpr {
|
2020-03-17 16:40:41 +01:00
|
|
|
g.if_expr(it)
|
2019-12-29 08:51:55 +01:00
|
|
|
}
|
2020-03-04 15:48:43 +01:00
|
|
|
ast.IfGuardExpr {
|
|
|
|
g.write('/* guard */')
|
|
|
|
}
|
2020-03-03 18:37:38 +01:00
|
|
|
ast.IndexExpr {
|
|
|
|
g.index_expr(it)
|
|
|
|
}
|
|
|
|
ast.InfixExpr {
|
2020-03-07 16:39:15 +01:00
|
|
|
g.infix_expr(it)
|
2020-03-03 18:37:38 +01:00
|
|
|
}
|
|
|
|
ast.IntegerLiteral {
|
2020-03-17 02:49:15 +01:00
|
|
|
g.write(it.val.int().str())
|
2020-03-03 18:37:38 +01:00
|
|
|
}
|
2020-02-07 14:49:14 +01:00
|
|
|
ast.MatchExpr {
|
2020-03-16 03:56:38 +01:00
|
|
|
g.match_expr(it)
|
2020-02-07 14:49:14 +01:00
|
|
|
}
|
2020-03-07 08:13:00 +01:00
|
|
|
ast.MapInit {
|
|
|
|
key_typ_sym := g.table.get_type_symbol(it.key_type)
|
|
|
|
value_typ_sym := g.table.get_type_symbol(it.value_type)
|
2020-03-17 15:13:55 +01:00
|
|
|
key_typ_str := key_typ_sym.name.replace('.', '__')
|
|
|
|
value_typ_str := value_typ_sym.name.replace('.', '__')
|
2020-03-07 08:13:00 +01:00
|
|
|
size := it.vals.len
|
2020-03-07 14:31:40 +01:00
|
|
|
if size > 0 {
|
2020-03-17 15:13:55 +01:00
|
|
|
g.write('new_map_init($size, sizeof($value_typ_str), (${key_typ_str}[$size]){')
|
2020-03-07 08:13:00 +01:00
|
|
|
for expr in it.keys {
|
|
|
|
g.expr(expr)
|
|
|
|
g.write(', ')
|
|
|
|
}
|
2020-03-17 15:13:55 +01:00
|
|
|
g.write('}, (${value_typ_str}[$size]){')
|
2020-03-07 08:13:00 +01:00
|
|
|
for expr in it.vals {
|
|
|
|
g.expr(expr)
|
|
|
|
g.write(', ')
|
|
|
|
}
|
|
|
|
g.write('})')
|
|
|
|
}
|
|
|
|
else {
|
2020-03-17 15:13:55 +01:00
|
|
|
g.write('new_map(1, sizeof($value_typ_str))')
|
2020-03-07 08:13:00 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 18:37:38 +01:00
|
|
|
ast.MethodCallExpr {
|
2020-03-06 10:52:03 +01:00
|
|
|
// TODO: there are still due to unchecked exprs (opt/some fn arg)
|
2020-03-18 09:56:19 +01:00
|
|
|
if it.expr_type == 0 {
|
|
|
|
verror('method receiver type is 0, this means there are some uchecked exprs')
|
|
|
|
}
|
|
|
|
typ_sym := g.table.get_type_symbol(it.expr_type)
|
|
|
|
// rec_sym := g.table.get_type_symbol(it.receiver_type)
|
|
|
|
mut receiver_name := typ_sym.name
|
|
|
|
if typ_sym.kind == .array && it.name in
|
|
|
|
// TODO performance, detect `array` method differently
|
|
|
|
['repeat', 'sort_with_compare', 'free', 'push_many', 'trim', 'first', 'clone'] {
|
|
|
|
// && rec_sym.name == 'array' {
|
|
|
|
// && rec_sym.name == 'array' && receiver_name.starts_with('array') {
|
|
|
|
// `array_byte_clone` => `array_clone`
|
|
|
|
receiver_name = 'array'
|
2020-03-06 10:52:03 +01:00
|
|
|
}
|
|
|
|
name := '${receiver_name}_$it.name'.replace('.', '__')
|
2020-03-10 23:21:26 +01:00
|
|
|
// if it.receiver_type != 0 {
|
|
|
|
// g.write('/*${g.typ(it.receiver_type)}*/')
|
|
|
|
// g.write('/*expr_type=${g.typ(it.expr_type)} rec type=${g.typ(it.receiver_type)}*/')
|
|
|
|
// }
|
2020-03-06 10:52:03 +01:00
|
|
|
g.write('${name}(')
|
2020-03-10 23:21:26 +01:00
|
|
|
if table.type_is_ptr(it.receiver_type) && !table.type_is_ptr(it.expr_type) {
|
|
|
|
// The receiver is a reference, but the caller provided a value
|
|
|
|
// Add `&` automatically.
|
2020-03-12 09:11:41 +01:00
|
|
|
// TODO same logic in call_args()
|
2020-03-10 23:21:26 +01:00
|
|
|
g.write('&')
|
|
|
|
}
|
2020-03-12 09:11:41 +01:00
|
|
|
else if !table.type_is_ptr(it.receiver_type) && table.type_is_ptr(it.expr_type) {
|
|
|
|
g.write('/*rec*/*')
|
|
|
|
}
|
2020-03-03 18:37:38 +01:00
|
|
|
g.expr(it.expr)
|
|
|
|
if it.args.len > 0 {
|
|
|
|
g.write(', ')
|
|
|
|
}
|
2020-03-11 23:22:40 +01:00
|
|
|
// /////////
|
|
|
|
/*
|
|
|
|
if name.contains('subkeys') {
|
|
|
|
println('call_args $name $it.arg_types.len')
|
|
|
|
for t in it.arg_types {
|
|
|
|
sym := g.table.get_type_symbol(t)
|
|
|
|
print('$sym.name ')
|
|
|
|
}
|
|
|
|
println('')
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// ///////
|
2020-03-14 11:11:43 +01:00
|
|
|
g.call_args(it.args)
|
2020-03-03 18:37:38 +01:00
|
|
|
g.write(')')
|
|
|
|
}
|
2020-03-04 15:48:43 +01:00
|
|
|
ast.None {
|
2020-03-13 05:57:51 +01:00
|
|
|
g.write('opt_none()')
|
2020-03-04 15:48:43 +01:00
|
|
|
}
|
2020-03-03 18:37:38 +01:00
|
|
|
ast.ParExpr {
|
|
|
|
g.write('(')
|
|
|
|
g.expr(it.expr)
|
|
|
|
g.write(')')
|
|
|
|
}
|
|
|
|
ast.PostfixExpr {
|
|
|
|
g.expr(it.expr)
|
|
|
|
g.write(it.op.str())
|
|
|
|
}
|
|
|
|
ast.PrefixExpr {
|
2020-03-10 23:21:26 +01:00
|
|
|
if it.op == .amp {
|
|
|
|
g.is_amp = true
|
|
|
|
}
|
2020-03-06 20:25:38 +01:00
|
|
|
// g.write('/*pref*/')
|
2020-03-10 23:21:26 +01:00
|
|
|
g.write(it.op.str())
|
2020-03-03 18:37:38 +01:00
|
|
|
g.expr(it.right)
|
2020-03-10 23:21:26 +01:00
|
|
|
g.is_amp = false
|
2020-03-03 18:37:38 +01:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
ast.UnaryExpr {
|
|
|
|
// probably not :D
|
|
|
|
if it.op in [.inc, .dec] {
|
|
|
|
g.expr(it.left)
|
|
|
|
g.write(it.op.str())
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.write(it.op.str())
|
|
|
|
g.expr(it.left)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
ast.SizeOf {
|
2020-03-18 08:41:49 +01:00
|
|
|
styp := g.typ(it.typ)
|
|
|
|
g.write('sizeof($styp)')
|
2020-03-03 18:37:38 +01:00
|
|
|
}
|
|
|
|
ast.StringLiteral {
|
2020-03-08 22:11:56 +01:00
|
|
|
// In C calls we have to generate C strings
|
|
|
|
// `C.printf("hi")` => `printf("hi");`
|
2020-03-12 11:13:46 +01:00
|
|
|
escaped_val := it.val.replace_each(['"', '\\"',
|
2020-03-12 14:21:43 +01:00
|
|
|
'\r\n', '\\n',
|
|
|
|
'\n', '\\n'])
|
2020-03-07 01:05:19 +01:00
|
|
|
if g.is_c_call {
|
2020-03-12 11:13:46 +01:00
|
|
|
g.write('"$escaped_val"')
|
2020-03-07 01:05:19 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-03-12 11:13:46 +01:00
|
|
|
g.write('tos3("$escaped_val")')
|
2020-03-07 01:05:19 +01:00
|
|
|
}
|
2020-03-03 18:37:38 +01:00
|
|
|
}
|
|
|
|
// `user := User{name: 'Bob'}`
|
|
|
|
ast.StructInit {
|
2020-03-06 22:36:51 +01:00
|
|
|
styp := g.typ(it.typ)
|
2020-03-10 23:21:26 +01:00
|
|
|
if g.is_amp {
|
|
|
|
g.out.go_back(1) // delete the & already generated in `prefix_expr()
|
|
|
|
g.write('($styp*)memdup(&($styp){')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.writeln('($styp){')
|
|
|
|
}
|
2020-03-03 18:37:38 +01:00
|
|
|
for i, field in it.fields {
|
|
|
|
g.write('\t.$field = ')
|
|
|
|
g.expr(it.exprs[i])
|
|
|
|
g.writeln(', ')
|
|
|
|
}
|
2020-03-13 01:43:30 +01:00
|
|
|
if it.fields.len == 0 {
|
|
|
|
g.write('0')
|
|
|
|
}
|
2020-03-03 18:37:38 +01:00
|
|
|
g.write('}')
|
2020-03-10 23:21:26 +01:00
|
|
|
if g.is_amp {
|
|
|
|
g.write(', sizeof($styp))')
|
|
|
|
}
|
2020-03-03 18:37:38 +01:00
|
|
|
}
|
|
|
|
ast.SelectorExpr {
|
|
|
|
g.expr(it.expr)
|
2020-03-08 19:38:27 +01:00
|
|
|
// if table.type_nr_muls(it.expr_type) > 0 {
|
|
|
|
if table.type_is_ptr(it.expr_type) {
|
2020-03-07 04:45:35 +01:00
|
|
|
g.write('->')
|
|
|
|
}
|
|
|
|
else {
|
2020-03-11 05:25:11 +01:00
|
|
|
// g.write('. /*typ= $it.expr_type */') // ${g.typ(it.expr_type)} /')
|
2020-03-07 04:45:35 +01:00
|
|
|
g.write('.')
|
|
|
|
}
|
2020-03-11 05:25:11 +01:00
|
|
|
if it.expr_type == 0 {
|
|
|
|
verror('cgen: SelectorExpr typ=0 field=$it.field')
|
|
|
|
}
|
2020-03-03 18:37:38 +01:00
|
|
|
g.write(it.field)
|
|
|
|
}
|
2020-03-04 15:48:43 +01:00
|
|
|
ast.Type {
|
2020-03-16 03:19:26 +01:00
|
|
|
// match sum Type
|
|
|
|
// g.write('/* Type */')
|
|
|
|
g.write('_type_idx_')
|
|
|
|
g.write(g.typ(it.typ))
|
2020-03-04 15:48:43 +01:00
|
|
|
}
|
2019-12-24 18:54:43 +01:00
|
|
|
else {
|
2020-03-03 18:37:38 +01:00
|
|
|
// #printf("node=%d\n", node.typ);
|
|
|
|
println(term.red('cgen.expr(): bad node ' + typeof(node)))
|
2019-12-24 18:54:43 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-26 11:27:35 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 10:07:42 +01:00
|
|
|
fn (g mut Gen) infix_expr(node ast.InfixExpr) {
|
2020-03-13 01:43:30 +01:00
|
|
|
// g.write('/*infix*/')
|
2020-03-07 16:39:15 +01:00
|
|
|
// if it.left_type == table.string_type_idx {
|
2020-03-12 10:07:42 +01:00
|
|
|
// g.write('/*$node.left_type str*/')
|
2020-03-07 16:39:15 +01:00
|
|
|
// }
|
|
|
|
// string + string, string == string etc
|
2020-03-15 01:36:17 +01:00
|
|
|
if node.left_type == table.string_type_idx && node.op != .key_in {
|
2020-03-12 10:07:42 +01:00
|
|
|
fn_name := match node.op {
|
2020-03-07 16:39:15 +01:00
|
|
|
.plus{
|
|
|
|
'string_add('
|
|
|
|
}
|
|
|
|
.eq{
|
|
|
|
'string_eq('
|
|
|
|
}
|
|
|
|
.ne{
|
|
|
|
'string_ne('
|
|
|
|
}
|
2020-03-11 02:28:19 +01:00
|
|
|
.lt{
|
|
|
|
'string_lt('
|
|
|
|
}
|
|
|
|
.le{
|
|
|
|
'string_le('
|
|
|
|
}
|
|
|
|
.gt{
|
|
|
|
'string_gt('
|
|
|
|
}
|
|
|
|
.ge{
|
|
|
|
'string_ge('
|
|
|
|
}
|
2020-03-07 16:39:15 +01:00
|
|
|
else {
|
2020-03-12 10:07:42 +01:00
|
|
|
'/*node error*/'}
|
2020-03-07 16:39:15 +01:00
|
|
|
}
|
|
|
|
g.write(fn_name)
|
2020-03-12 10:07:42 +01:00
|
|
|
g.expr(node.left)
|
2020-03-07 16:39:15 +01:00
|
|
|
g.write(', ')
|
2020-03-12 10:07:42 +01:00
|
|
|
g.expr(node.right)
|
2020-03-07 16:39:15 +01:00
|
|
|
g.write(')')
|
|
|
|
}
|
2020-03-12 10:07:42 +01:00
|
|
|
else if node.op == .key_in {
|
|
|
|
styp := g.typ(node.left_type)
|
2020-03-10 23:21:26 +01:00
|
|
|
g.write('_IN($styp, ')
|
2020-03-12 10:07:42 +01:00
|
|
|
g.expr(node.left)
|
2020-03-10 23:21:26 +01:00
|
|
|
g.write(', ')
|
2020-03-12 10:07:42 +01:00
|
|
|
g.expr(node.right)
|
2020-03-10 23:21:26 +01:00
|
|
|
g.write(')')
|
|
|
|
}
|
2020-03-07 16:39:15 +01:00
|
|
|
// arr << val
|
2020-03-12 10:07:42 +01:00
|
|
|
else if node.op == .left_shift && g.table.get_type_symbol(node.left_type).kind == .array {
|
2020-03-10 23:21:26 +01:00
|
|
|
tmp := g.new_tmp_var()
|
2020-03-15 00:46:08 +01:00
|
|
|
sym := g.table.get_type_symbol(node.left_type)
|
|
|
|
right_sym := g.table.get_type_symbol(node.right_type)
|
|
|
|
if right_sym.kind == .array {
|
|
|
|
// push an array => PUSH_MANY
|
|
|
|
g.write('_PUSH_MANY(&')
|
2020-03-17 15:13:55 +01:00
|
|
|
g.expr_with_cast(node.right_type, node.left_type, node.left)
|
2020-03-15 00:46:08 +01:00
|
|
|
g.write(', (')
|
|
|
|
g.expr(node.right)
|
|
|
|
styp := g.typ(node.left_type)
|
|
|
|
g.write('), $tmp, $styp)')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// push a single element
|
|
|
|
info := sym.info as table.Array
|
|
|
|
elem_type_str := g.typ(info.elem_type)
|
|
|
|
// g.write('array_push(&')
|
|
|
|
g.write('_PUSH(&')
|
2020-03-17 15:13:55 +01:00
|
|
|
g.expr_with_cast(node.right_type, info.elem_type, node.left)
|
2020-03-15 00:46:08 +01:00
|
|
|
g.write(', (')
|
|
|
|
g.expr(node.right)
|
|
|
|
g.write('), $tmp, $elem_type_str)')
|
|
|
|
}
|
2020-03-07 16:39:15 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-03-12 10:07:42 +01:00
|
|
|
// if node.op == .dot {
|
2020-03-07 16:39:15 +01:00
|
|
|
// println('!! dot')
|
|
|
|
// }
|
2020-03-12 10:07:42 +01:00
|
|
|
g.expr(node.left)
|
|
|
|
g.write(' $node.op.str() ')
|
|
|
|
g.expr(node.right)
|
2020-03-07 16:39:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 03:56:38 +01:00
|
|
|
fn (g mut Gen) match_expr(node ast.MatchExpr) {
|
|
|
|
// println('match expr typ=$it.expr_type')
|
|
|
|
// TODO
|
2020-03-18 12:23:32 +01:00
|
|
|
if node.cond_type == 0 {
|
2020-03-16 03:56:38 +01:00
|
|
|
g.writeln('// match 0')
|
|
|
|
return
|
|
|
|
}
|
2020-03-18 12:18:48 +01:00
|
|
|
is_expr := node.is_expr && node.return_type != table.void_type
|
2020-03-18 08:41:49 +01:00
|
|
|
if is_expr {
|
|
|
|
g.inside_ternary = true
|
2020-03-18 12:34:27 +01:00
|
|
|
// g.write('/* EM ret type=${g.typ(node.return_type)} expected_type=${g.typ(node.expected_type)} */')
|
2020-03-18 08:41:49 +01:00
|
|
|
}
|
2020-03-18 12:23:32 +01:00
|
|
|
type_sym := g.table.get_type_symbol(node.cond_type)
|
2020-03-16 03:56:38 +01:00
|
|
|
mut tmp := ''
|
|
|
|
if type_sym.kind != .void {
|
|
|
|
tmp = g.new_tmp_var()
|
|
|
|
}
|
2020-03-18 08:41:49 +01:00
|
|
|
// styp := g.typ(node.expr_type)
|
|
|
|
// g.write('$styp $tmp = ')
|
|
|
|
// g.expr(node.cond)
|
|
|
|
// g.writeln(';') // $it.blocks.len')
|
2020-03-16 03:56:38 +01:00
|
|
|
// mut sum_type_str = ''
|
|
|
|
for j, branch in node.branches {
|
|
|
|
if j == node.branches.len - 1 {
|
|
|
|
// last block is an `else{}`
|
2020-03-18 08:41:49 +01:00
|
|
|
if is_expr {
|
|
|
|
// TODO too many branches. maybe separate ?: matches
|
|
|
|
g.write(' : ')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.writeln('else {')
|
|
|
|
}
|
2020-03-16 03:56:38 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if j > 0 {
|
2020-03-18 08:41:49 +01:00
|
|
|
if is_expr {
|
|
|
|
g.write(' : ')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.write('else ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_expr {
|
|
|
|
g.write('(')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.write('if (')
|
2020-03-16 03:56:38 +01:00
|
|
|
}
|
|
|
|
for i, expr in branch.exprs {
|
|
|
|
if node.is_sum_type {
|
2020-03-18 08:41:49 +01:00
|
|
|
g.expr(node.cond)
|
2020-03-16 10:53:28 +01:00
|
|
|
g.write('.typ == ')
|
2020-03-18 08:41:49 +01:00
|
|
|
// g.write('${tmp}.typ == ')
|
2020-03-16 03:56:38 +01:00
|
|
|
// sum_type_str
|
|
|
|
}
|
|
|
|
else if type_sym.kind == .string {
|
2020-03-16 10:53:28 +01:00
|
|
|
g.write('string_eq(')
|
|
|
|
//
|
2020-03-18 08:41:49 +01:00
|
|
|
g.expr(node.cond)
|
|
|
|
g.write(', ')
|
|
|
|
// g.write('string_eq($tmp, ')
|
2020-03-16 03:56:38 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-03-16 10:53:28 +01:00
|
|
|
g.expr(node.cond)
|
|
|
|
g.write(' == ')
|
2020-03-18 08:41:49 +01:00
|
|
|
// g.write('$tmp == ')
|
2020-03-16 03:56:38 +01:00
|
|
|
}
|
|
|
|
g.expr(expr)
|
|
|
|
if type_sym.kind == .string {
|
|
|
|
g.write(')')
|
|
|
|
}
|
|
|
|
if i < branch.exprs.len - 1 {
|
|
|
|
g.write(' || ')
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 08:41:49 +01:00
|
|
|
if is_expr {
|
|
|
|
g.write(') ? ')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.writeln(') {')
|
|
|
|
}
|
2020-03-16 03:56:38 +01:00
|
|
|
}
|
2020-03-18 16:07:52 +01:00
|
|
|
// g.writeln('/* M sum_type=$node.is_sum_type is_expr=$node.is_expr exp_type=${g.typ(node.expected_type)}*/')
|
|
|
|
if node.is_sum_type && branch.exprs.len > 0 && !node.is_expr {
|
2020-03-16 03:56:38 +01:00
|
|
|
// The first node in expr is an ast.Type
|
|
|
|
// Use it to generate `it` variable.
|
2020-03-18 16:07:52 +01:00
|
|
|
first_expr := branch.exprs[0]
|
|
|
|
match first_expr {
|
2020-03-16 03:56:38 +01:00
|
|
|
ast.Type {
|
|
|
|
it_type := g.typ(it.typ)
|
2020-03-18 08:41:49 +01:00
|
|
|
// g.writeln('$it_type* it = ($it_type*)${tmp}.obj; // ST it')
|
|
|
|
g.write('$it_type* it = ($it_type*)')
|
|
|
|
g.expr(node.cond)
|
|
|
|
g.writeln('.obj; // ST it')
|
2020-03-16 03:56:38 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
verror('match sum type')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.stmts(branch.stmts)
|
2020-03-18 08:41:49 +01:00
|
|
|
if !g.inside_ternary {
|
|
|
|
g.writeln('}')
|
|
|
|
}
|
2020-03-16 03:56:38 +01:00
|
|
|
}
|
2020-03-18 08:41:49 +01:00
|
|
|
g.inside_ternary = false
|
2020-03-16 03:56:38 +01:00
|
|
|
}
|
|
|
|
|
2020-03-13 05:57:51 +01:00
|
|
|
fn (g mut Gen) ident(node ast.Ident) {
|
|
|
|
name := node.name.replace('.', '__')
|
|
|
|
if name.starts_with('C__') {
|
|
|
|
g.write(name[3..])
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO `is`
|
|
|
|
match node.info {
|
|
|
|
ast.IdentVar {
|
|
|
|
if it.is_optional {
|
2020-03-13 12:22:36 +01:00
|
|
|
g.write('/*opt*/')
|
2020-03-13 05:57:51 +01:00
|
|
|
styp := g.typ(it.typ)[7..] // Option_int => int TODO perf?
|
|
|
|
g.write('(*($styp*)${name}.data)')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
g.write(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 16:40:41 +01:00
|
|
|
fn (g mut Gen) if_expr(node ast.IfExpr) {
|
|
|
|
// If expression? Assign the value to a temp var.
|
|
|
|
// Previously ?: was used, but it's too unreliable.
|
|
|
|
type_sym := g.table.get_type_symbol(node.typ)
|
|
|
|
mut tmp := ''
|
|
|
|
if type_sym.kind != .void {
|
|
|
|
tmp = g.new_tmp_var()
|
|
|
|
// g.writeln('$ti.name $tmp;')
|
|
|
|
}
|
|
|
|
// one line ?:
|
|
|
|
// TODO clean this up once `is` is supported
|
2020-03-18 08:41:49 +01:00
|
|
|
if node.is_expr && node.stmts.len == 1 && node.else_stmts.len == 1 && type_sym.kind != .void {
|
2020-03-17 16:40:41 +01:00
|
|
|
cond := node.cond
|
|
|
|
stmt1 := node.stmts[0]
|
|
|
|
else_stmt1 := node.else_stmts[0]
|
|
|
|
match stmt1 {
|
|
|
|
ast.ExprStmt {
|
2020-03-18 08:41:49 +01:00
|
|
|
g.inside_ternary = true
|
2020-03-17 16:40:41 +01:00
|
|
|
g.expr(cond)
|
|
|
|
g.write(' ? ')
|
|
|
|
expr_stmt := stmt1 as ast.ExprStmt
|
|
|
|
g.expr(expr_stmt.expr)
|
|
|
|
g.write(' : ')
|
|
|
|
g.stmt(else_stmt1)
|
|
|
|
}
|
|
|
|
else {}
|
2020-03-18 08:41:49 +01:00
|
|
|
}
|
2020-03-17 16:40:41 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mut is_guard := false
|
2020-03-17 18:01:36 +01:00
|
|
|
guard_ok := g.new_tmp_var()
|
2020-03-17 16:40:41 +01:00
|
|
|
match node.cond {
|
|
|
|
ast.IfGuardExpr {
|
|
|
|
is_guard = true
|
2020-03-17 18:01:36 +01:00
|
|
|
g.writeln('bool $guard_ok;')
|
|
|
|
g.write('{ /* if guard */ ${g.typ(it.expr_type)} $it.var_name = ')
|
2020-03-17 16:40:41 +01:00
|
|
|
g.expr(it.expr)
|
|
|
|
g.writeln(';')
|
2020-03-17 18:01:36 +01:00
|
|
|
g.writeln('if (($guard_ok = ${it.var_name}.ok)) {')
|
2020-03-17 16:40:41 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-03-18 08:41:49 +01:00
|
|
|
g.inside_ternary = false
|
2020-03-17 16:40:41 +01:00
|
|
|
g.write('if (')
|
|
|
|
g.expr(node.cond)
|
|
|
|
g.writeln(') {')
|
|
|
|
}
|
2020-03-18 08:41:49 +01:00
|
|
|
}
|
2020-03-17 16:40:41 +01:00
|
|
|
for i, stmt in node.stmts {
|
|
|
|
// Assign ret value
|
|
|
|
if i == node.stmts.len - 1 && type_sym.kind != .void {}
|
|
|
|
// g.writeln('$tmp =')
|
|
|
|
g.stmt(stmt)
|
|
|
|
}
|
|
|
|
if is_guard {
|
|
|
|
g.write('}')
|
|
|
|
}
|
|
|
|
g.writeln('}')
|
|
|
|
if node.else_stmts.len > 0 {
|
2020-03-17 18:01:36 +01:00
|
|
|
if is_guard {
|
2020-03-18 08:41:49 +01:00
|
|
|
g.writeln('if (!$guard_ok) { /* else */')
|
|
|
|
}
|
|
|
|
else {
|
2020-03-17 18:01:36 +01:00
|
|
|
g.writeln('else { ')
|
|
|
|
}
|
2020-03-18 12:18:48 +01:00
|
|
|
g.stmts(node.else_stmts)
|
2020-03-17 16:40:41 +01:00
|
|
|
g.writeln('}')
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 08:41:49 +01:00
|
|
|
g.inside_ternary = false
|
2020-03-17 16:40:41 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:31:54 +01:00
|
|
|
fn (g mut Gen) index_expr(node ast.IndexExpr) {
|
|
|
|
// TODO else doesn't work with sum types
|
|
|
|
mut is_range := false
|
|
|
|
match node.index {
|
|
|
|
ast.RangeExpr {
|
2020-03-07 05:19:15 +01:00
|
|
|
// TODO should never be 0
|
|
|
|
if node.container_type != 0 {
|
|
|
|
sym := g.table.get_type_symbol(node.container_type)
|
|
|
|
is_range = true
|
|
|
|
if sym.kind == .string {
|
|
|
|
g.write('string_substr(')
|
|
|
|
}
|
|
|
|
else if sym.kind == .array {
|
|
|
|
g.write('array_slice(')
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 14:31:54 +01:00
|
|
|
g.expr(node.left)
|
|
|
|
g.write(', ')
|
2020-03-06 22:24:39 +01:00
|
|
|
if it.has_low {
|
|
|
|
g.expr(it.low)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.write('0')
|
|
|
|
}
|
2020-02-02 14:31:54 +01:00
|
|
|
g.write(', ')
|
2020-03-06 22:24:39 +01:00
|
|
|
if it.has_high {
|
|
|
|
g.expr(it.high)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.expr(node.left)
|
|
|
|
g.write('.len')
|
|
|
|
}
|
2020-02-02 14:31:54 +01:00
|
|
|
g.write(')')
|
2020-03-06 22:24:39 +01:00
|
|
|
return
|
2020-02-02 14:31:54 +01:00
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
2020-03-06 17:05:58 +01:00
|
|
|
// if !is_range && node.container_type == 0 {
|
|
|
|
// }
|
|
|
|
if !is_range && node.container_type != 0 {
|
|
|
|
sym := g.table.get_type_symbol(node.container_type)
|
2020-03-14 13:42:27 +01:00
|
|
|
if table.type_is_variadic(node.container_type) {
|
|
|
|
g.expr(node.left)
|
|
|
|
g.write('.args')
|
|
|
|
g.write('[')
|
|
|
|
g.expr(node.index)
|
|
|
|
g.write(']')
|
|
|
|
}
|
|
|
|
else if sym.kind == .array {
|
2020-03-11 21:11:27 +01:00
|
|
|
info := sym.info as table.Array
|
|
|
|
elem_type_str := g.typ(info.elem_type)
|
2020-03-07 16:39:15 +01:00
|
|
|
if g.is_assign_expr {
|
|
|
|
g.is_array_set = true
|
|
|
|
g.write('array_set(&')
|
|
|
|
g.expr(node.left)
|
|
|
|
g.write(', ')
|
|
|
|
g.expr(node.index)
|
2020-03-11 21:11:27 +01:00
|
|
|
g.write(', &($elem_type_str[]) { ')
|
2020-03-07 16:39:15 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-03-11 21:11:27 +01:00
|
|
|
g.write('(*($elem_type_str*)array_get(')
|
2020-03-07 16:39:15 +01:00
|
|
|
g.expr(node.left)
|
|
|
|
g.write(', ')
|
|
|
|
g.expr(node.index)
|
2020-03-11 03:52:01 +01:00
|
|
|
g.write('))')
|
2020-03-07 16:39:15 +01:00
|
|
|
}
|
2020-03-06 17:05:58 +01:00
|
|
|
}
|
2020-03-06 20:22:58 +01:00
|
|
|
else if sym.kind == .string {
|
2020-03-06 22:24:39 +01:00
|
|
|
g.write('string_at(')
|
2020-03-06 20:22:58 +01:00
|
|
|
g.expr(node.left)
|
|
|
|
g.write(', ')
|
|
|
|
g.expr(node.index)
|
|
|
|
g.write(')')
|
|
|
|
}
|
2020-03-07 00:19:27 +01:00
|
|
|
else {
|
|
|
|
g.expr(node.left)
|
|
|
|
g.write('[')
|
|
|
|
g.expr(node.index)
|
|
|
|
g.write(']')
|
|
|
|
}
|
2020-02-02 14:31:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 05:57:51 +01:00
|
|
|
fn (g mut Gen) return_statement(it ast.Return) {
|
|
|
|
g.write('return')
|
2020-03-18 08:41:49 +01:00
|
|
|
if g.fn_decl.name == 'main' {
|
|
|
|
g.writeln(' 0;')
|
|
|
|
return
|
|
|
|
}
|
2020-03-13 05:57:51 +01:00
|
|
|
// multiple returns
|
|
|
|
if it.exprs.len > 1 {
|
2020-03-16 07:42:45 +01:00
|
|
|
typ_sym := g.table.get_type_symbol(g.fn_decl.return_type)
|
|
|
|
mr_info := typ_sym.info as table.MultiReturn
|
2020-03-13 05:57:51 +01:00
|
|
|
styp := g.typ(g.fn_decl.return_type)
|
|
|
|
g.write(' ($styp){')
|
|
|
|
for i, expr in it.exprs {
|
|
|
|
g.write('.arg$i=')
|
|
|
|
g.expr(expr)
|
|
|
|
if i < it.exprs.len - 1 {
|
|
|
|
g.write(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.write('}')
|
|
|
|
}
|
|
|
|
// normal return
|
|
|
|
else if it.exprs.len == 1 {
|
|
|
|
g.write(' ')
|
|
|
|
// `return opt_ok(expr)` for functions that expect an optional
|
|
|
|
if table.type_is_optional(g.fn_decl.return_type) {
|
|
|
|
mut is_none := false
|
|
|
|
mut is_error := false
|
2020-03-18 12:23:32 +01:00
|
|
|
expr0 := it.exprs[0]
|
|
|
|
match expr0 {
|
2020-03-13 05:57:51 +01:00
|
|
|
ast.None {
|
|
|
|
is_none = true
|
|
|
|
}
|
|
|
|
ast.CallExpr {
|
|
|
|
is_error = true // TODO check name 'error'
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
if !is_none && !is_error {
|
|
|
|
styp := g.typ(g.fn_decl.return_type)[7..] // remove 'Option_'
|
2020-03-13 12:22:36 +01:00
|
|
|
g.write('opt_ok(& ($styp []) { ')
|
|
|
|
g.expr(it.exprs[0])
|
|
|
|
g.writeln(' }, sizeof($styp));')
|
2020-03-13 05:57:51 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// g.write('/*OPTIONAL*/')
|
|
|
|
}
|
2020-03-18 16:07:52 +01:00
|
|
|
if !table.type_is_ptr(g.fn_decl.return_type) && table.type_is_ptr(it.types[0]) {
|
|
|
|
// Automatic Dereference
|
|
|
|
g.write('*')
|
|
|
|
}
|
2020-03-17 15:13:55 +01:00
|
|
|
g.expr_with_cast(it.types[0], g.fn_decl.return_type, it.exprs[0])
|
2020-03-13 05:57:51 +01:00
|
|
|
}
|
|
|
|
g.writeln(';')
|
|
|
|
}
|
|
|
|
|
2020-03-06 14:03:35 +01:00
|
|
|
fn (g mut Gen) const_decl(node ast.ConstDecl) {
|
|
|
|
for i, field in node.fields {
|
|
|
|
name := field.name.replace('.', '__')
|
|
|
|
expr := node.exprs[i]
|
|
|
|
match expr {
|
|
|
|
// Simple expressions should use a #define
|
|
|
|
// so that we don't pollute the binary with unnecessary global vars
|
|
|
|
// Do not do this when building a module, otherwise the consts
|
|
|
|
// will not be accessible.
|
|
|
|
ast.CharLiteral, ast.IntegerLiteral {
|
2020-03-11 02:44:30 +01:00
|
|
|
g.definitions.write('#define $name ')
|
|
|
|
// TODO hack. Cut the generated value and paste it into definitions.
|
|
|
|
g.write('//')
|
|
|
|
pos := g.out.len
|
2020-03-06 14:03:35 +01:00
|
|
|
g.expr(expr)
|
2020-03-15 01:39:29 +01:00
|
|
|
mut b := g.out.buf[pos..g.out.buf.len].clone()
|
|
|
|
b << `\0`
|
2020-03-14 05:20:12 +01:00
|
|
|
val := string(b)
|
|
|
|
// val += '\0'
|
2020-03-11 02:44:30 +01:00
|
|
|
// g.out.go_back(val.len)
|
2020-03-14 05:20:12 +01:00
|
|
|
// println('pos=$pos buf.len=$g.out.buf.len len=$g.out.len val.len=$val.len val="$val"\n')
|
2020-03-15 01:39:29 +01:00
|
|
|
g.writeln('')
|
|
|
|
g.definitions.writeln(val)
|
2020-03-06 14:03:35 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-03-06 22:36:51 +01:00
|
|
|
styp := g.typ(field.typ)
|
2020-03-06 16:31:40 +01:00
|
|
|
g.definitions.writeln('$styp $name; // inited later') // = ')
|
2020-03-06 14:03:35 +01:00
|
|
|
// TODO
|
|
|
|
// g.expr(node.exprs[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 11:11:43 +01:00
|
|
|
fn (g mut Gen) call_args(args []ast.CallArg) {
|
|
|
|
for i, arg in args {
|
2020-03-14 13:15:07 +01:00
|
|
|
if table.type_is_variadic(arg.expected_type) {
|
|
|
|
struct_name := 'varg_' + g.typ(arg.expected_type).replace('*', '_ptr')
|
2020-03-15 00:46:08 +01:00
|
|
|
len := args.len - i
|
2020-03-14 13:42:27 +01:00
|
|
|
type_str := int(arg.expected_type).str()
|
|
|
|
if len > g.varaidic_args[type_str] {
|
|
|
|
g.varaidic_args[type_str] = len
|
|
|
|
}
|
2020-03-14 13:15:07 +01:00
|
|
|
g.write('($struct_name){.len=$len,.args={')
|
2020-03-15 00:46:08 +01:00
|
|
|
for j in i .. args.len {
|
2020-03-14 13:15:07 +01:00
|
|
|
g.ref_or_deref_arg(args[j])
|
|
|
|
g.expr(args[j].expr)
|
2020-03-15 00:46:08 +01:00
|
|
|
if j < args.len - 1 {
|
2020-03-14 13:15:07 +01:00
|
|
|
g.write(', ')
|
|
|
|
}
|
2020-03-12 09:11:41 +01:00
|
|
|
}
|
2020-03-14 13:15:07 +01:00
|
|
|
g.write('}}')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if arg.expected_type != 0 {
|
|
|
|
g.ref_or_deref_arg(arg)
|
2020-03-10 23:21:26 +01:00
|
|
|
}
|
2020-03-14 11:11:43 +01:00
|
|
|
g.expr(arg.expr)
|
2020-03-01 13:07:51 +01:00
|
|
|
if i != args.len - 1 {
|
|
|
|
g.write(', ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 13:15:07 +01:00
|
|
|
[inline]
|
|
|
|
fn (g mut Gen) ref_or_deref_arg(arg ast.CallArg) {
|
|
|
|
arg_is_ptr := table.type_is_ptr(arg.expected_type) || arg.expected_type == table.voidptr_type_idx
|
|
|
|
expr_is_ptr := table.type_is_ptr(arg.typ)
|
|
|
|
if arg.is_mut && !arg_is_ptr {
|
|
|
|
g.write('&/*mut*/')
|
|
|
|
}
|
|
|
|
else if arg_is_ptr && !expr_is_ptr {
|
|
|
|
g.write('&/*q*/')
|
|
|
|
}
|
|
|
|
else if !arg_is_ptr && expr_is_ptr {
|
|
|
|
// Dereference a pointer if a value is required
|
|
|
|
g.write('*/*d*/')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:27:35 +01:00
|
|
|
fn verror(s string) {
|
2020-03-04 15:48:43 +01:00
|
|
|
println('cgen error: $s')
|
|
|
|
// exit(1)
|
2019-12-24 18:54:43 +01:00
|
|
|
}
|
2020-03-10 23:21:26 +01:00
|
|
|
|
|
|
|
const (
|
2020-03-11 21:11:27 +01:00
|
|
|
// TODO all builtin types must be lowercase
|
2020-03-12 20:26:29 +01:00
|
|
|
builtins = ['string', 'array', 'KeyValue', 'DenseArray', 'map', 'Option']
|
2020-03-10 23:21:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
fn (g mut Gen) write_builtin_types() {
|
2020-03-05 23:27:21 +01:00
|
|
|
mut builtin_types := []table.TypeSymbol // builtin types
|
|
|
|
// builtin types need to be on top
|
|
|
|
// everything except builtin will get sorted
|
2020-03-06 16:57:27 +01:00
|
|
|
for builtin_name in builtins {
|
|
|
|
builtin_types << g.table.types[g.table.type_idxs[builtin_name]]
|
|
|
|
}
|
2020-03-10 23:21:26 +01:00
|
|
|
g.write_types(builtin_types)
|
|
|
|
}
|
|
|
|
|
|
|
|
// C struct definitions, ordered
|
|
|
|
// Sort the types, make sure types that are referenced by other types
|
|
|
|
// are added before them.
|
|
|
|
fn (g mut Gen) write_sorted_types() {
|
|
|
|
mut types := []table.TypeSymbol // structs that need to be sorted
|
2020-03-06 12:01:56 +01:00
|
|
|
for typ in g.table.types {
|
2020-03-06 16:57:27 +01:00
|
|
|
if !(typ.name in builtins) {
|
|
|
|
types << typ
|
2020-03-05 23:27:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// sort structs
|
|
|
|
types_sorted := g.sort_structs(types)
|
|
|
|
// Generate C code
|
|
|
|
g.definitions.writeln('// builtin types:')
|
2020-03-10 23:21:26 +01:00
|
|
|
// g.write_types(builtin_types)
|
2020-03-06 20:22:58 +01:00
|
|
|
g.definitions.writeln('//------------------ #endbuiltin')
|
2020-03-05 23:27:21 +01:00
|
|
|
g.write_types(types_sorted)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (g mut Gen) write_types(types []table.TypeSymbol) {
|
2020-03-16 03:19:26 +01:00
|
|
|
for i, typ in types {
|
2020-03-13 01:43:30 +01:00
|
|
|
if typ.name.starts_with('C.') {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-05 23:27:21 +01:00
|
|
|
// sym := g.table.get_type_symbol(typ)
|
2020-03-14 05:20:12 +01:00
|
|
|
name := typ.name.replace('.', '__')
|
2020-03-05 23:27:21 +01:00
|
|
|
match typ.info {
|
|
|
|
table.Struct {
|
|
|
|
info := typ.info as table.Struct
|
|
|
|
// g.definitions.writeln('typedef struct {')
|
|
|
|
g.definitions.writeln('struct $name {')
|
|
|
|
for field in info.fields {
|
2020-03-07 01:05:19 +01:00
|
|
|
type_name := g.typ(field.typ)
|
2020-03-05 23:27:21 +01:00
|
|
|
g.definitions.writeln('\t$type_name $field.name;')
|
|
|
|
}
|
|
|
|
// g.definitions.writeln('} $name;\n')
|
|
|
|
//
|
|
|
|
g.definitions.writeln('};\n')
|
2020-03-16 03:19:26 +01:00
|
|
|
g.typedefs.writeln('#define _type_idx_$name $i')
|
2020-03-05 23:27:21 +01:00
|
|
|
}
|
2020-03-14 05:20:12 +01:00
|
|
|
table.Enum {
|
|
|
|
g.definitions.writeln('typedef enum {')
|
2020-03-16 03:19:26 +01:00
|
|
|
for j, val in it.vals {
|
|
|
|
g.definitions.writeln('\t${name}_$val, // $j')
|
2020-03-14 05:20:12 +01:00
|
|
|
}
|
|
|
|
g.definitions.writeln('} $name;\n')
|
|
|
|
}
|
|
|
|
table.SumType {
|
|
|
|
g.definitions.writeln('// Sum type')
|
|
|
|
g.definitions.writeln('
|
|
|
|
typedef struct {
|
|
|
|
void* obj;
|
|
|
|
int typ;
|
|
|
|
} $name;')
|
|
|
|
}
|
2020-03-05 23:27:21 +01:00
|
|
|
else {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort structs by dependant fields
|
|
|
|
fn (g &Gen) sort_structs(types []table.TypeSymbol) []table.TypeSymbol {
|
|
|
|
mut dep_graph := depgraph.new_dep_graph()
|
|
|
|
// types name list
|
|
|
|
mut type_names := []string
|
|
|
|
for typ in types {
|
|
|
|
type_names << typ.name
|
|
|
|
}
|
|
|
|
// loop over types
|
|
|
|
for t in types {
|
2020-03-06 12:01:56 +01:00
|
|
|
// create list of deps
|
|
|
|
mut field_deps := []string
|
2020-03-05 23:27:21 +01:00
|
|
|
match t.info {
|
|
|
|
table.Struct {
|
|
|
|
info := t.info as table.Struct
|
|
|
|
for field in info.fields {
|
|
|
|
// Need to handle fixed size arrays as well (`[10]Point`)
|
|
|
|
// ft := if field.typ.starts_with('[') { field.typ.all_after(']') } else { field.typ }
|
2020-03-06 12:01:56 +01:00
|
|
|
dep := g.table.get_type_symbol(field.typ).name
|
2020-03-05 23:27:21 +01:00
|
|
|
// skip if not in types list or already in deps
|
2020-03-06 20:22:58 +01:00
|
|
|
if !(dep in type_names) || dep in field_deps || table.type_is_ptr(field.typ) {
|
2020-03-06 12:01:56 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
field_deps << dep
|
2020-03-05 23:27:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {}
|
2020-03-06 14:03:35 +01:00
|
|
|
}
|
2020-03-06 12:01:56 +01:00
|
|
|
// add type and dependant types to graph
|
|
|
|
dep_graph.add(t.name, field_deps)
|
2020-03-05 23:27:21 +01:00
|
|
|
}
|
|
|
|
// sort graph
|
|
|
|
dep_graph_sorted := dep_graph.resolve()
|
|
|
|
if !dep_graph_sorted.acyclic {
|
|
|
|
verror('cgen.sort_structs(): the following structs form a dependency cycle:\n' + dep_graph_sorted.display_cycles() + '\nyou can solve this by making one or both of the dependant struct fields references, eg: field &MyStruct' + '\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
|
|
|
|
}
|
|
|
|
// sort types
|
|
|
|
mut types_sorted := []table.TypeSymbol
|
|
|
|
for node in dep_graph_sorted.nodes {
|
2020-03-06 16:57:27 +01:00
|
|
|
types_sorted << g.table.types[g.table.type_idxs[node.name]]
|
2020-03-05 23:27:21 +01:00
|
|
|
}
|
|
|
|
return types_sorted
|
|
|
|
}
|
2020-03-10 14:40:30 +01:00
|
|
|
|
|
|
|
fn op_to_fn_name(name string) string {
|
|
|
|
return match name {
|
|
|
|
'+'{
|
|
|
|
'_op_plus'
|
|
|
|
}
|
|
|
|
'-'{
|
|
|
|
'_op_minus'
|
|
|
|
}
|
|
|
|
'*'{
|
|
|
|
'_op_mul'
|
|
|
|
}
|
|
|
|
'/'{
|
|
|
|
'_op_div'
|
|
|
|
}
|
|
|
|
'%'{
|
|
|
|
'_op_mod'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
'bad op $name'}
|
|
|
|
}
|
|
|
|
}
|