Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue with .c in folder name #126

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ No dependencies other than a `clang` binary.

### Usage

c2v accepts the following arguments:
```
-keep_ast keep ast files
-print_tree print the entire tree
```

```
c2v file.c
```
Expand Down
22 changes: 19 additions & 3 deletions src/c2v.v
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ fn filter_line(s string) string {
return s.replace('false_', 'false').replace('true_', 'true')
}

pub fn replace_file_extension(file_path string, old_extension string, new_extension string) string {
// NOTE: It can't be just `file_path.replace(old_extenstion, new_extension)`, because it will replace all occurencies of old_extenstion string.
// Path '/dir/dir/dir.c.c.c.c.c.c/kalle.c' will become '/dir/dir/dir.json.json.json.json.json.json/kalle.json'.
return file_path.trim_string_right(old_extension) + new_extension
}

fn (mut c C2V) genln(s string) {
if c.indent > 0 && c.out_line_empty {
c.out.write_string(tabs[c.indent])
Expand Down Expand Up @@ -321,6 +327,7 @@ fn (mut c C2V) fn_decl(mut node Node, gen_types string) {
vprintln('')
return
}

if c.is_dir && c.cur_file.ends_with('/info.c') {
// TODO tmp doom hack
return
Expand Down Expand Up @@ -2062,6 +2069,10 @@ fn main() {
eprintln(' c2v file.c')
eprintln(' c2v wrapper file.h')
eprintln(' c2v folder/')
eprintln('')
trollkarlen marked this conversation as resolved.
Show resolved Hide resolved
eprintln('args:')
eprintln(' -keep_ast keep ast files')
eprintln(' -print_tree print the entire tree')
exit(1)
}
vprintln(os.args.str())
Expand Down Expand Up @@ -2103,6 +2114,7 @@ fn (mut c2v C2V) translate_file(path string) {
mut lines := []string{}
mut ast_path := path
ext := os.file_ext(path)

if path.contains('/src/') {
// Hack to fix 'doomtype.h' file not found
// TODO come up with a better solution
Expand All @@ -2116,18 +2128,22 @@ fn (mut c2v C2V) translate_file(path string) {
vprintln(cmd)
out_ast := if c2v.is_dir {
os.getwd() + '/' + (os.dir(os.dir(path)) + '/${c2v.project_output_dirname}/' +
os.base(path.replace(ext, '.json')))
os.base(path).replace(ext, '.json'))
} else {
// file.c => file.json
path.replace(ext, '.json')
vprintln(path)
replace_file_extension(path, ext, '.json')
}
out_ast_dir := os.dir(out_ast)
if c2v.is_dir && !os.exists(out_ast_dir) {
os.mkdir(out_ast_dir) or { panic(err) }
}
vprintln('running in path: ${os.abs_path('.')}')
vprintln('EXT=${ext} out_ast=${out_ast}')
vprintln('out_ast=${out_ast}')
clang_result := os.system('${cmd} > ${out_ast}')
vprintln('${cmd} > "${out_ast}"')
clang_result := os.system('${cmd} > "${out_ast}"')
vprintln('${clang_result}')
if clang_result != 0 {
eprintln('\nThe file ${path} could not be parsed as a C source file.')
exit(1)
Expand Down
13 changes: 10 additions & 3 deletions tests/run_tests.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import term
import os

fn replace_file_extension(file_path string, old_extension string, new_extension string) string {
// NOTE: It can't be just `file_path.replace(old_extenstion, new_extension)`, because it will replace all occurencies of old_extenstion string.
// Path '/dir/dir/dir.c.c.c.c.c.c/kalle.c' will become '/dir/dir/dir.json.json.json.json.json.json/kalle.json'.
return file_path.trim_string_right(old_extension) + new_extension
}

fn try_process_filter_argument() string {
second_argument := os.args[1]

Expand Down Expand Up @@ -120,7 +126,8 @@ fn execute_c2v_command(options string, file string, c2v_dir string) {
}

fn try_get_generated_file(file string, test_file_extension string) !string {
generated_file := file.replace(test_file_extension, '.v')
generated_file := replace_file_extension(file, test_file_extension, '.v')
println(generated_file)

if !exists(generated_file) {
return error('Expected generated file `${generated_file}` does not exist')
Expand All @@ -134,12 +141,12 @@ fn format_generated_file(file string) {
}

fn get_expected_file_content(file string, test_file_extension string) string {
file_content := read_file(file.replace(test_file_extension, '.out')) or { '' }
file_content := read_file(replace_file_extension(file, test_file_extension, '.out')) or { '' }
return file_content.trim_space()
}

fn get_result_file_content(file string, test_file_extension string) string {
file_content := read_file(file.replace(test_file_extension, '.v')) or { '' }
file_content := read_file(file) or { '' }
return file_content.after('// vstart').trim_space()
}

Expand Down
16 changes: 16 additions & 0 deletions tests/test.c/1.hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>

int main() {
printf("hello world!");
return 0;
}

typedef struct Foo {
int bar;
} Foo;

void implicit_inits() {
int num;
Foo foo;
}

16 changes: 16 additions & 0 deletions tests/test.c/1.hello.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[translated]
module main

fn main() {
C.printf(c'hello world!')
return
}

struct Foo {
bar int
}

fn implicit_inits() {
num := 0
foo := Foo{}
}