DEV Community

Nevergarden
Nevergarden

Posted on

Type Rules

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" )
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

will generate:

%x = alloca i8, align 1
%y = alloca i8, align 1
store i8 12, ptr %y, align 1
Enter fullscreen mode Exit fullscreen mode

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" ) 
  }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This is just a demonstration, will change in future.

Top comments (0)