Compare commits

...

9 Commits

Author SHA1 Message Date
d6b064efcd 🐐 Test files fix 2026-04-25 22:08:25 +02:00
d2d09c770d IASON 2026-04-25 22:03:30 +02:00
ff1c392dd6 🐐 Hash tables 2026-04-25 20:49:31 +02:00
c9fd245bb3 🐐 Moving docs 2026-04-25 20:33:03 +02:00
272881e6cf 🐐 Line numbers in errors 2026-04-25 20:21:16 +02:00
382492a6fc 🐐 MVTA, CRIBRA, CONFLA 2026-04-25 20:20:54 +02:00
5e4c7350a9 🐐 ORDINA with comparitor 2026-04-25 18:50:37 +02:00
8d06407527 🐐 Zipping 2026-04-25 18:34:47 +02:00
3a80c4e941 🐐 Array functions 2026-04-25 18:18:13 +02:00
36 changed files with 2175 additions and 599 deletions

574
DOCS.md Normal file
View File

@@ -0,0 +1,574 @@
# CENTVRION Language Reference
Full syntax documentation for the CENTVRION language. For the formal grammar, see [`language/main.pdf`](language/main.pdf). For installation and a minimal example, see [`README.md`](README.md).
## Example code
### Hello World
![Hello World](snippets/hello_world.png)
### Recursive Fibonacci number function
![Fibonacci](snippets/fibonacci.png)
### Number guessing game
![Number guessing game](snippets/guessing.png)
## Variables
Variables are set with the `DESIGNA` and `VT` keywords. Type is inferred.
![Variable assignment](snippets/variable.png)
Variable can consist of lower-case letters, numbers, as well as `_`.
### Compound assignment
`AVGE` (+=), `MINVE` (-=), `MVLTIPLICA` (*=) and `DIVIDE` (/=) are shorthand for updating a variable with an arithmetic operation:
![Compound assignment](snippets/compound.png)
```
> VIII
```
`x AVGE III` is equivalent to `DESIGNA x VT x + III`; `MINVE`, `MVLTIPLICA` and `DIVIDE` expand the same way with subtraction, multiplication and division.
### Destructuring
Multiple variables can be assigned at once by unpacking an array or multi-return function:
![Destructuring function](snippets/destructure_fn.png)
The number of targets must match the length of the array. This also works with array literals:
![Destructuring array](snippets/destructure_array.png)
## Data types
### NVLLVS
`NVLLVS` is a special kind of data type in `CENTVRION`, similar to the `null` value in many other languages. `NVLLVS` can be 0 if evaluated as an int or float, or an empty string if evaluated as a string. `NVLLVS` cannot be evaluated as a boolean.
### Strings
Strings are written as text in quotes (`'` or `"`).
![String literal](snippets/string_literal.png)
Strings are concatenated with `&`:
![String concatenation](snippets/string_concat.png)
`NVLLVS` coerces to an empty string when used with `&`. Note: `+` is for arithmetic only — using it on strings raises an error.
#### String Interpolation
Double-quoted strings support interpolation with `{expression}`:
![String interpolation](snippets/string_interp.png)
Any expression can appear inside `{}`. Values are coerced to strings the same way as with `&` (integers become Roman numerals, booleans become `VERITAS`/`FALSITAS`, etc.).
Single-quoted strings do **not** interpolate — `'{nomen}'` is the literal text `{nomen}`. Use `{{` and `}}` for literal braces in double-quoted strings: `"use {{braces}}"``use {braces}`.
#### String Indexing and Slicing
Strings support the same indexing and slicing syntax as arrays. Indexing is 1-based and returns a single-character string:
![String indexing](snippets/string_index.png)
```
> S
> L
```
Slicing uses `VSQVE` with inclusive bounds, returning a substring:
![String slicing](snippets/string_slice.png)
```
> ALV
```
Integer modulo is `RELIQVVM`: `VII RELIQVVM III` evaluates to `I`. Under the `FRACTIO` module it returns a fraction, so `IIIS RELIQVVM IS` is `S` (i.e. 1/2).
### Integers
Integers must be written in roman numerals using the following symbols:
|Symbol|Value|
|------|-----|
|`I`|1|
|`V`|5|
|`X`|10|
|`L`|50|
|`C`|100|
|`D`|500|
|`M`|1000|
Each of the symbols written by themself is equal to the value of the symbol. Different symbols written from largest to smallest are equal to the sum of the symbols. Two to three of the same symbol written consecutively is equal to the sum of those symbols (only true for `I`s, `X`s, `C`s or `M`s ). A single `I` written before a `V` or `X` is equal to 1 less than the value of the second symbol. Similarly, an `X` written before a `L` or `C` is 10 less than the second symbol, and a `C` written before a `D` or `M` is 100 less than the second symbol.
Because of the restrictions of roman numerals, numbers above 3.999 are impossible to write in the base `CENTVRION` syntax. If numbers of that size are required, see the `MAGNVM` module.
The number 0 can be expressed with the keyword `NVLLVS`.
#### Negative numbers
Negative numbers can be expressed as `NVLLVS` minus the value. For an explicit definition of negative numbers, see the `SVBNVLLA` module.
### Floats
The base `CENTVRION` syntax does not allow for floats. However, the `FRACTIO` module adds a syntax for fractions.
### Booleans
Booleans are denoted with the keywords `VERITAS` for true and `FALSITAS` for false.
### Arrays
Arrays are defined using square brackets (`[]`) and commas (`,`):
![Array literal](snippets/array_literal.png)
An array of integers can also be initialized with the `VSQVE` keyword. The range is inclusive on both ends:
![Array with VSQVE](snippets/array_vsqve.png)
```
> [I, II, III, IV, V, VI, VII, VIII, IX, X]
```
Individual elements can be accessed by index using square brackets. Indexing is 1-based, so `I` refers to the first element:
![Array indexing](snippets/array_index.png)
```
> I
```
Arrays are concatenated with `@`:
![Array concatenation](snippets/array_concat.png)
```
> [I, II, III, IV, V]
```
Both operands must be arrays — using `@` on non-arrays raises an error.
A sub-array can be extracted with `VSQVE` inside the index brackets. Both bounds are inclusive and 1-based:
![Array slicing](snippets/array_slice.png)
```
> [XX, XXX, XL]
```
### Dicts (TABVLA)
Dicts are key-value maps created with the `TABVLA` keyword and curly braces:
![Dict creation](snippets/dict_create.png)
Keys must be strings or integers. Values are accessed and assigned with square brackets:
![Dict access](snippets/dict_access.png)
Iterating over a dict with `PER` loops over its keys:
![Dict iteration](snippets/dict_per.png)
`LONGITVDO(dict)` returns the number of entries. `CLAVES(dict)` returns the keys as an array.
## Conditionals
### SI/TVNC
If-then statements are denoted with the keywords `SI` (if) and `TVNC` (then). Thus, the code
![SI/TVNC](snippets/si_tvnc.png)
Will return `I` (1), as the conditional evaluates `x` to be true.
### Boolean expressions
In conditionals, `EST` functions as an equality evaluation, `DISPAR` as not-equal, and `MINVS` (<), `PLVS` (>), `HAVD_PLVS` (≤), and `HAVD_MINVS` (≥) function as inequality evaluation.
### ALIVD
When using `SI`/`TVNC` statements, you can also use `ALIVD` as an "else".
![ALIVD](snippets/alivd.png)
```
> I
```
`SI` statements may follow immediately after `ALIVD`.
![ALIVD SI](snippets/alivd_si.png)
```
> II
```
### Boolean operators
The keyword `ET` can be used as a boolean "and". The keyword `AVT` can be used as a boolean "or".
![Boolean operators](snippets/boolean_ops.png)
```
> II
```
## Loops
### DONICVM loops
![DONICVM loop](snippets/donicvm.png)
```
> LV
```
An optional `GRADV` clause sets the stride. The step must be a nonzero
integer expression; positive values ascend, negative values descend, and
the endpoint is included only when the stride lands on it exactly.
![DONICVM with GRADV](snippets/donicvm_gradv.png)
```
> XXV
```
### DVM loops
![DVM loop](snippets/dvm.png)
```
> XI
```
### AETERNVM loops
`AETERNVM FAC { ... }` is shorthand for an infinite loop — equivalent
to `DVM FALSITAS FAC { ... }` but without relying on `DVM`'s inverted
condition. Exit the loop with `ERVMPE` (or `REDI` from inside a function).
![AETERNVM loop](snippets/aeternvm.png)
```
> X
```
### PER loops
![PER loop](snippets/per.png)
```
> I
> II
> III
> IV
> V
```
Variables can be unpacked in `PER` loops, similar to `DESIGNA` destructuring:
![PER destructuring](snippets/per_destructure.png)
```
> III
> VII
```
## Error handling
Errors can be caught using `TEMPTA` (temptare = to try) and `CAPE` (capere = to catch). The `CAPE` block binds the error message to a variable as a string.
![TEMPTA / CAPE](snippets/tempta_cape.png)
```
> Division by zero
```
If the try block succeeds, the catch block is skipped. If an error occurs in the catch block, it propagates up. `TEMPTA`/`CAPE` blocks can be nested.
## Functions
Functions are defined with the `DEFINI` and `VT` keywords. The `REDI` keyword is used to return. `REDI` can also be used to end the program, if used outside of a function.
Calling a function is done with the `INVOCA` keyword.
![Function definition](snippets/function.png)
```
> CXXI
```
## First-class functions
Functions are first-class values in CENTVRION. They can be assigned to variables, passed as arguments, returned from functions, and stored in arrays or dicts.
Anonymous functions are created with the `FVNCTIO` keyword:
![FVNCTIO](snippets/fvnctio.png)
```
> XIV
```
`INVOCA` accepts any expression as the callee, not just a name:
![INVOCA expressions](snippets/invoca_expr.png)
```
> VI
> VI
> XVI
```
Note: CENTVRION does **not** have closures. When a function is called, it receives a copy of the *caller's* scope, not the scope where it was defined. Variables from a function's definition site are only available if they also exist in the caller's scope at call time.
## Built-ins
### DIC
`DIC(value, ...)`
Prints one or more values to stdout, space-separated, with integers rendered as Roman numerals. Returns the printed string.
![DIC](snippets/dic.png)
### AVDI
`AVDI()`
Reads one line from stdin and returns it as a string.
### AVDI_NVMERVS
`AVDI_NVMERVS()`
Reads one line from stdin, parses it as a Roman numeral, and returns it as an integer. Raises an error if the input is not a valid numeral.
### CONTINVA
`CONTINVA`
Skips the rest of the current loop body and continues to the next iteration (`DVM` or `PER`). Has no meaningful return value.
### ERVMPE
`ERVMPE`
Breaks out of the current loop (`DVM` or `PER`). Has no meaningful return value.
### LONGITVDO
`LONGITVDO(array)`, `LONGITVDO(string)`, or `LONGITVDO(dict)`
Returns the length of `array` (element count), `string` (character count), or `dict` (entry count) as an integer.
### CLAVES
`CLAVES(dict)`
Returns the keys of `dict` as an array.
### ORDINA
`ORDINA(array)` or `ORDINA(array, comparator)`
Sorts an array. Returns a new sorted array; the original is unchanged.
Without a comparator, sorts in ascending order. All elements must be the same type — integers, fractions, or strings. Integers and fractions sort numerically; strings sort lexicographically.
With a comparator, the type-uniformity rule is dropped — the comparator decides ordering. The comparator must be a function of exactly two parameters and must return `VERAX`. `comparator(a, b)` returns `VERITAS` iff `a` should come **before** `b`; two elements are treated as equal when both `comparator(a, b)` and `comparator(b, a)` are `FALSITAS`.
![ORDINA with comparator](snippets/ordina_cmp.png)
```
> [V III II I]
```
### MVTA
`MVTA(array, fn)`
Returns a new array obtained by applying `fn` to every element of `array`. The original array is unchanged. `fn` must be a function of exactly one parameter; its return value is unrestricted, so `MVTA` may produce an array of a different element type than its input.
![MVTA doubling each element](snippets/mvta.png)
```
> [II IV VI VIII]
```
### CRIBRA
`CRIBRA(array, predicate)`
Returns a new array containing the elements of `array` for which `predicate` returns `VERITAS`, in their original order. The original array is unchanged. `predicate` must be a function of exactly one parameter and must return `VERAX`.
![CRIBRA keeping elements ≤ III](snippets/cribra.png)
```
> [I II III]
```
### CONFLA
`CONFLA(array, initial, fn)`
Left fold: starts with `initial` as the accumulator, then for each element `e` of `array` updates the accumulator to `fn(acc, e)`, and returns the final accumulator. The original array is unchanged. `fn` must be a function of exactly two parameters. If `array` is empty, `initial` is returned unchanged.
![CONFLA summing elements](snippets/confla.png)
```
> XVI
```
### ADDE
`ADDE(array, value)`
Returns a new array with `value` appended at the end. The original array is unchanged.
### TOLLE
`TOLLE(array, idx)`
Returns a new array with the element at 1-based position `idx` removed. The index must be an integer in the range `[I, LONGITVDO(array)]`; out-of-range indices raise an error.
### INSERE
`INSERE(array, idx, value)`
Returns a new array with `value` inserted at 1-based position `idx`, shifting later elements one position to the right. The index must be an integer in the range `[I, LONGITVDO(array) + I]`; passing `LONGITVDO(array) + I` is equivalent to `ADDE`.
### NECTE
`NECTE(array1, array2)`
Weaves two arrays together into a new array of two-element pair arrays. The two inputs must have equal length; mismatched lengths raise an error.
### IVNGE
`IVNGE(keys, values)`
Builds a dict by yoking two parallel arrays — the i-th element of `keys` becomes the key for the i-th element of `values`. The two arrays must have equal length. Keys must be strings or integers. If `keys` contains duplicates, the later value wins.
### SENATVS
`SENATVS(bool, ...)` or `SENATVS([bool])`
Returns VERITAS if a strict majority of the arguments are VERITAS, FALSITAS otherwise. Also accepts a single array of booleans. All values must be booleans. Ties return FALSITAS.
### NVMERVS
`NVMERVS(string)`
Parses a Roman numeral string and returns its integer value. The argument must be a string containing a valid Roman numeral. Respects the `MAGNVM` and `SVBNVLLA` modules for large and negative numbers respectively.
### TYPVS
`TYPVS(value)`
Returns the type of `value` as a string: `NVMERVS` (integer), `LITTERA` (string), `VERAX` (boolean), `CATALOGVS` (list), `FRACTIO` (fraction), `TABVLA` (dict), `FVNCTIO` (function), or `NVLLVS` (null).
### LITTERA
`LITTERA(value)`
Returns `value` formatted as the same display string `DIC` would print. Integers become Roman numerals (zero becomes `NVLLVS`), fractions use the `S`/`:`/`.`/`|` notation, booleans become `VERITAS`/`FALSITAS`, arrays are space-separated in brackets, and dicts use the `{ key VT value, ... }` form. Strings pass through unchanged. Respects `MAGNVM` and `SVBNVLLA` for large and negative numbers. Inverse of `NVMERVS` for integers: `NVMERVS(LITTERA(n)) == n`.
### DORMI
`DORMI(n)`
Sleeps for `n` seconds, where `n` is an integer, fraction, or NVLLVS (treated as 0). Returns nothing meaningful.
### QVAERE
`QVAERE(pattern, string)`
Returns an array of all non-overlapping matches of the regex `pattern` in `string`. Both arguments must be strings. Patterns use extended regular expression syntax with Roman numeral quantifiers (`{III}` for exactly 3, `{II,V}` for 25, `{III,}` for 3 or more). Returns an empty array if there are no matches. Raises an error if the pattern is invalid.
### SVBSTITVE
`SVBSTITVE(pattern, replacement, string)`
Replaces all non-overlapping matches of the regex `pattern` in `string` with `replacement`. All three arguments must be strings. The replacement string supports backreferences (`\I`, `\II`, etc.) to captured groups. Returns the resulting string. Raises an error if the pattern is invalid.
### SCINDE
`SCINDE(string, delimiter)`
Splits `string` by `delimiter` and returns an array of substrings. Both arguments must be strings. If the delimiter is not found, returns a single-element array containing the original string. If the delimiter is an empty string, splits into individual characters.
### MAIVSCVLA
`MAIVSCVLA(string)`
Returns a new string with every ASCII letter `a``z` replaced by its uppercase counterpart `A``Z`. All other bytes (digits, punctuation, non-ASCII) pass through unchanged.
### MINVSCVLA
`MINVSCVLA(string)`
Returns a new string with every ASCII letter `A``Z` replaced by its lowercase counterpart `a``z`. All other bytes (digits, punctuation, non-ASCII) pass through unchanged.
## Modules
Modules are additions to the base `CENTVRION` syntax. They add or change certain features. Modules are included in your code by having
![Module declaration](snippets/module_decl.png)
In the beginning of your source file.
Vnlike many other programming languages with modules, the modules in `CENTVRION` are not libraries that can be "imported" from other scripts written in the language. They are features of the compiler, disabled by default.
### FORS
![CVM FORS](snippets/fors.png)
The `FORS` module allows you to add randomness to your `CENTVRION` program. It adds 4 new built-in functions: `FORTVITVS_NVMERVS(int, int)`, `FORTVITA_ELECTIO(['a])`, `DECIMATIO(['a])`, and `SEMEN(int)`.
`FORTVITVS_NVMERVS(int, int)` picks a random int in the (inclusive) range of the two given ints.
`FORTVITA_ELECTIO(['a])` picks a random element from the given array. `FORTVITA_ELECTIO(array)` is identical to ```array[FORTVITVS_NVMERVS(I, LONGITVDO(array))]```.
`DECIMATIO(['a])` returns a copy of the given array with a random tenth of its elements removed. Arrays with fewer than 10 elements are returned unchanged.
`SEMEN(int)` seeds the random number generator for reproducibility.
### FRACTIO
![CVM FRACTIO](snippets/fractio.png)
The `FRACTIO` module adds floats, in the form of base 12 fractions.
In the `FRACTIO` module, `.` represents 1/12, `:` represents 1/6 and `S` represents 1/2. The symbols must be written from highest to lowest. So 3/4 would be written as "`S:.`".
Fractions can be written as an extension of integers. So 3.5 would be "`IIIS`".
The symbol `|` can be used to denote that the following fraction symbols are 1 "level down" in base 12. So after the first `|`, the fraction symbols denote 144ths instead of 12ths. So 7 and 100/144 would be "`VIIS:|::`", as "7 + 100/144" is also "7+8/12+4/144".
A single "set" of fraction symbols can only represent up to 11/12, as 12/12 can be written as 1.
### IASON
> ⚠ **Warning.** The `IASON` module enables your program to read and write non-Roman numerals. Numbers handled by `IASON_LEGE` and `IASON_SCRIBE` use the decimal digits `0``9` (e.g. `42`, `1789`, `30`), not Roman numerals. This goes against the design philosophy of CENTVRION and should not be used unless absolutely necessary.
![CVM IASON](snippets/iason.png)
The `IASON` module adds two builtins for converting between `CENTVRION` values and JSON strings.
`IASON_LEGE(string)` parses a JSON string and returns the corresponding `CENTVRION` value. Mappings: JSON `null` → `NVLLVS`, `true`/`false` → `VERITAS`/`FALSITAS`, integer → numeral, string → string, array → array, object → `TABVLA` (string keys).
JSON floats with no fractional part (e.g. `3.0`) come back as integers. Other floats depend on whether the `FRACTIO` module is also loaded: with `FRACTIO`, `0.1` parses to the exact fraction `I:|::|::|S:.|S.|:` (1/10); without it, the value is floored to the nearest integer.
![IASON_LEGE example](snippets/iason_lege.png)
```
> Marcus
> XXX
> [gladius scutum]
```
`IASON_SCRIBE(value)` serializes a `CENTVRION` value to a JSON string. Integers and fractions become JSON numbers (fractions via shortest-round-trip float), strings become JSON strings (with the standard escapes), arrays become arrays, dicts become objects (insertion order preserved). Functions and dicts with non-string keys raise an error.
![IASON_SCRIBE example](snippets/iason_scribe.png)
```
> {"nomen": "Marcus", "anni": 30}
```
### MAGNVM
![CVM MAGNVM](snippets/magnvm.png)
`MAGNVM` adds the ability to write integers larger than `MMMCMXCIX` (3.999) in your code, by adding the thousands operator, "`_`".
When `_` is added _after_ a numeric symbol, the symbol becomes 1.000 times larger. The operator can be added to the same symbol multiple times. So "`V_`" is 5.000, and "`V__`" is 5.000.000. The strict rules for integers still apply, so 4.999 cannot be written as "`IV_`", but must instead be written as "`MV_CMXCIX`".
All integer symbols except `I` may be given a `_`.
### SCRIPTA
![CVM SCRIPTA](snippets/scripta.png)
The `SCRIPTA` module adds file I/O to your `CENTVRION` program. It adds 3 new built-in functions: `LEGE`, `SCRIBE`, and `ADIVNGE`.
`LEGE(string)` reads the contents of the file at the given path and returns them as a string.
`SCRIBE(string, string)` writes the second argument to the file at the path given by the first argument, overwriting any existing content.
`ADIVNGE(string, string)` appends the second argument to the file at the path given by the first argument.
### RETE
![CVM RETE](snippets/rete.png)
The `RETE` module adds networking to your `CENTVRION` program.
`PETE(string)` performs an HTTP GET request to the given URL and returns the response body as a string.
`PETITVR(string, function)` registers a GET handler for the given path. The handler function receives a single argument: a dictionary with keys `"via"` (the request path), `"quaestio"` (query string), and `"methodus"` (HTTP method). The handler's return value becomes the response body (200 OK). Unmatched paths return a 404.
`AVSCVLTA(integer)` starts an HTTP server on the given port. This call blocks indefinitely, serving registered routes. Routes must be registered with `PETITVR` before calling `AVSCVLTA`. Ports above 3999 require the `MAGNVM` module.
### SVBNVLLA
![CVM SVBNVLLA](snippets/svbnvlla.png)
The `SVBNVLLA` module adds the ability to write negative numbers as `-II` instead of `NVLLVS-II`.

