]> Projects - zig-lisp.git/commitdiff Test
global: formatting changes main
authorOrfeas <redacted>
Sat, 28 Mar 2026 12:57:52 +0000 (14:57 +0200)
committerOrfeas <redacted>
Sat, 28 Mar 2026 18:44:14 +0000 (20:44 +0200)
src/Parser.zig
src/main.zig
src/vm/special_forms.zig

index 92add12f9dc29d78f876a72e52be31c4f9ad215c..095ce1b46c6f72ec43aacc2f5c1874eaaedff95f 100644 (file)
@@ -106,28 +106,26 @@ pub const Expression = struct {
         return result;
     }
 
-    pub fn clone(self: Expression, allocator: Allocator) error{OutOfMemory}!Expression {
+    pub fn clone(
+        self: Expression,
+        allocator: Allocator,
+    ) error{OutOfMemory}!Expression {
         const result = Expression{
             .quote = self.quote,
             .tag = switch (self.tag) {
                 .nil, .t, .integer, .float => self.tag,
-
                 .string => |string| .{
                     .string = try allocator.dupe(u8, string),
                 },
-
                 .keyword => |keyword| .{
                     .keyword = try allocator.dupe(u8, keyword),
                 },
-
                 .symbol => |symbol| .{
                     .symbol = try allocator.dupe(u8, symbol),
                 },
-
                 .list => |list| .{
                     .list = try cloneExpressionList(list, allocator),
                 },
-
                 .funcall => |funcall| .{
                     .funcall = try cloneExpressionList(funcall, allocator),
                 },
@@ -145,12 +143,10 @@ pub const Expression = struct {
         alloc: Allocator,
         quote: ?QuoteTag,
     ) ParseError!Expression {
-        var result = Expression{
-            .tag = undefined,
-        };
-
+        var result = Expression{ .tag = undefined };
         const token = tokenizer.peek();
         const token_value = tokenizer.tokenValue(token);
+
         switch (token.tag) {
             .eof => return error.EndOfFile,
 
@@ -162,16 +158,14 @@ pub const Expression = struct {
 
             .quote => {
                 _ = tokenizer.next();
-                const expr = Expression.parse(
-                    tokenizer,
-                    alloc,
-                    quote orelse .quote,
-                ) catch |err| switch (err) {
-                    error.EndOfFile => {
-                        log.warn("Unexpected end of file after '", .{});
-                        return error.IncompleteExpression;
-                    },
-                    else => return err,
+                const expr = Expression.parse(tokenizer, alloc, quote orelse .quote) catch |err| {
+                    switch (err) {
+                        error.EndOfFile => {
+                            log.warn("Unexpected end of file after '", .{});
+                            return error.IncompleteExpression;
+                        },
+                        else => return err,
+                    }
                 };
                 const quoted = try alloc.create(Expression);
                 quoted.* = expr;
@@ -183,16 +177,14 @@ pub const Expression = struct {
 
             .backquote => {
                 _ = tokenizer.next();
-                const expr = Expression.parse(
-                    tokenizer,
-                    alloc,
-                    quote orelse .backquote,
-                ) catch |err| switch (err) {
-                    error.EndOfFile => {
-                        log.warn("Unexpected end of file after `", .{});
-                        return error.IncompleteExpression;
-                    },
-                    else => return err,
+                const expr = Expression.parse(tokenizer, alloc, quote orelse .backquote) catch |err| {
+                    switch (err) {
+                        error.EndOfFile => {
+                            log.warn("Unexpected end of file after `", .{});
+                            return error.IncompleteExpression;
+                        },
+                        else => return err,
+                    }
                 };
                 const quoted = try alloc.create(Expression);
                 quoted.* = expr;
@@ -204,20 +196,22 @@ pub const Expression = struct {
 
             .comma => {
                 _ = tokenizer.next();
-                const next_quote: ?QuoteTag = if (quote) |q| switch (q) {
-                    .backquote => null,
-                    .quote => .quote,
-                } else null;
-                const expr = Expression.parse(
-                    tokenizer,
-                    alloc,
-                    next_quote,
-                ) catch |err| switch (err) {
-                    error.EndOfFile => {
-                        log.warn("Unexpected end of file after ,", .{});
-                        return error.IncompleteExpression;
-                    },
-                    else => return err,
+                const next_quote: ?QuoteTag =
+                    if (quote) |q|
+                        switch (q) {
+                            .backquote => null,
+                            .quote => .quote,
+                        }
+                    else
+                        null;
+                const expr = Expression.parse(tokenizer, alloc, next_quote) catch |err| {
+                    switch (err) {
+                        error.EndOfFile => {
+                            log.warn("Unexpected end of file after ,", .{});
+                            return error.IncompleteExpression;
+                        },
+                        else => return err,
+                    }
                 };
                 result = .{
                     .tag = expr.tag,
@@ -227,22 +221,26 @@ pub const Expression = struct {
 
             .integer => {
                 _ = tokenizer.next();
-                const value = std.fmt.parseInt(i64, token_value, 10) catch |err| switch (err) {
-                    error.Overflow => {
-                        log.debug("Input number `{s}` is too large", .{token_value});
-                        return error.InvalidToken;
-                    },
-                    // tokenizer should have handled this case
-                    error.InvalidCharacter => unreachable,
+                const value = std.fmt.parseInt(i64, token_value, 10) catch |err| {
+                    switch (err) {
+                        error.Overflow => {
+                            log.debug("Input number `{s}` is too large", .{token_value});
+                            return error.InvalidToken;
+                        },
+                        // tokenizer should have handled this case
+                        error.InvalidCharacter => unreachable,
+                    }
                 };
                 result.tag = .{ .integer = value };
             },
 
             .float => {
                 _ = tokenizer.next();
-                const value = std.fmt.parseFloat(f64, token_value) catch |err| switch (err) {
-                    // tokenizer should have handled this case
-                    error.InvalidCharacter => unreachable,
+                const value = std.fmt.parseFloat(f64, token_value) catch |err| {
+                    switch (err) {
+                        // tokenizer should have handled this case
+                        error.InvalidCharacter => unreachable,
+                    }
                 };
                 result.tag = .{ .float = value };
             },
@@ -261,12 +259,13 @@ pub const Expression = struct {
             .identifier => {
                 _ = tokenizer.next();
                 const value = token_value;
-                result.tag = if (std.mem.eql(u8, value, "nil"))
-                    .nil
-                else if (std.mem.eql(u8, value, "t"))
-                    .t
-                else
-                    .{ .symbol = value };
+                result.tag =
+                    if (std.mem.eql(u8, value, "nil"))
+                        .nil
+                    else if (std.mem.eql(u8, value, "t"))
+                        .t
+                    else
+                        .{ .symbol = value };
             },
 
             .l_paren => {
@@ -285,6 +284,7 @@ pub const Expression = struct {
                                 .{ .funcall = list };
                         break;
                     }
+
                     if (Expression.parse(tokenizer, alloc, quote)) |expr| {
                         const elem = try alloc.create(Expression);
                         elem.* = expr;
@@ -296,7 +296,9 @@ pub const Expression = struct {
                 }
             },
 
-            .r_paren => return error.UnmatchedParenthesis,
+            .r_paren => {
+                return error.UnmatchedParenthesis;
+            },
 
             else => {
                 _ = tokenizer.next();
@@ -319,17 +321,31 @@ pub const Expression = struct {
         }
 
         switch (self.tag) {
-            .nil => try writer.print("nil", .{}),
-            .t => try writer.print("t", .{}),
-            .integer => |value| try writer.print("{}", .{value}),
-            .float => |value| try writer.print("{}", .{value}),
-            .string => |value| try writer.print("\"{s}\"", .{value}),
-            .keyword, .symbol => |value| try writer.print("{s}", .{value}),
+            .nil => {
+                try writer.print("nil", .{});
+            },
+            .t => {
+                try writer.print("t", .{});
+            },
+            .integer => |value| {
+                try writer.print("{}", .{value});
+            },
+            .float => |value| {
+                try writer.print("{}", .{value});
+            },
+            .string => |value| {
+                try writer.print("\"{s}\"", .{value});
+            },
+            .keyword, .symbol => |value| {
+                try writer.print("{s}", .{value});
+            },
             .list, .funcall => |list| {
                 try writer.print("(", .{});
                 var it = list.first;
+
                 while (it) |node| : (it = node.next) {
                     const elem: *Expression = @fieldParentPtr("node", node);
+
                     try writer.print("{f}", .{elem.*});
                     if (node.next != null) {
                         try writer.print(" ", .{});
@@ -337,7 +353,9 @@ pub const Expression = struct {
                 }
                 try writer.print(")", .{});
             },
-            .quoted => |value| try writer.print("{f}", .{value.*}),
+            .quoted => |value| {
+                try writer.print("{f}", .{value.*});
+            },
         }
     }
 
@@ -353,8 +371,12 @@ pub const Expression = struct {
     pub fn deinit(self: Expression, allocator: Allocator) void {
         switch (self.tag) {
             .nil, .t, .integer, .float => {},
-            .string, .keyword, .symbol => |string| allocator.free(string),
-            .list, .funcall => |list| deinitExpressionList(list, allocator),
+            .string, .keyword, .symbol => |string| {
+                allocator.free(string);
+            },
+            .list, .funcall => |list| {
+                deinitExpressionList(list, allocator);
+            },
             .quoted => |quoted| {
                 quoted.*.deinit(allocator);
                 allocator.destroy(quoted);
index 4698b886c0d1bf0c5523f02efd1a4487a320ece7..7cf1c6857b158396d7c6d5cca764cb01baab3508 100644 (file)
@@ -21,8 +21,10 @@ pub fn logFn(
     args: anytype,
 ) void {
     const prefix = "[" ++ comptime level.asText() ++ "] ";
+
     std.debug.lockStdErr();
     defer std.debug.unlockStdErr();
+
     var stderr_writer = std.fs.File.stderr().writer(&.{});
     const stderr = &stderr_writer.interface;
     stderr.print(prefix ++ format ++ "\n", args) catch return;
@@ -55,9 +57,11 @@ fn repl(
 
         const result = try builtins.eval(&env, expr);
         try env.stdout.print("{f}\n", .{result});
-    } else |err| switch (err) {
-        error.EndOfStream => return,
-        else => return err,
+    } else |err| {
+        switch (err) {
+            error.EndOfStream => return,
+            else => return err,
+        }
     }
 }
 
index bf0d61aa64a5ec435ffdac8d6b127d1cf6ce9eeb..eae53825f4c8e741b0a50f5d6497d3ed2c4e4d2a 100644 (file)
@@ -44,11 +44,13 @@ pub fn setq(env: *Environment, args: DoublyLinkedList) !Expression {
         last_result = value;
         try env.bind(assignee.tag.symbol, value);
     }
+
     return last_result;
 }
 
 pub fn @"if"(env: *Environment, args: DoublyLinkedList) !Expression {
-    if (args.len() < 2) { // Need at least a condition and a ‘then’ branch
+    // Need at least a condition and a ‘then’ branch
+    if (args.len() < 2) {
         return error.WrongNumberOfArguments;
     }
 
@@ -66,11 +68,13 @@ pub fn @"if"(env: *Environment, args: DoublyLinkedList) !Expression {
         const else_expr: *Expression = @fieldParentPtr("node", node);
         last_result = try builtins.eval(env, else_expr.*);
     }
+
     return last_result;
 }
 
 pub fn @"while"(env: *Environment, args: DoublyLinkedList) !Expression {
-    if (args.len() < 2) { // Need at least a condition and one body expression
+    // Need at least a condition and one body expression
+    if (args.len() < 2) {
         return error.WrongNumberOfArguments;
     }