GAMEEEEEEEEEE

This commit is contained in:
Masashi 2025-12-17 13:16:53 +05:30
commit e0a42d0262
45 changed files with 2771 additions and 782 deletions

136
tests/codegen/INDEX.md Normal file
View file

@ -0,0 +1,136 @@
# Codegen Tests Index
Quick reference guide for all codegen tests.
## 📋 Test Overview
| Test | Type | Features | Lines |
|------|------|----------|-------|
| `test_executable` | Basic | Integer math, functions | 4 |
| `test_array_simple` | Arrays | Literals, indexing, pointers | 4 |
| `test_enum_simple` | Enums | Variants, discriminant | 2 |
| `test_comprehensive` | Mixed | All basic types, structs | 18 |
| `test_all_types` | Coverage | Complete type test | 16 |
## 🚀 Quick Commands
### Generate C from all tests
```bash
for f in tests/codegen/*.sui; do
./target/debug/suicmez "$f"
done
```
### Compile all tests
```bash
for f in tests/codegen/*.c; do
gcc "$f" -o "/tmp/$(basename ${f%.c})"
done
```
### Run all tests
```bash
for exe in /tmp/test_*; do
echo "=== $(basename $exe) ==="
"$exe" 2>&1
done
```
### Run single test
```bash
./target/debug/suicmez tests/codegen/test_comprehensive.sui
gcc tests/codegen/test_comprehensive.c -o /tmp/test
/tmp/test
```
## 📖 Documentation Files
- **README.md** - Full documentation of all tests
- **TESTING.md** - How to run and debug tests
- **SUMMARY.md** - Organization and cleanup summary
- **INDEX.md** - This file
## 🎯 Test Selection Guide
**I want to test:**
- **Simple execution** → Use `test_executable`
- **Arrays** → Use `test_array_simple`
- **Enums** → Use `test_enum_simple`
- **Everything together** → Use `test_comprehensive`
- **All types** → Use `test_all_types`
## ✅ Verification Checklist
Before pushing:
- [ ] All `.sui` files transpile: `for f in tests/codegen/*.sui; do suicmez "$f" > /dev/null; done`
- [ ] All `.c` files compile: `for f in tests/codegen/*.c; do gcc "$f" -c; done`
- [ ] All tests execute: `for f in tests/codegen/*.c; do gcc "$f" -o /tmp/t && /tmp/t > /dev/null; done`
- [ ] Debug output works: `gcc tests/codegen/test_comprehensive.c -o /tmp/t && /tmp/t | head -3`
## 🔧 Debug Features
Each test includes debug output:
```
| <statement> |
<value>
```
Example:
```
| int x = 10 |
10
```
Format specifiers are automatic:
- `%d` - integers
- `%f` - floats
- `%s` - strings
- `%p` - pointers
## 📦 File Organization
```
tests/codegen/
├── test_executable.sui ← Source
├── test_executable.c ← Generated
├── test_array_simple.sui
├── test_array_simple.c
├── test_enum_simple.sui
├── test_enum_simple.c
├── test_comprehensive.sui
├── test_comprehensive.c
├── test_all_types.sui
├── test_all_types.c
├── README.md ← Documentation
├── TESTING.md
├── SUMMARY.md
└── INDEX.md ← This file
```
Each `.sui` file has a corresponding `.c` file containing the generated C code with debug output.
## 🎓 Learning Path
1. Start with `test_executable` - understand basic code generation
2. Move to `test_array_simple` - learn array handling
3. Check `test_enum_simple` - understand enum codegen
4. Study `test_comprehensive` - see everything together
5. Review `test_all_types` - verify complete coverage
## 💡 Tips
- Look at generated `.c` files to understand transpilation
- Run tests with output redirection: `./test 2>&1 | less`
- Compare `.sui` and `.c` files side by side
- Check debug output for execution trace
- Modify `.sui` files to experiment with codegen
## 📞 Getting Help
See **TESTING.md** for:
- Common issues and solutions
- Compilation troubleshooting
- Debug output interpretation
- Verification procedures

201
tests/codegen/README.md Normal file
View file

