Skip to content

Commit 64ed409

Browse files
committed
Fix more bugs in tinylisp example
1 parent 57e0c4c commit 64ed409

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

examples/tinylisp/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tinylisp"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["David Cruz <[email protected]>"]
55
edition = "2021"
66
publish = false

examples/tinylisp/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# tinyasm
1+
# tinylisp
22

33
Example that implements a tiny lisp-like language which is compiled AOT.
44

examples/tinylisp/src/main.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,13 @@ fn assemble<'a>(nodes: &'a [Node<'a>]) -> extern "C" fn() -> u64 {
160160
}
161161

162162
fn get_value<'a>(v: Option<&Node<'a>>, out: &mut Vec<u8>) -> Value {
163-
match v {
164-
Some(Node::Ident(i)) => Value::Register(ident_to_register(*i)),
165-
Some(Node::Number(n)) => Value::Imm(*n),
166-
Some(node) => {
167-
// expressions should pushed onto stack
168-
assemble_exp(&node, out);
169-
Value::Stack
170-
},
171-
None => panic!("Expected a value")
172-
}
163+
assemble_exp(v.expect("Expected a value"), out)
173164
}
174165

175166
fn assemble_exp<'a>(node: &'a Node<'a>, out: &mut Vec<u8>) -> Value {
176167
match node {
168+
Node::Ident(i) => Value::Register(ident_to_register(*i)),
169+
Node::Number(n) => Value::Imm(*n),
177170
Node::Call(name, args) => {
178171
match *name {
179172
b"set" => {
@@ -227,13 +220,15 @@ fn assemble<'a>(nodes: &'a [Node<'a>]) -> extern "C" fn() -> u64 {
227220
whatever => todo!("Not sure what {} is", core::str::from_utf8(whatever).unwrap())
228221
}
229222
},
230-
231-
whatever => todo!("Whatever this is {whatever:#?}")
232223
}
233224
}
234225

235226
while ind < nodes.len() {
236-
assemble_exp(&nodes[ind], &mut out);
227+
match assemble_exp(&nodes[ind], &mut out) {
228+
Value::Stack => out.extend(dasm::tier::raw::amd64::pop_r64(0)),
229+
_ => ()
230+
}
231+
237232
ind += 1;
238233
}
239234

@@ -258,10 +253,12 @@ fn main() {
258253
)
259254
)
260255
)
256+
257+
(add (add rax rax) 6)
261258
");
262259

263260
let nodes = parse(&tokens);
264261

265262
let out = assemble(&nodes);
266-
assert_eq!(out(), 1 + 5 + 72);
263+
assert_eq!(out(), (1 + 5 + 72) * 2 + 6);
267264
}

0 commit comments

Comments
 (0)