1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/v/gen/jsgen.v
2019-12-30 16:55:59 +01:00

161 lines
2.6 KiB
V

module gen
import (
strings
v.ast
term
)
struct JsGen {
out strings.Builder
}
pub fn jsgen(program ast.File) string {
mut g := JsGen{
out: strings.new_builder(100)
}
for stmt in program.stmts {
g.stmt(stmt)
g.writeln('')
}
return (g.out.str())
}
pub fn (g &JsGen) save() {}
pub fn (g mut JsGen) write(s string) {
g.out.write(s)
}
pub fn (g mut JsGen) writeln(s string) {
g.out.writeln(s)
}
fn (g mut JsGen) stmt(node ast.Stmt) {
match node {
ast.AssignStmt {
g.expr(it.left)
g.write(' $it.op.str() ')
g.expr(it.right)
g.writeln(';')
}
ast.FnDecl {
g.write('/** @return { $it.typ.name } **/\nfunction ${it.name}(')
for arg in it.args {
g.write(' /** @type { arg.typ.name } **/ $arg.name')
}
g.writeln(') { ')
for stmt in it.stmts {
g.stmt(stmt)
}
g.writeln('}')
}
ast.Return {
g.write('return ')
g.expr(it.expr)
g.writeln(';')
}
ast.VarDecl {
g.write('var /* $it.typ.name */ $it.name = ')
g.expr(it.expr)
g.writeln(';')
}
ast.ForStmt {
g.write('while (')
g.expr(it.cond)
g.writeln(') {')
for stmt in it.stmts {
g.stmt(stmt)
}
g.writeln('}')
}
ast.StructDecl {
// g.writeln('typedef struct {')
// for field in it.fields {
// g.writeln('\t$field.typ.name $field.name;')
// }
g.writeln('var $it.name = function() {};')
}
ast.ExprStmt {
g.expr(it.expr)
match it.expr {
// no ; after an if expression
ast.IfExpr {}
else {
g.writeln(';')
}
}
}
else {
verror('jsgen.stmt(): bad node')
}
}
}
fn (g mut JsGen) expr(node ast.Expr) {
// println('cgen expr()')
match node {
ast.IntegerLiteral {
g.write(it.val.str())
}
ast.FloatLiteral {
g.write(it.val)
}
ast.UnaryExpr {
g.expr(it.left)
g.write(' $it.op ')
}
ast.StringLiteral {
g.write('tos3("$it.val")')
}
ast.BinaryExpr {
g.expr(it.left)
g.write(' $it.op.str() ')
g.expr(it.right)
}
// `user := User{name: 'Bob'}`
ast.StructInit {
g.writeln('/*$it.typ.name*/{')
for i, field in it.fields {
g.write('\t$field : ')
g.expr(it.exprs[i])
g.writeln(', ')
}
g.write('}')
}
ast.CallExpr {
g.write('${it.name}(')
for i, expr in it.args {
g.expr(expr)
if i != it.args.len - 1 {
g.write(', ')
}
}
g.write(')')
}
ast.Ident {
g.write('$it.name')
}
ast.BoolLiteral {
if it.val == true {
g.write('true')
}
else {
g.write('false')
}
}
ast.IfExpr {
g.write('if (')
g.expr(it.cond)
g.writeln(') {')
for stmt in it.stmts {
g.stmt(stmt)
}
g.writeln('}')
}
else {
println(term.red('jsgen.expr(): bad node'))
}
}
}