Type Rules
Nevergarden
Posted on January 22, 2024
So I decided that types are not keywords in Shiro language and primitive types can be declared as set of rules of either assembly or the output IR or anything.
Like imagine we want to declare a 8-bit signed integer number.
type i8 = rule {
var => function(name:id, ?value:number) {
rule.out( "%" eval(name) "= alloca i8, align 1" )
branch {
(value != null) => {
rule.out( "store i8" eval(value) ", ptr %" eval(name) ", align 1" )
}
}
}
}
will be part of sys/llvm/i8.shr
file.
so if user includes it then they can assign an i8 variable.
var x : i8;
var y : i8 = 12;
will generate:
%x = alloca i8, align 1
%y = alloca i8, align 1
store i8 12, ptr %y, align 1
for LLVM IR.
A rule should not contain only the variable but all kind of operator functions like adding.
type i8 = rule {
var => function(name:id, ?value:number) {
...
}
+= => function(assignee:id, left:expr, right:expr) {
rule.out( "%tmp = add i8 %" eval(left) "," eval(right) )
rule.out( "store i8 %tmp, ptr %" eval(assignee) ", align 1" )
}
}
And therefore:
var x : i8 = y + 12;
will result in LLVM IR:
%x = alloca i8, align 1
%tmp = add i8 %y, 12;
store i8 %tmp, ptr %x, align 1
This is just a demonstration, will change in future.
💖 💪 🙅 🚩
Nevergarden
Posted on January 22, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.