Skip to content

Commit cc69c51

Browse files
committed
Enable Grammar2 JSON parsing path and add CI fmt/clippy checks
1 parent 78fc6da commit cc69c51

13 files changed

Lines changed: 5446 additions & 2540 deletions

File tree

.github/workflows/ci.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Rust CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
lint:
14+
name: fmt + clippy
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v4
19+
20+
- name: Setup Rust toolchain
21+
uses: dtolnay/rust-toolchain@stable
22+
with:
23+
components: rustfmt, clippy
24+
25+
- name: Cache cargo artifacts
26+
uses: Swatinem/rust-cache@v2
27+
28+
- name: Check formatting
29+
run: cargo fmt --all -- --check
30+
31+
- name: Run clippy
32+
run: cargo clippy --workspace --all-targets

crates/compiler/tests/integration_memory_test.rs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use zyntax_compiler::cranelift_backend::CraneliftBackend;
22
use zyntax_compiler::hir::{
3-
HirConstant, HirFunction, HirFunctionSignature, HirId, HirInstruction, HirParam,
4-
HirTerminator, HirType, HirValueKind, ParamAttributes,
3+
HirConstant, HirFunction, HirFunctionSignature, HirId, HirInstruction, HirParam, HirTerminator,
4+
HirType, HirValueKind, ParamAttributes,
55
};
6-
use zyntax_typed_ast::{InternedString, arena::AstArena};
6+
use zyntax_typed_ast::{arena::AstArena, InternedString};
77

