Commit Graph

78 Commits

Author SHA1 Message Date
Louis Gesbert
cee8e57d02 More precise positions for operators throughout 2024-04-30 16:35:08 +02:00
Louis Gesbert
eded54d2b5 Support for direct tuple member access
As discussed in #549

NOTE: This implements only the direct tuple member access (syntax `foo.N` with N a
number)

- It seems more efficient to wait for the general pattern-matching rewrite to
  handle pattern-matching on tuples
- Until then we keep the (now obsolete) `let (x, y) = pair in x` syntax, to
  leave time for updates, but we won't be documenting it
2024-04-13 09:37:03 +02:00
Louis Gesbert
a61ae7979f
Support for structure updates
Closes #592

A new node is added in `desugared`, and translated into an exploded structure
literal during translation to `scopelang`. The main reason to put it there is
that it needs to be after disambiguation, since that is used to discover the
type of the structure that is being updated.
2024-04-12 17:17:48 +02:00
Louis Gesbert
98fc97a241 Rewriting message calls to use the new intf 2024-04-10 19:26:23 +02:00
Louis Gesbert
eeaadef27c Output subscopes: implement syntax
required a little generalisation and explicit parsing errors to avoid conflicts,
but it remains reasonable
2024-04-04 10:24:18 +02:00
Louis Gesbert
2f2924944a Syntax: add keywords in front of list filtering / arg-extremum
Closes #574
2024-02-14 13:55:03 +01:00
Louis Gesbert
bf0ef0b1f5 Allow access to intermediate variable states
Through the syntax `var_name state state_name`.

Addresses #567, but
- requires documentation
- explicitely accessing states of the variable being defined is not allowed at
  the moment (warning proposed in #567 requires a whole-expression check, which
  is better done some place else entirely)
2024-02-12 18:15:48 +01:00
Louis Gesbert
371f9554b8 Support for list recombinations
The primary use-case for this was to be able to run computations on a list of
structures, then return an updated list with some fields in the structures
modified : that is what we need for distribution of tax amounts among household
members, for example.

This patch has a few components:

- Addition of a test as an example for tax distributions

- Added a transformation, performed during desugaring, that -- where lists are
  syntactically expected, i.e. after the `among` keyword -- turns a (syntactic)
  tuple of lists into a list of tuples ("zipping" the lists)

- Arg-extremum transformation was also fixed to use an intermediate list instead
  of computing the predicate twice

- For convenience, allow to bind multiple variables in most* list
  operations (previously only `let in` and functions allowed it)

- Fixed the printer for tuples to differentiate them from lists

*Note: tuples are not yet allowed on the left-hand side of filters and
arg-extremums for annoying syntax conflict reasons.
2024-01-26 11:29:33 +01:00
Louis Gesbert
94ebc1b65e Allow deconstruction of tuples using let in 2023-12-19 17:25:44 +01:00
Louis Gesbert
df3ab64fe9 Add tuples to the surface language
No helpers to destruct them at the moment
2023-12-19 17:25:44 +01:00
Louis Gesbert
e123d7eb95 Change type syntax of collection into list of 2023-12-19 15:26:44 +01:00
Louis Gesbert
9a255522be Document and first test for externals
Also some fixes for Clerk to properly support them
2023-12-06 11:06:54 +01:00
Louis Gesbert
9aed2c7479 support for parsing Module directives 2023-09-27 13:08:11 +02:00
Louis Gesbert
9bac045d03 Implement module lookups for scopes, structs, and enums 2023-08-31 17:54:39 +02:00
Louis Gesbert
26c75af2ae Allow declaration of toplevel values without definition 2023-06-15 17:57:01 +02:00
Aminata-Dev
10d147a8b1 Messages renamed to Message (lighter syntax) 2023-06-13 11:50:56 +02:00
Denis Merigoux
0faa97b8fc Abstract messages interface 2023-06-02 10:50:29 +02:00
Louis Gesbert
fc531777c0 Rework and normalise the Marked interface
The module is renamed to `Mark`, and functions renamed to avoid redundancy:

`Marked.mark` is now `Mark.add`
`Marked.unmark` is now `Mark.remove`
`Marked.map_under_mark` is now simply `Mark.map`
etc.

`Marked.same_mark_as` is replaced by `Mark.copy`, but with the arguments
swapped (which seemed more convenient throughout)

