The Zap programming language is a general-purpose systems language optimized for game development. This repository contains the source code for the Zap compiler, capable of parsing, analyzing, and generating code for the Zap programming language. The compiler generates C code as an intermediate representation, with plans to extend to other targets in the future.
- Automatic Memory Management: Zap uses mutable value semantics and reference counting for efficient memory management.
- Allocator Control: Provides fine-grained control over memory allocation via:
- Allocator parameters in type definitions.
- Scoped allocator context system.
- Direct allocator specification for individual allocations.
- Syntax: Utilizes significant whitespace and allows parentheses omission for streamlined function calls, e.g.,
print "Hello, World!"
. - Data Types:
- Numeric types (Int32, Int64, Float32, Float64).
- Vectors (Vec2, Vec3, Vec4).
- Strings, Booleans, Structs, and Arrays.
- Control Structures: Includes if-else, blocks, and loops with scoped variable control.
- Compilation Stages:
- Lexical Analysis.
- Parsing and AST generation.
- Semantic Analysis for type checking and validation.
- Intermediate Representation (IR) generation.
- C code generation.
- Optimization: Supports customizable optimization levels.
- Error Handling: Clear reporting for lexical, syntax, semantic, and generation errors.
- Haskell GHC for building the compiler.
- C compiler (e.g., GCC or Clang) for executing generated code.
- Clone the repository:
git clone <repository_url> cd zap-compiler
- Build the project:
stack build
- Run the tests:
stack test
To compile a Zap source file:
stack run zap-compiler -- input.zap output.c
The output will be a C file named output.c
.
Use a C compiler to compile and run the generated file:
gcc output.c -o program
./program
Tests for the Zap language and compiler are located in the tests/Zap
folder. These include:
- Syntax Tests: Validate correct parsing of language constructs.
- Semantic Tests: Verify type checking and semantic rules.
- Code Generation Tests: Ensure proper translation to C code.
To execute the test suite:
stack test
print "Hello, World!"
Hello, World!
print 1 + 1
print 2 * 2
print 4 / 2
2
4
2
let vec1 = Vec3(1.0, 2.0, 3.0)
let vec2 = Vec3 4.0, 5.0, 6.0
print (vec1 + vec2)
(5.000000, 7.000000, 9.000000)
type
Point = struct
x: f32
y: f32
let origin = Point 5.0, 2.5
print origin.x
5.000000
var n = 0
while n < 3:
print n
n += 1
0
1
2
fn add(x, y: i32): i32 =
x + y
print add(1, 2)
3
fn sumSquares(x, y: i32): i32 =
var sum = x * x
sum = sum + y * y
sum
print sumSquares(2, 2)
8
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes and open a pull request.
This project is licensed under the MIT License. See the LICENSE
file for details.