Skip to content

Commit

Permalink
Allow define in subexpressions (#11)
Browse files Browse the repository at this point in the history
* Allow define within subexpression of modules.

Closes #5

* Implement define within function definitions.

* Move some files under datastructures.

* Reduce number of instructions needed to execute a define.

Before: deref(define), push_symbol, push_val, eval(3)
After: push_symbol, define(symbol)

* Add golden test for define in lambda.
  • Loading branch information
wmedrano committed Aug 31, 2024
1 parent a347d8e commit 7a56b9b
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 138 deletions.
6 changes: 4 additions & 2 deletions site/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ following tradeoffs may happen:

## Releases

### 0.2.0 (In Progress)
### 0.2.0 (Current)

Will focus on:

- Improved error reporting.
- Incorporating user feedback.
- Performance & API tweaks.

### 0.1.1 (Current)
- Allow define within subexpressions.

### 0.1.1

- Add `map` and `filter` functions.

Expand Down
16 changes: 14 additions & 2 deletions src/ByteCode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Allocator = std.mem.Allocator;

name: []const u8,
arg_count: usize,
locals_count: usize,
instructions: std.ArrayListUnmanaged(Instruction),
module: *Module,

Expand All @@ -34,9 +35,12 @@ pub fn format(
writer: anytype,
) !void {
try writer.print(
"{{ .name = {s}, .arg_count = {d}, .instructions = {any} }}",
.{ self.name, self.arg_count, self.instructions.items },
"{{ .name = {s}, .arg_count = {d}, .locals_count = {d}, .instructions =\n",
.{ self.name, self.arg_count, self.locals_count },
);
for (self.instructions.items, 0..self.instructions.items.len) |instruction, idx| {
try writer.print(" {d}: {any}\n", .{ idx, instruction });
}
}

const ByteCodeValuesIter = struct {
Expand All @@ -52,6 +56,8 @@ const ByteCodeValuesIter = struct {
.deref_local => {},
.deref_global => {},
.get_arg => {},
.define => |v| return v,
.move => {},
.eval => {},
.jump => {},
.jump_if => {},
Expand Down Expand Up @@ -80,6 +86,10 @@ pub const Instruction = union(enum) {
deref_global: []const u8,
/// Get the nth value (0-based index) from the base of the current function call stack.
get_arg: usize,
/// Define a symbol on the current module.
define: Val,
/// Move the top value of the stack into the given index.
move: usize,
/// Evaluate the top n elements of the stack. The deepmost value should be a function.
eval: usize,
/// Jump instructions in the bytecode.
Expand Down Expand Up @@ -115,11 +125,13 @@ pub const Instruction = union(enum) {
.deref_global => |sym| try writer.print("deref_global({s})", .{sym}),
.deref_local => |sym| try writer.print("deref_local({s})", .{sym}),
.get_arg => |n| try writer.print("get_arg({d})", .{n}),
.move => |n| try writer.print("move({d})", .{n}),
.eval => |n| try writer.print("eval({d})", .{n}),
.jump => |n| try writer.print("jump({d})", .{n}),
.jump_if => |n| try writer.print("jump_if({d})", .{n}),
.import_module => |m| try writer.print("import({s})", .{m}),
.ret => try writer.print("ret()", .{}),
.define => |sym| try writer.print("define({s})", .{sym}),
}
}
};
Expand Down
Loading

0 comments on commit 7a56b9b

Please sign in to comment.