Skip to content

Commit

Permalink
strings and os.base
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Aug 4, 2024
1 parent 522b44e commit bf04be1
Show file tree
Hide file tree
Showing 8 changed files with 371 additions and 16 deletions.
9 changes: 5 additions & 4 deletions ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ struct Decl {
}

struct Spec {
node_type_str string @[json: '_type']
name Ident @[json: 'Name']
typ Type @[json: 'Type']
args []Expr @[json: 'Args']
node_type_str string @[json: '_type']
name Ident @[json: 'Name']
typ Type @[json: 'Type']
args []Expr @[json: 'Args']
path BasicLit @[json: 'Path']
}

struct ArrayType {
Expand Down
8 changes: 1 addition & 7 deletions expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ fn (mut app App) basic_lit(l BasicLit) {
fn (mut app App) selector_expr(s SelectorExpr) {
app.expr(s.x)
app.gen('.')
app.gen(s.sel.name)
}

fn (mut app App) selector_expr_fn_call(s SelectorExpr) {
app.expr(s.x)
app.gen('.')
app.gen(s.sel.name.to_lower())
app.gen(go2v_ident(s.sel.name))
}

fn (mut app App) index_expr(s IndexExpr) {
Expand Down
50 changes: 49 additions & 1 deletion fn_call.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.

fn (mut app App) call_expr(call CallExpr) {
// app.genln('// cal_expr')
mut fn_name := ''
mut is_println := false

Expand Down Expand Up @@ -31,7 +32,7 @@ fn (mut app App) call_expr(call CallExpr) {
// app.is_fn_call = false
if fun is SelectorExpr {
// Custom selector expr fn for lower case
app.selector_expr_fn_call(fun)
app.selector_expr_fn_call(call, fun) // fun)
} else {
app.expr(fun)
}
Expand All @@ -56,10 +57,57 @@ fn (mut app App) call_expr(call CallExpr) {
}

for i, arg in call.args {
if app.skip_first_arg {
app.skip_first_arg = false
continue
}
app.expr(arg)
if i < call.args.len - 1 {
app.gen(', ')
}
}
app.genln(')')
}

const unexisting_modules = ['fmt', 'path', 'strings']

fn (mut app App) selector_expr_fn_call(call CallExpr, sel SelectorExpr) {
// app.genln('///selector_expr_fn_call')
if sel.x is Ident {
if sel.x.name in unexisting_modules {
app.handle_nonexistent_module_call(sel.x.name, sel.sel.name, call)
return
}
}
app.expr(sel.x)
app.gen('.')
app.gen(sel.sel.name.to_lower())
}

fn (mut app App) handle_nonexistent_module_call(mod_name string, fn_name string, node CallExpr) {
// println('nonexistent module "${mod_name}" node=${node}')
match mod_name {
'strings' {
app.handle_strings_call(go2v_ident(fn_name), node.args)
}
'path' {
app.handle_path_call(go2v_ident(fn_name), node.args)
}
else {}
}
}

// strings functions are defined as string methods in V
fn (mut app App) handle_strings_call(fn_name string, args []Expr) {
app.expr(args[0])
app.gen('.')
app.gen(fn_name)
app.skip_first_arg = true
}

fn (mut app App) handle_path_call(fn_name string, args []Expr) {
// path.Base => os.base
if fn_name == 'base' {
app.gen('os.base')
}
}
16 changes: 12 additions & 4 deletions main.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ const passing_tests = [
'defer_multiple',
'map',
'if_nested',
'import_strings',
]

struct App {
mut:
sb strings.Builder
is_fn_call bool // for lowercase idents
tests_ok bool = true
sb strings.Builder
is_fn_call bool // for lowercase idents
tests_ok bool = true
skip_first_arg bool // for `strings.Replace(s...)` => `s.replace(...)`
}

fn (mut app App) genln(s string) {
Expand Down Expand Up @@ -92,7 +94,8 @@ fn (mut app App) run_test(test_name string) ! {
// println(res)
}

formatted_v_code := os.read_file(v_path) or { panic(err) }
mut formatted_v_code := os.read_file(v_path) or { panic(err) }
formatted_v_code = formatted_v_code.replace('\n\n\tprint', '\n\tprint') // TODO
// println('formatted:')
// println(formatted_v_code)

Expand All @@ -103,6 +106,7 @@ fn (mut app App) run_test(test_name string) ! {

println('Running test ${test_name}...')
// if generated_v_code == expected_v_code {
// if trim_space(formatted_v_code) == trim_space(expected_v_code) {
if formatted_v_code == expected_v_code {
println(term.green('OK'))
} else {
Expand Down Expand Up @@ -140,6 +144,10 @@ fn print_diff_line(formatted_v_code string, expected_v_code string) {
}
}

fn trim_space(s string) string {
return s.replace('\n\n', '\n')
}

fn main() {
go_file_name := if os.args.len > 1 { os.args[1] } else { '' }
mut app := &App{
Expand Down
11 changes: 11 additions & 0 deletions struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ fn (mut app App) gen_decl(decl Decl) {
for spec in decl.specs {
if spec.node_type_str == 'TypeSpec' && spec.typ.node_type_str == 'StructType' {
app.struct_decl(spec)
} else if spec.node_type_str == 'ImportSpec' && spec.path.value != '' {
app.import_spec(spec)
}
}
}

fn (mut app App) import_spec(spec Spec) {
name := spec.path.value.replace('"', '')
// Skip modules that don't exist in V (fmt, strings etc)
if name in unexisting_modules {
return
}
app.genln('import ${name}')
}

fn (mut app App) struct_decl(spec Spec) {
struct_name := spec.name.name
app.genln('struct ${struct_name} {')
Expand Down
16 changes: 16 additions & 0 deletions tests/import_strings/import_strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"os"
"path"
"strings"
)

func main() {
progname := path.Base(os.Args[0])
fmt.Println(progname)
p := strings.SplitN(progname, "-", 2)
fmt.Println(p)
fmt.Println("Hello")
}
Loading

0 comments on commit bf04be1

Please sign in to comment.