Embedded programming language for Zig
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Minor changes

IamPyu e3c740bc 7d00b102

+29 -23
+1 -8
build.zig
··· 19 19 }); 20 20 zexa_mod.addImport("mitochondria", mitochondria_mod); 21 21 22 - // { 23 22 const zexa_lib = b.addLibrary(.{ 24 23 .name = "zexa", 25 24 .root_module = zexa_mod, ··· 31 30 }); 32 31 const run_zexa_tests = b.addRunArtifact(zexa_tests); 33 32 test_step.dependOn(&run_zexa_tests.step); 34 - // } 35 33 36 34 const zexa_runtime_mod = b.addModule("zexa-runtime", .{ 37 35 .root_source_file = b.path("runtime/root.zig"), 38 - // .target = b.resolveTargetQuery(.{ 39 - // .os_tag = .wasi, 40 - // .cpu_arch = .wasm32, 41 - // }), 42 36 .target = target, 43 37 .optimize = optimize, 44 38 }); ··· 50 44 .linkage = .dynamic, 51 45 }); 52 46 b.installArtifact(zexa_runtime_lib); 53 - // { 47 + 54 48 const zexa_runtime_tests = b.addTest(.{ 55 49 .root_module = zexa_runtime_mod, 56 50 }); 57 51 58 52 const run_zexa_runtime_tests = b.addRunArtifact(zexa_runtime_tests); 59 53 test_step.dependOn(&run_zexa_runtime_tests.step); 60 - // } 61 54 62 55 const add_exe = b.option(bool, "exe", "Whether to build the Zexa interpreter") orelse true; 63 56
+13 -14
src/lang.zig
··· 301 301 }; 302 302 } 303 303 304 - /// Return whether this value is a cons pair or `nil` 305 - pub fn isConsOrNil(self: *const Self) bool { 306 - return switch (self.expr) { 307 - .nil => true, 308 - .cons => true, 309 - else => false, 310 - }; 311 - } 312 - 313 304 /// Return whether this value is truthy 314 305 pub fn isTruthy(self: *const Self) bool { 315 306 return switch (self.expr) { ··· 533 524 const Self = @This(); 534 525 const STACK_SIZE: usize = 100000; 535 526 536 - pub const VarMap = std.StringHashMap(*Value); 537 - pub const ValMap = std.ArrayList(*Value); 538 - 539 527 pub const CallStack = struct { 540 528 pub const Stack = std.ArrayList([]const u8); 541 529 ··· 576 564 } 577 565 }; 578 566 567 + pub const VarMap = std.StringHashMap(*Value); 568 + pub const ValMap = std.ArrayList(*Value); 569 + 579 570 allocator: Allocator, 580 571 arena: Arena, 581 572 variables: std.StringHashMap(*Value), 582 573 parent: ?*Scope = null, 583 574 tracked_values: ValMap, 575 + tracked_strings: std.ArrayList([]u8), 584 576 call_stack: CallStack, 585 577 exception: ?[]u8 = null, 586 578 ··· 591 583 .arena = Arena.init(allocator), 592 584 .variables = VarMap.init(allocator), 593 585 .tracked_values = try ValMap.initCapacity(allocator, STACK_SIZE), 586 + .tracked_strings = try .initCapacity(allocator, STACK_SIZE), 594 587 .call_stack = try CallStack.init(allocator), 595 588 }; 596 589 } ··· 601 594 for (self.tracked_values.items) |ptr| { 602 595 self.allocator.destroy(ptr); 603 596 } 597 + for (self.tracked_strings.items) |str| { 598 + self.allocator.free(str); 599 + } 604 600 self.tracked_values.deinit(self.allocator); 601 + self.tracked_strings.deinit(self.allocator); 602 + 605 603 self.call_stack.deinit(); 606 604 self.clearException(); 607 605 self.arena.deinit(); ··· 638 636 639 637 /// Allocate a string bounded to the scope 640 638 pub fn createString(self: *Self, str: []const u8) ![]const u8 { 641 - const out = try self.arena.allocator().alloc(u8, str.len); 639 + const out = try self.allocator.alloc(u8, str.len); 642 640 @memcpy(out, str); 641 + try self.tracked_strings.append(self.allocator, out); 643 642 return out; 644 643 } 645 644 ··· 744 743 } 745 744 } 746 745 747 - /// Evaluate the AST of a program 746 + /// Evaluate an AST and return the last evaluated expression 748 747 pub fn evalAst(self: *Self, ast: []const *Value) RuntimeError!*Value { 749 748 var iter = @import("./util.zig").SliceIterator(*Value).init(ast); 750 749 var value: ?*Value = null;
+1 -1
src/test/hello.zexa
··· 1 1 (print "Hello World") 2 - (eval '(print "Hello World")) 2 + (eval '(print "Hello World"))
+14
test/argument-grow.zexa
··· 1 + ;; this program keeps using more and more memory 2 + ;; currently function arguments are stored in an arena allocator and never freed 3 + ;; TODO: fix the above stated problem to prevent zexa programs from blowing up the 4 + ;; TODO: system. 5 + 6 + (define 'call '(print "Beginning")) 7 + (define 'accum 1) 8 + 9 + (while #t 10 + (set 'accum (add accum 1)) 11 + (eval call) 12 + (append call (format "Argument %{}" accum))) 13 + 14 + ;; TODO: I've also discovered that strings are completely broken on Debug / ReleaseSafe builds.