Skip to content

Commit a245d2b

Browse files
committed
feat: Improve playground UI with examples and favicon, and update WASM bindings for object system and control flow enhancements.
1 parent 0452456 commit a245d2b

File tree

11 files changed

+406
-22
lines changed

11 files changed

+406
-22
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ clap_complete = { version = "4", optional = true }
3030
pest = { version = "2.8.4", optional = true }
3131
pest_derive = { version = "2.8.4", optional = true }
3232
indexmap = "2.12.1"
33-
jiff = "0.2"
33+
jiff = { version = "0.2", features = ["js"] }
3434
serde = { version = "1.0", features = ["derive"] }
3535
serde_json = "1.0"
3636

docs/favicon.svg

Lines changed: 4 additions & 0 deletions
Loading

docs/index.html

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,34 @@
55
<meta charset="utf-8">
66
<title>Chen Lang Playground</title>
77
<link rel="stylesheet" href="style.css">
8+
<link rel="icon" type="image/svg+xml" href="favicon.svg">
89
</head>
910

1011
<body>
1112
<div class="container">
1213
<h1>Chen Lang Playground</h1>
13-
<textarea id="code" placeholder="Enter your Chen code here...">print("Hello, Chen Lang!")</textarea>
14-
<button id="run">Run</button>
15-
<pre id="output"></pre>
14+
<div class="controls">
15+
<label for="example-select">Load Example: </label>
16+
<select id="example-select">
17+
<option value="hello">Basic: Hello World</option>
18+
<option value="fib">Algorithm: Fibonacci</option>
19+
<option value="objects">Pattern: Objects & Methods</option>
20+
<option value="inheritance">Pattern: Inheritance</option>
21+
<option value="date">StdLib: Date & Time</option>
22+
<option value="json">StdLib: JSON</option>
23+
<option value="arrays">StdLib: Arrays</option>
24+
</select>
25+
</div>
26+
<div class="main-content">
27+
<div class="editor-section">
28+
<textarea id="code" placeholder="Enter your Chen code here...">print("Hello, Chen Lang!")</textarea>
29+
<button id="run">Run Code</button>
30+
</div>
31+
<div class="output-section">
32+
<div class="output-label">Output:</div>
33+
<pre id="output"></pre>
34+
</div>
35+
</div>
1636
</div>
1737
<script type="module" src="index.js"></script>
1838
</body>

docs/index.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,129 @@
11
import init, { run_wasm } from './pkg/chen_lang.js';
22

3+
const examples = {
4+
hello: `# Basic: Hello World
5+
print("Hello, Chen Lang!")
6+
7+
let a = 10
8+
if a > 5 {
9+
println("a is greater than 5")
10+
}
11+
`,
12+
fib: `# Algorithm: Fibonacci
13+
def fib(n) {
14+
if n <= 1 {
15+
return n
16+
}
17+
return fib(n-1) + fib(n-2)
18+
}
19+
20+
println("Fibonacci of 10 is:")
21+
println(fib(10))
22+
`,
23+
objects: `# Pattern: Objects & Methods (Lua-style)
24+
def Person(name) {
25+
let p = #{ name: name }
26+
27+
def greet(self) {
28+
println("Hello, my name is " + self.name)
29+
}
30+
31+
# Set prototype (methods)
32+
set_meta(p, #{
33+
__index: #{ greet: greet }
34+
})
35+
return p
36+
}
37+
38+
let chen = Person("Chen")
39+
chen.greet()
40+
`,
41+
inheritance: `# Pattern: Prototype Inheritance
42+
# Base "Class"
43+
def Animal(name) {
44+
let a = #{ name: name }
45+
def speak(self) {
46+
println(self.name + " makes a noise.")
47+
}
48+
set_meta(a, #{ __index: #{ speak: speak } })
49+
return a
50+
}
51+
52+
# Derived "Class"
53+
def Dog(name) {
54+
# 1. Create base instance
55+
let d = Animal(name)
56+
57+
# 2. Define derived methods
58+
def bark(self) {
59+
println(self.name + " barks: Woof!")
60+
}
61+
62+
# 3. Create methods table that inherits from base's methods
63+
# Get base prototype (metatable.__index)
64+
let base_proto = get_meta(d).__index
65+
66+
# Create new prototype that inherits from base_proto
67+
let dog_proto = #{ bark: bark }
68+
set_meta(dog_proto, #{ __index: base_proto })
69+
70+
# 4. Update instance's metatable to use new prototype
71+
set_meta(d, #{ __index: dog_proto })
72+
73+
return d
74+
}
75+
76+
let dog = Dog("Rex")
77+
dog.speak() # Inherited from Animal
78+
dog.bark() # Defined in Dog
79+
`,
80+
date: `# StdLib: Date & Time
81+
let now = Date.new()
82+
println("Current time (ISO): " + now.format("%Y-%m-%d %H:%M:%S"))
83+
84+
# JSON serialization of Date
85+
println("As JSON: " + JSON.stringify(now))
86+
`,
87+
json: `# StdLib: JSON Processing
88+
let data = #{
89+
name: "Chen Lang",
90+
features: ["Simple", "Dynamic", "Rust-based"],
91+
version: 0.1
92+
}
93+
94+
let jsonStr = JSON.stringify(data)
95+
println("Serialized JSON:")
96+
println(jsonStr)
97+
98+
let parsed = JSON.parse(jsonStr)
99+
println("Parsed JSON Name: " + parsed.name)
100+
`,
101+
arrays: `# StdLib: Arrays
102+
# Arrays are dynamic list-like objects
103+
let arr = [1, 2, 3]
104+
105+
arr.push(4)
106+
println("Array length: " + arr.len())
107+
108+
let popped = arr.pop()
109+
println("Popped value: " + popped)
110+
111+
# Arrays can store any type
112+
arr.push("Mixed")
113+
println(JSON.stringify(arr))
114+
`
115+
};
116+
3117
async function run() {
4118
await init();
5119
const runBtn = document.getElementById('run');
6120
const codeArea = document.getElementById('code');
7121
const outputArea = document.getElementById('output');
122+
const exampleSelect = document.getElementById('example-select');
123+
124+
// Load initial example
125+
codeArea.value = examples.hello;
126+
exampleSelect.value = 'hello';
8127

9128
runBtn.addEventListener('click', () => {
10129
const code = codeArea.value;
@@ -15,6 +134,15 @@ async function run() {
15134
outputArea.textContent = `Error: ${e}`;
16135
}
17136
});
137+
138+
exampleSelect.addEventListener('change', (e) => {
139+
const key = e.target.value;
140+
if (examples[key]) {
141+
codeArea.value = examples[key];
142+
// Clear output when changing example
143+
outputArea.textContent = '';
144+
}
145+
});
18146
}
19147