482
README.md
View File

@@ -1,483 +1,21 @@
# About
`CENTVRION` is the programming language for the modern roman.
# Documentation
## Hello World
## Example code
### Hello World
![Hello World](snippets/hello_world.png)
### Recursive Fibonacci number function
## Running
![Fibonacci](snippets/fibonacci.png)
### Number guessing game
![Number guessing game](snippets/guessing.png)
## Variables
Variables are set with the `DESIGNA` and `VT` keywords. Type is inferred.
![Variable assignment](snippets/variable.png)
Variable can consist of lower-case letters, numbers, as well as `_`.
### Compound assignment
`AVGE` (+=), `MINVE` (-=), `MVLTIPLICA` (*=) and `DIVIDE` (/=) are shorthand for updating a variable with an arithmetic operation:
![Compound assignment](snippets/compound.png)
```
> VIII
```bash
./cent -i FILE.cent # interpret a .cent file
./cent -c FILE.cent # compile (not yet implemented)
```
`x AVGE III` is equivalent to `DESIGNA x VT x + III`; `MINVE`, `MVLTIPLICA` and `DIVIDE` expand the same way with subtraction, multiplication and division.
Dependencies: `rply`, `docopt`. Install via `pip install rply docopt`.
### Destructuring
## Documentation
Multiple variables can be assigned at once by unpacking an array or multi-return function:
![Destructuring function](snippets/destructure_fn.png)
The number of targets must match the length of the array. This also works with array literals:
![Destructuring array](snippets/destructure_array.png)
## Data types
### NVLLVS
`NVLLVS` is a special kind of data type in `CENTVRION`, similar to the `null` value in many other languages. `NVLLVS` can be 0 if evaluated as an int or float, or an empty string if evaluated as a string. `NVLLVS` cannot be evaluated as a boolean.
### Strings
Strings are written as text in quotes (`'` or `"`).
![String literal](snippets/string_literal.png)
Strings are concatenated with `&`:
![String concatenation](snippets/string_concat.png)
`NVLLVS` coerces to an empty string when used with `&`. Note: `+` is for arithmetic only — using it on strings raises an error.
#### String Interpolation
Double-quoted strings support interpolation with `{expression}`:
![String interpolation](snippets/string_interp.png)
Any expression can appear inside `{}`. Values are coerced to strings the same way as with `&` (integers become Roman numerals, booleans become `VERITAS`/`FALSITAS`, etc.).
Single-quoted strings do **not** interpolate — `'{nomen}'` is the literal text `{nomen}`. Use `{{` and `}}` for literal braces in double-quoted strings: `"use {{braces}}"``use {braces}`.
#### String Indexing and Slicing
Strings support the same indexing and slicing syntax as arrays. Indexing is 1-based and returns a single-character string:
![String indexing](snippets/string_index.png)
```
> S
> L
```
Slicing uses `VSQVE` with inclusive bounds, returning a substring:
![String slicing](snippets/string_slice.png)
```
> ALV
```
Integer modulo is `RELIQVVM`: `VII RELIQVVM III` evaluates to `I`. Under the `FRACTIO` module it returns a fraction, so `IIIS RELIQVVM IS` is `S` (i.e. 1/2).
### Integers
Integers must be written in roman numerals using the following symbols:
|Symbol|Value|
|------|-----|
|`I`|1|
|`V`|5|
|`X`|10|
|`L`|50|
|`C`|100|
|`D`|500|
|`M`|1000|
Each of the symbols written by themself is equal to the value of the symbol. Different symbols written from largest to smallest are equal to the sum of the symbols. Two to three of the same symbol written consecutively is equal to the sum of those symbols (only true for `I`s, `X`s, `C`s or `M`s ). A single `I` written before a `V` or `X` is equal to 1 less than the value of the second symbol. Similarly, an `X` written before a `L` or `C` is 10 less than the second symbol, and a `C` written before a `D` or `M` is 100 less than the second symbol.
Because of the restrictions of roman numerals, numbers above 3.999 are impossible to write in the base `CENTVRION` syntax. If numbers of that size are required, see the `MAGNVM` module.
The number 0 can be expressed with the keyword `NVLLVS`.
#### Negative numbers
Negative numbers can be expressed as `NVLLVS` minus the value. For an explicit definition of negative numbers, see the `SVBNVLLA` module.
### Floats
The base `CENTVRION` syntax does not allow for floats. However, the `FRACTIO` module adds a syntax for fractions.
### Booleans
Booleans are denoted with the keywords `VERITAS` for true and `FALSITAS` for false.
### Arrays
Arrays are defined using square brackets (`[]`) and commas (`,`):
![Array literal](snippets/array_literal.png)
An array of integers can also be initialized with the `VSQVE` keyword. The range is inclusive on both ends:
![Array with VSQVE](snippets/array_vsqve.png)
```
> [I, II, III, IV, V, VI, VII, VIII, IX, X]
```
Individual elements can be accessed by index using square brackets. Indexing is 1-based, so `I` refers to the first element:
![Array indexing](snippets/array_index.png)
```
> I
```
Arrays are concatenated with `@`:
![Array concatenation](snippets/array_concat.png)
```
> [I, II, III, IV, V]
```
Both operands must be arrays — using `@` on non-arrays raises an error.
A sub-array can be extracted with `VSQVE` inside the index brackets. Both bounds are inclusive and 1-based:
![Array slicing](snippets/array_slice.png)
```
> [XX, XXX, XL]
```
### Dicts (TABVLA)
Dicts are key-value maps created with the `TABVLA` keyword and curly braces:
![Dict creation](snippets/dict_create.png)
Keys must be strings or integers. Values are accessed and assigned with square brackets:
![Dict access](snippets/dict_access.png)
Iterating over a dict with `PER` loops over its keys:
![Dict iteration](snippets/dict_per.png)
`LONGITVDO(dict)` returns the number of entries. `CLAVES(dict)` returns the keys as an array.
## Conditionals
### SI/TVNC
If-then statements are denoted with the keywords `SI` (if) and `TVNC` (then). Thus, the code
![SI/TVNC](snippets/si_tvnc.png)
Will return `I` (1), as the conditional evaluates `x` to be true.
### Boolean expressions
In conditionals, `EST` functions as an equality evaluation, `DISPAR` as not-equal, and `MINVS` (<), `PLVS` (>), `HAVD_PLVS` (≤), and `HAVD_MINVS` (≥) function as inequality evaluation.
### ALIVD
When using `SI`/`TVNC` statements, you can also use `ALIVD` as an "else".
![ALIVD](snippets/alivd.png)
```
> I
```
`SI` statements may follow immediately after `ALIVD`.
![ALIVD SI](snippets/alivd_si.png)
```
> II
```
### Boolean operators
The keyword `ET` can be used as a boolean "and". The keyword `AVT` can be used as a boolean "or".
![Boolean operators](snippets/boolean_ops.png)
```
> II
```
## Loops
### DONICVM loops
![DONICVM loop](snippets/donicvm.png)
```
> LV
```
An optional `GRADV` clause sets the stride. The step must be a nonzero
integer expression; positive values ascend, negative values descend, and
the endpoint is included only when the stride lands on it exactly.
![DONICVM with GRADV](snippets/donicvm_gradv.png)
```
> XXV
```
### DVM loops
![DVM loop](snippets/dvm.png)
```
> XI
```
### AETERNVM loops
`AETERNVM FAC { ... }` is shorthand for an infinite loop — equivalent
to `DVM FALSITAS FAC { ... }` but without relying on `DVM`'s inverted
condition. Exit the loop with `ERVMPE` (or `REDI` from inside a function).
![AETERNVM loop](snippets/aeternvm.png)
```
> X
```
### PER loops
![PER loop](snippets/per.png)
```
> I
> II
> III
> IV
> V
```
Variables can be unpacked in `PER` loops, similar to `DESIGNA` destructuring:
![PER destructuring](snippets/per_destructure.png)
```
> III
> VII
```
## Error handling
Errors can be caught using `TEMPTA` (temptare = to try) and `CAPE` (capere = to catch). The `CAPE` block binds the error message to a variable as a string.
![TEMPTA / CAPE](snippets/tempta_cape.png)
```
> Division by zero
```
If the try block succeeds, the catch block is skipped. If an error occurs in the catch block, it propagates up. `TEMPTA`/`CAPE` blocks can be nested.
## Functions
Functions are defined with the `DEFINI` and `VT` keywords. The `REDI` keyword is used to return. `REDI` can also be used to end the program, if used outside of a function.
Calling a function is done with the `INVOCA` keyword.
![Function definition](snippets/function.png)
```
> CXXI
```
## First-class functions
Functions are first-class values in CENTVRION. They can be assigned to variables, passed as arguments, returned from functions, and stored in arrays or dicts.
Anonymous functions are created with the `FVNCTIO` keyword:
![FVNCTIO](snippets/fvnctio.png)
```
> XIV
```
`INVOCA` accepts any expression as the callee, not just a name:
![INVOCA expressions](snippets/invoca_expr.png)
```
> VI
> VI
> XVI
```
Note: CENTVRION does **not** have closures. When a function is called, it receives a copy of the *caller's* scope, not the scope where it was defined. Variables from a function's definition site are only available if they also exist in the caller's scope at call time.
## Built-ins
### DIC
`DIC(value, ...)`
Prints one or more values to stdout, space-separated, with integers rendered as Roman numerals. Returns the printed string.
![DIC](snippets/dic.png)
### AVDI
`AVDI()`
Reads one line from stdin and returns it as a string.
### AVDI_NVMERVS
`AVDI_NVMERVS()`
Reads one line from stdin, parses it as a Roman numeral, and returns it as an integer. Raises an error if the input is not a valid numeral.
### CONTINVA
`CONTINVA`
Skips the rest of the current loop body and continues to the next iteration (`DVM` or `PER`). Has no meaningful return value.
### ERVMPE
`ERVMPE`
Breaks out of the current loop (`DVM` or `PER`). Has no meaningful return value.
### LONGITVDO
`LONGITVDO(array)`, `LONGITVDO(string)`, or `LONGITVDO(dict)`
Returns the length of `array` (element count), `string` (character count), or `dict` (entry count) as an integer.
### CLAVES
`CLAVES(dict)`
Returns the keys of `dict` as an array.
### ORDINA
`ORDINA(array)`
Sorts an array in ascending order. Returns a new sorted array. All elements must be the same type — integers, fractions, or strings. Integers and fractions sort numerically; strings sort lexicographically.
### SENATVS
`SENATVS(bool, ...)` or `SENATVS([bool])`
Returns VERITAS if a strict majority of the arguments are VERITAS, FALSITAS otherwise. Also accepts a single array of booleans. All values must be booleans. Ties return FALSITAS.
### NVMERVS
`NVMERVS(string)`
Parses a Roman numeral string and returns its integer value. The argument must be a string containing a valid Roman numeral. Respects the `MAGNVM` and `SVBNVLLA` modules for large and negative numbers respectively.
### TYPVS
`TYPVS(value)`
Returns the type of `value` as a string: `NVMERVS` (integer), `LITTERA` (string), `VERAX` (boolean), `CATALOGVS` (list), `FRACTIO` (fraction), `TABVLA` (dict), `FVNCTIO` (function), or `NVLLVS` (null).
### LITTERA
`LITTERA(value)`
Returns `value` formatted as the same display string `DIC` would print. Integers become Roman numerals (zero becomes `NVLLVS`), fractions use the `S`/`:`/`.`/`|` notation, booleans become `VERITAS`/`FALSITAS`, arrays are space-separated in brackets, and dicts use the `{ key VT value, ... }` form. Strings pass through unchanged. Respects `MAGNVM` and `SVBNVLLA` for large and negative numbers. Inverse of `NVMERVS` for integers: `NVMERVS(LITTERA(n)) == n`.
### DORMI
`DORMI(n)`
Sleeps for `n` seconds, where `n` is an integer, fraction, or NVLLVS (treated as 0). Returns nothing meaningful.
### QVAERE
`QVAERE(pattern, string)`
Returns an array of all non-overlapping matches of the regex `pattern` in `string`. Both arguments must be strings. Patterns use extended regular expression syntax with Roman numeral quantifiers (`{III}` for exactly 3, `{II,V}` for 25, `{III,}` for 3 or more). Returns an empty array if there are no matches. Raises an error if the pattern is invalid.
### SVBSTITVE
`SVBSTITVE(pattern, replacement, string)`
Replaces all non-overlapping matches of the regex `pattern` in `string` with `replacement`. All three arguments must be strings. The replacement string supports backreferences (`\I`, `\II`, etc.) to captured groups. Returns the resulting string. Raises an error if the pattern is invalid.
### SCINDE
`SCINDE(string, delimiter)`
Splits `string` by `delimiter` and returns an array of substrings. Both arguments must be strings. If the delimiter is not found, returns a single-element array containing the original string. If the delimiter is an empty string, splits into individual characters.
### MAIVSCVLA
`MAIVSCVLA(string)`
Returns a new string with every ASCII letter `a``z` replaced by its uppercase counterpart `A``Z`. All other bytes (digits, punctuation, non-ASCII) pass through unchanged.
### MINVSCVLA
`MINVSCVLA(string)`
Returns a new string with every ASCII letter `A``Z` replaced by its lowercase counterpart `a``z`. All other bytes (digits, punctuation, non-ASCII) pass through unchanged.
## Modules
Modules are additions to the base `CENTVRION` syntax. They add or change certain features. Modules are included in your code by having
![Module declaration](snippets/module_decl.png)
In the beginning of your source file.
Vnlike many other programming languages with modules, the modules in `CENTVRION` are not libraries that can be "imported" from other scripts written in the language. They are features of the compiler, disabled by default.
### FORS
![CVM FORS](snippets/fors.png)
The `FORS` module allows you to add randomness to your `CENTVRION` program. It adds 4 new built-in functions: `FORTVITVS_NVMERVS(int, int)`, `FORTVITA_ELECTIO(['a])`, `DECIMATIO(['a])`, and `SEMEN(int)`.
`FORTVITVS_NVMERVS(int, int)` picks a random int in the (inclusive) range of the two given ints.
`FORTVITA_ELECTIO(['a])` picks a random element from the given array. `FORTVITA_ELECTIO(array)` is identical to ```array[FORTVITVS_NVMERVS(I, LONGITVDO(array))]```.
`DECIMATIO(['a])` returns a copy of the given array with a random tenth of its elements removed. Arrays with fewer than 10 elements are returned unchanged.
`SEMEN(int)` seeds the random number generator for reproducibility.
### FRACTIO
![CVM FRACTIO](snippets/fractio.png)
The `FRACTIO` module adds floats, in the form of base 12 fractions.
In the `FRACTIO` module, `.` represents 1/12, `:` represents 1/6 and `S` represents 1/2. The symbols must be written from highest to lowest. So 3/4 would be written as "`S:.`".
Fractions can be written as an extension of integers. So 3.5 would be "`IIIS`".
The symbol `|` can be used to denote that the following fraction symbols are 1 "level down" in base 12. So after the first `|`, the fraction symbols denote 144ths instead of 12ths. So 7 and 100/144 would be "`VIIS:|::`", as "7 + 100/144" is also "7+8/12+4/144".
A single "set" of fraction symbols can only represent up to 11/12, as 12/12 can be written as 1.
### MAGNVM
![CVM MAGNVM](snippets/magnvm.png)
`MAGNVM` adds the ability to write integers larger than `MMMCMXCIX` (3.999) in your code, by adding the thousands operator, "`_`".
When `_` is added _after_ a numeric symbol, the symbol becomes 1.000 times larger. The operator can be added to the same symbol multiple times. So "`V_`" is 5.000, and "`V__`" is 5.000.000. The strict rules for integers still apply, so 4.999 cannot be written as "`IV_`", but must instead be written as "`MV_CMXCIX`".
All integer symbols except `I` may be given a `_`.
### SCRIPTA
![CVM SCRIPTA](snippets/scripta.png)
The `SCRIPTA` module adds file I/O to your `CENTVRION` program. It adds 3 new built-in functions: `LEGE`, `SCRIBE`, and `ADIVNGE`.
`LEGE(string)` reads the contents of the file at the given path and returns them as a string.
`SCRIBE(string, string)` writes the second argument to the file at the path given by the first argument, overwriting any existing content.
`ADIVNGE(string, string)` appends the second argument to the file at the path given by the first argument.
### RETE
![CVM RETE](snippets/rete.png)
The `RETE` module adds networking to your `CENTVRION` program.
`PETE(string)` performs an HTTP GET request to the given URL and returns the response body as a string.
`PETITVR(string, function)` registers a GET handler for the given path. The handler function receives a single argument: a dictionary with keys `"via"` (the request path), `"quaestio"` (query string), and `"methodus"` (HTTP method). The handler's return value becomes the response body (200 OK). Unmatched paths return a 404.
`AVSCVLTA(integer)` starts an HTTP server on the given port. This call blocks indefinitely, serving registered routes. Routes must be registered with `PETITVR` before calling `AVSCVLTA`. Ports above 3999 require the `MAGNVM` module.
### SVBNVLLA
![CVM SVBNVLLA](snippets/svbnvlla.png)
The `SVBNVLLA` module adds the ability to write negative numbers as `-II` instead of `NVLLVS-II`.
- [`DOCS.md`](DOCS.md) — full language reference: syntax, data types, built-ins, modules.
- [`language/main.pdf`](language/main.pdf) — formal grammar.
- [`examples/`](examples/) — example programs.

10
cent
View File

