DEV Community

Jishith Mp
Jishith Mp

Posted on

I made a programming language called Zen, wanted to share some examples

Hey, I've been working on this for a while now and finally feel okay sharing some code examples.

Zen is a statically typed language that compiles to native binaries through LLVM. Still a work in progress but the core stuff works.

One thing I'm kind of proud of is reactive variables. It's a first class feature where a variable automatically recomputes when something it depends on changes:

int cartItems = 3
int pricePerItem = 50
reactive int total = cartItems * pricePerItem

screen(`Total: $${total}`)   # 150

cartItems = 5
screen(`Total: $${total}`)   # 250, updates automatically

pricePerItem = 60
screen(`Total: $${total}`)   # 300
Enter fullscreen mode Exit fullscreen mode

no event listeners or anything, it just stays in sync. got the idea from how spreadsheet cells work honestly

also has a standard library with time, fs, sys etc. here's a small example using both:

fn greet() {
    string t = time.time()
    List<string> parts = split(t, ":")
    int hour = Int(parts[0])

    string greeting = ""

    if (hour < 12) {
        greeting = "Good Morning"
    } else if (hour < 17) {
        greeting = "Good Afternoon"
    } else {
        greeting = "Good Evening"
    }

    screen(greeting + ", Welcome to Zen!")
}

greet()
Enter fullscreen mode Exit fullscreen mode

and file I/O:

string path = "notes.txt"
string content = "Zen is minimal, fast, and fun to write."

fs.writeFile(path, content)

string data = fs.readFile(path)
screen(`File content: ${data}`)

fs.appendFile(path, " — written in Zen.")

string updated = fs.readFile(path)
screen(`Updated: ${updated}`)
Enter fullscreen mode Exit fullscreen mode

anyway the full pipeline works, lexer parser AST LLVM IR native binary and a CLI. still lots to do but it's at a point where I can actually write real programs in it.

docs if anyone wants to look: https://jishith-dev.github.io/zen-doc/site/

open to feedback, especially on the syntax.

Top comments (0)