Basic type checker #6

Merged
HEL merged 74 commits from feat/basic-type-checker into main 2026-06-05 09:31:54 +00:00
Owner

This PR implements a basic type checker. It handles simple python constructs such as:

  • literals (i.e. booleans, integers, floats, strings)
  • variable assignment (w/ and w/o type annotation)
  • variable references
  • binary operations (i.e. +, -, *, /, etc.)
  • comparisons (i.e. >, <, >=, <=, etc.)
  • logical operations (i.e. and, or)
  • if statements
  • ternary expression
  • function definition
  • function call
  • return statements
  • casting (i.e. cast(Type, expression))

Some types and operations are defined by default to match what Python provides (e.g. literal types and their operations). The checker also supports importing custom type definitions from Midas files.
Currently, type checking is strict: types must match exactly. This is especially true for function return types, which must all be the same in a given function, and ternary expressions, whose branches must be of the same type.
Subtyping and genericity will be added in a separate PR

The type checker generates a list of diagnostics (errors, warnings and infos), which can be highlighted in the source file using the following command:

uv run midas compile -l highlighted.html -t types.midas source.py
This PR implements a basic type checker. It handles simple python constructs such as: - literals (i.e. booleans, integers, floats, strings) - variable assignment (w/ and w/o type annotation) - variable references - binary operations (i.e. `+`, `-`, `*`, `/`, etc.) - comparisons (i.e. `>`, `<`, `>=`, `<=`, etc.) - logical operations (i.e. `and`, `or`) - `if` statements - ternary expression - function definition - function call - return statements - casting (i.e. `cast(Type, expression)`) Some types and operations are defined by default to match what Python provides (e.g. literal types and their operations). The checker also supports importing custom type definitions from Midas files. Currently, type checking is strict: types must match exactly. This is especially true for function return types, which must all be the same in a given function, and ternary expressions, whose branches must be of the same type. Subtyping and genericity will be added in a separate PR The type checker generates a list of diagnostics (errors, warnings and infos), which can be highlighted in the source file using the following command: ```shell uv run midas compile -l highlighted.html -t types.midas source.py ```
HEL self-assigned this 2026-06-05 09:31:36 +00:00
HEL added 74 commits 2026-06-05 09:31:37 +00:00
adapted from Pebble
adapted from Pebble
this is still very basic and only handle a few expressions
notably, it doesn't support generics, option types, conditions, predicates nor complex types
still very basic but lays out the structure and help methods
display missing arguments in a stable format, similar to how native Python does
to handle simple recursion cases where the function has an explicit return type hint, the function must be defined before evaluating its body
improve a couple methods names, namely evaluate → type_of and evaluate_block → process_block
remove custom import statement (`midas.using`) in favor of passing type definition files as arguments to the checker
Reviewed-on: #5
HEL merged commit 13d32d0d27 into main 2026-06-05 09:31:54 +00:00
HEL deleted branch feat/basic-type-checker 2026-06-05 09:31:54 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: HEL/midas#6