Repository navigation
Conversation
- Port codebase and tests to Zig v0.16.0 compiler and std lib changes. - Update vendored Isocline library to v1.1.0 and restore custom ic_completion_input hook. - Clean up redundant call_dcg/3 predicate. - Add and optimize integration test suites.
|
Ready to review this PR? Stage has broken it down into 14 individual chapters for you: Chapters generated by Stage for commit 864219a on May 20, 2026 8:16am UTC. |
There was a problem hiding this comment.
🟡 Parser parseIntegerLiteral fails on negative non-decimal numbers produced by lexer
The lexer's new negative number literal path (lines 443-457) explicitly handles negative non-decimal prefixes like -0b, -0o, -0x, producing tokens with values like "-0b101". However, the parser's parseIntegerLiteral at src/parser.zig:128 checks clean_value[0] == '0' to detect ISO syntax, which fails when the first character is -. The code falls through to std.fmt.parseInt(i64, clean_value, 10) at line 158, which will error on "-0b101" since that's not a valid decimal string. This causes a parse error for any negative binary, octal, or hex literal.
(Refers to lines 128-139)
Was this helpful? React with 👍 or 👎 to provide feedback.
| const can_be_negative_literal = self.last_token_type != .number and | ||
| self.last_token_type != .rparen and | ||
| self.last_token_type != .rbracket; |
There was a problem hiding this comment.
🔴 Incomplete last_token_type tracking causes incorrect negative number literal detection
The new last_token_type field in Lexer is only updated when makeToken() is called, which only happens for .number and .minus tokens (12 of 79 token return sites). All other tokens—atoms, variables, rparen, rbracket, operators, strings, etc.—return Token{...} directly and leave last_token_type at its previous value.
This means the negative literal check at src/lexer.zig:435-437 is unreliable. For example, a-3 should produce atom("a"), minus, number(3) but instead produces atom("a"), number(-3) because the atom token didn't update last_token_type (it remains .eof), so can_be_negative_literal is true. Similarly, X-3, f(x)-3, etc. all produce incorrect token sequences. On the other hand, 1+(-3) incorrectly treats -3 as subtraction because last_token_type is .number from the 1, even though the + and ( tokens didn't update it.
Tokens that bypass makeToken (sampling)
Atoms (line 758), variables (line 766), rparen (line 375), rbracket (line 377), plus (line 416), all comparison operators, string tokens, etc.
Prompt for agents
The root cause is that only makeToken() updates last_token_type, but the vast majority of tokens (67 of 79 return sites) use direct Token{...} returns. The fix should ensure ALL token returns in the next() function update last_token_type. The simplest approach: change next() so that all return paths go through makeToken(), OR add a wrapper that sets last_token_type before every return. For example, at the END of the next() function body, replace all direct `return Token{ .tag = X, ... }` with `return self.makeToken(X, ...)`. This affects approximately 67 return sites throughout src/lexer.zig's next() function. Alternatively, you could add a deferred assignment at the top of next() that captures the token and sets last_token_type, but this is harder since next() has many early returns.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Pull request overview
This PR migrates Ziglog to Zig v0.16.0 APIs, updates the vendored isocline dependency to v1.1.0, and expands ISO-focused integration tests/documentation while extending the language surface (lexer/parser/arithmetic) to cover additional ISO behavior.
Changes:
- Ported runtime/test I/O and entrypoints to Zig v0.16.0 (
std.process.Init,std.Io, new writers). - Updated vendored isocline sources to v1.1.0 and refreshed integration glue/docs.
- Expanded integration tests and docs for arithmetic extensions, occurs check, soft cut, term variable extraction, dynamic DB, and collection predicates.
Reviewed changes
Copilot reviewed 41 out of 42 changed files in this pull request and generated 12 comments.
Show a summary per file
| File | Description |
|---|---|
| build.zig | Build updates for Zig v0.16.0 + build_options version injection + isocline C compilation via module. |
| src/main.zig | REPL entrypoint migrated to std.process.Init, updated output and file loading to new I/O APIs. |
| src/test_runner.zig | Integration test runner migrated to std.Io and allocating writer API. |
| src/lexer.zig | Added ** token, *-> token, and negative-literal tokenization logic (stateful). |
| src/parser.zig | Added power precedence, *-> precedence, =.. infix handling, and Zig v0.16 container init updates. |
| src/arithmetic.zig | Added rounding/math/trig/power/atan2 functions and ISO min/max type preservation. |
| src/ast.zig | Added SWI-compat formatting mode and migrated formatting/writer APIs for Zig v0.16. |
| src/highlighter.zig | Highlighting for *-> and **. |
| src/indexing.zig | Zig v0.16 container init updates (.empty). |
| src/integration_test_main.zig | Zig v0.16 main(init: std.process.Init) and added new integration test files to the suite. |
| tests/README.md | Reworked testing documentation around ISO Phase 1 and new runners. |
| tests/integration/arithmetic.pl | Extended expectations for ISO-preserving abs/sign and min/max mixed-type behavior. |
| tests/integration/floats.pl | Adjusted min/max mixed-type expectation to ISO “winner preserves type”. |
| tests/integration/tail_call_optimization.pl | Reduced recursion depth for CI compatibility. |
| tests/integration/dynamic.pl | New integration tests for assert/retract/abolish/clause. |
| tests/integration/collection.pl | New integration tests for findall/bagof/setof. |
| tests/integration/occurs_check.pl | New integration tests for unify_with_occurs_check/2 and acyclic_term/1. |
| tests/integration/math.pl | New integration tests for advanced arithmetic functions. |
| tests/integration/term_variables.pl | New integration tests for term_variables/2. |
| tests/integration/soft_cut.pl | New integration tests for soft cut *->. |
| README.md | Updated docs for isocline REPL, expanded predicate/function coverage, and updated test counts. |
| CLAUDE.md | Updated architecture/testing docs to reflect new predicates/features and isocline usage. |
| docs/isocline-integration-options.md | Updated status notes for isocline integration and documentation references. |
| CHANGELOG.md | Large changelog update documenting Zig v0.16 migration, isocline v1.1.0 update, and feature additions. |
| src/isocline/UPSTREAM_VERSION | Updated vendored isocline version metadata. |
| src/isocline/isocline.h | Updated isocline headers for v1.1.0 (plus formatting updates). |
| src/isocline/isocline.c | Updated isocline implementation for v1.1.0. |
| src/isocline/common.h | Include path normalization/comment cleanup. |
| src/isocline/common.c | UTF-8 decoding correctness adjustments. |
| src/isocline/tty.h | Added tty_is_atty declaration + formatting fixes. |
| src/isocline/tty.c | Added tty_is_atty implementation + formatting changes. |
| src/isocline/term.c | Updated tty/term behavior (atty detection, stderr handling) + formatting fixes. |
| src/isocline/stringbuf.h | Added ic_count_end_overlap declaration. |
| src/isocline/stringbuf.c | Added ic_count_end_overlap implementation and split-at guard. |
| src/isocline/completions.h | Added completion apply “magic return values” constants. |
| src/isocline/completions.c | Updated completion apply semantics (noop vs fail) and overlap handling. |
| src/isocline/completers.c | Updated completers to account for postfix overlap (delete_after adjustment). |
| src/isocline/history.c | Windows CR handling and formatting cleanup. |
| src/isocline/editline.c | Adjusted cancel behavior (Ctrl+C handling) to match upstream changes. |
| src/isocline/editline_help.c | Updated help banner for v1.1. |
| src/isocline/editline_completion.c | Updated completion commit/refresh behavior and longest-prefix completion flow. |
Comments suppressed due to low confidence (2)
src/parser.zig:202
- The parser treats the new power operator (**) using the same left-associative loop as other infix operators. In ISO Prolog, exponentiation is right-associative (xfy), so
2 ** 3 ** 2should parse as2 ** (3 ** 2). Consider adding right-associative handling for.powerin parseExpression (e.g., parse the RHS with a slightly lower precedence) so AST shape matches Prolog semantics.
tests/integration/soft_cut.pl:40 - Test 7 has no
% EXPECT:directive. Since the then-branch isfail, this query should deterministically fail; add% EXPECT: falseso the test suite asserts the behavior instead of just printing empty output.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| self.last_token_type = tag; | ||
| return Token{ .tag = tag, .value = value, .start = start }; | ||
| } | ||
|
|
| // Only treat it as a negative literal if it doesn't follow a number/rparen/rbracket | ||
| // This matches ISO Prolog behavior: -5 is a literal, but 10-5 is subtraction | ||
| const can_be_negative_literal = self.last_token_type != .number and | ||
| self.last_token_type != .rparen and | ||
| self.last_token_type != .rbracket; |
| const rc = std.posix.system.write(std.posix.STDOUT_FILENO, text.ptr, text.len); | ||
| if (rc < 0) return error.WriteFailed; |
| // Parse command-line arguments | ||
| var args = init.minimal.args.iterate(); | ||
| _ = args.skip(); // Skip program name | ||
|
|
||
| while (args.next()) |arg| { | ||
| if (std.mem.eql(u8, arg, "--swi-compat")) { | ||
| ast.swi_compat_mode = true; | ||
| } else if (std.mem.eql(u8, arg, "--version") or std.mem.eql(u8, arg, "-V")) { | ||
| try rawPrint(version); | ||
| try rawPrint("\n"); | ||
| return; | ||
| } else if (std.mem.eql(u8, arg, "--help") or std.mem.eql(u8, arg, "-h")) { | ||
| try rawPrint("Ziglog - A Prolog interpreter written in Zig\n\n"); | ||
| try rawPrint("Usage: ziglog [OPTIONS]\n\n"); | ||
| try rawPrint("Options:\n"); | ||
| try rawPrint(" --swi-compat Enable SWI-Prolog compatibility mode (no spaces after commas, no quotes on strings, etc.)\n"); | ||
| try rawPrint(" --version, -V Show version number\n"); | ||
| try rawPrint(" --help, -h Show this help message\n"); | ||
| return; | ||
| } else { | ||
| try rawPrint("Unknown option: "); | ||
| try rawPrint(arg); | ||
| try rawPrint("\nUse --help for usage information.\n"); | ||
| return error.InvalidArgument; | ||
| } | ||
| } |
| // Rounding functions - always return integer | ||
| if (std.mem.eql(u8, s.functor, "floor")) { | ||
| const f = arg.toFloat(); | ||
| return .{ .int = @intFromFloat(@floor(f)) }; | ||
| } | ||
| if (std.mem.eql(u8, s.functor, "ceiling")) { | ||
| const f = arg.toFloat(); | ||
| return .{ .int = @intFromFloat(@ceil(f)) }; | ||
| } | ||
| if (std.mem.eql(u8, s.functor, "round")) { | ||
| const f = arg.toFloat(); | ||
| return .{ .int = @intFromFloat(@round(f)) }; | ||
| } | ||
| if (std.mem.eql(u8, s.functor, "truncate")) { | ||
| const f = arg.toFloat(); | ||
| return .{ .int = @intFromFloat(@trunc(f)) }; | ||
| } |
| % Test 29: Complex acyclic structure | ||
| % EXPECT: X = f(g(h(i(j(k(l(m(n(o(p(a))))))))))) | ||
| ?- X = f(g(h(i(j(k(l(m(n(o(p(a))))))))))), acyclic_term(X). | ||
|
|
| % Test 2: Soft cut with failure in condition | ||
| ?- choice(4) *-> result(Y). | ||
|
|
| % Test 21: With anonymous variables (treated as same variable) | ||
| % EXPECT: Vars = [_] | ||
| ?- term_variables(f(_, _), Vars). | ||
|
|
| Isocline is a pure C library that can be used as an alternative to the GNU readline library. | ||
|
|
||
| See the [Github repository](https://github.com/daanx/isocline#readme) | ||
| See the [Github repository](https://github.com/daanx/isocline#readme) |
| const std = @import("std"); | ||
| const zon = @import("build.zig.zon"); | ||
|
|
||
| pub fn build(b: *std.Build) void { | ||
| const target = b.standardTargetOptions(.{}); | ||
| const optimize = b.standardOptimizeOption(.{}); | ||
|
|
||
| // Get version from build.zig.zon | ||
| const version: []const u8 = zon.version; | ||
|
|
There was a problem hiding this comment.
Risk-prioritized pass focused on merge-blocking issues for this large PR.
Blocking feedback
- CI is still pinned to Zig 0.15.2 while this PR adopts 0.16-only entrypoint APIs (
std.process.Init), so the test matrix fails before exercising runtime behavior — src/main.zig#L120.
If you want me to push a fix, reply with please fix 1.
| var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
| defer arena.deinit(); | ||
| const alloc = arena.allocator(); | ||
| pub fn main(init: std.process.Init) !void { |
There was a problem hiding this comment.
This switches the entrypoint to Zig 0.16's main(init: std.process.Init).
Current CI still installs Zig 0.15.2 (.github/workflows/ci.yml), which is why all Test on * jobs are failing with std.process.Init missing.
Suggested fix: bump both setup-zig steps in CI to the same 0.16.x version targeted by this PR (or add compatibility code if 0.15.x support is still required).
There was a problem hiding this comment.
Blocking feedback
- Zig 0.16 entrypoint migration breaks the current CI/release toolchain (still pinned to 0.15.2), so the PR head does not compile on ubuntu/macos/windows checks — src/main.zig#L120 · src/integration_test_main.zig#L4.
- Negative-literal context tracking is unsafe because
last_token_typestays stale for most tokens, causing-to be tokenized incorrectly in regular expressions — src/lexer.zig#L435-L437. - The new lexer emits signed radix literals (
-0b...,-0x...,-0o...) that the parser cannot parse, producing parse errors for those forms — src/lexer.zig#L443-L457 · src/parser.zig#L128-L158.
If you want me to push fixes, reply with item numbers (for example: please fix 1,3).
| var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
| defer arena.deinit(); | ||
| const alloc = arena.allocator(); | ||
| pub fn main(init: std.process.Init) !void { |
There was a problem hiding this comment.
This migration to std.process.Init is 0.16-only, but CI/release workflows are still pinned to Zig 0.15.2. The current head fails to compile on ubuntu/macos/windows before tests run.
Suggested fix: bump workflow Zig versions to 0.16.x in this PR (or keep 0.15-compatible entrypoint signatures until the workflow bump lands).
| // Check if followed immediately by a digit - if so, it MIGHT be a negative number literal | ||
| // Only treat it as a negative literal if it doesn't follow a number/rparen/rbracket | ||
| // This matches ISO Prolog behavior: -5 is a literal, but 10-5 is subtraction | ||
| const can_be_negative_literal = self.last_token_type != .number and |
There was a problem hiding this comment.
can_be_negative_literal depends on last_token_type, but only makeToken() updates that field and most next() branches still return Token{...} directly. That leaves stale context and misclassifies - in common cases.
Suggested fix: route all token returns in next() through makeToken() (or centralize assignment so every return updates last_token_type).
| // Parse the number (same logic as positive numbers below) | ||
| const first_digit = self.source[self.pos]; | ||
|
|
||
| // Check for ISO syntax: 0b, 0o, 0x (after the minus) |
There was a problem hiding this comment.
This path emits number tokens like -0b101 / -0xFF, but parseIntegerLiteral only recognizes radix prefixes when the first byte is 0 (src/parser.zig lines 128-158). These tokens fall through to base-10 parsing and fail.
Suggested fix: either keep - separate for non-decimal literals, or update parseIntegerLiteral to strip an optional sign before radix-prefix handling.
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
abf52c6 to
864219a
Compare
|
Done — I fixed items 1, 2, and 3 and pushed the update in 864219a.
Validation run:
|
Uh oh!
There was an error while loading. Please reload this page.