parent:
5679446275e9c90525ed7267e7f42f633296e7a1
nmyk <nick@nmyk.io>
2026-11-14T13:56:56-05:00
Add errors, source-mode test runner, govlisp repl
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..e660fd93d3196215552065b1e63bf6a2f393ed86
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+bin/
diff --git a/DESIGN.md b/DESIGN.md
index b0eff7a836a4f710ec4f7882353facccb459b621..70cf23920b10b815ee23aa54683975a5bf91e8ea 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -319,6 +319,8 @@ | `government repeal prog.gov -depth i -o repeal.crl` | Write a CRL repealing depth i (`-key`, `-reason`, `-at`, `-next-update`) |
Shared flags: `-at <RFC 3339 time>`, `-anchor <cert or public key PEM>`, `-crl <file>` (repeatable), `-max-nodes <n>`.
+A second command, `govlisp`, runs Lisp source directly, with no certificates: `govlisp` alone is a REPL (multi-line forms, errors reported without ending the session), and `govlisp prog.lisp` runs a file, printing each non-`define` value and reporting the first error at its source line and column.
+
## Transpiler and test suite
Every language test is a plain `.lisp` file with expected results in comments; the runner transpiles it to `.gov` bytes and runs the full interpreter pipeline on them.
diff --git a/Makefile b/Makefile
index 95047882cff6075133c17a679014bd9f299233ec..1fa724fccf5a995a0c6b874a4167b1300e1c8b38 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-.PHONY: check test vet fmt
+.PHONY: check test vet fmt bin install clean
check: fmt vet test
@@ -10,4 +10,15 @@ go vet ./...
fmt:
@unformatted="$$(gofmt -l .)"; \
- if [ -n "$$unformatted" ]; then echo "gofmt needed:"; echo "$$unformatted"; exit 1; fi
\ No newline at end of file
+ if [ -n "$$unformatted" ]; then echo "gofmt needed:"; echo "$$unformatted"; exit 1; fi
+
+# Build every command (government, govlisp) into ./bin.
+bin:
+ go build -o bin/ ./cmd/...
+
+# Install every command into $(go env GOBIN), or $(go env GOPATH)/bin if unset.
+install:
+ go install ./cmd/...
+
+clean:
+ rm -rf bin
\ No newline at end of file
diff --git a/PLAN.md b/PLAN.md
index 7e18e5e788461c352b2569422b73aa35f2e3a701..ed5bb3a4524049636e669b68c4f968a1bec2041a 100644
--- a/PLAN.md
+++ b/PLAN.md
@@ -91,6 +91,8 @@ - Build: typed errors with codes (`E-UNBOUND`, `E-ARITY`, …) and an optional source location; a test runner that reads `testdata/*.lisp`, parses `;=>` and `;=!` annotations, and runs each top-level form through reader + evaluator.
- Learn: designing errors as data; golden-style tests that read like documentation.
- Done when: the design doc's example test files all pass. **Milestone: the language works.**
+**Extra: the `govlisp` REPL** (`cmd/govlisp`). Added between Phases 1 and 2. `govlisp` with no arguments is an interactive REPL; `govlisp prog.lisp` runs a file. A form may span lines: the reader marks syntax errors caused only by input ending early as incomplete, and the REPL reads another line instead of reporting them. Errors name the failing subexpression and keep the session going.
+
## Phase 2: The AST and OID encoding (steps 14–17)
Define the program tree that certificates will carry, and prove it round-trips, before any certificate exists. Everything here is pure data structures, so it is fast to test.
diff --git a/cmd/govlisp/lines.go b/cmd/govlisp/lines.go
new file mode 100644
index 0000000000000000000000000000000000000000..856923c765037bf19bf28f9509b29af033772449
--- /dev/null
+++ b/cmd/govlisp/lines.go
@@ -0,0 +1,30 @@
+package main
+
+import (
+ "bufio"
+ "io"
+)
+
+// lineReader yields input one line at a time, without the line ending. It
+// has no limit on line length, unlike bufio.Scanner's default.
+type lineReader struct {
+ r *bufio.Reader
+}
+
+func newLineReader(in io.Reader) *lineReader {
+ return &lineReader{r: bufio.NewReader(in)}
+}
+
+func (l *lineReader) next() (string, bool) {
+ line, err := l.r.ReadString('\n')
+ if err != nil && line == "" {
+ return "", false
+ }
+ if n := len(line); n > 0 && line[n-1] == '\n' {
+ line = line[:n-1]
+ if n := len(line); n > 0 && line[n-1] == '\r' {
+ line = line[:n-1]
+ }
+ }
+ return line, true
+}
diff --git a/cmd/govlisp/main.go b/cmd/govlisp/main.go
new file mode 100644
index 0000000000000000000000000000000000000000..e4a18621d89b1398fb969f0b1e17c4d46ff4b5ea
--- /dev/null
+++ b/cmd/govlisp/main.go
@@ -0,0 +1,159 @@
+// Command govlisp runs GOVERNMENT programs straight from Lisp source, with
+// no certificates involved.
+//
+// govlisp interactive REPL
+// govlisp prog.lisp run a file, printing the value of each non-define form
+//
+// The REPL has no line editing; wrap it with rlwrap for history and arrow
+// keys.
+package main
+
+import (
+ "fmt"
+ "io"
+ "os"
+ "strings"
+
+ "government/eval"
+ "government/sexpr"
+)
+
+const (
+ exitOK = 0
+ exitRuntime = 1
+ exitSyntax = 2
+)
+
+func main() {
+ switch len(os.Args) {
+ case 1:
+ os.Exit(repl(os.Stdin, os.Stdout, isTerminal(os.Stdin)))
+ case 2:
+ os.Exit(runFile(os.Args[1], os.Stdout, os.Stderr))
+ default:
+ fmt.Fprintln(os.Stderr, "usage: govlisp [file.lisp]")
+ os.Exit(exitSyntax)
+ }
+}
+
+func isTerminal(f *os.File) bool {
+ info, err := f.Stat()
+ return err == nil && info.Mode()&os.ModeCharDevice != 0
+}
+
+// runFile evaluates every top-level form in path, printing each non-define
+// value. It stops at the first error, which it reports with the source
+// position of the subexpression that failed.
+func runFile(path string, stdout, stderr io.Writer) int {
+ src, err := os.ReadFile(path)
+ if err != nil {
+ fmt.Fprintln(stderr, err)
+ return exitSyntax
+ }
+ r := sexpr.NewReader(string(src))
+ var forms []sexpr.Value
+ var starts []sexpr.Pos
+ for {
+ v, pos, err := r.ReadWithPos()
+ if err != nil {
+ fmt.Fprintf(stderr, "%s:%v\n", path, err)
+ return exitSyntax
+ }
+ if v == nil {
+ break
+ }
+ forms = append(forms, v)
+ starts = append(starts, pos)
+ }
+
+ top := eval.NewTopLevel()
+ for i, form := range forms {
+ v, err := top.Eval(form)
+ if err != nil {
+ pos := starts[i]
+ if e, ok := err.(*eval.Error); ok {
+ if p, ok := r.Position(e.Expr); ok {
+ pos = p
+ }
+ }
+ fmt.Fprintf(stderr, "%s:%v: %v\n", path, pos, err)
+ return exitRuntime
+ }
+ if !isDefine(form) {
+ fmt.Fprintln(stdout, sexpr.Print(v))
+ }
+ }
+ return exitOK
+}
+
+// repl reads forms from in until end of input. A form may span several
+// lines: while the input so far is an unfinished form, it keeps reading.
+// An error is reported and the session carries on; definitions made before
+// it are kept.
+func repl(in io.Reader, out io.Writer, prompts bool) int {
+ const (
+ prompt = "gov> "
+ continuation = "...> "
+ )
+ top := eval.NewTopLevel()
+ lines := newLineReader(in)
+ var entry strings.Builder
+
+ for {
+ if prompts {
+ if entry.Len() == 0 {
+ fmt.Fprint(out, prompt)
+ } else {
+ fmt.Fprint(out, continuation)
+ }
+ }
+ line, ok := lines.next()
+ if !ok {
+ if entry.Len() > 0 {
+ if _, err := sexpr.ReadAll(entry.String()); err != nil {
+ fmt.Fprintf(out, "syntax error: %v\n", err)
+ }
+ }
+ if prompts {
+ fmt.Fprintln(out)
+ }
+ return exitOK
+ }
+ entry.WriteString(line)
+ entry.WriteByte('\n')
+
+ forms, err := sexpr.ReadAll(entry.String())
+ if se, ok := err.(*sexpr.SyntaxError); ok && se.Incomplete {
+ continue
+ }
+ entry.Reset()
+ if err != nil {
+ fmt.Fprintf(out, "syntax error: %v\n", err)
+ continue
+ }
+ for _, form := range forms {
+ v, err := top.Eval(form)
+ if err != nil {
+ fmt.Fprintln(out, describe(err))
+ break
+ }
+ if !isDefine(form) {
+ fmt.Fprintln(out, sexpr.Print(v))
+ }
+ }
+ }
+}
+
+// describe adds the subexpression that failed to an evaluation error.
+func describe(err error) string {
+ e, ok := err.(*eval.Error)
+ if !ok || e.Expr == nil {
+ return err.Error()
+ }
+ return fmt.Sprintf("%v\n in %s", err, sexpr.Print(e.Expr))
+}
+
+func isDefine(form sexpr.Value) bool {
+ p, ok := form.(*sexpr.Pair)
+ return ok && p.Car == sexpr.Value(sexpr.Symbol("define"))
+}
diff --git a/cmd/govlisp/main_test.go b/cmd/govlisp/main_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..3a711ee50dbadacf350a412b97bd10f707305184
--- /dev/null
+++ b/cmd/govlisp/main_test.go
@@ -0,0 +1,105 @@
+package main
+
+import (
+ "bytes"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func runREPL(t *testing.T, input string, prompts bool) string {
+ t.Helper()
+ var out bytes.Buffer
+ if code := repl(strings.NewReader(input), &out, prompts); code != exitOK {
+ t.Fatalf("repl exited with %d", code)
+ }
+ return out.String()
+}
+
+func TestREPL(t *testing.T) {
+ input := `(+ 1 2)
+(define sq (lambda (n) (* n n)))
+(sq 7)
+sq
+1 2 'three
+(define fact (lambda (n)
+ (if (== n 0)
+ 1
+ (* n (fact (- n 1))))))
+(fact 10)
+"multi
+line"
+; just a comment
+
+(car '())
+(sq 3)
+(define sq 1)
+(+ 1 (car 5)) (sq 2)
+)
+(sq 4)
+`
+ want := `3
+49
+#<lambda sq>
+1
+2
+three
+3628800
+"multi\nline"
+E-TYPE: car needs a pair, got ()
+ in (car (quote ()))
+9
+E-IMMUTABLE: sq is already defined in this scope; bindings are immutable
+ in (define sq 1)
+E-TYPE: car needs a pair, got 5
+ in (car 5)
+syntax error: 1:1: unexpected )
+16
+`
+ if got := runREPL(t, input, false); got != want {
+ t.Errorf("transcript:\n%s\nwant:\n%s", got, want)
+ }
+}
+
+func TestREPLPrompts(t *testing.T) {
+ got := runREPL(t, "(+ 1\n2)\n", true)
+ want := "gov> ...> 3\ngov> \n"
+ if got != want {
+ t.Errorf("got %q, want %q", got, want)
+ }
+}
+
+func TestREPLUnfinishedInputAtEOF(t *testing.T) {
+ got := runREPL(t, "(+ 1\n", false)
+ if want := "syntax error: 1:1: unterminated list\n"; got != want {
+ t.Errorf("got %q, want %q", got, want)
+ }
+}
+
+func TestRunFile(t *testing.T) {
+ tests := []struct {
+ name, src, stdout, stderr string
+ code int
+ }{
+ {"ok", "(define x 2)\n(* x 21)\n'done\n", "42\ndone\n", "", exitOK},
+ {"runtime error", "(define f (lambda (l) (car l)))\n1\n(f\n ())\n2\n", "1\n", "prog.lisp:1:23: E-TYPE: car needs a pair, got ()\n", exitRuntime},
+ {"syntax error", "1\n(+ 2\n", "", "prog.lisp:2:1: unterminated list\n", exitSyntax},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ dir := t.TempDir()
+ path := filepath.Join(dir, "prog.lisp")
+ if err := os.WriteFile(path, []byte(tt.src), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ var stdout, stderr bytes.Buffer
+ code := runFile(path, &stdout, &stderr)
+ gotErr := strings.ReplaceAll(stderr.String(), dir+string(filepath.Separator), "")
+ if code != tt.code || stdout.String() != tt.stdout || gotErr != tt.stderr {
+ t.Errorf("code %d stdout %q stderr %q\nwant code %d stdout %q stderr %q",
+ code, stdout.String(), gotErr, tt.code, tt.stdout, tt.stderr)
+ }
+ })
+ }
+}
diff --git a/eval/closure.go b/eval/closure.go
index 2f029f1426844149f8de2f5355d0240c31ff561e..9cf8d3722c6a0f0f05b338bf2a8e32b788a50dd2 100644
--- a/eval/closure.go
+++ b/eval/closure.go
@@ -109,6 +109,7 @@ for _, form := range c.body[:last] {
var err error
if items, ok := defineForm(form); ok {
_, err = s.define(items)
+ err = at(err, form)
} else {
_, err = Eval(form, s.env)
}
diff --git a/eval/define.go b/eval/define.go
index 710cebf43f8d2ebbd0a851e45f758c9229605524..6d08815ff504a56096890971a2a2ffac680cecab 100644
--- a/eval/define.go
+++ b/eval/define.go
@@ -65,7 +65,8 @@ }
func (t *TopLevel) Eval(form sexpr.Value) (sexpr.Value, error) {
if items, ok := defineForm(form); ok {
- return t.scope.define(items)
+ v, err := t.scope.define(items)
+ return v, at(err, form)
}
return Eval(form, t.scope.env)
}
diff --git a/eval/error.go b/eval/error.go
index 633f8b7dc7104cbbc37f131f79f20c74e6ccae6d..f079fc6efa1d58be0690986855d547b7743890c2 100644
--- a/eval/error.go
+++ b/eval/error.go
@@ -1,8 +1,12 @@
package eval
-import "fmt"
+import (
+ "fmt"
-// Error codes. Step 1.13 adds source locations; the codes are stable.
+ "government/sexpr"
+)
+
+// Error codes.
const (
ErrUnbound = "E-UNBOUND"
ErrSyntax = "E-SYNTAX"
@@ -17,9 +21,23 @@
type Error struct {
Code string
Msg string
+ // Expr is the innermost list being evaluated when the error arose. The
+ // evaluator does not know about source text; whoever produced the code
+ // maps Expr back to a location (the reader gives line:col, and a .gov
+ // program will give the node's OID).
+ Expr sexpr.Value
}
func (e *Error) Error() string { return e.Code + ": " + e.Msg }
+
+// at records expr as the error's location unless an inner expression was
+// already recorded.
+func at(err error, expr sexpr.Value) error {
+ if e, ok := err.(*Error); ok && e.Expr == nil {
+ e.Expr = expr
+ }
+ return err
+}
func errorf(code, format string, args ...any) *Error {
return &Error{Code: code, Msg: fmt.Sprintf(format, args...)}
diff --git a/eval/error_test.go b/eval/error_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..b346460f54d76382dae484bf2fa537a7db986160
--- /dev/null
+++ b/eval/error_test.go
@@ -0,0 +1,43 @@
+package eval
+
+import (
+ "errors"
+ "testing"
+
+ "government/sexpr"
+)
+
+// Errors record the innermost list being evaluated, so a caller can point
+// at the exact subexpression that failed.
+func TestErrorLocation(t *testing.T) {
+ tests := []struct{ src, where string }{
+ {"(car '())", "(car (quote ()))"},
+ {"(+ 1 (car '()))", "(car (quote ()))"},
+ {"(+ 1 (* 2 (car '())))", "(car (quote ()))"},
+ {"(+ 1 boom)", "(+ 1 boom)"},
+ {"(if)", "(if)"},
+ {"(if T (if))", "(if)"},
+ {"(1 2)", "(1 2)"},
+ {"(define 1 2)", "(define 1 2)"},
+ {"(define f (lambda (x) (car x))) (f 1)", "(car x)"},
+ {"(define f (lambda (x) (define y (car x)) y)) (f 1)", "(car x)"},
+ {"(define f (lambda () (define 1 2) 3)) (f)", "(define 1 2)"},
+ {"(and T (/ 1 0))", "(/ 1 0)"},
+ {"((lambda (x) x))", "((lambda (x) x))"},
+ }
+ for _, tt := range tests {
+ t.Run(tt.src, func(t *testing.T) {
+ _, err := evalString(t, tt.src)
+ var e *Error
+ if !errors.As(err, &e) {
+ t.Fatalf("Eval(%s) error = %v, want *Error", tt.src, err)
+ }
+ if e.Expr == nil {
+ t.Fatalf("Eval(%s): error %v has no location", tt.src, err)
+ }
+ if got := sexpr.Print(e.Expr); got != tt.where {
+ t.Errorf("Eval(%s) error location = %s, want %s", tt.src, got, tt.where)
+ }
+ })
+ }
+}
diff --git a/eval/eval.go b/eval/eval.go
index b9f39c62656fd63ce75d5b909c46d9e3aa759c26..97081fb00624ca10e094a83f55f51fbe76a10cff 100644
--- a/eval/eval.go
+++ b/eval/eval.go
@@ -64,13 +64,13 @@ }
items, ok := properList(list)
if !ok {
- return nil, errorf(ErrSyntax, "cannot evaluate improper list %s", sexpr.Print(list))
+ return nil, at(errorf(ErrSyntax, "cannot evaluate improper list %s", sexpr.Print(list)), list)
}
if head, ok := items[0].(sexpr.Symbol); ok {
if form, ok := specialForms[head]; ok {
out, err := form(items[1:], env)
if err != nil {
- return nil, err
+ return nil, at(err, list)
}
if out.tail == nil {
return out.value, nil
@@ -82,17 +82,18 @@ }
f, args, err := evalCall(items, env)
if err != nil {
- return nil, err
+ return nil, at(err, list)
}
switch f := f.(type) {
case *Builtin:
if err := checkArity(f, len(args)); err != nil {
- return nil, err
+ return nil, at(err, list)
}
- return f.fn(args)
+ v, err := f.fn(args)
+ return v, at(err, list)
case *Closure:
if expr, env, err = enterClosure(f, args); err != nil {
- return nil, err
+ return nil, at(err, list)
}
default:
panic("eval: unknown function type")
diff --git a/internal/lisptest/lisptest.go b/internal/lisptest/lisptest.go
new file mode 100644
index 0000000000000000000000000000000000000000..a18e54cdf0cbb6355005cee2bc07948f3c5512f0
--- /dev/null
+++ b/internal/lisptest/lisptest.go
@@ -0,0 +1,151 @@
+// Package lisptest runs GOVERNMENT test files: ordinary Lisp source whose
+// expected results are written in comments after each top-level form.
+//
+// (+ 1 2) ;=> 3
+// (car '()) ;=! car needs a pair
+//
+// ";=> text" means the form's printed value must be exactly text.
+// ";=! text" means evaluating the form must fail with an error whose
+// message contains text, ignoring case. An annotation belongs to the last
+// top-level form that starts before it. A form without one must simply not
+// fail.
+//
+// An error ends only its own form, so one file can hold many error cases.
+package lisptest
+
+import (
+ "fmt"
+ "strings"
+
+ "government/eval"
+ "government/sexpr"
+)
+
+type Expect int
+
+const (
+ ExpectSuccess Expect = iota // no annotation: must not fail
+ ExpectValue // ;=>
+ ExpectError // ;=!
+)
+
+type Case struct {
+ Form sexpr.Value
+ Pos sexpr.Pos
+ Expect Expect
+ Want string
+}
+
+type File struct {
+ Name string
+ Cases []Case
+ reader *sexpr.Reader
+}
+
+func Parse(name, src string) (*File, error) {
+ r := sexpr.NewReader(src)
+ f := &File{Name: name, reader: r}
+ for {
+ v, pos, err := r.ReadWithPos()
+ if err != nil {
+ return nil, fmt.Errorf("%s:%v", name, err)
+ }
+ if v == nil {
+ break
+ }
+ f.Cases = append(f.Cases, Case{Form: v, Pos: pos})
+ }
+
+ for _, c := range r.Comments() {
+ expect, want, ok := annotation(c.Text)
+ if !ok {
+ continue
+ }
+ i := lastCaseBefore(f.Cases, c.Pos)
+ switch {
+ case i < 0:
+ return nil, fmt.Errorf("%s:%v: annotation before any form", name, c.Pos)
+ case f.Cases[i].Expect != ExpectSuccess:
+ return nil, fmt.Errorf("%s:%v: second annotation for the form at %v", name, c.Pos, f.Cases[i].Pos)
+ case want == "":
+ return nil, fmt.Errorf("%s:%v: empty annotation", name, c.Pos)
+ }
+ f.Cases[i].Expect, f.Cases[i].Want = expect, want
+ }
+ return f, nil
+}
+
+func annotation(comment string) (Expect, string, bool) {
+ switch {
+ case strings.HasPrefix(comment, "=>"):
+ return ExpectValue, strings.TrimSpace(comment[2:]), true
+ case strings.HasPrefix(comment, "=!"):
+ return ExpectError, strings.TrimSpace(comment[2:]), true
+ }
+ return ExpectSuccess, "", false
+}
+
+func lastCaseBefore(cases []Case, p sexpr.Pos) int {
+ found := -1
+ for i, c := range cases {
+ if c.Pos.Line < p.Line || (c.Pos.Line == p.Line && c.Pos.Col < p.Col) {
+ found = i
+ }
+ }
+ return found
+}
+
+// Evaluator runs one top-level form, keeping definitions between calls.
+type Evaluator interface {
+ Eval(form sexpr.Value) (sexpr.Value, error)
+}
+
+// Reporter is the part of *testing.T that Run uses.
+type Reporter interface {
+ Helper()
+ Errorf(format string, args ...any)
+}
+
+// Run evaluates every case in order and reports each mismatch.
+func Run(t Reporter, f *File, ev Evaluator) {
+ t.Helper()
+ for _, c := range f.Cases {
+ v, err := ev.Eval(c.Form)
+ at := fmt.Sprintf("%s:%v: %s", f.Name, c.Pos, abbreviate(sexpr.Print(c.Form)))
+ switch {
+ case c.Expect == ExpectError && err == nil:
+ t.Errorf("%s\n\tgot value %s, want an error containing %q", at, sexpr.Print(v), c.Want)
+ case c.Expect == ExpectError:
+ if !strings.Contains(strings.ToLower(err.Error()), strings.ToLower(c.Want)) {
+ t.Errorf("%s\n\tgot error %s, want one containing %q", at, f.Describe(err), c.Want)
+ }
+ case err != nil:
+ t.Errorf("%s\n\tunexpected error %s", at, f.Describe(err))
+ case c.Expect == ExpectValue && sexpr.Print(v) != c.Want:
+ t.Errorf("%s\n\tgot %s\n\twant %s", at, sexpr.Print(v), c.Want)
+ }
+ }
+}
+
+// Describe formats an error with the source position of the subexpression
+// that failed, when the evaluator recorded one.
+func (f *File) Describe(err error) string {
+ e, ok := err.(*eval.Error)
+ if !ok || e.Expr == nil {
+ return err.Error()
+ }
+ pos, ok := f.reader.Position(e.Expr)
+ if !ok {
+ return err.Error()
+ }
+ return fmt.Sprintf("%s (at %v: %s)", err, pos, abbreviate(sexpr.Print(e.Expr)))
+}
+
+func abbreviate(s string) string {
+ const max = 60
+ s = strings.Join(strings.Fields(s), " ")
+ if len(s) > max {
+ return s[:max-3] + "..."
+ }
+ return s
+}
diff --git a/internal/lisptest/lisptest_test.go b/internal/lisptest/lisptest_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..b795a8794bb584d5ed2f7a7355b76a0f67e64ef8
--- /dev/null
+++ b/internal/lisptest/lisptest_test.go
@@ -0,0 +1,87 @@
+package lisptest
+
+import (
+ "fmt"
+ "strings"
+ "testing"
+
+ "government/eval"
+)
+
+type recorder struct{ errors []string }
+
+func (r *recorder) Helper() {}
+func (r *recorder) Errorf(format string, args ...any) {
+ r.errors = append(r.errors, fmt.Sprintf(format, args...))
+}
+
+func TestAnnotationsAttachToPrecedingForm(t *testing.T) {
+ f, err := Parse("t.lisp", `
+(define x 1)
+x ;=> 1
+(car
+ '())
+; a plain comment
+ ;=! needs a pair
+(+ x 1) (+ x 2) ;=> 3
+`)
+ if err != nil {
+ t.Fatal(err)
+ }
+ got := []string{}
+ for _, c := range f.Cases {
+ got = append(got, fmt.Sprintf("%v %d %q", c.Pos, c.Expect, c.Want))
+ }
+ want := []string{
+ `2:1 0 ""`,
+ `3:1 1 "1"`,
+ `4:1 2 "needs a pair"`,
+ `8:1 0 ""`,
+ `8:9 1 "3"`,
+ }
+ if strings.Join(got, "\n") != strings.Join(want, "\n") {
+ t.Errorf("cases:\n%s\nwant:\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
+ }
+}
+
+func TestParseErrors(t *testing.T) {
+ tests := []struct{ src, want string }{
+ {";=> 1\n(+ 1 0)", "t.lisp:1:1: annotation before any form"},
+ {"1 ;=> 1\n;=> 1", "t.lisp:2:1: second annotation for the form at 1:1"},
+ {"1 ;=>", "t.lisp:1:3: empty annotation"},
+ {"(1", "t.lisp:1:1: unterminated list"},
+ }
+ for _, tt := range tests {
+ if _, err := Parse("t.lisp", tt.src); err == nil || err.Error() != tt.want {
+ t.Errorf("Parse(%q) error = %v, want %q", tt.src, err, tt.want)
+ }
+ }
+}
+
+// Every kind of mismatch is reported, with the failing subexpression's
+// position, and evaluation continues past failures.
+func TestRunReportsFailures(t *testing.T) {
+ f, err := Parse("t.lisp", `(+ 1 1) ;=> 3
+(+ 1 1) ;=! overflow
+(car '()) ;=> 1
+(/ 1 0) ;=! not a function
+(+ 1
+ (car 5))
+(+ 2 2) ;=> 4
+`)
+ if err != nil {
+ t.Fatal(err)
+ }
+ r := &recorder{}
+ Run(r, f, eval.NewTopLevel())
+ want := []string{
+ "t.lisp:1:1: (+ 1 1)\n\tgot 2\n\twant 3",
+ "t.lisp:2:1: (+ 1 1)\n\tgot value 2, want an error containing \"overflow\"",
+ "t.lisp:3:1: (car (quote ()))\n\tunexpected error E-TYPE: car needs a pair, got () (at 3:1: (car (quote ())))",
+ "t.lisp:4:1: (/ 1 0)\n\tgot error E-ARITH: division by zero (at 4:1: (/ 1 0)), want one containing \"not a function\"",
+ "t.lisp:5:1: (+ 1 (car 5))\n\tunexpected error E-TYPE: car needs a pair, got 5 (at 6:4: (car 5))",
+ }
+ if strings.Join(r.errors, "\n---\n") != strings.Join(want, "\n---\n") {
+ t.Errorf("reported:\n%s\n\nwant:\n%s", strings.Join(r.errors, "\n---\n"), strings.Join(want, "\n---\n"))
+ }
+}
diff --git a/lisp_test.go b/lisp_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..d256de0e017c5e48a086ec0a4a91d183a9cba81d
--- /dev/null
+++ b/lisp_test.go
@@ -0,0 +1,36 @@
+package government_test
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "government/eval"
+ "government/internal/lisptest"
+)
+
+// TestLispFiles runs every testdata/*.lisp file straight from source text.
+// Step 5.4 switches this to compiling each file to .gov and running the
+// result.
+func TestLispFiles(t *testing.T) {
+ paths, err := filepath.Glob("testdata/*.lisp")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(paths) == 0 {
+ t.Fatal("no testdata/*.lisp files found")
+ }
+ for _, path := range paths {
+ t.Run(filepath.Base(path), func(t *testing.T) {
+ src, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ f, err := lisptest.Parse(path, string(src))
+ if err != nil {
+ t.Fatal(err)
+ }
+ lisptest.Run(t, f, eval.NewTopLevel())
+ })
+ }
+}
diff --git a/sexpr/reader.go b/sexpr/reader.go
index c7edd7e2478425c515339af7003b53f7e461b02b..093c5001ea972e0f6f89ce91eca65ec601b644cf 100644
--- a/sexpr/reader.go
+++ b/sexpr/reader.go
@@ -13,14 +13,34 @@ //
// datum = atom | string | "'" datum | "(" list
// list = ")" | datum { datum } [ "." datum ] ")"
//
-// Comments are skipped for now; step 7.1 keeps them as notes.
+// The reader also records where each list starts, so errors can point at
+// source positions, and keeps the comments it passes, so tests can read
+// their annotations.
type Reader struct {
- lex *Lexer
- peeked *Token
+ lex *Lexer
+ peeked *Token
+ positions map[*Pair]Pos
+ comments []Token
}
func NewReader(src string) *Reader {
- return &Reader{lex: NewLexer(src)}
+ return &Reader{lex: NewLexer(src), positions: map[*Pair]Pos{}}
+}
+
+// Position returns where a list read by r starts: its "(" or, for 'x, its
+// quote mark. Atoms have no recorded position.
+func (r *Reader) Position(v Value) (Pos, bool) {
+ p, ok := v.(*Pair)
+ if !ok {
+ return Pos{}, false
+ }
+ pos, ok := r.positions[p]
+ return pos, ok
+}
+
+// Comments returns the comments read so far, in source order.
+func (r *Reader) Comments() []Token {
+ return r.comments
}
// ReadAll returns every top-level form in src.
@@ -41,14 +61,21 @@ }
// Read returns the next top-level form, or nil at end of input.
func (r *Reader) Read() (Value, error) {
+ v, _, err := r.ReadWithPos()
+ return v, err
+}
+
+// ReadWithPos is Read that also returns where the form starts.
+func (r *Reader) ReadWithPos() (Value, Pos, error) {
t, err := r.peek()
if err != nil {
- return nil, err
+ return nil, Pos{}, err
}
if t.Kind == TokEOF {
- return nil, nil
+ return nil, Pos{}, nil
}
- return r.datum()
+ v, err := r.datum()
+ return v, t.Pos, err
}
func (r *Reader) datum() (Value, error) {
@@ -67,15 +94,19 @@ if err != nil {
return nil, err
}
if next.Kind == TokEOF || next.Kind == TokRParen {
- return nil, &SyntaxError{Pos: t.Pos, Msg: "' must be followed by a value"}
+ return nil, &SyntaxError{Pos: t.Pos, Msg: "' must be followed by a value", Incomplete: next.Kind == TokEOF}
}
v, err := r.datum()
if err != nil {
return nil, err
}
- return List(Symbol("quote"), v), nil
+ return r.record(List(Symbol("quote"), v), t.Pos), nil
case TokLParen:
- return r.list(t.Pos)
+ v, err := r.list(t.Pos)
+ if err != nil {
+ return nil, err
+ }
+ return r.record(v, t.Pos), nil
case TokRParen:
return nil, &SyntaxError{Pos: t.Pos, Msg: "unexpected )"}
case TokDot:
@@ -96,7 +127,7 @@ return nil, err
}
switch t.Kind {
case TokEOF:
- return nil, &SyntaxError{Pos: open, Msg: "unterminated list"}
+ return nil, &SyntaxError{Pos: open, Msg: "unterminated list", Incomplete: true}
case TokRParen:
r.next()
return List(items...), nil
@@ -124,7 +155,7 @@ return nil, err
}
switch t.Kind {
case TokRParen, TokEOF:
- return nil, &SyntaxError{Pos: dot, Msg: "expected a value after ."}
+ return nil, &SyntaxError{Pos: dot, Msg: "expected a value after .", Incomplete: t.Kind == TokEOF}
case TokDot:
return nil, &SyntaxError{Pos: t.Pos, Msg: "only one . is allowed in a list"}
}
@@ -140,15 +171,22 @@ switch t.Kind {
case TokRParen:
return ListWithTail(tail, items...), nil
case TokEOF:
- return nil, &SyntaxError{Pos: open, Msg: "unterminated list"}
+ return nil, &SyntaxError{Pos: open, Msg: "unterminated list", Incomplete: true}
case TokDot:
return nil, &SyntaxError{Pos: t.Pos, Msg: "only one . is allowed in a list"}
}
return nil, &SyntaxError{Pos: t.Pos, Msg: "expected ) after the value following ."}
}
+func (r *Reader) record(v Value, pos Pos) Value {
+ if p, ok := v.(*Pair); ok {
+ r.positions[p] = pos
+ }
+ return v
+}
+
// next and peek give the reader one token of lookahead, which is all this
-// grammar needs (it is LL(1)). Comments are dropped here.
+// grammar needs (it is LL(1)). Comments are set aside here.
func (r *Reader) next() (Token, error) {
if r.peeked != nil {
t := *r.peeked
@@ -160,6 +198,7 @@ t, err := r.lex.Next()
if err != nil || t.Kind != TokComment {
return t, err
}
+ r.comments = append(r.comments, t)
}
}
diff --git a/sexpr/reader_test.go b/sexpr/reader_test.go
index 9fe4b5634a56e2bb00089fa44ff3a5fe88c1d0e5..d209c2107a216d193fc103c58550d38d1286e716 100644
--- a/sexpr/reader_test.go
+++ b/sexpr/reader_test.go
@@ -200,3 +200,96 @@ t.Fatalf("ReadAll(%q) returned %d forms, want 1", src, len(forms))
}
return forms[0]
}
+
+func TestReaderPositions(t *testing.T) {
+ src := "(a\n (b c)\n 'd)\nx\n'(e)"
+ r := NewReader(src)
+ outer, pos, err := r.ReadWithPos()
+ if err != nil || pos != (Pos{1, 1}) {
+ t.Fatalf("first form at %v, err %v; want 1:1", pos, err)
+ }
+ inner := outer.(*Pair).Cdr.(*Pair).Car
+ quoted := outer.(*Pair).Cdr.(*Pair).Cdr.(*Pair).Car
+ for _, tt := range []struct {
+ v Value
+ want Pos
+ }{{outer, Pos{1, 1}}, {inner, Pos{2, 3}}, {quoted, Pos{3, 3}}} {
+ if got, ok := r.Position(tt.v); !ok || got != tt.want {
+ t.Errorf("Position(%s) = %v, %v; want %v", Print(tt.v), got, ok, tt.want)
+ }
+ }
+
+ atom, pos, _ := r.ReadWithPos()
+ if pos != (Pos{4, 1}) {
+ t.Errorf("atom x at %v, want 4:1", pos)
+ }
+ if _, ok := r.Position(atom); ok {
+ t.Errorf("atoms should have no recorded position")
+ }
+
+ q, pos, _ := r.ReadWithPos()
+ if got, ok := r.Position(q); !ok || got != (Pos{5, 1}) || pos != (Pos{5, 1}) {
+ t.Errorf("'(e) at %v / %v, want 5:1", got, pos)
+ }
+ if got, ok := r.Position(q.(*Pair).Cdr.(*Pair).Car); !ok || got != (Pos{5, 2}) {
+ t.Errorf("(e) at %v, want 5:2", got)
+ }
+}
+
+func TestReaderKeepsComments(t *testing.T) {
+ r := NewReader("(+ 1 2) ;=> 3\n; note\nx ;=! oops")
+ for {
+ v, err := r.Read()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if v == nil {
+ break
+ }
+ }
+ var got []string
+ for _, c := range r.Comments() {
+ got = append(got, c.Pos.String()+" "+c.Text)
+ }
+ want := []string{"1:9 => 3", "2:1 note", "3:3 =! oops"}
+ if !reflect.DeepEqual(got, want) {
+ t.Errorf("Comments() = %q, want %q", got, want)
+ }
+}
+
+// Errors caused only by input ending early are marked Incomplete, so a REPL
+// can ask for more; real mistakes are not.
+func TestIncompleteInput(t *testing.T) {
+ tests := []struct {
+ src string
+ incomplete bool
+ }{
+ {"(", true},
+ {"(+ 1\n (* 2", true},
+ {`"abc`, true},
+ {`"abc\`, true},
+ {`"\u`, true},
+ {`"\u{4`, true},
+ {"'", true},
+ {"(1 .", true},
+ {"(1 . 2", true},
+ {")", false},
+ {"(')", false},
+ {"(1 . )", false},
+ {"(1 . 2 3)", false},
+ {`"\q"`, false},
+ {`"\u{zz}"`, false},
+ {"007", false},
+ }
+ for _, tt := range tests {
+ _, err := ReadAll(tt.src)
+ se, ok := err.(*SyntaxError)
+ if !ok {
+ t.Errorf("ReadAll(%q) error = %v, want SyntaxError", tt.src, err)
+ continue
+ }
+ if se.Incomplete != tt.incomplete {
+ t.Errorf("ReadAll(%q): Incomplete = %v, want %v (%v)", tt.src, se.Incomplete, tt.incomplete, se)
+ }
+ }
+}
diff --git a/sexpr/token.go b/sexpr/token.go
index 9fca94467ee6847ebf045fae435e43607582e04b..48e5f21260f5c2243243ad8cc21bd69071441d71 100644
--- a/sexpr/token.go
+++ b/sexpr/token.go
@@ -66,6 +66,9 @@
type SyntaxError struct {
Pos Pos
Msg string
+ // Incomplete means the input ended in the middle of a form, so more
+ // input could make it valid. A REPL uses this to ask for another line.
+ Incomplete bool
}
func (e *SyntaxError) Error() string { return e.Pos.String() + ": " + e.Msg }
@@ -174,7 +177,7 @@ l.advance()
var b strings.Builder
for {
if l.off >= len(l.src) {
- return Token{}, &SyntaxError{Pos: start, Msg: "unterminated string"}
+ return Token{}, &SyntaxError{Pos: start, Msg: "unterminated string", Incomplete: true}
}
escPos := l.pos
r := l.advance()
@@ -195,7 +198,7 @@ }
func (l *Lexer) lexEscape(escPos Pos) (rune, error) {
if l.off >= len(l.src) {
- return 0, &SyntaxError{Pos: escPos, Msg: "unterminated escape"}
+ return 0, &SyntaxError{Pos: escPos, Msg: "unterminated escape", Incomplete: true}
}
switch r := l.advance(); r {
case '"', '\\':
@@ -215,7 +218,7 @@ // lexUnicodeEscape reads the {hex} part of \u{hex}: 1 to 6 hex digits naming
// a Unicode scalar value (a code point that is not a surrogate).
func (l *Lexer) lexUnicodeEscape(escPos Pos) (rune, error) {
bad := func(msg string) (rune, error) {
- return 0, &SyntaxError{Pos: escPos, Msg: msg}
+ return 0, &SyntaxError{Pos: escPos, Msg: msg, Incomplete: l.off >= len(l.src)}
}
if l.off >= len(l.src) || l.peek() != '{' {
return bad(`\u must be followed by {hex}`)
diff --git a/testdata/arithmetic.lisp b/testdata/arithmetic.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..2ffb0965f53b6cfe5317a3bf6686ad1e7b14232e
--- /dev/null
+++ b/testdata/arithmetic.lisp
@@ -0,0 +1,18 @@
+; Integers stay integers; any float makes the result a float.
+(+) ;=> 0
+(*) ;=> 1
+(+ 1 2 3) ;=> 6
+(- 5) ;=> -5
+(- 10 3 2) ;=> 5
+(/ 7 2) ;=> 3
+(/ -7 2) ;=> -3
+(/ 7.0 2) ;=> 3.5
+(+ 1 2.0) ;=> 3.0
+(+ 0.1 0.2) ;=> 0.30000000000000004
+(* (+ 1 2) (- 10 4)) ;=> 18
+
+; Errors, never wraparound or infinity.
+(* 9223372036854775807 2) ;=! integer overflow
+(/ 1 0) ;=! division by zero
+(* 1.0e308 10) ;=! float overflow
+(+ 1 "2") ;=! must be a number
diff --git a/testdata/comparison.lisp b/testdata/comparison.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..5883e215d483fcc65db15fbf24187e980340a0fa
--- /dev/null
+++ b/testdata/comparison.lisp
@@ -0,0 +1,15 @@
+(< 1 2) ;=> T
+(>= 2 3) ;=> F
+(<= 1.0 1) ;=> T
+(== 1 1.0) ;=> T
+(== "a" "a") ;=> T
+(== 'a 'b) ;=> F
+(== 1 "1") ;=> F
+(== '(1 (2 3)) '(1 (2 3))) ;=> T
+(!= '(1 . 2) '(1 2)) ;=> T
+
+; Exact even where floats round: 9007199254740993 is 2^53 + 1.
+(== 9007199254740993 9007199254740992.0) ;=> F
+
+(< "a" "b") ;=! must be a number
+(== + +) ;=! cannot compare functions
diff --git a/testdata/functions.lisp b/testdata/functions.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..c0f5a74b0c3609dc640376b9264af7c87fa2f294
--- /dev/null
+++ b/testdata/functions.lisp
@@ -0,0 +1,33 @@
+; Functions are values: passed in, returned, stored, and named on first define.
+(define sq (lambda (n) (* n n)))
+(sq 7) ;=> 49
+sq ;=> #<lambda sq>
+(define square sq)
+square ;=> #<lambda sq>
++ ;=> #<builtin +>
+(lambda (x) x) ;=> #<lambda>
+
+(define compose (lambda (f g) (lambda (x) (f (g x)))))
+((compose sq (lambda (n) (+ n 1))) 3) ;=> 16
+
+(define make-adder (lambda (n) (lambda (x) (+ x n))))
+(define add3 (make-adder 3))
+(add3 4) ;=> 7
+((make-adder 10) 4) ;=> 14
+
+(define map (lambda (f l) (if (== l ()) () (cons (f (car l)) (map f (cdr l))))))
+(map sq '(1 2 3 4)) ;=> (1 4 9 16)
+(map (make-adder 1) '(1 2 3)) ;=> (2 3 4)
+
+(define filter (lambda (p l)
+ (if (== l ())
+ ()
+ (if (p (car l))
+ (cons (car l) (filter p (cdr l)))
+ (filter p (cdr l))))))
+(filter (lambda (n) (> n 2)) '(1 2 3 4)) ;=> (3 4)
+
+(sq) ;=! sq takes 1 argument, got 0
+(5 1) ;=! not a function
+if ;=! special form
+(lambda (+) 1) ;=! reserved name
diff --git a/testdata/lists.lisp b/testdata/lists.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..1e6da2f812554a737817601c2a08a4891a94752b
--- /dev/null
+++ b/testdata/lists.lisp
@@ -0,0 +1,12 @@
+(cons 1 2) ;=> (1 . 2)
+(cons 1 '(2 3)) ;=> (1 2 3)
+(cons 1 ()) ;=> (1)
+(car '(a b c)) ;=> a
+(cdr '(a b c)) ;=> (b c)
+(cdr '(a)) ;=> ()
+(car (cdr '(a b c))) ;=> b
+'(1 . (2 . (3 . ()))) ;=> (1 2 3)
+'(1 2 . 3) ;=> (1 2 . 3)
+
+(car '()) ;=! car needs a pair
+(cdr 5) ;=! cdr needs a pair
diff --git a/testdata/quote.lisp b/testdata/quote.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..1f9114377c0cff7ab9dca498643ad26cbc9d2cd1
--- /dev/null
+++ b/testdata/quote.lisp
@@ -0,0 +1,12 @@
+; quote returns its operand as data, unevaluated.
+'x ;=> x
+(quote x) ;=> x
+'(+ 1 2) ;=> (+ 1 2)
+''x ;=> (quote x)
+'() ;=> ()
+'T ;=> T
+'"s" ;=> "s"
+(car '(+ 1 2)) ;=> +
+(== (car '(+ 1 2)) '+) ;=> T
+
+(quote) ;=! quote takes 1 operand
diff --git a/testdata/recursion.lisp b/testdata/recursion.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..de70622fea839dc9a88491ebd6c588b3a5a19402
--- /dev/null
+++ b/testdata/recursion.lisp
@@ -0,0 +1,23 @@
+; A define can see itself, so functions can recurse.
+(define fact (lambda (n) (if (== n 0) 1 (* n (fact (- n 1))))))
+(fact 10) ;=> 3628800
+
+(define fib (lambda (n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))))
+(fib 20) ;=> 6765
+
+; Calls in tail position do not grow the stack.
+(define count (lambda (n) (if (== n 0) 'done (count (- n 1)))))
+(count 1000000) ;=> done
+
+(define sum (lambda (n acc) (if (== n 0) acc (sum (- n 1) (+ acc n)))))
+(sum 100000 0) ;=> 5000050000
+
+(define reverse (lambda (l acc) (if (== l ()) acc (reverse (cdr l) (cons (car l) acc)))))
+(reverse '(1 2 3) ()) ;=> (3 2 1)
+
+; Mutual recursion is impossible: odd? does not exist when even? is defined.
+(define even? (lambda (n) (if (== n 0) T (odd? (- n 1)))))
+(define odd? (lambda (n) (if (== n 0) F (even? (- n 1)))))
+(even? 4) ;=! no statute for symbol odd?
+
+(define y (+ y 1)) ;=! used before its definition is complete
diff --git a/testdata/scoping.lisp b/testdata/scoping.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..fbc06ff062651d1738dc5565de93172b9d167b51
--- /dev/null
+++ b/testdata/scoping.lisp
@@ -0,0 +1,9 @@
+; Scope is lexical: a function sees the bindings where it was written,
+; not where it is called.
+(define x 1)
+(define f (lambda () x))
+(define g (lambda (x) (f)))
+(g 2) ;=> 1
+(define x 2) ;=! immutable
+((lambda (x) (define y (+ x 1)) (define z (+ y 1)) z) 1) ;=> 3
+((lambda () (define a (lambda () (b))) (define b (lambda () 1)) (a))) ;=! unbound
diff --git a/testdata/strings.lisp b/testdata/strings.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..d2613a6b6e70a33e92c7125cbe86158ae386df95
--- /dev/null
+++ b/testdata/strings.lisp
@@ -0,0 +1,4 @@
+(strlen "héllo") ;=> 5
+(strcat "gov" "ern" "ment") ;=> "government"
+(str? "") ;=> T
+(str? 'foo) ;=> F
diff --git a/testdata/truthiness.lisp b/testdata/truthiness.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..2ce335df391a0a006fed84f01a8572bee8adf155
--- /dev/null
+++ b/testdata/truthiness.lisp
@@ -0,0 +1,6 @@
+; Only F is false.
+(and F (car '())) ;=> F
+(or F 7) ;=> 7
+(if 0 'true 'false) ;=> true
+(if F 1) ;=> ()
+(not 0) ;=> F