From 1584ec7563699862744dbac187d7515245ee5acf Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Tue, 17 Dec 2024 21:11:02 +0100 Subject: [PATCH] adding @Any directive Signed-off-by: Jakub Doka --- lang/README.md | 3 ++- lang/src/son.rs | 13 +++++++++++-- lang/tests/son_tests_die.txt | 10 ++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lang/README.md b/lang/README.md index 4c0f1a03..f3f0cea0 100644 --- a/lang/README.md +++ b/lang/README.md @@ -447,7 +447,7 @@ main := fn(): uint { return @inline(foo.foo) } -$sum := fn(a: @any(), b: @TypeOf(a)): @TypeOf(a) return a + b +$sum := fn(a: @Any(), b: @TypeOf(a)): @TypeOf(a) return a + b // in module: foo.hb @@ -473,6 +473,7 @@ arbitrary text - `@inline(, ...)`: equivalent to `(...)` but function is guaranteed to inline, compiler will otherwise never inline - `@len()`: reports a length of the type of indexing purposes or length ot a string constant - `@kindof()`: gives an u8 integer describing the kind of type as an index to array `[Builtin, Struct, Enum, Union, Ptr, Slice, Opt, Func, Template, Global, Const, Module]` +- `@Any()`: generic parameter based on inference, TBD: this will ake arguments in the future that restrict what is accepted #### c_strings ```hb diff --git a/lang/src/son.rs b/lang/src/son.rs index af5aefef..99542c78 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -2862,7 +2862,16 @@ impl<'a> Codegen<'a> { self.tys.tmp.args.push(ty); let sym = parser::find_symbol(&fast.symbols, carg.id); let ty = if ty == ty::Id::ANY_TYPE { - todo!() + let ty = self.infer_type(arg); + *self.tys.tmp.args.last_mut().unwrap() = ty; + self.ci.scope.vars.push(Variable::new( + carg.id, + ty, + false, + NEVER, + &mut self.ci.nodes, + )); + continue; } else if sym.flags & idfl::COMPTIME == 0 { // FIXME: could fuck us continue; @@ -2872,7 +2881,7 @@ impl<'a> Codegen<'a> { arg.pos(), fa!( "arbitrary comptime types are not supported yet \ - (expected '{}' got '{}')", + (expected '{}' got '{}')", self.ty_display(ty::Id::TYPE), self.ty_display(ty) ), diff --git a/lang/tests/son_tests_die.txt b/lang/tests/son_tests_die.txt index 253b7cdc..c33c8e22 100644 --- a/lang/tests/son_tests_die.txt +++ b/lang/tests/son_tests_die.txt @@ -1,5 +1,11 @@ -main: +fun: UN -code size: 9 +main: + ADDI64 r254, r254, -8d + ST r31, r254, 0a, 8h + JAL r31, r0, :fun + LD r31, r254, 0a, 8h + ADDI64 r254, r254, 8d +code size: 64 ret: 0 status: Err(Unreachable)