Skip to content

Commit

Permalink
allow running on non-tests via go2v file.go
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Aug 18, 2024
1 parent 3fde6c6 commit aeda1d3
Showing 1 changed file with 59 additions and 42 deletions.
101 changes: 59 additions & 42 deletions main.v
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,56 @@ fn type_or_ident(typ TypeOrIdent) string {
return if typ.elt.name != '' { '[]${typ.elt.name}' } else { typ.name }
}

fn generate_ast_for_go_file(go_file_path string) string {
tmpdir := os.temp_dir()
output_file := tmpdir + '/go.json'

// Check if asty is installed
asty_installed := os.system('go list -m -json github.com/asty-org/asty@latest > /dev/null 2>&1') == 0

if !asty_installed {
println('asty not found, installing...')
install_result := os.system('go install github.com/asty-org/asty@latest')
if install_result != 0 {
eprintln('Failed to install asty')
return ''
}
}

println('generating ast for ${go_file_path}')
run_result := os.system('asty go2json -indent 2 -input ${go_file_path} -output ${output_file}')
if run_result != 0 {
eprintln('Failed to run asty')
return ''
}

json_content := os.read_file(output_file) or {
eprintln('Failed to read ${output_file}')
return ''
}

// Replace "NodeType": " with "_type": " to handle sum types
updated_content := json_content.replace('"NodeType": "', '"_type": "')

os.write_file(output_file, updated_content) or {
eprintln('Failed to write to ${output_file}')
return ''
}
return output_file
}

fn (mut app App) translate_file(go_file_path string) {
println('Translating a single Go file ${go_file_path}...')
ast_file := generate_ast_for_go_file(go_file_path)
go_file := parse_go_ast(ast_file) or {
eprintln('Failed to parse Go AST: ${err}')
return
}
generated_v_code := app.generate_v_code(go_file)
v_path := go_file_path.replace('.go', '.v')
os.write_file(v_path, generated_v_code) or { panic(err) }
}

fn (mut app App) run_test(subdir string, test_name string) ! {
go_file_path := '${subdir}/${test_name}/${test_name}.go.json'
expected_v_code_path := '${subdir}/${test_name}/${test_name}.vv'
Expand Down Expand Up @@ -147,52 +197,23 @@ fn print_diff_line(formatted_v_code string, expected_v_code string) {

fn create_json(subdir string, test_name string) {
input_file := '${subdir}/${test_name}/${test_name}.go'
output_file := '${input_file}.json'

// Check if asty is installed
asty_installed := os.system('go list -m -json github.com/asty-org/asty@latest > /dev/null 2>&1') == 0
// output_file := '${input_file}.json'

if !asty_installed {
println('asty not found, installing...')
install_result := os.system('go install github.com/asty-org/asty@latest')
if install_result != 0 {
eprintln('Failed to install asty')
return
}
}

println('generating ast for ${input_file}')

run_result := os.system('asty go2json -indent 2 -input ${input_file} -output ${output_file}')

if run_result != 0 {
eprintln('Failed to run asty')
return
}

json_content := os.read_file(output_file) or {
eprintln('Failed to read ${output_file}')
return
}

// Replace "NodeType": " with "_type": " to handle sum types
updated_content := json_content.replace('"NodeType": "', '"_type": "')

os.write_file(output_file, updated_content) or {
eprintln('Failed to write to ${output_file}')
return
}
generate_ast_for_go_file(input_file)
}

fn main() {
mut subdir := 'tests'

mut go_file_name := if os.args.len > 1 { os.args[1] } else { '' }

mut app := &App{
sb: strings.new_builder(1000)
}

// Not a test
if go_file_name.ends_with('.go') {
app.translate_file(go_file_name)
return
}
// A single test
if go_file_name != '' {
go_file_name = go_file_name.trim_right('/')
subdir = os.dir(go_file_name)
Expand All @@ -203,17 +224,13 @@ fn main() {
app.run_test(subdir, test_name)!
return
}

// All tests
mut test_names := os.ls('tests') or { return }
test_names.sort()

mut tests_ok := true

for test_name in test_names {
create_json(subdir, test_name)

println('===========================================')

// A separate instance for each test
mut app2 := &App{
sb: strings.new_builder(1000)
Expand Down

0 comments on commit aeda1d3

Please sign in to comment.