From 82a614f3a1cb9f1db197ef4e84364c827aa21962 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 10 Oct 2026 08:37:00 +0000 Subject: [PATCH] Step 10: generic modules; Str, Buf, Fmt, List, Map A generic module is compiled once per distinct list of arguments, from its source, with each type parameter naming its argument and each procedure parameter its procedure, whose signature must match. Two imports with the same arguments share the instance; its symbols are named after it, List(I64).push. A string constant can be indexed and sliced. lib: Str (cmp, eq, hash, find, hasPrefix), Buf (a growable buffer), Fmt (print and write with {} and a slice of Arg), List(T) and Map(K, V; hash, eq). examples/Words.jed counts words with them. make bench takes the best of three runs, since one is noisy. --- diff --git a/Makefile b/Makefile index ce34c34..89d168d 100644 --- a/Makefile +++ b/Makefile @@ -27,14 +27,17 @@ check: jed test/check.sh -# the compiler's speed, on a generated program of about 350,000 lines; -# fails below 400,000 lines a second +# the compiler's speed, on a generated program of about 350,000 lines, +# the best of three runs; fails below 400,000 lines a second bench: jed | build python3 test/genbench.py 12000 > build/Bench.jed - @lines=$$(wc -l < build/Bench.jed); \ - start=$$(date +%s%N); ./jed build/Bench.jed || exit 1; end=$$(date +%s%N); \ - rate=$$((lines * 1000000000 / (end - start))); \ - echo "$$lines lines in $$(((end - start) / 1000000)) ms: $$rate lines a second"; \ + @lines=$$(wc -l < build/Bench.jed); best=0; \ + for i in 1 2 3; do \ + start=$$(date +%s%N); ./jed build/Bench.jed || exit 1; end=$$(date +%s%N); \ + t=$$((end - start)); if [ $$best = 0 ] || [ $$t -lt $$best ]; then best=$$t; fi; \ + done; \ + rate=$$((lines * 1000000000 / best)); \ + echo "$$lines lines in $$((best / 1000000)) ms: $$rate lines a second"; \ test $$rate -ge 400000 # lines of the compiler: all of them, and those that aren't blank or comments diff --git a/PLAN.md b/PLAN.md index a002abe..b234779 100644 --- a/PLAN.md +++ b/PLAN.md @@ -95,7 +95,7 @@ - [x] 7. Multiple results and errors; `INC` and `DEC`. - [x] 8. Enumerations and sets, record literals, `defer`; examples. - [x] 9. `NEW`; `lib/Mem.jed`, `lib/Arena.jed`, `lib/Heap.jed`. -- [ ] 10. Generic modules, with type and procedure arguments; +- [x] 10. Generic modules, with type and procedure arguments; `lib/Str.jed`, `lib/Buf.jed`, `lib/Fmt.jed`; examples. Stage 3: calling C diff --git a/doc/jed.md b/doc/jed.md index 1c4c523..c3d5b36 100644 --- a/doc/jed.md +++ b/doc/jed.md @@ -759,8 +759,9 @@ | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator | | `Str` | operations on `[]U8` strings | | `Buf` | a growable byte buffer | -| `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)])` | +| `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)])`, or into a `Buf` with `Fmt.write` | | `List` | a generic singly linked list, `List(T)` | +| `Map` | a generic hash map, `Map(K, V; hash, eq)` | ``` module Mem; diff --git a/examples/Words b/examples/Words new file mode 100755 index 0000000..61c7f70 Binary files /dev/null and b/examples/Words differ diff --git a/examples/Words.expect b/examples/Words.expect new file mode 100644 index 0000000..6b3fcd1 --- /dev/null +++ b/examples/Words.expect @@ -0,0 +1,6 @@ +the 3 +cat 1 +and 2 +hat 1 +bat 1 +8 words, 5 different diff --git a/examples/Words.jed b/examples/Words.jed new file mode 100644 index 0000000..ee1eba9 --- /dev/null +++ b/examples/Words.jed @@ -0,0 +1,37 @@ +(* Generic modules: counts the words of a text in a Map from []U8 to + I64, hashed and compared by Str's procedures, keeps them in order of + first sight in a List([]U8), and prints them with Fmt. *) +module Words; +import Fmt, Str, Heap, Count := Map([]U8, I64, Str.hash, Str.eq), Names := List([]U8); + +const Text = "the cat and the hat and the bat"; + +proc main*; +var + counts: Count.Map; order: Names.List; n: ^Names.Node; + i, start, k, total: I64; found, ok: BOOL; w: []U8; +begin + Count.init(ADR(counts), Heap.heap); + Names.init(ADR(order), Heap.heap); + start := 0; + for i := 0 to LEN(Text) do + if (i = LEN(Text)) or (Text[i] = ' ') then + w := Text[start..i]; + INC(total); + k, found := Count.get(ADR(counts), w); + if not found then ok := Names.push(ADR(order), w); end; + ok := Count.put(ADR(counts), w, k + 1); + start := i + 1; + end; + end; + Names.reverse(ADR(order)); + n := order.head; + while n # nil do + k, found := Count.get(ADR(counts), n^.val); + Fmt.print("{} {}\n", [Fmt.str(n^.val), Fmt.int(k)]); + n := n^.next; + end; + Fmt.print("{} words, {} different\n", [Fmt.int(total), Fmt.int(counts.len)]); +end; + +end Words. diff --git a/lib/Buf.jed b/lib/Buf.jed new file mode 100644 index 0000000..660f40f --- /dev/null +++ b/lib/Buf.jed @@ -0,0 +1,71 @@ +(* A growable byte buffer, its memory from an allocator. *) +module Buf; +import Mem; + +type + Buf* = record + a: ^Mem.Allocator; + data: ^U8; + len*, cap: I64; + end; + +proc init*(b: ^Buf; a: ^Mem.Allocator); +begin + b^ := {a: a}; +end; + +(* Room for n more bytes; false if the allocator has none. *) +proc room*(b: ^Buf; n: I64): BOOL; +var cap, i: I64; p: ^U8; +begin + if b^.len + n <= b^.cap then return true; end; + cap := b^.cap * 2; + if cap < 64 then cap := 64; end; + while cap < b^.len + n do cap := cap * 2; end; + p := Mem.alloc(b^.a, cap, 1); + if p = nil then return false; end; + for i := 0 to b^.len - 1 do p[i] := b^.data[i]; end; + if b^.data # nil then Mem.free(b^.a, b^.data); end; + b^.data := p; + b^.cap := cap; + return true; +end; + +proc add*(b: ^Buf; c: U8); +begin + if room(b, 1) then + b^.data[b^.len] := c; + INC(b^.len); + end; +end; + +proc str*(b: ^Buf; s: []U8); +var i: I64; +begin + if room(b, LEN(s)) then + for i := 0 to LEN(s) - 1 do b^.data[b^.len + i] := s[i]; end; + INC(b^.len, LEN(s)); + end; +end; + +(* What the buffer holds, until it next grows. *) +proc bytes*(b: ^Buf): []U8; +begin + if b^.len = 0 then return ""; end; + return b^.data[0..b^.len]; +end; + +proc reset*(b: ^Buf); +begin + b^.len := 0; +end; + +proc done*(b: ^Buf); +begin + if b^.data # nil then Mem.free(b^.a, b^.data); end; + b^.data := nil; + b^.len := 0; + b^.cap := 0; +end; + +end Buf. diff --git a/lib/Fmt.jed b/lib/Fmt.jed new file mode 100644 index 0000000..4c29a1a --- /dev/null +++ b/lib/Fmt.jed @@ -0,0 +1,90 @@ +(* Formatted output. Each {} in the format takes the next argument: + Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)]); + {{ and }} stand for { and }. print writes to standard output, + write appends to a Buf. *) +module Fmt; +import Out, Buf; + +type + Arg* = variant + int(i: I64); + uint(u: U64); + hex(h: U64); + str(s: []U8); + char(c: U8); + bool(b: BOOL); + end; + +(* The digits of u in base, at the end of the array: it and where they start. *) +proc digits(u: U64; base: U64): ([24]U8, I64); +var d: [24]U8; i: I64; v: U8; +begin + i := 24; + repeat + DEC(i); + v := U8(u mod base); + if v < 10 then d[i] := v + '0'; else d[i] := v - 10 + 'A'; end; + u := u div base; + until u = 0; + return d, i; +end; + +(* Where the pieces go: to b, or to standard output if b is nil. *) +proc put(b: ^Buf.Buf; s: []U8); +begin + if b = nil then Out.str(s); else Buf.str(b, s); end; +end; + +proc arg(b: ^Buf.Buf; a: Arg); +var d: [24]U8; at: I64; one: [1]U8; +begin + match a of + | int(i): + if i < 0 then + put(b, "-"); + d, at := digits(0 - U64(i), 10); + else + d, at := digits(U64(i), 10); + end; + put(b, d[at..24]); + | uint(u): d, at := digits(u, 10); put(b, d[at..24]); + | hex(h): d, at := digits(h, 16); put(b, d[at..24]); + | str(s): put(b, s); + | char(c): one[0] := c; put(b, one); + | bool(t): if t then put(b, "true"); else put(b, "false"); end; + end; +end; + +proc format(b: ^Buf.Buf; f: []U8; args: []Arg); +var i, start, n: I64; +begin + i := 0; start := 0; n := 0; + while i < LEN(f) do + if (f[i] = '{') or (f[i] = '}') then + put(b, f[start..i]); + if (i + 1 < LEN(f)) and (f[i + 1] = f[i]) then + put(b, f[i..i + 1]); + INC(i); + elsif (f[i] = '{') and (i + 1 < LEN(f)) and (f[i + 1] = '}') then + if n < LEN(args) then arg(b, args[n]); end; + INC(n); + INC(i); + end; + start := i + 1; + end; + INC(i); + end; + put(b, f[start..LEN(f)]); +end; + +proc print*(f: []U8; args: []Arg); +begin + format(nil, f, args); +end; + +proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); +begin + format(b, f, args); +end; + +end Fmt. diff --git a/lib/List.jed b/lib/List.jed new file mode 100644 index 0000000..13eeef2 --- /dev/null +++ b/lib/List.jed @@ -0,0 +1,68 @@ +(* A singly linked list of T, its nodes from an allocator. *) +module List(T); +import Mem; + +type + Node* = record + next*: ^Node; + val*: T; + end; + List* = record + head*: ^Node; + len*: I64; + a: ^Mem.Allocator; + end; + +proc init*(l: ^List; a: ^Mem.Allocator); +begin + l^ := {a: a}; +end; + +(* v at the front; false if the allocator has no room. *) +proc push*(l: ^List; v: T): BOOL; +var n: ^Node; +begin + NEW(n, l^.a); + if n = nil then return false; end; + n^.val := v; + n^.next := l^.head; + l^.head := n; + INC(l^.len); + return true; +end; + +(* The front value, taken off; false if the list is empty. *) +proc pop*(l: ^List): (T, BOOL); +var n: ^Node; v: T; +begin + n := l^.head; + if n = nil then return v, false; end; + v := n^.val; + l^.head := n^.next; + DEC(l^.len); + Mem.free(l^.a, ^U8(n)); + return v, true; +end; + +(* The list backwards. *) +proc reverse*(l: ^List); +var p, q, next: ^Node; +begin + p := l^.head; + q := nil; + while p # nil do + next := p^.next; + p^.next := q; + q := p; + p := next; + end; + l^.head := q; +end; + +proc clear*(l: ^List); +var ok: BOOL; v: T; +begin + while l^.head # nil do v, ok := pop(l); end; +end; + +end List. diff --git a/lib/Map.jed b/lib/Map.jed new file mode 100644 index 0000000..746d7af --- /dev/null +++ b/lib/Map.jed @@ -0,0 +1,81 @@ +(* A hash map from K to V, by open addressing; it hashes and compares + keys with the procedures it is given: + import Count := Map([]U8, I64, Str.hash, Str.eq); *) +module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); +import Mem; + +type + Entry = record + key: K; + val: V; + used: BOOL; + end; + Map* = record + a: ^Mem.Allocator; + slots: ^Entry; + cap: I64; (* a power of two, or 0 *) + len*: I64; + end; + +proc init*(m: ^Map; a: ^Mem.Allocator); +begin + m^ := {a: a}; +end; + +(* The slot of k, or of the empty slot where it would go. *) +proc slot(m: ^Map; k: K): I64; +var i: I64; +begin + i := I64(hash(k) and U64(m^.cap - 1)); + while m^.slots[i].used and not eq(m^.slots[i].key, k) do + i := (i + 1) and (m^.cap - 1); + end; + return i; +end; + +proc grow(m: ^Map): BOOL; +var old: ^Entry; n, i, j: I64; +begin + old := m^.slots; + n := m^.cap; + if n = 0 then m^.cap := 16; else m^.cap := n * 2; end; + m^.slots := ^Entry(Mem.alloc(m^.a, m^.cap * SIZE(Entry), 8)); + if m^.slots = nil then m^.slots := old; m^.cap := n; return false; end; + for i := 0 to m^.cap - 1 do m^.slots[i].used := false; end; + for i := 0 to n - 1 do + if old[i].used then + j := slot(m, old[i].key); + m^.slots[j] := old[i]; + end; + end; + if old # nil then Mem.free(m^.a, ^U8(old)); end; + return true; +end; + +(* k now maps to v; false if there was no room. *) +proc put*(m: ^Map; k: K; v: V): BOOL; +var i: I64; +begin + if (m^.len + 1) * 4 > m^.cap * 3 then + if not grow(m) then return false; end; + end; + i := slot(m, k); + if not m^.slots[i].used then + m^.slots[i] := {key: k, used: true}; + INC(m^.len); + end; + m^.slots[i].val := v; + return true; +end; + +(* What k maps to, and whether it maps to anything. *) +proc get*(m: ^Map; k: K): (V, BOOL); +var i: I64; none: V; +begin + if m^.cap = 0 then return none, false; end; + i := slot(m, k); + if not m^.slots[i].used then return none, false; end; + return m^.slots[i].val, true; +end; + +end Map. diff --git a/lib/Str.jed b/lib/Str.jed new file mode 100644 index 0000000..22bb5b3 --- /dev/null +++ b/lib/Str.jed @@ -0,0 +1,53 @@ +(* Operations on strings, which are []U8. = and # compare them; this + module orders, searches and hashes them. *) +module Str; + +(* -1, 0 or 1 as a sorts before, with or after b, byte by byte. *) +proc cmp*(a, b: []U8): I64; +var i, n: I64; +begin + n := LEN(a); + if LEN(b) < n then n := LEN(b); end; + for i := 0 to n - 1 do + if a[i] # b[i] then + if a[i] < b[i] then return -1; end; + return 1; + end; + end; + if LEN(a) < LEN(b) then return -1; end; + if LEN(a) > LEN(b) then return 1; end; + return 0; +end; + +proc eq*(a, b: []U8): BOOL; +begin + return a = b; +end; + +(* FNV-1a, 64 bits. *) +proc hash*(s: []U8): U64; +var h: U64; i: I64; +begin + h := 0CBF29CE484222325H; + for i := 0 to LEN(s) - 1 do + h := (h xor U64(s[i])) * 100000001B3H; + end; + return h; +end; + +(* Where sub first starts in s, or -1. *) +proc find*(s, sub: []U8): I64; +var i: I64; +begin + for i := 0 to LEN(s) - LEN(sub) do + if s[i..i + LEN(sub)] = sub then return i; end; + end; + return -1; +end; + +proc hasPrefix*(s, p: []U8): BOOL; +begin + return (LEN(s) >= LEN(p)) and (s[0..LEN(p)] = p); +end; + +end Str. diff --git a/src/gen.pas b/src/gen.pas index 0ff16ca..09bc2b5 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -496,6 +496,7 @@ procedure IndexPrep(var x: TItem); begin + if x.mode = mStr then Coerce(x, tBytes); if (x.typ^.form = fPtr) and (x.typ^.base^.form = fArray) then Mark('a pointer to an array is indexed as p^[i]'); if x.typ^.form = fPtr then begin diff --git a/src/parse.pas b/src/parse.pas index 04208df..87b41e5 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -35,7 +35,7 @@ firstModule, lastModule: PModule; { in the order they were finished } function CompileMain(const path: shortstring): PModule; -function ImportModule(name: longint): PModule; +function ImportModule(name: longint; const args: TArgs): PModule; implementation @@ -1322,7 +1322,7 @@ p^.exported := exp; if (curModule^.name = nameRt) and not checkMode and (n = nameFail) then p^.chunk := rtFail else if (curModule^.name = nameRt) and not checkMode and (n = nameEq) then p^.chunk := rtEq - else p^.chunk := NewChunk(secText, curModule^.name, n); + else p^.chunk := NewChunk(secText, curModule^.sname, n); Declare(p); end; if tok = sForward then begin @@ -1367,7 +1367,7 @@ o^.exported := exp and top; if t = tError then begin o^.typ := tError; - o^.val := ErrorChunk(x, curModule^.name, n) + 1; + o^.val := ErrorChunk(x, curModule^.sname, n) + 1; end else if x.mode = mStr then begin o^.val := x.val; o^.chunk := x.chunk end else o^.val := ConstValue(x); Declare(o); @@ -1419,7 +1419,7 @@ o^.isVar := false; if top then begin o^.lev := 0; - o^.chunk := NewChunk(secBss, curModule^.name, names[i]); + o^.chunk := NewChunk(secBss, curModule^.sname, names[i]); SetBss(o^.chunk, t^.size, t^.align); end else begin o^.lev := 1; @@ -1443,14 +1443,87 @@ { Modules } -function FindModule(n: longint): PModule; +{ Whether m is module n with these arguments. } +function IsModule(m: PModule; n: longint; const args: TArgs): boolean; +var i: longint; +begin + IsModule := false; + if (m^.name <> n) or (m^.args.n <> args.n) then exit; + for i := 0 to args.n - 1 do + if (m^.args.o[i] <> args.o[i]) or not Same(m^.args.t[i], args.t[i]) then exit; + IsModule := true; +end; + +function FindModule(n: longint; const args: TArgs): PModule; var m: PModule; begin m := firstModule; - while (m <> nil) and (m^.name <> n) do m := m^.next; + while (m <> nil) and not IsModule(m, n, args) do m := m^.next; FindModule := m; end; +{ The arguments of an instance: types, or procedures named. } +procedure ParseArgs(var args: TArgs); +var o: PObj; +begin + Next; + repeat + if args.n > High(args.t) then Mark('too many arguments'); + args.o[args.n] := nil; + if tok = sIdent then begin + o := QualIdent; + if o^.cls = oProc then args.o[args.n] := o + else if o^.cls <> oType then Mark('a type or a procedure expected'); + args.t[args.n] := o^.typ; + end else args.t[args.n] := ParseType; + inc(args.n); + if tok <> sComma then break; + Next; + until false; + Expect(sRparen, ')'); +end; + +{ A generic module's parameters, bound to the instance's arguments: a + type parameter names its type, a procedure parameter its procedure. } +procedure Params(m: PModule); +var i: longint; o, a: PObj; sig: PType; s: shortstring; +begin + i := 0; + s := NameOf(m^.name) + '('; + Next; + repeat + if (tok <> sIdent) or (i >= m^.args.n) or (m^.args.o[i] <> nil) then + Mark('needs a type for each type parameter'); + Declare(NewObj(oType, id, m^.args.t[i])); + s := s + TypeName(m^.args.t[i]) + ','; + inc(i); + Next; + if tok <> sComma then break; + Next; + until false; + while tok = sSemicolon do begin + Next; + if tok <> sIdent then Mark('name expected'); + o := NewObj(oProc, id, nil); + Next; + Expect(sColon, ':'); + Expect(sProc, 'proc'); + sig := Signature; + if i >= m^.args.n then Mark('needs a procedure for ' + NameOf(o^.name)); + a := m^.args.o[i]; + if (a = nil) or not Same(a^.typ, sig) then + Mark('needs a procedure with the signature of ' + NameOf(o^.name)); + o^.typ := a^.typ; o^.chunk := a^.chunk; o^.isCode := a^.isCode; + Declare(o); + s := s + NameOf(a^.module^.name) + '.' + NameOf(a^.name) + ','; + inc(i); + end; + Expect(sRparen, ')'); + if i <> m^.args.n then Mark('too many arguments'); + s[length(s)] := ')'; + m^.sname := Name(s); +end; + { Modules being compiled, not yet finished: for finding a cycle. } var pending: array[0..63] of PModule; @@ -1463,11 +1536,11 @@ Exists := (FpStat(@p[1], st) = 0) and ((st.st_mode and S_IFMT) = S_IFREG); end; -function CompileFile(const path: shortstring; n: longint; checkOnly: boolean): PModule; +function CompileFile(const path: shortstring; n: longint; const args: TArgs; checkOnly: boolean): PModule; var st: TScanState; saved: array[0..MaxDepth - 1] of longint; d: longint; oldMod: PModule; oldFile: longint; oldCheck: boolean; m, im: PModule; - imp: PImport; alias, mname: longint; o: PObj; body: longint; + imp: PImport; alias, mname: longint; o: PObj; body: longint; iargs: TArgs; begin SaveScan(st); SaveScopes(d, saved); @@ -1476,6 +1549,8 @@ New(m); FillChar(m^, SizeOf(TModule), 0); m^.name := n; + m^.sname := n; + m^.args := args; m^.scope := NewScope; m^.path := path; m^.body := -1; @@ -1493,7 +1568,8 @@ Expect(sModule, 'module'); if (tok <> sIdent) or (id <> n) then Mark('this file must hold module ' + NameOf(n)); Next; - if tok = sLparen then Mark('generic modules come in stage 2'); + if tok = sLparen then Params(m) + else if args.n > 0 then Mark(NameOf(n) + ' isn''t generic'); Expect(sSemicolon, ';'); while tok = sImport do begin Next; @@ -1501,15 +1577,16 @@ if tok <> sIdent then Mark('module name expected'); alias := id; mname := id; + iargs.n := 0; Next; if tok = sBecomes then begin Next; if tok <> sIdent then Mark('module name expected'); mname := id; Next; - if tok = sLparen then Mark('generic modules come in stage 2'); + if tok = sLparen then ParseArgs(iargs); end; - im := ImportModule(mname); + im := ImportModule(mname, iargs); o := NewObj(oModule, alias, nil); o^.module := im; Declare(o); @@ -1528,7 +1605,7 @@ if checkMode then SkipBody else begin Next; - body := NewChunk(secText, n, nameBody); + body := NewChunk(secText, m^.sname, nameBody); m^.body := body; BeginProc(tBodySig, nil); deferSlot := 0; @@ -1584,7 +1661,7 @@ { Reads every other file of module n in dir, and checks its interface against m, compiled from the file named chosen. } -procedure CheckSiblings(const dir, chosen: shortstring; n: longint; m: PModule); +procedure CheckSiblings(const dir, chosen: shortstring; n: longint; m: PModule; const args: TArgs); var dp: pDir; e: pDirent; f, prefix: shortstring; i: longint; others: array[0..31] of shortstring; numOthers: longint; @@ -1611,17 +1688,17 @@ until false; FpCloseDir(dp^); for i := 0 to numOthers - 1 do - CompareIface(m, CompileFile(dir + '/' + others[i], n, true)); + CompareIface(m, CompileFile(dir + '/' + others[i], n, args, true)); end; -function ImportModule(name: longint): PModule; +function ImportModule(name: longint; const args: TArgs): PModule; const Suffixes: array[0..3] of string[16] = ('.' + Target, '.' + TargetOS, '.' + TargetArch, ''); var m: PModule; i, j: longint; dir, f: shortstring; begin - m := FindModule(name); + m := FindModule(name, args); if m <> nil then exit(m); for i := 0 to numPending - 1 do - if pending[i]^.name = name then Mark(NameOf(name) + ' imports itself'); + if IsModule(pending[i], name, args) then Mark(NameOf(name) + ' imports itself'); for i := -1 to numInc do begin if i = -1 then dir := mainDir else if i = numInc then dir := libDir @@ -1629,8 +1706,8 @@ for j := 0 to 3 do begin f := NameOf(name) + Suffixes[j] + '.jed'; if Exists(dir + '/' + f) then begin - m := CompileFile(dir + '/' + f, name, false); - CheckSiblings(dir, f, name, m); + m := CompileFile(dir + '/' + f, name, args, false); + CheckSiblings(dir, f, name, m, args); exit(m); end; end; @@ -1640,8 +1717,9 @@ end; function CompileMain(const path: shortstring): PModule; -var base: shortstring; i, n: longint; +var base: shortstring; i, n: longint; none: TArgs; begin + none.n := 0; i := length(path); while (i > 0) and (path[i] <> '/') do dec(i); if i = 0 then mainDir := '.' else mainDir := copy(path, 1, i - 1); @@ -1650,9 +1728,9 @@ if i > 0 then base := copy(base, 1, i - 1); if base = '' then Fatal('no module in ' + path); n := Name(base); - if n <> nameRt then ImportModule(nameRt); - if FindModule(n) <> nil then CompileMain := FindModule(n) - else CompileMain := CompileFile(path, n, false); + if n <> nameRt then ImportModule(nameRt, none); + if FindModule(n, none) <> nil then CompileMain := FindModule(n, none) + else CompileMain := CompileFile(path, n, none, false); end; initialization diff --git a/src/sym.pas b/src/sym.pas index 41f8c8c..41f35ea 100644 --- a/src/sym.pas +++ b/src/sym.pas @@ -70,6 +70,14 @@ link: PObj; { the next top-level declaration of its module } end; + { The arguments of a generic module's instance: a type, or a + procedure (o) and its type. } + TArgs = record + n: longint; + t: array[0..7] of PType; + o: array[0..7] of PObj; + end; + TImport = record module: PModule; next: PImport; @@ -77,6 +85,8 @@ TModule = record name: longint; + sname: longint; { the name of its symbols: List(I64) for an instance } + args: TArgs; scope: longint; path: shortstring; first, last: PObj; { its top-level declarations, in order } @@ -345,7 +355,7 @@ if t = nil then exit('nothing'); if t^.obj <> nil then begin if (t^.obj^.module <> nil) and (t^.obj^.module <> curModule) then - exit(NameOf(t^.obj^.module^.name) + '.' + NameOf(t^.obj^.name)); + exit(NameOf(t^.obj^.module^.sname) + '.' + NameOf(t^.obj^.name)); exit(NameOf(t^.obj^.name)); end; case t^.form of @@ -378,7 +388,7 @@ if t = nil then begin Mix(h, -1); exit end; if (t^.obj <> nil) and not top then begin Mix(h, t^.obj^.name); - if t^.obj^.module <> nil then Mix(h, t^.obj^.module^.name); + if t^.obj^.module <> nil then Mix(h, t^.obj^.module^.sname); exit; end; Mix(h, ord(t^.form)); diff --git a/test/err/BadProc.err b/test/err/BadProc.err new file mode 100644 index 0000000..4e86a72 --- /dev/null +++ b/test/err/BadProc.err @@ -0,0 +1 @@ +needs a procedure with the signature of hash diff --git a/test/err/BadProc.jed b/test/err/BadProc.jed new file mode 100644 index 0000000..97268d2 --- /dev/null +++ b/test/err/BadProc.jed @@ -0,0 +1,3 @@ +module BadProc; +import Str, M := Map([]U8, I64, Str.cmp, Str.eq); +end BadProc. diff --git a/test/err/NoArgs.err b/test/err/NoArgs.err new file mode 100644 index 0000000..10303bf --- /dev/null +++ b/test/err/NoArgs.err @@ -0,0 +1 @@ +needs a type for each type parameter diff --git a/test/err/NoArgs.jed b/test/err/NoArgs.jed new file mode 100644 index 0000000..f472904 --- /dev/null +++ b/test/err/NoArgs.jed @@ -0,0 +1,3 @@ +module NoArgs; +import List; +end NoArgs. diff --git a/test/err/NotGeneric.err b/test/err/NotGeneric.err new file mode 100644 index 0000000..0945d7b --- /dev/null +++ b/test/err/NotGeneric.err @@ -0,0 +1 @@ +Str isn't generic diff --git a/test/err/NotGeneric.jed b/test/err/NotGeneric.jed new file mode 100644 index 0000000..547e417 --- /dev/null +++ b/test/err/NotGeneric.jed @@ -0,0 +1,3 @@ +module NotGeneric; +import S := Str(I64); +end NotGeneric. diff --git a/test/err/TwoTypes.err b/test/err/TwoTypes.err new file mode 100644 index 0000000..ebb66ba --- /dev/null +++ b/test/err/TwoTypes.err @@ -0,0 +1 @@ +can't use List(I32).List as List(I64).List diff --git a/test/err/TwoTypes.jed b/test/err/TwoTypes.jed new file mode 100644 index 0000000..3269f35 --- /dev/null +++ b/test/err/TwoTypes.jed @@ -0,0 +1,8 @@ +module TwoTypes; +import A := List(I64), B := List(I32); +proc main*; +var a: A.List; b: B.List; +begin + a := b; +end; +end TwoTypes. diff --git a/test/run/Generic.expect b/test/run/Generic.expect new file mode 100644 index 0000000..5b3656e --- /dev/null +++ b/test/run/Generic.expect @@ -0,0 +1,6 @@ +8 2 true +8 1 +abtrue +[] false +157 152 -1 +{ BEEF } x18446744073709551615 diff --git a/test/run/Generic.jed b/test/run/Generic.jed new file mode 100644 index 0000000..e8a1bf0 --- /dev/null +++ b/test/run/Generic.jed @@ -0,0 +1,30 @@ +module Generic; +import Fmt, Buf, Str, Heap, Stack, Ints := List(I64), More := List(I64), Strs := List([]U8); + +proc twice(x: I64): I64; begin return x * 2; end; + +proc main*; +var l: Strs.List; v: I64; s: []U8; ok: BOOL; b: Buf.Buf; n: ^Ints.Node; +begin + ok := Ints.push(ADR(Stack.s), 7); + ok := More.push(ADR(Stack.s), 8); + Fmt.print("{} {} {}\n", [Fmt.int(Stack.top()), Fmt.int(Stack.s.len), Fmt.bool(ok)]); + n := Stack.s.head; + v, ok := Ints.pop(ADR(Stack.s)); + Fmt.print("{} {}\n", [Fmt.int(v), Fmt.int(Stack.s.len)]); + Strs.init(ADR(l), Heap.heap); + ok := Strs.push(ADR(l), "b"); + ok := Strs.push(ADR(l), "a"); + s, ok := Strs.pop(ADR(l)); + Fmt.print("{}{}{}\n", [Fmt.str(s), Fmt.str(l.head^.val), Fmt.bool(ok)]); + s, ok := Strs.pop(ADR(l)); + s, ok := Strs.pop(ADR(l)); + Fmt.print("[{}] {}\n", [Fmt.str(s), Fmt.bool(ok)]); + Buf.init(ADR(b), Heap.heap); + for v := 1 to 40 do Fmt.write(ADR(b), "{},", [Fmt.int(v * v)]); end; + Fmt.print("{} {} {}\n", [Fmt.int(b.len), Fmt.int(Str.find(Buf.bytes(ADR(b)), "1600")), Fmt.int(Str.cmp("ab", "b"))]); + Buf.done(ADR(b)); + Fmt.print("{{ {} }} {}{}\n", [Fmt.hex(48879), Fmt.char('x'), Fmt.uint(0FFFFFFFFFFFFFFFFH)]); +end; + +end Generic. diff --git a/test/run/mods/Stack.jed b/test/run/mods/Stack.jed new file mode 100644 index 0000000..6048ee4 --- /dev/null +++ b/test/run/mods/Stack.jed @@ -0,0 +1,13 @@ +module Stack; +import Ints := List(I64), Heap; + +var s*: Ints.List; + +proc top*(): I64; +begin + return s.head^.val; +end; + +begin + Ints.init(ADR(s), Heap.heap); +end Stack.