datalog: parser accepts negative integer literals (248/248)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 42s

Bug: `n(-1).` failed to parse — the tokenizer produced op `-`
followed by number `1`, and dl-pp-parse-arg expected a term after
seeing `-` as an op (and a `(` for a compound) but found a bare
number. Users had to write `(- 0 1)` or compute via `is`.

Fix: dl-pp-parse-arg detects op `-` directly followed by a number
token (no intervening `(`) and consumes both as a single negative
number literal. Subtraction (`is(Y, -(X, 2))`) and compound
arithmetic via the operator form are unaffected — they use the
`-(` lookahead path.

2 new parser tests: negative integer literal and subtraction
compound preserved.
This commit is contained in:
2026-05-10 20:55:42 +00:00
parent 2c8c1f75b3
commit 2a1d8eeab2
4 changed files with 31 additions and 6 deletions

View File

@@ -91,6 +91,16 @@
((= ty "number") (do (dl-pp-advance! st) vv))
((= ty "string") (do (dl-pp-advance! st) vv))
((= ty "var") (do (dl-pp-advance! st) (string->symbol vv)))
;; Negative numeric literal: `-` op directly followed by a
;; number (no `(`) is parsed as a single negative number.
;; This keeps `(-X Y)` (compound) and `-N` (literal) distinct.
((and (= ty "op") (= vv "-")
(= (get (dl-pp-peek2 st) :type) "number"))
(do
(dl-pp-advance! st)
(let
((n (get (dl-pp-peek st) :value)))
(do (dl-pp-advance! st) (- 0 n)))))
((or (= ty "atom") (= ty "op"))
(do
(dl-pp-advance! st)

View File

@@ -1,11 +1,11 @@
{
"lang": "datalog",
"total_passed": 246,
"total_passed": 248,
"total_failed": 0,
"total": 246,
"total": 248,
"suites": [
{"name":"tokenize","passed":26,"failed":0,"total":26},
{"name":"parse","passed":20,"failed":0,"total":20},
{"name":"parse","passed":22,"failed":0,"total":22},
{"name":"unify","passed":28,"failed":0,"total":28},
{"name":"eval","passed":36,"failed":0,"total":36},
{"name":"builtins","passed":23,"failed":0,"total":23},
@@ -16,5 +16,5 @@
{"name":"magic","passed":34,"failed":0,"total":34},
{"name":"demo","passed":21,"failed":0,"total":21}
],
"generated": "2026-05-10T20:51:33+00:00"
"generated": "2026-05-10T20:55:23+00:00"
}

View File

@@ -1,11 +1,11 @@
# datalog scoreboard
**246 / 246 passing** (0 failure(s)).
**248 / 248 passing** (0 failure(s)).
| Suite | Passed | Total | Status |
|-------|--------|-------|--------|
| tokenize | 26 | 26 | ok |
| parse | 20 | 20 | ok |
| parse | 22 | 22 | ok |
| unify | 28 | 28 | ok |
| eval | 36 | 36 | ok |
| builtins | 23 | 23 | ok |

View File

@@ -126,6 +126,21 @@
"underscore var"
(dl-parse "p(X) :- q(X, _).")
(list {:body (list (list (quote q) (quote X) (quote _))) :head (list (quote p) (quote X))}))
;; Negative number literals parse as one negative number,
;; while subtraction (`-(X, Y)`) compound is preserved.
(dl-pt-test!
"negative integer literal"
(dl-parse "n(-3).")
(list {:head (list (quote n) -3) :body (list)}))
(dl-pt-test!
"subtraction compound preserved"
(dl-parse "r(X) :- is(X, -(10, 3)).")
(list
{:head (list (quote r) (quote X))
:body (list (list (quote is) (quote X)
(list (string->symbol "-") 10 3)))}))
(dl-pt-test!
"number as relation name raises"
(dl-pt-throws? (fn () (dl-parse "1(X) :- p(X).")))