Skip to content

Commit

Permalink
fix ref; handle StarExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Aug 6, 2024
1 parent 20c941e commit f5aa1e9
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 39 deletions.
8 changes: 5 additions & 3 deletions ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Expr = ArrayType
| KeyValueExpr
| ParenExpr
| SelectorExpr
| StarExpr
| TypeOrIdent
| UnaryExpr

Expand Down Expand Up @@ -146,9 +147,10 @@ struct FieldList {
}

struct Field {
node_type_str string @[json: '_type']
names []Ident @[json: 'Names']
typ TypeOrIdent @[json: 'Type']
node_type_str string @[json: '_type']
names []Ident @[json: 'Names']
// typ TypeOrIdent @[json: 'Type']
typ Type2 @[json: 'Type']
}

struct Ident {
Expand Down
27 changes: 24 additions & 3 deletions expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn (mut app App) expr(expr Expr) {
app.binary_expr(expr)
}
Ident {
app.gen(go2v_ident(expr.name))
app.ident(expr)
}
CallExpr {
app.call_expr(expr)
Expand Down Expand Up @@ -40,6 +40,9 @@ fn (mut app App) expr(expr Expr) {
ArrayType {
app.array_type(expr)
}
StarExpr {
app.star_expr(expr)
}
FuncLit {
app.func_lit(expr)
}
Expand All @@ -59,9 +62,11 @@ fn (mut app App) basic_lit(l BasicLit) {
}

fn (mut app App) selector_expr(s SelectorExpr) {
force_upper := app.force_upper // save force upper for `mod.ForceUpper`
app.expr(s.x)
app.gen('.')
app.gen(go2v_ident(s.sel.name))
app.force_upper = force_upper
app.gen(app.go2v_ident(s.sel.name))
}

fn (mut app App) index_expr(s IndexExpr) {
Expand Down Expand Up @@ -106,5 +111,21 @@ fn (mut app App) key_value_expr(expr KeyValueExpr) {
}

fn (mut app App) array_type(node ArrayType) {
app.gen('array type TODO')
if node.elt is Ident {
app.gen('[]${node.elt.name}')
} else if node.elt is StarExpr {
app.gen('[]')
app.star_expr(node.elt)
// app.gen('[]&${node.elt.name}')
}
}

fn (mut app App) star_expr(node StarExpr) {
app.gen('&')
app.expr(node.x)
}

fn (mut app App) ident(node Ident) {
// app.gen('f=${app.force_upper}')
app.gen(app.go2v_ident(node.name))
}
7 changes: 4 additions & 3 deletions fn_call.v
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ fn (mut app App) call_expr(call CallExpr) {
// app.is_fn_call = true
// app.expr(fun)
// app.is_fn_call = false
// app.force_lower = true
if fun is SelectorExpr {
// Custom selector expr fn for lower case
app.selector_expr_fn_call(call, fun) // fun)
} else {
app.expr(fun)
}
}
app.gen('${fn_name}(')
app.gen('${fn_name}(') // fn_name is empty unless print
// In V println can only accept one argument, so convert multiple argumnents into a single string
// with concatenation:
// `println(a, b)` => `println('${a} ${b}')`
Expand Down Expand Up @@ -91,10 +92,10 @@ fn (mut app App) handle_nonexistent_module_call(mod_name string, fn_name string,
// println('nonexistent module "${mod_name}" node=${node}')
match mod_name {
'strings' {
app.handle_strings_call(go2v_ident(fn_name), node.args)
app.handle_strings_call(app.go2v_ident(fn_name), node.args)
}
'path' {
app.handle_path_call(go2v_ident(fn_name), node.args)
app.handle_path_call(app.go2v_ident(fn_name), node.args)
}
else {}
}
Expand Down
53 changes: 31 additions & 22 deletions fn_decl.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,54 @@
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 := type_or_ident(decl.recv.list[0].typ)
recv_name := decl.recv.list[0].names[0].name
recv = '(${recv_name} ${recv_type})'
}
// mut recv := ''
// if decl.recv.list.len > 0 {
// recv_type := 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(', ') + ' ' +
// type_or_ident(it.typ)).join(', ')
results := if decl.typ.results.list.len > 0 {
' ${decl.typ.results.list.map(type_or_ident(it.typ)).join(', ')}'
} else {
''
}
if recv != '' {
app.gen('fn ${recv} ')
// if recv != '' {
if decl.recv.list.len > 0 {
// app.gen('fn ${recv} ')
recv_name := decl.recv.list[0].names[0].name
app.gen('fn (${recv_name} ')
app.typ(decl.recv.list[0].typ)
app.gen(') ')
} else {
app.gen('fn ')
}
app.gen(method_name)
app.func_params(decl.typ.params)
app.genln(results)
// app.genln(results)
// Return types
for i, res in decl.typ.results.list {
app.typ(res.typ)
if i < decl.typ.results.list.len - 1 {
app.gen(',')
}
//' ${decl.typ.results.list.map(type_or_ident(it.typ)).join(', ')}'
}
app.block_stmt(decl.body)
}

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

fn (mut app App) func_lit(node FuncLit) {
Expand Down
17 changes: 16 additions & 1 deletion main.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mut:
is_fn_call bool // for lowercase idents
tests_ok bool = true
skip_first_arg bool // for `strings.Replace(s...)` => `s.replace(...)`
force_upper bool // for `field Type` in struct decl, `mod.UpperCase` types etc
}

fn (mut app App) genln(s string) {
Expand Down Expand Up @@ -44,7 +45,21 @@ fn (mut app App) generate_v_code(go_file GoFile) string {
return app.sb.str()
}

fn (mut app App) typ() {
fn (mut app App) typ(t Type2) {
app.force_upper = true
match t {
Ident {
app.gen(go2v_type(t.name))
}
ArrayType {
app.array_type(t)
// app.gen('[]${t.elt.name}')
}
StarExpr {
app.star_expr(t)
}
}
app.force_upper = false
}

fn type_or_ident(typ TypeOrIdent) string {
Expand Down
16 changes: 13 additions & 3 deletions struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn (mut app App) gen_decl(decl Decl) {
}

fn (mut app App) import_spec(spec Spec) {
name := spec.path.value.replace('"', '')
name := spec.path.value.replace('"', '').replace('/', '.')
// Skip modules that don't exist in V (fmt, strings etc)
if name in unexisting_modules {
return
Expand All @@ -33,9 +33,19 @@ fn (mut app App) struct_decl(spec Spec) {
app.genln('pub mut:')
}
for field in spec.typ.fields.list {
type_name := type_or_ident(field.typ)
// type_name := type_or_ident(field.typ)
for n in field.names {
app.genln('\t${go2v_ident(n.name)} ${go2v_type(type_name)}')
// app.genln('\t${go2v_ident(n.name)} ${go2v_type(type_name)}')
app.gen('\t')
// app.force_upper = true
app.gen(app.go2v_ident(n.name))
app.gen(' ')
app.force_upper = true
app.typ(field.typ)
if field.typ is StarExpr {
app.gen(' = unsafe { nil }')
}
app.genln('')
}
}
app.genln('}\n')
Expand Down
6 changes: 3 additions & 3 deletions tests/ref/ref.vv
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module main
import crypto.aes

struct BinTree {
mut:
pub mut:
value string
left &BinTree = nil
right &BinTree = nil
left &BinTree = unsafe { nil }
right &BinTree = unsafe { nil }
list []&BinTree
exter []&aes.KeySizeError
}
19 changes: 18 additions & 1 deletion util.v
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ fn go2v_type(typ string) string {
return typ
}

fn go2v_ident(ident string) string {
fn (mut app App) go2v_ident(ident string) string {
// println('ident=${ident} force_upper=${app.force_upper}')
if app.force_upper {
app.force_upper = false
return ident // go2v_ident2(ident)
}
return go2v_ident2(ident)
/*
return if ident[0].is_capital() {
// println('is cap')
ident
} else {
go2v_ident2(ident)
}
*/
}

fn go2v_ident2(ident string) string {
return ident.camel_to_snake() // to_lower()) // TODO ?
}

0 comments on commit f5aa1e9

Please sign in to comment.