@ -0,0 +1,201 @@
# Codegen Tests
This directory contains test cases for the suicmez compiler's code generation (codegen) phase. Each test demonstrates different language features and their corresponding C output.
## Test Files
### Basic Tests
#### `test_executable.sui` / `test_executable.c`
**Purpose:** Simple executable test with basic integer arithmetic
**Features:**
- Basic function definitions
- Integer variables and operations
- Return statements
**Run:**
```bash
suicmez tests/codegen/test_executable.sui
gcc tests/codegen/test_executable.c -o test_executable
./test_executable
```
#### `test_array_simple.sui` / `test_array_simple.c`
**Purpose:** Test heap-allocated arrays with indexing
**Features:**
- Array literals: `[1, 2, 3]`
- Array indexing: `arr[0]`
- Arrays as pointers: `int*`
- C99 compound literals for array initialization
**Output:**
```
| int* arr = (int[]){1, 2, 3} |
0x...
| int x = arr[0] |
1
```
#### `test_enum_simple.sui` / `test_enum_simple.c`
**Purpose:** Test enum types with discriminant + union pattern
**Features:**
- Enum variants
- Discriminant field for pattern matching
- Union-based storage for variant data
- Proper struct ordering
**Generated Structures:**
```c
struct Color_Red { };
struct Color_Green { };
struct Color_Blue { };
struct Color_union {
struct Color_Red red;
struct Color_Green green;
struct Color_Blue blue;
};
struct Color {
int discriminant;
struct Color_union data;
};
```
### Comprehensive Tests
#### `test_comprehensive.sui` / `test_comprehensive.c`
**Purpose:** Combined test of all major features
**Features:**
- Stack-allocated types: `int`, `float`, `bool`
- Heap-allocated types: arrays, strings
- Struct instantiation and field access
- Enum creation and initialization
- Function calls with multiple parameters
- Complex expressions
**Demonstrates:**
- Proper memory management distinctions
- Type conversions and initializations
- Debug output for each statement
**Run:**
```bash
suicmez tests/codegen/test_comprehensive.sui
gcc tests/codegen/test_comprehensive.c -o test_comprehensive
./test_comprehensive
```
**Sample Output:**
```
| int x = 10 |
10
| int y = 20 |
20
| int sum = add(x, y) |
30
| struct Point p = (struct Point){ .x = 5, .y = 15 } |
5
| int* arr = (int[]){1, 2, 3, 4, 5} |
0x7ffd6d57d0b0
| int first = arr[0] |
1
| char* msg = "hello" |
hello
```
#### `test_all_types.sui` / `test_all_types.c`
**Purpose:** Exhaustive test of all language types
**Features:**
- All primitive types: `int`, `float`, `bool`
- Strings with heap allocation
- Structs with multiple fields
- Enums with discriminant
- Arrays with indexing
**Run:**
```bash
suicmez tests/codegen/test_all_types.sui
gcc tests/codegen/test_all_types.c -o test_all_types
./test_all_types
```
## Memory Management Strategy
The codegen implements the following allocation strategy:
### Stack-Allocated (Copyable)
- `int`, `float`, `bool`, `char` - passed by value
- Structs (stored directly, not as pointers)
- Enums (stored directly with discriminant + union)
### Heap-Allocated (with Debug Visibility)
- **Arrays**: `int*` pointers created with C99 compound literals
```c
int* arr = (int[]){1, 2, 3, 4, 5};
```
- **Strings**: `char*` pointers to string constants
```c
char* msg = "hello";
```
## Debug Output Feature
Every statement generates debug output showing:
1. The executed statement
2. The resulting value (if applicable)
**Format:**
```c
int x = 10;
printf("| int x = 10 | \n %d\n", x);
```
**Output:**
```
| int x = 10 |
10
```
### Format Specifiers
The system automatically selects the correct format:
- `%d` - integers and booleans
- `%f` - floats
- `%s` - strings (char*)
- `%p` - pointers and arrays
## Compilation
All generated C code compiles with standard C99:
```bash
gcc -std=c99 <file>.c -o <executable>
```
## Running Tests Manually
```bash
# Generate C code from .sui file
suicmez tests/codegen/test_comprehensive.sui
# Compile the generated C code
gcc tests/codegen/test_comprehensive.c -o /tmp/test_comprehensive
# Run with debug output
/tmp/test_comprehensive
```
## Expected Behavior
All tests should:
1. ✅ Compile successfully with GCC
2. ✅ Generate valid C99 code
3. ✅ Execute without errors
4. ✅ Display debug output for each statement
5. ✅ Return correct exit codes
## Implementation Notes
- Structs are declared before enums in the output to avoid forward references
- Enum variant structs are generated before the union
- Array types use C99 compound literals for initialization
- All pointers are properly typed (not generic `void*`)
- Debug output uses `printf` with appropriate format specifiers

175
tests/codegen/SUMMARY.md Normal file
View file

