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.
break and continue inside loops.
- Static Analysis:
- Symbol Table: Tracks variable scopes, identifier resolution, and constant enforcement.
- Pre-execution Checks: Catches undefined variables and illegal reassignments before running code.
- Support for Comments: Single-line (
#) and multi-line (/* */).
- Standard Operators:
- Arithmetic:
+, -, *, /, % (Modulo)
- Advanced:
** (Power), // (Floor Division)
- Comparison:
==, !=, <, >, <=, >=
- Logical:
&& (AND), || (OR) β with short-circuit evaluation β and ! (Negation)
- Compound Assignment:
+=, -=, *=, /=, %=, **=, //=
- 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, Structs, 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.
- REPL: Interactive shell with persistent history and precise line/column error tracking.
- File Execution: Run scripts with the
.cl extension.
π Getting Started
Prerequisites
- Go (version 1.21 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;
};
const PI = 3.14159;
# PI = 3.14; # Error: cannot reassign to const
print(add(10, 15)); # Output: 25
Arrays and Hashes
let fibonacci = [0, 1, 1, 2, 3, 5, 8];
print(fibonacci[3]); # 2
let person = {"name": "Alice", "age": 30};
print(person.name); # Alice
Structs
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"
};
Logical Operators (&& / ||)
&& and || use short-circuit evaluation β the right side is only evaluated when necessary.
let a = true;
let b = false;
print(a && b); # false
print(a || b); # true
print(!a); # false
# Short-circuit: the right side is never evaluated when
# the result is already known from the left side.
let x = false && someUndefinedFn(); # safe β right side skipped
let y = true || someUndefinedFn(); # safe β right side skipped
# Combine with comparisons
let age = 20;
let hasId = true;
if (age >= 18 && hasId) {
print("Access granted");
};
Loops
# While loop
let i = 0;
while (i < 5) {
print(i);
i += 1;
};
# For loop with break and continue
for (let j = 0; j < 10; j += 1) {
if (j == 2) { continue; };
if (j == 6) { break; };
print(j);
};
Standard Library Examples
Networking & JSON
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);
Miscellaneous
# Single-line comment
/*
Multi-line comment
*/
# Formatted print
let name = "Alice";
printf("Hello, %s!\n", name);
# Type checking
print(typeof(10)); # INTEGER
print(typeof("hi")); # STRING
print(typeof([])); # ARRAY
πΊ Roadmap
| Feature |
Status |
| Better Error Reporting (line & column tracking) |
β
Done |
| Comments (single & multi-line) |
β
Done |
while and for loops with break/continue |
β
Done |
Logical Operators && / ` |
|
Standard Library (math, strings, time, hash, os, json, net) |
β
Done |
Import System (.cl files) |
β
Done |
| Member Access (dot notation) |
β
Done |
Compound Assignment (+=, -=, etc.) |
β
Done |
| Structs (define custom types & create instances) |
β
Done |
Constants (const) |
β
Done |
| Static Analysis (Symbol Table & Scope Awareness) |
β
Done |
| Web Server (request/response handling) |
π§ WIP |
| Struct Methods |
π Planned |
fs module (file system access) |
π Planned |
| REPL Multi-line Support |
π Planned |
| VSCode Extension (syntax highlighting) |
π Planned |
| LSP (Language Server Protocol) |
π Planned |
π 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.