88
/// Helper to create an interned string for tests
99
fn create_test_string(s: &str) -> InternedString {
@@ -14,13 +14,14 @@ fn create_test_string(s: &str) -> InternedString {
1414
/// Compile a HIR function and return the backend + function pointer
1515
fn compile_and_get_ptr(func: HirFunction) -> (CraneliftBackend, Option<*const u8>) {
1616
let func_id = func.id;
17-
let mut backend = CraneliftBackend::new()
18-
.expect("Failed to create Cranelift backend");
19-
backend.compile_function(func_id, &func)
17+
let mut backend = CraneliftBackend::new().expect("Failed to create Cranelift backend");
18+
backend
19+
.compile_function(func_id, &func)
2020
.expect("Failed to compile function");
2121

2222
// Manually finalize and get function pointer
23-
backend.finalize_definitions()
23+
backend
24+
.finalize_definitions()
2425
.expect("Failed to finalize definitions");
2526

2627
let func_ptr = backend.get_function_ptr(func_id);
@@ -60,7 +61,10 @@ fn test_memory_roundtrip_execution() {
6061
let param_x = func.create_value(HirType::I32, HirValueKind::Parameter(0));
6162

6263
// Alloca i32
63-
let ptr = func.create_value(HirType::Ptr(Box::new(HirType::I32)), HirValueKind::Instruction);
64+
let ptr = func.create_value(
65+
HirType::Ptr(Box::new(HirType::I32)),
66+
HirValueKind::Instruction,
67+
);
6468
let alloca = HirInstruction::Alloca {
6569
result: ptr,
6670
ty: HirType::I32,
@@ -90,7 +94,9 @@ fn test_memory_roundtrip_execution() {
9094
block.add_instruction(alloca);
9195
block.add_instruction(store);
9296
block.add_instruction(load);
93-
block.set_terminator(HirTerminator::Return { values: vec![result] });
97+
block.set_terminator(HirTerminator::Return {
98+
values: vec![result],
99+
});
94100

95101
let (_backend, func_ptr) = compile_and_get_ptr(func);
96102
let func_ptr = func_ptr.expect("Failed to get function pointer");
@@ -139,7 +145,10 @@ fn test_memory_i64_execution() {
139145
let param_x = func.create_value(HirType::I64, HirValueKind::Parameter(0));
140146

141147
// Alloca i64
142-
let ptr = func.create_value(HirType::Ptr(Box::new(HirType::I64)), HirValueKind::Instruction);
148+
let ptr = func.create_value(
149+
HirType::Ptr(Box::new(HirType::I64)),
150+
HirValueKind::Instruction,
151+
);
143152
let alloca = HirInstruction::Alloca {
144153
result: ptr,
145154
ty: HirType::I64,
@@ -169,7 +178,9 @@ fn test_memory_i64_execution() {
169178
block.add_instruction(alloca);
170179
block.add_instruction(store);
171180
block.add_instruction(load);
172-
block.set_terminator(HirTerminator::Return { values: vec![result] });
181+
block.set_terminator(HirTerminator::Return {
182+
values: vec![result],
183+
});
173184

174185
let (_backend, func_ptr) = compile_and_get_ptr(func);
175186
let func_ptr = func_ptr.expect("Failed to get function pointer");
@@ -218,7 +229,10 @@ fn test_memory_f64_execution() {
218229
let param_x = func.create_value(HirType::F64, HirValueKind::Parameter(0));
219230

220231
// Alloca f64
221-
let ptr = func.create_value(HirType::Ptr(Box::new(HirType::F64)), HirValueKind::Instruction);
232+
let ptr = func.create_value(
233+
HirType::Ptr(Box::new(HirType::F64)),
234+
HirValueKind::Instruction,
235+
);
222236
let alloca = HirInstruction::Alloca {
223237
result: ptr,
224238
ty: HirType::F64,
@@ -248,7 +262,9 @@ fn test_memory_f64_execution() {
248262
block.add_instruction(alloca);
249263
block.add_instruction(store);
250264
block.add_instruction(load);
251-
block.set_terminator(HirTerminator::Return { values: vec![result] });
265+
block.set_terminator(HirTerminator::Return {
266+
values: vec![result],
267+
});
252268

253269
let (_backend, func_ptr) = compile_and_get_ptr(func);
254270
let func_ptr = func_ptr.expect("Failed to get function pointer");
@@ -257,7 +273,7 @@ fn test_memory_f64_execution() {
257273
let func_typed: extern "C" fn(f64) -> f64 = unsafe { std::mem::transmute(func_ptr) };
258274

259275
// Test with various values
260-
assert_eq!(func_typed(3.14159), 3.14159);
276+
assert_eq!(func_typed(std::f64::consts::PI), std::f64::consts::PI);
261277
assert_eq!(func_typed(0.0), 0.0);
262278
assert_eq!(func_typed(-123.456), -123.456);
263279
assert_eq!(func_typed(f64::MAX), f64::MAX);
@@ -309,7 +325,10 @@ fn test_multiple_allocations_execution() {
309325
let param_b = func.create_value(HirType::I32, HirValueKind::Parameter(1));
310326

311327
// Alloca for a
312-
let ptr_a = func.create_value(HirType::Ptr(Box::new(HirType::I32)), HirValueKind::Instruction);
328+
let ptr_a = func.create_value(
329+
HirType::Ptr(Box::new(HirType::I32)),
330+
HirValueKind::Instruction,
331+
);
313332
let alloca_a = HirInstruction::Alloca {
314333
result: ptr_a,
315334
ty: HirType::I32,
@@ -318,7 +337,10 @@ fn test_multiple_allocations_execution() {
318337
};
319338

320339
// Alloca for b
321-
let ptr_b = func.create_value(HirType::Ptr(Box::new(HirType::I32)), HirValueKind::Instruction);
340+
let ptr_b = func.create_value(
341+
HirType::Ptr(Box::new(HirType::I32)),
342+
HirValueKind::Instruction,
343+
);
322344
let alloca_b = HirInstruction::Alloca {
323345
result: ptr_b,
324346
ty: HirType::I32,
@@ -431,7 +453,10 @@ fn test_memory_update_execution() {
431453
let param_x = func.create_value(HirType::I32, HirValueKind::Parameter(0));
432454

433455
// Alloca i32
434-
let ptr = func.create_value(HirType::Ptr(Box::new(HirType::I32)), HirValueKind::Instruction);
456+
let ptr = func.create_value(
457+
HirType::Ptr(Box::new(HirType::I32)),
458+
HirValueKind::Instruction,
459+
);
435460
let alloca = HirInstruction::Alloca {
436461
result: ptr,
437462
ty: HirType::I32,
@@ -493,7 +518,9 @@ fn test_memory_update_execution() {
493518
block.add_instruction(add);
494519
block.add_instruction(store2);
495520
block.add_instruction(load2);
496-
block.set_terminator(HirTerminator::Return { values: vec![result] });
521+
block.set_terminator(HirTerminator::Return {
522+
values: vec![result],
523+
});
497524

498525
let (_backend, func_ptr) = compile_and_get_ptr(func);
499526
let func_ptr = func_ptr.expect("Failed to get function pointer");

crates/haxe_zyntax_runtime/src/lib.rs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
1313
extern crate libc;
1414

15-
use zyntax_plugin_macros::{runtime_plugin, runtime_export};
16-
use zyntax_compiler::zrtl::{DynamicValue, TypeId, TypeCategory};
15+
use zyntax_compiler::zrtl::{DynamicValue, TypeCategory, TypeId};
16+
use zyntax_plugin_macros::{runtime_export, runtime_plugin};
1717

1818
// Declare this as the Haxe runtime plugin
1919
runtime_plugin! {
@@ -40,7 +40,7 @@ const HEADER_SIZE: isize = 1;
4040
/// - value: The string to print (length-prefixed Haxe string)
4141
/// - pos_info: Optional position info string (e.g., "Test.hx:10:")
4242
#[runtime_export("$haxe$trace")]
43-
pub extern "C" fn haxe_trace(value: *const i32, pos_info: *const i32) {
43+
pub unsafe extern "C" fn haxe_trace(value: *const i32, pos_info: *const i32) {
4444
unsafe {
4545
// Print position info if provided
4646
if !pos_info.is_null() {
@@ -77,13 +77,13 @@ pub extern "C" fn haxe_trace(value: *const i32, pos_info: *const i32) {
7777
/// Simple trace function - just prints the string with newline
7878
/// Used when no position info is available
7979
#[runtime_export("$haxe$trace$simple")]
80-
pub extern "C" fn haxe_trace_simple(value: *const i32) {
80+
pub unsafe extern "C" fn haxe_trace_simple(value: *const i32) {
8181
haxe_trace(value, core::ptr::null());
8282
}
8383

8484
/// Trace an integer value directly
8585
#[runtime_export("$haxe$trace$int")]
86-
pub extern "C" fn haxe_trace_int(value: i32, pos_info: *const i32) {
86+
pub unsafe extern "C" fn haxe_trace_int(value: i32, pos_info: *const i32) {
8787
unsafe {
8888
print_pos_info(pos_info);
8989
libc::printf(b"%d\n\0".as_ptr() as *const i8, value);
@@ -92,7 +92,7 @@ pub extern "C" fn haxe_trace_int(value: i32, pos_info: *const i32) {
9292

9393
/// Trace a float value directly
9494
#[runtime_export("$haxe$trace$float")]
95-
pub extern "C" fn haxe_trace_float(value: f64, pos_info: *const i32) {
95+
pub unsafe extern "C" fn haxe_trace_float(value: f64, pos_info: *const i32) {
9696
unsafe {
9797
print_pos_info(pos_info);
9898
libc::printf(b"%g\n\0".as_ptr() as *const i8, value);
@@ -101,7 +101,7 @@ pub extern "C" fn haxe_trace_float(value: f64, pos_info: *const i32) {
101101

102102
/// Trace a boolean value directly
103103
#[runtime_export("$haxe$trace$bool")]
104-
pub extern "C" fn haxe_trace_bool(value: i32, pos_info: *const i32) {
104+
pub unsafe extern "C" fn haxe_trace_bool(value: i32, pos_info: *const i32) {
105105
unsafe {
106106
print_pos_info(pos_info);
107107
if value != 0 {
@@ -154,9 +154,9 @@ unsafe fn print_haxe_string(str_ptr: *const i32) {
154154
#[repr(C)]
155155
#[derive(Copy, Clone)]
156156
pub struct HaxeString {
157-
pub ptr: *mut u8, // Pointer to string data (UTF-8)
158-
pub len: usize, // Length in bytes
159-
pub cap: usize, // Capacity in bytes
157+
pub ptr: *mut u8, // Pointer to string data (UTF-8)
158+
pub len: usize, // Length in bytes
159+
pub cap: usize, // Capacity in bytes
160160
}
161161

162162
/// Trace any Dynamic value - dispatches based on runtime type
@@ -168,7 +168,7 @@ pub struct HaxeString {
168168
/// - dynamic_ptr: Pointer to a DynamicValue struct
169169
/// - pos_info: Optional position info string (length-prefixed Haxe string)
170170
#[runtime_export("$haxe$trace$any")]
171-
pub extern "C" fn haxe_trace_any(dynamic_ptr: *const DynamicValue, pos_info: *const i32) {
171+
pub unsafe extern "C" fn haxe_trace_any(dynamic_ptr: *const DynamicValue, pos_info: *const i32) {
172172
unsafe {
173173
print_pos_info(pos_info);
174174

@@ -229,30 +229,28 @@ pub extern "C" fn haxe_trace_any(dynamic_ptr: *const DynamicValue, pos_info: *co
229229
}
230230
}
231231
}
232-
TypeCategory::UInt => {
233-
match type_id {
234-
t if t == TypeId::U8 => {
235-
if let Some(&v) = dynamic.as_ref::<u8>() {
236-
libc::printf(b"%u\n\0".as_ptr() as *const i8, v as u32);
237-
}
232+
TypeCategory::UInt => match type_id {
233+
t if t == TypeId::U8 => {
234+
if let Some(&v) = dynamic.as_ref::<u8>() {
235+
libc::printf(b"%u\n\0".as_ptr() as *const i8, v as u32);
238236
}
239-
t if t == TypeId::U16 => {
240-
if let Some(&v) = dynamic.as_ref::<u16>() {
241-
libc::printf(b"%u\n\0".as_ptr() as *const i8, v as u32);
242-
}
237+
}
238+
t if t == TypeId::U16 => {
239+
if let Some(&v) = dynamic.as_ref::<u16>() {
240+
libc::printf(b"%u\n\0".as_ptr() as *const i8, v as u32);
243241
}
244-
t if t == TypeId::U32 => {
245-
if let Some(&v) = dynamic.as_ref::<u32>() {
246-
libc::printf(b"%u\n\0".as_ptr() as *const i8, v);
247-
}
242+
}
243+
t if t == TypeId::U32 => {
244+
if let Some(&v) = dynamic.as_ref::<u32>() {
245+
libc::printf(b"%u\n\0".as_ptr() as *const i8, v);
248246
}
249-
_ => {
250-
if let Some(&v) = dynamic.as_ref::<u64>() {
251-
libc::printf(b"%llu\n\0".as_ptr() as *const i8, v);
252-
}
247+
}
248+
_ => {
249+
if let Some(&v) = dynamic.as_ref::<u64>() {
250+
libc::printf(b"%llu\n\0".as_ptr() as *const i8, v);
253251
}
254252
}
255-
}
253+
},
256254
TypeCategory::Float => {
257255
if type_id == TypeId::F32 {
258256
if let Some(&v) = dynamic.as_ref::<f32>() {
@@ -294,10 +292,7 @@ pub extern "C" fn haxe_trace_any(dynamic_ptr: *const DynamicValue, pos_info: *co
294292
}
295293
TypeCategory::Struct | TypeCategory::Class => {
296294
// For struct/class, show address
297-
libc::printf(
298-
b"<Object@%p>\n\0".as_ptr() as *const i8,
299-
dynamic.value_ptr,
300-
);
295+
libc::printf(b"<Object@%p>\n\0".as_ptr() as *const i8, dynamic.value_ptr);
301296
}
302297
TypeCategory::Enum => {
303298
libc::printf(b"[Enum]\n\0".as_ptr() as *const i8);
@@ -313,18 +308,15 @@ pub extern "C" fn haxe_trace_any(dynamic_ptr: *const DynamicValue, pos_info: *co
313308
}
314309
_ => {
315310
// Unknown type - print address
316-
libc::printf(
317-
b"<Dynamic@%p>\n\0".as_ptr() as *const i8,
318-
dynamic.value_ptr,
319-
);
311+
libc::printf(b"<Dynamic@%p>\n\0".as_ptr() as *const i8, dynamic.value_ptr);
320312
}
321313
}
322314
}
323315
}
324316

325317
/// Trace any Dynamic value without position info
326318
#[runtime_export("$haxe$trace$any$simple")]
327-
pub extern "C" fn haxe_trace_any_simple(dynamic_ptr: *const DynamicValue) {
319+
pub unsafe extern "C" fn haxe_trace_any_simple(dynamic_ptr: *const DynamicValue) {
328320
haxe_trace_any(dynamic_ptr, core::ptr::null());
329321
}
330322

@@ -335,8 +327,8 @@ pub extern "C" fn haxe_trace_any_simple(dynamic_ptr: *const DynamicValue) {
335327
// These exports allow this library to be loaded dynamically as a ZRTL plugin.
336328
// The `_zrtl_info` and `_zrtl_symbols` are required by the ZrtlPlugin loader.
337329

338-
use zyntax_compiler::zrtl::{ZrtlInfo, ZrtlSymbol, ZRTL_VERSION};
339330
use std::ffi::c_char;
331+
use zyntax_compiler::zrtl::{ZrtlInfo, ZrtlSymbol, ZRTL_VERSION};
340332

341333
/// Plugin info - required for ZRTL dynamic loading
342334
#[no_mangle]

crates/runtime/src/lib.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
extern crate libc;
1616

17-
use zyntax_plugin_macros::{runtime_plugin, runtime_export};
17+
use zyntax_plugin_macros::{runtime_export, runtime_plugin};
1818

1919
// Declare this as the standard library runtime plugin
2020
runtime_plugin! {
@@ -74,21 +74,19 @@ pub extern "C" fn print_cstr(ptr: *const u8) {
7474

7575
/// Print a string with known length
7676
#[runtime_export("print_str")]
77-
pub extern "C" fn print_str(ptr: *const u8, len: i32) {
77+
pub unsafe extern "C" fn print_str(ptr: *const u8, len: i32) {
7878
if ptr.is_null() || len < 0 {
7979
return;
8080
}
81-
unsafe {
82-
for i in 0..len {
83-
libc::putchar(*ptr.offset(i as isize) as i32);
84-
}
85-
libc::fflush(core::ptr::null_mut());
81+
for i in 0..len {
82+
libc::putchar(*ptr.offset(i as isize) as i32);
8683
}
84+
libc::fflush(core::ptr::null_mut());
8785
}
8886

8987
/// Print a string with newline
9088
#[runtime_export("println_str")]
91-
pub extern "C" fn println_str(ptr: *const u8, len: i32) {
89+
pub unsafe extern "C" fn println_str(ptr: *const u8, len: i32) {
9290
print_str(ptr, len);
9391
print_newline();
9492
}
@@ -99,4 +97,3 @@ pub extern "C" fn println_i32(value: i32) {
9997
print_i32(value);
10098
print_newline();
10199
}
102-

0 commit comments

Comments
 (0)