@ -0,0 +1,175 @@
# Codegen Tests - Summary
## Organization
The codegen tests have been organized into a dedicated `tests/codegen/` subdirectory with the following structure:
```
tests/codegen/
├── README.md # Comprehensive documentation
├── TESTING.md # Testing instructions
├── SUMMARY.md # This file
├── test_executable.* # Simple arithmetic test
├── test_array_simple.* # Array operations test
├── test_enum_simple.* # Enum types test
├── test_comprehensive.* # Mixed types test
└── test_all_types.* # Type coverage test
```
Each test has two files:
- `.sui` - Source code in the suicmez language
- `.c` - Generated C code with debug output
## What Was Cleaned Up
Removed from root directory:
- `simple.sui`, `simple_test.*` - Incomplete test files
- `syntax_test.sui` - Old syntax exploration
- `test_affine.sui`, `test_binding.sui` - Abandoned tests
- `test_linear*.sui` - Linear type system experiments
- `test_scoping.sui`, `test_shadow.sui` - Scoping tests
- Other misc test files
Kept in root (valid existing tests):
- None - all valid tests are now organized in `tests/` subdirectories
## Test Files
### New Codegen Tests (in `tests/codegen/`)
1. **test_executable** - Minimal executable test
- Basic integer operations
- Function calls
- Return values
2. **test_array_simple** - Array functionality
- Array literals: `[1, 2, 3]`
- Array indexing: `arr[0]`
- Pointer-based array representation
3. **test_enum_simple** - Enumeration types
- Enum variants
- Discriminant + union pattern
- Struct ordering
4. **test_comprehensive** - Combined features
- All basic types (int, float, bool, string)
- Structs with multiple fields
- Enums with variants
- Arrays with complex operations
- Function calls with parameters
5. **test_all_types** - Complete type coverage
- Exhaustive test of all language types
- Demonstrates each type independently
- Shows debug output for each
### Existing Tests (preserved in `tests/`)
- `arrays.sui` - Additional array tests
- `basic_types.sui/c` - Basic type tests
- `control_flow.sui/c` - If/while/for loops
- `enums.sui` - Enum functionality
- `functions.sui/c` - Function definitions
- `generics_comprehensive.sui/c` - Generic types
- `import_tests/` - Module imports
- `pointers.sui` - Pointer operations
- `structs.sui/c` - Struct definitions
- `traits.sui/c` - Trait system
- And more...
## Features Demonstrated
### Memory Management
- Stack allocation: `int`, `float`, `bool`
- Heap allocation: arrays with pointers
- Strings as `char*`
- Structs and enums stack-allocated
### Language Features
- Variable declarations and assignments
- Function definitions and calls
- Struct literals with initialization
- Enum variants with discriminants
- Array literals and indexing
- Type conversions (implicit)
### Debug Output
Every statement generates debug output:
```c
int x = 10;
printf("| int x = 10 | \n %d\n", x);
// Output: | int x = 10 |
// 10
```
## Running the Tests
### Quick Check
```bash
cd /home/nafi/langjam/suicmez
for f in tests/codegen/*.sui; do
./target/debug/suicmez "$f" > /dev/null && echo "✓ $(basename $f)"
done
```
### Full Test Run
```bash
# Regenerate all C code
for f in tests/codegen/*.sui; do
./target/debug/suicmez "$f"
done
# Compile all tests
for f in tests/codegen/*.c; do
gcc "$f" -o "/tmp/$(basename ${f%.c})"
done
# Run all tests
for exe in /tmp/test_*; do
echo "Running $(basename $exe)..."
"$exe" 2>&1 | head -5
echo ""
done
```
### Individual Test
```bash
./target/debug/suicmez tests/codegen/test_comprehensive.sui
gcc tests/codegen/test_comprehensive.c -o /tmp/test
/tmp/test
```
## Quality Assurance
All codegen tests:
- ✅ Generate valid C99 code
- ✅ Compile with GCC without errors
- ✅ Execute successfully
- ✅ Display proper debug output
- ✅ Return correct values
- ✅ Include comprehensive documentation
## Documentation
- **README.md** - Feature overview and structure
- **TESTING.md** - How to run and troubleshoot tests
- **SUMMARY.md** - This file, organization summary
## Next Steps
To add new codegen tests:
1. Create `test_feature.sui` in `tests/codegen/`
2. Run transpiler: `./target/debug/suicmez tests/codegen/test_feature.sui`
3. Verify C code: `gcc tests/codegen/test_feature.c -c`
4. Test execution: `gcc tests/codegen/test_feature.c -o /tmp/test && /tmp/test`
5. Document in README.md
## Files Status
✅ All files properly organized
✅ All tests generate valid C code
✅ All tests compile and run
✅ Documentation complete
✅ Debug output working
✅ Root directory cleaned

115
tests/codegen/TESTING.md Normal file
View file

