From: Orfeas Date: Sat, 28 Mar 2026 12:57:52 +0000 (+0200) Subject: global: formatting changes X-Git-Url: https://git.orfeas.xyz/?a=commitdiff_plain;ds=sidebyside;p=zig-lisp.git global: formatting changes --- diff --git a/src/Parser.zig b/src/Parser.zig index 92add12..095ce1b 100644 --- a/src/Parser.zig +++ b/src/Parser.zig @@ -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); diff --git a/src/main.zig b/src/main.zig index 4698b88..7cf1c68 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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, + } } } diff --git a/src/vm/special_forms.zig b/src/vm/special_forms.zig index bf0d61a..eae5382 100644 --- a/src/vm/special_forms.zig +++ b/src/vm/special_forms.zig @@ -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; }