v/vlib/v/gen/cgen.v

414 lines
8.1 KiB
V
Raw Normal View History

2019-12-30 08:30:24 +01:00
module gen
2019-12-24 18:54:43 +01:00
import (
strings
v.ast
v.table
term
2019-12-24 18:54:43 +01:00
)
struct Gen {
2020-01-02 08:30:15 +01:00
out strings.Builder
definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file)
table &table.Table
mut:
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
tmp_count int
2019-12-24 18:54:43 +01:00
}
pub fn cgen(files []ast.File, table &table.Table) string {
2020-01-06 16:13:12 +01:00
println('start cgen')
2019-12-28 11:02:06 +01:00
mut g := Gen{
out: strings.new_builder(100)
2020-01-02 08:30:15 +01:00
definitions: strings.new_builder(100)
table: table
fn_decl: 0
2019-12-28 11:02:06 +01:00
}
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-01-02 08:30:15 +01:00
return g.definitions.str() + g.out.str()
2019-12-24 18:54:43 +01:00
}
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)
}
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)
g.writeln('')
}
}
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 {
ast.ConstDecl {
for i, field in it.fields {
field_type_sym := g.table.get_type_symbol(field.typ)
g.write('$field_type_sym.name $field.name = ')
g.expr(it.exprs[i])
g.writeln(';')
}
}
2020-02-25 11:52:41 +01:00
ast.EnumDecl {
2020-02-25 13:30:43 +01:00
g.writeln('typedef enum {')
2020-02-25 11:52:41 +01:00
for i, val in it.vals {
g.writeln('\t${it.name}_$val, // $i')
}
2020-02-25 13:30:43 +01:00
g.writeln('} $it.name;')
2020-02-25 11:52:41 +01:00
}
ast.Import {}
2019-12-28 14:11:05 +01:00
ast.FnDecl {
g.reset_tmp_count()
2020-02-03 11:13:36 +01:00
g.fn_decl = it // &it
2020-01-02 08:30:15 +01:00
is_main := it.name == 'main'
if is_main {
2019-12-30 09:23:17 +01:00
g.write('int ${it.name}(')
}
else {
type_sym := g.table.get_type_symbol(it.typ)
2020-03-01 21:56:07 +01:00
name := it.name.replace('.', '__')
g.write('$type_sym.name ${name}(')
g.definitions.write('$type_sym.name ${name}(')
2019-12-30 09:23:17 +01:00
}
for i, arg in it.args {
arg_type_sym := g.table.get_type_symbol(arg.typ)
mut arg_type_name := arg_type_sym.name
2020-02-17 22:50:04 +01:00
if i == it.args.len - 1 && it.is_variadic {
arg_type_name = 'variadic_$arg_type_sym.name'
}
g.write(arg_type_name + ' ' + arg.name)
g.definitions.write(arg_type_name + ' ' + arg.name)
if i < it.args.len - 1 {
g.write(', ')
g.definitions.write(', ')
}
2019-12-29 07:24:17 +01:00
}
g.writeln(') { ')
2020-01-02 08:30:15 +01:00
if !is_main {
g.definitions.writeln(');')
}
2019-12-28 14:11:05 +01:00
for stmt in it.stmts {
g.stmt(stmt)
}
2020-01-02 08:30:15 +01:00
if is_main {
2019-12-30 09:23:17 +01:00
g.writeln('return 0;')
}
2019-12-28 14:11:05 +01:00
g.writeln('}')
g.fn_decl = 0
2019-12-28 14:11:05 +01:00
}
ast.Return {
g.write('return')
// multiple returns
if it.exprs.len > 1 {
type_sym := g.table.get_type_symbol(g.fn_decl.typ)
g.write(' ($type_sym.name){')
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(' ')
g.expr(it.exprs[0])
}
2019-12-28 14:11:05 +01:00
g.writeln(';')
}
2020-02-06 17:38:02 +01:00
ast.AssignStmt {
// ident0 := it.left[0]
// info0 := ident0.var_info()
// for i, ident in it.left {
2020-02-07 14:49:14 +01:00
// info := ident.var_info()
// if info0.typ.typ.kind == .multi_return {
// if i == 0 {
// g.write('$info.typ.typ.name $ident.name = ')
// g.expr(it.right[0])
// } else {
// arg_no := i-1
// g.write('$info.typ.typ.name $ident.name = $ident0.name->arg[$arg_no]')
// }
2020-02-06 17:38:02 +01:00
// }
2020-02-07 14:49:14 +01:00
// g.writeln(';')
// }
println('assign')
2020-02-06 17:38:02 +01:00
}
2019-12-28 14:11:05 +01:00
ast.VarDecl {
type_sym := g.table.get_type_symbol(it.typ)
g.write('$type_sym.name $it.name = ')
2019-12-28 14:11:05 +01:00
g.expr(it.expr)
g.writeln(';')
}
ast.ForStmt {
g.write('while (')
2020-01-07 00:14:19 +01:00
g.expr(it.cond)
g.writeln(') {')
for stmt in it.stmts {
g.stmt(stmt)
2020-01-01 22:34:46 +01:00
}
2020-01-07 00:14:19 +01:00
g.writeln('}')
}
ast.ForCStmt {
g.write('for (')
g.stmt(it.init)
// g.write('; ')
g.expr(it.cond)
g.write('; ')
g.stmt(it.inc)
g.writeln(') {')
for stmt in it.stmts {
g.stmt(stmt)
}
g.writeln('}')
}
ast.StructDecl {
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('} $it.name;')
}
2019-12-28 14:11:05 +01:00
ast.ExprStmt {
g.expr(it.expr)
match it.expr {
// no ; after an if expression
ast.IfExpr {}
else {
g.writeln(';')
}
}
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
}
}
}
fn (g mut Gen) expr(node ast.Expr) {
2019-12-28 11:02:06 +01:00
// println('cgen expr()')
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)
g.writeln('new_array_from_c_array($it.exprs.len, $it.exprs.len, sizeof($type_sym.name), {\t')
for expr in it.exprs {
g.expr(expr)
g.write(', ')
}
g.write('\n})')
}
2020-01-06 16:13:12 +01:00
ast.AssignExpr {
g.expr(it.left)
g.write(' $it.op.str() ')
g.expr(it.val)
}
2020-02-17 22:50:04 +01:00
ast.BoolLiteral {
g.write(it.val.str())
}
2020-02-25 15:02:34 +01:00
ast.EnumVal {
g.write('${it.enum_name}_$it.val')
}
2019-12-26 11:21:41 +01:00
ast.IntegerLiteral {
2019-12-24 18:54:43 +01:00
g.write(it.val.str())
}
2019-12-27 10:03:29 +01:00
ast.FloatLiteral {
g.write(it.val)
}
2020-01-06 16:13:12 +01:00
ast.PostfixExpr {
g.expr(it.expr)
g.write(it.op.str())
}
/*
2019-12-26 10:02:38 +01:00
ast.UnaryExpr {
// probably not :D
if it.op in [.inc, .dec] {
g.expr(it.left)
g.write(it.op.str())
2020-01-01 22:34:46 +01:00
}
else {
g.write(it.op.str())
g.expr(it.left)
}
2019-12-26 10:02:38 +01:00
}
*/
2019-12-24 18:54:43 +01:00
ast.StringLiteral {
2019-12-27 08:52:20 +01:00
g.write('tos3("$it.val")')
2019-12-24 18:54:43 +01:00
}
2020-01-06 16:13:12 +01:00
ast.PrefixExpr {
g.write(it.op.str())
g.expr(it.right)
}
ast.InfixExpr {
g.expr(it.left)
if it.op == .dot {
println('!! dot')
}
g.write(' $it.op.str() ')
g.expr(it.right)
2019-12-28 11:02:06 +01:00
// if typ.name != typ2.name {
// verror('bad types $typ.name $typ2.name')
// }
2019-12-24 18:54:43 +01:00
}
// `user := User{name: 'Bob'}`
ast.StructInit {
type_sym := g.table.get_type_symbol(it.typ)
g.writeln('($type_sym.name){')
for i, field in it.fields {
g.write('\t.$field = ')
g.expr(it.exprs[i])
g.writeln(', ')
}
g.write('}')
}
2019-12-29 07:24:17 +01:00
ast.CallExpr {
2020-03-01 21:56:07 +01:00
name := it.name.replace('.', '__')
g.write('${name}(')
g.call_args(it.args)
g.write(')')
/*
for i, expr in it.args {
g.expr(expr)
if i != it.args.len - 1 {
g.write(', ')
}
}
*/
}
ast.MethodCallExpr {
typ := 'TODO'
2020-03-01 21:56:07 +01:00
name := it.name.replace('.', '__')
g.write('${typ}_${name}(')
g.expr(it.expr)
if it.args.len > 0 {
g.write(', ')
}
g.call_args(it.args)
g.write(')')
2019-12-29 07:24:17 +01:00
}
2019-12-28 14:11:05 +01:00
ast.Ident {
g.write('$it.name')
2019-12-24 18:54:43 +01:00
}
ast.SelectorExpr {
g.expr(it.expr)
g.write('.')
g.write(it.field)
}
2020-01-07 12:14:10 +01:00
ast.IndexExpr {
2020-02-02 14:31:54 +01:00
g.index_expr(it)
2020-01-07 12:14:10 +01:00
}
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(it.typ)
mut tmp := ''
if type_sym.kind != .void {
tmp = g.new_tmp_var()
// g.writeln('$ti.name $tmp;')
}
g.write('if (')
g.expr(it.cond)
g.writeln(') {')
for i, stmt in it.stmts {
// Assign ret value
if i == it.stmts.len - 1 && type_sym.kind != .void {
// g.writeln('$tmp =')
println(1)
}
g.stmt(stmt)
}
g.writeln('}')
2019-12-31 19:42:16 +01:00
if it.else_stmts.len > 0 {
g.writeln('else { ')
for stmt in it.else_stmts {
g.stmt(stmt)
}
g.writeln('}')
}
}
2020-02-07 14:49:14 +01:00
ast.MatchExpr {
type_sym := g.table.get_type_symbol(it.typ)
2020-02-07 14:49:14 +01:00
mut tmp := ''
if type_sym.kind != .void {
tmp = g.new_tmp_var()
2020-02-07 14:49:14 +01:00
}
g.write('$type_sym.name $tmp = ')
2020-02-07 14:49:14 +01:00
g.expr(it.cond)
g.writeln(';') // $it.blocks.len')
for i, block in it.blocks {
match_expr := it.match_exprs[i]
g.write('if $tmp == ')
g.expr(match_expr)
g.writeln('{')
g.stmts(block.stmts)
g.writeln('}')
}
}
2019-12-24 18:54:43 +01:00
else {
println(term.red('cgen.expr(): bad node'))
2019-12-24 18:54:43 +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 {
is_range = true
g.write('array_slice(')
g.expr(node.left)
g.write(', ')
// g.expr(it.low)
g.write('0')
g.write(', ')
g.expr(it.high)
g.write(')')
}
else {}
}
if !is_range {
g.expr(node.left)
g.write('[')
g.expr(node.index)
g.write(']')
}
}
fn (g mut Gen) call_args(args []ast.Expr) {
for i, expr in args {
g.expr(expr)
if i != args.len - 1 {
g.write(', ')
}
}
}
fn verror(s string) {
println(s)
exit(1)
2019-12-24 18:54:43 +01:00
}