20148
run();

docs/pkg/README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
* **Expression-Oriented**: `if/else` blocks and code blocks are expressions that return values.
1515
* **Dynamic Typing**: Supports Integers, Floats, Strings, Booleans, and Null.
1616
* **Functions**: First-class functions with implicit returns.
17-
* **Control Flow**: `if/else` expressions and `for` loops.
17+
* **Control Flow**: `if/else` expressions, `for` loops, `break`, and `continue`.
18+
* **Object System**: Supports object literals, property access, indexing, and metatables.
1819
* **Scope Isolation**: Block-scoped variables.
1920

21+
2022
---
2123

2224
### 🚀 Quick Start
@@ -83,6 +85,25 @@ for i <= 9 {
8385
}
8486
```
8587

88+
#### 6. Object System
89+
```python
90+
# Object literal
91+
let person = #{
92+
name: "Alice",
93+
age: 25
94+
}
95+
96+
# Property access
97+
println(person.name) # Output: Alice
98+
99+
# Property assignment
100+
person.age = 26
101+
102+
# Index access
103+
let key = "name"
104+
println(person[key]) # Output: Alice
105+
```
106+
86107
---
87108

88109
### 🛠️ Language Reference
@@ -109,14 +130,14 @@ for i <= 9 {
109130
### 📝 TODO / Roadmap
110131

111132
* [x] **Core**: Integers, Booleans, Arithmetic, Logic
112-
* [x] **Control Flow**: `if/else` (expression), `for` loops
133+
* [x] **Control Flow**: `if/else` (expression), `for` loops, `break`, `continue`
113134
* [x] **Functions**: Definition, Call, Recursion, Implicit Return
114135
* [x] **Types**: Floats, Strings
115-
* [ ] **Control Flow**: `break`, `continue`
116-
* [ ] **Data Structures**: Arrays, Maps
136+
* [x] **Object System**: Object literals, property access, indexing, metatables (can be used as arrays)
117137
* [ ] **Standard Library**: File I/O, Math functions
118138
* [ ] **Error Handling**: Try/Catch or Result type
119139
140+
120141
---
121142
122143
### 📄 License

docs/pkg/chen_lang.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
77

88
export interface InitOutput {
99
readonly memory: WebAssembly.Memory;
10-
readonly run: (a: number) => number;
10+
readonly run: (a: number, b: number) => void;
1111
readonly run_wasm: (a: number, b: number, c: number) => void;
12-
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
1312
readonly __wbindgen_export: (a: number, b: number) => number;
1413
readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
15-
readonly __wbindgen_export3: (a: number, b: number, c: number) => void;
14+
readonly __wbindgen_export3: (a: number) => void;
15+
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
16+
readonly __wbindgen_export4: (a: number, b: number, c: number) => void;
1617
}
1718

1819
export type SyncInitInput = BufferSource | WebAssembly.Module;

0 commit comments

Comments
 (0)