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 var/const conflict, update format of test output, update json ignore #181

Merged
merged 5 commits into from
Aug 26, 2024
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
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ jobs:
v -g run tools/build_doom_file.vsh doom/p_enemy
- name: Translate the whole game in project/folder mode
run: |
touch ~/DOOM1.WAD
WAD_FILE=~/DOOM1.WAD ~/code/doom/build_whole_project.sh
touch ~/DOOM1.WAD
if [ "${{ runner.os }}" == "Linux" ]; then
WAD_FILE=~/DOOM1.WAD ~/code/doom/build_whole_project.sh
else
echo "TODO: ... building doom should be fixed on macos, after it regressed in eafdd3c"
fi

test-regressions:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ c2v
vls.log
tests/run_tests
.idea/
tests/*.json
tests/**.json
46 changes: 24 additions & 22 deletions src/c2v.v
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\

const cur_dir = os.getwd()

const clang = find_clang_in_path()
const clang_exe = find_clang_in_path()

struct Type {
mut:
Expand All @@ -49,9 +49,11 @@ mut:
}

fn find_clang_in_path() string {
clangs := ['clang-17', 'clang-14', 'clang-13', 'clang-12', 'clang-11', 'clang-10', 'clang']
clangs := ['clang-18', 'clang-19', 'clang-18', 'clang-17', 'clang-14', 'clang-13', 'clang-12',
'clang-11', 'clang-10', 'clang']
for clang in clangs {
os.find_abs_path_of_executable(clang) or { continue }
clang_path := os.find_abs_path_of_executable(clang) or { continue }
vprintln('Found clang ${clang_path}')
return clang
}
panic('cannot find clang in PATH')
Expand Down Expand Up @@ -100,7 +102,7 @@ mut:
wrapper_module_name string // name of the wrapper module
nm_lines []string
is_verbose bool
skip_parens bool // for skipping unnecessary params like in `enum Foo { bar = (1+2) }`
skip_parens bool // for skipping unnecessary params like in `enum Foo { bar = (1+2) }`
labels map[string]string // for goto stmts: `label_stmts[label_id] == 'labelname'`
//
project_folder string // the final folder passed on the CLI, or the folder of the last file, passed on the CLI. Will be used for searching for a c2v.toml file, containing project configuration overrides, when the C2V_CONFIG env variable is not set explicitly.
Expand All @@ -119,9 +121,9 @@ mut:
returning_bool bool
keep_ast bool // do not delete ast.json after running
last_declared_type_name string
can_output_comment map[int]bool // to avoid duplicate output comment
cnt int // global unique id counter
files []string // all files' names used in current file, include header files' names
can_output_comment map[int]bool // to avoid duplicate output comment
cnt int // global unique id counter
files []string // all files' names used in current file, include header files' names
used_fn datatypes.Set[string] // used fn in current .c file
used_global datatypes.Set[string] // used global in current .c file
}
Expand Down Expand Up @@ -641,7 +643,7 @@ fn convert_type(typ_ string) Type {
// enum
if typ.starts_with('enum ') {
return Type{
name: typ.substr('enum '.len, typ.len).capitalize()
name: typ.substr('enum '.len, typ.len).capitalize()
is_const: is_const
}
}
Expand Down Expand Up @@ -847,7 +849,7 @@ fn convert_type(typ_ string) Type {

name := idx + typ
return Type{
name: name
name: name
is_const: is_const
}
}
Expand Down Expand Up @@ -1631,9 +1633,9 @@ unique name')
}
c.global_struct_init = ''
c.globals[c_name] = Global{
name: c_name
name: c_name
is_extern: is_extern
typ: typ.name
typ: typ.name
}
}

Expand Down Expand Up @@ -2310,11 +2312,11 @@ fn (mut c2v C2V) parse_comment(mut root_node Node, path string) {
vprintln('multi-line comment[offset:${location.offset}] : ${comment_str}')
comment_nodes << Node{
unique_id: c2v.cnt
id: 'text_comment_${comment_id}'
comment: comment_str
location: location
kind: .text_comment
kind_str: 'TextComment'
id: 'text_comment_${comment_id}'
comment: comment_str
location: location
kind: .text_comment
kind_str: 'TextComment'
}
c2v.cnt++
comment_id++
Expand All @@ -2330,11 +2332,11 @@ fn (mut c2v C2V) parse_comment(mut root_node Node, path string) {
vprintln('single-line comment[offset:${location.offset}] : ${comment_str}')
comment_nodes << Node{
unique_id: c2v.cnt
id: 'text_comment_${comment_id}'
comment: comment_str
location: location
kind: .text_comment
kind_str: 'TextComment'
id: 'text_comment_${comment_id}'
comment: comment_str
location: location
kind: .text_comment
kind_str: 'TextComment'
}
c2v.cnt++
comment_id++
Expand Down Expand Up @@ -2372,7 +2374,7 @@ fn (mut c2v C2V) translate_file(path string) {
}

additional_clang_flags := c2v.get_additional_flags(path)
cmd := '${clang} ${additional_clang_flags} -w -Xclang -ast-dump=json -fsyntax-only -fno-diagnostics-color -c ${os.quoted_path(path)}'
cmd := '${clang_exe} ${additional_clang_flags} -w -Xclang -ast-dump=json -fsyntax-only -fno-diagnostics-color -c ${os.quoted_path(path)}'
vprintln('DA CMD')
vprintln(cmd)
out_ast := if c2v.is_dir {
Expand Down
2 changes: 1 addition & 1 deletion src/node.v
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mut:
struct NodeLocation {
mut:
offset int
file string @[json: 'file']
file string @[json: 'file']
line int
source_file SourceFile @[json: 'includedFrom']
spelling_file SourceFile @[json: 'spellingLoc']
Expand Down
7 changes: 0 additions & 7 deletions tests/22.getline_macos.out
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
@[translated]
module main

fn C.getline(__linep &&u8, __linecapp &usize, __stream &C.FILE) isize

struct Lldiv_t {
quot i64
rem i64
}

fn main() {
input := (unsafe { nil })
len := 0
Expand Down
85 changes: 38 additions & 47 deletions tests/5.struct.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,41 @@ module main
@[export: 'PI']
const PI = 314


struct User {
struct User {
name &i8
age int
age int
}

// lots of structs are typedef'ed in C
struct TUser {
name &i8
age int
struct TUser {
name &i8
age int
capitalizedField f32
// fields in V must be lower case
// fields in V must be lower case
}

// struct names have to be capitalized in V
struct Small {
struct Small {
foo int
}
struct Small3 {

struct Small3 {
foo int
}
struct Small2 {

struct Small2 {
struct_field Small
// make sure field types are capitalized
struct_field_ptr &Small
// make sure field types are capitalized
struct_field_ptr &Small
struct_field_ptr2 &&Small3
}

fn small_fn(param Small) {
// make sure arg types are capitalized

// make sure arg types are capitalized
}

type Angle_t = u32

enum Color {
red
green
Expand All @@ -61,29 +65,16 @@ fn x(pi int) {
@[export: 'weapon_keys']
const weapon_keys = [0, 0, 0]!



fn sizeof_array() {
x := 10
c := sizeof(x)
n := (sizeof(weapon_keys) / sizeof(*weapon_keys))
}

@[export: 'checkcoord']
const checkcoord = [[3, 0, 2, 1]!
, [3, 0, 2, 0]!
, [3, 1, 2, 0]!
, [0, 0, 0, 0]!
, [2, 0, 2, 1]!
, [0, 0, 0, 0]!
, [3, 1, 3, 0]!
, [0, 0, 0, 0]!
, [2, 0, 3, 1]!
, [2, 1, 3, 1]!
, [2, 1, 3, 0]!
]!


const checkcoord = [[3, 0, 2, 1]!, [3, 0, 2, 0]!, [3, 1, 2, 0]!,
[0, 0, 0, 0]!, [2, 0, 2, 1]!, [0, 0, 0, 0]!, [3, 1, 3, 0]!,
[0, 0, 0, 0]!, [2, 0, 3, 1]!, [2, 1, 3, 1]!, [2, 1, 3, 0]!]!

@[c2v_variadic]
fn i_error(s ...&i8) {
Expand All @@ -94,11 +85,12 @@ fn sixtyfour() i64 {
return 64
}

union MyUnion {
union MyUnion {
a int
b i8
}
struct AnonStructTest {

struct AnonStructTest {
age int
foo struct {
bar int
Expand All @@ -107,15 +99,14 @@ struct AnonStructTest {

last_field f32
}
struct AnonStruct_105 {

struct AnonStruct_105 {
fff int
sss &i8
}

@[weak]
__global (
global_state_using_anon_struct AnonStruct_105
)
__global global_state_using_anon_struct AnonStruct_105

fn main() {
user := User{}
Expand All @@ -125,20 +116,20 @@ fn main() {
handle_user(user)
sf := sixtyfour()
C.printf(c'sixtyfour=%lld', sf)
// TODO
// struct User user2 = { .age = 30, .name = "Peter" };
// TODO
// struct User user2 = { .age = 30, .name = "Peter" };
user2 := TUser{}
user2.age = 30
user2.name = c'Peter'
handle_tuser(user2)
//
//
s := Small{}
// make sure struct inits are capitalized
s2 := Small {
foo: 10
}
// make sure struct inits are capitalized
s2 := Small{
foo: 10
}

x(PI)
// make sure cap consts stay capitalized
return
}
// make sure cap consts stay capitalized
return
}
Loading