Skip to content

0.1.9

Latest

Choose a tag to compare

@Morglod Morglod released this 06 Oct 23:53
· 2 commits to master since this release

0.1.9

A lot of fixes around generic functions.

Most major thing is that function pointers and slices now could be trasferred from comptime to runtime:

struct Command {
    name: []u8;
    run: *fn (x: i32): i32;
};

commands: [16]Command = 0; #no_comptime_reset
commands_len: i32 = 0;     #no_comptime_reset

fn register(name: []u8, run: *fn(x: i32): i32) {
    commands[commands_len].name = name;
    commands[commands_len].run = run;
    commands_len = commands_len + 1;
}

fn double(x: i32): i32 { return x * 2; }
fn square(x: i32): i32 { return x * x; }

#run {
    register("double", double);
    register("square", square);
}

#linkc fn main(): i32 {
    return commands[1].run(commands_len);   // square(2) == 4
}

See examples/5_comptime_command_list.mox for more advanced example

Compiler

Added untyped literals passed to generic type error

Fixed types declaration resolution though named module import

Interp with failed poly instantiation now prints same diagnostic as compilation path

Added __compiler_register_hook(hook_id, #callback, custom_data_ptr) builtin:

MOX_COMPILER_HOOK_COMPILATION_ENDED hook expands macro #callback as if it was written like #run #callback(custom_data_ptr) at the end of the module
but it runs at the end of whole compilation. Useful to emit additional code or patch #no_comptime_reset globals

Slices of any type and function pointers now survive comptime -> runtime:
in #no_comptime_reset globals (including structs and arrays of structs with slices / function pointers, nested slices),
in global initializers (log_fn: *fn(): i32 = default_log;)
and in #run results / constants.

Raw pointers are still rejected, see known_limitations.md

Global variable of slice type are now writable (before they were lowered as readonly)

A function can be used as a global initializer value even when it is declared below the global

Fixed address of a #linkc function in JIT output pointing to an internal trampoline instead of the external symbol

Fixed address of a function taken before its definition missing a relocation in JIT object output

Fixed interpreter not able to call function pointers produced by native comptime code or pointing to #linkc functions

Fixed comptime function handles of different poly instances (and functions nested in poly bodies) being merged into one

Fixed field access on an element of a self-referential struct's slice (tree.kids[0].name)

Fixed comparing a nullable function pointer with 0 in runtime code (if (cb != 0))

Named constants (including struct constants) stay comptime values in runtime code: they can be passed straight to $ params (no more #run NAME)

When a name has both $ and runtime overloads, a comptime argument now picks the $ overload and a runtime argument picks the runtime one.
(before, an exact runtime match won first, so f(CONST) went to the runtime overload while f(5) went to $)

Typed struct literal arguments S.{ ... } pick the $ overload when all fields are comptime, and the runtime overload otherwise

Anonymous .{ ... } literal can be passed to a concrete struct param of a generic function (generic struct params like Box($T) fails)

Field access on a comptime struct value in runtime code ($s.y, CONST.field) now folds to a comptime value

Fixed a runtime value passed to a $ param silently reusing an already baked instance, now it is an "Expected comptime value" error

Fixed JIT panic when a comptime argument could not be converted to runtime

Fixed generic function called through a named import (lib.make_box(x)) resolving its signature types in the caller's module ("Type with this name was not found")

Fixed loops and switch inside a generic function

Fixed parsing identifiers with op signs

Functions that returns non void without 'return' is control flow error now (before it was not checked in some cases)

Comma separators and semicolons could be mixed in struct literals or arrays. Last separator is optional.

Modules

A lot of math utils

arena_push_slice

fn c_string_into(arena: *Arena, str: []u8): *u8

fn bitcast($T_Dst: __type_ptr, x: $T_Src): $T_Dst

fn utf8_decode_iterate(text: []u8): Utf8DecodeIterator

c_interp.mox renamed to c_interop.mox

arena_push_slice added to alocate new slice on arena