Skip to content

Commit

Permalink
fix: Rationalizing and simplifying buzz_api
Browse files Browse the repository at this point in the history
- All functions operate on Values
- Removed a bunch of useless functions
- Consistant naming: bz_<type><whatitdoes>
  • Loading branch information
giann committed Aug 30, 2024
1 parent 4f2dc9c commit 4884fdb
Show file tree
Hide file tree
Showing 17 changed files with 1,007 additions and 1,272 deletions.
182 changes: 103 additions & 79 deletions src/Jit.zig

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/builtin/list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn remove(ctx: *NativeCtx) c_int {
}

const SortContext = struct {
sort_closure: *ObjClosure,
sort_closure: Value,
ctx: *NativeCtx,
};

Expand All @@ -138,7 +138,7 @@ fn lessThan(context: SortContext, lhs: Value, rhs: Value) bool {
pub fn sort(ctx: *NativeCtx) c_int {
var self = ObjList.cast(ctx.vm.peek(1).obj()).?;
// fun compare(T lhs, T rhs) > bool
const sort_closure = ObjClosure.cast(ctx.vm.peek(0).obj()).?;
const sort_closure = ctx.vm.peek(0);

std.sort.insertion(
Value,
Expand Down Expand Up @@ -310,7 +310,7 @@ pub fn next(ctx: *NativeCtx) c_int {

pub fn forEach(ctx: *NativeCtx) c_int {
const list = ObjList.cast(ctx.vm.peek(1).obj()).?;
const closure = ObjClosure.cast(ctx.vm.peek(0).obj()).?;
const closure = ctx.vm.peek(0);

for (list.items.items, 0..) |item, index| {
const index_value = Value.fromInteger(@as(i32, @intCast(index)));
Expand All @@ -331,7 +331,7 @@ pub fn forEach(ctx: *NativeCtx) c_int {

pub fn reduce(ctx: *NativeCtx) c_int {
const list = ObjList.cast(ctx.vm.peek(2).obj()).?;
const closure = ObjClosure.cast(ctx.vm.peek(1).obj()).?;
const closure = ctx.vm.peek(1);
var accumulator = ctx.vm.peek(0);

for (list.items.items, 0..) |item, index| {
Expand Down Expand Up @@ -361,7 +361,7 @@ pub fn reduce(ctx: *NativeCtx) c_int {

pub fn filter(ctx: *NativeCtx) c_int {
const list = ObjList.cast(ctx.vm.peek(1).obj()).?;
const closure = ObjClosure.cast(ctx.vm.peek(0).obj()).?;
const closure = ctx.vm.peek(0);

var new_list: *ObjList = ctx.vm.gc.allocateObject(
ObjList,
Expand Down Expand Up @@ -404,9 +404,9 @@ pub fn filter(ctx: *NativeCtx) c_int {

pub fn map(ctx: *NativeCtx) c_int {
const list = ObjList.cast(ctx.vm.peek(1).obj()).?;
const closure = ObjClosure.cast(ctx.vm.peek(0).obj()).?;
const closure = ctx.vm.peek(0);

const mapped_type = closure.function.type_def.resolved_type.?.Function.return_type;
const mapped_type = ObjClosure.cast(closure.obj()).?.function.type_def.resolved_type.?.Function.return_type;
var new_list: *ObjList = ctx.vm.gc.allocateObject(
ObjList,
ObjList.init(
Expand Down
14 changes: 7 additions & 7 deletions src/builtin/map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn clone(ctx: *NativeCtx) c_int {

pub fn reduce(ctx: *NativeCtx) c_int {
const self = ObjMap.cast(ctx.vm.peek(2).obj()).?;
const closure = ObjClosure.cast(ctx.vm.peek(1).obj()).?;
const closure = ctx.vm.peek(1);
var accumulator = ctx.vm.peek(0);

var it = self.map.iterator();
Expand All @@ -66,7 +66,7 @@ pub fn reduce(ctx: *NativeCtx) c_int {

pub fn filter(ctx: *NativeCtx) c_int {
const self = ObjMap.cast(ctx.vm.peek(1).obj()).?;
const closure = ObjClosure.cast(ctx.vm.peek(0).obj()).?;
const closure = ctx.vm.peek(0);

var new_map: *ObjMap = ctx.vm.gc.allocateObject(
ObjMap,
Expand Down Expand Up @@ -109,7 +109,7 @@ pub fn filter(ctx: *NativeCtx) c_int {

pub fn forEach(ctx: *NativeCtx) c_int {
const self = ObjMap.cast(ctx.vm.peek(1).obj()).?;
const closure = ObjClosure.cast(ctx.vm.peek(0).obj()).?;
const closure = ctx.vm.peek(0);

var it = self.map.iterator();
while (it.next()) |kv| {
Expand All @@ -129,9 +129,9 @@ pub fn forEach(ctx: *NativeCtx) c_int {

pub fn map(ctx: *NativeCtx) c_int {
const self = ObjMap.cast(ctx.vm.peek(1).obj()).?;
const closure = ObjClosure.cast(ctx.vm.peek(0).obj()).?;
const closure = ctx.vm.peek(0);

const mapped_type = closure.function.type_def.resolved_type.?.Function
const mapped_type = ObjClosure.cast(closure.obj()).?.function.type_def.resolved_type.?.Function
.return_type.resolved_type.?.ObjectInstance
.resolved_type.?.Object;

Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn map(ctx: *NativeCtx) c_int {
}

const SortContext = struct {
sort_closure: *ObjClosure,
sort_closure: Value,
ctx: *NativeCtx,
map: *ObjMap,

Expand All @@ -221,7 +221,7 @@ const SortContext = struct {

pub fn sort(ctx: *NativeCtx) c_int {
const self: *ObjMap = ObjMap.cast(ctx.vm.peek(1).obj()).?;
const sort_closure = ObjClosure.cast(ctx.vm.peek(0).obj()).?;
const sort_closure = ctx.vm.peek(0);

self.map.sort(
SortContext{
Expand Down
Loading

0 comments on commit 4884fdb

Please sign in to comment.