@@ -53,17 +53,19 @@ def main():
sys.exit(f"CENTVRION error: {e}")
else:
c_source = compile_program(program)
runtime_c = os.path.join(
runtime_dir = os.path.join(
os.path.dirname(__file__),
"centvrion", "compiler", "runtime", "cent_runtime.c"
"centvrion", "compiler", "runtime"
)
runtime_c = os.path.join(runtime_dir, "cent_runtime.c")
iason_c = os.path.join(runtime_dir, "cent_iason.c")
out_path = os.path.splitext(file_path)[0]
if args["--keep-c"]:
tmp_path = out_path + ".c"
with open(tmp_path, "w") as f:
f.write(c_source)
subprocess.run(
["gcc", "-O2", tmp_path, runtime_c, "-o", out_path, "-lcurl", "-lmicrohttpd"],
["gcc", "-O2", tmp_path, runtime_c, iason_c, "-o", out_path, "-lcurl", "-lmicrohttpd", "-lm"],
check=True,
)
else:
@@ -72,7 +74,7 @@ def main():
tmp_path = tmp.name
try:
subprocess.run(
["gcc", "-O2", tmp_path, runtime_c, "-o", out_path, "-lcurl", "-lmicrohttpd"],
["gcc", "-O2", tmp_path, runtime_c, iason_c, "-o", out_path, "-lcurl", "-lmicrohttpd", "-lm"],
check=True,
)
finally:

View File

@@ -1,4 +1,7 @@
import functools
import http.server
import json
import math
import re
import time
import urllib.parse
@@ -289,6 +292,51 @@ def frac_to_fraction(s, magnvm=False, svbnvlla=False):
return total
def _json_to_val(obj):
if obj is None:
return ValNul()
if isinstance(obj, bool):
return ValBool(obj)
if isinstance(obj, int):
return ValInt(obj)
if isinstance(obj, Fraction):
if obj.denominator == 1:
return ValInt(obj.numerator)
return ValFrac(obj)
if isinstance(obj, str):
return ValStr(obj)
if isinstance(obj, list):
return ValList([_json_to_val(x) for x in obj])
if isinstance(obj, dict):
return ValDict({k: _json_to_val(v) for k, v in obj.items()})
raise CentvrionError(f"IASON_LEGE: unsupported JSON value of type {type(obj).__name__}")
def _val_to_json(val):
if isinstance(val, ValNul):
return None
if isinstance(val, ValBool):
return val.value()
if isinstance(val, ValInt):
return val.value()
if isinstance(val, ValFrac):
return float(val.value())
if isinstance(val, ValStr):
return val.value()
if isinstance(val, ValList):
return [_val_to_json(x) for x in val.value()]
if isinstance(val, ValDict):
out = {}
for k, v in val.value().items():
if not isinstance(k, str):
raise CentvrionError("IASON_SCRIBE: dict keys must be strings to serialize as JSON")
out[k] = _val_to_json(v)
return out
if isinstance(val, ValFunc):
raise CentvrionError("IASON_SCRIBE: cannot serialize a function")
raise CentvrionError(f"IASON_SCRIBE: cannot serialize value of type {type(val).__name__}")
def fraction_to_frac(f, magnvm=False, svbnvlla=False) -> str:
if f < 0:
if not svbnvlla:
@@ -317,8 +365,15 @@ def fraction_to_frac(f, magnvm=False, svbnvlla=False) -> str:
class Node(BaseBox):
pos = None # (lineno, colno) — set in parser productions
def eval(self, vtable):
return self._eval(vtable.copy())
try:
return self._eval(vtable.copy())
except CentvrionError as e:
if e.lineno is None and self.pos is not None:
e.lineno, e.colno = self.pos
raise
def _eval(self, vtable):
raise NotImplementedError
@@ -1320,7 +1375,7 @@ class TemptaStatement(Node):
if vtable["#return"] is not None or vtable["#break"] or vtable["#continue"]:
return vtable, last_val
except CentvrionError as e:
vtable[self.error_var.name] = ValStr(str(e))
vtable[self.error_var.name] = ValStr(e.msg)
for statement in self.catch_statements:
vtable, last_val = statement.eval(vtable)
if vtable["#return"] is not None or vtable["#break"] or vtable["#continue"]:
@@ -1328,6 +1383,22 @@ class TemptaStatement(Node):
return vtable, last_val
def _call_func(func: ValFunc, args: list, vtable: dict, callee_desc: str = "function"):
if len(args) != len(func.params):
raise CentvrionError(
f"{callee_desc} expects {len(func.params)} argument(s), got {len(args)}"
)
func_vtable = vtable.copy()
for i, param in enumerate(func.params):
func_vtable[param.name] = args[i]
func_vtable["#return"] = None
for statement in func.body:
func_vtable, _ = statement.eval(func_vtable)
if func_vtable["#return"] is not None:
return func_vtable["#return"]
return ValNul()
class Invoca(Node):
def __init__(self, callee, parameters) -> None:
self.callee = callee
@@ -1354,21 +1425,9 @@ class Invoca(Node):
callee_desc = (self.callee.name
if isinstance(self.callee, ID) else "expression")
raise CentvrionError(f"{callee_desc} is not a function")
if len(params) != len(func.params):
callee_desc = (self.callee.name
if isinstance(self.callee, ID) else "FVNCTIO")
raise CentvrionError(
f"{callee_desc} expects {len(func.params)} argument(s), got {len(params)}"
)
func_vtable = vtable.copy()
for i, param in enumerate(func.params):
func_vtable[param.name] = params[i]
func_vtable["#return"] = None
for statement in func.body:
func_vtable, _ = statement.eval(func_vtable)
if func_vtable["#return"] is not None:
return vtable, func_vtable["#return"]
return vtable, ValNul()
callee_desc = (self.callee.name
if isinstance(self.callee, ID) else "FVNCTIO")
return vtable, _call_func(func, params, vtable, callee_desc)
class BuiltIn(Node):
@@ -1496,17 +1555,130 @@ class BuiltIn(Node):
case "EVERRE":
print("\033[2J\033[H", end="", flush=True)
return vtable, ValNul()
case "ADDE":
if len(params) != 2:
raise CentvrionError("ADDE takes exactly II arguments")
if not isinstance(params[0], ValList):
raise CentvrionError("ADDE requires an array")
return vtable, ValList(list(params[0].value()) + [params[1]])
case "TOLLE":
if len(params) != 2:
raise CentvrionError("TOLLE takes exactly II arguments")
if not isinstance(params[0], ValList):
raise CentvrionError("TOLLE requires an array")
items = list(params[0].value())
idx = _to_index_int(params[1])
if idx < 1 or idx > len(items):
raise CentvrionError(f"Index {idx} out of range for array of length {len(items)}")
return vtable, ValList(items[:idx - 1] + items[idx:])
case "INSERE":
if len(params) != 3:
raise CentvrionError("INSERE takes exactly III arguments")
if not isinstance(params[0], ValList):
raise CentvrionError("INSERE requires an array")
items = list(params[0].value())
idx = _to_index_int(params[1])
if idx < 1 or idx > len(items) + 1:
raise CentvrionError(f"Index {idx} out of range for array of length {len(items)}")
return vtable, ValList(items[:idx - 1] + [params[2]] + items[idx - 1:])
case "NECTE":
if len(params) != 2:
raise CentvrionError("NECTE takes exactly II arguments")
if not isinstance(params[0], ValList) or not isinstance(params[1], ValList):
raise CentvrionError("NECTE requires two arrays")
a, b = list(params[0].value()), list(params[1].value())
if len(a) != len(b):
raise CentvrionError("NECTE requires arrays of equal length")
return vtable, ValList([ValList([x, y]) for x, y in zip(a, b)])
case "IVNGE":
if len(params) != 2:
raise CentvrionError("IVNGE takes exactly II arguments")
if not isinstance(params[0], ValList) or not isinstance(params[1], ValList):
raise CentvrionError("IVNGE requires two arrays")
keys, vals = list(params[0].value()), list(params[1].value())
if len(keys) != len(vals):
raise CentvrionError("IVNGE requires arrays of equal length")
d = {}
for k, v in zip(keys, vals):
if not isinstance(k, (ValStr, ValInt)):
raise CentvrionError("Dict keys must be strings or integers")
d[k.value()] = v
return vtable, ValDict(d)
case "ORDINA":
if not 1 <= len(params) <= 2:
raise CentvrionError("ORDINA takes 1 or 2 arguments")
if not isinstance(params[0], ValList):
raise CentvrionError("ORDINA requires an array")
items = list(params[0].value())
if not items:
return vtable, ValList([])
if len(params) == 2:
cmp = params[1]
if not isinstance(cmp, ValFunc):
raise CentvrionError("ORDINA comparator must be a function")
if len(cmp.params) != 2:
raise CentvrionError("ORDINA comparator must take 2 arguments")
def adapter(a, b):
r = _call_func(cmp, [a, b], vtable, "ORDINA comparator")
if not isinstance(r, ValBool):
raise CentvrionError("ORDINA comparator must return VERAX")
if r.value():
return -1
r2 = _call_func(cmp, [b, a], vtable, "ORDINA comparator")
if not isinstance(r2, ValBool):
raise CentvrionError("ORDINA comparator must return VERAX")
return 1 if r2.value() else 0
return vtable, ValList(sorted(items, key=functools.cmp_to_key(adapter)))
all_numeric = all(isinstance(i, (ValInt, ValFrac)) for i in items)
all_string = all(isinstance(i, ValStr) for i in items)
if not (all_numeric or all_string):
raise CentvrionError("ORDINA requires all elements to be numbers or all strings")
return vtable, ValList(sorted(items, key=lambda v: v.value()))
case "MVTA":
if len(params) != 2:
raise CentvrionError("MVTA takes II arguments")
if not isinstance(params[0], ValList):
raise CentvrionError("MVTA requires an array")
fn = params[1]
if not isinstance(fn, ValFunc):
raise CentvrionError("MVTA requires a function")
if len(fn.params) != 1:
raise CentvrionError("MVTA function must take I argument")
out = [_call_func(fn, [item], vtable, "MVTA function")
for item in params[0].value()]
return vtable, ValList(out)
case "CRIBRA":
if len(params) != 2:
raise CentvrionError("CRIBRA takes II arguments")
if not isinstance(params[0], ValList):
raise CentvrionError("CRIBRA requires an array")
fn = params[1]
if not isinstance(fn, ValFunc):
raise CentvrionError("CRIBRA requires a function")
if len(fn.params) != 1:
raise CentvrionError("CRIBRA predicate must take I argument")
out = []
for item in params[0].value():
r = _call_func(fn, [item], vtable, "CRIBRA predicate")
if not isinstance(r, ValBool):
raise CentvrionError("CRIBRA predicate must return VERAX")
if r.value():
out.append(item)
return vtable, ValList(out)
case "CONFLA":
if len(params) != 3:
raise CentvrionError("CONFLA takes III arguments")
if not isinstance(params[0], ValList):
raise CentvrionError("CONFLA requires an array")
fn = params[2]
if not isinstance(fn, ValFunc):
raise CentvrionError("CONFLA requires a function")
if len(fn.params) != 2:
raise CentvrionError("CONFLA function must take II arguments")
acc = params[1]
for item in params[0].value():
acc = _call_func(fn, [acc, item], vtable, "CONFLA function")
return vtable, acc
case "TYPVS":
type_map = {
ValInt: "NVMERVS", ValStr: "LITTERA", ValBool: "VERAX",
@@ -1657,6 +1829,30 @@ class BuiltIn(Node):
server = http.server.HTTPServer(("0.0.0.0", port.value()), _CentHandler)
server.serve_forever()
return vtable, ValNul()
case "IASON_LEGE":
if "IASON" not in vtable["#modules"]:
raise CentvrionError("Cannot use 'IASON_LEGE' without module 'IASON'")
if len(params) != 1:
raise CentvrionError("IASON_LEGE takes exactly I argument")
s = params[0]
if not isinstance(s, ValStr):
raise CentvrionError("IASON_LEGE requires a string")
fractio = "FRACTIO" in vtable["#modules"]
try:
if fractio:
parsed = json.loads(s.value(), parse_float=Fraction)
else:
parsed = json.loads(s.value(), parse_float=lambda x: math.floor(float(x)))
except json.JSONDecodeError as e:
raise CentvrionError(f"IASON_LEGE: invalid JSON: {e.msg}")
return vtable, _json_to_val(parsed)
case "IASON_SCRIBE":
if "IASON" not in vtable["#modules"]:
raise CentvrionError("Cannot use 'IASON_SCRIBE' without module 'IASON'")
if len(params) != 1:
raise CentvrionError("IASON_SCRIBE takes exactly I argument")
obj = _val_to_json(params[0])
return vtable, ValStr(json.dumps(obj, ensure_ascii=False))
case _:
raise NotImplementedError(self.builtin)

View File

@@ -7,6 +7,14 @@ from centvrion.ast_nodes import (
num_to_int, frac_to_fraction,
)
def _err(node, msg):
"""Build a CentvrionError stamped with a node's source position, if any."""
pos = getattr(node, "pos", None)
if pos is not None:
return CentvrionError(msg, pos[0], pos[1])
return CentvrionError(msg)
_BINOP_FN = {
"SYMBOL_PLUS": "cent_add",
"SYMBOL_MINUS": "cent_sub",
@@ -84,7 +92,7 @@ def emit_expr(node, ctx):
if isinstance(node, Fractio):
if not ctx.has_module("FRACTIO"):
raise CentvrionError("Cannot use fraction literals without 'FRACTIO' module")
raise _err(node, "Cannot use fraction literals without 'FRACTIO' module")
tmp = ctx.fresh_tmp()
magnvm = "MAGNVM" in ctx.modules
svbnvlla = "SVBNVLLA" in ctx.modules
@@ -311,7 +319,50 @@ def _emit_builtin(node, ctx):
lines.append(f"CentValue {tmp} = cent_dict_keys({param_vars[0]});")
case "ORDINA":
lines.append(f"CentValue {tmp} = cent_ordina({param_vars[0]});")
if len(param_vars) == 1:
lines.append(f"CentValue {tmp} = cent_ordina({param_vars[0]});")
elif len(param_vars) == 2:
lines.append(
f"CentValue {tmp} = cent_ordina_cmp({param_vars[0]}, {param_vars[1]}, _scope);"
)
else:
raise _err(node, "ORDINA takes 1 or 2 arguments")
case "MVTA":
if len(param_vars) != 2:
raise _err(node, "MVTA takes II arguments")
lines.append(
f"CentValue {tmp} = cent_mvta({param_vars[0]}, {param_vars[1]}, _scope);"
)
case "CRIBRA":
if len(param_vars) != 2:
raise _err(node, "CRIBRA takes II arguments")
lines.append(
f"CentValue {tmp} = cent_cribra({param_vars[0]}, {param_vars[1]}, _scope);"
)
case "CONFLA":
if len(param_vars) != 3:
raise _err(node, "CONFLA takes III arguments")
lines.append(
f"CentValue {tmp} = cent_confla({param_vars[0]}, {param_vars[1]}, {param_vars[2]}, _scope);"
)
case "ADDE":
lines.append(f"CentValue {tmp} = cent_adde({param_vars[0]}, {param_vars[1]});")
case "TOLLE":
lines.append(f"CentValue {tmp} = cent_tolle({param_vars[0]}, {param_vars[1]});")
case "INSERE":
lines.append(f"CentValue {tmp} = cent_insere({param_vars[0]}, {param_vars[1]}, {param_vars[2]});")
case "NECTE":
lines.append(f"CentValue {tmp} = cent_necte({param_vars[0]}, {param_vars[1]});")
case "IVNGE":
lines.append(f"CentValue {tmp} = cent_ivnge({param_vars[0]}, {param_vars[1]});")
case "EVERRE":
lines.append("cent_everre();")
@@ -386,6 +437,25 @@ def _emit_builtin(node, ctx):
lines.append(f"cent_avscvlta({param_vars[0]});")
lines.append(f"CentValue {tmp} = cent_null();")
case "IASON_LEGE":
if not ctx.has_module("IASON"):
lines.append('cent_runtime_error("IASON module required for IASON_LEGE");')
lines.append(f"CentValue {tmp} = cent_null();")
elif len(param_vars) != 1:
raise _err(node, "IASON_LEGE takes exactly I argument")
else:
fractio_flag = "1" if ctx.has_module("FRACTIO") else "0"
lines.append(f"CentValue {tmp} = cent_iason_lege({param_vars[0]}, {fractio_flag});")
case "IASON_SCRIBE":
if not ctx.has_module("IASON"):
lines.append('cent_runtime_error("IASON module required for IASON_SCRIBE");')
lines.append(f"CentValue {tmp} = cent_null();")
elif len(param_vars) != 1:
raise _err(node, "IASON_SCRIBE takes exactly I argument")
else:
lines.append(f"CentValue {tmp} = cent_iason_scribe({param_vars[0]});")
case _:
raise NotImplementedError(node.builtin)
@@ -413,7 +483,7 @@ def _emit_invoca(node, ctx):
lines.append(f"CentScope {call_scope_var} = cent_scope_copy(&_scope);")
param_names = ctx.functions[c_func_name]
if len(param_vars) != len(param_names):
raise CentvrionError(
raise _err(node,
f"Function '{node.callee.name}' expects {len(param_names)} argument(s), "
f"got {len(param_vars)}"
)

View File

@@ -11,6 +11,14 @@ def emit_stmt(node, ctx):
Emit C code for a CENTVRION statement node.
Returns lines — list of C statements.
"""
body = _emit_stmt_body(node, ctx)
pos = getattr(node, "pos", None)
if pos is not None:
return [f"_cent_current_line = {pos[0]};"] + body
return body
def _emit_stmt_body(node, ctx):
if isinstance(node, Designa):
val_lines, val_var = emit_expr(node.value, ctx)
return val_lines + [f'cent_scope_set(&_scope, "{node.id.name}", {val_var});']

View File

@@ -58,6 +58,7 @@ def compile_program(program):
# Includes
lines += [
f'#include "{_RUNTIME_DIR}/cent_runtime.h"',
f'#include "{_RUNTIME_DIR}/cent_iason.h"',
"",
]

View File

@@ -0,0 +1,426 @@
#include "cent_iason.h"
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ---------- shared helpers ----------------------------------------- */
static long iason_gcd(long a, long b) {
if (a < 0) a = -a;
if (b < 0) b = -b;
while (b) { long t = b; b = a % b; a = t; }
return a ? a : 1;
}
static CentValue iason_frac_reduce(long num, long den) {
if (den < 0) { num = -num; den = -den; }
long g = iason_gcd(num, den);
num /= g; den /= g;
if (den == 1) return cent_int(num);
return cent_frac(num, den);
}
/* ---------- parser ------------------------------------------------- */
typedef struct {
const char *src;
size_t pos;
size_t len;
int fractio;
} IasonParser;
static void iason_die(const char *msg) {
cent_runtime_error(msg);
}
static void iason_skip_ws(IasonParser *p) {
while (p->pos < p->len) {
char c = p->src[p->pos];
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') p->pos++;
else break;
}
}
static int iason_peek(IasonParser *p) {
return (p->pos < p->len) ? (unsigned char)p->src[p->pos] : -1;
}
static void iason_expect(IasonParser *p, char c, const char *msg) {
if (p->pos >= p->len || p->src[p->pos] != c)
iason_die(msg);
p->pos++;
}
static void iason_expect_word(IasonParser *p, const char *word) {
size_t n = strlen(word);
if (p->len - p->pos < n || memcmp(p->src + p->pos, word, n) != 0)
iason_die("IASON_LEGE: invalid JSON literal");
p->pos += n;
}
/* Encode a Unicode codepoint as UTF-8 into buf; returns bytes written. */
static int iason_utf8_encode(unsigned cp, char *buf) {
if (cp <= 0x7F) { buf[0] = (char)cp; return 1; }
if (cp <= 0x7FF) { buf[0] = (char)(0xC0 | (cp >> 6));
buf[1] = (char)(0x80 | (cp & 0x3F)); return 2; }
if (cp <= 0xFFFF) { buf[0] = (char)(0xE0 | (cp >> 12));
buf[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
buf[2] = (char)(0x80 | (cp & 0x3F)); return 3; }
if (cp <= 0x10FFFF) { buf[0] = (char)(0xF0 | (cp >> 18));
buf[1] = (char)(0x80 | ((cp >> 12) & 0x3F));
buf[2] = (char)(0x80 | ((cp >> 6) & 0x3F));
buf[3] = (char)(0x80 | (cp & 0x3F)); return 4; }
iason_die("IASON_LEGE: codepoint out of range");
return 0;
}
static unsigned iason_read_hex4(IasonParser *p) {
if (p->len - p->pos < 4) iason_die("IASON_LEGE: truncated \\u escape");
unsigned v = 0;
for (int i = 0; i < 4; i++) {
char c = p->src[p->pos++];
v <<= 4;
if (c >= '0' && c <= '9') v |= c - '0';
else if (c >= 'a' && c <= 'f') v |= c - 'a' + 10;
else if (c >= 'A' && c <= 'F') v |= c - 'A' + 10;
else iason_die("IASON_LEGE: invalid hex in \\u escape");
}
return v;
}
/* Parses a JSON string literal at p->pos (positioned at the opening "),
returns an arena-allocated NUL-terminated UTF-8 string. */
static char *iason_parse_string(IasonParser *p) {
iason_expect(p, '"', "IASON_LEGE: expected string");
/* upper bound on output: same as remaining input (escapes shrink). */
size_t cap = (p->len - p->pos) + 1;
char *buf = cent_arena_alloc(cent_arena, cap);
size_t out = 0;
while (p->pos < p->len) {
unsigned char c = (unsigned char)p->src[p->pos++];
if (c == '"') { buf[out] = '\0'; return buf; }
if (c == '\\') {
if (p->pos >= p->len) iason_die("IASON_LEGE: trailing \\ in string");
char esc = p->src[p->pos++];
switch (esc) {
case '"': buf[out++] = '"'; break;
case '\\': buf[out++] = '\\'; break;
case '/': buf[out++] = '/'; break;
case 'b': buf[out++] = '\b'; break;
case 'f': buf[out++] = '\f'; break;
case 'n': buf[out++] = '\n'; break;
case 'r': buf[out++] = '\r'; break;
case 't': buf[out++] = '\t'; break;
case 'u': {
unsigned cp = iason_read_hex4(p);
if (cp >= 0xD800 && cp <= 0xDBFF) {
/* high surrogate; expect \uXXXX low surrogate */
if (p->len - p->pos < 6 || p->src[p->pos] != '\\' || p->src[p->pos + 1] != 'u')
iason_die("IASON_LEGE: missing low surrogate after high surrogate");
p->pos += 2;
unsigned lo = iason_read_hex4(p);
if (lo < 0xDC00 || lo > 0xDFFF)
iason_die("IASON_LEGE: invalid low surrogate");
cp = 0x10000 + (((cp - 0xD800) << 10) | (lo - 0xDC00));
} else if (cp >= 0xDC00 && cp <= 0xDFFF) {
iason_die("IASON_LEGE: stray low surrogate");
}
out += iason_utf8_encode(cp, buf + out);
break;
}
default: iason_die("IASON_LEGE: invalid escape sequence");
}
} else if (c < 0x20) {
iason_die("IASON_LEGE: unescaped control character in string");
} else {
buf[out++] = (char)c;
}
}
iason_die("IASON_LEGE: unterminated string");
return NULL;
}
/* Cap on fractional digits parsed exactly; beyond this we truncate to
keep `long` arithmetic safe (10^18 fits in int64). */
#define IASON_MAX_FRAC_DIGITS 18
static CentValue iason_parse_number(IasonParser *p) {
size_t start = p->pos;
int negative = 0;
if (p->src[p->pos] == '-') { negative = 1; p->pos++; }
/* Integer part. */
if (p->pos >= p->len || !isdigit((unsigned char)p->src[p->pos]))
iason_die("IASON_LEGE: invalid number");
if (p->src[p->pos] == '0') {
p->pos++;
} else {
while (p->pos < p->len && isdigit((unsigned char)p->src[p->pos])) p->pos++;
}
int has_frac = 0, has_exp = 0;
size_t frac_start = 0, frac_end = 0;
if (p->pos < p->len && p->src[p->pos] == '.') {
has_frac = 1;
p->pos++;
frac_start = p->pos;
if (p->pos >= p->len || !isdigit((unsigned char)p->src[p->pos]))
iason_die("IASON_LEGE: invalid number");
while (p->pos < p->len && isdigit((unsigned char)p->src[p->pos])) p->pos++;
frac_end = p->pos;
}
long exp = 0;
if (p->pos < p->len && (p->src[p->pos] == 'e' || p->src[p->pos] == 'E')) {
has_exp = 1;
p->pos++;
int esign = 1;
if (p->pos < p->len && (p->src[p->pos] == '+' || p->src[p->pos] == '-')) {
if (p->src[p->pos] == '-') esign = -1;
p->pos++;
}
if (p->pos >= p->len || !isdigit((unsigned char)p->src[p->pos]))
iason_die("IASON_LEGE: invalid number");
while (p->pos < p->len && isdigit((unsigned char)p->src[p->pos])) {
exp = exp * 10 + (p->src[p->pos] - '0');
p->pos++;
}
exp *= esign;
}
size_t end = p->pos;
if (!has_frac && !has_exp) {
/* Pure integer. Use strtol-style parse (we already validated digits). */
long v = 0;
for (size_t i = (negative ? start + 1 : start); i < end; i++) {
v = v * 10 + (p->src[i] - '0');
}
return cent_int(negative ? -v : v);
}
if (!p->fractio) {
/* Floor to int. strtod handles the full grammar here. */
char *tmp = cent_arena_alloc(cent_arena, end - start + 1);
memcpy(tmp, p->src + start, end - start);
tmp[end - start] = '\0';
double d = strtod(tmp, NULL);
return cent_int((long)floor(d));
}
/* FRACTIO loaded: build an exact fraction from the decimal/exponent form. */
long num = 0;
/* Integer-part digits */
size_t int_start = negative ? start + 1 : start;
size_t int_end = has_frac ? (frac_start - 1) : (has_exp ? end - 0 : end);
/* If we have an exponent without a fraction part, find where digits end. */
if (has_exp && !has_frac) {
int_end = int_start;
while (int_end < p->len && isdigit((unsigned char)p->src[int_end])) int_end++;
}
for (size_t i = int_start; i < int_end; i++) {
num = num * 10 + (p->src[i] - '0');
}
/* Fractional digits, capped */
long den = 1;
if (has_frac) {
size_t take = frac_end - frac_start;
if (take > IASON_MAX_FRAC_DIGITS) take = IASON_MAX_FRAC_DIGITS;
for (size_t i = 0; i < take; i++) {
num = num * 10 + (p->src[frac_start + i] - '0');
den *= 10;
}
}
if (negative) num = -num;
/* Apply exponent: positive shifts num, negative shifts den. */
while (exp > 0) { num *= 10; exp--; }
while (exp < 0) { den *= 10; exp++; }
return iason_frac_reduce(num, den);
}
static CentValue iason_parse_value(IasonParser *p);
static CentValue iason_parse_array(IasonParser *p) {
iason_expect(p, '[', "IASON_LEGE: expected [");
iason_skip_ws(p);
CentValue lst = cent_list_new(4);
if (iason_peek(p) == ']') { p->pos++; return lst; }
for (;;) {
iason_skip_ws(p);
CentValue elem = iason_parse_value(p);
cent_list_push(&lst, elem);
iason_skip_ws(p);
int c = iason_peek(p);
if (c == ',') { p->pos++; continue; }
if (c == ']') { p->pos++; return lst; }
iason_die("IASON_LEGE: expected , or ] in array");
}
}
static CentValue iason_parse_object(IasonParser *p) {
iason_expect(p, '{', "IASON_LEGE: expected {");
iason_skip_ws(p);
CentValue d = cent_dict_new(4);
if (iason_peek(p) == '}') { p->pos++; return d; }
for (;;) {
iason_skip_ws(p);
if (iason_peek(p) != '"') iason_die("IASON_LEGE: object key must be a string");
char *key = iason_parse_string(p);
iason_skip_ws(p);
iason_expect(p, ':', "IASON_LEGE: expected : after object key");
iason_skip_ws(p);
CentValue val = iason_parse_value(p);
cent_dict_set(&d, cent_str(key), val);
iason_skip_ws(p);
int c = iason_peek(p);
if (c == ',') { p->pos++; continue; }
if (c == '}') { p->pos++; return d; }
iason_die("IASON_LEGE: expected , or } in object");
}
}
static CentValue iason_parse_value(IasonParser *p) {
iason_skip_ws(p);
int c = iason_peek(p);
if (c < 0) iason_die("IASON_LEGE: unexpected end of input");
if (c == '{') return iason_parse_object(p);
if (c == '[') return iason_parse_array(p);
if (c == '"') return cent_str(iason_parse_string(p));
if (c == 't') { iason_expect_word(p, "true"); return cent_bool(1); }
if (c == 'f') { iason_expect_word(p, "false"); return cent_bool(0); }
if (c == 'n') { iason_expect_word(p, "null"); return cent_null(); }
if (c == '-' || (c >= '0' && c <= '9')) return iason_parse_number(p);
iason_die("IASON_LEGE: unexpected character");
return cent_null();
}
CentValue cent_iason_lege(CentValue s, int fractio_loaded) {
if (s.type != CENT_STR) cent_type_error("IASON_LEGE requires a string");
IasonParser p = { s.sval, 0, strlen(s.sval), fractio_loaded };
CentValue v = iason_parse_value(&p);
iason_skip_ws(&p);
if (p.pos != p.len) iason_die("IASON_LEGE: trailing data after JSON value");
return v;
}
/* ---------- serializer --------------------------------------------- */
typedef struct {
char *buf;
size_t len;
size_t cap;
} IasonBuf;
static void iason_buf_reserve(IasonBuf *b, size_t extra) {
if (b->len + extra <= b->cap) return;
size_t new_cap = b->cap ? b->cap * 2 : 64;
while (new_cap < b->len + extra) new_cap *= 2;
char *nb = cent_arena_alloc(cent_arena, new_cap);
if (b->len) memcpy(nb, b->buf, b->len);
b->buf = nb;
b->cap = new_cap;
}
static void iason_buf_putc(IasonBuf *b, char c) {
iason_buf_reserve(b, 1);
b->buf[b->len++] = c;
}
static void iason_buf_puts(IasonBuf *b, const char *s) {
size_t n = strlen(s);
iason_buf_reserve(b, n);
memcpy(b->buf + b->len, s, n);
b->len += n;
}
static void iason_buf_putn(IasonBuf *b, const char *s, size_t n) {
iason_buf_reserve(b, n);
memcpy(b->buf + b->len, s, n);
b->len += n;
}
static void iason_emit_string(IasonBuf *b, const char *s) {
iason_buf_putc(b, '"');
for (const unsigned char *p = (const unsigned char *)s; *p; p++) {
unsigned char c = *p;
switch (c) {
case '"': iason_buf_puts(b, "\\\""); break;
case '\\': iason_buf_puts(b, "\\\\"); break;
case '\b': iason_buf_puts(b, "\\b"); break;
case '\f': iason_buf_puts(b, "\\f"); break;
case '\n': iason_buf_puts(b, "\\n"); break;
case '\r': iason_buf_puts(b, "\\r"); break;
case '\t': iason_buf_puts(b, "\\t"); break;
default:
if (c < 0x20) {
char tmp[8];
snprintf(tmp, sizeof tmp, "\\u%04x", c);
iason_buf_puts(b, tmp);
} else {
iason_buf_putc(b, (char)c);
}
}
}
iason_buf_putc(b, '"');
}
static void iason_emit_value(IasonBuf *b, CentValue v) {
switch (v.type) {
case CENT_NULL: iason_buf_puts(b, "null"); return;
case CENT_BOOL: iason_buf_puts(b, v.bval ? "true" : "false"); return;
case CENT_INT: {
char tmp[32];
int n = snprintf(tmp, sizeof tmp, "%ld", v.ival);
iason_buf_putn(b, tmp, (size_t)n);
return;
}
case CENT_FRAC: {
double d = (double)v.fval.num / (double)v.fval.den;
/* Shortest round-trippable representation, like Python's float repr. */
char tmp[64];
int n = 0;
for (int prec = 15; prec <= 17; prec++) {
n = snprintf(tmp, sizeof tmp, "%.*g", prec, d);
if (strtod(tmp, NULL) == d) break;
}
iason_buf_putn(b, tmp, (size_t)n);
return;
}
case CENT_STR: iason_emit_string(b, v.sval); return;
case CENT_LIST: {
iason_buf_putc(b, '[');
for (int i = 0; i < v.lval.len; i++) {
if (i > 0) iason_buf_puts(b, ", ");
iason_emit_value(b, v.lval.items[i]);
}
iason_buf_putc(b, ']');
return;
}
case CENT_DICT: {
iason_buf_putc(b, '{');
for (int i = 0; i < v.dval.len; i++) {
if (i > 0) iason_buf_puts(b, ", ");
CentValue k = v.dval.keys[i];
if (k.type != CENT_STR)
cent_runtime_error("IASON_SCRIBE: dict keys must be strings to serialize as JSON");
iason_emit_string(b, k.sval);
iason_buf_puts(b, ": ");
iason_emit_value(b, v.dval.vals[i]);
}
iason_buf_putc(b, '}');
return;
}
case CENT_FUNC:
cent_runtime_error("IASON_SCRIBE: cannot serialize a function");
return;
}
}
CentValue cent_iason_scribe(CentValue v) {
IasonBuf b = { NULL, 0, 0 };
iason_emit_value(&b, v);
iason_buf_putc(&b, '\0');
return cent_str(b.buf);
}

View File

@@ -0,0 +1,14 @@
#ifndef CENT_IASON_H
#define CENT_IASON_H
#include "cent_runtime.h"
/* IASON_LEGE — parse a JSON string into a CENTVRION value tree.
When fractio_loaded != 0, JSON floats become exact fractions; otherwise
they are floored to ints. */
CentValue cent_iason_lege(CentValue s, int fractio_loaded);
/* IASON_SCRIBE — serialize a CENTVRION value to a JSON string. */
CentValue cent_iason_scribe(CentValue v);
#endif /* CENT_IASON_H */

View File

@@ -35,6 +35,7 @@ static uint32_t cent_rng_next(void) {
jmp_buf _cent_try_stack[CENT_TRY_STACK_MAX];
int _cent_try_depth = 0;
const char *_cent_error_msg = NULL;
int _cent_current_line = 0;
/* ------------------------------------------------------------------ */
/* Arena allocator */
@@ -74,13 +75,20 @@ void *cent_arena_alloc(CentArena *a, size_t n) {
/* Error handling */
/* ------------------------------------------------------------------ */
static void _cent_die(const char *kind, const char *msg) {
if (_cent_current_line > 0)
fprintf(stderr, "CENTVRION %s: %s at line %d\n", kind, msg, _cent_current_line);
else
fprintf(stderr, "CENTVRION %s: %s\n", kind, msg);
exit(1);
}
void cent_type_error(const char *msg) {
if (_cent_try_depth > 0) {
_cent_error_msg = msg;
longjmp(_cent_try_stack[_cent_try_depth - 1], 1);
}
fprintf(stderr, "CENTVRION type error: %s\n", msg);
exit(1);
_cent_die("type error", msg);
}
void cent_runtime_error(const char *msg) {
@@ -88,8 +96,7 @@ void cent_runtime_error(const char *msg) {
_cent_error_msg = msg;
longjmp(_cent_try_stack[_cent_try_depth - 1], 1);
}
fprintf(stderr, "CENTVRION error: %s\n", msg);
exit(1);
_cent_die("error", msg);
}
/* ------------------------------------------------------------------ */
@@ -101,8 +108,11 @@ CentValue cent_scope_get(CentScope *s, const char *name) {
if (strcmp(s->names[i], name) == 0)
return s->vals[i];
}
fprintf(stderr, "CENTVRION error: undefined variable '%s'\n", name);
exit(1);
size_t bufsz = strlen(name) + 32;
char *buf = cent_arena_alloc(cent_arena, bufsz);
snprintf(buf, bufsz, "undefined variable '%s'", name);
cent_runtime_error(buf);
return cent_null(); /* unreachable */
}
void cent_scope_set(CentScope *s, const char *name, CentValue v) {
@@ -236,8 +246,10 @@ long cent_roman_to_int(const char *s) {
}
}
if (!matched) {
fprintf(stderr, "CENTVRION error: invalid Roman numeral: %s\n", s);
exit(1);
size_t bufsz = strlen(s) + 32;
char *buf = cent_arena_alloc(cent_arena, bufsz);
snprintf(buf, bufsz, "invalid Roman numeral: %s", s);
cent_runtime_error(buf);
}
}
return result;
@@ -848,6 +860,193 @@ CentValue cent_ordina(CentValue lst) {
return result;
}
/* User-comparator sort: single-threaded runtime, so the active comparator
and its calling scope are stashed in file-scope statics. Save/restore
them around the qsort call so nested ORDINA(..., cmp) calls inside a
comparator still work. */
static CentValue _cmp_active;
static CentScope _cmp_scope;
static int _ordina_user_comparator(const void *a, const void *b) {
const CentValue *va = (const CentValue *)a;
const CentValue *vb = (const CentValue *)b;
CentScope s1 = cent_scope_copy(&_cmp_scope);
cent_scope_set(&s1, _cmp_active.fnval.param_names[0], *va);
cent_scope_set(&s1, _cmp_active.fnval.param_names[1], *vb);
CentValue r1 = _cmp_active.fnval.fn(s1);
if (r1.type != CENT_BOOL)
cent_type_error("'ORDINA' comparator must return VERAX");
if (r1.bval) return -1;
CentScope s2 = cent_scope_copy(&_cmp_scope);
cent_scope_set(&s2, _cmp_active.fnval.param_names[0], *vb);
cent_scope_set(&s2, _cmp_active.fnval.param_names[1], *va);
CentValue r2 = _cmp_active.fnval.fn(s2);
if (r2.type != CENT_BOOL)
cent_type_error("'ORDINA' comparator must return VERAX");
return r2.bval ? 1 : 0;
}
CentValue cent_ordina_cmp(CentValue lst, CentValue cmp, CentScope scope) {
if (lst.type != CENT_LIST)
cent_type_error("'ORDINA' requires a list");
if (cmp.type != CENT_FUNC)
cent_type_error("'ORDINA' comparator must be a function");
if (cmp.fnval.param_count != 2)
cent_runtime_error("'ORDINA' comparator must take 2 arguments");
int len = lst.lval.len;
CentValue result = cent_list_new(len);
for (int i = 0; i < len; i++)
cent_list_push(&result, lst.lval.items[i]);
if (len > 1) {
CentValue saved_cmp = _cmp_active;
CentScope saved_scope = _cmp_scope;
_cmp_active = cmp;
_cmp_scope = scope;
qsort(result.lval.items, len, sizeof(CentValue), _ordina_user_comparator);
_cmp_active = saved_cmp;
_cmp_scope = saved_scope;
}
return result;
}
CentValue cent_mvta(CentValue lst, CentValue fn, CentScope scope) {
if (lst.type != CENT_LIST)
cent_type_error("'MVTA' requires an array");
if (fn.type != CENT_FUNC)
cent_type_error("'MVTA' requires a function");
if (fn.fnval.param_count != 1)
cent_runtime_error("'MVTA' function must take I argument");
int len = lst.lval.len;
CentValue result = cent_list_new(len);
for (int i = 0; i < len; i++) {
CentScope s = cent_scope_copy(&scope);
cent_scope_set(&s, fn.fnval.param_names[0], lst.lval.items[i]);
cent_list_push(&result, fn.fnval.fn(s));
}
return result;
}
CentValue cent_cribra(CentValue lst, CentValue fn, CentScope scope) {
if (lst.type != CENT_LIST)
cent_type_error("'CRIBRA' requires an array");
if (fn.type != CENT_FUNC)
cent_type_error("'CRIBRA' requires a function");
if (fn.fnval.param_count != 1)
cent_runtime_error("'CRIBRA' predicate must take I argument");
int len = lst.lval.len;
CentValue result = cent_list_new(len);
for (int i = 0; i < len; i++) {
CentScope s = cent_scope_copy(&scope);
cent_scope_set(&s, fn.fnval.param_names[0], lst.lval.items[i]);
CentValue r = fn.fnval.fn(s);
if (r.type != CENT_BOOL)
cent_type_error("'CRIBRA' predicate must return VERAX");
if (r.bval)
cent_list_push(&result, lst.lval.items[i]);
}
return result;
}
CentValue cent_confla(CentValue lst, CentValue init, CentValue fn, CentScope scope) {
if (lst.type != CENT_LIST)
cent_type_error("'CONFLA' requires an array");
if (fn.type != CENT_FUNC)
cent_type_error("'CONFLA' requires a function");
if (fn.fnval.param_count != 2)
cent_runtime_error("'CONFLA' function must take II arguments");
CentValue acc = init;
int len = lst.lval.len;
for (int i = 0; i < len; i++) {
CentScope s = cent_scope_copy(&scope);
cent_scope_set(&s, fn.fnval.param_names[0], acc);
cent_scope_set(&s, fn.fnval.param_names[1], lst.lval.items[i]);
acc = fn.fnval.fn(s);
}
return acc;
}
static long _index_arg(CentValue idx, const char *name) {
if (idx.type == CENT_INT)
return idx.ival;
if (idx.type == CENT_FRAC && idx.fval.den == 1)
return idx.fval.num;
cent_type_error(name);
return 0;
}
CentValue cent_adde(CentValue lst, CentValue v) {
if (lst.type != CENT_LIST)
cent_type_error("'ADDE' requires a list");
int len = lst.lval.len;
CentValue result = cent_list_new(len + 1);
for (int i = 0; i < len; i++)
cent_list_push(&result, lst.lval.items[i]);
cent_list_push(&result, v);
return result;
}
CentValue cent_tolle(CentValue lst, CentValue idx) {
if (lst.type != CENT_LIST)
cent_type_error("'TOLLE' requires a list");
long i = _index_arg(idx, "'TOLLE' index must be an integer");
int len = lst.lval.len;
if (i < 1 || i > len)
cent_runtime_error("'TOLLE' index out of range");
CentValue result = cent_list_new(len - 1);
for (int j = 0; j < len; j++)
if (j != i - 1)
cent_list_push(&result, lst.lval.items[j]);
return result;
}
CentValue cent_insere(CentValue lst, CentValue idx, CentValue v) {
if (lst.type != CENT_LIST)
cent_type_error("'INSERE' requires a list");
long i = _index_arg(idx, "'INSERE' index must be an integer");
int len = lst.lval.len;
if (i < 1 || i > len + 1)
cent_runtime_error("'INSERE' index out of range");
CentValue result = cent_list_new(len + 1);
for (int j = 0; j < i - 1; j++)
cent_list_push(&result, lst.lval.items[j]);
cent_list_push(&result, v);
for (int j = i - 1; j < len; j++)
cent_list_push(&result, lst.lval.items[j]);
return result;
}
CentValue cent_necte(CentValue a, CentValue b) {
if (a.type != CENT_LIST || b.type != CENT_LIST)
cent_type_error("'NECTE' requires two arrays");
if (a.lval.len != b.lval.len)
cent_runtime_error("'NECTE' requires arrays of equal length");
int len = a.lval.len;
CentValue result = cent_list_new(len);
for (int i = 0; i < len; i++) {
CentValue pair = cent_list_new(2);
cent_list_push(&pair, a.lval.items[i]);
cent_list_push(&pair, b.lval.items[i]);
cent_list_push(&result, pair);
}
return result;
}
CentValue cent_ivnge(CentValue keys, CentValue vals) {
if (keys.type != CENT_LIST || vals.type != CENT_LIST)
cent_type_error("'IVNGE' requires two arrays");
if (keys.lval.len != vals.lval.len)
cent_runtime_error("'IVNGE' requires arrays of equal length");
int len = keys.lval.len;
CentValue result = cent_dict_new(len);
for (int i = 0; i < len; i++) {
CentValue k = keys.lval.items[i];
if (k.type != CENT_INT && k.type != CENT_STR)
cent_runtime_error("Dict keys must be strings or integers");
cent_dict_set(&result, k, vals.lval.items[i]);
}
return result;
}
/* ------------------------------------------------------------------ */
/* Array helpers */
/* ------------------------------------------------------------------ */
@@ -973,44 +1172,123 @@ static int _cent_key_eq(CentValue a, CentValue b) {
return 0;
}
/* splitmix64 finalizer — good distribution for sequential ints */
static uint32_t _cent_hash_int(long v) {
uint64_t x = (uint64_t)v;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
x = x ^ (x >> 31);
return (uint32_t)x;
}
/* FNV-1a */
static uint32_t _cent_hash_str(const char *s) {
uint32_t h = 2166136261u;
for (; *s; s++) {
h ^= (uint8_t)*s;
h *= 16777619u;
}
return h;
}
static uint32_t _cent_hash_key(CentValue k) {
if (k.type == CENT_INT) return _cent_hash_int(k.ival);
if (k.type == CENT_STR) return _cent_hash_str(k.sval);
cent_type_error("dict key must be a numeral or string");
return 0;
}
static int _next_pow2(int n) {
int p = 1;
while (p < n) p <<= 1;
return p;
}
/* Probe for `key` in the bucket array. Returns the bucket slot — either
one whose stored index points to a matching key (hit), or an empty
slot (-1) where the key would be inserted. nbuckets is a power of 2. */
static int _cent_dict_probe(const CentDict *d, CentValue key, uint32_t h) {
uint32_t mask = (uint32_t)d->nbuckets - 1;
uint32_t i = h & mask;
while (1) {
int idx = d->buckets[i];
if (idx < 0) return (int)i;
if (_cent_key_eq(d->keys[idx], key)) return (int)i;
i = (i + 1) & mask;
}
}
static void _cent_dict_rehash(CentDict *d, int new_nbuckets) {
int *new_buckets = cent_arena_alloc(cent_arena, new_nbuckets * sizeof(int));
for (int i = 0; i < new_nbuckets; i++) new_buckets[i] = -1;
uint32_t mask = (uint32_t)new_nbuckets - 1;
for (int idx = 0; idx < d->len; idx++) {
uint32_t h = _cent_hash_key(d->keys[idx]);
uint32_t i = h & mask;
while (new_buckets[i] >= 0) i = (i + 1) & mask;
new_buckets[i] = idx;
}
d->buckets = new_buckets;
d->nbuckets = new_nbuckets;
}
CentValue cent_dict_new(int cap) {
if (cap < 4) cap = 4;
int nbuckets = _next_pow2(cap * 2);
CentValue *keys = cent_arena_alloc(cent_arena, cap * sizeof(CentValue));
CentValue *vals = cent_arena_alloc(cent_arena, cap * sizeof(CentValue));
return cent_dict_val(keys, vals, 0, cap);
int *buckets = cent_arena_alloc(cent_arena, nbuckets * sizeof(int));
for (int i = 0; i < nbuckets; i++) buckets[i] = -1;
return cent_dict_val(keys, vals, buckets, 0, cap, nbuckets);
}
void cent_dict_set(CentValue *dict, CentValue key, CentValue val) {
if (dict->type != CENT_DICT)
cent_type_error("dict-set requires a dict");
for (int i = 0; i < dict->dval.len; i++) {
if (_cent_key_eq(dict->dval.keys[i], key)) {
dict->dval.vals[i] = val;
return;
}
CentDict *d = &dict->dval;
uint32_t h = _cent_hash_key(key);
int slot = _cent_dict_probe(d, key, h);
int idx = d->buckets[slot];
if (idx >= 0) {
d->vals[idx] = val;
return;
}
if (dict->dval.len >= dict->dval.cap) {
int new_cap = dict->dval.cap * 2;
/* Grow the keys/vals arrays first so the new entry has a stable index. */
if (d->len >= d->cap) {
int new_cap = d->cap * 2;
CentValue *new_keys = cent_arena_alloc(cent_arena, new_cap * sizeof(CentValue));
CentValue *new_vals = cent_arena_alloc(cent_arena, new_cap * sizeof(CentValue));
memcpy(new_keys, dict->dval.keys, dict->dval.len * sizeof(CentValue));
memcpy(new_vals, dict->dval.vals, dict->dval.len * sizeof(CentValue));
dict->dval.keys = new_keys;
dict->dval.vals = new_vals;
dict->dval.cap = new_cap;
memcpy(new_keys, d->keys, d->len * sizeof(CentValue));
memcpy(new_vals, d->vals, d->len * sizeof(CentValue));
d->keys = new_keys;
d->vals = new_vals;
d->cap = new_cap;
}
int new_idx = d->len;
d->keys[new_idx] = key;
d->vals[new_idx] = val;
d->len++;
/* If load factor would exceed 0.75, rehash — this re-inserts every
entry including the one we just appended, so we're done. Otherwise
the slot picked by the earlier probe is still valid. */
if (d->len * 4 >= d->nbuckets * 3) {
_cent_dict_rehash(d, d->nbuckets * 2);
} else {
d->buckets[slot] = new_idx;
}
dict->dval.keys[dict->dval.len] = key;
dict->dval.vals[dict->dval.len] = val;
dict->dval.len++;
}
CentValue cent_dict_get(CentValue dict, CentValue key) {
if (dict.type != CENT_DICT)
cent_type_error("dict-get requires a dict");
for (int i = 0; i < dict.dval.len; i++) {
if (_cent_key_eq(dict.dval.keys[i], key))
return dict.dval.vals[i];
}
uint32_t h = _cent_hash_key(key);
int slot = _cent_dict_probe(&dict.dval, key, h);
int idx = dict.dval.buckets[slot];
if (idx >= 0) return dict.dval.vals[idx];
cent_runtime_error("Key not found in dict");
return cent_null();
}

View File

@@ -47,10 +47,13 @@ struct CentList {
};
struct CentDict {
CentValue *keys;
CentValue *vals;
int len;
int cap;
CentValue *keys; /* insertion-order array, len entries */
CentValue *vals; /* parallel to keys */
int *buckets; /* hash table; values are indices into */
/* keys/vals, or -1 for empty */
int len; /* number of entries */
int cap; /* capacity of keys/vals */
int nbuckets; /* size of buckets, power of 2 */
};
struct CentValue {
@@ -135,13 +138,17 @@ static inline CentValue cent_func_val(CentFuncPtr fn, const char **param_names,
r.fnval.param_count = param_count;
return r;
}
static inline CentValue cent_dict_val(CentValue *keys, CentValue *vals, int len, int cap) {
static inline CentValue cent_dict_val(CentValue *keys, CentValue *vals,
int *buckets, int len, int cap,
int nbuckets) {
CentValue r;
r.type = CENT_DICT;
r.dval.keys = keys;
r.dval.vals = vals;
r.dval.len = len;
r.dval.cap = cap;
r.dval.keys = keys;
r.dval.vals = vals;
r.dval.buckets = buckets;
r.dval.len = len;
r.dval.cap = cap;
r.dval.nbuckets = nbuckets;
return r;
}
@@ -154,6 +161,9 @@ extern jmp_buf _cent_try_stack[];
extern int _cent_try_depth;
extern const char *_cent_error_msg;
/* Updated at the start of every emitted statement; 0 means "no line known". */
extern int _cent_current_line;
void cent_type_error(const char *msg); /* type mismatch → longjmp or exit(1) */
void cent_runtime_error(const char *msg); /* runtime fault → longjmp or exit(1) */
@@ -239,6 +249,15 @@ CentValue cent_senatus(CentValue *args, int n); /* SENATVS */
CentValue cent_typvs(CentValue v); /* TYPVS */
void cent_dormi(CentValue n); /* DORMI */
CentValue cent_ordina(CentValue lst); /* ORDINA */
CentValue cent_ordina_cmp(CentValue lst, CentValue cmp, CentScope scope); /* ORDINA w/ comparator */
CentValue cent_mvta(CentValue lst, CentValue fn, CentScope scope); /* MVTA */
CentValue cent_cribra(CentValue lst, CentValue fn, CentScope scope); /* CRIBRA */
CentValue cent_confla(CentValue lst, CentValue init, CentValue fn, CentScope scope); /* CONFLA */
CentValue cent_adde(CentValue lst, CentValue v); /* ADDE */
CentValue cent_tolle(CentValue lst, CentValue idx); /* TOLLE */
CentValue cent_insere(CentValue lst, CentValue idx, CentValue v); /* INSERE */
CentValue cent_necte(CentValue a, CentValue b); /* NECTE */
CentValue cent_ivnge(CentValue keys, CentValue vals); /* IVNGE */
CentValue cent_lege(CentValue path); /* LEGE */
void cent_scribe(CentValue path, CentValue content); /* SCRIBE */
void cent_adivnge(CentValue path, CentValue content); /* ADIVNGE */

View File

@@ -1 +1,13 @@
class CentvrionError(Exception): pass
class CentvrionError(Exception):
def __init__(self, msg, lineno=None, colno=None):
self.msg = msg
self.lineno = lineno
self.colno = colno
super().__init__(msg)
def __str__(self):
if self.lineno is None:
return self.msg
if self.colno is None:
return f"{self.msg} at line {self.lineno}"
return f"{self.msg} at line {self.lineno}, column {self.colno}"

View File

@@ -46,23 +46,31 @@ keyword_tokens = [("KEYWORD_"+i, i) for i in [
]]
builtin_tokens = [("BUILTIN", i) for i in [
"ADDE",
"AVDI_NVMERVS",
"AVDI",
"CLAVES",
"CONFLA",
"CRIBRA",
"DECIMATIO",
"DIC",
"DORMI",
"EVERRE",
"FORTVITVS_NVMERVS",
"FORTVITA_ELECTIO",
"INSERE",
"IVNGE",
"LITTERA",
"LONGITVDO",
"MAIVSCVLA",
"MINVSCVLA",
"MVTA",
"NECTE",
"NVMERVS",
"ORDINA",
"SEMEN",
"SENATVS",
"TOLLE",
"TYPVS",
"LEGE",
"SCRIBE",
@@ -72,7 +80,9 @@ builtin_tokens = [("BUILTIN", i) for i in [
"SCINDE",
"PETE",
"PETITVR",
"AVSCVLTA"
"AVSCVLTA",
"IASON_LEGE",
"IASON_SCRIBE"
]]
data_tokens = [
@@ -84,6 +94,7 @@ data_tokens = [
module_tokens = [("MODULE", i) for i in [
"FORS",
"FRACTIO",
"IASON",
"MAGNVM",
"SCRIPTA",
"SVBNVLLA",

View File

@@ -42,7 +42,30 @@ def _unescape(s):
return ''.join(out)
def _parse_interpolated(raw_value):
def _at(node, src):
"""Stamp a (lineno, colno) onto a freshly built AST node.
`src` can be an rply Token (uses .source_pos) or another Node (copies .pos).
"""
if src is None:
return node
pos = getattr(src, "pos", None)
if pos is not None:
node.pos = pos
return node
sp = getattr(src, "source_pos", None)
if sp is not None:
node.pos = (sp.lineno, sp.colno)
return node
def _parse_interpolated(raw_value, source_pos=None):
lineno = source_pos.lineno if source_pos is not None else None
colno = source_pos.colno if source_pos is not None else None
def _err(msg):
return CentvrionError(msg, lineno, colno)
quote_char = raw_value[0]
inner = raw_value[1:-1]
@@ -79,15 +102,15 @@ def _parse_interpolated(raw_value):
depth -= 1
j += 1
if depth != 0:
raise CentvrionError("Unclosed '{' in interpolated string")
raise _err("Unclosed '{' in interpolated string")
expr_src = inner[i + 1:j - 1]
tokens = Lexer().get_lexer().lex(expr_src + "\n")
program = Parser().parse(tokens)
if len(program.statements) != 1:
raise CentvrionError("Interpolation must contain exactly one expression")
raise _err("Interpolation must contain exactly one expression")
stmt = program.statements[0]
if not isinstance(stmt, ast_nodes.ExpressionStatement):
raise CentvrionError("Interpolation must contain an expression, not a statement")
raise _err("Interpolation must contain an expression, not a statement")
parts.append(stmt.expression)
i = j
elif ch == '}':
@@ -95,7 +118,7 @@ def _parse_interpolated(raw_value):
current.append('}')
i += 2
continue
raise CentvrionError("Unmatched '}' in string (use '}}' for literal '}')")
raise _err("Unmatched '}' in string (use '}}' for literal '}')")
else:
current.append(ch)
i += 1
@@ -154,7 +177,7 @@ class Parser():
@self.pg.production('module_call : KEYWORD_CVM MODULE')
def module_call(tokens):
return ast_nodes.ModuleCall(tokens[1].value)
return _at(ast_nodes.ModuleCall(tokens[1].value), tokens[0])
# Statements
@@ -172,7 +195,7 @@ class Parser():
@self.pg.production('statement : KEYWORD_DESIGNA id KEYWORD_VT expression')
def statement_designa(tokens):
return ast_nodes.Designa(tokens[1], tokens[3])
return _at(ast_nodes.Designa(tokens[1], tokens[3]), tokens[0])
@self.pg.production('index_chain : SYMBOL_LBRACKET expression SYMBOL_RBRACKET')
def index_chain_single(tokens):
@@ -184,39 +207,39 @@ class Parser():
@self.pg.production('statement : KEYWORD_DESIGNA id index_chain KEYWORD_VT expression')
def statement_designa_index(tokens):
return ast_nodes.DesignaIndex(tokens[1], tokens[2], tokens[4])
return _at(ast_nodes.DesignaIndex(tokens[1], tokens[2], tokens[4]), tokens[0])
@self.pg.production('statement : KEYWORD_DESIGNA id SYMBOL_COMMA id_list_rest KEYWORD_VT expression')
def statement_designa_destructure(tokens):
return ast_nodes.DesignaDestructure([tokens[1]] + tokens[3], tokens[5])
return _at(ast_nodes.DesignaDestructure([tokens[1]] + tokens[3], tokens[5]), tokens[0])
@self.pg.production('statement : id KEYWORD_AVGE expression')
def statement_avge(tokens):
return ast_nodes.Designa(tokens[0], ast_nodes.BinOp(tokens[0], tokens[2], "SYMBOL_PLUS"))
return _at(ast_nodes.Designa(tokens[0], ast_nodes.BinOp(tokens[0], tokens[2], "SYMBOL_PLUS")), tokens[0])
@self.pg.production('statement : id KEYWORD_MINVE expression')
def statement_minve(tokens):
return ast_nodes.Designa(tokens[0], ast_nodes.BinOp(tokens[0], tokens[2], "SYMBOL_MINUS"))
return _at(ast_nodes.Designa(tokens[0], ast_nodes.BinOp(tokens[0], tokens[2], "SYMBOL_MINUS")), tokens[0])
@self.pg.production('statement : id KEYWORD_MVLTIPLICA expression')
def statement_mvltiplica(tokens):
return ast_nodes.Designa(tokens[0], ast_nodes.BinOp(tokens[0], tokens[2], "SYMBOL_TIMES"))
return _at(ast_nodes.Designa(tokens[0], ast_nodes.BinOp(tokens[0], tokens[2], "SYMBOL_TIMES")), tokens[0])
@self.pg.production('statement : id KEYWORD_DIVIDE expression')
def statement_divide(tokens):
return ast_nodes.Designa(tokens[0], ast_nodes.BinOp(tokens[0], tokens[2], "SYMBOL_DIVIDE"))
return _at(ast_nodes.Designa(tokens[0], ast_nodes.BinOp(tokens[0], tokens[2], "SYMBOL_DIVIDE")), tokens[0])
@self.pg.production('statement : expression')
def statement_expression(tokens):
return ast_nodes.ExpressionStatement(tokens[0])
return _at(ast_nodes.ExpressionStatement(tokens[0]), tokens[0])
@self.pg.production('statement : KEYWORD_DEFINI id ids KEYWORD_VT SYMBOL_LCURL statements SYMBOL_RCURL')
def defini(tokens):
return ast_nodes.Defini(tokens[1], tokens[2], tokens[5])
return _at(ast_nodes.Defini(tokens[1], tokens[2], tokens[5]), tokens[0])
@self.pg.production('statement : KEYWORD_REDI expressions')
def redi(tokens):
return ast_nodes.Redi(tokens[1])
return _at(ast_nodes.Redi(tokens[1]), tokens[0])
@self.pg.production('statement : per_statement')
@self.pg.production('statement : dum_statement')
@@ -227,20 +250,20 @@ class Parser():
return tokens[0]
@self.pg.production('statement : KEYWORD_ERVMPE')
def erumpe(_):
return ast_nodes.Erumpe()
def erumpe(tokens):
return _at(ast_nodes.Erumpe(), tokens[0])
@self.pg.production('statement : KEYWORD_CONTINVA')
def continva(_):
return ast_nodes.Continva()
def continva(tokens):
return _at(ast_nodes.Continva(), tokens[0])
@self.pg.production('si_statement : KEYWORD_SI expression KEYWORD_TVNC SYMBOL_LCURL statements SYMBOL_RCURL')
@self.pg.production('si_statement : KEYWORD_SI expression KEYWORD_TVNC SYMBOL_LCURL statements SYMBOL_RCURL aluid_statement')
def si_statement(tokens):
if len(tokens) == 7:
return ast_nodes.SiStatement(tokens[1], tokens[4], tokens[6])
return _at(ast_nodes.SiStatement(tokens[1], tokens[4], tokens[6]), tokens[0])
else:
return ast_nodes.SiStatement(tokens[1], tokens[4], None)
return _at(ast_nodes.SiStatement(tokens[1], tokens[4], None), tokens[0])
@self.pg.production('aluid_statement : KEYWORD_ALIVD si_statement')
def aluid_si(tokens):
@@ -252,34 +275,34 @@ class Parser():
@self.pg.production('dum_statement : KEYWORD_DVM expression KEYWORD_FAC SYMBOL_LCURL statements SYMBOL_RCURL')
def dum(tokens):
return ast_nodes.DumStatement(tokens[1], tokens[4])
return _at(ast_nodes.DumStatement(tokens[1], tokens[4]), tokens[0])
# AETERNVM is sugar for `DVM FALSITAS` — same AST, no observable difference.
@self.pg.production('dum_statement : KEYWORD_AETERNVM KEYWORD_FAC SYMBOL_LCURL statements SYMBOL_RCURL')
def aeternvm(tokens):
return ast_nodes.DumStatement(ast_nodes.Bool(False), tokens[3])
return _at(ast_nodes.DumStatement(ast_nodes.Bool(False), tokens[3]), tokens[0])
@self.pg.production('per_statement : KEYWORD_PER id SYMBOL_COMMA id_list_rest KEYWORD_IN expression KEYWORD_FAC SYMBOL_LCURL statements SYMBOL_RCURL')
def per_destructure(tokens):
return ast_nodes.PerStatement(tokens[5], [tokens[1]] + tokens[3], tokens[8])
return _at(ast_nodes.PerStatement(tokens[5], [tokens[1]] + tokens[3], tokens[8]), tokens[0])
@self.pg.production('per_statement : KEYWORD_PER id KEYWORD_IN expression KEYWORD_FAC SYMBOL_LCURL statements SYMBOL_RCURL')
def per(tokens):
return ast_nodes.PerStatement(tokens[3], tokens[1], tokens[6])
return _at(ast_nodes.PerStatement(tokens[3], tokens[1], tokens[6]), tokens[0])
@self.pg.production('tempta_statement : KEYWORD_TEMPTA SYMBOL_LCURL statements SYMBOL_RCURL KEYWORD_CAPE id SYMBOL_LCURL statements SYMBOL_RCURL')
def tempta(tokens):
return ast_nodes.TemptaStatement(tokens[2], tokens[5], tokens[7])
return _at(ast_nodes.TemptaStatement(tokens[2], tokens[5], tokens[7]), tokens[0])
@self.pg.production('donicum_statement : KEYWORD_DONICVM id KEYWORD_VT expression KEYWORD_VSQVE expression KEYWORD_FAC SYMBOL_LCURL statements SYMBOL_RCURL')
def donicum(tokens):
range_array = ast_nodes.DataRangeArray(tokens[3], tokens[5])
return ast_nodes.PerStatement(range_array, tokens[1], tokens[8])
range_array = _at(ast_nodes.DataRangeArray(tokens[3], tokens[5]), tokens[0])
return _at(ast_nodes.PerStatement(range_array, tokens[1], tokens[8]), tokens[0])
@self.pg.production('donicum_statement : KEYWORD_DONICVM id KEYWORD_VT expression KEYWORD_VSQVE expression KEYWORD_GRADV expression KEYWORD_FAC SYMBOL_LCURL statements SYMBOL_RCURL')
def donicum_step(tokens):
range_array = ast_nodes.DataRangeArray(tokens[3], tokens[5], tokens[7])
return ast_nodes.PerStatement(range_array, tokens[1], tokens[10])
range_array = _at(ast_nodes.DataRangeArray(tokens[3], tokens[5], tokens[7]), tokens[0])
return _at(ast_nodes.PerStatement(range_array, tokens[1], tokens[10]), tokens[0])
# expressions
@self.pg.production('expressions : SYMBOL_LPARENS expression_list')
@@ -311,28 +334,29 @@ class Parser():
@self.pg.production('expression : BUILTIN expressions')
def expression_builtin(tokens):
return ast_nodes.BuiltIn(tokens[0].value, tokens[1])
return _at(ast_nodes.BuiltIn(tokens[0].value, tokens[1]), tokens[0])
@self.pg.production('expression : DATA_STRING')
def expression_string(tokens):
return _parse_interpolated(tokens[0].value)
node = _parse_interpolated(tokens[0].value, tokens[0].source_pos)
return _at(node, tokens[0])
@self.pg.production('expression : DATA_NUMERAL')
def expression_numeral(tokens):
return ast_nodes.Numeral(tokens[0].value)
return _at(ast_nodes.Numeral(tokens[0].value), tokens[0])
@self.pg.production('expression : DATA_FRACTION')
def expression_fraction(tokens):
return ast_nodes.Fractio(tokens[0].value)
return _at(ast_nodes.Fractio(tokens[0].value), tokens[0])
@self.pg.production('expression : KEYWORD_FALSITAS')
@self.pg.production('expression : KEYWORD_VERITAS')
def expression_bool(tokens):
return ast_nodes.Bool(tokens[0].name == "KEYWORD_VERITAS")
return _at(ast_nodes.Bool(tokens[0].name == "KEYWORD_VERITAS"), tokens[0])
@self.pg.production('expression : KEYWORD_NVLLVS')
def expression_nullus(_):
return ast_nodes.Nullus()
def expression_nullus(tokens):
return _at(ast_nodes.Nullus(), tokens[0])
@self.pg.production('expression : expression SYMBOL_AT expression')
@self.pg.production('expression : expression SYMBOL_AMPERSAND expression')
@@ -350,23 +374,23 @@ class Parser():
@self.pg.production('expression : expression KEYWORD_ET expression')
@self.pg.production('expression : expression KEYWORD_AVT expression')
def binop(tokens):
return ast_nodes.BinOp(tokens[0], tokens[2], tokens[1].name)
return _at(ast_nodes.BinOp(tokens[0], tokens[2], tokens[1].name), tokens[0])
@self.pg.production('expression : SYMBOL_MINUS expression', precedence='UMINUS')
def unary_minus(tokens):
return ast_nodes.UnaryMinus(tokens[1])
return _at(ast_nodes.UnaryMinus(tokens[1]), tokens[0])
@self.pg.production('expression : KEYWORD_NON expression', precedence='UNOT')
def unary_not(tokens):
return ast_nodes.UnaryNot(tokens[1])
return _at(ast_nodes.UnaryNot(tokens[1]), tokens[0])
@self.pg.production('expression : KEYWORD_INVOCA expression expressions')
def invoca(tokens):
return ast_nodes.Invoca(tokens[1], tokens[2])
return _at(ast_nodes.Invoca(tokens[1], tokens[2]), tokens[0])
@self.pg.production('expression : KEYWORD_FVNCTIO ids KEYWORD_VT SYMBOL_LCURL statements SYMBOL_RCURL')
def fvnctio(tokens):
return ast_nodes.Fvnctio(tokens[1], tokens[4])
return _at(ast_nodes.Fvnctio(tokens[1], tokens[4]), tokens[0])
@self.pg.production('expression : SYMBOL_LPARENS expression SYMBOL_RPARENS')
def parens(tokens):
@@ -382,40 +406,40 @@ class Parser():
@self.pg.production('expression : KEYWORD_TABVLA SYMBOL_LCURL opt_newline SYMBOL_RCURL')
def dict_literal_empty(tokens):
return ast_nodes.DataDict([])
return _at(ast_nodes.DataDict([]), tokens[0])
@self.pg.production('expression : KEYWORD_TABVLA SYMBOL_LCURL opt_newline dict_items opt_newline SYMBOL_RCURL')
def dict_literal(tokens):
return ast_nodes.DataDict(tokens[3])
return _at(ast_nodes.DataDict(tokens[3]), tokens[0])
@self.pg.production('expression : SYMBOL_LBRACKET SYMBOL_RBRACKET')
@self.pg.production('expression : SYMBOL_LBRACKET newlines SYMBOL_RBRACKET')
def array_empty(_):
return ast_nodes.DataArray([])
def array_empty(tokens):
return _at(ast_nodes.DataArray([]), tokens[0])
@self.pg.production('expression : SYMBOL_LBRACKET array_items opt_newline SYMBOL_RBRACKET')
def array(tokens):
return ast_nodes.DataArray(tokens[1])
return _at(ast_nodes.DataArray(tokens[1]), tokens[0])
@self.pg.production('expression : SYMBOL_LBRACKET newlines array_items opt_newline SYMBOL_RBRACKET')
def array_leading_newline(tokens):
return ast_nodes.DataArray(tokens[2])
return _at(ast_nodes.DataArray(tokens[2]), tokens[0])
@self.pg.production('expression : SYMBOL_LBRACKET expression KEYWORD_VSQVE expression SYMBOL_RBRACKET')
def range_array(tokens):
return ast_nodes.DataRangeArray(tokens[1], tokens[3])
return _at(ast_nodes.DataRangeArray(tokens[1], tokens[3]), tokens[0])
@self.pg.production('expression : SYMBOL_LBRACKET expression KEYWORD_VSQVE expression KEYWORD_GRADV expression SYMBOL_RBRACKET')
def range_array_step(tokens):
return ast_nodes.DataRangeArray(tokens[1], tokens[3], tokens[5])
return _at(ast_nodes.DataRangeArray(tokens[1], tokens[3], tokens[5]), tokens[0])
@self.pg.production('expression : expression SYMBOL_LBRACKET expression SYMBOL_RBRACKET', precedence='INDEX')
def array_index(tokens):
return ast_nodes.ArrayIndex(tokens[0], tokens[2])
return _at(ast_nodes.ArrayIndex(tokens[0], tokens[2]), tokens[0])
@self.pg.production('expression : expression SYMBOL_LBRACKET expression KEYWORD_VSQVE expression SYMBOL_RBRACKET', precedence='INDEX')
def array_slice(tokens):
return ast_nodes.ArraySlice(tokens[0], tokens[2], tokens[4])
return _at(ast_nodes.ArraySlice(tokens[0], tokens[2], tokens[4]), tokens[0])
# ids
@self.pg.production('ids : SYMBOL_LPARENS id_list')
@@ -443,7 +467,7 @@ class Parser():
@self.pg.production("id : ID")
def id_expression(tokens):
return ast_nodes.ID(tokens[0].value)
return _at(ast_nodes.ID(tokens[0].value), tokens[0])
@self.pg.error
def error_handle(token):

View File

@@ -104,7 +104,7 @@
\newpage
\begin{itemize}
\item \textbf{newline}: \\ Newlines are combined, so a single newline is the same as multiple.
\item \textbf{module-name}: \\ Modules are flags given to the interpreter/compiler, to let it know you want to be using certain rules, functions, or features. Available modules: \texttt{FORS} (randomness), \texttt{FRACTIO} (fractions), \texttt{MAGNVM} (large integers), \texttt{SCRIPTA} (file I/O: \texttt{LEGE}, \texttt{SCRIBE}, \texttt{ADIVNGE}), \texttt{SVBNVLLA} (negative literals), \texttt{RETE} (networking: \texttt{PETE}, \texttt{PETITVR}, \texttt{AVSCVLTA}).
\item \textbf{module-name}: \\ Modules are flags given to the interpreter/compiler, to let it know you want to be using certain rules, functions, or features. Available modules: \texttt{FORS} (randomness), \texttt{FRACTIO} (fractions), \texttt{IASON} (JSON I/O: \texttt{IASON\_LEGE}, \texttt{IASON\_SCRIBE}), \texttt{MAGNVM} (large integers), \texttt{SCRIPTA} (file I/O: \texttt{LEGE}, \texttt{SCRIBE}, \texttt{ADIVNGE}), \texttt{SVBNVLLA} (negative literals), \texttt{RETE} (networking: \texttt{PETE}, \texttt{PETITVR}, \texttt{AVSCVLTA}).
\item \textbf{id}: \\ Variable. Can only consist of lowercase characters and underscores, but not the letters j, u, or w.
\item \textbf{builtin}: \\ Builtin functions are uppercase latin words.
\item \textbf{string}: \\ Any text encased in \texttt{"} or \texttt{'} characters. Single-quoted strings are always literal. Strings support 1-based indexing (\texttt{string[I]}) and inclusive slicing (\texttt{string[I VSQVE III]}), returning single-character strings and substrings respectively.

4
snippets/confla.cent Normal file
View File

@@ -0,0 +1,4 @@
DEFINI addi (a, b) VT {
REDI (a + b)
}
DIC (CONFLA([I, II, III, IV, V], I, addi))

BIN
snippets/confla.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

3
snippets/cribra.cent Normal file
View File

@@ -0,0 +1,3 @@
DIC (CRIBRA([I, II, III, IV, V, VI], FVNCTIO (x) VT {
REDI (x HAVD_PLVS III)
}))

BIN
snippets/cribra.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

1
snippets/iason.cent Normal file
View File

@@ -0,0 +1 @@
CVM IASON

BIN
snippets/iason.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

5
snippets/iason_lege.cent Normal file
View File

@@ -0,0 +1,5 @@
CVM IASON
DESIGNA data VT IASON_LEGE('{"nomen": "Marcus", "anni": 30, "armorum": ["gladius", "scutum"]}')
DIC(data["nomen"])
DIC(data["anni"])
DIC(data["armorum"])

BIN
snippets/iason_lege.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

View File

@@ -0,0 +1,3 @@
CVM IASON
DESIGNA persona VT TABVLA {"nomen" VT "Marcus", "anni" VT XXX}
DIC(IASON_SCRIBE(persona))

BIN
snippets/iason_scribe.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

4
snippets/mvta.cent Normal file
View File

@@ -0,0 +1,4 @@
DEFINI dbl (x) VT {
REDI (x + x)
}
DIC (MVTA([I, II, III, IV], dbl))

BIN
snippets/mvta.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

4
snippets/ordina_cmp.cent Normal file
View File

@@ -0,0 +1,4 @@
DEFINI gt (a, b) VT {
REDI (a PLVS b)
}
DIC (ORDINA([II, V, I, III], gt))

BIN
snippets/ordina_cmp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View File

@@ -70,11 +70,11 @@ contexts:
scope: constant.language.centvrion
builtins:
- match: '\b(ADIVNGE|AVDI_NVMERVS|AVDI|AVSCVLTA|CLAVES|DECIMATIO|DIC|DORMI|EVERRE|FORTVITVS_NVMERVS|FORTVITA_ELECTIO|LEGE|LITTERA|LONGITVDO|MAIVSCVLA|MINVSCVLA|NVMERVS|ORDINA|PETE|PETITVR|QVAERE|SCINDE|SCRIBE|SEMEN|SENATVS|SVBSTITVE|TYPVS)\b'
- match: '\b(ADDE|ADIVNGE|AVDI_NVMERVS|AVDI|AVSCVLTA|CLAVES|CONFLA|CRIBRA|DECIMATIO|DIC|DORMI|EVERRE|FORTVITVS_NVMERVS|FORTVITA_ELECTIO|IASON_LEGE|IASON_SCRIBE|INSERE|IVNGE|LEGE|LITTERA|LONGITVDO|MAIVSCVLA|MINVSCVLA|MVTA|NECTE|NVMERVS|ORDINA|PETE|PETITVR|QVAERE|SCINDE|SCRIBE|SEMEN|SENATVS|SVBSTITVE|TOLLE|TYPVS)\b'
scope: support.function.builtin.centvrion
modules:
- match: '\b(FORS|FRACTIO|MAGNVM|RETE|SCRIPTA|SVBNVLLA)\b'
- match: '\b(FORS|FRACTIO|IASON|MAGNVM|RETE|SCRIPTA|SVBNVLLA)\b'
scope: support.class.module.centvrion
keywords:

View File

@@ -76,6 +76,78 @@ builtin_tests = [
("CVM FRACTIO\nORDINA([III, S, II])", Program([ModuleCall("FRACTIO")], [ExpressionStatement(BuiltIn("ORDINA", [DataArray([Numeral("III"), Fractio("S"), Numeral("II")])]))]), ValList([ValFrac(Fraction(1, 2)), ValInt(2), ValInt(3)])),
# ORDINA: array passed via variable
("DESIGNA x VT [III, I, II]\nORDINA(x)", Program([], [Designa(ID("x"), DataArray([Numeral("III"), Numeral("I"), Numeral("II")])), ExpressionStatement(BuiltIn("ORDINA", [ID("x")]))]), ValList([ValInt(1), ValInt(2), ValInt(3)])),
# ORDINA: descending sort with named comparator
("DEFINI gt (a, b) VT { REDI (a PLVS b) }\nORDINA([II, V, I, III], gt)", Program([], [Defini(ID("gt"), [ID("a"), ID("b")], [Redi([BinOp(ID("a"), ID("b"), "KEYWORD_PLVS")])]), ExpressionStatement(BuiltIn("ORDINA", [DataArray([Numeral("II"), Numeral("V"), Numeral("I"), Numeral("III")]), ID("gt")]))]), ValList([ValInt(5), ValInt(3), ValInt(2), ValInt(1)])),
# ORDINA: ascending sort with inline FVNCTIO comparator
("ORDINA([V, I, III], FVNCTIO (a, b) VT { REDI (a MINVS b) })", Program([], [ExpressionStatement(BuiltIn("ORDINA", [DataArray([Numeral("V"), Numeral("I"), Numeral("III")]), Fvnctio([ID("a"), ID("b")], [Redi([BinOp(ID("a"), ID("b"), "KEYWORD_MINVS")])])]))]), ValList([ValInt(1), ValInt(3), ValInt(5)])),
# ORDINA: empty list with comparator
("ORDINA([], FVNCTIO (a, b) VT { REDI (VERITAS) })", Program([], [ExpressionStatement(BuiltIn("ORDINA", [DataArray([]), Fvnctio([ID("a"), ID("b")], [Redi([Bool(True)])])]))]), ValList([])),
# ORDINA: single element with comparator
("ORDINA([VII], FVNCTIO (a, b) VT { REDI (VERITAS) })", Program([], [ExpressionStatement(BuiltIn("ORDINA", [DataArray([Numeral("VII")]), Fvnctio([ID("a"), ID("b")], [Redi([Bool(True)])])]))]), ValList([ValInt(7)])),
# ORDINA: comparator sorting two-element subarrays by first element
("ORDINA([[II, I], [I, II]], FVNCTIO (a, b) VT { REDI (a[I] PLVS b[I]) })", Program([], [ExpressionStatement(BuiltIn("ORDINA", [DataArray([DataArray([Numeral("II"), Numeral("I")]), DataArray([Numeral("I"), Numeral("II")])]), Fvnctio([ID("a"), ID("b")], [Redi([BinOp(ArrayIndex(ID("a"), Numeral("I")), ArrayIndex(ID("b"), Numeral("I")), "KEYWORD_PLVS")])])]))]), ValList([ValList([ValInt(2), ValInt(1)]), ValList([ValInt(1), ValInt(2)])])),
# MVTA: empty array → empty array
("MVTA([], FVNCTIO (x) VT { REDI (x + x) })", Program([], [ExpressionStatement(BuiltIn("MVTA", [DataArray([]), Fvnctio([ID("x")], [Redi([BinOp(ID("x"), ID("x"), "SYMBOL_PLUS")])])]))]), ValList([])),
# MVTA: single element doubled
("MVTA([V], FVNCTIO (x) VT { REDI (x + x) })", Program([], [ExpressionStatement(BuiltIn("MVTA", [DataArray([Numeral("V")]), Fvnctio([ID("x")], [Redi([BinOp(ID("x"), ID("x"), "SYMBOL_PLUS")])])]))]), ValList([ValInt(10)])),
# MVTA: multiple elements with named function
("DEFINI dbl (x) VT { REDI (x + x) }\nMVTA([I, II, III], dbl)", Program([], [Defini(ID("dbl"), [ID("x")], [Redi([BinOp(ID("x"), ID("x"), "SYMBOL_PLUS")])]), ExpressionStatement(BuiltIn("MVTA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III")]), ID("dbl")]))]), ValList([ValInt(2), ValInt(4), ValInt(6)])),
# MVTA: inline FVNCTIO squaring
("MVTA([I, II, III], FVNCTIO (x) VT { REDI (x * x) })", Program([], [ExpressionStatement(BuiltIn("MVTA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III")]), Fvnctio([ID("x")], [Redi([BinOp(ID("x"), ID("x"), "SYMBOL_TIMES")])])]))]), ValList([ValInt(1), ValInt(4), ValInt(9)])),
# MVTA: print form to confirm output rendering
("DIC(MVTA([I, II, III], FVNCTIO (x) VT { REDI (x + x) }))", Program([], [ExpressionStatement(BuiltIn("DIC", [BuiltIn("MVTA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III")]), Fvnctio([ID("x")], [Redi([BinOp(ID("x"), ID("x"), "SYMBOL_PLUS")])])])]))]), ValStr("[II IV VI]"), "[II IV VI]\n"),
# MVTA: array passed via variable
("DESIGNA xs VT [I, II, III]\nDIC(MVTA(xs, FVNCTIO (x) VT { REDI (x + I) }))", Program([], [Designa(ID("xs"), DataArray([Numeral("I"), Numeral("II"), Numeral("III")])), ExpressionStatement(BuiltIn("DIC", [BuiltIn("MVTA", [ID("xs"), Fvnctio([ID("x")], [Redi([BinOp(ID("x"), Numeral("I"), "SYMBOL_PLUS")])])])]))]), ValStr("[II III IV]"), "[II III IV]\n"),
# MVTA: callback may return a different type than input
('DIC(MVTA([I, II, III], FVNCTIO (x) VT { REDI (LITTERA(x)) }))', Program([], [ExpressionStatement(BuiltIn("DIC", [BuiltIn("MVTA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III")]), Fvnctio([ID("x")], [Redi([BuiltIn("LITTERA", [ID("x")])])])])]))]), ValStr("[I II III]"), "[I II III]\n"),
# CRIBRA: empty array → empty array
("CRIBRA([], FVNCTIO (x) VT { REDI (VERITAS) })", Program([], [ExpressionStatement(BuiltIn("CRIBRA", [DataArray([]), Fvnctio([ID("x")], [Redi([Bool(True)])])]))]), ValList([])),
# CRIBRA: predicate always true keeps everything
("CRIBRA([I, II, III], FVNCTIO (x) VT { REDI (VERITAS) })", Program([], [ExpressionStatement(BuiltIn("CRIBRA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III")]), Fvnctio([ID("x")], [Redi([Bool(True)])])]))]), ValList([ValInt(1), ValInt(2), ValInt(3)])),
# CRIBRA: predicate always false drops everything
("CRIBRA([I, II, III], FVNCTIO (x) VT { REDI (FALSITAS) })", Program([], [ExpressionStatement(BuiltIn("CRIBRA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III")]), Fvnctio([ID("x")], [Redi([Bool(False)])])]))]), ValList([])),
# CRIBRA: keep elements ≤ III
("CRIBRA([I, II, III, IV, V], FVNCTIO (x) VT { REDI (x HAVD_PLVS III) })", Program([], [ExpressionStatement(BuiltIn("CRIBRA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III"), Numeral("IV"), Numeral("V")]), Fvnctio([ID("x")], [Redi([BinOp(ID("x"), Numeral("III"), "KEYWORD_HAVD_PLVS")])])]))]), ValList([ValInt(1), ValInt(2), ValInt(3)])),
# CRIBRA: with named predicate
("DEFINI big (x) VT { REDI (x PLVS III) }\nCRIBRA([I, II, III, IV, V], big)", Program([], [Defini(ID("big"), [ID("x")], [Redi([BinOp(ID("x"), Numeral("III"), "KEYWORD_PLVS")])]), ExpressionStatement(BuiltIn("CRIBRA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III"), Numeral("IV"), Numeral("V")]), ID("big")]))]), ValList([ValInt(4), ValInt(5)])),
# CRIBRA: print form
("DIC(CRIBRA([I, II, III, IV, V], FVNCTIO (x) VT { REDI (x PLVS II) }))", Program([], [ExpressionStatement(BuiltIn("DIC", [BuiltIn("CRIBRA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III"), Numeral("IV"), Numeral("V")]), Fvnctio([ID("x")], [Redi([BinOp(ID("x"), Numeral("II"), "KEYWORD_PLVS")])])])]))]), ValStr("[III IV V]"), "[III IV V]\n"),
# CONFLA: empty array returns initial unchanged
("CONFLA([], V, FVNCTIO (a, b) VT { REDI (a + b) })", Program([], [ExpressionStatement(BuiltIn("CONFLA", [DataArray([]), Numeral("V"), Fvnctio([ID("a"), ID("b")], [Redi([BinOp(ID("a"), ID("b"), "SYMBOL_PLUS")])])]))]), ValInt(5)),
# CONFLA: sum from initial I (so result = 1 + 1 + 2 + 3 = 7)
("CONFLA([I, II, III], I, FVNCTIO (a, b) VT { REDI (a + b) })", Program([], [ExpressionStatement(BuiltIn("CONFLA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III")]), Numeral("I"), Fvnctio([ID("a"), ID("b")], [Redi([BinOp(ID("a"), ID("b"), "SYMBOL_PLUS")])])]))]), ValInt(7)),
# CONFLA: product starting from I
("CONFLA([II, III, IV], I, FVNCTIO (a, b) VT { REDI (a * b) })", Program([], [ExpressionStatement(BuiltIn("CONFLA", [DataArray([Numeral("II"), Numeral("III"), Numeral("IV")]), Numeral("I"), Fvnctio([ID("a"), ID("b")], [Redi([BinOp(ID("a"), ID("b"), "SYMBOL_TIMES")])])]))]), ValInt(24)),
# CONFLA: with named function
("DEFINI addi (a, b) VT { REDI (a + b) }\nCONFLA([I, II, III, IV], V, addi)", Program([], [Defini(ID("addi"), [ID("a"), ID("b")], [Redi([BinOp(ID("a"), ID("b"), "SYMBOL_PLUS")])]), ExpressionStatement(BuiltIn("CONFLA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III"), Numeral("IV")]), Numeral("V"), ID("addi")]))]), ValInt(15)),
# CONFLA: string concatenation
('CONFLA(["b", "c"], "a", FVNCTIO (a, b) VT { REDI (a & b) })', Program([], [ExpressionStatement(BuiltIn("CONFLA", [DataArray([String("b"), String("c")]), String("a"), Fvnctio([ID("a"), ID("b")], [Redi([BinOp(ID("a"), ID("b"), "SYMBOL_AMPERSAND")])])]))]), ValStr("abc")),
# CONFLA: print sum
("DIC(CONFLA([I, II, III, IV], I, FVNCTIO (a, b) VT { REDI (a + b) }))", Program([], [ExpressionStatement(BuiltIn("DIC", [BuiltIn("CONFLA", [DataArray([Numeral("I"), Numeral("II"), Numeral("III"), Numeral("IV")]), Numeral("I"), Fvnctio([ID("a"), ID("b")], [Redi([BinOp(ID("a"), ID("b"), "SYMBOL_PLUS")])])])]))]), ValStr("XI"), "XI\n"),
# ADDE: append to non-empty
("ADDE([I, II], III)", Program([], [ExpressionStatement(BuiltIn("ADDE", [DataArray([Numeral("I"), Numeral("II")]), Numeral("III")]))]), ValList([ValInt(1), ValInt(2), ValInt(3)])),
# ADDE: append to empty
("ADDE([], V)", Program([], [ExpressionStatement(BuiltIn("ADDE", [DataArray([]), Numeral("V")]))]), ValList([ValInt(5)])),
# ADDE: heterogeneous types are allowed
('ADDE([I, II], "x")', Program([], [ExpressionStatement(BuiltIn("ADDE", [DataArray([Numeral("I"), Numeral("II")]), String("x")]))]), ValList([ValInt(1), ValInt(2), ValStr("x")])),
# ADDE: print form to confirm output rendering
("DIC(ADDE([I, II], III))", Program([], [ExpressionStatement(BuiltIn("DIC", [BuiltIn("ADDE", [DataArray([Numeral("I"), Numeral("II")]), Numeral("III")])]))]), ValStr("[I II III]"), "[I II III]\n"),
# TOLLE: remove middle element
("TOLLE([I, II, III], II)", Program([], [ExpressionStatement(BuiltIn("TOLLE", [DataArray([Numeral("I"), Numeral("II"), Numeral("III")]), Numeral("II")]))]), ValList([ValInt(1), ValInt(3)])),
# TOLLE: remove first element
("TOLLE([I, II, III], I)", Program([], [ExpressionStatement(BuiltIn("TOLLE", [DataArray([Numeral("I"), Numeral("II"), Numeral("III")]), Numeral("I")]))]), ValList([ValInt(2), ValInt(3)])),
# TOLLE: remove last element
("TOLLE([I, II, III], III)", Program([], [ExpressionStatement(BuiltIn("TOLLE", [DataArray([Numeral("I"), Numeral("II"), Numeral("III")]), Numeral("III")]))]), ValList([ValInt(1), ValInt(2)])),
# TOLLE: single-element array → empty
("TOLLE([V], I)", Program([], [ExpressionStatement(BuiltIn("TOLLE", [DataArray([Numeral("V")]), Numeral("I")]))]), ValList([])),
# INSERE: insert into middle
("INSERE([I, III], II, II)", Program([], [ExpressionStatement(BuiltIn("INSERE", [DataArray([Numeral("I"), Numeral("III")]), Numeral("II"), Numeral("II")]))]), ValList([ValInt(1), ValInt(2), ValInt(3)])),
# INSERE: insert at front
("INSERE([II, III], I, I)", Program([], [ExpressionStatement(BuiltIn("INSERE", [DataArray([Numeral("II"), Numeral("III")]), Numeral("I"), Numeral("I")]))]), ValList([ValInt(1), ValInt(2), ValInt(3)])),
# INSERE: insert at len+1 (== append)
("INSERE([I, II], III, III)", Program([], [ExpressionStatement(BuiltIn("INSERE", [DataArray([Numeral("I"), Numeral("II")]), Numeral("III"), Numeral("III")]))]), ValList([ValInt(1), ValInt(2), ValInt(3)])),
# INSERE: into empty array at idx 1
("INSERE([], I, V)", Program([], [ExpressionStatement(BuiltIn("INSERE", [DataArray([]), Numeral("I"), Numeral("V")]))]), ValList([ValInt(5)])),
# TYPVS: integer
("TYPVS(V)", Program([], [ExpressionStatement(BuiltIn("TYPVS", [Numeral("V")]))]), ValStr("NVMERVS")),
# TYPVS: string
@@ -218,6 +290,30 @@ builtin_tests = [
('MINVSCVLA("A,B!1")', Program([], [ExpressionStatement(BuiltIn("MINVSCVLA", [String("A,B!1")]))]), ValStr("a,b!1")),
# MINVSCVLA round-trips MAIVSCVLA on lowercase input
('MINVSCVLA(MAIVSCVLA("hi"))', Program([], [ExpressionStatement(BuiltIn("MINVSCVLA", [BuiltIn("MAIVSCVLA", [String("hi")])]))]), ValStr("hi")),
# NECTE: zip two integer arrays
("NECTE([I, II, III], [IV, V, VI])", Program([], [ExpressionStatement(BuiltIn("NECTE", [DataArray([Numeral("I"), Numeral("II"), Numeral("III")]), DataArray([Numeral("IV"), Numeral("V"), Numeral("VI")])]))]), ValList([ValList([ValInt(1), ValInt(4)]), ValList([ValInt(2), ValInt(5)]), ValList([ValInt(3), ValInt(6)])])),
# NECTE: empty + empty
("NECTE([], [])", Program([], [ExpressionStatement(BuiltIn("NECTE", [DataArray([]), DataArray([])]))]), ValList([])),
# NECTE: single element
("NECTE([I], [II])", Program([], [ExpressionStatement(BuiltIn("NECTE", [DataArray([Numeral("I")]), DataArray([Numeral("II")])]))]), ValList([ValList([ValInt(1), ValInt(2)])])),
# NECTE: mixed types (numerals paired with strings)
('NECTE([I, II], ["a", "b"])', Program([], [ExpressionStatement(BuiltIn("NECTE", [DataArray([Numeral("I"), Numeral("II")]), DataArray([String("a"), String("b")])]))]), ValList([ValList([ValInt(1), ValStr("a")]), ValList([ValInt(2), ValStr("b")])])),
# NECTE: print form
('DIC(NECTE([I, II], [III, IV]))', Program([], [ExpressionStatement(BuiltIn("DIC", [BuiltIn("NECTE", [DataArray([Numeral("I"), Numeral("II")]), DataArray([Numeral("III"), Numeral("IV")])])]))]), ValStr("[[I III] [II IV]]"), "[[I III] [II IV]]\n"),
# NECTE: via variables
("DESIGNA a VT [I, II]\nDESIGNA b VT [III, IV]\nNECTE(a, b)", Program([], [Designa(ID("a"), DataArray([Numeral("I"), Numeral("II")])), Designa(ID("b"), DataArray([Numeral("III"), Numeral("IV")])), ExpressionStatement(BuiltIn("NECTE", [ID("a"), ID("b")]))]), ValList([ValList([ValInt(1), ValInt(3)]), ValList([ValInt(2), ValInt(4)])])),
# IVNGE: string keys
('IVNGE(["a", "b"], [I, II])', Program([], [ExpressionStatement(BuiltIn("IVNGE", [DataArray([String("a"), String("b")]), DataArray([Numeral("I"), Numeral("II")])]))]), ValDict({"a": ValInt(1), "b": ValInt(2)})),
# IVNGE: integer keys
('IVNGE([I, II], ["one", "two"])', Program([], [ExpressionStatement(BuiltIn("IVNGE", [DataArray([Numeral("I"), Numeral("II")]), DataArray([String("one"), String("two")])]))]), ValDict({1: ValStr("one"), 2: ValStr("two")})),
# IVNGE: empty + empty
("IVNGE([], [])", Program([], [ExpressionStatement(BuiltIn("IVNGE", [DataArray([]), DataArray([])]))]), ValDict({})),
# IVNGE: duplicate keys → last wins
('IVNGE(["a", "a"], [I, II])', Program([], [ExpressionStatement(BuiltIn("IVNGE", [DataArray([String("a"), String("a")]), DataArray([Numeral("I"), Numeral("II")])]))]), ValDict({"a": ValInt(2)})),
# IVNGE: print form
('DIC(IVNGE(["a", "b"], [I, II]))', Program([], [ExpressionStatement(BuiltIn("DIC", [BuiltIn("IVNGE", [DataArray([String("a"), String("b")]), DataArray([Numeral("I"), Numeral("II")])])]))]), ValStr("{a VT I, b VT II}"), "{a VT I, b VT II}\n"),
# IVNGE composes with CLAVES (round-trip keys)
('CLAVES(IVNGE(["a", "b"], [I, II]))', Program([], [ExpressionStatement(BuiltIn("CLAVES", [BuiltIn("IVNGE", [DataArray([String("a"), String("b")]), DataArray([Numeral("I"), Numeral("II")])])]))]), ValList([ValStr("a"), ValStr("b")])),
]
class TestBuiltins(unittest.TestCase):

View File

@@ -189,3 +189,34 @@ class TestDictDisplay(unittest.TestCase):
@parameterized.expand(dict_display_tests)
def test_dict_display(self, source, nodes, value, output):
run_test(self, source, nodes, value, output)
class TestDictGrowth(unittest.TestCase):
def test_dict_growth_preserves_order_and_lookup(self):
# Inserts XX entries via PER; pushes the compiled dict through
# multiple rehashes (initial cap=4) and verifies that lookup, length,
# and insertion-order iteration all still hold afterwards.
source = (
"DESIGNA d VT TABVLA {}\n"
"PER i IN [I VSQVE XX] FAC {\n"
"DESIGNA d[i] VT i * II\n"
"}\n"
"DIC(d[X])\n"
"DIC(LONGITVDO(d))\n"
"DIC(CLAVES(d))"
)
nodes = Program([], [
Designa(ID("d"), DataDict([])),
PerStatement(
DataRangeArray(Numeral("I"), Numeral("XX")),
ID("i"),
[DesignaIndex(ID("d"), [ID("i")],
BinOp(ID("i"), Numeral("II"), "SYMBOL_TIMES"))],
),
ExpressionStatement(BuiltIn("DIC", [ArrayIndex(ID("d"), Numeral("X"))])),
ExpressionStatement(BuiltIn("DIC", [BuiltIn("LONGITVDO", [ID("d")])])),
ExpressionStatement(BuiltIn("DIC", [BuiltIn("CLAVES", [ID("d")])])),
])
keys_str = "[" + " ".join(int_to_num(i, False) for i in range(1, 21)) + "]"
output = f"XX\nXX\n{keys_str}\n"
run_test(self, source, nodes, ValStr(keys_str), output)

173
tests/12_test_iason___.py Normal file
View File

@@ -0,0 +1,173 @@
from tests._helpers import (
unittest, parameterized, Fraction,
run_test,
Bool, BuiltIn, DataArray, DataDict, Designa, ExpressionStatement, ID,
ModuleCall, Nullus, Numeral, Program, String,
ValInt, ValStr, ValBool, ValList, ValDict, ValNul, ValFrac,
)
def _scribe(arg, modules=("IASON",)):
return Program(
[ModuleCall(m) for m in modules],
[ExpressionStatement(BuiltIn("IASON_SCRIBE", [arg]))],
)
def _lege(arg, modules=("IASON",)):
return Program(
[ModuleCall(m) for m in modules],
[ExpressionStatement(BuiltIn("IASON_LEGE", [String(arg)]))],
)
def _src_lege(arg, extra_modules=()):
modules = ("IASON",) + tuple(extra_modules)
prefix = "\n".join(f"CVM {m}" for m in modules) + "\n"
return prefix + f"IASON_LEGE('{arg}')"
def _src_scribe(arg_text, extra_modules=()):
modules = ("IASON",) + tuple(extra_modules)
prefix = "\n".join(f"CVM {m}" for m in modules) + "\n"
return prefix + f"IASON_SCRIBE({arg_text})"
iason_tests = [
# ---- Parse: scalars ----
(_src_lege("null"), _lege("null"), ValNul()),
(_src_lege("true"), _lege("true"), ValBool(True)),
(_src_lege("false"), _lege("false"), ValBool(False)),
(_src_lege("42"), _lege("42"), ValInt(42)),
(_src_lege('"hello"'), _lege('"hello"'), ValStr("hello")),
# ---- Parse: empty containers ----
(_src_lege("[]"), _lege("[]"), ValList([])),
(_src_lege("{}"), _lege("{}"), ValDict({})),
# ---- Parse: array of mixed types ----
(_src_lege('[1, true, null, "x"]'),
_lege('[1, true, null, "x"]'),
ValList([ValInt(1), ValBool(True), ValNul(), ValStr("x")])),
# ---- Parse: nested ----
(_src_lege('{"a": [1, 2], "b": {"c": 3}}'),
_lege('{"a": [1, 2], "b": {"c": 3}}'),
ValDict({
"a": ValList([ValInt(1), ValInt(2)]),
"b": ValDict({"c": ValInt(3)}),
})),
# ---- Parse: numbers ----
(_src_lege("-7"), _lege("-7"), ValInt(-7)),
(_src_lege("0"), _lege("0"), ValInt(0)),
# ---- Parse: string escapes ----
# NB: single-quoted CENTVRION strings unescape \n / \" / \\ before the
# JSON parser sees them, so direct parse tests for those escapes would
# have ambiguous semantics. Serialize tests below cover the inverse, and
# this \u test exercises the JSON parser's escape path.
(_src_lege('"\\u00e9"'),
_lege('"\\u00e9"'),
ValStr("é")),
# ---- Parse: float without FRACTIO floors ----
(_src_lege("3.7"), _lege("3.7"), ValInt(3)),
(_src_lege("-2.5"), _lege("-2.5"), ValInt(-3)),
(_src_lege("1e2"), _lege("1e2"), ValInt(100)),
# ---- Parse: float with FRACTIO is exact ----
(_src_lege("0.5", extra_modules=("FRACTIO",)),
_lege("0.5", modules=("IASON", "FRACTIO")),
ValFrac(Fraction(1, 2))),
(_src_lege("0.1", extra_modules=("FRACTIO",)),
_lege("0.1", modules=("IASON", "FRACTIO")),
ValFrac(Fraction(1, 10))),
(_src_lege("-0.25", extra_modules=("FRACTIO",)),
_lege("-0.25", modules=("IASON", "FRACTIO")),
ValFrac(Fraction(-1, 4))),
(_src_lege("5", extra_modules=("FRACTIO",)),
_lege("5", modules=("IASON", "FRACTIO")),
ValInt(5)),
(_src_lege("3.0", extra_modules=("FRACTIO",)),
_lege("3.0", modules=("IASON", "FRACTIO")),
ValInt(3)),
# ---- Serialize: scalars ----
(_src_scribe("NVLLVS"), _scribe(Nullus()), ValStr("null")),
(_src_scribe("VERITAS"), _scribe(Bool(True)), ValStr("true")),
(_src_scribe("FALSITAS"), _scribe(Bool(False)), ValStr("false")),
(_src_scribe("XLII"), _scribe(Numeral("XLII")), ValStr("42")),
(_src_scribe('"hello"'), _scribe(String("hello")), ValStr('"hello"')),
(_src_scribe("[]"), _scribe(DataArray([])), ValStr("[]")),
(_src_scribe("TABVLA {}"), _scribe(DataDict([])), ValStr("{}")),
# ---- Serialize: nested ----
(_src_scribe("[I, II, III]"),
_scribe(DataArray([Numeral("I"), Numeral("II"), Numeral("III")])),
ValStr("[1, 2, 3]")),
(_src_scribe('TABVLA {"a" VT I, "b" VT VERITAS}'),
_scribe(DataDict([(String("a"), Numeral("I")), (String("b"), Bool(True))])),
ValStr('{"a": 1, "b": true}')),
# ---- Serialize: special chars ----
(_src_scribe('"a\\nb"'),
_scribe(String("a\nb")),
ValStr('"a\\nb"')),
(_src_scribe('"a\\"b"'),
_scribe(String('a"b')),
ValStr('"a\\"b"')),
(_src_scribe('"a\\\\b"'),
_scribe(String("a\\b")),
ValStr('"a\\\\b"')),
# ---- Round-trip ----
("CVM IASON\nDIC(IASON_LEGE('[1, 2, 3]'))",
Program([ModuleCall("IASON")], [ExpressionStatement(BuiltIn("DIC",
[BuiltIn("IASON_LEGE", [String("[1, 2, 3]")])]))]),
ValStr("[I II III]"), "[I II III]\n"),
("CVM IASON\nDIC(IASON_SCRIBE(IASON_LEGE('{\"a\": [1, true, null]}')))",
Program([ModuleCall("IASON")], [ExpressionStatement(BuiltIn("DIC",
[BuiltIn("IASON_SCRIBE",
[BuiltIn("IASON_LEGE", [String('{"a": [1, true, null]}')])])]))]),
ValStr('{"a": [1, true, null]}'),
'{"a": [1, true, null]}\n'),
("CVM IASON\nCVM FRACTIO\nDIC(IASON_SCRIBE(IASON_LEGE('0.5')))",
Program([ModuleCall("IASON"), ModuleCall("FRACTIO")],
[ExpressionStatement(BuiltIn("DIC",
[BuiltIn("IASON_SCRIBE",
[BuiltIn("IASON_LEGE", [String("0.5")])])]))]),
ValStr("0.5"), "0.5\n"),
("CVM IASON\nCVM FRACTIO\nDIC(IASON_SCRIBE(IASON_LEGE('0.1')))",
Program([ModuleCall("IASON"), ModuleCall("FRACTIO")],
[ExpressionStatement(BuiltIn("DIC",
[BuiltIn("IASON_SCRIBE",
[BuiltIn("IASON_LEGE", [String("0.1")])])]))]),
ValStr("0.1"), "0.1\n"),
# ---- Serialize: insertion order preserved ----
(_src_scribe('TABVLA {"b" VT II, "a" VT I, "c" VT III}'),
_scribe(DataDict([
(String("b"), Numeral("II")),
(String("a"), Numeral("I")),
(String("c"), Numeral("III")),
])),
ValStr('{"b": 2, "a": 1, "c": 3}')),
# ---- Whitespace-tolerant parse ----
(_src_lege(" [ 1 , 2 ] "),
_lege(" [ 1 , 2 ] "),
ValList([ValInt(1), ValInt(2)])),
# ---- Unicode passes through serialize (ensure_ascii=False) ----
('CVM IASON\nDIC(IASON_SCRIBE("café"))',
Program([ModuleCall("IASON")], [ExpressionStatement(BuiltIn("DIC",
[BuiltIn("IASON_SCRIBE", [String("café")])]))]),
ValStr('"café"'), '"café"\n'),
]
class TestIason(unittest.TestCase):
@parameterized.expand(iason_tests)
def test_iason(self, source, nodes, value, output="", input_lines=[]):
run_test(self, source, nodes, value, output, input_lines)

View File

@@ -8,7 +8,7 @@ from tests._helpers import (
String, TemptaStatement, UnaryMinus, UnaryNot, Fractio, frac_to_fraction,
fraction_to_frac, num_to_int, int_to_num, make_string,
ValInt, ValStr, ValBool, ValList, ValDict, ValNul, ValFunc, ValFrac,
CentvrionError, _RUNTIME_C, _cent_rng,
CentvrionError, _RUNTIME_C, _IASON_C, _cent_rng,
Lexer, Parser, compile_program,
os, subprocess, tempfile, StringIO, patch,
)
@@ -71,6 +71,26 @@ error_tests = [
("ORDINA(I)", CentvrionError), # ORDINA on non-array
('ORDINA([I, "a"])', CentvrionError), # ORDINA mixed types
("DESIGNA x VT I\nORDINA(x)", CentvrionError), # ORDINA on id (non-array)
("ORDINA([I, II], V)", CentvrionError), # ORDINA comparator not a function
("DEFINI bad (a) VT { REDI (VERITAS) }\nORDINA([I, II], bad)", CentvrionError), # ORDINA comparator wrong arity
("DEFINI bad (a, b) VT { REDI (V) }\nORDINA([I, II], bad)", CentvrionError), # ORDINA comparator returns non-bool
("ORDINA([I], V, V)", CentvrionError), # ORDINA too many args
("MVTA([I, II])", CentvrionError), # MVTA too few args
("MVTA([I, II], FVNCTIO (x) VT { REDI (x) }, V)", CentvrionError), # MVTA too many args
("MVTA(I, FVNCTIO (x) VT { REDI (x) })", CentvrionError), # MVTA on non-array
("MVTA([I, II], V)", CentvrionError), # MVTA function arg not a function
("DEFINI bad (a, b) VT { REDI (a) }\nMVTA([I, II], bad)", CentvrionError), # MVTA function wrong arity
("CRIBRA([I, II])", CentvrionError), # CRIBRA too few args
("CRIBRA([I, II], FVNCTIO (x) VT { REDI (VERITAS) }, V)", CentvrionError), # CRIBRA too many args
("CRIBRA(I, FVNCTIO (x) VT { REDI (VERITAS) })", CentvrionError), # CRIBRA on non-array
("CRIBRA([I, II], V)", CentvrionError), # CRIBRA predicate not a function
("DEFINI bad (a, b) VT { REDI (VERITAS) }\nCRIBRA([I, II], bad)", CentvrionError), # CRIBRA predicate wrong arity
("DEFINI bad (x) VT { REDI (V) }\nCRIBRA([I, II], bad)", CentvrionError), # CRIBRA predicate returns non-bool
("CONFLA([I, II], I)", CentvrionError), # CONFLA too few args
("CONFLA([I, II], I, FVNCTIO (a, b) VT { REDI (a + b) }, V)", CentvrionError), # CONFLA too many args
("CONFLA(I, I, FVNCTIO (a, b) VT { REDI (a + b) })", CentvrionError), # CONFLA on non-array
("CONFLA([I, II], I, V)", CentvrionError), # CONFLA function arg not a function
("DEFINI bad (a) VT { REDI (a) }\nCONFLA([I, II], I, bad)", CentvrionError), # CONFLA function wrong arity
("SENATVS(I)", CentvrionError), # SENATVS requires booleans
("SENATVS(VERITAS, I)", CentvrionError), # SENATVS mixed types
("SENATVS([I, II, III])", CentvrionError), # SENATVS array of non-bools
@@ -132,6 +152,32 @@ error_tests = [
('CVM RETE\nPETITVR("/", FVNCTIO (r) VT {\nREDI("hi")\n})\nAVSCVLTA("text")', CentvrionError), # AVSCVLTA port must be integer
("DONICVM i VT I VSQVE X GRADV I - I FAC { DIC(i) }", CentvrionError), # GRADV zero step
('DONICVM i VT I VSQVE X GRADV "foo" FAC { DIC(i) }', CentvrionError), # GRADV non-integer step
("NECTE([I, II], [III])", CentvrionError), # NECTE length mismatch
('NECTE(I, [II])', CentvrionError), # NECTE first arg not a list
('NECTE([I], II)', CentvrionError), # NECTE second arg not a list
("IVNGE([I, II], [III])", CentvrionError), # IVNGE length mismatch
('IVNGE(I, [II])', CentvrionError), # IVNGE first arg not a list
('IVNGE(["a"], II)', CentvrionError), # IVNGE second arg not a list
("IVNGE([VERITAS], [I])", CentvrionError), # IVNGE invalid key type (bool)
("IVNGE([[I]], [II])", CentvrionError), # IVNGE invalid key type (list)
("IASON_LEGE('null')", CentvrionError), # IASON module required for IASON_LEGE
("IASON_SCRIBE(NVLLVS)", CentvrionError), # IASON module required for IASON_SCRIBE
("CVM IASON\nIASON_LEGE(I)", CentvrionError), # IASON_LEGE non-string arg
("CVM IASON\nIASON_LEGE()", CentvrionError), # IASON_LEGE no args
("CVM IASON\nIASON_LEGE('null', 'null')", CentvrionError), # IASON_LEGE too many args
("CVM IASON\nIASON_LEGE('not json')", CentvrionError), # invalid JSON
("CVM IASON\nIASON_LEGE('[1,]')", CentvrionError), # trailing comma in array
("CVM IASON\nIASON_LEGE('{\"a\":}')", CentvrionError), # missing value in object
("CVM IASON\nIASON_LEGE('{\"a\" 1}')", CentvrionError), # missing colon in object
("CVM IASON\nIASON_LEGE('[1, 2')", CentvrionError), # unterminated array
("CVM IASON\nIASON_LEGE('{')", CentvrionError), # unterminated object
("CVM IASON\nIASON_LEGE('\"abc')", CentvrionError), # unterminated string
("CVM IASON\nIASON_LEGE('[1] junk')", CentvrionError), # trailing data
("CVM IASON\nIASON_LEGE('[\"a\\\\x\"]')", CentvrionError), # invalid escape
("CVM IASON\nIASON_SCRIBE()", CentvrionError), # IASON_SCRIBE no args
("CVM IASON\nIASON_SCRIBE(I, II)", CentvrionError), # IASON_SCRIBE too many args
('CVM IASON\nIASON_SCRIBE(IVNGE([I], ["v"]))', CentvrionError), # IASON_SCRIBE int dict keys
("CVM IASON\nIASON_SCRIBE(FVNCTIO (x) VT { REDI(x) })", CentvrionError), # IASON_SCRIBE function
]
class TestErrors(unittest.TestCase):
@@ -146,3 +192,34 @@ class TestCompilerErrors(unittest.TestCase):
@parameterized.expand(compiler_error_tests)
def test_compiler_errors(self, source, error_type):
run_compiler_error_test(self, source)
class TestErrorLineNumbers(unittest.TestCase):
def test_interpreter_error_includes_line(self):
source = "DESIGNA x VT III\nDIC(y)\n"
tokens = Lexer().get_lexer().lex(source)
program = Parser().parse(tokens)
with self.assertRaisesRegex(CentvrionError, r"at line 2"):
program.eval()
def test_compiled_error_includes_line(self):
source = "DESIGNA x VT III\nDIC(y)\n"
tokens = Lexer().get_lexer().lex(source)
program = Parser().parse(tokens)
c_source = compile_program(program)
with tempfile.NamedTemporaryFile(suffix=".c", delete=False, mode="w") as tmp_c:
tmp_c.write(c_source)
tmp_c_path = tmp_c.name
with tempfile.NamedTemporaryFile(suffix="", delete=False) as tmp_bin:
tmp_bin_path = tmp_bin.name
try:
subprocess.run(
["gcc", "-O2", tmp_c_path, _RUNTIME_C, _IASON_C, "-o", tmp_bin_path, "-lcurl", "-lmicrohttpd", "-lm"],
check=True, capture_output=True,
)
proc = subprocess.run([tmp_bin_path], capture_output=True, text=True)
self.assertNotEqual(proc.returncode, 0)
self.assertIn("at line 2", proc.stderr)
finally:
os.unlink(tmp_c_path)
os.unlink(tmp_bin_path)

View File

@@ -24,10 +24,12 @@ from centvrion.lexer import Lexer
from centvrion.parser import Parser
from centvrion.values import ValInt, ValStr, ValBool, ValList, ValDict, ValNul, ValFunc, ValFrac
_RUNTIME_C = os.path.join(
_RUNTIME_DIR = os.path.join(
os.path.dirname(__file__), "..",
"centvrion", "compiler", "runtime", "cent_runtime.c"
"centvrion", "compiler", "runtime"
)
_RUNTIME_C = os.path.join(_RUNTIME_DIR, "cent_runtime.c")
_IASON_C = os.path.join(_RUNTIME_DIR, "cent_iason.c")
def run_test(self, source, target_nodes, target_value, target_output="", input_lines=[]):
_cent_rng.seed(1)
@@ -92,7 +94,7 @@ def run_test(self, source, target_nodes, target_value, target_output="", input_l
tmp_bin_path = tmp_bin.name
try:
subprocess.run(
["gcc", "-O2", tmp_c_path, _RUNTIME_C, "-o", tmp_bin_path, "-lcurl", "-lmicrohttpd"],
["gcc", "-O2", tmp_c_path, _RUNTIME_C, _IASON_C, "-o", tmp_bin_path, "-lcurl", "-lmicrohttpd", "-lm"],
check=True, capture_output=True,
)
stdin_data = "".join(f"{l}\n" for l in input_lines)
@@ -124,7 +126,7 @@ def run_compiler_error_test(self, source):
tmp_bin_path = tmp_bin.name
try:
subprocess.run(
["gcc", "-O2", tmp_c_path, _RUNTIME_C, "-o", tmp_bin_path, "-lcurl", "-lmicrohttpd"],
["gcc", "-O2", tmp_c_path, _RUNTIME_C, _IASON_C, "-o", tmp_bin_path, "-lcurl", "-lmicrohttpd", "-lm"],
check=True, capture_output=True,
)
proc = subprocess.run([tmp_bin_path], capture_output=True, text=True)

View File

@@ -65,7 +65,7 @@
"patterns": [
{
"name": "support.function.builtin.cent",
"match": "\\b(ADIVNGE|AVDI_NVMERVS|AVDI|AVSCVLTA|CLAVES|DECIMATIO|DIC|DORMI|EVERRE|FORTVITVS_NVMERVS|FORTVITA_ELECTIO|LEGE|LITTERA|LONGITVDO|MAIVSCVLA|MINVSCVLA|NVMERVS|ORDINA|PETE|PETITVR|QVAERE|SCINDE|SCRIBE|SEMEN|SENATVS|SVBSTITVE|TYPVS)\\b"
"match": "\\b(ADDE|ADIVNGE|AVDI_NVMERVS|AVDI|AVSCVLTA|CLAVES|CONFLA|CRIBRA|DECIMATIO|DIC|DORMI|EVERRE|FORTVITVS_NVMERVS|FORTVITA_ELECTIO|IASON_LEGE|IASON_SCRIBE|INSERE|IVNGE|LEGE|LITTERA|LONGITVDO|MAIVSCVLA|MINVSCVLA|MVTA|NECTE|NVMERVS|ORDINA|PETE|PETITVR|QVAERE|SCINDE|SCRIBE|SEMEN|SENATVS|SVBSTITVE|TOLLE|TYPVS)\\b"
}
]
},
@@ -73,7 +73,7 @@
"patterns": [
{
"name": "support.class.module.cent",
"match": "\\b(FORS|FRACTIO|MAGNVM|RETE|SCRIPTA|SVBNVLLA)\\b"
"match": "\\b(FORS|FRACTIO|IASON|MAGNVM|RETE|SCRIPTA|SVBNVLLA)\\b"
}
]
},