Code-Lang π
Code-Lang is a modern, interpreted programming language written in Go. It began as an implementation following the excellent book "Writing An Interpreter In Go" by Thorsten Ball, and has since evolved with additional features and custom extensions.
[!IMPORTANT]
Status: Code-Lang is a passion project and is currently under active development. While the core language is functional, it is not production-ready. If you intend to use this in a production environment, significant work, security audits, and optimizations are required.
β¨ Features
- Rich Type System:
- Integers and Floats
- Strings and Characters
- Booleans
- Arrays (e.g.,
[1, 2, 3])
- Hashes/Dictionaries (e.g.,
{"name": "Code-Lang"})
- Structs: Custom data structures with default values and member access.
- First-Class Functions: Function literals, closures, and higher-order functions.
- Control Flow:
if-elseif-else expressions (everything is an expression!).
while loops for simple iteration.
for loops for structured iteration.
- Support for Comments: Single-line (
#) and multi-line (/* */).
- Standard Operators:
- Arithmetic:
+, -, *, /, % (Modulo)
- Advanced:
** (Power), // (Floor Division), = (Assignment)
- Comparison:
==, !=, <, >, <=, >=
- Logical:
! (Negation)
- Built-in Functions:
print, printf, typeof, len, push, and more.
- Module System: Import other
.cl files or built-in modules using import "module".
- Member Access: Dot notation (
obj.prop) for Hashes, Modules, and Servers.
- Networking: Built-in
http client (GET, POST, etc.) and net.server for creating web servers.
- JSON Support: Built-in
json.parse() and json.stringify().
- Standard Library: Go-backed modules for
math, strings, time, hash, os, json, and net.
- Compound Assignment: Supports
+=, -=, *=, /=, etc.
- REPL: Interactive shell with precise line/column error tracking.
- File Execution: Run scripts with the
.cl extension.
π Getting Started
Prerequisites
- Go (version 1.25.3 or higher recommended)
Installation
Option 1: Using go install (Recommended)
You can install the code-lang binary directly to your $GOPATH/bin:
go install github.com/walonCode/code-lang@latest
Option 2: Pre-built Binaries
Head over to the Releases section to download path-ready binaries for Windows, macOS, and Linux.
Option 3: From Source
Clone and build manually:
git clone https://github.com/walonCode/code-lang.git
cd code-lang
go build -o code-lang main.go
Running the REPL
Start the interactive shell by running:
go run main.go
Running a Script
You can execute a Code-Lang script by passing the filename as an argument:
go run main.go hello.cl
π Language Syntax at a Glance
Variables & Functions
let age = 25;
let name = "Developer";
let isLearning = true;
let add = fn(a, b) {
return a + b;
};
print(add(10, 15)); // Output: 25
Arrays and Indexing
let fibonacci = [0, 1, 1, 2, 3, 5, 8];
let person = {"name": "Alice", "age": 30};
### Structs
```rust
struct User {
name: "Guest",
role: "User",
}
let u = User { name: "Walon", role: "Admin" };
let guest = User {}; # Uses default values
print(u.name); # Walon
print(guest.name); # Guest
Conditionals
let x = 10;
let result = if (x > 10) {
"Greater"
} elseif (x == 10) {
"Equal"
} else {
"Smaller"
};
Loops
// While loop
let i = 0;
while (i < 5) {
print(i);
i = i + 1;
};
// For loop
for (let j = 0; j < 5; j += 1) {
print(j);
};
Modules & Member Access
### Standard Library Examples
#### Networking & JSON
```rust
import "http";
import "json";
let res = http.get("https://jsonplaceholder.typicode.com/todos/1");
let data = json.parse(res.body);
print(data.title);
Math & Time
import "math";
import "time";
let radius = 10;
let area = math.PI * math.pow(radius, 2);
print("Area:", math.round(area));
let start = time.now();
time.sleep(100);
print("Elapsed (ms):", time.since(start));
Strings & Hashes
import "strings";
import "hash";
let s = " hello world ";
print(strings.trim(strings.to_upper(s))); # HELLO WORLD
let user = {"name": "walon", "age": 25};
if (hash.has_key(user, "name")) {
print("User keys:", hash.keys(user));
}
OS & Environment
import "os";
print("Platform:", os.platform);
print("API Key:", os.get_env("API_KEY"));
os.exit(0);
Advanced Features
// Comments
# This is a single line comment
/*
This is a
multi-line comment
*/
// Formatted print
let name = "Alice";
printf("Hello, %s!", name);
// Type checking
print(typeof(10)); // Output: INTEGER
print(typeof("hi")); // Output: STRING
πΊ Roadmap
We are constantly working to make Code-Lang better. Here is what's coming next:
- Better Error Reporting: Line and column tracking for precise debugging.
- Comments: Support for single and multi-line comments.
- Loops: Implementing
while and for loops.
- Logical Operators: Adding
&& (AND) and || (OR) with short-circuiting.
- Standard Library (Internal): Modules for
fmt, math, strings, time, hash, os, net/http, and json.
- Import System: Ability to include other
.cl files.
- Member Access: Dot notation for objects, modules, and structs.
- Compound Assignment: Support for
+=, -=, etc.
- Structs: Support for defining custom types and creating instances.
- [WIP] Web Server: Request/Response handling and server state.
π License
This project is licensed under the MIT License. See the LICENSE file for details.
π Acknowledgments
- Thorsten Ball for the foundational guide Writing An Interpreter In Go.
- The Go community for providing an incredible ecosystem for language development.