Skip to content

zuisong/chen_lang

Repository files navigation

chen_lang

A tiny, expression-oriented programming language written in Rust

🇨🇳 中文版

chen_lang is a dynamic, interpreted programming language designed for learning and experimentation. It features a clean syntax, expression-based control flow, and first-class functions.


✨ Key Features

  • Expression-Oriented: if/else blocks and code blocks are expressions that return values.
  • Dynamic Typing: Supports Integers, Floats, Strings, Booleans, and Null.
  • Functions: First-class functions with implicit returns.
  • Control Flow: if/else expressions, for loops, break, and continue.
  • Object System: Supports object literals, property access, indexing, and metatables.
  • Scope Isolation: Block-scoped variables.

🚀 Quick Start

1. Hello World & String Operations

let name = "Chen Lang"
println("Hello, " + name + "!")
# Output: Hello, Chen Lang!

2. If/Else as Expression

In chen_lang, if/else is an expression, meaning it returns a value.

let a = 10
let result = if a > 5 {
    "Greater"
} else {
    "Smaller"
}
println(result) 
# Output: Greater

3. Functions & Implicit Return

Functions implicitly return the value of the last expression.

def add(a, b) {
    a + b  # Implicit return
}

let sum = add(3, 5)
println(sum)
# Output: 8

4. Recursive Fibonacci

def fib(n) {
    if n <= 1 {
        n
    } else {
        fib(n - 1) + fib(n - 2)
    }
}

println(fib(10))
# Output: 55

5. Loops & Logic

# Print multiplication table
let i = 1
for i <= 9 {
    let j = 1
    for j <= i {
        let prod = i * j
        print(j + "x" + i + "=" + prod + " ")
        j = j + 1
    }
    println("")
    i = i + 1
}

6. Object System

# Object literal
let person = #{ 
    name: "Alice", 
    age: 25 
}

# Property access
println(person.name)  # Output: Alice

# Property assignment
person.age = 26

# Index access
let key = "name"
println(person[key])  # Output: Alice

🛠️ Language Reference

Data Types

  • Integer: 1, 42, -10
  • Float: 3.14, -0.01
  • String: "Hello World"
  • Boolean: true, false
  • Null: null (implicit return for empty blocks or if without else)

Operators

  • Arithmetic: +, -, *, /, %
  • Comparison: >, >=, <, <=, ==, !=
  • Logical: &&, ||, !

Comments

# This is a single-line comment

📝 TODO / Roadmap

  • Core: Integers, Booleans, Arithmetic, Logic
  • Control Flow: if/else (expression), for loops, break, continue
  • Functions: Definition, Call, Recursion, Implicit Return
  • Types: Floats, Strings
  • Object System: Object literals, property access, indexing, metatables (can be used as arrays)
  • Standard Library: File I/O, Math functions
  • Error Handling: Try/Catch or Result type

📄 License

MIT License

About

A super tiny and toy language write by rust

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •