107 Commits

Author SHA1 Message Date
Johan B.W. de Vries
10946486fc Notes 2025-07-20 13:11:49 +02:00
Johan B.W. de Vries
8a1a6af3e7 Reworked the examples to be more welcoming 2025-06-05 19:49:07 +02:00
Johan B.W. de Vries
3cb4860973 Subscriptable is now less hardcoded
Now only the tuple variant is hardcoded. The rest is via
a typeclass.
2025-06-02 19:01:20 +02:00
Johan B.W. de Vries
6f40276a9c Fix: Subscript all dynamic arrays
The type checker would only allow bytes.
2025-06-02 18:06:12 +02:00
Johan B.W. de Vries
38294497cb Moves the prelude to runtime
Previously, it was hardcoded at 'compile' time (in as much
Python has that). This would make it more difficult to add
stuff to it. Also, in a lot of places we made assumptions
about prelude instead of checking properly.
2025-05-29 16:43:37 +02:00
Johan B.W. de Vries
d97be81828 Optimise: Remove unused functions
By default, we add a lot of build in functions that may
never get called.

This commit adds a simple reachability graph algorithm
to remove functions that can't be called from outside.

Also, unmarks a lot of functions as being exported. It
was the default to export - now it's the default to not
export.

Also, some general cleanup to the wasm statement calls.
2025-05-25 16:39:25 +02:00
Johan B.W. de Vries
84e7c42ea4 Implements u16 / i16 support
Keep in mind that WebAssembly is u32 native by default,
some operations may be more expensive than you expect
them to be.
2025-05-25 15:31:23 +02:00
Johan B.W. de Vries
2c2a96c8a7 Added some more missing test cases for promotable 2025-05-25 14:21:25 +02:00
Johan B.W. de Vries
b670bb02ad Exposes Wasm's convert and trunc(ate) function
Also adds a missing type case for promotable
2025-05-25 14:13:46 +02:00
Johan B.W. de Vries
56ab88db2c Exposes Wasm's reinterpret function 2025-05-25 13:45:18 +02:00
Johan B.W. de Vries
cfdcaa230d Added a missing type check test 2025-05-21 19:22:45 +02:00
Johan B.W. de Vries
b48260ccfa Removes the special casing for foldl
Now both dynamic and static arrays can be fully fold'ed.

Also adds support for type classes that have a function
argument.

Also, various usability improvements to WasmGenerator.

Also, integration tests now don't dump their stuff without
VERBOSE=1, this speeds up the tests suite by a factor of 9.

Also, tests can now set with_traces to add a number of
tracing functions for help debugging your code.
2025-05-21 18:28:37 +02:00
Johan B.W. de Vries
46b06dbcf1 Cleanup: TYPE_INFO_MAP
This also removes the InternalPassAsPointer experiment.

This also fixes that u8 values were stores as 32 bits
in structs and tuples (but not dynamic arrays since that
is special cased as bytes).

Also, fixes allocation issue wi	th dynamic arrays, it
would allocate quadratic amount of memory.
2025-05-19 21:04:13 +02:00
Johan B.W. de Vries
83186cce78 Reworks bytes into dynamic array
bytes continues to be the preferred name for u8[...].
Also, putting bytes values into the VM and taking them
out still uses Python bytes values.

This also lets used use the len function on them, for
whatever that's worth.
2025-05-18 15:37:13 +02:00
Johan B.W. de Vries
a72bd60de2 Adds functions as passable values 2025-05-17 19:43:52 +02:00
Johan B.W. de Vries
99d2b22336 Moved the typeclasse tests. Fix typeclass name. 2025-05-17 18:42:27 +02:00
Johan B.W. de Vries
ac4b46bbe7 Fix: You could assign structs to each other
As long as the arguments matched at least.
2025-05-12 20:00:56 +02:00
Johan B.W. de Vries
67af569448 Cleanup CanBeSubscriptedConstraint
It was using an AST argument, and I'd rather not have those
in the typing system (except the generator).
2025-05-12 19:20:50 +02:00
Johan B.W. de Vries
b5f0fda133 Implements sum for Foldable types
Foldable take a TypeConstructor. The first argument must be a
NatNum.