@ -0,0 +1,115 @@
# Running the Codegen Tests
## Quick Start
```bash
# Regenerate C code from all tests
for f in tests/codegen/*.sui; do
./target/debug/suicmez "$f"
done
# Compile and run a single test
gcc tests/codegen/test_comprehensive.c -o /tmp/test_comprehensive
/tmp/test_comprehensive
```
## Test Summary
| Test | Purpose | Input | Output |
|------|---------|-------|--------|
| `test_executable.sui` | Simple arithmetic | Basic int operations | Exit code with result |
| `test_array_simple.sui` | Array operations | Array literal and indexing | Debug output with array values |
| `test_enum_simple.sui` | Enum types | Enum variant creation | Discriminant value |
| `test_comprehensive.sui` | Mixed types | All basic + complex types | Full debug trace |
| `test_all_types.sui` | Type coverage | Every language type | Debug output for each |
## Running Individual Tests
### 1. Array Test
```bash
./target/debug/suicmez tests/codegen/test_array_simple.sui
gcc tests/codegen/test_array_simple.c -o /tmp/array_test
/tmp/array_test
```
**Expected Output:**
```
| int* arr = (int[]){1, 2, 3} |
0x...
| int x = arr[0] |
1
```
### 2. Enum Test
```bash
./target/debug/suicmez tests/codegen/test_enum_simple.sui
gcc tests/codegen/test_enum_simple.c -o /tmp/enum_test
/tmp/enum_test
```
**Expected Output:**
```
| struct Color c = (struct Color){ .discriminant = 0, .data = { .red = (struct Color_Red){} } } |
0
```
### 3. Comprehensive Test
```bash
./target/debug/suicmez tests/codegen/test_comprehensive.sui
gcc tests/codegen/test_comprehensive.c -o /tmp/comprehensive_test
/tmp/comprehensive_test
```
**Expected Output:** (full debug trace of all operations)
```
| int x = 10 |
10
| int y = 20 |
20
| int sum = add(x, y) |
30
... (more output)
```
## Debugging the Codegen
Each test's generated C file includes debug `printf` statements. To see what's happening:
1. **Look at the generated C code:**
```bash
cat tests/codegen/test_comprehensive.c
```
2. **Run with debug output:**
```bash
gcc tests/codegen/test_comprehensive.c -o /tmp/test
/tmp/test 2>&1 | head -20
```
3. **Check for compilation errors:**
```bash
gcc tests/codegen/test_comprehensive.c -c -o /tmp/test.o
```
## Verifying Correctness
All tests should:
- ✅ Generate valid C99 code
- ✅ Compile without warnings
- ✅ Execute without segmentation faults
- ✅ Show proper debug output for each statement
## Common Issues
### Test fails to compile
- Check that the `.sui` file was properly transpiled
- Regenerate with: `./target/debug/suicmez tests/codegen/test_name.sui`
### Debug output missing
- Ensure the printf statements are in the generated C code
- Check for return statements that might skip debug output
### Wrong values printed
- The debug output uses automatic format specifiers (`%d`, `%f`, `%s`, `%p`)
- Struct types print the first field value
- Arrays print the pointer address

View file

@ -0,0 +1,29 @@
struct Point
x: int,
y: int,
end
enum Result
Ok,
Error,
end
fn main() -> int do
# Test basic types
let int_val = 42;
let float_val = 3.14;
let bool_val = true;
let str_val = "Hello, World!";
# Test struct
let point = Point { x: 10, y: 20 };
# Test enum
let result = Result::Ok();
# Test array
let numbers = [1, 2, 3, 4, 5];
let first_num = numbers[0];
first_num
end

View file

@ -0,0 +1,5 @@
fn main() -> int do
let arr = [1, 2, 3];
let x = arr[0];
x
end

View file

@ -0,0 +1,36 @@
struct Point
x: int,
y: int,
end
enum Status
Ok,
Error,
end
fn add(a: int, b: int) -> int do
a + b
end
fn main() -> int do
# Test stack-allocated types (copyable)
let x = 10;
let y = 20;
let sum = add(x, y);
# Test struct (stack-allocated for now, but declared as such in language)
let p = Point { x: 5, y: 15 };
let point_sum = p.x + p.y;
# Test enum (stack-allocated for now)
let status = Status::Ok();
# Test array (heap-allocated)
let arr = [1, 2, 3, 4, 5];
let first = arr[0];
# Test string (heap-allocated)
let msg = "hello";
sum + point_sum + first
end

View file

@ -0,0 +1,10 @@
enum Color
Red,
Green,
Blue,
end
fn main() -> int do
let c = Color::Red();
0
end

View file

@ -0,0 +1,5 @@
fn main() -> int do
let x = 10;
let y = 20;
x + y
end