Production-ready C code generator for the FreeLang programming language with comprehensive language features



Production-ready C code generator for the FreeLang programming language.
A high-performance compiler backend that transforms FreeLang abstract syntax trees (AST) into optimized C code, combining FreeLang's type safety with C's portability and performance.
``bash`
npm install @freelang/backend-c
`bashInstall FreeLang and C backend
npm install -g @freelang/freelang @freelang/backend-c
$3
`typescript
import { compileToC } from '@freelang/backend-c';
import { Parser, TypeChecker } from '@freelang/freelang';const source =
;// Parse and type-check
const ast = new Parser(source).parse();
const typed = new TypeChecker().check(ast);
// Generate C code
const cCode = compileToC(typed);
console.log(cCode);
`๐ Documentation
- Getting Started Guide - Complete usage instructions
- C Runtime Library - FreeLang C runtime functions
- Examples - 10 complete example programs
๐ 10 Complete Examples
1. hello-world - Basic I/O and functions
2. functions - Function definitions and calls
3. structs - Struct definitions and usage
4. fibonacci - Recursive functions
5. pattern-matching - if-let, match expressions, Option/Result
6. generics - Generic functions and structs with type parameters
7. error-handling - Result types, error propagation
8. stdlib - 45+ built-in functions demonstration
9. memory-optimization - Allocator strategies, manual optimization
10. performance - Inline, constant folding, loop optimization
Run any example:
`bash
freelang build examples/pattern-matching/main.free --backend c
gcc -O2 pattern-matching.c -o pattern-matching -lm
./pattern-matching
`๐ง Configuration
$3
`typescript
import { CCodeGenerator } from '@freelang/backend-c';const generator = new CCodeGenerator({
allocatorStrategy: 'arena' // Fast, short-lived allocations
// or 'stack' (stack-based, fastest)
// or 'heap' (malloc/free, flexible)
// or 'pool' (fixed-size pool, predictable)
});
`$3
`bash
Debug build (with assertions and symbols)
gcc main.c -o main -g -DDEBUGOptimized build (O2)
gcc -O2 main.c -o main -lmSize-optimized build (Os)
gcc -Os main.c -o main -lmFastest build (O3)
gcc -O3 main.c -o main -lm
`๐ Features Summary
| Feature | Status | Examples |
|---------|--------|----------|
| Pattern Matching | โ
Complete | pattern-matching/ |
| Generic Types | โ
Complete | generics/ |
| Error Handling | โ
Complete | error-handling/ |
| Standard Library | โ
Complete | stdlib/ |
| Memory Optimization | โ
Complete | memory-optimization/ |
| Performance Optimization | โ
Complete | performance/ |
| Total | โ
Complete | 10 programs |
๐งช Testing
`bash
Run all tests
npm testRun specific test file
npm test -- codegen.test.tsWatch mode
npm run test:watchCoverage report
npm run test:coverage
`๐๏ธ Project Structure
`
src/
โโ index.ts # Plugin interface
โโ codegen.ts # Main code generator (1,100+ lines)
โโ stdlib.ts # 45+ built-in functions
โโ types.ts # Type mapping
โโ templates/ # C code templatestests/
โโ codegen.test.ts # Code generation tests
โโ types.test.ts # Type mapping tests
โโ patterns.test.ts # Pattern matching tests
โโ generics.test.ts # Generic type tests
โโ error-handling.test.ts # Error handling tests
โโ stdlib.test.ts # Standard library tests
โโ memory-optimization.test.ts
โโ performance.test.ts
โโ integration.test.ts # Integration tests
examples/
โโ hello-world/
โโ functions/
โโ structs/
โโ fibonacci/
โโ pattern-matching/
โโ generics/
โโ error-handling/
โโ stdlib/
โโ memory-optimization/
โโ performance/
`๐ Statistics
| Metric | Value |
|--------|-------|
| Code Lines (src/) | 1,100+ |
| Built-in Functions | 45+ |
| Example Programs | 10 |
| Test Files | 9 |
| Documentation | Complete |
๐ Code Generation Pipeline
`
FreeLang Source Code
โ
Parser (FreeLang)
โ
Type Checker (FreeLang)
โ
AST
โ
CCodeGenerator โ [This Package]
โ
C Code
โ
C Compiler (gcc/clang)
โ
Native Executable
`๐ Built-in Functions (45+)
String Functions (8): strlen, strcat, strcmp, strcpy, substring, uppercase, lowercase, trim
Array Functions (10): first, last, take, drop, flatten, unique, compact, count, sum, average
Math Functions (12): min, max, gcd, lcm, clamp, sqrt, pow, floor, ceil, round, abs, exp
Type Functions (4): typeof, sizeof, is_null, is_empty
System Functions (3): sleep, timestamp, now
Memory Functions (4): malloc_c, free_c, calloc_c, realloc_c
Utility Functions (4): panic, unwrap, assert
See stdlib.ts for complete list and mappings.
๐ค Contributing
Contributions are welcome! Please:
1. Fork the repository
2. Create a feature branch
3. Add tests for new functionality
4. Ensure all tests pass (
npm test`)MIT - See LICENSE file
- FreeLang - The FreeLang programming language
- FreeLang Zig Backend - Zig code generator
- C Standard Library - C reference documentation
- ๐ Full Documentation
- ๐ Issue Tracker
- ๐ฌ Discussions
---
Version 1.0.0 - Production Ready โ
Made with โค๏ธ for the FreeLang community