Skip to content

Commit

Permalink
defer; func lit
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Jul 28, 2024
1 parent b6f845f commit 9d4365e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
16 changes: 15 additions & 1 deletion ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Expr = ArrayType
| CallExpr
| CompositeLit
| Ellipsis
| FuncLit
| Ident
| IndexExpr
| KeyValueExpr
Expand All @@ -27,6 +28,7 @@ type Stmt = AssignStmt
| IfStmt
| IncDecStmt
| RangeStmt
| ReturnStmt
| SwitchStmt

struct GoFile {
Expand Down Expand Up @@ -145,7 +147,8 @@ struct Field {
}

struct Ident {
name string @[json: 'Name']
node_type_str string @[json: '_type']
name string @[json: 'Name']
}

struct TypeOrIdent {
Expand Down Expand Up @@ -241,6 +244,17 @@ struct DeferStmt {
call Expr @[json: 'Call']
}

struct ReturnStmt {
node_type_str string @[json: '_type']
results []Expr @[json: 'Results']
}

struct FuncLit {
node_type_str string @[json: '_type']
typ FuncType @[json: 'Type']
body BlockStmt @[json: 'Body']
}

fn parse_go_ast(file_path string) !GoFile {
data := os.read_file(file_path)!
return json.decode(GoFile, data)!
Expand Down
5 changes: 4 additions & 1 deletion expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn (mut app App) expr(expr Expr) {
app.binary_expr(expr)
}
Ident {
app.gen(expr.name.to_lower()) // TODO ?
app.gen(expr.name.camel_to_snake()) // to_lower()) // TODO ?
}
CallExpr {
app.call_expr(expr)
Expand Down Expand Up @@ -39,6 +39,9 @@ fn (mut app App) expr(expr Expr) {
ArrayType {
app.array_type(expr)
}
FuncLit {
app.func_lit(expr)
}
Ellipsis {}
// else {
// app.gen('/* UNHANDLED EXPR ${expr.node_type} */')
Expand Down
32 changes: 29 additions & 3 deletions fn_decl.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
fn (mut app App) func_decl(decl Decl) {
method_name := decl.name.name.to_lower()
// println('FUNC DECL ${method_name}')
mut recv := ''
if decl.recv.list.len > 0 {
recv_type := app.type_or_ident(decl.recv.list[0].typ)
recv_name := decl.recv.list[0].names[0].name
recv = '(${recv_name} ${recv_type})'
}
params := decl.typ.params.list.map(it.names.map(it.name).join(', ') + ' ' +
app.type_or_ident(it.typ)).join(', ')
// params := decl.typ.params.list.map(it.names.map(it.name).join(', ') + ' ' +
// app.type_or_ident(it.typ)).join(', ')
results := if decl.typ.results.list.len > 0 {
' ${decl.typ.results.list.map(app.type_or_ident(it.typ)).join(', ')}'
} else {
Expand All @@ -20,6 +21,31 @@ fn (mut app App) func_decl(decl Decl) {
} else {
app.gen('fn ')
}
app.genln('${method_name}(${params})${results}')
app.gen(method_name)
app.func_params(decl.typ.params)
app.genln(results)
app.block_stmt(decl.body)
}

fn (mut app App) func_params(params FieldList) {
p := params.list.map(it.names.map(it.name).join(', ') + ' ' + app.type_or_ident(it.typ)).join(', ')
app.gen('(')
app.gen(p)
app.gen(')')
/*
for i, param in params.list {
app.gen(param.names[0].name)
app.gen(' ')
app.gen(app.type_or_ident(param.typ))
if i < params.list.len - 1 {
app.gen(',')
}
}
*/
}

fn (mut app App) func_lit(node FuncLit) {
app.gen('fn')
app.func_params(node.typ.params)
app.block_stmt(node.body)
}
1 change: 1 addition & 0 deletions main.v
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const passing_tests = [
'array_byte',
'array_fixed_size',
'defer',
'defer_multiple',
]

struct App {
Expand Down
30 changes: 27 additions & 3 deletions stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ fn (mut app App) stmt(stmt Stmt) {
RangeStmt {
app.range_stmt(stmt)
}
ReturnStmt {
app.return_stmt(stmt)
}
DeferStmt {
app.defer_stmt(stmt)
}
Expand All @@ -61,6 +64,8 @@ fn (mut app App) expr_stmt(stmt ExprStmt) {

fn (mut app App) block_stmt(body BlockStmt) {
app.genln('{')
// println('LIST=')
// println(body.list)
app.stmt_list(body.list)
app.genln('}')
}
Expand Down Expand Up @@ -136,9 +141,28 @@ fn (mut app App) decl_stmt(d DeclStmt) {
}

fn (mut app App) defer_stmt(node DeferStmt) {
app.genln('defer {')
app.expr(node.call)
app.genln('}')
// print_backtrace()
app.gen('defer')
// `defer fn() { ... } ()
// empty function, just generate `defer { ... }` in V
if node.call is CallExpr && node.call.args.len == 0 {
func_lit := node.call.fun as FuncLit
app.block_stmt(func_lit.body)
} else {
app.genln('{')
app.expr(node.call)
app.genln('}')
}
}

fn (mut app App) return_stmt(node ReturnStmt) {
app.gen('return ')
for i, result in node.results {
app.expr(result)
if i < node.results.len - 1 {
app.gen(',')
}
}
}

fn go2v_type(typ string) string {
Expand Down

0 comments on commit 9d4365e

Please sign in to comment.