Since a type `Mark.t` would indicate a mark, and to avoid confusion, the type
`Marked.t` is renamed to `Mark.ed` as a shorthand for `Mark.marked` ; this part
can easily be removed if that's too much quirkiness.
2023-05-17 17:37:00 +02:00
Raphaël Monat
b013f4257d Update parser error message 2023-03-16 17:20:13 +01:00
Raphaël Monat
7021c41f93 Add date rounding option within scopes 2023-03-16 16:55:55 +01:00
Louis Gesbert
6388a4b79f Improved some error messages (cyclic defs, def positions) 2023-03-08 15:27:48 +01:00
Louis Gesbert
188a186735 Parser: make state rule syntax consistent with state definitions
Previously the `state` marker for rules was in a weird position:
```catala
rule foo under condition bar
  consequence state st fulfilled
```

This patch unifies the syntax with definitions, now using instead:
```catala
rule foo state st
  under condition bar
  consequence fulfilled.
```
2023-03-08 15:17:58 +01:00
Louis Gesbert
3766e9db2c Fix function argument naming consistency in the examples 2023-02-28 16:17:19 +01:00
Louis Gesbert
fc5910e093 Fix multi-argument scopedefs, improve error message positions
This adds a few positions to the parser, and tweaks some others, vastly
improving the reporting of some errors (inconsistent functions definitions, but
also exceptions cycles, etc.)
2023-02-28 15:53:50 +01:00
Louis Gesbert
2c97d5de14 Enforce consistency of function arguments naming 2023-02-28 15:53:50 +01:00
Louis Gesbert
8200457e43 Syntax change: require declaration of function argument names 2023-02-28 15:53:50 +01:00
Louis Gesbert
72cf477dd9 Toplevel definitions: fixes following review
Thanks @denismerigoux!
2023-02-15 12:40:15 +01:00
Louis Gesbert
6f1ac5837d Add syntax for calling multi-argument functions
* temporary and undocumented while waiting for discussion an approval
* previous patches already allowed definition (at toplevel) but there was no
syntax for calls
* no syntax for multi-args _local_ functions yet
2023-02-13 11:44:32 +01:00
Louis Gesbert
d66cd1e29c Toplevel defs: tests & fixes 2023-02-13 11:44:32 +01:00
Louis Gesbert
9b0c7583ec Add top-level definitions
Only handled until before scalc at the moment.
2023-02-13 11:43:49 +01:00
Louis Gesbert
98ebc36343 Add all missing parser error messages
... that's one less thing to do

Two notes:

- Updated the syntax errors in
  examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en ; those
  probably aren't expected though, but fixing them is outside my purpose here

- There is consensus on keeping the error messages in English; however, here,
  the error messages include hints on the syntax to use, which are only valid
  for users of the English syntax.
  * A possible solution would be to apply cppo on parser.messages, using the
    macros already defined in lexer_LANG.cppo.ml. However, we would then need to
    tweak (or duplicate!) the parser to use the messages for the correct language.
    Furthermore, updating and merging the file on parser updates would need
    special care.
  * Another, maybe easier solution would be manual processing, using a custom
    escape in the parser messages and rewriting that at runtime when printing
    the message. We would need to extract a runtime version of the macro
    definitions though.
2023-02-13 10:51:42 +01:00
Louis Gesbert
3f487a16ed WIP: handle toplevel definitions at the parser level 2023-02-13 10:51:42 +01:00
Louis Gesbert
19033669f5 Add support for paths in the parser
Using them will lead to "not supported yet" errors soon after, but it's a start
to get to handling separate modules.

The idea is that `foo` can now also be `Bar.foo`, `Bar.Baz.foo`, `foo.Struc.fld`
can be `foo.Bar.Baz.Struc.fld`, etc.
The next steps are to enable the lookups to handle this paths, and to provide
ways to load the external modules to feed these lookups.
2023-01-16 12:09:23 +01:00
Louis Gesbert
f835225a34 Tiny parser simplification
There is no need to keep separate rules for the different kinds of binops anymore.
2023-01-16 12:09:19 +01:00
Denis Merigoux
d2ce111fc2
Better legifrance inclusion 2023-01-04 16:30:14 +01:00
Louis Gesbert
2003566867 Force parens in compound logic formulas
Closes #373

This forbids expressions such as `a and b or c`, avoiding the need to set an
implicit priority between `and`, `or` and `xor`, which I find error-prone.

Instead, when that appears, a message asking for explicit parentheses will be
shown to the user.