The FunctionSignatureRouter wasn't completely on point, instead
this commit adds an TypeClassArgsRouter lookup router. This
makes sense since the only available arguments we have to find
a router is the list of type class arguments.
2025-05-12 18:36:37 +02:00
Johan B.W. de Vries
6c627bca01 Reworks function lookup
Before this commit, finding the implementation for a type
class method was done with a simple lookup table.

This commit adds a router based on function signature.
This also paves the way for adding type constructor
arguments in function signatures.

And it removes quite a few references to the prelude out
of the compiler.

Also adds a bunch of helper methods to render signatures
as strings.
2025-05-10 19:42:17 +02:00
Johan B.W. de Vries
f8d107f4fa Replaces did_construct with a proper router
By annotating types with the constructor application
that was used to create them.

Later on we can use the router to replace compiler's
INSTANCES or for user defined types.
2025-05-10 16:49:10 +02:00
Johan B.W. de Vries
6b66935c67 Chore: Cleanup type checks
A lot of isinstance checks no longer did anything, since
the referred to variable was always a type.

In some places, we used it to check if a type was internal,
it's unclear if we will need to rebuild those checks in
the future.
2025-05-07 19:07:55 +02:00
Johan B.W. de Vries
d9a08cf0f7 Chore: Placeholders are now internal
They were exposed on AST, causing confusion.
Now they're only used in constraints.
2025-05-07 19:07:51 +02:00
Johan B.W. de Vries
f6cb1a8c1d Adds a FIXME for accessing non-struct members 2025-05-05 12:35:49 +02:00
Johan B.W. de Vries
44b95af4ba Removes the cast / u32 hacky way of casting. 2025-05-02 21:12:05 +02:00
Johan B.W. de Vries
1da1adac9f Implements Extendable and Promotable 2025-05-02 21:05:07 +02:00
Johan B.W. de Vries
a2e1dfd799 Reworks type class instantiation
Before, a type class was a property of a type.
But that doesn't make any sense for multi parameter
type classes.

Had to make a hacky way for type classes with
type constructors.
2025-04-27 17:45:13 +02:00
Johan B.W. de Vries
11fde4cb9e Add missing fix for generator 2025-04-27 15:30:17 +02:00
Johan B.W. de Vries
c8009403c4 Separates out TypeVariable and constraints
They look a lot like placeholders, but they exist before
the typing system takes place. And there's a (much smaller)
context to deal with.

For now, removes Placeholders in user function definitions
as they were not implemented.

Adds signature to function to try to get them closer to
type class methods. Already seeing some benefit in the
constraint generator.

Stricter zipping for safety.
2025-04-27 15:28:08 +02:00
Johan B.W. de Vries
d3e38b96b2 Removed the len special casing 2025-04-27 12:37:17 +02:00
Johan B.W. de Vries
292c9548fb Removes some hardcoded references to prelude 2025-04-27 12:10:27 +02:00
Johan B.W. de Vries
faaf7912b1 Various cleanup to type system
- Make struct into a type constuctor
- Rework placeholders
- Got rid of 'PrimitiveType' as a concept
- Moved out the prelude to its own folder
2025-04-21 16:49:04 +02:00
Johan B.W. de Vries
234bfaa8df Changes AppliedType to TypeConstructor
First to be more in line with how the literature
treats these types. But also to make them workable with
type classes.
2025-04-21 11:14:30 +02:00
Johan B.W. de Vries
20c507a9ec Adds the Bits type class
Also adds the remaining unexposed WebAssembly opcodes as
comments (eqz, clz, ctz, popcnt, copysign).

This also means that BinaryOp.operator is now always
a type class method.
2025-04-09 15:40:20 +02:00
Johan B.W. de Vries
99e407c3c0 Adds arithmetic shift shift
"An arithmetic shift is usually equivalent to
multiplying the number by a positive or a negative
integral power of the radix." -- Federal Standard 1037C

"This is the same as multiplying x by 2**y."

