|
1 |
| -import os |
2 |
| - |
3 |
| -// TODO: add a system with a watcher function to make the tree construction stage concurrent and possibly also to the other stages |
| 1 | +module main |
4 | 2 |
|
5 |
| -struct Params { |
6 |
| - outputs_file bool |
7 |
| - input_str string |
8 |
| - input_path string |
9 |
| - output_path string = 'out.v' |
10 |
| - go_path string = 'go' |
11 |
| -} |
| 3 | +import os |
| 4 | +import cli |
| 5 | +import transpiler |
12 | 6 |
|
13 |
| -// TODO TEMP REMOVE |
14 | 7 | fn main() {
|
15 |
| - go_to_v(Params{ |
16 |
| - input_path: 'test.go' |
17 |
| - // outputs_file: true |
18 |
| - }) ? |
19 |
| -} |
20 |
| - |
21 |
| -pub fn go_to_v(options Params) ?string { |
22 |
| - if !os.exists_in_system_path('go') { |
23 |
| - // TODO: better error message |
24 |
| - panic("Go is needed by the utility and it isn't installed or not present in your path, get it at https://golang.org or manually specify the path to the go binary by passing the --go-path parameter if using the CLI or the {go_path} parameter in the function call") |
25 |
| - } |
26 |
| - |
27 |
| - mut str := '' |
28 |
| - mut had_to_create_a_file := false |
29 |
| - mut input_path := options.input_path |
30 |
| - |
31 |
| - if options.input_path == '' && options.input_str == '' { |
32 |
| - panic('Either the input_path or input_str parameter must be set') |
33 |
| - } else if options.input_path != '' && options.input_str != '' { |
34 |
| - panic('Only one of the input_path or input_str parameter must be set') |
35 |
| - } else if options.input_path != '' { |
36 |
| - str = os.read_file(options.input_path) ? |
37 |
| - } else { |
38 |
| - str = options.input_str |
39 |
| - input_path = 'temp' |
40 |
| - had_to_create_a_file = true |
41 |
| - os.write_file('temp', str) ? |
42 |
| - } |
43 |
| - |
44 |
| - os.execute('$options.go_path run ${os.resource_abs_path('/')}/get_ast.go -f $input_path -o $options.output_path') |
45 |
| - raw_input := os.read_file(options.output_path) ? |
46 |
| - os.rm(options.output_path) ? |
47 |
| - if had_to_create_a_file { |
48 |
| - os.rm(input_path) ? |
49 |
| - } |
50 |
| - |
51 |
| - input := raw_input.runes() |
52 |
| - tokens := tokenizer(input) |
53 |
| - tree := tree_constructor(tokens) |
54 |
| - v_ast := ast_constructor(tree) |
55 |
| - v_file := v_file_constructor(v_ast) |
56 |
| - |
57 |
| - if options.outputs_file { |
58 |
| - os.write_file(options.output_path, v_file) ? |
| 8 | + mut app := cli.Command{ |
| 9 | + name: 'go2v' |
| 10 | + description: 'Go2V is a CLI utility to transpile Go source code into V source code.' |
| 11 | + execute: fn (cmd cli.Command) ? { |
| 12 | + if cmd.args.len == 0 { println('Wrong usage, see "go2v help"') } else { transpiler.go_to_v(cmd.args[0], cmd.args[2] or { |
| 13 | + ''}) ? } |
| 14 | + return |
| 15 | + } |
| 16 | + commands: [ |
| 17 | + cli.Command{ |
| 18 | + name: 'help' |
| 19 | + execute: fn (cmd cli.Command) ? { |
| 20 | + help_topic := cmd.args[0] or { 'help' } |
| 21 | + println(os.read_file(os.resource_abs_path('/help/${help_topic}.txt')) or { |
| 22 | + 'No help topic for $help_topic' |
| 23 | + }) |
| 24 | + return |
| 25 | + } |
| 26 | + }, |
| 27 | + cli.Command{ |
| 28 | + name: 'test' |
| 29 | + execute: fn (cmd cli.Command) ? { |
| 30 | + mut stats := '' |
| 31 | + if cmd.args.len == 0 { |
| 32 | + stats = '-stats' |
| 33 | + } else if cmd.args[0] == '-compact' { |
| 34 | + stats = '' |
| 35 | + } else { |
| 36 | + println('Wrong usage, see "go2v help test"') |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + os.execvp('${@VEXE}', [stats, 'test', os.resource_abs_path('/tests')]) ? |
| 41 | + return |
| 42 | + } |
| 43 | + }, |
| 44 | + ] |
59 | 45 | }
|
60 |
| - |
61 |
| - // TODO TEMP REMOVE |
62 |
| - os.mkdir('test') or {} |
63 |
| - os.write_file('test/tree', tree.str()) ? |
64 |
| - os.write_file('test/ast', v_ast.str()) ? |
65 |
| - os.write_file('test/file.v', v_file) ? |
66 |
| - |
67 |
| - return v_file |
| 46 | + app.setup() |
| 47 | + app.parse(os.args) |
68 | 48 | }
|
0 commit comments