Implementation note: since that would be extremely tedious to do in the parser
directly, the parser is set to allow right-associativity without discrimination
for the logical operators, and the check is done during desugaring. This
required to explicit parentheses in the surface AST to discriminate the case
where the priority was explicit.
2023-01-04 10:46:14 +01:00
Louis Gesbert
51df581aba Small cleanup/fixes following PR review on Syntax Changes 2022-12-20 16:03:41 +01:00
Louis Gesbert
9e514755b7 Collection syntax: re-add combined filter+map 2022-12-19 15:17:17 +01:00
Louis Gesbert
e678d0770f Rename a few tokens
In particular `CONSTRUCTOR` is no longer valid for paths & modules, so let's
switch to the more usual LIDENT / UIDENT for lower- or upper- case idents.

cd compiler/surface
sed -i 's/VERTICAL/BAR/g' *
sed -i 's/BRACKET/BRACE/g' *
sed -i 's/SQUARE/BRACKET/g' *
sed -i 's/IDENT/LIDENT/g' *
sed -i 's/CONSTRUCTOR/UIDENT/g' *
2022-12-19 15:17:17 +01:00
Louis Gesbert
47502335aa Refactor the parser to use priorities
Define a single expression rule with disambiguation using token priorities
instead of the many layers of intermediate rules with explicit sub-terms.

Also replaces `in` for collection operations (`x+1 for foo in [1;2]`) with
`among` which helps a lot.
2022-12-19 15:12:53 +01:00
Louis Gesbert
29a961f786 Puns 2022-12-19 13:03:19 +01:00
Louis Gesbert
b9cc89ebb5 Switch the parser to "new" menhir syntax 2022-12-19 13:03:19 +01:00
Louis Gesbert
f236e2cfb2 Replace the type conversion and rounding operators with overloads
Ref. #366

Also updates `CONTRIBUTING.md`.

This was pretty straight-forward :)
2022-12-13 15:32:49 +01:00
Louis Gesbert
9b939d07a4 New syntax for collection operations 2022-12-13 12:30:40 +01:00
Louis Gesbert
bb58d11ca8 Improve syntax for scope calls
implements #357
2022-12-13 12:30:38 +01:00
Louis Gesbert
fea01cfe4c Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.

The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions

The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-12-13 11:55:24 +01:00
Louis Gesbert
660e5775de Rename utils to catala_utils 2022-11-28 16:38:09 +01:00
Louis Gesbert
47799ea24f Uniform naming of conversion modules across compilation passes 2022-11-22 12:08:18 +01:00
Louis Gesbert
41d6d3cbe9 Make scopes directly callable
Quite a few changes are included here, some of which have some extra
implications visible in the language:

- adds the `Scope of { -- input_v: value; ... }` construct in the language

- handle it down the pipeline:
  * `ScopeCall` in the surface AST
  * `EScopeCall` in desugared and scopelang
  * expressions are now traversed to detect dependencies between scopes
  * transformed into a normal function call in dcalc

- defining a scope now implicitely defines a structure with the same name, with
  the output variables of the scope defined as fields. This allows us to type
  the return value from a scope call and access its fields easily.
  * the implications are mostly in surface/name_resolution.ml code-wise
  * the `Scope_out` struct that was defined in scope_to_dcalc is no longer
    needed/used and the fields are no longer renamed (changes some outputs; the
    explicit suffix for variables with multiple states is ignored as well)
  * one benefit is that disambiguation works just like for structures when there
    are conflicts on field names
  * however, it's now a conflict if a scope and a structure have the same
    name (side-note: issues with conflicting enum / struct names or scope
    variables / subscope names were silent and are now properly reported)

- you can consequently use scope names as types for variables as well. Writing
  literals is not allowed though, they can only be obtained by calling the
  scope.

Remaining TODOs:

- context variables are not handled properly at the moment

- error handling on invalid calls

- tests show a small error message regression; lots of examples will need
  tweaking to avoid scope/struct name or struct fields / output variable
  conflicts

- add a `->` syntax to make struct field access distinct from scope output var
  access, enforced with typing. This is expected to reduce confusion of users
  and add a little typing precision.

- document the new syntax & implications (tutorial, cheat-sheet)

- a consequence of the changes is that subscope variables also can now be typed.
  A possible future evolution / simplification would be to rewrite subscopes as
  explicit scope calls early in the pipeline. That could also allow to manipulate
  them as expressions (bind them in let-ins, return them...)
2022-10-21 17:17:26 +02:00
Denis Merigoux
8445174a5b
Improvements to expiration checking 2022-09-07 17:14:22 +02:00