"A right shift by n bits is defined as floor division
by pow(2,n). A left shift by n bits is defined as
multiplication with pow(2,n)."
2025-04-09 14:23:51 +02:00
Johan B.W. de Vries
da381e4a48 Made Integral use Python's operators
Rather than Haskell's div and rem methods, we use the
Python operators since these are often used. And we have
as goal to make it 'look like Python'
2025-04-09 13:16:50 +02:00
Johan B.W. de Vries
94c8f9388c Implements the Ord type class 2025-04-09 12:44:32 +02:00
Johan B.W. de Vries
46dbc90475 Implements ceil, floor, trunc, nearest
To round of the f32 / f64 wasm supported opcodes.

This also means we can remove the now outdated
WEBASSEMBLY_BUILTIN_FLOAT_OPS.
2025-04-06 16:38:57 +02:00
Johan B.W. de Vries
9bc8d94ffd Implements != 2025-04-06 16:20:01 +02:00
Johan B.W. de Vries
7544055a94 Split Num into NatNum and IntNum
This is because Haskell defines negate, abs and signum
for Num, but they don't work with our unsigned number
types. (abs would be a noop.) Haskell has Word32 / Word64,
but there negate doesn't make much sense to me.

Implemented neg and abs.

Implemented a type class inheritance check.

Removed Integral from u8 and i8 since it wasn't implemented.
2025-04-06 16:12:36 +02:00
Johan B.W. de Vries
4001b086db Cleanup, got rid of OPERATOR_MAP 2025-04-06 12:58:40 +02:00
Johan B.W. de Vries
19a29b7327 Migrated Num 2025-04-06 12:58:40 +02:00
Johan B.W. de Vries
ffd11c4f72 Started on a type class system 2025-04-06 12:58:40 +02:00
Johan B.W. de Vries
5d9ef0e276 Code review
Added a test, simplified another and added
a lot of TODO's.
2025-04-06 12:58:20 +02:00
Johan B.W. de Vries
521171540b Properly implemented test_crc32
Also extended the hack around u8 to u32 cast used
in crc32 - its type was lost, so it would just cast
to whatever the environment was expecting.

Had to do_format_check=False since the file has
some comments and such.
2025-04-05 16:41:46 +02:00
Johan B.W. de Vries
3e916a242e Minor fixes
- Fix python version in docs
- Made a note on which CRC32 we're using in the example
- Removed a redundant parameter
2025-04-05 16:19:26 +02:00
Johan B.W. de Vries
5c537f712e Project update
Various updates to bring the project uptodate.

- Updated required packages
- Removed runtimes that are not being updated
- wasmtime is for now the only supported runtime
- Implements imports for wasmtime runtime
- Fixes a memory access bug for wasmtime runtime
- compile_wasm is now optional - runtimes have to
  implement and call this themselves
- Typing fixes
- Linting fixes
2025-04-05 15:43:49 +02:00
Johan B.W. de Vries
97b61e3ee1 Test generation framework with typing improvements
Prior to this PR, each type would have its own handwritten
test suite. The end result was that not all types were tested
for all situations.

This PR adds a framework based on a Markdown file, which
generates the basic tests for the types defined in json
files. These are auto generated and updated by the Makefile
before the test suite is run.

Also, a number of unsupported type combinations are now
supported.

Also, we now support negative literals.

Also, allocation calculation fixes for nested types.

Also, the test helpers can now properly import and export
typed variables such as bytes, static arrays and tuples. This
may come in handy when it comes to phasm platform wanting to
route data.

Also, adds better support for i8 type.

Also, started on a runtime.py, since there's quite some code
now that deals with compile time handling of WebAssembly stuff.

Also, minor improvement to the type constrains, namely we
better match 'tuple' literals with static array types.

Also, reduced spam when printing the type analysis results;
constraints that go back on the backlog are now no longer
printed one by one. It now also prints the end results of
the typing analysis.

Also, reorganized the big test_primitives test into type
classes.

Also, replaced pylint with ruff.
2023-11-15 12:52:23 +01:00
Johan B.W. de Vries
fd3939a680 Feat: Use struct in tuple 2023-11-10 15:35:16 +01:00
Johan B.W. de Vries
16ec664cb6 Feature: Tuples with tuples in constants 2023-11-10 15:23:10 +01:00