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

161 lines
2.6 KiB
V
Raw Normal View History

2019-12-30 10:30:24 +03:00
module gen
2019-12-24 20:54:43 +03:00
import (
strings
v.ast
term
2019-12-24 20:54:43 +03:00
)
2019-12-30 10:30:24 +03:00
struct JsGen {
2019-12-24 20:54:43 +03:00
out strings.Builder
}
2019-12-30 14:10:46 +03:00
pub fn jsgen(program ast.File) string {
2019-12-30 10:30:24 +03:00
mut g := JsGen{
2019-12-28 13:02:06 +03:00
out: strings.new_builder(100)
}
2019-12-28 16:11:05 +03:00
for stmt in program.stmts {
g.stmt(stmt)
2019-12-24 20:54:43 +03:00
g.writeln('')
}
return (g.out.str())
2019-12-24 20:54:43 +03:00
}
2019-12-30 10:30:24 +03:00
pub fn (g &JsGen) save() {}
2019-12-24 20:54:43 +03:00
2019-12-30 10:30:24 +03:00
pub fn (g mut JsGen) write(s string) {
2019-12-24 20:54:43 +03:00
g.out.write(s)
}
2019-12-30 10:30:24 +03:00
pub fn (g mut JsGen) writeln(s string) {
2019-12-24 20:54:43 +03:00
g.out.writeln(s)
}
2019-12-30 10:30:24 +03:00
fn (g mut JsGen) stmt(node ast.Stmt) {
2019-12-28 16:11:05 +03:00
match node {
2019-12-28 21:16:04 +03:00
ast.AssignStmt {
g.expr(it.left)
g.write(' $it.op.str() ')
g.expr(it.right)
g.writeln(';')
}
2019-12-28 16:11:05 +03:00
ast.FnDecl {
2019-12-30 10:30:24 +03:00
g.write('/** @return { $it.typ.name } **/\nfunction ${it.name}(')
2019-12-29 09:24:17 +03:00
for arg in it.args {
2019-12-30 10:30:24 +03:00
g.write(' /** @type { arg.typ.name } **/ $arg.name')
2019-12-29 09:24:17 +03:00
}
g.writeln(') { ')
2019-12-28 16:11:05 +03:00
for stmt in it.stmts {
g.stmt(stmt)
}
g.writeln('}')
}
ast.Return {
g.write('return ')
g.expr(it.expr)
g.writeln(';')
}
ast.VarDecl {
2019-12-30 10:30:24 +03:00
g.write('var /* $it.typ.name */ $it.name = ')
2019-12-28 16:11:05 +03:00
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 {
2019-12-30 10:30:24 +03:00
// g.writeln('typedef struct {')
// for field in it.fields {
// g.writeln('\t$field.typ.name $field.name;')
// }
g.writeln('var $it.name = function() {};')
}
2019-12-28 16:11:05 +03:00
ast.ExprStmt {
g.expr(it.expr)
match it.expr {
// no ; after an if expression
ast.IfExpr {}
else {
g.writeln(';')
}
}
2019-12-28 16:11:05 +03:00
}
else {
2019-12-30 10:30:24 +03:00
verror('jsgen.stmt(): bad node')
2019-12-28 16:11:05 +03:00
}
}
}
2019-12-30 10:30:24 +03:00
fn (g mut JsGen) expr(node ast.Expr) {
2019-12-28 13:02:06 +03:00
// println('cgen expr()')
2019-12-24 20:54:43 +03:00
match node {
2019-12-26 13:21:41 +03:00
ast.IntegerLiteral {
2019-12-24 20:54:43 +03:00
g.write(it.val.str())
}
2019-12-27 12:03:29 +03:00
ast.FloatLiteral {
g.write(it.val)
}
2019-12-26 12:02:38 +03:00
ast.UnaryExpr {
g.expr(it.left)
g.write(' $it.op ')
}
2019-12-24 20:54:43 +03:00
ast.StringLiteral {
2019-12-27 10:52:20 +03:00
g.write('tos3("$it.val")')
2019-12-24 20:54:43 +03:00
}
ast.BinaryExpr {
g.expr(it.left)
g.write(' $it.op.str() ')
g.expr(it.right)
2019-12-24 20:54:43 +03:00
}
// `user := User{name: 'Bob'}`
ast.StructInit {
2019-12-30 10:30:24 +03:00
g.writeln('/*$it.typ.name*/{')
for i, field in it.fields {
2019-12-30 10:30:24 +03:00
g.write('\t$field : ')
g.expr(it.exprs[i])
g.writeln(', ')
}
g.write('}')
}
2019-12-29 09:24:17 +03:00
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(')')
2019-12-29 09:24:17 +03:00
}
2019-12-28 16:11:05 +03:00
ast.Ident {
g.write('$it.name')
2019-12-24 20:54:43 +03:00
}
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('}')
}
2019-12-24 20:54:43 +03:00
else {
2019-12-30 10:30:24 +03:00
println(term.red('jsgen.expr(): bad node'))
2019-12-24 20:54:43 +03:00
}
}
}