Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ tests/object/
**.exe

zlang-support/

**/.vscode/
104 changes: 0 additions & 104 deletions .vscode/settings.json

This file was deleted.

157 changes: 120 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,59 @@ A lightweight statically typed programming language that compiles to **x86_64 Li
```
zlang/
├── include/
│ ├── common/
│ │ ├── Colors.hpp
│ │ ├── Errors.hpp
│ │ ├── Logging.hpp
│ │ └── StringUtils.hpp
│ ├── support/
│ │ ├── CommandLine.hpp
│ │ └── File.hpp
│ ├── ast/
│ │ └── ASTNode.hpp
│ ├── lexer/
│ │ └── Lexer.hpp
│ ├── parser/
│ │ ├── ASTNode.hpp
│ │ ├── Parser.hpp
│ │ ── ScopeContext.hpp
│ └── Token.hpp
── codegen/
│ ├── CodegenLinux.hpp
── RegisterAllocator.hpp
── typechecker/
│ └── TypeChecker.hpp
│ │ ── ScopeContext.hpp
── typechecker/
│ └── TypeChecker.hpp
── codegen/
── CodeGen.hpp
── Canaries.hpp
│ └── RegisterAllocator.hpp
├── src/
│ ├── common/
│ │ ├── Logging.cpp
│ │ └── StringUtils.cpp
│ ├── support/
│ │ ├── CommandLine.cpp
│ │ └── File.cpp
│ ├── ast/
│ │ └── ASTNode.cpp
│ ├── lexer/
│ │ └── Lexer.cpp
│ ├── parser/
│ │ ├── ScopeContext.cpp
│ │ └── Parser.cpp
│ ├── codegen/
│ │ └── CodegenLinux.cpp
│ └── typechecker/
│ └── TypeChecker.cpp
│ ├── typechecker/
│ │ └── TypeChecker.cpp
│ └── codegen/
│ ├── CodeGenWindows.cpp
│ ├── CodeGenLinux.cpp
│ ├── CodeGenLLVM.cpp
│ └── RegisterAllocator.cpp
├── tests/
│ ├── zz/
│ | ├── [Various test categorized in folders]
├── examples/
│ ├── [current example we are working on]
├── main.cpp
├── Makefile
├── CMakeLists.txt
├── test_runner.py
├── LICENSE
└── README.md

```

---
Expand All @@ -52,9 +85,11 @@ zlang/
### Prerequisites

- Linux (x86_64)
- `gnu` assembler
- `gnu` (linux)
- `ml64.exe` (windows)
- `llc & clang` (LLVM)
- `g++` or `clang++` compiler
- `make` or `cmake` (for build automation)
- `cmake`

### Build Instructions

Expand All @@ -74,43 +109,92 @@ make

## 🔧 Usage

### On Linux when --format is x86_64-linux

- **Compile**

```bash
./zpiler <source_file.zz>
./zpiler --format x86_64-linux -o out.asm <source_file.zz>
```

This will:
This will:

1. Parse the input `.zz` file.
2. Typecheck the AST.
3. Generate `out.asm` containing x86_64 assembly.
4. Assemble and link the code to produce an ELF executable.
1. Parse the input`.zz` file.
2. Typecheck the AST.
3. Generate `out.asm` containing x86_64 assembly.

- **Assemble, Link and Run**

```bash
as out.asm -o out.o
ld out.o -o out
gcc out.o -o out
./out
```

---
### On Linux when --format is llvm-ir

## 🧠 Example Program (zlang)
- **Compile**

```zlang
let x: integer = 10;
let y: integer;
```bash
./zpiler --format llvm-ir -o out.ll <source_file.zz>
```

y = 13;
x = 11;
This will:

let msg: string = "Hello World";
let pi: double = 3.1415;
let precision: float = 0.0001f;
1. Parse the input`.zz` file.
2. Typecheck the AST.
3. Generate `out.ll` containing LLVM IR.

let a: uint32_t = 123;
let b: float32_t = 12.3f;
- **Assemble, Link and Run**

if (a > b + 10) {
// do something
```bash
llc -filetype=obj -o out.ll <llvm_out_path>
gcc out.o -o out -no-pie
./out
```

### On Windows when --format is x86_64-windows

- **Compile**

```bash
./zpiler --format x86_64-windows -o out.asm <source_file.zz>
```

This will:

1. Parse the input`.zz` file.
2. Typecheck the AST.
3. Generate `out.asm` containing x86_64 assembly.

- **Assemble, Link and Run**

```bash
ml64 /nologo /c .\out.asm
gcc out.o -o out
./out
```

---

## 🧠 Example Program (zlang)

```zlang
extern fn printf(fmt: string, ...) -> int32_t;

fn factorial(x: uint64_t) -> uint64_t{
fn multiply(x: uint64_t, y: uint64_t) -> uint64_t{
return x * y;
}
if(x <= 1){
return 1;
}else{
return multiply(x, factorial(x - 1));
}
}

fn main() {
printf("Factorial of 10: %d\n", factorial(10));
}
```

Expand Down Expand Up @@ -142,4 +226,3 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
**Mihir Patel**

Feel free to reach out for contributions, discussions, or collaborations!

21 changes: 10 additions & 11 deletions examples/functions.zz
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@

extern fn printf(fmt: string, ...) -> int32_t;

let a: int64_t = 10;
let f: float = 20.5;
let u: uint8_t = 10;

fn print_int(x: int64_t) -> none {
printf("%d\n", x);
fn factorial(x: uint64_t) -> uint64_t{
fn multiply(x: uint64_t, y: uint64_t) -> uint64_t{
return x * y;
}
if(x <= 1){
return 1;
}else{
return multiply(x, factorial(x - 1));
}
}

fn main() {
if (a > 100) {
let res: int64_t = a * 2;
print_int(res);
}
printf("Factorial of 10: %d\n", factorial(10));
}