Widen pseudo for numerical work: sized types, FLOAT32, exact built-ins, USE IEEE
Pseudo could say what a procedure computes only up to INTEGER and FLOAT, so it could not state what a Mojo or Python program with Int32, UInt8 or Float32 values will compute, and it had no words for rounding itself. - lang/parse: INT8..INT64, UINT8..UINT64, FLOAT32, FLOAT64 as types (INTEGER or FLOAT with a width, so every "whole number" rule accepts them); conversions written as Mojo writes them, INT32(X), FLOAT32(X); 1_000_000, 0xFF and 0b1010 literals; a type name as an argument (MAX_VALUE(INT8)); the USE line (USE IEEE now, library modules next). - check: widths and precisions never mix silently (mixed-width, mixed-precision); a literal takes the type of the sized value beside it, as in Mojo, and one that does not fit is an error before any run (literal-range); typing for every new built-in. - interp: a sized result, store, parameter, RETURN or conversion that does not fit stops the run (kind overflow), WRAP(X, T) wraps on purpose; FLOAT32 results are rounded to single precision; USE IEEE lets NaN and infinities flow. - numeric.js: exact helpers: FMA rounded once, EXACT(X) with every digit a FLOAT holds, ULP and NEXT_AFTER for both precisions, bit operations in two's complement. - Built-ins: ASIN ACOS ATAN SINH COSH TANH HYPOT POW CBRT EXPM1 LOG1P E TRUNC SIGN COPYSIGN CLAMP FMA IS_NAN IS_INF IS_FINITE INF NAN ULP NEXT_AFTER IS_CLOSE EXACT MAX_VALUE MIN_VALUE EPSILON GCD LCM BIT_AND BIT_OR BIT_XOR BIT_NOT SHIFT_LEFT SHIFT_RIGHT WRAP. They stay usable as names (a variable E, a FUNCTION GCD): the program's own wins. - vcgen: an overflow obligation on every sized result, then the range as a fact (also for sized parameters and loop-cut variables); FLOAT32 arithmetic as its own uninterpreted functions, constants rounded exactly, TOFLOAT32(v) otherwise; argument obligations for SQRT, the logarithms, ASIN/ACOS, LOG1P, CLAMP and the shifts; nothing proved under USE IEEE. - testing: the fuzzer tries the edges of a sized type and generates FLOAT32 values. - pseudo-to-mojo: Int8..Float32, conversions, the new built-ins through std.math (each name in the harvested index), a note where Mojo wraps. - Tests: numeric.test.mjs; corpus 49-52; prover fixtures int8-overflow, float32-store, ieee-nan; the reference documents every new form. Gate: 324 pass (core). New Mojo uses std.math names the interpreter has not been verified on (listed in sync-mojo's KNOWN_GAPS); they need pixi run mojo-verify on the network before the interpreter runs them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
This commit is contained in:
24 files changed
+1271
-96
No files matched your search
@@ -20,6 +20,25 @@ The prover is three TypeScript modules under strict mode; the rest of the engine
|
|||||||
|
|
||||||
"Proved" is about the notation's own number model. INTEGER is a whole number of any size and a FLOAT is a finite number whose rounding the prover does not see into. A run of the interpreter stops where a value leaves that model (a whole number past 2^53 − 1, a FLOAT that is not finite, a division by zero), so every run stays inside what a proof covers. The Mojo made from a program has other limits (a 64-bit `Int` that wraps, a `Float64` that becomes `inf` or `nan`), and a unit whose proof leans on the number model carries an `int-model` or `float-model` note saying so.
|
"Proved" is about the notation's own number model. INTEGER is a whole number of any size and a FLOAT is a finite number whose rounding the prover does not see into. A run of the interpreter stops where a value leaves that model (a whole number past 2^53 − 1, a FLOAT that is not finite, a division by zero), so every run stays inside what a proof covers. The Mojo made from a program has other limits (a 64-bit `Int` that wraps, a `Float64` that becomes `inf` or `nan`), and a unit whose proof leans on the number model carries an `int-model` or `float-model` note saying so.
|
||||||
|
|
||||||
|
### Numbers
|
||||||
|
|
||||||
|
Pseudo is meant for numerical work, so its number model is stated exactly ([the reference](../src/common/reference.ts) shows every form with a runnable example):
|
||||||
|
|
||||||
|
| Type | Holds | When a result does not fit | The prover |
|
||||||
|
|---|---|---|---|
|
||||||
|
| `INTEGER` | A whole number of any size; a run holds it exactly to 2^53 − 1 | The run stops past 2^53 − 1 | Exact arithmetic of any size; an `int-model` note says the lab's and Mojo's limits are not part of the proof |
|
||||||
|
| `INT8` … `INT64`, `UINT8` … `UINT64` | Two's complement or unsigned, n bits (64-bit types held exactly to 2^53 − 1 in this lab) | The run stops (kind `overflow`); `WRAP(X, T)` wraps on purpose (up to 32 bits) | Exact arithmetic plus an `overflow` obligation on every result, store, parameter, RETURN and conversion: "fits INT8". After it, the range is a fact. A sized parameter and a sized variable cut by a loop keep their range |
|
||||||
|
| `FLOAT`, `FLOAT64` | IEEE 754 double | The run stops on infinity or NaN | `FLOAT+` and its kind are uninterpreted: the same operation on the same values gives the same result, nothing more |
|
||||||
|
| `FLOAT32` | IEEE 754 single: every result rounded to 24 bits (`Math.fround`, exact for +, −, ×, ÷ of singles) | As FLOAT | Its own uninterpreted functions (`F32+`, ...); a constant is rounded exactly; a value that becomes a FLOAT32 is `TOFLOAT32(v)`, about which nothing is assumed |
|
||||||
|
|
||||||
|
Rules the checker holds: two different sized types never mix silently (`mixed-width`, `mixed-precision`; convert with `INT32(X)`, `FLOAT32(X)`); a numeric literal takes the type of the sized value beside it, as in Mojo, and a whole-number literal that does not fit is an error before any run (`literal-range`); INTEGER may go into a sized type (checked when it happens), never a different sized type.
|
||||||
|
|
||||||
|
`USE IEEE` lets NaN and the infinities flow as IEEE 754 says (SQRT(−1.0) is NaN, 1.0 / 0.0 is infinity; whole-number division by zero still stops). The prover's reasoning about numbers does not hold for those values, so in such a program nothing is marked proved, and each unit carries an `ieee` note.
|
||||||
|
|
||||||
|
The numerical built-ins (`HYPOT`, `EXPM1`, `LOG1P`, `FMA`, `ULP`, `NEXT_AFTER`, `EXACT`, `IS_CLOSE`, `MAX_VALUE`, the bit operations, ...) are exact or correctly rounded: `src/engines/pseudo/numeric.js` computes `FMA` with one rounding by exact binary arithmetic, `EXACT(X)` gives every decimal digit a FLOAT holds, and `ULP` and `NEXT_AFTER` step through the representation. Functions JavaScript has no exact version of (erf, gamma) are not built-ins; they belong in the library as verified pseudo. Built-ins that stop the run for some arguments (SQRT, the logarithms, ASIN and ACOS, LOG1P, CLAMP, the shifts) carry an `argument` obligation. The built-ins added for numerical work stay usable as names (a variable `E`, a `FUNCTION GCD`): the program's own definition wins (`SOFT_BUILTINS` in `lang.js`).
|
||||||
|
|
||||||
|
`tests/numeric.test.mjs` pins each rule; the corpus programs `49-sized-integers` to `52-elementary` show them in use.
|
||||||
|
|
||||||
### Soundness
|
### Soundness
|
||||||
|
|
||||||
A "proved" must be a proof. Two test files hold the prover to that, and a change to `logic.ts`, `solver.ts` or `vcgen.ts` must keep both green:
|
A "proved" must be a proof. Two test files hold the prover to that, and a change to `logic.ts`, `solver.ts` or `vcgen.ts` must keep both green:
|
||||||
@@ -36,6 +55,9 @@ When a soundness bug is found, add the program that shows it to `tests/prover/`
|
|||||||
| `fill-negative.pseudo`, `randint-empty.pseudo` | `FILL` and `RANDINT` assumed facts about arguments the interpreter checks by stopping, without asking whether they hold | An `argument` obligation, and guarded facts |
|
| `fill-negative.pseudo`, `randint-empty.pseudo` | `FILL` and `RANDINT` assumed facts about arguments the interpreter checks by stopping, without asking whether they hold | An `argument` obligation, and guarded facts |
|
||||||
| `float-overflow.pseudo` | The prover treats FLOAT as numbers (`Y == Y`), while a run went on with infinity and NaN | The interpreter stops on a FLOAT that is not finite |
|
| `float-overflow.pseudo` | The prover treats FLOAT as numbers (`Y == Y`), while a run went on with infinity and NaN | The interpreter stops on a FLOAT that is not finite |
|
||||||
| `foreach-empty.pseudo` | The way out of `FOR EACH` kept "X is in the collection", false after zero passes over an empty list | The fact holds only inside a pass |
|
| `foreach-empty.pseudo` | The way out of `FOR EACH` kept "X is in the collection", false after zero passes over an empty list | The fact holds only inside a pass |
|
||||||
|
| `int8-overflow.pseudo` | (a guard written with the sized types) Sized arithmetic treated as exact with no range would prove `X + 1` fits INT8 | Every sized result has an `overflow` obligation |
|
||||||
|
| `float32-store.pseudo` | (a guard) A whole number stored in a FLOAT32 keeping its exact value would prove `X == N` for N = 16777217 | A value becoming a FLOAT32 is rounded (constants) or `TOFLOAT32(v)` |
|
||||||
|
| `ieee-nan.pseudo` | (a guard) `X == X` for every number fails for NaN | Nothing is proved in a program that says `USE IEEE` |
|
||||||
|
|
||||||
In the app, an obligation the prover marks proved that a generated input breaks at the same place is shown as broken, not proved.
|
In the app, an obligation the prover marks proved that a generated input breaks at the same place is shown as broken, not proved.
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,13 @@
|
|||||||
|
|
||||||
<!-- Written by tests/sync-coverage.test.mjs. Do not edit; rerun `npm test` to refresh. -->
|
<!-- Written by tests/sync-coverage.test.mjs. Do not edit; rerun `npm test` to refresh. -->
|
||||||
|
|
||||||
Each translator in `src/sync/` is run over every exercise with pseudo (17), every program in `tests/corpus/` (48), and 17 one-construct fixtures (`tests/helpers/sync-programs.mjs`). 3 of the 82 have pseudo that does not read, by design (they teach the notation's error messages), and are left out below. That leaves **79 programs**.
|
Each translator in `src/sync/` is run over every exercise with pseudo (17), every program in `tests/corpus/` (52), and 17 one-construct fixtures (`tests/helpers/sync-programs.mjs`). 3 of the 86 have pseudo that does not read, by design (they teach the notation's error messages), and are left out below. That leaves **83 programs**.
|
||||||
|
|
||||||
| From pseudo to | Says it completely | Says it with a gap or a checker error | Cannot say it |
|
| From pseudo to | Says it completely | Says it with a gap or a checker error | Cannot say it |
|
||||||
| --- | ---: | ---: | ---: |
|
| --- | ---: | ---: | ---: |
|
||||||
| Mojo | 77 | 2 | 0 |
|
| Mojo | 80 | 3 | 0 |
|
||||||
| lingo | 31 | 0 | 48 |
|
| lingo | 31 | 0 | 52 |
|
||||||
| matho | 3 | 0 | 76 |
|
| matho | 3 | 0 | 80 |
|
||||||
|
|
||||||
Mojo is a program-for-program translation that always produces something: a line with no faithful spelling becomes a visible `raise Error("not translated: pseudo line N")` and a warning. Lingo and matho must read back as the same program, so what they cannot say exactly they refuse, with the construct and its line, and the pane keeps its last good text.
|
Mojo is a program-for-program translation that always produces something: a line with no faithful spelling becomes a visible `raise Error("not translated: pseudo line N")` and a warning. Lingo and matho must read back as the same program, so what they cannot say exactly they refuse, with the construct and its line, and the pane keeps its last good text.
|
||||||
|
|
||||||
@@ -16,7 +16,9 @@ Mojo is a program-for-program translation that always produces something: a line
|
|||||||
|
|
||||||
These programs translate, with the listed lines left as a `raise Error`.
|
These programs translate, with the listed lines left as a `raise Error`.
|
||||||
|
|
||||||
Nothing.
|
| What it has no form for | Programs | Which |
|
||||||
|
| --- | ---: | --- |
|
||||||
|
| Programming+ could not translate this line into Mojo (the built-in EXACT, which Mojo's standard library has no function for). The generated code stops here with an error instead of guessing. | 1 | 50-float32.pseudo |
|
||||||
|
|
||||||
## Mojo: programs the translator refuses
|
## Mojo: programs the translator refuses
|
||||||
|
|
||||||
@@ -30,7 +32,7 @@ English is read by `readSteps`, whose grammar has no sentence for these.
|
|||||||
|
|
||||||
| What it has no form for | Programs | Which |
|
| What it has no form for | Programs | Which |
|
||||||
| --- | ---: | --- |
|
| --- | ---: | --- |
|
||||||
| steps outside a routine (every English step belongs to a routine) | 46 | 01-arithmetic.pseudo, 02-temperature.pseudo, 03-compound-interest.pseudo, 04-gcd-recursion.pseudo, and 42 more |
|
| steps outside a routine (every English step belongs to a routine) | 50 | 01-arithmetic.pseudo, 02-temperature.pseudo, 03-compound-interest.pseudo, 04-gcd-recursion.pseudo, and 46 more |
|
||||||
| a fixed-length ARRAY | 4 | ex:edit-distance, 05-primes-sieve.pseudo, 11-matrix-multiply.pseudo, 15-game-of-life.pseudo |
|
| a fixed-length ARRAY | 4 | ex:edit-distance, 05-primes-sieve.pseudo, 11-matrix-multiply.pseudo, 15-game-of-life.pseudo |
|
||||||
| a BYREF parameter | 3 | 06-fibonacci-memo.pseudo, 10-insertion-sort.pseudo, 26-swap-byref.pseudo |
|
| a BYREF parameter | 3 | 06-fibonacci-memo.pseudo, 10-insertion-sort.pseudo, 26-swap-byref.pseudo |
|
||||||
| a HASHMAP whose values are not counts | 3 | ex:translate-codons, 13-stack-brackets.pseudo, 39-constants-codons.pseudo |
|
| a HASHMAP whose values are not counts | 3 | ex:translate-codons, 13-stack-brackets.pseudo, 39-constants-codons.pseudo |
|
||||||
@@ -62,7 +64,7 @@ An equation lowers to one `FUNCTION` that returns a value; a program with output
|
|||||||
|
|
||||||
| What it has no form for | Programs | Which |
|
| What it has no form for | Programs | Which |
|
||||||
| --- | ---: | --- |
|
| --- | ---: | --- |
|
||||||
| steps outside a function (an equation is a function) | 42 | 01-arithmetic.pseudo, 02-temperature.pseudo, 03-compound-interest.pseudo, 04-gcd-recursion.pseudo, and 38 more |
|
| steps outside a function (an equation is a function) | 46 | 01-arithmetic.pseudo, 02-temperature.pseudo, 03-compound-interest.pseudo, 04-gcd-recursion.pseudo, and 42 more |
|
||||||
| a function that answers LIST (equations answer numbers) | 5 | ex:match, ex:match-proved, ex:match-off-by-one, ex:match-reset, and 1 more |
|
| a function that answers LIST (equations answer numbers) | 5 | ex:match, ex:match-proved, ex:match-off-by-one, ex:match-reset, and 1 more |
|
||||||
| a function that answers STRING (equations answer numbers) | 5 | ex:revcomp, fixture:string-build, fixture:slice, fixture:case, and 1 more |
|
| a function that answers STRING (equations answer numbers) | 5 | ex:revcomp, fixture:string-build, fixture:slice, fixture:case, and 1 more |
|
||||||
| a function that answers BOOL (equations answer numbers) | 3 | ex:binary-search, fixture:early-return, fixture:forall |
|
| a function that answers BOOL (equations answer numbers) | 3 | ex:binary-search, fixture:early-return, fixture:forall |
|
||||||
|
|||||||
+1
-1
@@ -29,7 +29,7 @@ The Prove tab reports every obligation in three statuses (`src/engines/pseudo/vc
|
|||||||
|
|
||||||
| Shown | Earned by | Means | Does not mean |
|
| Shown | Earned by | Means | Does not mean |
|
||||||
|---|---|---|---|
|
|---|---|---|---|
|
||||||
| **Proved** | The built-in prover (`solver.ts`), on every path it examined, in a unit it examined completely | The claim follows from what is known, in the notation's number model: INTEGER as whole numbers of any size, FLOAT as finite numbers whose rounding is not seen into. The prover is sound (`tests/prover-soundness.test.mjs`) | That the Mojo made from the program has the same property. A 64-bit `Int` wraps and a `Float64` becomes `inf` or `nan`; the `int-model` and `float-model` notes say when a proof leans on the number model. A unit marked **partial** shows no claim as proved |
|
| **Proved** | The built-in prover (`solver.ts`), on every path it examined, in a unit it examined completely | The claim follows from what is known, in the notation's number model: INTEGER as whole numbers of any size, the sized types (INT8 … UINT64) exactly, with an `overflow` obligation wherever a value could leave its range, FLOAT and FLOAT32 as finite numbers whose rounding is not seen into. The prover is sound (`tests/prover-soundness.test.mjs`). In a program that says `USE IEEE` nothing is marked proved | That the Mojo made from the program has the same property. A 64-bit `Int` wraps and a `Float64` becomes `inf` or `nan`; the `int-model` and `float-model` notes say when a proof leans on the number model. A unit marked **partial** shows no claim as proved |
|
||||||
| **Broken** | A generated input (the fuzzer, seeded) or a run that breaks the claim at that place | There is an input for which the claim is false, and **Step through** shows it | Anything about other claims. A run outranks the prover: a proved obligation that an input breaks is shown as broken (and would be a soundness bug) |
|
| **Broken** | A generated input (the fuzzer, seeded) or a run that breaks the claim at that place | There is an input for which the claim is false, and **Step through** shows it | Anything about other claims. A run outranks the prover: a proved obligation that an input breaks is shown as broken (and would be a soundness bug) |
|
||||||
| **Open** | Neither of the above | The checker could not finish the argument, usually because a loop needs an INVARIANT | That the claim is false |
|
| **Open** | Neither of the above | The checker could not finish the argument, usually because a loop needs an INVARIANT | That the claim is false |
|
||||||
| An invariant "the checker found on its own" | Inference in `vcgen.ts`, kept only at a fixed point | It holds on entry and survives every pass, and the proof used it | That it is the invariant a reader needs |
|
| An invariant "the checker found on its own" | Inference in `vcgen.ts`, kept only at a fixed point | It holds on entry and survives every pass, and the proof used it | That it is the invariant a reader needs |
|
||||||
|
|||||||
+17
-6
@@ -9,7 +9,7 @@
|
|||||||
* written here by hand (statement forms, what stops a run, speco syntax)
|
* written here by hand (statement forms, what stops a run, speco syntax)
|
||||||
* carry examples, and `tests/reference.test.mjs` parses every one.
|
* carry examples, and `tests/reference.test.mjs` parses every one.
|
||||||
*/
|
*/
|
||||||
import { KEYWORDS, BUILTINS, PRECEDENCE, CONTEXTUAL } from '../engines/pseudo/lang.js';
|
import { KEYWORDS, BUILTINS, PRECEDENCE, CONTEXTUAL, CONVERSIONS, SOFT_BUILTINS } from '../engines/pseudo/lang.js';
|
||||||
import { SENTENCE_FORMS, COMPARISONS, IN_PHRASES } from '../engines/pseudo/english.js';
|
import { SENTENCE_FORMS, COMPARISONS, IN_PHRASES } from '../engines/pseudo/english.js';
|
||||||
import { OPS, CONDITION_TEXT, REG64, SYSCALLS } from '../engines/asm86/interp.js';
|
import { OPS, CONDITION_TEXT, REG64, SYSCALLS } from '../engines/asm86/interp.js';
|
||||||
import { PRESETS } from '../engines/asm86/machine.js';
|
import { PRESETS } from '../engines/asm86/machine.js';
|
||||||
@@ -25,7 +25,9 @@ export interface RefLanguage { id: LangId; title: string; blurb: string; section
|
|||||||
|
|
||||||
const KEYWORD_GROUPS: Array<[string, string[]]> = [
|
const KEYWORD_GROUPS: Array<[string, string[]]> = [
|
||||||
['Declarations', ['DECLARE', 'CONSTANT', 'DEFINE', 'TYPE', 'ENDTYPE', 'OF']],
|
['Declarations', ['DECLARE', 'CONSTANT', 'DEFINE', 'TYPE', 'ENDTYPE', 'OF']],
|
||||||
|
['Libraries', ['USE']],
|
||||||
['Types', ['INTEGER', 'FLOAT', 'CHAR', 'STRING', 'BOOL', 'BOOLEAN', 'ARRAY', 'LIST', 'HASHMAP', 'SET']],
|
['Types', ['INTEGER', 'FLOAT', 'CHAR', 'STRING', 'BOOL', 'BOOLEAN', 'ARRAY', 'LIST', 'HASHMAP', 'SET']],
|
||||||
|
['Sized numbers', ['INT8', 'INT16', 'INT32', 'INT64', 'UINT8', 'UINT16', 'UINT32', 'UINT64', 'FLOAT32', 'FLOAT64']],
|
||||||
['Selection', ['IF', 'THEN', 'ELSEIF', 'ELSE', 'ENDIF', 'CASE', 'OTHERWISE', 'ENDCASE']],
|
['Selection', ['IF', 'THEN', 'ELSEIF', 'ELSE', 'ENDIF', 'CASE', 'OTHERWISE', 'ENDCASE']],
|
||||||
['Iteration', ['FOR', 'TO', 'STEP', 'NEXT', 'PARALLEL', 'WHILE', 'ENDWHILE', 'REPEAT', 'UNTIL', 'BREAK', 'CONTINUE']],
|
['Iteration', ['FOR', 'TO', 'STEP', 'NEXT', 'PARALLEL', 'WHILE', 'ENDWHILE', 'REPEAT', 'UNTIL', 'BREAK', 'CONTINUE']],
|
||||||
['Subroutines', ['FUNCTION', 'ENDFUNCTION', 'PROCEDURE', 'ENDPROCEDURE', 'RETURNS', 'RETURN', 'CALL', 'BYREF', 'BYVAL']],
|
['Subroutines', ['FUNCTION', 'ENDFUNCTION', 'PROCEDURE', 'ENDPROCEDURE', 'RETURNS', 'RETURN', 'CALL', 'BYREF', 'BYVAL']],
|
||||||
@@ -38,7 +40,11 @@ const KEYWORD_GROUPS: Array<[string, string[]]> = [
|
|||||||
/** What each keyword means, for hover. */
|
/** What each keyword means, for hover. */
|
||||||
export const KEYWORD_MEANING: Record<string, string> = {
|
export const KEYWORD_MEANING: Record<string, string> = {
|
||||||
DECLARE: 'Creates a name and fixes its type, with or without a starting value: DECLARE Counter : INTEGER, or DECLARE Counter : INTEGER <- 0', CONSTANT: 'Names a value that never changes: CONSTANT BasesPerCodon = 3', DEFINE: 'Makes one fixed set of a named set type: DEFINE Purines (\'A\', \'G\') : BaseSet', TYPE: 'Defines a record (TYPE … ENDTYPE with one DECLARE per field) or names a set type (TYPE BaseSet = SET OF CHAR)', ENDTYPE: 'Closes a TYPE block', OF: 'Joins a collection to its element type: LIST OF INTEGER',
|
DECLARE: 'Creates a name and fixes its type, with or without a starting value: DECLARE Counter : INTEGER, or DECLARE Counter : INTEGER <- 0', CONSTANT: 'Names a value that never changes: CONSTANT BasesPerCodon = 3', DEFINE: 'Makes one fixed set of a named set type: DEFINE Purines (\'A\', \'G\') : BaseSet', TYPE: 'Defines a record (TYPE … ENDTYPE with one DECLARE per field) or names a set type (TYPE BaseSet = SET OF CHAR)', ENDTYPE: 'Closes a TYPE block', OF: 'Joins a collection to its element type: LIST OF INTEGER',
|
||||||
INTEGER: 'A whole number', FLOAT: 'A number with a fractional part; / always produces one', CHAR: 'Exactly one character, in single quotes', STRING: 'Any number of characters, in double quotes; positions count from 0', BOOL: 'TRUE or FALSE', ARRAY: 'A fixed number of values of one type, declared by length: ARRAY[4] OF INTEGER, or ARRAY[R, C] for two dimensions', LIST: 'Any number of values of one type, in order; starts empty and grows with APPEND', HASHMAP: 'Values reached by key: HASHMAP OF STRING : CHAR', SET: 'Distinct values in no order; ask IN',
|
USE: 'Brings routines from the library into the program: USE stats (the whole module) or USE stats.Mean (one routine). USE IEEE lets NaN and infinities flow as IEEE 754 says instead of stopping the run',
|
||||||
|
INTEGER: 'A whole number of any size (a run holds it exactly to 9,007,199,254,740,991)', FLOAT: 'A number with a fractional part, in IEEE 754 double precision; / always produces one',
|
||||||
|
INT8: 'A whole number from -128 to 127; a result outside that stops the run', INT16: 'A whole number from -32,768 to 32,767', INT32: 'A whole number from -2,147,483,648 to 2,147,483,647', INT64: 'A 64-bit whole number (held exactly in this lab to ±9,007,199,254,740,991)',
|
||||||
|
UINT8: 'A whole number from 0 to 255: a byte', UINT16: 'A whole number from 0 to 65,535', UINT32: 'A whole number from 0 to 4,294,967,295', UINT64: 'A 64-bit whole number of 0 or more (held exactly in this lab to 9,007,199,254,740,991)',
|
||||||
|
FLOAT32: 'A FLOAT in single precision (about 7 significant digits): every result is rounded to the nearest single', FLOAT64: 'Another name for FLOAT: double precision, about 16 significant digits', CHAR: 'Exactly one character, in single quotes', STRING: 'Any number of characters, in double quotes; positions count from 0', BOOL: 'TRUE or FALSE', ARRAY: 'A fixed number of values of one type, declared by length: ARRAY[4] OF INTEGER, or ARRAY[R, C] for two dimensions', LIST: 'Any number of values of one type, in order; starts empty and grows with APPEND', HASHMAP: 'Values reached by key: HASHMAP OF STRING : CHAR', SET: 'Distinct values in no order; ask IN',
|
||||||
IF: 'Runs its body when the condition is TRUE', THEN: 'Ends the condition of an IF', ELSEIF: 'One word: another condition under the same IF, tested only if the ones above failed', ELSE: 'The body that runs when no condition above was TRUE', ENDIF: 'Closes an IF', CASE: 'CASE OF X: picks the first arm whose value, list of values, or half-open range matches X', OTHERWISE: 'The arm of a CASE that runs when nothing else matched; must come last', ENDCASE: 'Closes a CASE',
|
IF: 'Runs its body when the condition is TRUE', THEN: 'Ends the condition of an IF', ELSEIF: 'One word: another condition under the same IF, tested only if the ones above failed', ELSE: 'The body that runs when no condition above was TRUE', ENDIF: 'Closes an IF', CASE: 'CASE OF X: picks the first arm whose value, list of values, or half-open range matches X', OTHERWISE: 'The arm of a CASE that runs when nothing else matched; must come last', ENDCASE: 'Closes a CASE',
|
||||||
FOR: 'A counted loop: FOR I <- 0 TO N runs with I = 0, 1, …, N-1. The bound is not reached. FOR EACH X IN C visits every value', PARALLEL: 'PARALLEL FOR I <- 0 TO N: a counted loop whose passes are independent, as on a GPU where each runs on its own thread. The lab runs them and stops with a data race if one pass writes a location another pass touches', TO: 'The stop value of a FOR, which the counter never reaches (half-open), or the upper end of a CASE range', STEP: 'The increment of a FOR counter; negative counts down', NEXT: 'Closes a FOR, naming its counter: NEXT I', WHILE: 'Tests before each pass; the body may run zero times', ENDWHILE: 'Closes a WHILE', REPEAT: 'Runs the body, then tests; the body runs at least once', UNTIL: 'Closes a REPEAT with the condition that stops it', BREAK: 'Leaves the innermost loop at once', CONTINUE: 'Skips the rest of this pass and starts the next',
|
FOR: 'A counted loop: FOR I <- 0 TO N runs with I = 0, 1, …, N-1. The bound is not reached. FOR EACH X IN C visits every value', PARALLEL: 'PARALLEL FOR I <- 0 TO N: a counted loop whose passes are independent, as on a GPU where each runs on its own thread. The lab runs them and stops with a data race if one pass writes a location another pass touches', TO: 'The stop value of a FOR, which the counter never reaches (half-open), or the upper end of a CASE range', STEP: 'The increment of a FOR counter; negative counts down', NEXT: 'Closes a FOR, naming its counter: NEXT I', WHILE: 'Tests before each pass; the body may run zero times', ENDWHILE: 'Closes a WHILE', REPEAT: 'Runs the body, then tests; the body runs at least once', UNTIL: 'Closes a REPEAT with the condition that stops it', BREAK: 'Leaves the innermost loop at once', CONTINUE: 'Skips the rest of this pass and starts the next',
|
||||||
FUNCTION: 'A subroutine that hands back one value: FUNCTION F(X : INTEGER) RETURNS INTEGER', ENDFUNCTION: 'Closes a FUNCTION', PROCEDURE: 'A subroutine that does something and returns nothing; called with CALL', ENDPROCEDURE: 'Closes a PROCEDURE', RETURNS: 'States the type of a FUNCTION\'s answer', RETURN: 'Hands back the answer and ends the subroutine at once', CALL: 'Runs a PROCEDURE on a line of its own: CALL REPORT("ATT", Found)', BYREF: 'Marks a parameter the procedure may change in the caller; everything else is a copy', BYVAL: 'States the default explicitly: the parameter is a copy',
|
FUNCTION: 'A subroutine that hands back one value: FUNCTION F(X : INTEGER) RETURNS INTEGER', ENDFUNCTION: 'Closes a FUNCTION', PROCEDURE: 'A subroutine that does something and returns nothing; called with CALL', ENDPROCEDURE: 'Closes a PROCEDURE', RETURNS: 'States the type of a FUNCTION\'s answer', RETURN: 'Hands back the answer and ends the subroutine at once', CALL: 'Runs a PROCEDURE on a line of its own: CALL REPORT("ATT", Found)', BYREF: 'Marks a parameter the procedure may change in the caller; everything else is a copy', BYVAL: 'States the default explicitly: the parameter is a copy',
|
||||||
@@ -59,6 +65,8 @@ const OPERATOR_MEANING: Record<string, string> = {
|
|||||||
const PSEUDO_STATEMENTS: RefEntry[] = [
|
const PSEUDO_STATEMENTS: RefEntry[] = [
|
||||||
{ term: 'DECLARE <name> : <type>', meaning: 'Creates a name of that type with no value yet. Several names may share a line.', example: 'DECLARE Counter : INTEGER\nDECLARE N, M : INTEGER' },
|
{ term: 'DECLARE <name> : <type>', meaning: 'Creates a name of that type with no value yet. Several names may share a line.', example: 'DECLARE Counter : INTEGER\nDECLARE N, M : INTEGER' },
|
||||||
{ term: 'DECLARE <name> : <type> <- <expr>', meaning: 'Creates a name and gives it its starting value on the same line, as Mojo\'s var total: Int = 0 does. One name per line in this form; the two-line form (DECLARE, then an assignment) means the same.', example: 'DECLARE T : STRING <- "GATTACA"\nDECLARE N : INTEGER <- LENGTH(T)\nDECLARE Total : INTEGER <- 0\nOUTPUT N, " ", Total' },
|
{ term: 'DECLARE <name> : <type> <- <expr>', meaning: 'Creates a name and gives it its starting value on the same line, as Mojo\'s var total: Int = 0 does. One name per line in this form; the two-line form (DECLARE, then an assignment) means the same.', example: 'DECLARE T : STRING <- "GATTACA"\nDECLARE N : INTEGER <- LENGTH(T)\nDECLARE Total : INTEGER <- 0\nOUTPUT N, " ", Total' },
|
||||||
|
{ term: 'USE <module> / USE <module>.<Routine>', meaning: 'Brings verified routines from the library into the program, to be called like your own. A run does not step into them (open the routine in the library to do that); the prover uses their REQUIRE and ENSURE lines at each call. Lines go at the top of the program.', example: 'USE IEEE\nDECLARE X : FLOAT\nX <- 1.0 / 0.0\nOUTPUT IS_INF(X)' },
|
||||||
|
{ term: 'DECLARE <name> : INT32 (or another sized type)', meaning: 'A number of a fixed width. A whole-number result that does not fit stops the run rather than wrapping around; a FLOAT32 result is rounded to single precision. Two different sized types meet only through a conversion such as INT32(X) or FLOAT32(X), and a literal takes the type beside it.', example: 'DECLARE Count : UINT8 <- 250\nCount <- Count + 5\nOUTPUT Count, " ", WRAP(Count + 1, UINT8)\nDECLARE X : FLOAT32 <- 0.1\nOUTPUT X, " holds ", EXACT(X)' },
|
||||||
{ term: 'CONSTANT <name> = <value>', meaning: 'A value fixed for the whole program; literals and other constants only.', example: 'CONSTANT BasesPerCodon = 3' },
|
{ term: 'CONSTANT <name> = <value>', meaning: 'A value fixed for the whole program; literals and other constants only.', example: 'CONSTANT BasesPerCodon = 3' },
|
||||||
{ term: '<name> <- <expr>', meaning: 'Assignment. The right side is worked out first, then stored.', example: 'DECLARE Counter : INTEGER\nCounter <- 0\nCounter <- Counter + 1' },
|
{ term: '<name> <- <expr>', meaning: 'Assignment. The right side is worked out first, then stored.', example: 'DECLARE Counter : INTEGER\nCounter <- 0\nCounter <- Counter + 1' },
|
||||||
{ term: 'OUTPUT <expr>, <expr>, ...', meaning: 'Shows values in order. A collection prints the way you would type it.', example: 'OUTPUT "Found ", 3, " matches"' },
|
{ term: 'OUTPUT <expr>, <expr>, ...', meaning: 'Shows values in order. A collection prints the way you would type it.', example: 'OUTPUT "Found ", 3, " matches"' },
|
||||||
@@ -81,6 +89,8 @@ const PSEUDO_STATEMENTS: RefEntry[] = [
|
|||||||
const PSEUDO_LITERALS: RefEntry[] = [
|
const PSEUDO_LITERALS: RefEntry[] = [
|
||||||
{ term: '42, -3', meaning: 'an INTEGER' },
|
{ term: '42, -3', meaning: 'an INTEGER' },
|
||||||
{ term: '0.5, 2.0, 1.5e-3', meaning: 'a FLOAT; a digit on each side of the point is required' },
|
{ term: '0.5, 2.0, 1.5e-3', meaning: 'a FLOAT; a digit on each side of the point is required' },
|
||||||
|
{ term: '1_000_000, 0xFF, 0b1010', meaning: 'an INTEGER with underscores between digits, in hexadecimal, or in binary' },
|
||||||
|
{ term: '0.1 next to a FLOAT32', meaning: 'a literal takes the type of the sized value beside it: 0.1 becomes the nearest FLOAT32, and 1 next to an INT8 is an INT8' },
|
||||||
{ term: "'A'", meaning: 'a CHAR: exactly one character in single quotes' },
|
{ term: "'A'", meaning: 'a CHAR: exactly one character in single quotes' },
|
||||||
{ term: '"GATTACA", ""', meaning: 'a STRING in double quotes; \\n, \\t, \\" and \\\\ are escapes' },
|
{ term: '"GATTACA", ""', meaning: 'a STRING in double quotes; \\n, \\t, \\" and \\\\ are escapes' },
|
||||||
{ term: 'TRUE, FALSE', meaning: 'a BOOL' },
|
{ term: 'TRUE, FALSE', meaning: 'a BOOL' },
|
||||||
@@ -97,8 +107,8 @@ const PSEUDO_STOPS: RefEntry[] = [
|
|||||||
{ term: 'No value yet', meaning: 'reading a name, a position, or a field before anything is stored in it, or a FOR counter after its loop' },
|
{ term: 'No value yet', meaning: 'reading a name, a position, or a field before anything is stored in it, or a FOR counter after its loop' },
|
||||||
{ term: 'Missing key', meaning: 'reading H[K] when K is not a key of H; test with IN first' },
|
{ term: 'Missing key', meaning: 'reading H[K] when K is not a key of H; test with IN first' },
|
||||||
{ term: 'Division by zero', meaning: 'dividing by 0 with /, DIV, or MOD' },
|
{ term: 'Division by zero', meaning: 'dividing by 0 with /, DIV, or MOD' },
|
||||||
{ term: 'No answer', meaning: 'a whole number to a negative power, SQRT of a negative, LOG of a non-positive, MIN of an empty list, text TO_INTEGER cannot read' },
|
{ term: 'No answer', meaning: 'a whole number to a negative power, SQRT of a negative, LOG of a non-positive, ASIN or ACOS outside [-1, 1], MIN of an empty list, text TO_INTEGER cannot read (the FLOAT cases give NaN or an infinity instead when the program says USE IEEE)' },
|
||||||
{ term: 'Too large', meaning: 'a whole number beyond ±9,007,199,254,740,991' },
|
{ term: 'Too large', meaning: 'a whole number beyond ±9,007,199,254,740,991, a result outside its sized type (an INT8 past 127), a conversion such as INT32(X) that does not fit, or a FLOAT past about 1.8 × 10^308 (unless the program says USE IEEE)' },
|
||||||
{ term: 'Input', meaning: 'INPUT with nothing left, or input that does not convert' },
|
{ term: 'Input', meaning: 'INPUT with nothing left, or input that does not convert' },
|
||||||
{ term: 'A broken promise', meaning: 'a REQUIRE, ENSURE, INVARIANT, DECREASES, or ASSERT that is FALSE' },
|
{ term: 'A broken promise', meaning: 'a REQUIRE, ENSURE, INVARIANT, DECREASES, or ASSERT that is FALSE' },
|
||||||
{ term: 'No end in sight', meaning: 'more steps than the lab allows (20,000 here); the lab names the loop and says whether its condition has stopped changing' },
|
{ term: 'No end in sight', meaning: 'more steps than the lab allows (20,000 here); the lab names the loop and says whether its condition has stopped changing' },
|
||||||
@@ -172,12 +182,13 @@ export const PSEUDO_IDIOMS: RefEntry[] = [
|
|||||||
export function reservedNames(): RefEntry[] {
|
export function reservedNames(): RefEntry[] {
|
||||||
const kw = [...(KEYWORDS as Set<string>)].sort().map((w) => ({ term: w, meaning: 'keyword', note: KEYWORD_MEANING[w] ? KEYWORD_MEANING[w].split(/[:;.]/)[0] : '' }));
|
const kw = [...(KEYWORDS as Set<string>)].sort().map((w) => ({ term: w, meaning: 'keyword', note: KEYWORD_MEANING[w] ? KEYWORD_MEANING[w].split(/[:;.]/)[0] : '' }));
|
||||||
const ctx = Object.keys(CONTEXTUAL as Record<string, string>).sort().map((w) => ({ term: w, meaning: 'contextual keyword', note: KEYWORD_MEANING[w] ? KEYWORD_MEANING[w].split(/[:;.]/)[0] : '' }));
|
const ctx = Object.keys(CONTEXTUAL as Record<string, string>).sort().map((w) => ({ term: w, meaning: 'contextual keyword', note: KEYWORD_MEANING[w] ? KEYWORD_MEANING[w].split(/[:;.]/)[0] : '' }));
|
||||||
const bi = Object.keys(BUILTINS as Record<string, any>).sort().map((w) => ({ term: w, meaning: 'built-in function', note: ((BUILTINS as any)[w].doc || '').split(/[:;.]/)[0] }));
|
const bi = Object.keys(BUILTINS as Record<string, any>).filter((w) => !(SOFT_BUILTINS as Set<string>).has(w)).sort().map((w) => ({ term: w, meaning: 'built-in function', note: ((BUILTINS as any)[w].doc || '').split(/[:;.]/)[0] }));
|
||||||
return [...kw, ...ctx, ...bi];
|
return [...kw, ...ctx, ...bi];
|
||||||
}
|
}
|
||||||
|
|
||||||
function builtinEntries(): RefEntry[] {
|
function builtinEntries(): RefEntry[] {
|
||||||
return Object.entries(BUILTINS as Record<string, any>).map(([name, b]) => ({ term: b.sig || name, meaning: b.doc || '', note: b.group || '' }));
|
return Object.entries(BUILTINS as Record<string, any>).map(([name, b]) => ({ term: b.sig || name, meaning: b.doc || '', note: b.group || '' }))
|
||||||
|
.concat(Object.values(CONVERSIONS as Record<string, any>).map((c) => ({ term: c.sig, meaning: c.doc, note: c.group })));
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PSEUDO_REF: RefLanguage = {
|
export const PSEUDO_REF: RefLanguage = {
|
||||||
|
|||||||
+187
-25
@@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
T, BUILTINS, MUTATORS, typeName, sameType, assignable, isNumeric, isAny, isText,
|
T, BUILTINS, MUTATORS, typeName, sameType, assignable, isNumeric, isAny, isText,
|
||||||
|
SIZED, isSizedInt, isFloat32, intRange, intRangeText, TYPE_ARGS, SOFT_BUILTINS,
|
||||||
} from './lang.js';
|
} from './lang.js';
|
||||||
import { diag, sourceOf } from './parse.js';
|
import { diag, sourceOf } from './parse.js';
|
||||||
import { editDistance } from '../shared/text.js';
|
import { editDistance } from '../shared/text.js';
|
||||||
@@ -50,8 +51,8 @@ for (const k of Object.keys(FOREIGN_FUNCTIONS)) if (!FOREIGN_FUNCTIONS[k]) delet
|
|||||||
|
|
||||||
/** Type names from Python and Mojo, and this notation's word for each. */
|
/** Type names from Python and Mojo, and this notation's word for each. */
|
||||||
const FOREIGN_TYPES = {
|
const FOREIGN_TYPES = {
|
||||||
INT: 'INTEGER', INT64: 'INTEGER', LONG: 'INTEGER', NUMBER: 'INTEGER or FLOAT',
|
INT: 'INTEGER (or INT64 for a 64-bit whole number)', LONG: 'INT64', NUMBER: 'INTEGER or FLOAT',
|
||||||
STR: 'STRING', TEXT: 'STRING', FLOAT64: 'FLOAT', DOUBLE: 'FLOAT', REAL: 'FLOAT',
|
STR: 'STRING', TEXT: 'STRING', DOUBLE: 'FLOAT64', REAL: 'FLOAT', SINGLE: 'FLOAT32',
|
||||||
BOOLEAN: 'BOOL', DICT: 'HASHMAP OF <key type> TO <value type>', MAP: 'HASHMAP OF <key type> TO <value type>',
|
BOOLEAN: 'BOOL', DICT: 'HASHMAP OF <key type> TO <value type>', MAP: 'HASHMAP OF <key type> TO <value type>',
|
||||||
ARRAYLIST: 'LIST OF <type>',
|
ARRAYLIST: 'LIST OF <type>',
|
||||||
};
|
};
|
||||||
@@ -85,12 +86,24 @@ function elementOf(t) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Two numbers combine to FLOAT unless both are INTEGER. */
|
/**
|
||||||
|
* The type two numbers combine to. A FLOAT with anything is a FLOAT (FLOAT32 stays FLOAT32 next to a whole
|
||||||
|
* number; FLOAT32 next to FLOAT64 is a clash the caller reports). Two whole numbers are INTEGER, or the sized
|
||||||
|
* type when one is sized (two different widths are a clash the caller reports).
|
||||||
|
*/
|
||||||
function numericResult(a, b) {
|
function numericResult(a, b) {
|
||||||
if (isAny(a) || isAny(b)) return isAny(a) ? (isAny(b) ? T.ANY : b) : a;
|
if (isAny(a) || isAny(b)) return isAny(a) ? (isAny(b) ? T.ANY : b) : a;
|
||||||
return a.k === 'FLOAT' || b.k === 'FLOAT' ? T.FLOAT : T.INTEGER;
|
if (a.k === 'FLOAT' || b.k === 'FLOAT') {
|
||||||
|
if (a.k === 'FLOAT' && b.k === 'FLOAT') return isFloat32(a) && isFloat32(b) ? a : (a.bits === 64 ? a : b.bits === 64 ? b : T.FLOAT);
|
||||||
|
return a.k === 'FLOAT' ? a : b;
|
||||||
|
}
|
||||||
|
if (a.bits) return a;
|
||||||
|
return b.bits ? b : T.INTEGER;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** A numeric literal, which takes the type of the sized value beside it: X + 1 for an INT8 X is INT8 arithmetic. */
|
||||||
|
const isLiteral = (n) => !!n && n.kind === 'Num';
|
||||||
|
|
||||||
/** Does a list of statements end every path in RETURN? */
|
/** Does a list of statements end every path in RETURN? */
|
||||||
export function alwaysReturns(body) {
|
export function alwaysReturns(body) {
|
||||||
for (const s of body) {
|
for (const s of body) {
|
||||||
@@ -124,8 +137,10 @@ class Scope {
|
|||||||
/* ------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------ */
|
||||||
|
|
||||||
class Checker {
|
class Checker {
|
||||||
constructor(parsed) {
|
constructor(parsed, options = {}) {
|
||||||
this.parsed = parsed;
|
this.parsed = parsed;
|
||||||
|
this.libs = options.libs || new Map(); // routines from the library: UPPER name -> checked routine
|
||||||
|
this.libOrigin = options.libOrigin || new Map(); // UPPER name -> { module, line } of the USE that brought it
|
||||||
this.diags = [];
|
this.diags = [];
|
||||||
this.types = new Map(); // record types
|
this.types = new Map(); // record types
|
||||||
this.setTypes = new Map(); // TYPE X = SET OF ...
|
this.setTypes = new Map(); // TYPE X = SET OF ...
|
||||||
@@ -138,6 +153,9 @@ class Checker {
|
|||||||
this.ctx = null;
|
this.ctx = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** A routine defined in this program, or brought in from the library with USE. */
|
||||||
|
findSub(name) { const k = up(name); return this.subs.get(k) || this.libs.get(k); }
|
||||||
|
|
||||||
error(code, node, message, extra = {}) { this.diags.push(diag('error', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); }
|
error(code, node, message, extra = {}) { this.diags.push(diag('error', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); }
|
||||||
warn(code, node, message, extra = {}) { this.diags.push(diag('warning', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); }
|
warn(code, node, message, extra = {}) { this.diags.push(diag('warning', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); }
|
||||||
note(code, node, message, extra = {}) { this.diags.push(diag('note', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); }
|
note(code, node, message, extra = {}) { this.diags.push(diag('note', code, node?.line ?? 1, message, { col: node?.col ?? 1, ...extra })); }
|
||||||
@@ -158,7 +176,8 @@ class Checker {
|
|||||||
} else if (s.kind === 'Function' || s.kind === 'Procedure') {
|
} else if (s.kind === 'Function' || s.kind === 'Procedure') {
|
||||||
if (s.name.startsWith('<')) this.subs.set(`${up(s.name)}#${s.line}`, { name: s.name, kind: s.kind, node: s, params: [], returns: null, requires: [], ensures: [], decreases: null });
|
if (s.name.startsWith('<')) this.subs.set(`${up(s.name)}#${s.line}`, { name: s.name, kind: s.kind, node: s, params: [], returns: null, requires: [], ensures: [], decreases: null });
|
||||||
else if (this.subs.has(up(s.name))) this.error('duplicate', s, `${s.name} is already defined on line ${this.subs.get(up(s.name)).node.line}.`);
|
else if (this.subs.has(up(s.name))) this.error('duplicate', s, `${s.name} is already defined on line ${this.subs.get(up(s.name)).node.line}.`);
|
||||||
else if (BUILTINS[up(s.name)]) this.error('builtin-name', s, `${up(s.name)} is a built-in, so a ${s.kind.toUpperCase()} cannot take its name.`);
|
else if ((BUILTINS[up(s.name)] && !SOFT_BUILTINS.has(up(s.name))) || SIZED[up(s.name)]) this.error('builtin-name', s, `${up(s.name)} is a built-in, so a ${s.kind.toUpperCase()} cannot take its name.`);
|
||||||
|
else if (this.libs.has(up(s.name))) this.error('library-clash', s, `${s.name} also comes from the library (USE ${this.libOrigin.get(up(s.name))?.module} on line ${this.libOrigin.get(up(s.name))?.line}). Rename this one, or bring in only the routines you need: USE ${this.libOrigin.get(up(s.name))?.module}.<Name>.`, { guide: 'libraries' });
|
||||||
else this.subs.set(up(s.name), { name: s.name, kind: s.kind, node: s, params: [], returns: null, requires: [], ensures: [], decreases: null });
|
else this.subs.set(up(s.name), { name: s.name, kind: s.kind, node: s, params: [], returns: null, requires: [], ensures: [], decreases: null });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -250,7 +269,7 @@ class Checker {
|
|||||||
this.collect(ast.body);
|
this.collect(ast.body);
|
||||||
const mainBody = [];
|
const mainBody = [];
|
||||||
for (const s of ast.body) {
|
for (const s of ast.body) {
|
||||||
if (['TypeDef', 'SetTypeDef', 'Constant', 'Define'].includes(s.kind)) continue;
|
if (['TypeDef', 'SetTypeDef', 'Constant', 'Define', 'Use'].includes(s.kind)) continue;
|
||||||
if (s.kind === 'Function' || s.kind === 'Procedure') { this.subroutine(this.subs.get(up(s.name)) || this.subs.get(`${up(s.name)}#${s.line}`) || { node: s, params: [], returns: T.ANY, requires: [], ensures: [] }); continue; }
|
if (s.kind === 'Function' || s.kind === 'Procedure') { this.subroutine(this.subs.get(up(s.name)) || this.subs.get(`${up(s.name)}#${s.line}`) || { node: s, params: [], returns: T.ANY, requires: [], ensures: [] }); continue; }
|
||||||
mainBody.push(s);
|
mainBody.push(s);
|
||||||
}
|
}
|
||||||
@@ -265,6 +284,9 @@ class Checker {
|
|||||||
constants: this.constants,
|
constants: this.constants,
|
||||||
defines: this.defines,
|
defines: this.defines,
|
||||||
subs: this.subs,
|
subs: this.subs,
|
||||||
|
libs: this.libs,
|
||||||
|
uses: ast.body.filter((s) => s.kind === 'Use'),
|
||||||
|
ieee: ast.body.some((s) => s.kind === 'Use' && s.ieee),
|
||||||
main: { body: mainBody, scope: this.mainScope, symbols: [...this.mainScope.map.values()] },
|
main: { body: mainBody, scope: this.mainScope, symbols: [...this.mainScope.map.values()] },
|
||||||
blanks: this.blanks,
|
blanks: this.blanks,
|
||||||
};
|
};
|
||||||
@@ -337,8 +359,8 @@ class Checker {
|
|||||||
}
|
}
|
||||||
const g = this.global.get(name);
|
const g = this.global.get(name);
|
||||||
if (g) this.error('duplicate', node, `${name} is already the name of a ${g.kind === 'const' ? 'CONSTANT' : 'set'} defined on line ${g.line}.`);
|
if (g) this.error('duplicate', node, `${name} is already the name of a ${g.kind === 'const' ? 'CONSTANT' : 'set'} defined on line ${g.line}.`);
|
||||||
if (this.subs.has(k)) this.error('duplicate', node, `${name} is already the name of a ${this.subs.get(k).kind.toUpperCase()}.`);
|
if (this.findSub(k)) this.error('duplicate', node, `${name} is already the name of a ${this.findSub(k).kind.toUpperCase()}${this.libs.has(k) ? ' from the library' : ''}.`);
|
||||||
if (BUILTINS[k]) this.error('builtin-name', node, `${k} is a built-in, so a variable cannot take its name.`);
|
if ((BUILTINS[k] && !SOFT_BUILTINS.has(k)) || SIZED[k]) this.error('builtin-name', node, `${k} is a built-in, so a variable cannot take its name.`);
|
||||||
if (this.types.has(k) || this.setTypes.has(k)) this.error('duplicate', node, `${name} is already the name of a type.`);
|
if (this.types.has(k) || this.setTypes.has(k)) this.error('duplicate', node, `${name} is already the name of a type.`);
|
||||||
return scope.set(name, sym);
|
return scope.set(name, sym);
|
||||||
}
|
}
|
||||||
@@ -359,6 +381,7 @@ class Checker {
|
|||||||
this.error('global-only', s, `${s.kind === 'TypeDef' || s.kind === 'SetTypeDef' ? 'TYPE' : s.kind === 'Constant' ? 'CONSTANT' : 'DEFINE'} lines go at the top level, outside every block.`);
|
this.error('global-only', s, `${s.kind === 'TypeDef' || s.kind === 'SetTypeDef' ? 'TYPE' : s.kind === 'Constant' ? 'CONSTANT' : 'DEFINE'} lines go at the top level, outside every block.`);
|
||||||
return;
|
return;
|
||||||
case 'Function': case 'Procedure': return;
|
case 'Function': case 'Procedure': return;
|
||||||
|
case 'Use': this.error('use-place', s, 'USE lines go at the top of the program, outside every block.', { guide: 'libraries' }); return;
|
||||||
case 'Assign': this.assign(s, scope); return;
|
case 'Assign': this.assign(s, scope); return;
|
||||||
case 'Input': {
|
case 'Input': {
|
||||||
if (!s.target) return;
|
if (!s.target) return;
|
||||||
@@ -553,11 +576,13 @@ class Checker {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!s.value) { this.error('return-value', s, `FUNCTION ${sub.name} has to hand back ${article(sub.returns)}, so its RETURN needs a value.`, { guide: 'function' }); return; }
|
if (!s.value) { this.error('return-value', s, `FUNCTION ${sub.name} has to hand back ${article(sub.returns)}, so its RETURN needs a value.`, { guide: 'function' }); return; }
|
||||||
const t = this.expr(s.value, scope);
|
const t = this.adopt(s.value, sub.returns, this.expr(s.value, scope));
|
||||||
if (!assignable(sub.returns, t)) this.error('type', s.value, `FUNCTION ${sub.name} promises ${article(sub.returns)} with RETURNS, and ${sourceOf(s.value)} is ${article(t)}.${this.narrowHint(sub.returns, t, s.value)}`, { guide: 'function' });
|
if (!assignable(sub.returns, t)) this.error('type', s.value, `FUNCTION ${sub.name} promises ${article(sub.returns)} with RETURNS, and ${sourceOf(s.value)} is ${article(t)}.${this.narrowHint(sub.returns, t, s.value)}`, { guide: 'function' });
|
||||||
}
|
}
|
||||||
|
|
||||||
narrowHint(to, from, value) {
|
narrowHint(to, from, value) {
|
||||||
|
if (to.k === 'INTEGER' && from.k === 'INTEGER' && to.bits && from.bits) return ` Two different sized types do not mix silently: convert with ${to.name}(${sourceOf(value)}), which stops the run if the value does not fit.`;
|
||||||
|
if (isFloat32(to) && from.k === 'FLOAT') return ` A FLOAT64 rounds when it becomes a FLOAT32, so the conversion is written out: FLOAT32(${sourceOf(value)}).`;
|
||||||
if (to.k === 'INTEGER' && from.k === 'FLOAT') {
|
if (to.k === 'INTEGER' && from.k === 'FLOAT') {
|
||||||
if (value && value.kind === 'Binary' && value.op === '/') return ' / always gives a FLOAT, even when both sides are whole. Use DIV for a whole-number answer.';
|
if (value && value.kind === 'Binary' && value.op === '/') return ' / always gives a FLOAT, even when both sides are whole. Use DIV for a whole-number answer.';
|
||||||
return ' Use INT or ROUND to turn a FLOAT into a whole number, and decide which one you mean.';
|
return ' Use INT or ROUND to turn a FLOAT into a whole number, and decide which one you mean.';
|
||||||
@@ -568,7 +593,7 @@ class Checker {
|
|||||||
|
|
||||||
assign(s, scope) {
|
assign(s, scope) {
|
||||||
const tt = this.target(s.target, scope, 'assign');
|
const tt = this.target(s.target, scope, 'assign');
|
||||||
const vt = this.expr(s.value, scope, { expect: tt });
|
const vt = this.adopt(s.value, tt, this.expr(s.value, scope, { expect: tt }));
|
||||||
if (!tt || isAny(tt) || isAny(vt)) return;
|
if (!tt || isAny(tt) || isAny(vt)) return;
|
||||||
if (vt.k === 'VOID') { this.error('type', s.value, `${sourceOf(s.value)} hands nothing back, so there is nothing to store.`); return; }
|
if (vt.k === 'VOID') { this.error('type', s.value, `${sourceOf(s.value)} hands nothing back, so there is nothing to store.`); return; }
|
||||||
if (tt.k === 'STRING' && vt.k === 'CHAR') return;
|
if (tt.k === 'STRING' && vt.k === 'CHAR') return;
|
||||||
@@ -642,7 +667,7 @@ class Checker {
|
|||||||
e.sym = mainSym;
|
e.sym = mainSym;
|
||||||
return mainSym;
|
return mainSym;
|
||||||
}
|
}
|
||||||
if (this.subs.has(up(name))) { this.error('sub-as-value', e, `${name} is a ${this.subs.get(up(name)).kind.toUpperCase()}, and it needs brackets and its arguments to be called.`); return null; }
|
if (this.findSub(name)) { this.error('sub-as-value', e, `${name} is a ${this.findSub(name).kind.toUpperCase()}, and it needs brackets and its arguments to be called.`); return null; }
|
||||||
if (up(name) === 'RESULT') { this.error('result-place', e, 'RESULT means the value a FUNCTION returns, so it can appear only in that function\'s ENSURE lines.', { guide: 'contracts' }); return null; }
|
if (up(name) === 'RESULT') { this.error('result-place', e, 'RESULT means the value a FUNCTION returns, so it can appear only in that function\'s ENSURE lines.', { guide: 'contracts' }); return null; }
|
||||||
const pool = [...scope.all().values()].map((x) => x.name);
|
const pool = [...scope.all().values()].map((x) => x.name);
|
||||||
const s = suggest(name, pool);
|
const s = suggest(name, pool);
|
||||||
@@ -662,7 +687,7 @@ class Checker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
callStmt(s, scope) {
|
callStmt(s, scope) {
|
||||||
const sub = this.subs.get(up(s.name));
|
const sub = this.findSub(s.name);
|
||||||
if (!sub) {
|
if (!sub) {
|
||||||
if (BUILTINS[up(s.name)]?.kind === 'procedure') {
|
if (BUILTINS[up(s.name)]?.kind === 'procedure') {
|
||||||
this.note('builtin-call', s, `${up(s.name)} is a built-in procedure and needs no CALL: write ${up(s.name)}(...) on its own.`);
|
this.note('builtin-call', s, `${up(s.name)} is a built-in procedure and needs no CALL: write ${up(s.name)}(...) on its own.`);
|
||||||
@@ -670,7 +695,7 @@ class Checker {
|
|||||||
s.builtin = true;
|
s.builtin = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const hint = suggest(s.name, [...this.subs.values()].map((x) => x.name));
|
const hint = suggest(s.name, [...this.subs.values(), ...this.libs.values()].map((x) => x.name));
|
||||||
this.error('unknown-sub', s, `No PROCEDURE is called ${s.name}.${hint.length ? ` Did you mean ${hint.join(' or ')}?` : ''}`, { guide: 'procedure' });
|
this.error('unknown-sub', s, `No PROCEDURE is called ${s.name}.${hint.length ? ` Did you mean ${hint.join(' or ')}?` : ''}`, { guide: 'procedure' });
|
||||||
for (const a of s.args) this.expr(a, scope);
|
for (const a of s.args) this.expr(a, scope);
|
||||||
return;
|
return;
|
||||||
@@ -698,8 +723,9 @@ class Checker {
|
|||||||
}
|
}
|
||||||
args.forEach((a, i) => {
|
args.forEach((a, i) => {
|
||||||
const p = sub.params[i];
|
const p = sub.params[i];
|
||||||
const t = this.expr(a, scope);
|
let t = this.expr(a, scope);
|
||||||
if (!p) return;
|
if (!p) return;
|
||||||
|
if (p.mode !== 'BYREF') t = this.adopt(a, p.type);
|
||||||
if (p.mode === 'BYREF') {
|
if (p.mode === 'BYREF') {
|
||||||
if (a.kind !== 'Name') this.error('byref-arg', a, `Parameter ${p.name} of ${sub.name} is BYREF, so the call has to pass a variable it can change, not ${sourceOf(a)}.`, { guide: 'passing-by-value-or-by-reference' });
|
if (a.kind !== 'Name') this.error('byref-arg', a, `Parameter ${p.name} of ${sub.name} is BYREF, so the call has to pass a variable it can change, not ${sourceOf(a)}.`, { guide: 'passing-by-value-or-by-reference' });
|
||||||
else {
|
else {
|
||||||
@@ -719,7 +745,7 @@ class Checker {
|
|||||||
exprStmt(s, scope) {
|
exprStmt(s, scope) {
|
||||||
const e = s.expr;
|
const e = s.expr;
|
||||||
if (e.kind === 'CallExpr') {
|
if (e.kind === 'CallExpr') {
|
||||||
const sub = this.subs.get(up(e.name));
|
const sub = e.convert ? null : this.findSub(e.name);
|
||||||
if (sub && sub.kind === 'Procedure') {
|
if (sub && sub.kind === 'Procedure') {
|
||||||
this.error('missing-call', s, `Call a PROCEDURE with CALL, on a line of its own: CALL ${sourceOf(e)}.`, { guide: 'procedure', fix: { line: s.line, col: s.col, endCol: s.col, text: 'CALL ' } });
|
this.error('missing-call', s, `Call a PROCEDURE with CALL, on a line of its own: CALL ${sourceOf(e)}.`, { guide: 'procedure', fix: { line: s.line, col: s.col, endCol: s.col, text: 'CALL ' } });
|
||||||
s.sub = sub;
|
s.sub = sub;
|
||||||
@@ -810,6 +836,9 @@ class Checker {
|
|||||||
case 'Bool': return T.BOOL;
|
case 'Bool': return T.BOOL;
|
||||||
case 'Blank': if (e.text !== '...') this.blank(e, e.text); return T.ANY;
|
case 'Blank': if (e.text !== '...') this.blank(e, e.text); return T.ANY;
|
||||||
case 'Error': return T.ANY;
|
case 'Error': return T.ANY;
|
||||||
|
case 'TypeRef':
|
||||||
|
if (!opts.typeArg) { this.error('type-as-value', e, `${e.name} is a type, and a value was expected here. To convert a number to it, write ${e.name}(X)${['INTEGER', 'FLOAT'].includes(e.name) ? ` with a sized type, or ${e.name === 'INTEGER' ? 'INT(X)' : 'TO_FLOAT(X)'}` : ''}.`, { guide: 'the-types' }); return T.ANY; }
|
||||||
|
return T[e.name] || T.ANY;
|
||||||
case 'Name': {
|
case 'Name': {
|
||||||
if (this.ctx?.constOnly) {
|
if (this.ctx?.constOnly) {
|
||||||
const sym = scope.get(e.name);
|
const sym = scope.get(e.name);
|
||||||
@@ -849,8 +878,9 @@ class Checker {
|
|||||||
case 'CallExpr': return this.call(e, scope);
|
case 'CallExpr': return this.call(e, scope);
|
||||||
case 'ListLit': {
|
case 'ListLit': {
|
||||||
let el = T.ANY;
|
let el = T.ANY;
|
||||||
|
const want = opts.expect && ['LIST', 'ARRAY'].includes(opts.expect.k) ? opts.expect.of : null;
|
||||||
for (const it of e.items) {
|
for (const it of e.items) {
|
||||||
const t = this.expr(it, scope);
|
const t = this.adopt(it, want, this.expr(it, scope));
|
||||||
if (isAny(el)) el = t;
|
if (isAny(el)) el = t;
|
||||||
else if (isNumeric(el) && isNumeric(t)) el = numericResult(el, t);
|
else if (isNumeric(el) && isNumeric(t)) el = numericResult(el, t);
|
||||||
else if (!isAny(t) && !sameType(el, t)) this.error('type', it, `Every value in a list has one type, and ${sourceOf(it)} is ${article(t)} where the others are ${typeName(el)}.`);
|
else if (!isAny(t) && !sameType(el, t)) this.error('type', it, `Every value in a list has one type, and ${sourceOf(it)} is ${article(t)} where the others are ${typeName(el)}.`);
|
||||||
@@ -920,29 +950,37 @@ class Checker {
|
|||||||
return T.BOOL;
|
return T.BOOL;
|
||||||
case '+': case '-': case '*':
|
case '+': case '-': case '*':
|
||||||
if (any) return isNumeric(lt) ? lt : isNumeric(rt) ? rt : T.ANY;
|
if (any) return isNumeric(lt) ? lt : isNumeric(rt) ? rt : T.ANY;
|
||||||
|
if (isNumeric(lt) && isNumeric(rt)) return this.mix(e, lt, rt);
|
||||||
if (op === '+' && isText(lt) && isText(rt)) { this.error('type', e, `Text is joined with &, not +: ${sourceOf(e.left)} & ${sourceOf(e.right)}.`, { guide: 'joining-and-cutting-text' }); return T.STRING; }
|
if (op === '+' && isText(lt) && isText(rt)) { this.error('type', e, `Text is joined with &, not +: ${sourceOf(e.left)} & ${sourceOf(e.right)}.`, { guide: 'joining-and-cutting-text' }); return T.STRING; }
|
||||||
if (!isNumeric(lt) || !isNumeric(rt)) { this.error('type', e, `${op} works on numbers, and ${!isNumeric(lt) ? sourceOf(e.left) : sourceOf(e.right)} is ${article(!isNumeric(lt) ? lt : rt)}.${isText(lt) && isNumeric(rt) ? ' TO_STRING turns a number into text, and & joins text.' : ''}`); return T.ANY; }
|
if (!isNumeric(lt) || !isNumeric(rt)) { this.error('type', e, `${op} works on numbers, and ${!isNumeric(lt) ? sourceOf(e.left) : sourceOf(e.right)} is ${article(!isNumeric(lt) ? lt : rt)}.${isText(lt) && isNumeric(rt) ? ' TO_STRING turns a number into text, and & joins text.' : ''}`); return T.ANY; }
|
||||||
return numericResult(lt, rt);
|
return numericResult(lt, rt);
|
||||||
case '/':
|
case '/':
|
||||||
if (!any && (!isNumeric(lt) || !isNumeric(rt))) { this.error('type', e, `/ divides numbers, and ${!isNumeric(lt) ? sourceOf(e.left) : sourceOf(e.right)} is ${article(!isNumeric(lt) ? lt : rt)}.`); }
|
if (!any && (!isNumeric(lt) || !isNumeric(rt))) { this.error('type', e, `/ divides numbers, and ${!isNumeric(lt) ? sourceOf(e.left) : sourceOf(e.right)} is ${article(!isNumeric(lt) ? lt : rt)}.`); }
|
||||||
return T.FLOAT;
|
if (!any && isNumeric(lt) && isNumeric(rt) && (isFloat32(lt) || isFloat32(rt))) { const m = this.mix(e, lt, rt); return isFloat32(m) ? m : T.FLOAT; }
|
||||||
|
return lt.bits === 64 || rt.bits === 64 ? T.FLOAT64 : T.FLOAT;
|
||||||
case 'DIV': case 'MOD':
|
case 'DIV': case 'MOD':
|
||||||
for (const [t, n] of [[lt, e.left], [rt, e.right]]) {
|
for (const [t, n] of [[lt, e.left], [rt, e.right]]) {
|
||||||
if (!isAny(t) && t.k !== 'INTEGER') this.error('type', n, `${op} works on whole numbers, and ${sourceOf(n)} is ${article(t)}.${t.k === 'FLOAT' ? ' Use / to divide FLOAT values.' : ''}`, { guide: 'arithmetic' });
|
if (!isAny(t) && t.k !== 'INTEGER') this.error('type', n, `${op} works on whole numbers, and ${sourceOf(n)} is ${article(t)}.${t.k === 'FLOAT' ? ' Use / to divide FLOAT values.' : ''}`, { guide: 'arithmetic' });
|
||||||
}
|
}
|
||||||
|
if (!any && lt.k === 'INTEGER' && rt.k === 'INTEGER') return this.mix(e, lt, rt);
|
||||||
return T.INTEGER;
|
return T.INTEGER;
|
||||||
case '^':
|
case '^':
|
||||||
if (!any && (!isNumeric(lt) || !isNumeric(rt))) this.error('type', e, '^ raises a number to a power, and both sides have to be numbers.');
|
if (!any && (!isNumeric(lt) || !isNumeric(rt))) { this.error('type', e, '^ raises a number to a power, and both sides have to be numbers.'); return T.ANY; }
|
||||||
return any ? T.ANY : numericResult(lt, rt);
|
if (any) return T.ANY;
|
||||||
|
// the power keeps the base's width: INT32 ^ 2 is INT32, whatever the exponent's type
|
||||||
|
if (lt.bits && rt.k === 'INTEGER') { this.adopt(e.right, lt.k === 'INTEGER' ? lt : null); return lt; }
|
||||||
|
return this.mix(e, lt, rt);
|
||||||
case '&':
|
case '&':
|
||||||
for (const [t, n] of [[lt, e.left], [rt, e.right]]) {
|
for (const [t, n] of [[lt, e.left], [rt, e.right]]) {
|
||||||
if (!isAny(t) && !isText(t)) this.error('type', n, `& joins text, and ${sourceOf(n)} is ${article(t)}. TO_STRING(${sourceOf(n)}) writes it as text first.`);
|
if (!isAny(t) && !isText(t)) this.error('type', n, `& joins text, and ${sourceOf(n)} is ${article(t)}. TO_STRING(${sourceOf(n)}) writes it as text first.`);
|
||||||
}
|
}
|
||||||
return T.STRING;
|
return T.STRING;
|
||||||
case '==': case '!=':
|
case '==': case '!=':
|
||||||
|
this.adoptPair(e, lt, rt);
|
||||||
this.comparable(e, lt, rt, true);
|
this.comparable(e, lt, rt, true);
|
||||||
return T.BOOL;
|
return T.BOOL;
|
||||||
case '<': case '<=': case '>': case '>=':
|
case '<': case '<=': case '>': case '>=':
|
||||||
|
this.adoptPair(e, lt, rt);
|
||||||
this.comparable(e, lt, rt, false);
|
this.comparable(e, lt, rt, false);
|
||||||
return T.BOOL;
|
return T.BOOL;
|
||||||
case 'IN': {
|
case 'IN': {
|
||||||
@@ -964,6 +1002,58 @@ class Checker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A numeric literal next to a sized value takes that value's type: X + 1 with an INT8 X is INT8 arithmetic,
|
||||||
|
* and 0.1 next to a FLOAT32 is the FLOAT32 nearest 0.1, as in Mojo. A whole-number literal that does not fit
|
||||||
|
* the type is an error here, before any run. Returns the node's type afterwards.
|
||||||
|
*/
|
||||||
|
adopt(node, want, current = node?.type) {
|
||||||
|
if (!isLiteral(node) || !want || isAny(want) || !isNumeric(want)) return current;
|
||||||
|
if (isSizedInt(want) && !node.float) {
|
||||||
|
const [lo, hi] = intRange(want);
|
||||||
|
if (node.value < lo || node.value > hi) this.error('literal-range', node, `${sourceOf(node)} does not fit ${want.name}, which holds ${intRangeText(want)}.`, { guide: 'sized-types' });
|
||||||
|
node.type = want;
|
||||||
|
return want;
|
||||||
|
}
|
||||||
|
if (isFloat32(want)) { node.type = want; return want; }
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** In a binary operation, a literal on either side adopts the other side's sized type. */
|
||||||
|
adoptPair(e, lt, rt) {
|
||||||
|
if (isLiteral(e.left) && !isLiteral(e.right)) return [this.adopt(e.left, rt, lt), rt];
|
||||||
|
if (isLiteral(e.right) && !isLiteral(e.left)) return [lt, this.adopt(e.right, lt, rt)];
|
||||||
|
return [lt, rt];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The type of +, -, *, DIV, MOD on two numbers, after literals adopt; two widths that do not mix are an error. */
|
||||||
|
mix(e, lt0, rt0) {
|
||||||
|
const [lt, rt] = this.adoptPair(e, lt0, rt0);
|
||||||
|
if (lt.k === 'INTEGER' && rt.k === 'INTEGER' && lt.bits && rt.bits && !sameType(lt, rt)) {
|
||||||
|
const [narrow, wide] = lt.bits <= rt.bits ? [e.left, rt] : [e.right, lt];
|
||||||
|
this.error('mixed-width', e, `${sourceOf(e.left)} is ${lt.name} and ${sourceOf(e.right)} is ${rt.name}, and two different sized types do not mix silently. Convert one so both sides agree, as in ${wide.name}(${sourceOf(narrow)}).`, { guide: 'sized-types' });
|
||||||
|
return T.ANY;
|
||||||
|
}
|
||||||
|
if (lt.k === 'FLOAT' && rt.k === 'FLOAT' && isFloat32(lt) !== isFloat32(rt)) {
|
||||||
|
const [s32, s64] = isFloat32(lt) ? [e.left, e.right] : [e.right, e.left];
|
||||||
|
this.error('mixed-precision', e, `${sourceOf(s32)} is FLOAT32 and ${sourceOf(s64)} is a FLOAT64, and the two precisions do not mix silently. Write FLOAT32(${sourceOf(s64)}) to round it to single precision, or FLOAT64(${sourceOf(s32)}) to widen the other side.`, { guide: 'sized-types' });
|
||||||
|
return T.ANY;
|
||||||
|
}
|
||||||
|
return numericResult(lt, rt);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** INT32(X), FLOAT32(X), ...: one number, converted to the named type. */
|
||||||
|
conversion(e, scope) {
|
||||||
|
const to = SIZED[up(e.name)];
|
||||||
|
const ts = e.args.map((a) => this.expr(a, scope));
|
||||||
|
if (ts.length !== 1) { this.error('arg-count', e, `${up(e.name)}(X) converts one number, and this call gives it ${ts.length}.`); return to; }
|
||||||
|
const t = ts[0];
|
||||||
|
if (!isAny(t) && !isNumeric(t)) this.error('type', e.args[0], `${up(e.name)}(X) converts a number, and ${sourceOf(e.args[0])} is ${article(t)}.${isText(t) ? ' TO_INTEGER or TO_FLOAT reads a number out of text first.' : ''}`, { guide: 'sized-types' });
|
||||||
|
else if (!isAny(t) && sameType(t, to) && t.name === to.name) this.note('already-type', e, `${sourceOf(e.args[0])} is already ${to.name}, so ${to.name}(...) changes nothing.`);
|
||||||
|
if (isLiteral(e.args[0]) && to.k === 'INTEGER' && !e.args[0].float) this.adopt(e.args[0], to);
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
|
||||||
comparable(e, lt, rt, equality) {
|
comparable(e, lt, rt, equality) {
|
||||||
if (isAny(lt) || isAny(rt)) return;
|
if (isAny(lt) || isAny(rt)) return;
|
||||||
if (isNumeric(lt) && isNumeric(rt)) return;
|
if (isNumeric(lt) && isNumeric(rt)) return;
|
||||||
@@ -1032,7 +1122,8 @@ class Checker {
|
|||||||
}
|
}
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
const sub = this.subs.get(name);
|
if (e.convert) return this.conversion(e, scope);
|
||||||
|
const sub = this.findSub(name);
|
||||||
if (sub) {
|
if (sub) {
|
||||||
e.sub = sub;
|
e.sub = sub;
|
||||||
if (sub.kind === 'Procedure') {
|
if (sub.kind === 'Procedure') {
|
||||||
@@ -1047,7 +1138,7 @@ class Checker {
|
|||||||
if (!b) {
|
if (!b) {
|
||||||
const sym = scope.get(e.name);
|
const sym = scope.get(e.name);
|
||||||
if (sym) { this.error('not-callable', e, `${e.name} is ${sym.kind === 'var' ? 'a variable' : 'not a subroutine'}, so it cannot be called. Square brackets reach a position: ${e.name}[I].`); return T.ANY; }
|
if (sym) { this.error('not-callable', e, `${e.name} is ${sym.kind === 'var' ? 'a variable' : 'not a subroutine'}, so it cannot be called. Square brackets reach a position: ${e.name}[I].`); return T.ANY; }
|
||||||
const s = suggest(e.name, [...this.subs.values()].map((x) => x.name).concat(Object.keys(BUILTINS)));
|
const s = suggest(e.name, [...this.subs.values(), ...this.libs.values()].map((x) => x.name).concat(Object.keys(BUILTINS)));
|
||||||
const other = FOREIGN_FUNCTIONS[name];
|
const other = FOREIGN_FUNCTIONS[name];
|
||||||
if (other) this.error('unknown-function', e, `No FUNCTION is called ${e.name}. ${other}`, { guide: 'function' });
|
if (other) this.error('unknown-function', e, `No FUNCTION is called ${e.name}. ${other}`, { guide: 'function' });
|
||||||
else this.error('unknown-function', e, `No FUNCTION is called ${e.name}.${s.length ? ` Did you mean ${s.join(' or ')}?` : ' Define it with FUNCTION ... ENDFUNCTION.'}`, { guide: 'function' });
|
else this.error('unknown-function', e, `No FUNCTION is called ${e.name}.${s.length ? ` Did you mean ${s.join(' or ')}?` : ' Define it with FUNCTION ... ENDFUNCTION.'}`, { guide: 'function' });
|
||||||
@@ -1060,7 +1151,7 @@ class Checker {
|
|||||||
return T.VOID;
|
return T.VOID;
|
||||||
}
|
}
|
||||||
e.builtin = name;
|
e.builtin = name;
|
||||||
const ts = e.args.map((a) => this.expr(a, scope));
|
const ts = e.args.map((a, i) => this.expr(a, scope, { typeArg: TYPE_ARGS[name] === i }));
|
||||||
const argc = (n) => {
|
const argc = (n) => {
|
||||||
if (ts.length !== n) { this.error('arg-count', e, `${b.sig} takes ${n} ${n === 1 ? 'value' : 'values'}, and this call gives it ${ts.length}.`); return false; }
|
if (ts.length !== n) { this.error('arg-count', e, `${b.sig} takes ${n} ${n === 1 ? 'value' : 'values'}, and this call gives it ${ts.length}.`); return false; }
|
||||||
return true;
|
return true;
|
||||||
@@ -1181,13 +1272,84 @@ class Checker {
|
|||||||
case 'SORTED':
|
case 'SORTED':
|
||||||
if (argc(1) && !isAny(ts[0]) && !['LIST', 'SET', 'ARRAY'].includes(ts[0].k)) { this.error('type', e.args[0], `SORTED puts the values of a LIST or SET in order, and ${sourceOf(e.args[0])} is ${article(ts[0])}.`); return T.list(T.ANY); }
|
if (argc(1) && !isAny(ts[0]) && !['LIST', 'SET', 'ARRAY'].includes(ts[0].k)) { this.error('type', e.args[0], `SORTED puts the values of a LIST or SET in order, and ${sourceOf(e.args[0])} is ${article(ts[0])}.`); return T.list(T.ANY); }
|
||||||
return T.list(isAny(ts[0]) ? T.ANY : ts[0].of);
|
return T.list(isAny(ts[0]) ? T.ANY : ts[0].of);
|
||||||
|
// ---- numerical work
|
||||||
|
case 'ASIN': case 'ACOS': case 'ATAN': case 'SINH': case 'COSH': case 'TANH': case 'CBRT': case 'EXPM1': case 'LOG1P':
|
||||||
|
if (argc(1)) num(0);
|
||||||
|
return T.FLOAT;
|
||||||
|
case 'HYPOT': case 'POW': case 'COPYSIGN': if (argc(2)) { num(0); num(1); } return T.FLOAT;
|
||||||
|
case 'E': case 'INF': case 'NAN': argc(0); return T.FLOAT;
|
||||||
|
case 'TRUNC': if (argc(1)) { num(0); return ts[0].k === 'FLOAT' ? ts[0] : T.FLOAT; } return T.FLOAT;
|
||||||
|
case 'SIGN': if (argc(1)) return isAny(num(0)) ? T.ANY : ts[0]; return T.ANY;
|
||||||
|
case 'CLAMP': {
|
||||||
|
if (!argc(3)) return T.ANY;
|
||||||
|
ts.forEach((_, i) => num(i));
|
||||||
|
if (ts.some((x) => isAny(x))) return T.ANY;
|
||||||
|
for (const i of [1, 2]) this.adopt(e.args[i], ts[0]);
|
||||||
|
return ts[0];
|
||||||
|
}
|
||||||
|
case 'FMA': {
|
||||||
|
if (!argc(3)) return T.FLOAT;
|
||||||
|
ts.forEach((_, i) => num(i));
|
||||||
|
const f32 = ts.some((x) => isFloat32(x));
|
||||||
|
if (f32) e.args.forEach((a) => this.adopt(a, T.FLOAT32));
|
||||||
|
return f32 && e.args.every((a) => isFloat32(a.type) || a.type?.k === 'INTEGER') ? T.FLOAT32 : T.FLOAT;
|
||||||
|
}
|
||||||
|
case 'IS_NAN': case 'IS_INF': case 'IS_FINITE': if (argc(1)) num(0); return T.BOOL;
|
||||||
|
case 'IS_CLOSE': if (argc(4)) ts.forEach((_, i) => num(i)); return T.BOOL;
|
||||||
|
case 'ULP': case 'NEXT_AFTER': {
|
||||||
|
if (!argc(name === 'ULP' ? 1 : 2)) return T.FLOAT;
|
||||||
|
ts.forEach((_, i) => num(i));
|
||||||
|
return isFloat32(ts[0]) ? ts[0] : T.FLOAT;
|
||||||
|
}
|
||||||
|
case 'EXACT': if (argc(1)) num(0); return T.STRING;
|
||||||
|
case 'MAX_VALUE': case 'MIN_VALUE': case 'EPSILON': {
|
||||||
|
if (!argc(1)) return T.ANY;
|
||||||
|
const a = e.args[0];
|
||||||
|
if (a.kind !== 'TypeRef') { this.error('type', a, `${name} takes the name of a numeric type, as in ${name}(${name === 'EPSILON' ? 'FLOAT64' : 'INT32'}).`, { guide: 'sized-types' }); return T.ANY; }
|
||||||
|
const ty = T[a.name];
|
||||||
|
if (name === 'EPSILON' && ty.k !== 'FLOAT') { this.error('type', a, `EPSILON measures the spacing of FLOAT values near 1.0; ${a.name} has none. For whole numbers the step is always 1.`, { guide: 'sized-types' }); return T.ANY; }
|
||||||
|
if (ty.k === 'INTEGER' && !ty.bits) { this.error('type', a, `INTEGER has no ${name === 'MAX_VALUE' ? 'largest' : 'lowest'} value: it is a whole number of any size. Name a sized type, such as INT32.`, { guide: 'sized-types' }); return T.INTEGER; }
|
||||||
|
if (ty.k === 'INTEGER' && ty.bits === 64 && (name === 'MAX_VALUE' || ty.signed)) { this.error('exact-limit', a, `${name}(${ty.name}) is ${name === 'MAX_VALUE' ? (ty.signed ? '9,223,372,036,854,775,807' : '18,446,744,073,709,551,615') : '-9,223,372,036,854,775,808'}, more than this lab holds exactly (9,007,199,254,740,991). Name a type up to 32 bits, or write the limit you need as a constant.`, { guide: 'sized-types' }); return ty; }
|
||||||
|
return ty.k === 'FLOAT' && !ty.bits ? T.FLOAT : ty;
|
||||||
|
}
|
||||||
|
case 'GCD': case 'LCM': case 'BIT_AND': case 'BIT_OR': case 'BIT_XOR': {
|
||||||
|
if (!argc(2)) return T.INTEGER;
|
||||||
|
int(0); int(1);
|
||||||
|
if (isAny(ts[0]) || isAny(ts[1])) return T.INTEGER;
|
||||||
|
return this.mix({ ...e, left: e.args[0], right: e.args[1], op: name }, ts[0], ts[1]);
|
||||||
|
}
|
||||||
|
case 'BIT_NOT': if (argc(1)) { int(0); return ts[0].k === 'INTEGER' ? ts[0] : T.INTEGER; } return T.INTEGER;
|
||||||
|
case 'SHIFT_LEFT': case 'SHIFT_RIGHT':
|
||||||
|
if (argc(2)) { int(0); int(1); return ts[0].k === 'INTEGER' ? ts[0] : T.INTEGER; }
|
||||||
|
return T.INTEGER;
|
||||||
|
case 'WRAP': {
|
||||||
|
if (!argc(2)) return T.ANY;
|
||||||
|
int(0);
|
||||||
|
// the arithmetic inside WRAP is exact (INTEGER), so that WRAP(Count + 1, UINT8) wraps instead of stopping at Count + 1
|
||||||
|
const exact = (n) => {
|
||||||
|
if (!n || typeof n !== 'object') return;
|
||||||
|
if ((n.kind === 'Binary' || n.kind === 'Unary' || n.kind === 'Num') && isSizedInt(n.type)) n.type = T.INTEGER;
|
||||||
|
if (n.kind === 'Binary') { exact(n.left); exact(n.right); } else if (n.kind === 'Unary') exact(n.arg);
|
||||||
|
};
|
||||||
|
exact(e.args[0]);
|
||||||
|
const a = e.args[1];
|
||||||
|
if (a.kind !== 'TypeRef' || !isSizedInt(T[a.name])) { this.error('type', a, 'WRAP takes a sized whole-number type as its second value, as in WRAP(X, UINT8).', { guide: 'sized-types' }); return T.ANY; }
|
||||||
|
const ty = T[a.name];
|
||||||
|
if (ty.bits > 32) { this.error('wrap-64', a, `WRAP works on types up to 32 bits in this lab, which holds whole numbers exactly only to 2^53 - 1; ${ty.name} wraps at 2^64.`, { guide: 'sized-types' }); return ty; }
|
||||||
|
return ty;
|
||||||
|
}
|
||||||
default: return T.ANY;
|
default: return T.ANY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function check(parsed) {
|
/**
|
||||||
const c = new Checker(parsed);
|
* @param {any} parsed
|
||||||
|
* @param {{ libs?: Map<string, any>, libOrigin?: Map<string, { module: string, line: number }> }} [options]
|
||||||
|
* libs: routines brought in with USE, already checked in their own files (UPPER name -> routine)
|
||||||
|
*/
|
||||||
|
export function check(parsed, options = {}) {
|
||||||
|
const c = new Checker(parsed, options);
|
||||||
const program = c.run();
|
const program = c.run();
|
||||||
program.source = parsed.source;
|
program.source = parsed.source;
|
||||||
program.lines = parsed.lines;
|
program.lines = parsed.lines;
|
||||||
|
|||||||
@@ -355,7 +355,7 @@ function exprChecks(e, say) {
|
|||||||
say('note', 'bool-compare', x, `${sourceOf(other)} already holds TRUE or FALSE, so comparing it with ${lit.value ? 'TRUE' : 'FALSE'} asks the same question twice. Write ${positive ? sourceOf(other) : `NOT ${sourceOf(other)}`}.`, { guide: 'if' });
|
say('note', 'bool-compare', x, `${sourceOf(other)} already holds TRUE or FALSE, so comparing it with ${lit.value ? 'TRUE' : 'FALSE'} asks the same question twice. Write ${positive ? sourceOf(other) : `NOT ${sourceOf(other)}`}.`, { guide: 'if' });
|
||||||
}
|
}
|
||||||
if ((a.type?.k === 'FLOAT' || b.type?.k === 'FLOAT') && isNumeric(a.type) && isNumeric(b.type)) {
|
if ((a.type?.k === 'FLOAT' || b.type?.k === 'FLOAT') && isNumeric(a.type) && isNumeric(b.type)) {
|
||||||
say('warning', 'float-equal', x, `${sourceOf(x)} compares FLOAT values with ${x.op}. Two measurements that should be equal often differ in the last digit, so compare the difference against a tolerance: ABS(${sourceOf(a)} - ${sourceOf(b)}) < 0.0001.`, { guide: 'comparison' });
|
say('warning', 'float-equal', x, `${sourceOf(x)} compares FLOAT values with ${x.op}. Two measurements that should be equal often differ in the last digit, so compare the difference against a tolerance: ABS(${sourceOf(a)} - ${sourceOf(b)}) < 0.0001, or IS_CLOSE(${sourceOf(a)}, ${sourceOf(b)}, 1e-9, 0.0) to say the tolerance as a fraction of the values.`, { guide: 'comparison' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (x.kind === 'Unary' && x.op === 'NOT' && !x.arg.paren && (x.arg.kind === 'Binary' && ['==', '!=', '<', '<=', '>', '>='].includes(x.arg.op))) {
|
if (x.kind === 'Unary' && x.op === 'NOT' && !x.arg.paren && (x.arg.kind === 'Binary' && ['==', '!=', '<', '<=', '>', '>='].includes(x.arg.op))) {
|
||||||
|
|||||||
+191
-24
@@ -29,8 +29,10 @@
|
|||||||
import {
|
import {
|
||||||
T, UNSET, PArray, PRecord, cloneValue, chars, textLength, sortedKeys, compareValues,
|
T, UNSET, PArray, PRecord, cloneValue, chars, textLength, sortedKeys, compareValues,
|
||||||
valuesEqual, formatValue, outputText, typeName, isNumeric,
|
valuesEqual, formatValue, outputText, typeName, isNumeric,
|
||||||
|
SIZED, isSizedInt, isFloat32, intRange, intRangeText, EXACT_MAX,
|
||||||
} from './lang.js';
|
} from './lang.js';
|
||||||
import { sourceOf } from './parse.js';
|
import { sourceOf } from './parse.js';
|
||||||
|
import { fma, exactDecimal, nextAfter, ulp, copySign, gcd, bitOp, wrap } from './numeric.js';
|
||||||
|
|
||||||
/* ------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------ */
|
||||||
/* Control flow and errors */
|
/* Control flow and errors */
|
||||||
@@ -110,8 +112,8 @@ PArray.prototype.fillWith = function fillWith(of, interp, at) {
|
|||||||
export function valueFits(type, v) {
|
export function valueFits(type, v) {
|
||||||
if (!type || type.k === 'ANY') return true;
|
if (!type || type.k === 'ANY') return true;
|
||||||
switch (type.k) {
|
switch (type.k) {
|
||||||
case 'INTEGER': return Number.isInteger(v);
|
case 'INTEGER': { if (!Number.isInteger(v)) return false; const [lo, hi] = intRange(type); return v >= lo && v <= hi; }
|
||||||
case 'FLOAT': return typeof v === 'number';
|
case 'FLOAT': return typeof v === 'number' && (!isFloat32(type) || Math.fround(v) === v || !Number.isFinite(v));
|
||||||
case 'CHAR': return typeof v === 'string' && textLength(v) === 1;
|
case 'CHAR': return typeof v === 'string' && textLength(v) === 1;
|
||||||
case 'STRING': return typeof v === 'string';
|
case 'STRING': return typeof v === 'string';
|
||||||
case 'BOOL': return typeof v === 'boolean';
|
case 'BOOL': return typeof v === 'boolean';
|
||||||
@@ -162,6 +164,8 @@ class Interpreter {
|
|||||||
this.lastSnapshot = null;
|
this.lastSnapshot = null;
|
||||||
this.loopStack = [];
|
this.loopStack = [];
|
||||||
this.quiet = 0; // >0 while evaluating contracts: their reads are not the program's
|
this.quiet = 0; // >0 while evaluating contracts: their reads are not the program's
|
||||||
|
this.ieee = !!program.ieee; // USE IEEE: NaN and infinities flow instead of stopping the run
|
||||||
|
this.inLib = 0; // >0 while a library routine runs without being stepped into
|
||||||
}
|
}
|
||||||
|
|
||||||
get frame() { return this.frames[this.frames.length - 1]; }
|
get frame() { return this.frames[this.frames.length - 1]; }
|
||||||
@@ -188,6 +192,13 @@ class Interpreter {
|
|||||||
/** One recorded step. `node` places it on a line. */
|
/** One recorded step. `node` places it on a line. */
|
||||||
step(node, kind, text, extra = {}) {
|
step(node, kind, text, extra = {}) {
|
||||||
this.work.steps++;
|
this.work.steps++;
|
||||||
|
if (this.inLib) {
|
||||||
|
// inside a library routine the run is not stepped into: its lines are not this program's
|
||||||
|
if (this.work.steps > this.opts.maxSteps) throw this.limitError(node);
|
||||||
|
this.reads = [];
|
||||||
|
this.writes = [];
|
||||||
|
return;
|
||||||
|
}
|
||||||
const line = extra.line ?? node?.line ?? null;
|
const line = extra.line ?? node?.line ?? null;
|
||||||
if (line != null) this.hits.set(line, (this.hits.get(line) || 0) + 1);
|
if (line != null) this.hits.set(line, (this.hits.get(line) || 0) + 1);
|
||||||
if (this.work.steps > this.opts.maxSteps) throw this.limitError(node);
|
if (this.work.steps > this.opts.maxSteps) throw this.limitError(node);
|
||||||
@@ -423,8 +434,47 @@ class Interpreter {
|
|||||||
this.noteWrite(root.name, targetPath(target, key));
|
this.noteWrite(root.name, targetPath(target, key));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A whole number stored under a FLOAT name stays a number; nothing to convert. */
|
/**
|
||||||
fitNumber(type, v) { return v; }
|
* A value going into a name, a position, a parameter or a RETURN of a sized type: a whole number that does not
|
||||||
|
* fit stops the run, and a number going into a FLOAT32 is rounded to the nearest single. Collections of sized
|
||||||
|
* numbers are checked value by value. Everything else is stored as it is.
|
||||||
|
*/
|
||||||
|
fitNumber(type, v, at) {
|
||||||
|
if (!type) return v;
|
||||||
|
if (typeof v === 'number') {
|
||||||
|
if (isSizedInt(type)) return this.fitInt(type, v, at, 'store');
|
||||||
|
if (isFloat32(type)) return this.finiteCheck(Math.fround(v), at);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
if ((type.k === 'LIST' || type.k === 'ARRAY') && type.of && (isSizedInt(type.of) || isFloat32(type.of))) {
|
||||||
|
if (Array.isArray(v)) return v.map((x) => (x === UNSET ? x : this.fitNumber(type.of, x, at)));
|
||||||
|
if (v instanceof PArray) { v.data = v.data.map((x) => (x === UNSET ? x : this.fitNumber(type.of, x, at))); return v; }
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A whole number checked against a sized type. `how` is 'store' (a value put into a name or position),
|
||||||
|
* 'result' (what an operator computed) or 'convert' (INT32(X) and its kind).
|
||||||
|
*/
|
||||||
|
fitInt(type, v, at, how) {
|
||||||
|
if (!Number.isFinite(v) || !Number.isInteger(v)) throw new PseudoError('value', `${formatValue(v)} is not a whole number, and ${type.name} holds whole numbers only.`, at);
|
||||||
|
const [lo, hi] = intRange(type);
|
||||||
|
if (v >= lo && v <= hi) return v;
|
||||||
|
const big = Math.abs(v) > EXACT_MAX ? ' (past what this lab holds exactly)' : '';
|
||||||
|
const what = how === 'result' ? `${sourceOf(at)} comes to ${formatValue(v)}${big}`
|
||||||
|
: how === 'convert' ? `${sourceOf(at)} gets ${formatValue(v)}${big}`
|
||||||
|
: `${at?.target ? sourceOf(at.target) : 'The value'} would receive ${formatValue(v)}${big}`;
|
||||||
|
throw new PseudoError('overflow', `${what}, and ${type.name} holds ${intRangeText(type)}. A sized type stops the run instead of wrapping around; write WRAP(..., ${type.name}) when wrapping is what you mean, or use a wider type.`, at, { type: type.name });
|
||||||
|
}
|
||||||
|
|
||||||
|
/** What an arithmetic operator's result becomes in its type: checked for a sized whole number, rounded for FLOAT32. */
|
||||||
|
sized(e, v) {
|
||||||
|
const t = e.type;
|
||||||
|
if (isSizedInt(t)) return this.fitInt(t, v, e, 'result');
|
||||||
|
if (isFloat32(t)) return this.finiteCheck(Math.fround(v), e);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
/** The container a target writes into, and the key within it. */
|
/** The container a target writes into, and the key within it. */
|
||||||
locate(target, at) {
|
locate(target, at) {
|
||||||
@@ -784,6 +834,7 @@ class Interpreter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
invoke(sub, argSlots, at, entry) {
|
invoke(sub, argSlots, at, entry) {
|
||||||
|
if (sub.lib && !entry && !this.opts.stepIntoLibrary && !this.inLib) return this.invokeLibrary(sub, argSlots, at);
|
||||||
if (this.frames.length > this.opts.maxDepth) throw new PseudoError('depth', `${sub.name} has been called ${this.opts.maxDepth} levels deep without returning. A recursive subroutine needs a case that stops calling itself, and every call has to move toward it.`, at);
|
if (this.frames.length > this.opts.maxDepth) throw new PseudoError('depth', `${sub.name} has been called ${this.opts.maxDepth} levels deep without returning. A recursive subroutine needs a case that stops calling itself, and every call has to move toward it.`, at);
|
||||||
this.work.calls++;
|
this.work.calls++;
|
||||||
const caller = this.frame;
|
const caller = this.frame;
|
||||||
@@ -791,7 +842,7 @@ class Interpreter {
|
|||||||
sub.params.forEach((p, i) => {
|
sub.params.forEach((p, i) => {
|
||||||
const a = argSlots[i];
|
const a = argSlots[i];
|
||||||
if (a.byref) { f.vars.set(up(p.name), a.byref); f.dirty.add(up(p.name)); }
|
if (a.byref) { f.vars.set(up(p.name), a.byref); f.dirty.add(up(p.name)); }
|
||||||
else f.vars.set(up(p.name), { name: p.name, type: p.type, value: a.value, line: sub.node.line, kind: 'param' });
|
else f.vars.set(up(p.name), { name: p.name, type: p.type, value: this.fitNumber(p.type, a.value, { line: at.line, col: at.col, target: { kind: 'Name', name: `Parameter ${p.name} of ${sub.name}` } }), line: sub.node.line, kind: 'param' });
|
||||||
});
|
});
|
||||||
const shown = sub.params.map((p) => `${p.name} = ${formatValue(f.vars.get(up(p.name)).value, p.type, { bareChar: true, max: 10 })}`).join(', ');
|
const shown = sub.params.map((p) => `${p.name} = ${formatValue(f.vars.get(up(p.name)).value, p.type, { bareChar: true, max: 10 })}`).join(', ');
|
||||||
this.frames.push(f);
|
this.frames.push(f);
|
||||||
@@ -818,7 +869,7 @@ class Interpreter {
|
|||||||
this.step({ line: sub.node.endLine }, 'return', `${sub.name} ends and control goes back to line ${at.line}.`);
|
this.step({ line: sub.node.endLine }, 'return', `${sub.name} ends and control goes back to line ${at.line}.`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (!(e instanceof ReturnSignal)) throw e;
|
if (!(e instanceof ReturnSignal)) throw e;
|
||||||
result = e.value;
|
result = sub.returns ? this.fitNumber(sub.returns, e.value, { line: e.line, col: 1, target: { kind: 'Name', name: `the RESULT of ${sub.name}` } }) : e.value;
|
||||||
}
|
}
|
||||||
if (this.opts.contracts && sub.ensures.length) {
|
if (this.opts.contracts && sub.ensures.length) {
|
||||||
for (const en of sub.ensures) {
|
for (const en of sub.ensures) {
|
||||||
@@ -838,6 +889,32 @@ class Interpreter {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A routine from the library, run without stepping into it: one step where it is called, one where it hands
|
||||||
|
* back, and nothing of its own lines in this program's trace (they belong to another file). Its contracts are
|
||||||
|
* still checked; a failure is reported at the call, naming the routine.
|
||||||
|
*/
|
||||||
|
invokeLibrary(sub, argSlots, at) {
|
||||||
|
const origin = sub.lib ? `${sub.lib.module}.${sub.name}` : sub.name;
|
||||||
|
const shown = sub.params.map((p, i) => `${p.name} = ${formatValue(argSlots[i].byref ? argSlots[i].byref.value : argSlots[i].value, p.type, { bareChar: true, max: 10 })}`).join(', ');
|
||||||
|
this.step(at, 'call', `${sub.name} from the library (${origin}) runs with ${shown || 'no values'}; open it in the library to step through it.`, { callee: sub.name, library: origin });
|
||||||
|
this.inLib++;
|
||||||
|
let result;
|
||||||
|
try {
|
||||||
|
result = this.invoke(sub, argSlots, at, false);
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof PseudoError) {
|
||||||
|
const where = e.line != null ? ` (line ${e.line} of ${origin})` : '';
|
||||||
|
throw new PseudoError(e.kind, `In the library routine ${origin}${where}: ${e.message}`, at, { library: origin, libraryLine: e.line });
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
this.inLib--;
|
||||||
|
}
|
||||||
|
if (sub.kind === 'Function') this.step(at, 'return', `${origin} hands back ${formatValue(result, sub.returns)}.`, { value: result, library: origin });
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Evaluate an ENSURE: RESULT is the returned value, and a by-value
|
* Evaluate an ENSURE: RESULT is the returned value, and a by-value
|
||||||
* parameter means the value it was called with, even if the body changed
|
* parameter means the value it was called with, even if the body changed
|
||||||
@@ -919,7 +996,7 @@ class Interpreter {
|
|||||||
/* ---------- expressions ---------- */
|
/* ---------- expressions ---------- */
|
||||||
evaluate(e) {
|
evaluate(e) {
|
||||||
switch (e.kind) {
|
switch (e.kind) {
|
||||||
case 'Num': return this.finiteCheck(e.value, e);
|
case 'Num': return this.finiteCheck(isFloat32(e.type) ? Math.fround(e.value) : e.value, e);
|
||||||
case 'Str': case 'Char': case 'Bool': return e.value;
|
case 'Str': case 'Char': case 'Bool': return e.value;
|
||||||
case 'Name': {
|
case 'Name': {
|
||||||
if (up(e.name) === 'RESULT' && e.sym?.kind === 'result') {
|
if (up(e.name) === 'RESULT' && e.sym?.kind === 'result') {
|
||||||
@@ -933,7 +1010,7 @@ class Interpreter {
|
|||||||
case 'Unary': {
|
case 'Unary': {
|
||||||
const v = this.evaluate(e.arg);
|
const v = this.evaluate(e.arg);
|
||||||
if (e.op === 'NOT') return !v;
|
if (e.op === 'NOT') return !v;
|
||||||
return this.intCheck(-v, e);
|
return this.sized(e, this.intCheck(-v, e));
|
||||||
}
|
}
|
||||||
case 'Binary': return this.binary(e);
|
case 'Binary': return this.binary(e);
|
||||||
case 'Chain': {
|
case 'Chain': {
|
||||||
@@ -994,7 +1071,7 @@ class Interpreter {
|
|||||||
* run inside what a proof covers (tests/prover/float-overflow.pseudo).
|
* run inside what a proof covers (tests/prover/float-overflow.pseudo).
|
||||||
*/
|
*/
|
||||||
finiteCheck(v, e) {
|
finiteCheck(v, e) {
|
||||||
if (typeof v === 'number' && !Number.isFinite(v)) {
|
if (typeof v === 'number' && !Number.isFinite(v) && !this.ieee) {
|
||||||
throw new PseudoError('overflow', Number.isNaN(v)
|
throw new PseudoError('overflow', Number.isNaN(v)
|
||||||
? `${sourceOf(e)} has no number as its answer: the calculation went past the largest FLOAT this lab can hold, about 1.8 × 10^308, along the way.`
|
? `${sourceOf(e)} has no number as its answer: the calculation went past the largest FLOAT this lab can hold, about 1.8 × 10^308, along the way.`
|
||||||
: `${sourceOf(e)} comes to ${v > 0 ? 'more than' : 'less than minus'} about 1.8 × 10^308, beyond the largest FLOAT this lab can hold.`, e);
|
: `${sourceOf(e)} comes to ${v > 0 ? 'more than' : 'less than minus'} about 1.8 × 10^308, beyond the largest FLOAT this lab can hold.`, e);
|
||||||
@@ -1006,24 +1083,26 @@ class Interpreter {
|
|||||||
const op = e.op;
|
const op = e.op;
|
||||||
if (op === 'AND') { const l = this.evaluate(e.left); return l ? Boolean(this.evaluate(e.right)) : false; }
|
if (op === 'AND') { const l = this.evaluate(e.left); return l ? Boolean(this.evaluate(e.right)) : false; }
|
||||||
if (op === 'OR') { const l = this.evaluate(e.left); return l ? true : Boolean(this.evaluate(e.right)); }
|
if (op === 'OR') { const l = this.evaluate(e.left); return l ? true : Boolean(this.evaluate(e.right)); }
|
||||||
const l = this.evaluate(e.left);
|
let l = this.evaluate(e.left);
|
||||||
const r = this.evaluate(e.right);
|
let r = this.evaluate(e.right);
|
||||||
|
// FLOAT32 arithmetic: both sides are singles (a whole number is converted first), and the result is rounded
|
||||||
|
if (isFloat32(e.type) && ['+', '-', '*', '/', '^'].includes(op)) { l = Math.fround(l); r = Math.fround(r); }
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case '+': return this.intCheck(l + r, e);
|
case '+': return this.sized(e, this.intCheck(l + r, e));
|
||||||
case '-': return this.intCheck(l - r, e);
|
case '-': return this.sized(e, this.intCheck(l - r, e));
|
||||||
case '*': return this.intCheck(l * r, e);
|
case '*': return this.sized(e, this.intCheck(l * r, e));
|
||||||
case '/':
|
case '/':
|
||||||
if (r === 0) throw this.zeroError(e);
|
if (r === 0 && !(this.ieee && e.type?.k === 'FLOAT')) throw this.zeroError(e);
|
||||||
return this.finiteCheck(l / r, e);
|
return this.sized(e, this.finiteCheck(l / r, e));
|
||||||
case 'DIV':
|
case 'DIV':
|
||||||
if (r === 0) throw this.zeroError(e);
|
if (r === 0) throw this.zeroError(e);
|
||||||
return Math.floor(l / r);
|
return this.sized(e, Math.floor(l / r));
|
||||||
case 'MOD':
|
case 'MOD':
|
||||||
if (r === 0) throw this.zeroError(e);
|
if (r === 0) throw this.zeroError(e);
|
||||||
return l - r * Math.floor(l / r);
|
return this.sized(e, l - r * Math.floor(l / r));
|
||||||
case '^': {
|
case '^': {
|
||||||
if (e.type?.k === 'INTEGER' && r < 0) throw new PseudoError('value', `${sourceOf(e)} raises a whole number to the power ${r}, which is not whole. Write ${sourceOf(e.left)} as a FLOAT, such as ${l}.0, for a fractional answer.`, e);
|
if (e.type?.k === 'INTEGER' && r < 0) throw new PseudoError('value', `${sourceOf(e)} raises a whole number to the power ${r}, which is not whole. Write ${sourceOf(e.left)} as a FLOAT, such as ${l}.0, for a fractional answer.`, e);
|
||||||
return this.intCheck(l ** r, e);
|
return this.sized(e, this.intCheck(l ** r, e));
|
||||||
}
|
}
|
||||||
case '&': return String(l) + String(r);
|
case '&': return String(l) + String(r);
|
||||||
default: return this.compare(op, l, r, e, e.left, e.right);
|
default: return this.compare(op, l, r, e, e.left, e.right);
|
||||||
@@ -1147,18 +1226,33 @@ class Interpreter {
|
|||||||
const slots = this.argSlots(sub, e.args);
|
const slots = this.argSlots(sub, e.args);
|
||||||
return this.invoke(sub, slots, e, false);
|
return this.invoke(sub, slots, e, false);
|
||||||
}
|
}
|
||||||
const args = e.args.map((a) => this.evaluate(a));
|
if (e.convert) return this.convert(e, this.evaluate(e.args[0]));
|
||||||
|
const args = e.args.map((a) => (a.kind === 'TypeRef' ? T[a.name] : this.evaluate(a)));
|
||||||
return this.finiteCheck(this.builtin(up(e.name), args, e), e);
|
return this.finiteCheck(this.builtin(up(e.name), args, e), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** INT32(X), FLOAT32(X), ...: a FLOAT loses its fraction toward zero for a whole-number type; out of range stops. */
|
||||||
|
convert(e, v) {
|
||||||
|
const to = SIZED[up(e.name)];
|
||||||
|
if (to.k === 'INTEGER') {
|
||||||
|
if (!Number.isFinite(v)) throw new PseudoError('value', `${sourceOf(e)} has no answer: ${formatValue(v)} is not a number that has a whole part.`, e);
|
||||||
|
return this.fitInt(to, Math.trunc(v) || 0, e, 'convert');
|
||||||
|
}
|
||||||
|
return isFloat32(to) ? this.finiteCheck(Math.fround(v), e) : v;
|
||||||
|
}
|
||||||
|
|
||||||
/** A whole-number result that must still fit exactly. */
|
/** A whole-number result that must still fit exactly. */
|
||||||
checkInt(v, e) {
|
checkInt(v, e) {
|
||||||
if (!Number.isSafeInteger(v)) throw new PseudoError('overflow', `${sourceOf(e)} comes to more than 9,007,199,254,740,991, the largest whole number this lab can hold exactly.`, e);
|
if (!Number.isSafeInteger(v)) throw new PseudoError('overflow', `${sourceOf(e)} comes to more than 9,007,199,254,740,991, the largest whole number this lab can hold exactly.`, e);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** A whole-number result checked against its type: a sized type's range, or what INTEGER holds exactly. */
|
||||||
|
intResult(e, v) { return isSizedInt(e.type) ? this.fitInt(e.type, v, e, 'result') : this.checkInt(v, e); }
|
||||||
|
|
||||||
builtin(name, a, e) {
|
builtin(name, a, e) {
|
||||||
const [x, y, z] = a;
|
const [x, y, z] = a;
|
||||||
|
const a4 = () => a[3];
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case 'LENGTH':
|
case 'LENGTH':
|
||||||
if (typeof x === 'string') return textLength(x);
|
if (typeof x === 'string') return textLength(x);
|
||||||
@@ -1177,10 +1271,10 @@ class Interpreter {
|
|||||||
if (!list.length) throw new PseudoError('value', `${name} of an empty collection has no answer.`, e);
|
if (!list.length) throw new PseudoError('value', `${name} of an empty collection has no answer.`, e);
|
||||||
return list.reduce((m, v) => ((name === 'MIN' ? compareValues(v, m) < 0 : compareValues(v, m) > 0) ? v : m));
|
return list.reduce((m, v) => ((name === 'MIN' ? compareValues(v, m) < 0 : compareValues(v, m) > 0) ? v : m));
|
||||||
}
|
}
|
||||||
case 'SQRT': if (x < 0) throw new PseudoError('value', `SQRT(${formatValue(x)}) has no real answer: the number is negative.`, e); return Math.sqrt(x);
|
case 'SQRT': if (x < 0 && !this.ieee) throw new PseudoError('value', `SQRT(${formatValue(x)}) has no real answer: the number is negative.`, e); return Math.sqrt(x);
|
||||||
case 'EXP': return Math.exp(x);
|
case 'EXP': return Math.exp(x);
|
||||||
case 'LOG': case 'LOG2':
|
case 'LOG': case 'LOG2':
|
||||||
if (x <= 0) throw new PseudoError('value', `${name}(${formatValue(x)}) has no answer: a logarithm needs a number above 0.${x === 0 ? ' A probability of 0 often means a pseudocount is missing.' : ''}`, e);
|
if (x <= 0 && !this.ieee) throw new PseudoError('value', `${name}(${formatValue(x)}) has no answer: a logarithm needs a number above 0.${x === 0 ? ' A probability of 0 often means a pseudocount is missing.' : ''}`, e);
|
||||||
return name === 'LOG' ? Math.log(x) : Math.log2(x);
|
return name === 'LOG' ? Math.log(x) : Math.log2(x);
|
||||||
case 'RAND': return this.random() * x;
|
case 'RAND': return this.random() * x;
|
||||||
case 'RANDINT':
|
case 'RANDINT':
|
||||||
@@ -1231,7 +1325,7 @@ class Interpreter {
|
|||||||
case 'FLOOR': return Math.floor(x);
|
case 'FLOOR': return Math.floor(x);
|
||||||
case 'CEIL': return Math.ceil(x);
|
case 'CEIL': return Math.ceil(x);
|
||||||
case 'LOG10':
|
case 'LOG10':
|
||||||
if (x <= 0) throw new PseudoError('value', `LOG10(${formatValue(x)}) has no answer: a logarithm needs a number above 0.`, e);
|
if (x <= 0 && !this.ieee) throw new PseudoError('value', `LOG10(${formatValue(x)}) has no answer: a logarithm needs a number above 0.`, e);
|
||||||
return Math.log10(x);
|
return Math.log10(x);
|
||||||
case 'SIN': return Math.sin(x);
|
case 'SIN': return Math.sin(x);
|
||||||
case 'COS': return Math.cos(x);
|
case 'COS': return Math.cos(x);
|
||||||
@@ -1251,7 +1345,14 @@ class Interpreter {
|
|||||||
const xs = x instanceof PArray ? x.data : x;
|
const xs = x instanceof PArray ? x.data : x;
|
||||||
for (const v of xs) if (v === UNSET) throw new PseudoError('unset', `${name}(${sourceOf(e.args[0])}): a position of ${sourceOf(e.args[0])} has not been given a value yet.`, e);
|
for (const v of xs) if (v === UNSET) throw new PseudoError('unset', `${name}(${sourceOf(e.args[0])}): a position of ${sourceOf(e.args[0])} has not been given a value yet.`, e);
|
||||||
const n = xs.length;
|
const n = xs.length;
|
||||||
const isFloat = e.args[0].type?.of?.k === 'FLOAT' || xs.some((v) => !Number.isInteger(v));
|
const el = e.args[0].type?.of;
|
||||||
|
const isFloat = el?.k === 'FLOAT' || xs.some((v) => !Number.isInteger(v));
|
||||||
|
if (name === 'SUM_OF' && (isFloat32(el) || isSizedInt(el))) {
|
||||||
|
// a running total kept in the values' own type, as a loop with a total of that type would keep it
|
||||||
|
let t = 0;
|
||||||
|
for (const v of xs) t = isFloat32(el) ? Math.fround(t + v) : this.fitInt(el, t + v, e, 'result');
|
||||||
|
return this.finiteCheck(t, e);
|
||||||
|
}
|
||||||
if (name === 'SUM_OF') { const t = xs.reduce((m, v) => m + v, 0); return isFloat ? t : this.checkInt(t, e); }
|
if (name === 'SUM_OF') { const t = xs.reduce((m, v) => m + v, 0); return isFloat ? t : this.checkInt(t, e); }
|
||||||
if (name === 'NORM_OF') return Math.sqrt(xs.reduce((m, v) => m + v * v, 0));
|
if (name === 'NORM_OF') return Math.sqrt(xs.reduce((m, v) => m + v * v, 0));
|
||||||
if (n === 0) throw new PseudoError('value', `${name} of an empty collection has no answer.`, e);
|
if (n === 0) throw new PseudoError('value', `${name} of an empty collection has no answer.`, e);
|
||||||
@@ -1265,6 +1366,11 @@ class Interpreter {
|
|||||||
case 'DOT': {
|
case 'DOT': {
|
||||||
const xs = x instanceof PArray ? x.data : x; const ys = y instanceof PArray ? y.data : y;
|
const xs = x instanceof PArray ? x.data : x; const ys = y instanceof PArray ? y.data : y;
|
||||||
if (xs.length !== ys.length) throw new PseudoError('value', `DOT needs two collections of the same length, and these hold ${xs.length} and ${ys.length} values.`, e);
|
if (xs.length !== ys.length) throw new PseudoError('value', `DOT needs two collections of the same length, and these hold ${xs.length} and ${ys.length} values.`, e);
|
||||||
|
if (isFloat32(e.type) || isSizedInt(e.type)) {
|
||||||
|
let t = 0;
|
||||||
|
for (let i = 0; i < xs.length; i++) t = isFloat32(e.type) ? Math.fround(t + Math.fround(xs[i] * ys[i])) : this.fitInt(e.type, t + this.fitInt(e.type, xs[i] * ys[i], e, 'result'), e, 'result');
|
||||||
|
return t;
|
||||||
|
}
|
||||||
let t = 0; for (let i = 0; i < xs.length; i++) t += xs[i] * ys[i];
|
let t = 0; for (let i = 0; i < xs.length; i++) t += xs[i] * ys[i];
|
||||||
const isFloat = e.type?.k === 'FLOAT' || !Number.isInteger(t);
|
const isFloat = e.type?.k === 'FLOAT' || !Number.isInteger(t);
|
||||||
return isFloat ? t : this.checkInt(t, e);
|
return isFloat ? t : this.checkInt(t, e);
|
||||||
@@ -1273,6 +1379,67 @@ class Interpreter {
|
|||||||
if (!Number.isInteger(x) || x < 0) throw new PseudoError('value', `FILL(${formatValue(x)}, ...) needs a whole number of copies, 0 or more.`, e);
|
if (!Number.isInteger(x) || x < 0) throw new PseudoError('value', `FILL(${formatValue(x)}, ...) needs a whole number of copies, 0 or more.`, e);
|
||||||
const out = []; for (let i = 0; i < x; i++) out.push(cloneValue(y)); return out;
|
const out = []; for (let i = 0; i < x; i++) out.push(cloneValue(y)); return out;
|
||||||
}
|
}
|
||||||
|
// ---- numerical work
|
||||||
|
case 'ASIN': case 'ACOS':
|
||||||
|
if ((x < -1 || x > 1) && !this.ieee) throw new PseudoError('value', `${name}(${formatValue(x)}) has no answer: ${name === 'ASIN' ? 'a sine' : 'a cosine'} lies between -1 and 1.`, e);
|
||||||
|
return name === 'ASIN' ? Math.asin(x) : Math.acos(x);
|
||||||
|
case 'ATAN': return Math.atan(x);
|
||||||
|
case 'SINH': return Math.sinh(x);
|
||||||
|
case 'COSH': return Math.cosh(x);
|
||||||
|
case 'TANH': return Math.tanh(x);
|
||||||
|
case 'HYPOT': return Math.hypot(x, y);
|
||||||
|
case 'POW':
|
||||||
|
if (x < 0 && !Number.isInteger(y) && !this.ieee) throw new PseudoError('value', `POW(${formatValue(x)}, ${formatValue(y)}) has no real answer: a negative number raised to a power that is not whole.`, e);
|
||||||
|
if (x === 0 && y < 0 && !this.ieee) throw new PseudoError('value', `POW(0, ${formatValue(y)}) has no answer: it divides by zero.`, e);
|
||||||
|
return x ** y;
|
||||||
|
case 'CBRT': return Math.cbrt(x);
|
||||||
|
case 'EXPM1': return Math.expm1(x);
|
||||||
|
case 'LOG1P':
|
||||||
|
if (x <= -1 && !this.ieee) throw new PseudoError('value', `LOG1P(${formatValue(x)}) has no answer: 1 + X has to be above 0.`, e);
|
||||||
|
return Math.log1p(x);
|
||||||
|
case 'E': return Math.E;
|
||||||
|
case 'TRUNC': return Math.trunc(x);
|
||||||
|
case 'SIGN': return x > 0 ? 1 : x < 0 ? -1 : 0;
|
||||||
|
case 'COPYSIGN': return copySign(x, y);
|
||||||
|
case 'CLAMP':
|
||||||
|
if (y > z) throw new PseudoError('value', `CLAMP(${formatValue(x)}, ${formatValue(y)}, ${formatValue(z)}) has no answer: the low end is above the high end.`, e);
|
||||||
|
return x < y ? y : x > z ? z : x;
|
||||||
|
case 'FMA': return fma(x, y, z, isFloat32(e.type));
|
||||||
|
case 'IS_NAN': return Number.isNaN(x);
|
||||||
|
case 'IS_INF': return x === Infinity || x === -Infinity;
|
||||||
|
case 'IS_FINITE': return Number.isFinite(x);
|
||||||
|
case 'INF': return Infinity;
|
||||||
|
case 'NAN': return NaN;
|
||||||
|
case 'ULP': return ulp(x, isFloat32(e.type));
|
||||||
|
case 'NEXT_AFTER': return nextAfter(isFloat32(e.type) ? Math.fround(x) : x, y, isFloat32(e.type));
|
||||||
|
case 'IS_CLOSE': {
|
||||||
|
const rel = z; const tol = a4();
|
||||||
|
if (rel < 0 || tol < 0) throw new PseudoError('value', 'IS_CLOSE needs tolerances of 0 or more.', e);
|
||||||
|
if (x === y) return true;
|
||||||
|
if (!Number.isFinite(x) || !Number.isFinite(y)) return false;
|
||||||
|
return Math.abs(x - y) <= Math.max(rel * Math.max(Math.abs(x), Math.abs(y)), tol);
|
||||||
|
}
|
||||||
|
case 'EXACT': return exactDecimal(x);
|
||||||
|
case 'MAX_VALUE': case 'MIN_VALUE': {
|
||||||
|
const ty = x;
|
||||||
|
if (ty.k === 'INTEGER') { const [lo, hi] = intRange(ty); return name === 'MAX_VALUE' ? hi : lo; }
|
||||||
|
const m = isFloat32(ty) ? 3.4028234663852886e38 : Number.MAX_VALUE;
|
||||||
|
return name === 'MAX_VALUE' ? m : -m;
|
||||||
|
}
|
||||||
|
case 'EPSILON': return isFloat32(x) ? 2 ** -23 : Number.EPSILON;
|
||||||
|
case 'GCD': return gcd(x, y);
|
||||||
|
case 'LCM': return x === 0 || y === 0 ? 0 : this.intResult(e, Math.abs((x / gcd(x, y)) * y));
|
||||||
|
case 'BIT_AND': return this.intResult(e, bitOp('AND', x, y));
|
||||||
|
case 'BIT_OR': return this.intResult(e, bitOp('OR', x, y));
|
||||||
|
case 'BIT_XOR': return this.intResult(e, bitOp('XOR', x, y));
|
||||||
|
case 'BIT_NOT': {
|
||||||
|
const ty = e.args[0].type;
|
||||||
|
return isSizedInt(ty) && !ty.signed ? intRange(ty)[1] - x : -x - 1;
|
||||||
|
}
|
||||||
|
case 'SHIFT_LEFT': case 'SHIFT_RIGHT':
|
||||||
|
if (!Number.isInteger(y) || y < 0) throw new PseudoError('value', `${name} moves bits by 0 or more places, and ${formatValue(y)} is not that.`, e);
|
||||||
|
return this.intResult(e, bitOp(name === 'SHIFT_LEFT' ? 'SHL' : 'SHR', x, y));
|
||||||
|
case 'WRAP': return wrap(x, y.bits, y.signed);
|
||||||
default: throw new PseudoError('value', `${name} is not a built-in function.`, e);
|
default: throw new PseudoError('value', `${name} is not a built-in function.`, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+147
-3
@@ -16,10 +16,13 @@
|
|||||||
/** Words that belong to the notation and can never be names. */
|
/** Words that belong to the notation and can never be names. */
|
||||||
export const KEYWORDS = new Set([
|
export const KEYWORDS = new Set([
|
||||||
'PARALLEL',
|
'PARALLEL',
|
||||||
|
// libraries
|
||||||
|
'USE',
|
||||||
// declarations
|
// declarations
|
||||||
'DECLARE', 'CONSTANT', 'DEFINE', 'TYPE', 'ENDTYPE', 'OF',
|
'DECLARE', 'CONSTANT', 'DEFINE', 'TYPE', 'ENDTYPE', 'OF',
|
||||||
// types
|
// types
|
||||||
'INTEGER', 'FLOAT', 'CHAR', 'STRING', 'BOOL', 'BOOLEAN', 'ARRAY', 'LIST', 'HASHMAP', 'SET',
|
'INTEGER', 'FLOAT', 'CHAR', 'STRING', 'BOOL', 'BOOLEAN', 'ARRAY', 'LIST', 'HASHMAP', 'SET',
|
||||||
|
'INT8', 'INT16', 'INT32', 'INT64', 'UINT8', 'UINT16', 'UINT32', 'UINT64', 'FLOAT32', 'FLOAT64',
|
||||||
// selection
|
// selection
|
||||||
'IF', 'THEN', 'ELSEIF', 'ELSE', 'ENDIF', 'CASE', 'OTHERWISE', 'ENDCASE',
|
'IF', 'THEN', 'ELSEIF', 'ELSE', 'ENDIF', 'CASE', 'OTHERWISE', 'ENDCASE',
|
||||||
// iteration
|
// iteration
|
||||||
@@ -77,10 +80,10 @@ export const KEYWORD_CLASS = (() => {
|
|||||||
const m = {};
|
const m = {};
|
||||||
const put = (cls, words) => { for (const w of words.split(' ')) m[w] = cls; };
|
const put = (cls, words) => { for (const w of words.split(' ')) m[w] = cls; };
|
||||||
put('flow', 'IF THEN ELSEIF ELSE ENDIF CASE OTHERWISE ENDCASE FOR TO STEP NEXT WHILE ENDWHILE REPEAT UNTIL RETURN BREAK CONTINUE EACH');
|
put('flow', 'IF THEN ELSEIF ELSE ENDIF CASE OTHERWISE ENDCASE FOR TO STEP NEXT WHILE ENDWHILE REPEAT UNTIL RETURN BREAK CONTINUE EACH');
|
||||||
put('decl', 'DECLARE CONSTANT DEFINE TYPE ENDTYPE FUNCTION ENDFUNCTION PROCEDURE ENDPROCEDURE RETURNS');
|
put('decl', 'DECLARE CONSTANT DEFINE TYPE ENDTYPE FUNCTION ENDFUNCTION PROCEDURE ENDPROCEDURE RETURNS USE');
|
||||||
put('kw', 'AND OR NOT DIV MOD OF CALL IN BYREF BYVAL');
|
put('kw', 'AND OR NOT DIV MOD OF CALL IN BYREF BYVAL');
|
||||||
put('io', 'INPUT OUTPUT');
|
put('io', 'INPUT OUTPUT');
|
||||||
put('type', 'INTEGER FLOAT CHAR STRING BOOL BOOLEAN ARRAY LIST HASHMAP SET');
|
put('type', 'INTEGER FLOAT CHAR STRING BOOL BOOLEAN ARRAY LIST HASHMAP SET INT8 INT16 INT32 INT64 UINT8 UINT16 UINT32 UINT64 FLOAT32 FLOAT64');
|
||||||
put('const', 'TRUE FALSE');
|
put('const', 'TRUE FALSE');
|
||||||
put('proof', 'REQUIRE ENSURE INVARIANT DECREASES ASSERT FORALL EXISTS SUM RESULT OLD');
|
put('proof', 'REQUIRE ENSURE INVARIANT DECREASES ASSERT FORALL EXISTS SUM RESULT OLD');
|
||||||
return m;
|
return m;
|
||||||
@@ -96,9 +99,61 @@ export const CLOSERS = {
|
|||||||
/* Types */
|
/* Types */
|
||||||
/* ------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The sized numeric types. Each is an INTEGER or a FLOAT with a width, so every rule that asks for "a whole
|
||||||
|
* number" (a position, a FOR counter, DIV) accepts INT32 as readily as INTEGER, and only the rules about
|
||||||
|
* range, rounding and mixing look at the width.
|
||||||
|
*
|
||||||
|
* INTEGER a whole number of any size (the teaching integer; a run holds it exactly to 2^53 - 1)
|
||||||
|
* INT8 ... INT64 two's complement, n bits; a result outside the range stops the run (obligation 'overflow')
|
||||||
|
* UINT8 ... UINT64 0 to 2^n - 1, the same way
|
||||||
|
* FLOAT, FLOAT64 IEEE 754 double precision (the two names are one type)
|
||||||
|
* FLOAT32 IEEE 754 single precision: every result is rounded to the nearest single
|
||||||
|
*
|
||||||
|
* INT64 and UINT64 are held exactly only to 2^53 - 1 in this lab (a JavaScript number); a result past that
|
||||||
|
* stops the run with a message that says so, and the prover checks against the same limit.
|
||||||
|
*/
|
||||||
|
export const SIZED = {
|
||||||
|
INT8: { k: 'INTEGER', bits: 8, signed: true, name: 'INT8' },
|
||||||
|
INT16: { k: 'INTEGER', bits: 16, signed: true, name: 'INT16' },
|
||||||
|
INT32: { k: 'INTEGER', bits: 32, signed: true, name: 'INT32' },
|
||||||
|
INT64: { k: 'INTEGER', bits: 64, signed: true, name: 'INT64' },
|
||||||
|
UINT8: { k: 'INTEGER', bits: 8, signed: false, name: 'UINT8' },
|
||||||
|
UINT16: { k: 'INTEGER', bits: 16, signed: false, name: 'UINT16' },
|
||||||
|
UINT32: { k: 'INTEGER', bits: 32, signed: false, name: 'UINT32' },
|
||||||
|
UINT64: { k: 'INTEGER', bits: 64, signed: false, name: 'UINT64' },
|
||||||
|
FLOAT32: { k: 'FLOAT', bits: 32, name: 'FLOAT32' },
|
||||||
|
FLOAT64: { k: 'FLOAT', bits: 64, name: 'FLOAT64' },
|
||||||
|
};
|
||||||
|
export const SIZED_NAMES = new Set(Object.keys(SIZED));
|
||||||
|
/** The largest whole number a run holds exactly. */
|
||||||
|
export const EXACT_MAX = Number.MAX_SAFE_INTEGER;
|
||||||
|
|
||||||
|
/** A whole-number type with a width (INT32, UINT8, ...), as opposed to INTEGER. */
|
||||||
|
export const isSizedInt = (t) => !!t && t.k === 'INTEGER' && !!t.bits;
|
||||||
|
/** FLOAT32, the one FLOAT that rounds to fewer bits than FLOAT. */
|
||||||
|
export const isFloat32 = (t) => !!t && t.k === 'FLOAT' && t.bits === 32;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The values a sized whole-number type holds in a run: [lowest, highest]. For INT64 and UINT64 the run's range
|
||||||
|
* is cut at 2^53 - 1, the largest whole number it holds exactly; `full` gives the type's own range as text.
|
||||||
|
*/
|
||||||
|
export function intRange(t) {
|
||||||
|
if (!isSizedInt(t)) return [-EXACT_MAX, EXACT_MAX];
|
||||||
|
if (t.bits >= 64) return t.signed ? [-EXACT_MAX, EXACT_MAX] : [0, EXACT_MAX];
|
||||||
|
return t.signed ? [-(2 ** (t.bits - 1)), 2 ** (t.bits - 1) - 1] : [0, 2 ** t.bits - 1];
|
||||||
|
}
|
||||||
|
export function intRangeText(t) {
|
||||||
|
const full = { INT64: ['-9,223,372,036,854,775,808', '9,223,372,036,854,775,807'], UINT64: ['0', '18,446,744,073,709,551,615'] }[t.name];
|
||||||
|
if (full) return `${full[0]} to ${full[1]}, held exactly in this lab only from ${t.signed ? '-9,007,199,254,740,991' : '0'} to 9,007,199,254,740,991`;
|
||||||
|
const [lo, hi] = intRange(t);
|
||||||
|
return `${lo.toLocaleString('en-US')} to ${hi.toLocaleString('en-US')}`;
|
||||||
|
}
|
||||||
|
|
||||||
export const T = {
|
export const T = {
|
||||||
INTEGER: { k: 'INTEGER' },
|
INTEGER: { k: 'INTEGER' },
|
||||||
FLOAT: { k: 'FLOAT' },
|
FLOAT: { k: 'FLOAT' },
|
||||||
|
...SIZED,
|
||||||
CHAR: { k: 'CHAR' },
|
CHAR: { k: 'CHAR' },
|
||||||
STRING: { k: 'STRING' },
|
STRING: { k: 'STRING' },
|
||||||
BOOL: { k: 'BOOL' },
|
BOOL: { k: 'BOOL' },
|
||||||
@@ -129,6 +184,7 @@ export function typeName(t) {
|
|||||||
case 'RANGE': return 'a range of positions';
|
case 'RANGE': return 'a range of positions';
|
||||||
case 'ANY': return 'any type';
|
case 'ANY': return 'any type';
|
||||||
case 'VOID': return 'nothing';
|
case 'VOID': return 'nothing';
|
||||||
|
case 'INTEGER': case 'FLOAT': return t.name || t.k;
|
||||||
default: return t.k;
|
default: return t.k;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,6 +198,8 @@ export function sameType(a, b) {
|
|||||||
case 'ARRAY': return a.dims === b.dims && sameType(a.of, b.of);
|
case 'ARRAY': return a.dims === b.dims && sameType(a.of, b.of);
|
||||||
case 'HASHMAP': return sameType(a.key, b.key) && sameType(a.val, b.val);
|
case 'HASHMAP': return sameType(a.key, b.key) && sameType(a.val, b.val);
|
||||||
case 'RECORD': return a.name.toUpperCase() === b.name.toUpperCase();
|
case 'RECORD': return a.name.toUpperCase() === b.name.toUpperCase();
|
||||||
|
case 'INTEGER': return (a.bits || 0) === (b.bits || 0) && (!a.bits || a.signed === b.signed);
|
||||||
|
case 'FLOAT': return isFloat32(a) === isFloat32(b);
|
||||||
default: return true;
|
default: return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -154,6 +212,11 @@ export function sameType(a, b) {
|
|||||||
export function assignable(to, from) {
|
export function assignable(to, from) {
|
||||||
if (isAny(to) || isAny(from)) return true;
|
if (isAny(to) || isAny(from)) return true;
|
||||||
if (to.k === 'FLOAT' && from.k === 'INTEGER') return true;
|
if (to.k === 'FLOAT' && from.k === 'INTEGER') return true;
|
||||||
|
// INTEGER into a sized type is checked when it happens (the run stops if it does not fit, and the prover has
|
||||||
|
// an obligation for it); a sized value always fits INTEGER; two different widths need a conversion: INT32(X).
|
||||||
|
if (to.k === 'INTEGER' && from.k === 'INTEGER') return !to.bits || !from.bits || sameType(to, from);
|
||||||
|
// FLOAT32 widens into FLOAT exactly; the other way rounds, so it is written FLOAT32(X).
|
||||||
|
if (to.k === 'FLOAT' && from.k === 'FLOAT') return !isFloat32(to) || isFloat32(from);
|
||||||
if (to.k === 'ARRAY' && from.k === 'LIST') return assignable(to.of, from.of);
|
if (to.k === 'ARRAY' && from.k === 'LIST') return assignable(to.of, from.of);
|
||||||
if (to.k === 'LIST' && from.k === 'LIST') return assignable(to.of, from.of) && (isAny(from.of) || sameType(to.of, from.of));
|
if (to.k === 'LIST' && from.k === 'LIST') return assignable(to.of, from.of) && (isAny(from.of) || sameType(to.of, from.of));
|
||||||
if (to.k === 'SET' && from.k === 'SET') return isAny(from.of) || sameType(to.of, from.of);
|
if (to.k === 'SET' && from.k === 'SET') return isAny(from.of) || sameType(to.of, from.of);
|
||||||
@@ -264,13 +327,26 @@ export function formatFloat(x) {
|
|||||||
* "GATTACA" with its quotes, [4, 17] for a list. `type` decides INTEGER
|
* "GATTACA" with its quotes, [4, 17] for a list. `type` decides INTEGER
|
||||||
* versus FLOAT, since both are JavaScript numbers underneath.
|
* versus FLOAT, since both are JavaScript numbers underneath.
|
||||||
*/
|
*/
|
||||||
|
/**
|
||||||
|
* A FLOAT32 as the shortest decimal that rounds back to the same single: FLOAT32(0.1) shows as 0.1, though the
|
||||||
|
* value it holds is 0.100000001490116119384765625 (EXACT shows that).
|
||||||
|
*/
|
||||||
|
export function formatFloat32(x) {
|
||||||
|
if (!Number.isFinite(x)) return formatFloat(x);
|
||||||
|
for (let p = 1; p <= 9; p++) {
|
||||||
|
const s = x.toPrecision(p);
|
||||||
|
if (Math.fround(Number(s)) === x) return formatFloat(Number(s));
|
||||||
|
}
|
||||||
|
return formatFloat(x);
|
||||||
|
}
|
||||||
|
|
||||||
export function formatValue(v, type, opts = {}) {
|
export function formatValue(v, type, opts = {}) {
|
||||||
const max = opts.max ?? 40;
|
const max = opts.max ?? 40;
|
||||||
if (v === UNSET) return opts.unset ?? '(no value)';
|
if (v === UNSET) return opts.unset ?? '(no value)';
|
||||||
if (v === undefined || v === null) return '(nothing)';
|
if (v === undefined || v === null) return '(nothing)';
|
||||||
if (typeof v === 'boolean') return v ? 'TRUE' : 'FALSE';
|
if (typeof v === 'boolean') return v ? 'TRUE' : 'FALSE';
|
||||||
if (typeof v === 'number') {
|
if (typeof v === 'number') {
|
||||||
if (type && type.k === 'FLOAT') return formatFloat(v);
|
if (type && type.k === 'FLOAT') return isFloat32(type) ? formatFloat32(v) : formatFloat(v);
|
||||||
if (type && type.k === 'INTEGER') return String(v);
|
if (type && type.k === 'INTEGER') return String(v);
|
||||||
return Number.isInteger(v) ? String(v) : formatFloat(v);
|
return Number.isInteger(v) ? String(v) : formatFloat(v);
|
||||||
}
|
}
|
||||||
@@ -364,6 +440,48 @@ export const BUILTINS = {
|
|||||||
TO_STRING: { kind: 'function', sig: 'TO_STRING(X)', doc: 'Any value written as a STRING, the way OUTPUT would print it.', group: 'Converting' },
|
TO_STRING: { kind: 'function', sig: 'TO_STRING(X)', doc: 'Any value written as a STRING, the way OUTPUT would print it.', group: 'Converting' },
|
||||||
TO_INTEGER: { kind: 'function', sig: 'TO_INTEGER(S)', doc: 'The INTEGER a STRING of digits spells, or the code of a CHAR digit.', group: 'Converting' },
|
TO_INTEGER: { kind: 'function', sig: 'TO_INTEGER(S)', doc: 'The INTEGER a STRING of digits spells, or the code of a CHAR digit.', group: 'Converting' },
|
||||||
TO_FLOAT: { kind: 'function', sig: 'TO_FLOAT(X)', doc: 'The FLOAT a STRING spells, or an INTEGER as a FLOAT.', group: 'Converting' },
|
TO_FLOAT: { kind: 'function', sig: 'TO_FLOAT(X)', doc: 'The FLOAT a STRING spells, or an INTEGER as a FLOAT.', group: 'Converting' },
|
||||||
|
// ---- more numbers, for numerical work
|
||||||
|
ASIN: { kind: 'function', sig: 'ASIN(X)', doc: 'The angle in radians whose sine is X, in [-PI()/2, PI()/2]; X must lie in [-1, 1].', group: 'Numbers' },
|
||||||
|
ACOS: { kind: 'function', sig: 'ACOS(X)', doc: 'The angle in radians whose cosine is X, in [0, PI()]; X must lie in [-1, 1].', group: 'Numbers' },
|
||||||
|
ATAN: { kind: 'function', sig: 'ATAN(X)', doc: 'The angle in radians whose tangent is X, in (-PI()/2, PI()/2).', group: 'Numbers' },
|
||||||
|
SINH: { kind: 'function', sig: 'SINH(X)', doc: 'The hyperbolic sine of X, as a FLOAT.', group: 'Numbers' },
|
||||||
|
COSH: { kind: 'function', sig: 'COSH(X)', doc: 'The hyperbolic cosine of X, as a FLOAT.', group: 'Numbers' },
|
||||||
|
TANH: { kind: 'function', sig: 'TANH(X)', doc: 'The hyperbolic tangent of X, as a FLOAT, in (-1, 1).', group: 'Numbers' },
|
||||||
|
HYPOT: { kind: 'function', sig: 'HYPOT(X, Y)', doc: 'SQRT(X * X + Y * Y), computed without overflowing on the way when X or Y is very large.', group: 'Numbers' },
|
||||||
|
POW: { kind: 'function', sig: 'POW(X, Y)', doc: 'X raised to the power Y, as a FLOAT; X must not be negative unless Y is whole.', group: 'Numbers' },
|
||||||
|
CBRT: { kind: 'function', sig: 'CBRT(X)', doc: 'The cube root of X, as a FLOAT; defined for negative X too.', group: 'Numbers' },
|
||||||
|
EXPM1: { kind: 'function', sig: 'EXPM1(X)', doc: 'EXP(X) - 1, accurate when X is close to 0, where the subtraction would lose every digit.', group: 'Numbers' },
|
||||||
|
LOG1P: { kind: 'function', sig: 'LOG1P(X)', doc: 'LOG(1 + X), accurate when X is close to 0; X must be above -1.', group: 'Numbers' },
|
||||||
|
E: { kind: 'function', sig: 'E()', doc: 'The number e, 2.71828…, as a FLOAT.', group: 'Numbers' },
|
||||||
|
TRUNC: { kind: 'function', sig: 'TRUNC(X)', doc: 'X with its fraction dropped toward zero, kept as a FLOAT: TRUNC(-2.7) is -2.0. INT does the same and gives an INTEGER.', group: 'Numbers' },
|
||||||
|
SIGN: { kind: 'function', sig: 'SIGN(X)', doc: '-1, 0 or 1, as X is negative, zero or positive, in the type of X.', group: 'Numbers' },
|
||||||
|
COPYSIGN: { kind: 'function', sig: 'COPYSIGN(X, Y)', doc: 'The size of X with the sign of Y, as a FLOAT.', group: 'Numbers' },
|
||||||
|
CLAMP: { kind: 'function', sig: 'CLAMP(X, Lo, Hi)', doc: 'X held inside [Lo, Hi]: Lo when X is below it, Hi when X is above it, X otherwise; Lo must not exceed Hi.', group: 'Numbers' },
|
||||||
|
FMA: { kind: 'function', sig: 'FMA(A, B, C)', doc: 'A * B + C with a single rounding at the end, as the processor\'s fused multiply-add computes it.', group: 'Precision' },
|
||||||
|
// ---- floating point itself
|
||||||
|
IS_NAN: { kind: 'function', sig: 'IS_NAN(X)', doc: 'TRUE when X is not a number. Only a program that says USE IEEE can hold one.', group: 'Precision' },
|
||||||
|
IS_INF: { kind: 'function', sig: 'IS_INF(X)', doc: 'TRUE when X is infinity or minus infinity. Only a program that says USE IEEE can hold one.', group: 'Precision' },
|
||||||
|
IS_FINITE: { kind: 'function', sig: 'IS_FINITE(X)', doc: 'TRUE when X is an ordinary number, neither infinite nor not a number.', group: 'Precision' },
|
||||||
|
INF: { kind: 'function', sig: 'INF()', doc: 'Positive infinity. A program has to say USE IEEE to hold it; otherwise the run stops.', group: 'Precision' },
|
||||||
|
NAN: { kind: 'function', sig: 'NAN()', doc: 'Not a number. A program has to say USE IEEE to hold it; otherwise the run stops.', group: 'Precision' },
|
||||||
|
ULP: { kind: 'function', sig: 'ULP(X)', doc: 'The gap between X and the next FLOAT away from zero: one unit in the last place, the size of X\'s rounding step. For a FLOAT32 X, the gap between singles.', group: 'Precision' },
|
||||||
|
NEXT_AFTER: { kind: 'function', sig: 'NEXT_AFTER(X, Y)', doc: 'The FLOAT next to X in the direction of Y (a FLOAT32 next to X, for a FLOAT32 X).', group: 'Precision' },
|
||||||
|
IS_CLOSE: { kind: 'function', sig: 'IS_CLOSE(A, B, RelTol, AbsTol)', doc: 'TRUE when ABS(A - B) <= MAX(RelTol * MAX(ABS(A), ABS(B)), AbsTol): equality for measured values, with the tolerance said out loud.', group: 'Precision' },
|
||||||
|
EXACT: { kind: 'function', sig: 'EXACT(X)', doc: 'The exact decimal value a FLOAT holds, as a STRING: EXACT(0.1) is "0.1000000000000000055511151231257827021181583404541015625".', group: 'Precision' },
|
||||||
|
// ---- the limits of a type
|
||||||
|
MAX_VALUE: { kind: 'function', sig: 'MAX_VALUE(T)', doc: 'The largest value of the numeric type T: MAX_VALUE(INT8) is 127, MAX_VALUE(FLOAT64) about 1.8 × 10^308.', group: 'Precision' },
|
||||||
|
MIN_VALUE: { kind: 'function', sig: 'MIN_VALUE(T)', doc: 'The lowest value of the numeric type T: MIN_VALUE(INT8) is -128, MIN_VALUE(FLOAT64) is -MAX_VALUE(FLOAT64).', group: 'Precision' },
|
||||||
|
EPSILON: { kind: 'function', sig: 'EPSILON(T)', doc: 'For FLOAT32 or FLOAT64, the gap between 1.0 and the next value above it: 2^-23 or 2^-52.', group: 'Precision' },
|
||||||
|
// ---- whole numbers bit by bit, and arithmetic that wraps
|
||||||
|
GCD: { kind: 'function', sig: 'GCD(A, B)', doc: 'The greatest common divisor of two whole numbers; GCD(0, 0) is 0.', group: 'Whole numbers' },
|
||||||
|
LCM: { kind: 'function', sig: 'LCM(A, B)', doc: 'The least common multiple of two whole numbers; 0 when either is 0.', group: 'Whole numbers' },
|
||||||
|
BIT_AND: { kind: 'function', sig: 'BIT_AND(A, B)', doc: 'The bits set in both A and B (two\'s complement for negative values).', group: 'Whole numbers' },
|
||||||
|
BIT_OR: { kind: 'function', sig: 'BIT_OR(A, B)', doc: 'The bits set in A or B or both.', group: 'Whole numbers' },
|
||||||
|
BIT_XOR: { kind: 'function', sig: 'BIT_XOR(A, B)', doc: 'The bits set in exactly one of A and B.', group: 'Whole numbers' },
|
||||||
|
BIT_NOT: { kind: 'function', sig: 'BIT_NOT(A)', doc: 'Every bit of A flipped, in A\'s type: BIT_NOT of a UINT8 0 is 255, of an INT8 or INTEGER 0 is -1.', group: 'Whole numbers' },
|
||||||
|
SHIFT_LEFT: { kind: 'function', sig: 'SHIFT_LEFT(A, N)', doc: 'A times 2^N: the bits moved N places up. A result outside A\'s type stops the run, as any overflow does.', group: 'Whole numbers' },
|
||||||
|
SHIFT_RIGHT: { kind: 'function', sig: 'SHIFT_RIGHT(A, N)', doc: 'A divided by 2^N, rounded down: the bits moved N places down (the sign kept, for a negative A).', group: 'Whole numbers' },
|
||||||
|
WRAP: { kind: 'function', sig: 'WRAP(A, T)', doc: 'The whole number A brought into the sized type T (up to 32 bits) by wrapping around, as hardware does: WRAP(300, UINT8) is 44. Arithmetic that is meant to wrap says so with WRAP.', group: 'Whole numbers' },
|
||||||
KEYS: { kind: 'function', sig: 'KEYS(H)', doc: 'The keys of a HASHMAP as a LIST, smallest first.', group: 'Collections' },
|
KEYS: { kind: 'function', sig: 'KEYS(H)', doc: 'The keys of a HASHMAP as a LIST, smallest first.', group: 'Collections' },
|
||||||
SORTED: { kind: 'function', sig: 'SORTED(X)', doc: 'The values of a LIST or SET as a new LIST, smallest first.', group: 'Collections' },
|
SORTED: { kind: 'function', sig: 'SORTED(X)', doc: 'The values of a LIST or SET as a new LIST, smallest first.', group: 'Collections' },
|
||||||
APPEND: { kind: 'procedure', sig: 'APPEND(L, V)', doc: 'Adds V to the end of the LIST L.', group: 'Collections' },
|
APPEND: { kind: 'procedure', sig: 'APPEND(L, V)', doc: 'Adds V to the end of the LIST L.', group: 'Collections' },
|
||||||
@@ -374,6 +492,32 @@ export const BUILTINS = {
|
|||||||
SORT: { kind: 'procedure', sig: 'SORT(L)', doc: 'Puts the values of the LIST L in order, smallest first. Equal values keep their order.', group: 'Collections' },
|
SORT: { kind: 'procedure', sig: 'SORT(L)', doc: 'Puts the values of the LIST L in order, smallest first. Equal values keep their order.', group: 'Collections' },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A sized type's name followed by brackets converts a number to that type, as Mojo writes Int32(x):
|
||||||
|
* INT32(X) and the other whole-number types stop the run when X does not fit (and drop a FLOAT's fraction toward
|
||||||
|
* zero first); FLOAT32(X) rounds to the nearest single; FLOAT64(X) gives a FLOAT.
|
||||||
|
*/
|
||||||
|
export const CONVERSIONS = Object.fromEntries(Object.keys(SIZED).map((n) => [n, {
|
||||||
|
sig: `${n}(X)`,
|
||||||
|
doc: SIZED[n].k === 'INTEGER' ? `X as ${n}, from ${intRangeText(SIZED[n])}; a FLOAT loses its fraction toward zero first, and a value outside the range stops the run.` : n === 'FLOAT32' ? 'X rounded to the nearest FLOAT32 (single precision, about 7 significant digits).' : 'X as a FLOAT64 (the same type as FLOAT).',
|
||||||
|
group: 'Converting',
|
||||||
|
}]));
|
||||||
|
|
||||||
|
/** Built-ins that take a type's name as an argument, and at which position. */
|
||||||
|
export const TYPE_ARGS = { MAX_VALUE: 0, MIN_VALUE: 0, EPSILON: 0, WRAP: 1 };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Built-ins added for numerical work after programs were already being written. A program may still name its
|
||||||
|
* own variable E or define its own FUNCTION GCD: that definition wins inside the program, and the built-in of
|
||||||
|
* the same name is simply not reachable there. The older built-ins stay reserved.
|
||||||
|
*/
|
||||||
|
export const SOFT_BUILTINS = new Set([
|
||||||
|
'ASIN', 'ACOS', 'ATAN', 'SINH', 'COSH', 'TANH', 'HYPOT', 'POW', 'CBRT', 'EXPM1', 'LOG1P', 'E', 'TRUNC', 'SIGN',
|
||||||
|
'COPYSIGN', 'CLAMP', 'FMA', 'IS_NAN', 'IS_INF', 'IS_FINITE', 'INF', 'NAN', 'ULP', 'NEXT_AFTER', 'IS_CLOSE', 'EXACT',
|
||||||
|
'MAX_VALUE', 'MIN_VALUE', 'EPSILON', 'GCD', 'LCM', 'BIT_AND', 'BIT_OR', 'BIT_XOR', 'BIT_NOT', 'SHIFT_LEFT',
|
||||||
|
'SHIFT_RIGHT', 'WRAP',
|
||||||
|
]);
|
||||||
|
|
||||||
/** Built-ins that change their first argument in place. */
|
/** Built-ins that change their first argument in place. */
|
||||||
export const MUTATORS = new Set(['APPEND', 'INSERT', 'REMOVE', 'ADD', 'DELETE', 'SORT']);
|
export const MUTATORS = new Set(['APPEND', 'INSERT', 'REMOVE', 'ADD', 'DELETE', 'SORT']);
|
||||||
|
|
||||||
|
|||||||
@@ -466,8 +466,9 @@ export function show(x: Term, names: Map<string, string> = new Map(), prec = 0):
|
|||||||
}
|
}
|
||||||
|
|
||||||
function showApp(x: App, names: Map<string, string>, p: (inner: string, level: number) => string): string {
|
function showApp(x: App, names: Map<string, string>, p: (inner: string, level: number) => string): string {
|
||||||
if (/^FLOAT[-+*/^]$/.test(x.f)) {
|
if (x.f === 'TOFLOAT32') return `FLOAT32(${show(x.args[0], names)})`;
|
||||||
const op = x.f.slice(5);
|
if (/^(FLOAT|F32)[-+*/^]$/.test(x.f)) {
|
||||||
|
const op = x.f.slice(-1);
|
||||||
const lv = ({ '+': 6, '-': 6, '*': 7, '/': 7, '^': 9 } as Record<string, number>)[op] ?? 7;
|
const lv = ({ '+': 6, '-': 6, '*': 7, '/': 7, '^': 9 } as Record<string, number>)[op] ?? 7;
|
||||||
return p(`${show(x.args[0], names, lv)} ${op} ${show(x.args[1], names, lv + (op === '^' ? 0 : 1))}`, lv);
|
return p(`${show(x.args[0], names, lv)} ${op} ${show(x.args[1], names, lv + (op === '^' ? 0 : 1))}`, lv);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,156 @@
|
|||||||
|
/**
|
||||||
|
* pseudo/numeric.js
|
||||||
|
*
|
||||||
|
* The exact arithmetic behind the numerical built-ins: what a FLOAT holds digit for digit, the next value
|
||||||
|
* after it, a fused multiply-add rounded once, and whole numbers bit by bit. Every function here is exact
|
||||||
|
* (or correctly rounded) for the values a run can hold, so a lesson about rounding is never itself rounded.
|
||||||
|
*
|
||||||
|
* Nothing here depends on the DOM.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const view = new DataView(new ArrayBuffer(8));
|
||||||
|
|
||||||
|
/** A finite double as M * 2^E with M a BigInt (signed). 0 is { m: 0n, e: 0 }. */
|
||||||
|
export function decompose(x) {
|
||||||
|
if (x === 0) return { m: 0n, e: 0 };
|
||||||
|
view.setFloat64(0, x);
|
||||||
|
const hi = view.getUint32(0);
|
||||||
|
const lo = view.getUint32(4);
|
||||||
|
const exp = (hi >>> 20) & 0x7ff;
|
||||||
|
let mant = (BigInt(hi & 0xfffff) << 32n) | BigInt(lo);
|
||||||
|
let e;
|
||||||
|
if (exp === 0) e = -1074;
|
||||||
|
else { mant |= 1n << 52n; e = exp - 1075; }
|
||||||
|
return { m: x < 0 ? -mant : mant, e };
|
||||||
|
}
|
||||||
|
|
||||||
|
const bitLength = (a) => (a === 0n ? 0 : a.toString(2).length);
|
||||||
|
|
||||||
|
/** x times 2^e, for an x and e whose product is representable: never rounds twice. */
|
||||||
|
function scale(x, e) {
|
||||||
|
while (e > 1000) { x *= 2 ** 1000; e -= 1000; }
|
||||||
|
while (e < -1000) { x *= 2 ** -1000; e += 1000; }
|
||||||
|
return x * 2 ** e;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The value M * 2^E rounded to the nearest binary floating-point number with `p` bits of precision and smallest
|
||||||
|
* exponent `emin` (ties to even): p = 53, emin = -1074 for FLOAT64; p = 24, emin = -149 for FLOAT32.
|
||||||
|
*/
|
||||||
|
export function roundDyadic(m, e, p = 53, emin = -1074) {
|
||||||
|
if (m === 0n) return 0;
|
||||||
|
const neg = m < 0n;
|
||||||
|
let a = neg ? -m : m;
|
||||||
|
const lead = bitLength(a) - 1 + e;
|
||||||
|
const q = Math.max(lead - (p - 1), emin);
|
||||||
|
const shift = q - e;
|
||||||
|
if (shift > 0) {
|
||||||
|
const s = BigInt(shift);
|
||||||
|
let quot = a >> s;
|
||||||
|
const rem = a - (quot << s);
|
||||||
|
const half = 1n << (s - 1n);
|
||||||
|
if (rem > half || (rem === half && (quot & 1n) === 1n)) quot += 1n;
|
||||||
|
a = quot;
|
||||||
|
e = q;
|
||||||
|
}
|
||||||
|
const r = scale(Number(a), e);
|
||||||
|
return neg ? -r : r;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A * B + C with one rounding at the end, in FLOAT64 or (f32) FLOAT32 precision. */
|
||||||
|
export function fma(a, b, c, f32 = false) {
|
||||||
|
if (![a, b, c].every(Number.isFinite)) { const r = a * b + c; return f32 ? Math.fround(r) : r; }
|
||||||
|
const x = decompose(a); const y = decompose(b); const z = decompose(c);
|
||||||
|
const pm = x.m * y.m; const pe = x.e + y.e;
|
||||||
|
if (pm === 0n && z.m === 0n) return f32 ? Math.fround(a * b + c) : a * b + c; // keeps the sign of zero
|
||||||
|
let m; let e;
|
||||||
|
if (pm === 0n) { m = z.m; e = z.e; } else if (z.m === 0n) { m = pm; e = pe; } else {
|
||||||
|
e = Math.min(pe, z.e);
|
||||||
|
m = (pm << BigInt(pe - e)) + (z.m << BigInt(z.e - e));
|
||||||
|
}
|
||||||
|
if (m === 0n) return 0;
|
||||||
|
const r = f32 ? roundDyadic(m, e, 24, -149) : roundDyadic(m, e, 53, -1074);
|
||||||
|
return f32 ? Math.fround(r) : r;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The exact decimal value of a finite FLOAT: 0.1 is 0.1000000000000000055511151231257827021181583404541015625. */
|
||||||
|
export function exactDecimal(x) {
|
||||||
|
if (Number.isNaN(x)) return 'not a number';
|
||||||
|
if (!Number.isFinite(x)) return x > 0 ? 'infinity' : '-infinity';
|
||||||
|
if (x === 0) return Object.is(x, -0) ? '-0.0' : '0.0';
|
||||||
|
const { m, e } = decompose(x);
|
||||||
|
const neg = m < 0n;
|
||||||
|
const a = neg ? -m : m;
|
||||||
|
let s;
|
||||||
|
if (e >= 0) s = `${(a << BigInt(e)).toString()}.0`;
|
||||||
|
else {
|
||||||
|
const digits = (a * 5n ** BigInt(-e)).toString().padStart(-e + 1, '0');
|
||||||
|
const cut = digits.length + e;
|
||||||
|
s = `${digits.slice(0, cut)}.${digits.slice(cut)}`.replace(/0+$/, '');
|
||||||
|
if (s.endsWith('.')) s += '0';
|
||||||
|
}
|
||||||
|
return neg ? `-${s}` : s;
|
||||||
|
}
|
||||||
|
|
||||||
|
const f32buf = new Float32Array(1);
|
||||||
|
const u32buf = new Uint32Array(f32buf.buffer);
|
||||||
|
|
||||||
|
/** The next representable value after x toward y, in FLOAT64 or (f32) FLOAT32. */
|
||||||
|
export function nextAfter(x, y, f32 = false) {
|
||||||
|
if (Number.isNaN(x) || Number.isNaN(y)) return NaN;
|
||||||
|
if (x === y) return y;
|
||||||
|
if (f32) {
|
||||||
|
if (x === 0) { u32buf[0] = 1; return y > 0 ? f32buf[0] : -f32buf[0]; }
|
||||||
|
f32buf[0] = x;
|
||||||
|
if ((y > x) === (x > 0)) u32buf[0] += 1; else u32buf[0] -= 1;
|
||||||
|
return f32buf[0];
|
||||||
|
}
|
||||||
|
if (x === 0) return y > 0 ? Number.MIN_VALUE : -Number.MIN_VALUE;
|
||||||
|
view.setFloat64(0, x);
|
||||||
|
let bits = view.getBigUint64(0);
|
||||||
|
if ((y > x) === (x > 0)) bits += 1n; else bits -= 1n;
|
||||||
|
view.setBigUint64(0, bits);
|
||||||
|
return view.getFloat64(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One unit in the last place of x: the gap from |x| to the next value away from zero (or below it, at the top). */
|
||||||
|
export function ulp(x, f32 = false) {
|
||||||
|
if (!Number.isFinite(x)) return NaN;
|
||||||
|
const a = Math.abs(x);
|
||||||
|
const up = nextAfter(a, Infinity, f32);
|
||||||
|
if (Number.isFinite(up)) return up - a;
|
||||||
|
return a - nextAfter(a, 0, f32);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The sign of y on the size of x, as IEEE 754 copySign (the sign of zero counts). */
|
||||||
|
export function copySign(x, y) {
|
||||||
|
const neg = y < 0 || Object.is(y, -0);
|
||||||
|
return neg ? -Math.abs(x) : Math.abs(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function gcd(a, b) {
|
||||||
|
a = Math.abs(a); b = Math.abs(b);
|
||||||
|
while (b) [a, b] = [b, a % b];
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Bitwise operations on whole numbers held exactly, two's complement for negative values. */
|
||||||
|
export function bitOp(op, a, b) {
|
||||||
|
const x = BigInt(a); const y = BigInt(b);
|
||||||
|
switch (op) {
|
||||||
|
case 'AND': return Number(x & y);
|
||||||
|
case 'OR': return Number(x | y);
|
||||||
|
case 'XOR': return Number(x ^ y);
|
||||||
|
case 'SHL': return Number(x << y);
|
||||||
|
case 'SHR': return Number(x >> y);
|
||||||
|
default: throw new Error(op);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A whole number brought into an n-bit type by wrapping around (n up to 32). */
|
||||||
|
export function wrap(a, bits, signed) {
|
||||||
|
const m = 2 ** bits;
|
||||||
|
let r = ((a % m) + m) % m;
|
||||||
|
if (signed && r >= m / 2) r -= m;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
@@ -13,7 +13,10 @@
|
|||||||
* closing keyword.
|
* closing keyword.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { KEYWORDS, FOREIGN_WORDS, CLOSERS, BUILTINS } from './lang.js';
|
import { KEYWORDS, FOREIGN_WORDS, CLOSERS, BUILTINS, SIZED_NAMES } from './lang.js';
|
||||||
|
|
||||||
|
/** Keywords that name a type, so they can stand as an argument: MAX_VALUE(INT8), WRAP(X, UINT8). */
|
||||||
|
const TYPE_WORDS = new Set(['INTEGER', 'FLOAT', ...SIZED_NAMES]);
|
||||||
|
|
||||||
/* ------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------ */
|
||||||
/* Diagnostics */
|
/* Diagnostics */
|
||||||
@@ -127,10 +130,20 @@ export function lex(source) {
|
|||||||
if (m) { push('blank', m[1].trim(), i, i + m[0].length); i += m[0].length; continue; }
|
if (m) { push('blank', m[1].trim(), i, i + m[0].length); i += m[0].length; continue; }
|
||||||
}
|
}
|
||||||
|
|
||||||
// numbers
|
// numbers: 255, 2.5, 6.022e23, 1_000_000, 0xFF, 0b1010
|
||||||
if (/[0-9]/.test(c)) {
|
if (/[0-9]/.test(c)) {
|
||||||
const m = /^[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?/.exec(text.slice(i));
|
const based = /^0([xX][0-9A-Fa-f_]+|[bB][01_]+)/.exec(text.slice(i));
|
||||||
const s = m[0];
|
if (based) {
|
||||||
|
const s = based[0];
|
||||||
|
if (/_$|__|^0[xXbB]_/.test(s)) diags.push(diag('error', 'bad-number', line, `An underscore goes between two digits, as in 0xFF_FF; ${s} has one elsewhere.`, { col: i + 1 }));
|
||||||
|
if (/[A-Za-z0-9_]/.test(text[i + s.length] || '')) diags.push(diag('error', 'bad-number', line, `${/^[0-9A-Za-z_]+/.exec(text.slice(i))[0]} is not a number: ${/[xX]/.test(s[1]) ? 'hexadecimal digits are 0 to 9 and A to F' : 'binary digits are 0 and 1'}.`, { col: i + 1 }));
|
||||||
|
push('int', Number(s.replace(/_/g, '')), i, i + s.length);
|
||||||
|
i += s.length;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const m = /^[0-9][0-9_]*(\.[0-9][0-9_]*)?([eE][+-]?[0-9]+)?/.exec(text.slice(i));
|
||||||
|
let s = m[0];
|
||||||
|
if (/_$|__|_\.|\._/.test(s)) diags.push(diag('error', 'bad-number', line, `An underscore goes between two digits, as in 1_000_000; ${s} has one elsewhere.`, { col: i + 1 }));
|
||||||
const isFloat = Boolean(m[1] || m[2]);
|
const isFloat = Boolean(m[1] || m[2]);
|
||||||
if (text[i + s.length] === '.' && /[0-9]/.test(text[i + s.length + 1] || '') === false && !m[1]) {
|
if (text[i + s.length] === '.' && /[0-9]/.test(text[i + s.length + 1] || '') === false && !m[1]) {
|
||||||
diags.push(diag('error', 'bad-number', line, `Write a FLOAT with a digit on each side of the point, as in ${s}.0.`, { col: i + 1 }));
|
diags.push(diag('error', 'bad-number', line, `Write a FLOAT with a digit on each side of the point, as in ${s}.0.`, { col: i + 1 }));
|
||||||
@@ -138,7 +151,7 @@ export function lex(source) {
|
|||||||
if (/[A-Za-z_]/.test(text[i + s.length] || '')) {
|
if (/[A-Za-z_]/.test(text[i + s.length] || '')) {
|
||||||
diags.push(diag('error', 'bad-name', line, `A name must start with a letter, and "${/^[0-9A-Za-z_]+/.exec(text.slice(i))[0]}" starts with a digit.`, { col: i + 1, guide: 'names' }));
|
diags.push(diag('error', 'bad-name', line, `A name must start with a letter, and "${/^[0-9A-Za-z_]+/.exec(text.slice(i))[0]}" starts with a digit.`, { col: i + 1, guide: 'names' }));
|
||||||
}
|
}
|
||||||
push(isFloat ? 'float' : 'int', Number(s), i, i + s.length);
|
push(isFloat ? 'float' : 'int', Number(s.replace(/_/g, '')), i, i + s.length);
|
||||||
i += s.length;
|
i += s.length;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -446,6 +459,7 @@ class Parser {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
case 'CALL': return this.callStmt();
|
case 'CALL': return this.callStmt();
|
||||||
|
case 'USE': return this.useStmt();
|
||||||
case 'REQUIRE': case 'ENSURE': case 'INVARIANT': case 'DECREASES': case 'ASSERT': {
|
case 'REQUIRE': case 'ENSURE': case 'INVARIANT': case 'DECREASES': case 'ASSERT': {
|
||||||
this.next();
|
this.next();
|
||||||
const kind = t.v[0] + t.v.slice(1).toLowerCase();
|
const kind = t.v[0] + t.v.slice(1).toLowerCase();
|
||||||
@@ -552,6 +566,27 @@ class Parser {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** USE stats, USE stats.Mean, or USE IEEE: routines from the library, or IEEE 754 arithmetic. */
|
||||||
|
useStmt() {
|
||||||
|
const t = this.next();
|
||||||
|
if (this.tok.t !== 'name') {
|
||||||
|
this.error('expected', this.tok, 'USE names a module of the library, as in USE stats, or one routine of it, as in USE stats.Mean. USE IEEE asks for IEEE 754 arithmetic.', { guide: 'libraries' });
|
||||||
|
this.skipLine(); this.endLine('USE'); return null;
|
||||||
|
}
|
||||||
|
const m = this.next();
|
||||||
|
let routine = null;
|
||||||
|
let routineCol = null;
|
||||||
|
if (this.isOp('.')) {
|
||||||
|
this.next();
|
||||||
|
if (this.tok.t !== 'name') { this.error('expected', this.tok, `After USE ${m.v}. comes the name of one routine, as in USE ${m.v}.Mean.`, { guide: 'libraries' }); this.skipLine(); this.endLine('USE'); return null; }
|
||||||
|
const r = this.next();
|
||||||
|
routine = r.v; routineCol = r.col;
|
||||||
|
}
|
||||||
|
const s = this.node('Use', t, { module: m.v, moduleCol: m.col, routine, routineCol, ieee: m.v.toUpperCase() === 'IEEE' && !routine });
|
||||||
|
this.endLine('USE');
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
declare() {
|
declare() {
|
||||||
const t = this.next();
|
const t = this.next();
|
||||||
const names = [];
|
const names = [];
|
||||||
@@ -583,6 +618,7 @@ class Parser {
|
|||||||
if (t.t !== 'kw') { this.error('expected', t, `Expected a type here${this.describeFound()}, such as INTEGER, STRING, or LIST OF INTEGER.`, { guide: 'the-types' }); return { tk: 'error', line: t.line, col: t.col }; }
|
if (t.t !== 'kw') { this.error('expected', t, `Expected a type here${this.describeFound()}, such as INTEGER, STRING, or LIST OF INTEGER.`, { guide: 'the-types' }); return { tk: 'error', line: t.line, col: t.col }; }
|
||||||
switch (t.v) {
|
switch (t.v) {
|
||||||
case 'INTEGER': case 'FLOAT': case 'CHAR': case 'STRING': case 'BOOL':
|
case 'INTEGER': case 'FLOAT': case 'CHAR': case 'STRING': case 'BOOL':
|
||||||
|
case 'INT8': case 'INT16': case 'INT32': case 'INT64': case 'UINT8': case 'UINT16': case 'UINT32': case 'UINT64': case 'FLOAT32': case 'FLOAT64':
|
||||||
this.next(); return { tk: 'scalar', name: t.v, line: t.line, col: t.col };
|
this.next(); return { tk: 'scalar', name: t.v, line: t.line, col: t.col };
|
||||||
case 'LIST': case 'SET': {
|
case 'LIST': case 'SET': {
|
||||||
this.next();
|
this.next();
|
||||||
@@ -613,7 +649,7 @@ class Parser {
|
|||||||
return { tk: 'map', key, val: this.typeExpr(), line: t.line, col: t.col };
|
return { tk: 'map', key, val: this.typeExpr(), line: t.line, col: t.col };
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
this.error('expected', t, `${t.v} is not a type. The types are INTEGER, FLOAT, CHAR, STRING, BOOL, ARRAY, LIST, HASHMAP, SET, and records you define with TYPE.`, { guide: 'the-types' });
|
this.error('expected', t, `${t.v} is not a type. The types are INTEGER, FLOAT, CHAR, STRING, BOOL, the sized numbers (INT8 to INT64, UINT8 to UINT64, FLOAT32, FLOAT64), ARRAY, LIST, HASHMAP, SET, and records you define with TYPE.`, { guide: 'the-types' });
|
||||||
this.next();
|
this.next();
|
||||||
return { tk: 'error', line: t.line, col: t.col };
|
return { tk: 'error', line: t.line, col: t.col };
|
||||||
}
|
}
|
||||||
@@ -1177,6 +1213,13 @@ class Parser {
|
|||||||
}
|
}
|
||||||
case 'kw':
|
case 'kw':
|
||||||
if (t.v === 'TRUE' || t.v === 'FALSE') { this.next(); return { kind: 'Bool', value: t.v === 'TRUE', line: t.line, col: t.col }; }
|
if (t.v === 'TRUE' || t.v === 'FALSE') { this.next(); return { kind: 'Bool', value: t.v === 'TRUE', line: t.line, col: t.col }; }
|
||||||
|
if (SIZED_NAMES.has(t.v) && this.isOp('(', this.peek())) {
|
||||||
|
// INT32(X), FLOAT32(X): a conversion, written the way Mojo writes Int32(x)
|
||||||
|
this.next();
|
||||||
|
const open = this.next();
|
||||||
|
return { kind: 'CallExpr', name: t.v, args: this.args(')'), convert: true, line: t.line, col: t.col, openCol: open.col };
|
||||||
|
}
|
||||||
|
if (TYPE_WORDS.has(t.v)) { this.next(); return { kind: 'TypeRef', name: t.v, line: t.line, col: t.col }; }
|
||||||
if (t.v === 'FORALL' || t.v === 'EXISTS') return this.quantifier(t.v);
|
if (t.v === 'FORALL' || t.v === 'EXISTS') return this.quantifier(t.v);
|
||||||
if (t.v === 'NOT') return this.notExpr();
|
if (t.v === 'NOT') return this.notExpr();
|
||||||
this.error('expected', t, BUILTINS[t.v]
|
this.error('expected', t, BUILTINS[t.v]
|
||||||
@@ -1264,6 +1307,7 @@ export function sourceOf(e) {
|
|||||||
case 'Result': return 'RESULT';
|
case 'Result': return 'RESULT';
|
||||||
case 'Old': return `OLD(${sourceOf(e.arg)})`;
|
case 'Old': return `OLD(${sourceOf(e.arg)})`;
|
||||||
case 'Blank': return `<${e.text}>`;
|
case 'Blank': return `<${e.text}>`;
|
||||||
|
case 'TypeRef': return e.name;
|
||||||
case 'Unary': return e.op === 'NOT' ? `NOT ${wrap(e.arg)}` : `-${wrap(e.arg)}`;
|
case 'Unary': return e.op === 'NOT' ? `NOT ${wrap(e.arg)}` : `-${wrap(e.arg)}`;
|
||||||
case 'Binary': return `${sideOf(e.left, e.op, 'l')} ${e.op} ${sideOf(e.right, e.op, 'r')}`;
|
case 'Binary': return `${sideOf(e.left, e.op, 'l')} ${e.op} ${sideOf(e.right, e.op, 'r')}`;
|
||||||
case 'Chain': return e.operands.map((o, i) => (i ? `${e.ops[i - 1]} ${sourceOf(o)}` : sourceOf(o))).join(' ');
|
case 'Chain': return e.operands.map((o, i) => (i ? `${e.ops[i - 1]} ${sourceOf(o)}` : sourceOf(o))).join(' ');
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
import { parseExpression, sourceOf } from './parse.js';
|
import { parseExpression, sourceOf } from './parse.js';
|
||||||
import { run, evaluateConstant, makeRandom, valueFits } from './interp.js';
|
import { run, evaluateConstant, makeRandom, valueFits } from './interp.js';
|
||||||
import { valuesEqual, formatValue, outputText, typeName, T, cloneValue, PArray } from './lang.js';
|
import { valuesEqual, formatValue, outputText, typeName, T, cloneValue, PArray, intRange } from './lang.js';
|
||||||
|
|
||||||
const up = (s) => String(s).toUpperCase();
|
const up = (s) => String(s).toUpperCase();
|
||||||
|
|
||||||
@@ -175,8 +175,20 @@ function generatorFor(param, rnd, opts) {
|
|||||||
const int = (lo, hi) => lo + Math.floor(rnd() * (hi - lo + 1));
|
const int = (lo, hi) => lo + Math.floor(rnd() * (hi - lo + 1));
|
||||||
const make = (t) => {
|
const make = (t) => {
|
||||||
switch (t?.k) {
|
switch (t?.k) {
|
||||||
case 'INTEGER': return () => (rnd() < 0.3 ? pick([0, 1, 2, -1]) : int(opts.intMin ?? -3, opts.intMax ?? 12));
|
case 'INTEGER': {
|
||||||
case 'FLOAT': return () => Math.round((rnd() * 12 - 2) * 100) / 100;
|
if (t.bits) {
|
||||||
|
// a sized type: small values, and the edges of its range, where overflow lives
|
||||||
|
const [lo, hi] = intRange(t);
|
||||||
|
const small = () => Math.min(hi, Math.max(lo, int(opts.intMin ?? -3, opts.intMax ?? 12)));
|
||||||
|
const edges = [lo, lo + 1, hi - 1, hi, 0, 1].filter((v) => v >= lo && v <= hi);
|
||||||
|
return () => (rnd() < 0.3 ? pick(edges) : small());
|
||||||
|
}
|
||||||
|
return () => (rnd() < 0.3 ? pick([0, 1, 2, -1]) : int(opts.intMin ?? -3, opts.intMax ?? 12));
|
||||||
|
}
|
||||||
|
case 'FLOAT': {
|
||||||
|
const f = () => Math.round((rnd() * 12 - 2) * 100) / 100;
|
||||||
|
return t.bits === 32 ? () => Math.fround(f()) : f;
|
||||||
|
}
|
||||||
case 'BOOL': return () => rnd() < 0.5;
|
case 'BOOL': return () => rnd() < 0.5;
|
||||||
case 'CHAR': return () => (rnd() < 0.08 ? 'N' : pick(alphabet));
|
case 'CHAR': return () => (rnd() < 0.08 ? 'N' : pick(alphabet));
|
||||||
case 'STRING': return () => {
|
case 'STRING': return () => {
|
||||||
|
|||||||
+116
-9
@@ -34,6 +34,7 @@ import type { Term, Sort, TypeLike, CmpOp } from './logic.js';
|
|||||||
import { prove } from './solver.js';
|
import { prove } from './solver.js';
|
||||||
import { sourceOf } from './parse.js';
|
import { sourceOf } from './parse.js';
|
||||||
import { walkStatements, rootName, statementExprs, walkExpr } from './analyze.js';
|
import { walkStatements, rootName, statementExprs, walkExpr } from './analyze.js';
|
||||||
|
import { isSizedInt, isFloat32, intRange } from './lang.js';
|
||||||
|
|
||||||
/* ------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------ */
|
||||||
/* Types */
|
/* Types */
|
||||||
@@ -246,10 +247,41 @@ class Exec {
|
|||||||
|
|
||||||
fresh(name: string, sort: Sort): Term { return sym(name, sort); }
|
fresh(name: string, sort: Sort): Term { return sym(name, sort); }
|
||||||
|
|
||||||
|
/** The range of a sized whole-number type as a fact about a term: lowest <= v <= highest. */
|
||||||
|
rangeOf(type: TypeLike, v: Term): Term {
|
||||||
|
const [lo, hi] = intRange(type);
|
||||||
|
return and(cmp('<=', num(lo), v), cmp('<=', v, num(hi)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A value going into a sized type (a store, a parameter, a RETURN, a conversion, an operator's result). A whole
|
||||||
|
* number gets an obligation that it fits: the run stops right there when it does not, so from then on the
|
||||||
|
* range is a fact. A number going into a FLOAT32 is rounded: a constant is rounded here, exactly; anything
|
||||||
|
* else that is not already a FLOAT32 becomes TOFLOAT32(v), about which nothing is assumed.
|
||||||
|
*/
|
||||||
|
fit(type: TypeLike | undefined, v: Term, from: TypeLike | undefined, node: PNode, st: State, g: Term[], what: string, ctx: Ctx = {}): Term {
|
||||||
|
if (!type) return v;
|
||||||
|
if (isSizedInt(type)) {
|
||||||
|
if (isSizedInt(from) && (from as any).bits === (type as any).bits && (from as any).signed === (type as any).signed) return v;
|
||||||
|
if (!ctx.spec) {
|
||||||
|
this.obligation('overflow', node, `${what} fits ${(type as any).name}`, st, g, this.rangeOf(type, v));
|
||||||
|
st.facts.push(this.rangeOf(type, v));
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
if (isFloat32(type)) {
|
||||||
|
if (isFloat32(from)) return v;
|
||||||
|
if (v.k === 'num') return num(Math.fround(v.v), 'float');
|
||||||
|
return app('TOFLOAT32', [v], 'float');
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
/* ---------- expressions ---------- */
|
/* ---------- expressions ---------- */
|
||||||
tr(e: PNode, st: State, g: Term[] = [], ctx: Ctx = {}): Term {
|
tr(e: PNode, st: State, g: Term[] = [], ctx: Ctx = {}): Term {
|
||||||
switch (e.kind) {
|
switch (e.kind) {
|
||||||
case 'Num': return num(e.value, e.float ? 'float' : 'int');
|
case 'Num': return isFloat32(e.type) ? num(Math.fround(e.value), 'float') : num(e.value, e.float ? 'float' : 'int');
|
||||||
|
case 'TypeRef': return this.fresh(e.name, 'any');
|
||||||
case 'Str': return lit(e.value, 'string');
|
case 'Str': return lit(e.value, 'string');
|
||||||
case 'Char': return lit(e.value, 'char');
|
case 'Char': return lit(e.value, 'char');
|
||||||
case 'Bool': return e.value ? TRUE : FALSE;
|
case 'Bool': return e.value ? TRUE : FALSE;
|
||||||
@@ -257,7 +289,14 @@ class Exec {
|
|||||||
case 'Name': return this.name(e, st, ctx);
|
case 'Name': return this.name(e, st, ctx);
|
||||||
case 'Unary': {
|
case 'Unary': {
|
||||||
const a = this.tr(e.arg, st, g, ctx);
|
const a = this.tr(e.arg, st, g, ctx);
|
||||||
return e.op === 'NOT' ? not(a) : neg(a);
|
if (e.op === 'NOT') return not(a);
|
||||||
|
if (isFloat32(e.type)) return neg(a); // negation is exact in floating point
|
||||||
|
const r = neg(a);
|
||||||
|
if (isSizedInt(e.type) && !ctx.spec) {
|
||||||
|
this.obligation('overflow', e, `${sourceOf(e)} fits ${e.type.name}`, st, g, this.rangeOf(e.type, r));
|
||||||
|
st.facts.push(this.rangeOf(e.type, r));
|
||||||
|
}
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
case 'Binary': return this.binary(e, st, g, ctx);
|
case 'Binary': return this.binary(e, st, g, ctx);
|
||||||
case 'Chain': {
|
case 'Chain': {
|
||||||
@@ -321,10 +360,17 @@ class Exec {
|
|||||||
// again; comparisons of FLOAT values stay exact.
|
// again; comparisons of FLOAT values stay exact.
|
||||||
if (['+', '-', '*', '/', '^'].includes(op) && (op === '/' || sortOf(e.type) === 'float' || e.left.type?.k === 'FLOAT' || e.right.type?.k === 'FLOAT')) {
|
if (['+', '-', '*', '/', '^'].includes(op) && (op === '/' || sortOf(e.type) === 'float' || e.left.type?.k === 'FLOAT' || e.right.type?.k === 'FLOAT')) {
|
||||||
if (!ctx.spec) this.floatArith = true;
|
if (!ctx.spec) this.floatArith = true;
|
||||||
return app(`FLOAT${op}`, [l, r], 'float');
|
// FLOAT32 arithmetic is a different function: the same values rounded to single precision
|
||||||
|
return app(isFloat32(e.type) ? `F32${op}` : `FLOAT${op}`, [l, r], 'float');
|
||||||
}
|
}
|
||||||
if (!ctx.spec && ['+', '-', '*', '^'].includes(op) && sortOf(e.type) === 'int') this.intArith = e.line ?? e.left?.line ?? true;
|
if (!ctx.spec && ['+', '-', '*', '^'].includes(op) && sortOf(e.type) === 'int' && !isSizedInt(e.type)) this.intArith = e.line ?? e.left?.line ?? true;
|
||||||
return arith(op, l, r, sortOf(e.type));
|
const res = arith(op, l, r, sortOf(e.type));
|
||||||
|
// a sized whole number: the exact result, and an obligation that it fits (the run stops where it does not)
|
||||||
|
if (isSizedInt(e.type) && !ctx.spec && ['+', '-', '*', '^', 'DIV', 'MOD'].includes(op)) {
|
||||||
|
this.obligation('overflow', e, `${sourceOf(e)} fits ${e.type.name}`, st, g, this.rangeOf(e.type, res));
|
||||||
|
st.facts.push(this.rangeOf(e.type, res));
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
membership(x: Term, rightNode: PNode, st: State, g: Term[], ctx: Ctx): Term {
|
membership(x: Term, rightNode: PNode, st: State, g: Term[], ctx: Ctx): Term {
|
||||||
@@ -383,8 +429,44 @@ class Exec {
|
|||||||
}
|
}
|
||||||
const args: Term[] = e.args.map((a: PNode) => this.tr(a, st, g, ctx));
|
const args: Term[] = e.args.map((a: PNode) => this.tr(a, st, g, ctx));
|
||||||
const s = sortOf(e.type);
|
const s = sortOf(e.type);
|
||||||
|
if (e.convert) {
|
||||||
|
const from = e.args[0]?.type;
|
||||||
|
const x = args[0] ?? this.fresh('?', 'any');
|
||||||
|
if (isSizedInt(e.type)) return this.fit(e.type, from?.k === 'FLOAT' ? app('TRUNC', [x], 'int') : x, from?.k === 'FLOAT' ? undefined : from, e, st, g, sourceOf(e), ctx);
|
||||||
|
if (isFloat32(e.type)) return this.fit(e.type, x, from, e, st, g, sourceOf(e), ctx);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
if (e.builtin) {
|
if (e.builtin) {
|
||||||
const [x, y, z] = args;
|
const [x, y, z] = args;
|
||||||
|
// A built-in the run stops on for some arguments has an obligation that the arguments are allowed
|
||||||
|
// (kind 'argument'); without USE IEEE these are the FLOAT functions with a domain.
|
||||||
|
const domain = (goal: Term, label: string) => { if (!ctx.spec) this.obligation('argument', e, label, st, g, goal); };
|
||||||
|
const call = `${e.builtin}(${e.args.map(sourceOf).join(', ')})`;
|
||||||
|
const zero = num(0, x?.t === 'float' ? 'float' : 'int');
|
||||||
|
switch (e.builtin) {
|
||||||
|
case 'SQRT': domain(cmp('>=', x, zero), `${call} takes a number of 0 or more`); return app('SQRT', args, s);
|
||||||
|
case 'LOG': case 'LOG2': case 'LOG10': domain(cmp('>', x, zero), `${call} takes a number above 0`); return app(e.builtin, args, s);
|
||||||
|
case 'ASIN': case 'ACOS': domain(and(cmp('<=', num(-1), x), cmp('<=', x, num(1))), `${call} takes a number in [-1, 1]`); return app(e.builtin, args, s);
|
||||||
|
case 'LOG1P': domain(cmp('>', x, num(-1)), `${call} takes a number above -1`); return app(e.builtin, args, s);
|
||||||
|
case 'CLAMP': domain(cmp('<=', y, z), `${call} has its low end at or below its high end`); return ite(cmp('<', x, y), y, ite(cmp('>', x, z), z, x));
|
||||||
|
case 'SHIFT_LEFT': case 'SHIFT_RIGHT': {
|
||||||
|
domain(cmp('>=', y, num(0)), `${call} moves bits by 0 or more places`);
|
||||||
|
const r = app(e.builtin, args, s);
|
||||||
|
if (e.builtin === 'SHIFT_LEFT') this.fitResult(e, r, st, g, ctx);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
case 'LCM': { const r = app('LCM', args, s); this.fitResult(e, r, st, g, ctx); return r; }
|
||||||
|
case 'MAX_VALUE': case 'MIN_VALUE': case 'EPSILON': {
|
||||||
|
const ty = e.args[0]?.kind === 'TypeRef' ? e.type : null;
|
||||||
|
if (!ty) return app(e.builtin, args, s);
|
||||||
|
if (e.builtin === 'EPSILON') return num(isFloat32(ty) ? 2 ** -23 : Number.EPSILON, 'float');
|
||||||
|
if (ty.k === 'INTEGER') { const [lo, hi] = intRange(ty); return num(e.builtin === 'MAX_VALUE' ? hi : lo, 'int'); }
|
||||||
|
const m = isFloat32(ty) ? 3.4028234663852886e38 : Number.MAX_VALUE;
|
||||||
|
return num(e.builtin === 'MAX_VALUE' ? m : -m, 'float');
|
||||||
|
}
|
||||||
|
case 'SUM_OF': case 'DOT': if (isSizedInt(e.type)) { const r = app(e.builtin, args, s); this.fitResult(e, r, st, g, ctx); return r; } break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
switch (e.builtin) {
|
switch (e.builtin) {
|
||||||
case 'LENGTH': return length(x);
|
case 'LENGTH': return length(x);
|
||||||
case 'ROWS': return length(x);
|
case 'ROWS': return length(x);
|
||||||
@@ -433,6 +515,7 @@ class Exec {
|
|||||||
}
|
}
|
||||||
const sub = e.sub;
|
const sub = e.sub;
|
||||||
if (!sub) return this.fresh(e.name, s);
|
if (!sub) return this.fresh(e.name, s);
|
||||||
|
sub.params.forEach((p: PNode, i: number) => { if (args[i] && e.args[i]) args[i] = this.fit(p.type, args[i], e.args[i].type, e.args[i], st, g, `the value for ${p.name}`, ctx); });
|
||||||
const result = app(`FN:${sub.name}`, args, s);
|
const result = app(`FN:${sub.name}`, args, s);
|
||||||
const paramEnv: Env = new Map(sub.params.map((p: PNode, i: number): [string, Val] => [up(p.name), args[i]]));
|
const paramEnv: Env = new Map(sub.params.map((p: PNode, i: number): [string, Val] => [up(p.name), args[i]]));
|
||||||
if (!ctx.spec) {
|
if (!ctx.spec) {
|
||||||
@@ -448,6 +531,13 @@ class Exec {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** A built-in's sized whole-number result: an obligation that it fits, then the range as a fact. */
|
||||||
|
fitResult(e: PNode, r: Term, st: State, g: Term[], ctx: Ctx): void {
|
||||||
|
if (!isSizedInt(e.type) || ctx.spec) return;
|
||||||
|
this.obligation('overflow', e, `${sourceOf(e)} fits ${e.type.name}`, st, g, this.rangeOf(e.type, r));
|
||||||
|
st.facts.push(this.rangeOf(e.type, r));
|
||||||
|
}
|
||||||
|
|
||||||
/** A recursive call has to make the DECREASES measure smaller, and keep it at 0 or above. */
|
/** A recursive call has to make the DECREASES measure smaller, and keep it at 0 or above. */
|
||||||
recursion(sub: Routine, at: PNode, args: Term[], st: State, g: Term[]): void {
|
recursion(sub: Routine, at: PNode, args: Term[], st: State, g: Term[]): void {
|
||||||
const cur = this.unit.sub;
|
const cur = this.unit.sub;
|
||||||
@@ -496,14 +586,16 @@ class Exec {
|
|||||||
return [n];
|
return [n];
|
||||||
}
|
}
|
||||||
case 'Assign': {
|
case 'Assign': {
|
||||||
const v = this.tr(s.value, st, []);
|
const v = this.fit(s.target.type, this.tr(s.value, st, []), s.value.type, s.value, st, [], sourceOf(s.value));
|
||||||
const n = st.clone();
|
const n = st.clone();
|
||||||
this.store(s.target, v, n, st);
|
this.store(s.target, v, n, st);
|
||||||
return [n];
|
return [n];
|
||||||
}
|
}
|
||||||
case 'Input': {
|
case 'Input': {
|
||||||
|
const raw = this.fresh(rootName(s.target) || 'input', sortOf(s.target.type));
|
||||||
|
const v = this.fit(s.target.type, raw, undefined, s, st, [], `the value INPUT reads into ${sourceOf(s.target)}`);
|
||||||
const n = st.clone();
|
const n = st.clone();
|
||||||
this.store(s.target, this.fresh(rootName(s.target) || 'input', sortOf(s.target.type)), n, st);
|
this.store(s.target, v, n, st);
|
||||||
return [n];
|
return [n];
|
||||||
}
|
}
|
||||||
case 'Output': case 'ExprStmt': {
|
case 'Output': case 'ExprStmt': {
|
||||||
@@ -519,7 +611,7 @@ class Exec {
|
|||||||
case 'Break': ctx.breaks?.push(st); return [];
|
case 'Break': ctx.breaks?.push(st); return [];
|
||||||
case 'Continue': ctx.continues?.push(st); return [];
|
case 'Continue': ctx.continues?.push(st); return [];
|
||||||
case 'Return': {
|
case 'Return': {
|
||||||
const v = s.value ? this.tr(s.value, st, []) : null;
|
const v = s.value ? this.fit(this.unit.sub?.returns, this.tr(s.value, st, []), s.value.type, s.value, st, [], sourceOf(s.value)) : null;
|
||||||
this.ensure(s, st, v);
|
this.ensure(s, st, v);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
@@ -595,6 +687,7 @@ class Exec {
|
|||||||
const sub = s.sub;
|
const sub = s.sub;
|
||||||
if (!sub) return st;
|
if (!sub) return st;
|
||||||
const args: Term[] = s.args.map((a: PNode) => this.tr(a, st, []));
|
const args: Term[] = s.args.map((a: PNode) => this.tr(a, st, []));
|
||||||
|
sub.params.forEach((p: PNode, i: number) => { if (args[i] && s.args[i] && p.mode !== 'BYREF') args[i] = this.fit(p.type, args[i], s.args[i].type, s.args[i], st, [], `the value for ${p.name}`); });
|
||||||
const paramEnv: Env = new Map(sub.params.map((p: PNode, i: number): [string, Val] => [up(p.name), args[i]]));
|
const paramEnv: Env = new Map(sub.params.map((p: PNode, i: number): [string, Val] => [up(p.name), args[i]]));
|
||||||
for (const r of sub.requires) {
|
for (const r of sub.requires) {
|
||||||
this.obligation('require', s, `this call to ${sub.name} meets its REQUIRE, ${sourceOf(r.expr)}`, st, [], this.tr(r.expr, st, [], { paramEnv, spec: true }), { target: r.line });
|
this.obligation('require', s, `this call to ${sub.name} meets its REQUIRE, ${sourceOf(r.expr)}`, st, [], this.tr(r.expr, st, [], { paramEnv, spec: true }), { target: r.line });
|
||||||
@@ -668,6 +761,9 @@ class Exec {
|
|||||||
n.env.set(k, fresh);
|
n.env.set(k, fresh);
|
||||||
// an ARRAY keeps its dimensions, however its contents change
|
// an ARRAY keeps its dimensions, however its contents change
|
||||||
if (known && known.t === 'array') n.facts.push(cmp('==', length(fresh), length(known)), cmp('==', columns(fresh), columns(known)));
|
if (known && known.t === 'array') n.facts.push(cmp('==', length(fresh), length(known)), cmp('==', columns(fresh), columns(known)));
|
||||||
|
// a sized whole number stays in its range: every store into it is checked, and the run stops otherwise
|
||||||
|
const ty = this.types.get(k);
|
||||||
|
if (isSizedInt(ty)) n.facts.push(this.rangeOf(ty!, fresh));
|
||||||
}
|
}
|
||||||
n.trail.push(`the top of the loop on line ${loopLine}`);
|
n.trail.push(`the top of the loop on line ${loopLine}`);
|
||||||
return n;
|
return n;
|
||||||
@@ -948,6 +1044,8 @@ class Exec {
|
|||||||
const v = this.fresh(p.name, sortOf(p.type));
|
const v = this.fresh(p.name, sortOf(p.type));
|
||||||
st.env.set(up(p.name), v);
|
st.env.set(up(p.name), v);
|
||||||
this.entryEnv.set(up(p.name), v);
|
this.entryEnv.set(up(p.name), v);
|
||||||
|
// a parameter of a sized type holds a value of that type: the call checked it on the way in
|
||||||
|
if (isSizedInt(p.type)) st.facts.push(this.rangeOf(p.type, v));
|
||||||
}
|
}
|
||||||
for (const r of u.requires || []) st.facts.push(this.tr(r.expr, st, [], { spec: true }));
|
for (const r of u.requires || []) st.facts.push(this.tr(r.expr, st, [], { spec: true }));
|
||||||
const ends = this.run(u.body, [st], { breaks: [], continues: [] });
|
const ends = this.run(u.body, [st], { breaks: [], continues: [] });
|
||||||
@@ -1052,7 +1150,7 @@ export function verifyUnit(program: Program, unit: Unit, options: VerifyOptions
|
|||||||
return { name: unit.name, kind: unit.kind, line: unit.node?.line ?? 1, obligations, inferred: ex.inferred, notes: ex.notes, partial: ex.partial };
|
return { name: unit.name, kind: unit.kind, line: unit.node?.line ?? 1, obligations, inferred: ex.inferred, notes: ex.notes, partial: ex.partial };
|
||||||
}
|
}
|
||||||
|
|
||||||
const KIND_ORDER = ['require', 'index', 'key', 'slice', 'divide', 'argument', 'invariant-init', 'invariant-keep', 'decreases-bound', 'decreases-step', 'terminates', 'assert', 'ensure'];
|
const KIND_ORDER = ['require', 'index', 'key', 'slice', 'divide', 'overflow', 'argument', 'invariant-init', 'invariant-keep', 'decreases-bound', 'decreases-step', 'terminates', 'assert', 'ensure'];
|
||||||
|
|
||||||
export function verify(program: Program, options: VerifyOptions = {}): Report {
|
export function verify(program: Program, options: VerifyOptions = {}): Report {
|
||||||
const units: UnitReport[] = [];
|
const units: UnitReport[] = [];
|
||||||
@@ -1060,6 +1158,14 @@ export function verify(program: Program, options: VerifyOptions = {}): Report {
|
|||||||
units.push(verifyUnit(program, { name: sub.name, kind: sub.kind, node: sub.node, body: sub.node.body, params: sub.params, symbols: sub.symbols || [], requires: sub.requires, ensures: sub.ensures, sub }, options));
|
units.push(verifyUnit(program, { name: sub.name, kind: sub.kind, node: sub.node, body: sub.node.body, params: sub.params, symbols: sub.symbols || [], requires: sub.requires, ensures: sub.ensures, sub }, options));
|
||||||
}
|
}
|
||||||
if (program.main.body.length) units.push(verifyUnit(program, { name: 'main program', kind: 'main', node: null, body: program.main.body, params: [], symbols: program.main.symbols, requires: [], ensures: [] }, options));
|
if (program.main.body.length) units.push(verifyUnit(program, { name: 'main program', kind: 'main', node: null, body: program.main.body, params: [], symbols: program.main.symbols, requires: [], ensures: [] }, options));
|
||||||
|
// USE IEEE lets NaN and infinities into the run, and the prover's reasoning about numbers (X == X; X < Y or
|
||||||
|
// X >= Y) does not hold for them, so nothing in such a program is called proved.
|
||||||
|
if ((program as any).ieee) {
|
||||||
|
for (const u of units) {
|
||||||
|
u.notes.push({ line: 1, kind: 'ieee', text: 'This program says USE IEEE, so a FLOAT may be NaN or infinite, and the prover\'s reasoning about numbers does not cover those values. Nothing here is marked proved; the obligations are listed so you can see what each line relies on.' });
|
||||||
|
for (const o of u.obligations) if (o.status === 'proved') (o as any).status = 'unknown';
|
||||||
|
}
|
||||||
|
}
|
||||||
const all = units.flatMap((u) => u.obligations);
|
const all = units.flatMap((u) => u.obligations);
|
||||||
return { units, counts: { proved: all.filter((o) => o.status === 'proved').length, unknown: all.filter((o) => o.status !== 'proved').length, total: all.length } };
|
return { units, counts: { proved: all.filter((o) => o.status === 'proved').length, unknown: all.filter((o) => o.status !== 'proved').length, total: all.length } };
|
||||||
}
|
}
|
||||||
@@ -1069,6 +1175,7 @@ export const OBLIGATION_WORDS: Record<string, string> = {
|
|||||||
key: 'Key present',
|
key: 'Key present',
|
||||||
slice: 'Slice in range',
|
slice: 'Slice in range',
|
||||||
divide: 'No division by zero',
|
divide: 'No division by zero',
|
||||||
|
overflow: 'Fits its type',
|
||||||
argument: 'Argument allowed',
|
argument: 'Argument allowed',
|
||||||
require: 'Precondition met',
|
require: 'Precondition met',
|
||||||
ensure: 'Postcondition holds',
|
ensure: 'Postcondition holds',
|
||||||
|
|||||||
+118
-7
@@ -49,10 +49,24 @@ export const USED_STDLIB: Readonly<Record<string, string>> = {
|
|||||||
sqrt: 'std.math', exp: 'std.math', log: 'std.math', log2: 'std.math', log10: 'std.math',
|
sqrt: 'std.math', exp: 'std.math', log: 'std.math', log2: 'std.math', log10: 'std.math',
|
||||||
sin: 'std.math', cos: 'std.math', tan: 'std.math', atan2: 'std.math', pi: 'std.math',
|
sin: 'std.math', cos: 'std.math', tan: 'std.math', atan2: 'std.math', pi: 'std.math',
|
||||||
floor: 'std.math', ceil: 'std.math', comb: 'std.math',
|
floor: 'std.math', ceil: 'std.math', comb: 'std.math',
|
||||||
|
asin: 'std.math', acos: 'std.math', atan: 'std.math', sinh: 'std.math', cosh: 'std.math', tanh: 'std.math',
|
||||||
|
hypot: 'std.math', cbrt: 'std.math', expm1: 'std.math', log1p: 'std.math', trunc: 'std.math', copysign: 'std.math',
|
||||||
|
fma: 'std.math', isnan: 'std.math', isinf: 'std.math', isfinite: 'std.math', inf: 'std.math', nan: 'std.math',
|
||||||
|
ulp: 'std.math', nextafter: 'std.math', gcd: 'std.math', lcm: 'std.math',
|
||||||
random_float64: 'std.random', random_si64: 'std.random',
|
random_float64: 'std.random', random_si64: 'std.random',
|
||||||
};
|
};
|
||||||
/** Names the translator writes that Mojo provides without an import. */
|
/** Names the translator writes that Mojo provides without an import. */
|
||||||
export const USED_PRELUDE: readonly string[] = ['Int', 'Float64', 'String', 'Bool', 'List', 'Dict', 'print', 'range', 'len', 'min', 'max', 'abs', 'round', 'input', 'atol', 'atof', 'chr', 'ord', 'Error'];
|
export const USED_PRELUDE: readonly string[] = ['Int', 'Float64', 'String', 'Bool', 'List', 'Dict', 'print', 'range', 'len', 'min', 'max', 'abs', 'round', 'input', 'atol', 'atof', 'chr', 'ord', 'Error',
|
||||||
|
'Int8', 'Int16', 'Int32', 'Int64', 'UInt8', 'UInt16', 'UInt32', 'UInt64', 'Float32', 'DType'];
|
||||||
|
|
||||||
|
/** The Mojo type for a pseudo number type: INT8 is Int8, FLOAT32 is Float32, INTEGER is Int, FLOAT and FLOAT64 are Float64. */
|
||||||
|
export function mojoNumber(t: any): string | null {
|
||||||
|
if (!t) return null;
|
||||||
|
if (t.k === 'INTEGER') return t.bits ? `${t.signed ? 'Int' : 'UInt'}${t.bits}` : 'Int';
|
||||||
|
if (t.k === 'FLOAT') return t.bits === 32 ? 'Float32' : 'Float64';
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const isSized = (t: any): boolean => !!t && ((t.k === 'INTEGER' && !!t.bits) || (t.k === 'FLOAT' && t.bits === 32));
|
||||||
|
|
||||||
const up = (s: unknown): string => String(s).toUpperCase();
|
const up = (s: unknown): string => String(s).toUpperCase();
|
||||||
|
|
||||||
@@ -71,6 +85,7 @@ const RESERVED = new Set([
|
|||||||
'and', 'or', 'not', 'in', 'is', 'if', 'elif', 'else', 'for', 'while', 'break', 'continue', 'return', 'pass', 'del', 'assert', 'class', 'lambda', 'global', 'yield', 'match', 'case', 'self',
|
'and', 'or', 'not', 'in', 'is', 'if', 'elif', 'else', 'for', 'while', 'break', 'continue', 'return', 'pass', 'del', 'assert', 'class', 'lambda', 'global', 'yield', 'match', 'case', 'self',
|
||||||
'len', 'min', 'max', 'abs', 'round', 'print', 'range', 'input', 'sum', 'type', 'list', 'str', 'int', 'float', 'set', 'dict', 'chr', 'ord', 'pow', 'sort', 'sorted', 'copy', 'main',
|
'len', 'min', 'max', 'abs', 'round', 'print', 'range', 'input', 'sum', 'type', 'list', 'str', 'int', 'float', 'set', 'dict', 'chr', 'ord', 'pow', 'sort', 'sorted', 'copy', 'main',
|
||||||
'atol', 'atof', 'floor', 'ceil', 'sqrt', 'exp', 'log', 'log2', 'log10', 'sin', 'cos', 'tan', 'atan2', 'pi', 'comb', 'true', 'false', 'none',
|
'atol', 'atof', 'floor', 'ceil', 'sqrt', 'exp', 'log', 'log2', 'log10', 'sin', 'cos', 'tan', 'atan2', 'pi', 'comb', 'true', 'false', 'none',
|
||||||
|
'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'float32', 'float64', 'dtype',
|
||||||
...Object.keys(USED_STDLIB),
|
...Object.keys(USED_STDLIB),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -253,8 +268,7 @@ class Gen {
|
|||||||
type(t: any): string {
|
type(t: any): string {
|
||||||
if (!t) this.gap('a value whose type is not known');
|
if (!t) this.gap('a value whose type is not known');
|
||||||
switch (t.k) {
|
switch (t.k) {
|
||||||
case 'INTEGER': return 'Int';
|
case 'INTEGER': case 'FLOAT': return mojoNumber(t)!;
|
||||||
case 'FLOAT': return 'Float64';
|
|
||||||
case 'CHAR': case 'STRING': return 'String';
|
case 'CHAR': case 'STRING': return 'String';
|
||||||
case 'BOOL': return 'Bool';
|
case 'BOOL': return 'Bool';
|
||||||
case 'ARRAY': if (t.dims === 2) return `List[List[${this.type(t.of)}]]`; return `List[${this.type(t.of)}]`;
|
case 'ARRAY': if (t.dims === 2) return `List[List[${this.type(t.of)}]]`; return `List[${this.type(t.of)}]`;
|
||||||
@@ -286,10 +300,17 @@ class Gen {
|
|||||||
return `${text}.copy()`;
|
return `${text}.copy()`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** An INTEGER value going where a FLOAT belongs becomes a Float64, as the notation widens it. */
|
/**
|
||||||
|
* A value going where another number type belongs, converted as the notation converts it: an INTEGER into a
|
||||||
|
* FLOAT becomes Float64(...) (Float32(...) for a FLOAT32), an INTEGER into a sized type becomes Int32(...), a
|
||||||
|
* FLOAT32 into a FLOAT becomes Float64(...). A literal needs nothing: Mojo gives it the type it lands in.
|
||||||
|
*/
|
||||||
widened(target: any, e: any, text: string): string {
|
widened(target: any, e: any, text: string): string {
|
||||||
if (target?.k === 'FLOAT' && e?.type?.k === 'INTEGER' && e.kind !== 'Num') return `Float64(${text})`;
|
if (!target || !e?.type || e.kind === 'Num') return text;
|
||||||
return text;
|
const want = mojoNumber(target); const have = mojoNumber(e.type);
|
||||||
|
if (!want || !have || want === have) return text;
|
||||||
|
if (isSized(target) && target.k === 'INTEGER') this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
|
||||||
|
return `${want}(${text})`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------- program ---------- */
|
/* ---------- program ---------- */
|
||||||
@@ -650,6 +671,7 @@ class Gen {
|
|||||||
case 'Str': case 'Char': return JSON.stringify(e.value);
|
case 'Str': case 'Char': return JSON.stringify(e.value);
|
||||||
case 'Bool': return e.value ? 'True' : 'False';
|
case 'Bool': return e.value ? 'True' : 'False';
|
||||||
case 'Blank': return this.gap(`a blank to fill in: <${e.text}>`);
|
case 'Blank': return this.gap(`a blank to fill in: <${e.text}>`);
|
||||||
|
case 'TypeRef': return e.name === 'INTEGER' ? 'Int' : e.name === 'FLOAT' || e.name === 'FLOAT64' ? 'Float64' : e.name === 'FLOAT32' ? 'Float32' : e.name.startsWith('UINT') ? `UInt${e.name.slice(4)}` : `Int${e.name.slice(3)}`;
|
||||||
case 'Name':
|
case 'Name':
|
||||||
if (e.sym?.kind === 'const') return this.constName(e.name);
|
if (e.sym?.kind === 'const') return this.constName(e.name);
|
||||||
if (e.sym?.kind === 'define') return `${this.name(e.name)}()`;
|
if (e.sym?.kind === 'define') return `${this.name(e.name)}()`;
|
||||||
@@ -659,7 +681,12 @@ class Gen {
|
|||||||
case 'Binary': {
|
case 'Binary': {
|
||||||
const map: Record<string, string> = { AND: 'and', OR: 'or', DIV: '//', MOD: '%', '^': '**', '&': '+', IN: 'in' };
|
const map: Record<string, string> = { AND: 'and', OR: 'or', DIV: '//', MOD: '%', '^': '**', '&': '+', IN: 'in' };
|
||||||
if (e.op === '/') this.note('divide', 'In the notation, dividing by zero stops the program. In Mojo a Float64 division by zero gives inf or nan and the program goes on.');
|
if (e.op === '/') this.note('divide', 'In the notation, dividing by zero stops the program. In Mojo a Float64 division by zero gives inf or nan and the program goes on.');
|
||||||
|
if (e.op === '/' && mojoNumber(e.type) === 'Float32') {
|
||||||
|
const side = (x: any) => (x.kind === 'Num' || mojoNumber(x.type) === 'Float32' ? this.sideExpr(x, e, x === e.left ? 'l' : 'r') : `Float32(${this.expr(x)})`);
|
||||||
|
return `${side(e.left)} / ${side(e.right)}`;
|
||||||
|
}
|
||||||
if (e.op === '/') return `Float64(${this.expr(e.left)}) / Float64(${this.expr(e.right)})`;
|
if (e.op === '/') return `Float64(${this.expr(e.left)}) / Float64(${this.expr(e.right)})`;
|
||||||
|
if (isSized(e.type) && e.type.k === 'INTEGER' && ['+', '-', '*', '^', 'DIV', 'MOD'].includes(e.op)) this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
|
||||||
if (e.op === 'IN' && e.right.kind === 'Range') return `${this.expr(e.right.lo)} <= ${P(e.left)} < ${this.expr(e.right.hi)}`;
|
if (e.op === 'IN' && e.right.kind === 'Range') return `${this.expr(e.right.lo)} <= ${P(e.left)} < ${this.expr(e.right.hi)}`;
|
||||||
if (e.op === '&') {
|
if (e.op === '&') {
|
||||||
const side = (x: any) => (x.type?.k === 'CHAR' || x.type?.k === 'STRING' ? P(x) : `String(${this.expr(x)})`);
|
const side = (x: any) => (x.type?.k === 'CHAR' || x.type?.k === 'STRING' ? P(x) : `String(${this.expr(x)})`);
|
||||||
@@ -720,7 +747,17 @@ class Gen {
|
|||||||
const BIND: Record<string, number> = { OR: 1, AND: 2, '==': 4, '!=': 4, '<': 4, '<=': 4, '>': 4, '>=': 4, IN: 4, '&': 5, '+': 6, '-': 6, '*': 7, '/': 7, DIV: 7, MOD: 7, '^': 9 };
|
const BIND: Record<string, number> = { OR: 1, AND: 2, '==': 4, '!=': 4, '<': 4, '<=': 4, '>': 4, '>=': 4, IN: 4, '&': 5, '+': 6, '-': 6, '*': 7, '/': 7, DIV: 7, MOD: 7, '^': 9 };
|
||||||
let s = this.expr(x);
|
let s = this.expr(x);
|
||||||
const other = side === 'l' ? parent.right : parent.left;
|
const other = side === 'l' ? parent.right : parent.left;
|
||||||
if (['+', '-', '*', '==', '!=', '<', '<=', '>', '>='].includes(parent.op) && x.type?.k === 'INTEGER' && other?.type?.k === 'FLOAT' && x.kind !== 'Num') s = `Float64(${s})`;
|
if (['+', '-', '*', '==', '!=', '<', '<=', '>', '>=', 'DIV', 'MOD', '^'].includes(parent.op) && x.kind !== 'Num' && other?.type) {
|
||||||
|
// two sides of different number types meet as the notation has them meet
|
||||||
|
const mine = mojoNumber(x.type); const theirs = mojoNumber(other.type);
|
||||||
|
const compare = ['==', '!=', '<', '<=', '>', '>='].includes(parent.op);
|
||||||
|
if (mine && theirs && mine !== theirs) {
|
||||||
|
if (x.type.k === 'INTEGER' && other.type.k === 'FLOAT') s = `${theirs}(${s})`;
|
||||||
|
else if (x.type.k === 'INTEGER' && other.type.k === 'INTEGER' && !x.type.bits) s = `${theirs}(${s})`;
|
||||||
|
else if (compare && x.type.k === 'FLOAT' && other.type.k === 'FLOAT' && mine === 'Float32') s = `Float64(${s})`;
|
||||||
|
else if (compare && x.type.k === 'INTEGER' && other.type.k === 'INTEGER') s = `Int(${s})`;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (x.kind === 'Chain' || x.kind === 'Quant') return `(${s})`;
|
if (x.kind === 'Chain' || x.kind === 'Quant') return `(${s})`;
|
||||||
if (x.kind === 'Unary' && x.op === 'NOT' && BIND[parent.op] > 3) return `(${s})`;
|
if (x.kind === 'Unary' && x.op === 'NOT' && BIND[parent.op] > 3) return `(${s})`;
|
||||||
if (x.kind !== 'Binary') return s;
|
if (x.kind !== 'Binary') return s;
|
||||||
@@ -729,6 +766,18 @@ class Gen {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** A value as the Mojo number type T: unchanged when it already is one, or a literal; converted otherwise. */
|
||||||
|
as(T: string, node: any, text: string): string {
|
||||||
|
if (mojoNumber(node?.type) === T) return text;
|
||||||
|
return `${T}(${text})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** IS_CLOSE's four values, as Float64 unless they are already FLOATs of one precision. */
|
||||||
|
args4(e: any): string[] {
|
||||||
|
const f32 = e.args.slice(0, 2).every((x: any) => mojoNumber(x.type) === 'Float32');
|
||||||
|
return e.args.map((x: any) => (x.kind === 'Num' || mojoNumber(x.type) === (f32 ? 'Float32' : 'Float64') ? this.expr(x) : `${f32 ? 'Float32' : 'Float64'}(${this.expr(x)})`));
|
||||||
|
}
|
||||||
|
|
||||||
/** A built-in that is a short loop: write it before the statement and name its result. */
|
/** A built-in that is a short loop: write it before the statement and name its result. */
|
||||||
loopResult(stem: string, type: string, init: string, lines: Array<[number, string]>): string {
|
loopResult(stem: string, type: string, init: string, lines: Array<[number, string]>): string {
|
||||||
const t = this.fresh(stem);
|
const t = this.fresh(stem);
|
||||||
@@ -738,6 +787,13 @@ class Gen {
|
|||||||
|
|
||||||
call(e: any): string {
|
call(e: any): string {
|
||||||
if (e.isOld) return this.gap('OLD(...) outside a contract');
|
if (e.isOld) return this.gap('OLD(...) outside a contract');
|
||||||
|
if (e.convert) {
|
||||||
|
const to = mojoNumber(e.type)!;
|
||||||
|
if (e.type.k === 'INTEGER') this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
|
||||||
|
const arg = e.args[0];
|
||||||
|
// a FLOAT literal becomes a Float64 first, so the conversion is from a runtime value, as in the notation
|
||||||
|
return `${to}(${arg.kind === 'Num' && arg.float ? `Float64(${this.expr(arg)})` : this.expr(arg)})`;
|
||||||
|
}
|
||||||
if (!e.builtin) {
|
if (!e.builtin) {
|
||||||
const flags = this.varParams.get(key(e.name)) || [];
|
const flags = this.varParams.get(key(e.name)) || [];
|
||||||
const args = e.args.map((a: any, i: number) => this.callArg(a, e.sub?.params?.[i], flags[i]));
|
const args = e.args.map((a: any, i: number) => this.callArg(a, e.sub?.params?.[i], flags[i]));
|
||||||
@@ -808,6 +864,7 @@ class Gen {
|
|||||||
}
|
}
|
||||||
case 'SUM_OF': {
|
case 'SUM_OF': {
|
||||||
const x = this.fresh('x');
|
const x = this.fresh('x');
|
||||||
|
if (isSized(e.type)) return this.loopResult('total', this.type(e.type), '0', [[0, `for ${x} in ${a}:`], [1, `$T += ${x}`]]);
|
||||||
const zero = e.type?.k === 'FLOAT' ? '0.0' : '0';
|
const zero = e.type?.k === 'FLOAT' ? '0.0' : '0';
|
||||||
return this.loopResult('total', '', zero, [[0, `for ${x} in ${a}:`], [1, `$T += ${x}`]]);
|
return this.loopResult('total', '', zero, [[0, `for ${x} in ${a}:`], [1, `$T += ${x}`]]);
|
||||||
}
|
}
|
||||||
@@ -818,6 +875,7 @@ class Gen {
|
|||||||
}
|
}
|
||||||
case 'DOT': {
|
case 'DOT': {
|
||||||
const i = this.fresh('i');
|
const i = this.fresh('i');
|
||||||
|
if (isSized(e.type)) return this.loopResult('dot', this.type(e.type), '0', [[0, `for ${i} in range(len(${a})):`], [1, `$T += ${a}[${i}] * ${b}[${i}]`]]);
|
||||||
return this.loopResult('dot', '', e.type?.k === 'FLOAT' ? '0.0' : '0', [[0, `for ${i} in range(len(${a})):`], [1, `$T += ${a}[${i}] * ${b}[${i}]`]]);
|
return this.loopResult('dot', '', e.type?.k === 'FLOAT' ? '0.0' : '0', [[0, `for ${i} in range(len(${a})):`], [1, `$T += ${a}[${i}] * ${b}[${i}]`]]);
|
||||||
}
|
}
|
||||||
case 'VARIANCE_OF': case 'STDEV_OF': {
|
case 'VARIANCE_OF': case 'STDEV_OF': {
|
||||||
@@ -864,6 +922,59 @@ class Gen {
|
|||||||
this.hoist([[0, `sort(${items})`]]);
|
this.hoist([[0, `sort(${items})`]]);
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
// ---- numerical work
|
||||||
|
case 'ASIN': return math('asin');
|
||||||
|
case 'ACOS': return math('acos');
|
||||||
|
case 'ATAN': return math('atan');
|
||||||
|
case 'SINH': return math('sinh');
|
||||||
|
case 'COSH': return math('cosh');
|
||||||
|
case 'TANH': return math('tanh');
|
||||||
|
case 'CBRT': return math('cbrt');
|
||||||
|
case 'EXPM1': return math('expm1');
|
||||||
|
case 'LOG1P': return math('log1p');
|
||||||
|
case 'HYPOT': this.need('hypot'); return `hypot(Float64(${a}), Float64(${b}))`;
|
||||||
|
case 'POW': return `Float64(${a}) ** Float64(${b})`;
|
||||||
|
case 'COPYSIGN': this.need('copysign'); return `copysign(Float64(${a}), Float64(${b}))`;
|
||||||
|
case 'E': return '2.718281828459045';
|
||||||
|
case 'TRUNC': this.need('trunc'); return `trunc(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
|
||||||
|
case 'SIGN': { const T = this.type(e.type); return `(${T}(1) if ${a} > 0 else (${T}(-1) if ${a} < 0 else ${T}(0)))`; }
|
||||||
|
case 'CLAMP': return `min(max(${a}, ${b}), ${c})`;
|
||||||
|
case 'FMA': { const T = mojoNumber(e.type)!; this.need('fma'); return `fma(${this.as(T, e.args[0], a)}, ${this.as(T, e.args[1], b)}, ${this.as(T, e.args[2], c)})`; }
|
||||||
|
case 'IS_NAN': this.need('isnan'); return `isnan(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
|
||||||
|
case 'IS_INF': this.need('isinf'); return `isinf(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
|
||||||
|
case 'IS_FINITE': this.need('isfinite'); return `isfinite(${e.args[0].type?.k === 'FLOAT' ? a : `Float64(${a})`})`;
|
||||||
|
case 'INF': this.need('inf'); return 'inf[DType.float64]()';
|
||||||
|
case 'NAN': this.need('nan'); return 'nan[DType.float64]()';
|
||||||
|
case 'ULP': { const T = mojoNumber(e.type)!; this.need('ulp'); return `ulp(${this.as(T, e.args[0], a)})`; }
|
||||||
|
case 'NEXT_AFTER': { const T = mojoNumber(e.type)!; this.need('nextafter'); return `nextafter(${this.as(T, e.args[0], a)}, ${this.as(T, e.args[1], b)})`; }
|
||||||
|
case 'IS_CLOSE': {
|
||||||
|
const d = this.args4(e);
|
||||||
|
return `(${d[0]} == ${d[1]} or abs(${d[0]} - ${d[1]}) <= max(${d[2]} * max(abs(${d[0]}), abs(${d[1]})), ${d[3]}))`;
|
||||||
|
}
|
||||||
|
case 'MAX_VALUE': case 'MIN_VALUE': case 'EPSILON': {
|
||||||
|
const T = mojoNumber(e.type)!;
|
||||||
|
const ty = e.type;
|
||||||
|
if (e.builtin === 'EPSILON') return `${T}(${ty.bits === 32 ? '1.1920928955078125e-07' : '2.220446049250313e-16'})`;
|
||||||
|
if (ty.k === 'INTEGER') {
|
||||||
|
const hi = ty.signed ? 2 ** (ty.bits - 1) - 1 : 2 ** ty.bits - 1; const lo = ty.signed ? -(2 ** (ty.bits - 1)) : 0;
|
||||||
|
return `${T}(${e.builtin === 'MAX_VALUE' ? hi : lo})`;
|
||||||
|
}
|
||||||
|
const m = ty.bits === 32 ? '3.4028234663852886e38' : '1.7976931348623157e308';
|
||||||
|
return `${T}(${e.builtin === 'MAX_VALUE' ? m : `-${m}`})`;
|
||||||
|
}
|
||||||
|
case 'GCD': this.need('gcd'); return `gcd(${a}, ${b})`;
|
||||||
|
case 'LCM': this.need('lcm'); return `lcm(${a}, ${b})`;
|
||||||
|
case 'BIT_AND': return `(${a} & ${b})`;
|
||||||
|
case 'BIT_OR': return `(${a} | ${b})`;
|
||||||
|
case 'BIT_XOR': return `(${a} ^ ${b})`;
|
||||||
|
case 'BIT_NOT': return `~${a}`;
|
||||||
|
case 'SHIFT_LEFT': case 'SHIFT_RIGHT': {
|
||||||
|
const T = this.type(e.type);
|
||||||
|
const n = isSized(e.type) ? `${T}(${b})` : b;
|
||||||
|
if (e.builtin === 'SHIFT_LEFT' && isSized(e.type)) this.note('sized', 'In the notation, a value that does not fit a sized whole-number type stops the run. In Mojo, Int8 and the other fixed-width types wrap around silently, and a conversion such as Int8(x) keeps only the low bits.');
|
||||||
|
return `(${a} ${e.builtin === 'SHIFT_LEFT' ? '<<' : '>>'} ${n})`;
|
||||||
|
}
|
||||||
|
case 'WRAP': return `${this.type(e.type)}(${a})`;
|
||||||
default:
|
default:
|
||||||
return this.gap(`the built-in ${e.builtin}, which Mojo's standard library has no function for`);
|
return this.gap(`the built-in ${e.builtin}, which Mojo's standard library has no function for`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
// Sized whole numbers: arithmetic that stays inside its type, wrapping only when asked, and conversions that check.
|
||||||
|
// EXPECT: 255 0 255
|
||||||
|
// EXPECT: 1500000000 -2 256
|
||||||
|
// EXPECT: 8 14 6 255 1024 -4
|
||||||
|
// EXPECT: 12 12 -128 2147483647
|
||||||
|
DECLARE Count : UINT8 <- 250
|
||||||
|
Count <- Count + 5
|
||||||
|
OUTPUT Count, " ", WRAP(Count + 1, UINT8), " ", MAX_VALUE(UINT8)
|
||||||
|
DECLARE Big : INT64 <- 3_000_000_000
|
||||||
|
DECLARE Small : INT32
|
||||||
|
Small <- INT32(Big DIV 2)
|
||||||
|
OUTPUT Small, " ", INT16(-2.9), " ", 0xFF + 0b1
|
||||||
|
OUTPUT BIT_AND(0b1100, 0b1010), " ", BIT_OR(12, 10), " ", BIT_XOR(12, 10), " ", BIT_NOT(UINT8(0)), " ", SHIFT_LEFT(1, 10), " ", SHIFT_RIGHT(-8, 1)
|
||||||
|
OUTPUT GCD(84, 36), " ", LCM(4, 6), " ", MIN_VALUE(INT8), " ", MAX_VALUE(INT32)
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
// Single precision: every FLOAT32 result is rounded to 24 bits, and a literal next to a FLOAT32 is a FLOAT32.
|
||||||
|
// EXPECT: 1.0000001 0.9999999999999999
|
||||||
|
// EXPECT: TRUE FALSE
|
||||||
|
// EXPECT: 0.100000001490116119384765625
|
||||||
|
// EXPECT: 16777216.0 1.1920929e-7 1.1920929e-7
|
||||||
|
// EXPECT: 5.551115123125783e-17 0.0
|
||||||
|
// EXPECT: TRUE TRUE
|
||||||
|
DECLARE X : FLOAT32 <- 0.1
|
||||||
|
DECLARE Total32 : FLOAT32 <- 0.0
|
||||||
|
DECLARE Total64 : FLOAT <- 0.0
|
||||||
|
FOR I <- 0 TO 10
|
||||||
|
Total32 <- Total32 + X
|
||||||
|
Total64 <- Total64 + 0.1
|
||||||
|
NEXT I
|
||||||
|
OUTPUT Total32, " ", Total64
|
||||||
|
OUTPUT X == 0.1, " ", FLOAT64(X) == 0.1
|
||||||
|
OUTPUT EXACT(X)
|
||||||
|
OUTPUT FLOAT32(16777217), " ", ULP(FLOAT32(1.0)), " ", EPSILON(FLOAT32)
|
||||||
|
OUTPUT FMA(0.1, 10.0, -1.0), " ", 0.1 * 10.0 - 1.0
|
||||||
|
OUTPUT IS_CLOSE(Total32, 1.0, 1e-6, 0.0), " ", IS_CLOSE(Total64, 1.0, 1e-15, 0.0)
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// USE IEEE: NaN and the infinities flow through a calculation as IEEE 754 says, instead of stopping the run.
|
||||||
|
// EXPECT: infinity -infinity not a number
|
||||||
|
// EXPECT: TRUE TRUE FALSE TRUE
|
||||||
|
// EXPECT: not a number -infinity FALSE
|
||||||
|
USE IEEE
|
||||||
|
DECLARE Zero : FLOAT <- 0.0
|
||||||
|
DECLARE Inf : FLOAT
|
||||||
|
Inf <- 1.0 / Zero
|
||||||
|
DECLARE NotANumber : FLOAT <- Inf - Inf
|
||||||
|
OUTPUT Inf, " ", -Inf, " ", NotANumber
|
||||||
|
OUTPUT IS_INF(Inf), " ", IS_NAN(NotANumber), " ", NotANumber == NotANumber, " ", NotANumber != NotANumber
|
||||||
|
OUTPUT SQRT(-1.0), " ", LOG(0.0), " ", IS_FINITE(MAX_VALUE(FLOAT64) * 2.0)
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// Elementary functions for numerical work, and the accurate forms near 0.
|
||||||
|
// EXPECT: 5.0 TRUE -3.0 1024.0
|
||||||
|
// EXPECT: 1.00000000005e-10 1.000000082740371e-10 9.999999999500001e-11
|
||||||
|
// EXPECT: -2.0 -1 -2.0 10 0.0
|
||||||
|
// EXPECT: 3.142 3.142 0.0 0.0 2.718
|
||||||
|
// EXPECT: TRUE 2.220446049250313e-16
|
||||||
|
OUTPUT HYPOT(3.0, 4.0), " ", HYPOT(1e200, 1e200) > 1e200, " ", CBRT(-27.0), " ", POW(2.0, 10.0)
|
||||||
|
OUTPUT EXPM1(1e-10), " ", EXP(1e-10) - 1.0, " ", LOG1P(1e-10)
|
||||||
|
OUTPUT TRUNC(-2.7), " ", SIGN(-3), " ", COPYSIGN(2.0, -0.0), " ", CLAMP(15, 0, 10), " ", CLAMP(-0.5, 0.0, 1.0)
|
||||||
|
OUTPUT ROUND(ASIN(1.0) * 2 * 1000) / 1000, " ", ROUND(ACOS(-1.0) * 1000) / 1000, " ", ATAN(0.0), " ", TANH(0.0), " ", ROUND(E() * 1000) / 1000
|
||||||
|
OUTPUT NEXT_AFTER(1.0, 2.0) - 1.0 == EPSILON(FLOAT64), " ", ULP(1.0)
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
// Pseudo for numerical work: sized types, FLOAT32, conversions, the numerical built-ins, USE IEEE, and what the
|
||||||
|
// prover may and may not say about them. The corpus programs 49 to 52 show the same features in use; this file
|
||||||
|
// pins each rule on its own.
|
||||||
|
import test from 'node:test';
|
||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import { studyOf, runPseudo, provePseudo } from '../dist/services/pseudo.js';
|
||||||
|
import { fma, exactDecimal, nextAfter, ulp, roundDyadic, wrap } from '../dist/engines/pseudo/numeric.js';
|
||||||
|
import { intRange, SIZED, formatFloat32 } from '../dist/engines/pseudo/lang.js';
|
||||||
|
|
||||||
|
const run = (src) => {
|
||||||
|
const st = studyOf(src);
|
||||||
|
assert.ok(st.ok, st.diagnostics.filter((d) => d.severity === 'error').map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | '));
|
||||||
|
return runPseudo(st, {});
|
||||||
|
};
|
||||||
|
const out = (src) => { const r = run(src); assert.equal(r.error, null, r.error?.message); return r.output; };
|
||||||
|
const errors = (src) => studyOf(src).diagnostics.filter((d) => d.severity === 'error').map((d) => d.id);
|
||||||
|
|
||||||
|
test('the sized types have the ranges of their names (64-bit types cut at what a run holds exactly)', () => {
|
||||||
|
assert.deepEqual(intRange(SIZED.INT8), [-128, 127]);
|
||||||
|
assert.deepEqual(intRange(SIZED.UINT16), [0, 65535]);
|
||||||
|
assert.deepEqual(intRange(SIZED.INT32), [-2147483648, 2147483647]);
|
||||||
|
assert.deepEqual(intRange(SIZED.UINT32), [0, 4294967295]);
|
||||||
|
assert.deepEqual(intRange(SIZED.INT64), [-Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('literals: underscores, hexadecimal and binary', () => {
|
||||||
|
assert.deepEqual(out('OUTPUT 1_000_000, " ", 0xFF, " ", 0b1010, " ", 1_000.5'), ['1000000 255 10 1000.5']);
|
||||||
|
assert.ok(errors('OUTPUT 1__0').includes('bad-number'));
|
||||||
|
assert.ok(errors('OUTPUT 0xFG').includes('bad-number'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a sized result that leaves its range stops the run; WRAP wraps on purpose', () => {
|
||||||
|
const r = run('DECLARE X : INT8 <- 127\nX <- X + 1\n');
|
||||||
|
assert.equal(r.error?.kind, 'overflow');
|
||||||
|
assert.equal(r.error.line, 2);
|
||||||
|
assert.match(r.error.message, /INT8 holds -128 to 127/);
|
||||||
|
assert.deepEqual(out('DECLARE X : INT8 <- 127\nOUTPUT WRAP(X + 1, INT8), " ", WRAP(-1, UINT8), " ", WRAP(70000, UINT16)'), ['-128 255 4464']);
|
||||||
|
assert.equal(run('DECLARE U : UINT8 <- 0\nU <- U - 1\n').error?.kind, 'overflow');
|
||||||
|
assert.equal(run('DECLARE X : INT8\nX <- -128\nX <- -X\n').error?.kind, 'overflow');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('storing, passing and returning check the range of the receiving type', () => {
|
||||||
|
assert.equal(run('DECLARE N : INTEGER <- 300\nDECLARE B : UINT8\nB <- N\n').error?.kind, 'overflow');
|
||||||
|
const src = 'FUNCTION Id(X : UINT8) RETURNS UINT8\n RETURN X\nENDFUNCTION\nDECLARE N : INTEGER <- 256\nOUTPUT Id(N)\n';
|
||||||
|
const r = run(src);
|
||||||
|
assert.equal(r.error?.kind, 'overflow');
|
||||||
|
assert.match(r.error.message, /Parameter X of Id/);
|
||||||
|
assert.equal(run('FUNCTION Big(N : INTEGER) RETURNS INT16\n RETURN N * 1000\nENDFUNCTION\nOUTPUT Big(40)\n').error?.kind, 'overflow');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('conversions: a FLOAT loses its fraction toward zero, and a value that does not fit stops the run', () => {
|
||||||
|
assert.deepEqual(out('OUTPUT INT32(-2.9), " ", UINT8(200), " ", INT64(2.5e9), " ", FLOAT32(0.1) == FLOAT32(0.1)'), ['-2 200 2500000000 TRUE']);
|
||||||
|
assert.deepEqual(errors('OUTPUT INT8(200)'), ['literal-range']); // a literal is checked before any run
|
||||||
|
assert.equal(run('DECLARE N : INTEGER <- 200\nOUTPUT INT8(N)').error?.kind, 'overflow');
|
||||||
|
assert.equal(run('DECLARE F : FLOAT <- 3.0e9\nOUTPUT INT32(F)').error?.kind, 'overflow');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('the checker: widths and precisions do not mix silently, and literals take the type beside them', () => {
|
||||||
|
assert.deepEqual(errors('DECLARE A : INT8 <- 1\nDECLARE B : INT32 <- 2\nOUTPUT A + B'), ['mixed-width']);
|
||||||
|
assert.deepEqual(errors('DECLARE A : FLOAT32 <- 1.0\nDECLARE B : FLOAT <- 2.0\nOUTPUT A * B'), ['mixed-precision']);
|
||||||
|
assert.deepEqual(errors('DECLARE A : INT8\nA <- 300'), ['literal-range']);
|
||||||
|
assert.deepEqual(errors('DECLARE A : INT8 <- 1\nDECLARE B : INT32 <- 2\nB <- A'), ['type']);
|
||||||
|
assert.deepEqual(errors('DECLARE A : FLOAT32\nDECLARE D : FLOAT <- 0.5\nA <- D'), ['type']);
|
||||||
|
assert.deepEqual(errors('DECLARE A : FLOAT32\nDECLARE D : FLOAT <- 0.5\nA <- FLOAT32(D)'), []);
|
||||||
|
assert.deepEqual(errors('DECLARE A : FLOAT32\nA <- 0.1'), []);
|
||||||
|
assert.deepEqual(errors('OUTPUT INT8'), ['type-as-value']);
|
||||||
|
assert.deepEqual(errors('OUTPUT MAX_VALUE(INT64)'), ['exact-limit']);
|
||||||
|
assert.deepEqual(errors('OUTPUT WRAP(5, UINT64)'), ['wrap-64']);
|
||||||
|
assert.deepEqual(errors('DECLARE X : INTEGER <- 1\nOUTPUT EPSILON(INT32)'), ['type']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('FLOAT32: results round to single precision, a literal next to one is a single, and it prints as the shortest decimal', () => {
|
||||||
|
assert.deepEqual(out('DECLARE X : FLOAT32 <- 0.1\nOUTPUT X, " ", X == 0.1, " ", FLOAT64(X) == 0.1, " ", X + 0.2'), ['0.1 TRUE FALSE 0.3']);
|
||||||
|
assert.deepEqual(out('DECLARE X : FLOAT32 <- 16777216.0\nOUTPUT X + 1.0, " ", FLOAT64(X) + 1.0'), ['16777216.0 16777217.0']);
|
||||||
|
assert.equal(formatFloat32(Math.fround(0.1)), '0.1');
|
||||||
|
assert.equal(formatFloat32(Math.fround(1 / 3)), '0.33333334');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('exact helpers: FMA rounds once, EXACT shows every digit, ULP and NEXT_AFTER step by one value', () => {
|
||||||
|
assert.equal(fma(0.1, 10, -1), 5.551115123125783e-17);
|
||||||
|
assert.equal(0.1 * 10 - 1, 0);
|
||||||
|
assert.equal(fma(2, 3, 4), 10);
|
||||||
|
assert.equal(fma(Math.fround(0.1), 10, -1, true), Math.fround(1.4901161193847656e-8));
|
||||||
|
assert.equal(exactDecimal(0.1), '0.1000000000000000055511151231257827021181583404541015625');
|
||||||
|
assert.equal(exactDecimal(-2.5), '-2.5');
|
||||||
|
assert.equal(exactDecimal(1e21), '1000000000000000000000.0');
|
||||||
|
assert.equal(exactDecimal(Number.MIN_VALUE).length, 1076); // 4.94...e-324 has 1074 digits after the point
|
||||||
|
assert.equal(nextAfter(1, 2), 1 + Number.EPSILON);
|
||||||
|
assert.equal(nextAfter(0, -1), -Number.MIN_VALUE);
|
||||||
|
assert.equal(nextAfter(1, 2, true), 1 + 2 ** -23);
|
||||||
|
assert.equal(ulp(1), Number.EPSILON);
|
||||||
|
assert.equal(ulp(Number.MAX_VALUE), 2 ** 971);
|
||||||
|
assert.equal(ulp(1, true), 2 ** -23);
|
||||||
|
assert.equal(roundDyadic(3n, -1), 1.5);
|
||||||
|
assert.equal(roundDyadic((1n << 54n) + 1n, 0), 2 ** 54); // ties and below round to even
|
||||||
|
assert.equal(wrap(-129, 8, true), 127);
|
||||||
|
assert.deepEqual(out('OUTPUT FMA(0.1, 10.0, -1.0), " ", ULP(1.0) == EPSILON(FLOAT64), " ", EXACT(0.5)'), ['5.551115123125783e-17 TRUE 0.5']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('whole numbers bit by bit, two\'s complement for negative values', () => {
|
||||||
|
assert.deepEqual(out('OUTPUT BIT_AND(-1, 255), " ", BIT_OR(-8, 1), " ", BIT_XOR(5, 3), " ", BIT_NOT(5), " ", BIT_NOT(UINT8(5))'), ['255 -7 6 -6 250']);
|
||||||
|
assert.deepEqual(out('OUTPUT SHIFT_LEFT(3, 4), " ", SHIFT_RIGHT(-9, 1), " ", SHIFT_RIGHT(UINT8(255), 4), " ", GCD(-12, 18), " ", LCM(6, 8)'), ['48 -5 15 6 24']);
|
||||||
|
assert.equal(run('DECLARE B : UINT8 <- 200\nOUTPUT SHIFT_LEFT(B, 1)').error?.kind, 'overflow');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('the limits of each type', () => {
|
||||||
|
assert.deepEqual(out('OUTPUT MAX_VALUE(INT8), " ", MIN_VALUE(INT16), " ", MAX_VALUE(UINT32), " ", MIN_VALUE(UINT64)'), ['127 -32768 4294967295 0']);
|
||||||
|
assert.deepEqual(out('OUTPUT MAX_VALUE(FLOAT64) > 1e308, " ", EPSILON(FLOAT64), " ", MAX_VALUE(FLOAT32)'), ['TRUE 2.220446049250313e-16 3.4028235e38']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('domains: without USE IEEE the run stops where IEEE 754 would give NaN or an infinity; with it, the values flow', () => {
|
||||||
|
for (const [e, kind] of [['SQRT(-1.0)', 'value'], ['LOG(0.0)', 'value'], ['ASIN(2.0)', 'value'], ['1.0 / 0.0', 'divide'], ['MAX_VALUE(FLOAT64) * 2.0', 'overflow'], ['INF()', 'overflow'], ['LOG1P(-1.0)', 'value']]) {
|
||||||
|
assert.equal(run(`OUTPUT ${e}`).error?.kind, kind, e);
|
||||||
|
}
|
||||||
|
assert.deepEqual(out('USE IEEE\nOUTPUT SQRT(-1.0), " ", 1.0 / 0.0, " ", -1.0 / 0.0, " ", IS_NAN(NAN()), " ", NAN() == NAN()'), ['not a number infinity -infinity TRUE FALSE']);
|
||||||
|
// whole-number division by zero still stops: IEEE 754 is about FLOAT values
|
||||||
|
assert.equal(run('USE IEEE\nOUTPUT 1 DIV 0').error?.kind, 'divide');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('names: a program may still call its variable E or its function GCD, and its own definition wins', () => {
|
||||||
|
assert.deepEqual(out('DECLARE E : FLOAT <- 2.0\nOUTPUT E * 2.0, " ", ROUND(E() * 100)'), ['4.0 272']);
|
||||||
|
assert.deepEqual(out('FUNCTION GCD(A : INTEGER, B : INTEGER) RETURNS INTEGER\n RETURN 7\nENDFUNCTION\nOUTPUT GCD(12, 18)'), ['7']);
|
||||||
|
assert.ok(errors('DECLARE LENGTH : INTEGER').includes('builtin-name'));
|
||||||
|
assert.ok(errors('DECLARE INT8 : INTEGER').length > 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('the prover: sized arithmetic carries an overflow obligation; FLOAT32 is a function of its own; USE IEEE proves nothing', () => {
|
||||||
|
const report = (src) => provePseudo(studyOf(src)).units.flatMap((u) => u.obligations.map((o) => ({ ...o, unit: u.name, notes: u.notes })));
|
||||||
|
const guarded = report('FUNCTION Inc(X : INT8) RETURNS INT8\n REQUIRE X < 127\n RETURN X + 1\nENDFUNCTION\n');
|
||||||
|
assert.deepEqual(guarded.filter((o) => o.kind === 'overflow').map((o) => o.status), ['proved']);
|
||||||
|
const open = report('FUNCTION Inc(X : INT8) RETURNS INT8\n RETURN X + 1\nENDFUNCTION\n');
|
||||||
|
assert.deepEqual(open.filter((o) => o.kind === 'overflow').map((o) => o.status), ['broken']); // the fuzzer tries 127
|
||||||
|
const narrow = report('FUNCTION N(K : INTEGER) RETURNS UINT8\n REQUIRE 0 <= K AND K < 256\n RETURN K\nENDFUNCTION\n');
|
||||||
|
assert.deepEqual(narrow.filter((o) => o.kind === 'overflow').map((o) => o.status), ['proved']);
|
||||||
|
const loop = report('FUNCTION Count(N : UINT8) RETURNS UINT8\n DECLARE C : UINT8 <- 0\n FOR I <- 0 TO N\n INVARIANT C == I\n C <- C + 1\n NEXT I\n RETURN C\nENDFUNCTION\n');
|
||||||
|
assert.ok(loop.filter((o) => o.kind === 'overflow').every((o) => o.status === 'proved'), JSON.stringify(loop.map((o) => [o.kind, o.status])));
|
||||||
|
const half = report('FUNCTION Half(X : FLOAT32) RETURNS FLOAT32\n ENSURE RESULT == X / 2.0\n RETURN X / 2.0\nENDFUNCTION\n');
|
||||||
|
assert.ok(half.find((o) => o.kind === 'ensure').status === 'proved');
|
||||||
|
const roots = report('FUNCTION R(X : FLOAT) RETURNS FLOAT\n RETURN SQRT(X)\nENDFUNCTION\n');
|
||||||
|
assert.deepEqual(roots.filter((o) => o.kind === 'argument').map((o) => o.status), ['broken']);
|
||||||
|
const ieee = report('USE IEEE\nFUNCTION F(X : INTEGER) RETURNS INTEGER\n REQUIRE X > 0\n RETURN 10 DIV X\nENDFUNCTION\n');
|
||||||
|
assert.ok(ieee.length && ieee.every((o) => o.status !== 'proved'));
|
||||||
|
assert.ok(ieee[0].notes.some((n) => /USE IEEE/.test(n)));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('the fuzzer reaches the edges of a sized type and generates FLOAT32 values', async () => {
|
||||||
|
const { checkProperties } = await import('../dist/engines/pseudo/testing.js');
|
||||||
|
const st = studyOf('FUNCTION F(X : UINT8, Y : FLOAT32) RETURNS BOOL\n ASSERT X != 255\n ASSERT FLOAT32(Y) == Y\n RETURN TRUE\nENDFUNCTION\n');
|
||||||
|
const fails = checkProperties(st.program.program, 'F', { count: 300 }).failures;
|
||||||
|
assert.ok(fails.some((f) => f.kind === 'assert' && f.line === 2), 'X = 255 is never tried');
|
||||||
|
assert.ok(!fails.some((f) => f.line === 3), 'a generated FLOAT32 is not a single');
|
||||||
|
});
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// A whole number stored in a FLOAT32 is rounded to the nearest single:
|
||||||
|
// 16777217 becomes 16777216. A rule that kept the INTEGER's value for the
|
||||||
|
// FLOAT32 would prove this ASSERT, and the run breaks it.
|
||||||
|
// MUST-NOT-PROVE: assert@8
|
||||||
|
DECLARE N : INTEGER <- 16777217
|
||||||
|
DECLARE X : FLOAT32
|
||||||
|
X <- N
|
||||||
|
ASSERT X == N
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// With USE IEEE a FLOAT can be NaN, and NaN == NaN is FALSE. The prover's
|
||||||
|
// rule that every number equals itself does not hold for NaN, so nothing in
|
||||||
|
// a program that says USE IEEE may be marked proved.
|
||||||
|
// MUST-NOT-PROVE: assert@7
|
||||||
|
USE IEEE
|
||||||
|
DECLARE X : FLOAT <- NAN()
|
||||||
|
ASSERT X == X
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// INT8 holds -128 to 127. X + 1 for X = 127 is 128, which does not fit, so
|
||||||
|
// the run stops there. A rule that treated the sum as an exact whole number
|
||||||
|
// with no range would prove it fits without a REQUIRE to guarantee it.
|
||||||
|
// MUST-NOT-PROVE: overflow@6
|
||||||
|
FUNCTION Inc(X : INT8) RETURNS INT8
|
||||||
|
RETURN X + 1
|
||||||
|
ENDFUNCTION
|
||||||
@@ -7,7 +7,7 @@ import { studyOf } from '../dist/services/pseudo.js';
|
|||||||
import { readLingo } from '../dist/services/lingo.js';
|
import { readLingo } from '../dist/services/lingo.js';
|
||||||
import { parseSpeco, toAscii, toMath } from '../dist/services/speco.js';
|
import { parseSpeco, toAscii, toMath } from '../dist/services/speco.js';
|
||||||
import { parseAsm } from '../dist/services/asm.js';
|
import { parseAsm } from '../dist/services/asm.js';
|
||||||
import { KEYWORDS, BUILTINS } from '../dist/engines/pseudo/lang.js';
|
import { KEYWORDS, BUILTINS, SOFT_BUILTINS } from '../dist/engines/pseudo/lang.js';
|
||||||
import { OPS } from '../dist/engines/asm86/interp.js';
|
import { OPS } from '../dist/engines/asm86/interp.js';
|
||||||
|
|
||||||
test('every pseudocode statement example parses and checks', () => {
|
test('every pseudocode statement example parses and checks', () => {
|
||||||
@@ -109,7 +109,8 @@ test('every pseudo idiom runs and prints what its note says', async () => {
|
|||||||
const words = new Set(reserved.entries.map((e) => e.term));
|
const words = new Set(reserved.entries.map((e) => e.term));
|
||||||
for (const k of KW) assert.ok(words.has(k), `reserved list misses keyword ${k}`);
|
for (const k of KW) assert.ok(words.has(k), `reserved list misses keyword ${k}`);
|
||||||
for (const k of Object.keys(CONTEXTUAL)) assert.ok(words.has(k), `reserved list misses ${k}`);
|
for (const k of Object.keys(CONTEXTUAL)) assert.ok(words.has(k), `reserved list misses ${k}`);
|
||||||
for (const k of Object.keys(BI)) assert.ok(words.has(k), `reserved list misses built-in ${k}`);
|
// the built-ins added for numerical work stay usable as names (a variable E, a FUNCTION GCD), so they are not listed
|
||||||
|
for (const k of Object.keys(BI)) assert.ok(SOFT_BUILTINS.has(k) ? !words.has(k) : words.has(k), SOFT_BUILTINS.has(k) ? `${k} can be a name, so it is not reserved` : `reserved list misses built-in ${k}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('every matho example translates to pseudo that checks, and every refusal is refused', () => {
|
test('every matho example translates to pseudo that checks, and every refusal is refused', () => {
|
||||||
|
|||||||
@@ -41,6 +41,10 @@ const KNOWN_GAPS = {
|
|||||||
'33-stops-div.pseudo': ['differs', 'dividing by zero stops the notation; a Float64 division in Mojo gives nan'],
|
'33-stops-div.pseudo': ['differs', 'dividing by zero stops the notation; a Float64 division in Mojo gives nan'],
|
||||||
'44-parallel-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'],
|
'44-parallel-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'],
|
||||||
'45-parallel-neighbour-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'],
|
'45-parallel-neighbour-race.pseudo': ['differs', 'the notation detects a data race in PARALLEL FOR; Mojo runs the passes in order and prints a result'],
|
||||||
|
'49-sized-integers.pseudo': ['declined', 'gcd and lcm from std.math are not run by the interpreter yet'],
|
||||||
|
'50-float32.pseudo': ['gap', 'EXACT has no Mojo spelling: Mojo prints the shortest decimal that rounds back, never the exact value'],
|
||||||
|
'51-ieee.pseudo': ['declined', 'isinf, isnan and inf[DType.float64]() are not run by the interpreter yet'],
|
||||||
|
'52-elementary.pseudo': ['declined', 'hypot and the other math functions are not run by the interpreter yet'],
|
||||||
'35-stops-unset.pseudo': ['checker', 'the pseudo reads a variable that was never given a value; the checker reports exactly that, as the compiler would'],
|
'35-stops-unset.pseudo': ['checker', 'the pseudo reads a variable that was never given a value; the checker reports exactly that, as the compiler would'],
|
||||||
'36-stops-counter.pseudo': ['checker', 'the pseudo reads a FOR counter after its loop, where it has no value; the checker reports exactly that'],
|
'36-stops-counter.pseudo': ['checker', 'the pseudo reads a FOR counter after its loop, where it has no value; the checker reports exactly that'],
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user