Add some ir examples

This commit is contained in:
Andrew Morris
2022-04-24 16:14:49 +10:00
parent 225175dc57
commit 2eedfa745a
9 changed files with 119 additions and 0 deletions

16
concept-code/eg1.ts Normal file
View File

@@ -0,0 +1,16 @@
import { inc } from 'value-script';
export default function main() {
function f1(a: number, b: number, c: number) {
return a + b * c;
}
function f2() {
inc.call(this);
}
let x = f1(1, 2, 3);
f2.call(x);
return x;
}

12
concept-code/eg1.vsir Normal file
View File

@@ -0,0 +1,12 @@
function @main() {
call @f1 [1, 2, 3] %ignore %x
call @f2 [] %x %ignore
mov %x %return
}
function @f1(%a, %b, %c) {
mul %b %c %_tmp0
add %a %_tmp0 %return
}
function @f2() {
inc %this
}

9
concept-code/eg2.ts Normal file
View File

@@ -0,0 +1,9 @@
export default function main() {
const x = 37;
function foo() {
return x + 5;
}
return foo();
}

8
concept-code/eg2.vsir Normal file
View File

@@ -0,0 +1,8 @@
function @main() {
mov 37 %x
bind @foo [%x] %foo
call %foo [] %ignore %return
}
function @foo(%x) {
add %x 5 %return
}

14
concept-code/eg3.ts Normal file
View File

@@ -0,0 +1,14 @@
export default function main() {
class Counter {
value = 0;
inc() { this.value++; }
}
let counter = new Counter();
counter.inc();
counter.inc();
const counter2 = counter;
counter.inc();
return [counter.value, counter2.value];
}

20
concept-code/eg3.vsir Normal file
View File

@@ -0,0 +1,20 @@
function @main() {
new @Counter [] %counter
subcall %counter "inc" [] %ignore
subcall %counter "inc" [] %ignore
mov %counter %counter2
subcall %counter "inc" [] %ignore
sub %counter "value" %_tmp0
sub %counter2 "value" %_tmp1
mov [%_tmp0 %_tmp1] %return
}
function @Counter() {
submov "value" 0 %this
} prototype {
"inc": @Counter_inc,
}
function @Counter_inc() {
sub %this "value" %_tmp0
inc %_tmp0
submov "value" %_tmp0 %this
}

17
concept-code/eg4.ts Normal file
View File

@@ -0,0 +1,17 @@
export default function main() {
function Counter() {
this.value = 0;
}
Counter.prototype.inc = function() {
this.value++;
}
let counter = new Counter();
counter.inc();
counter.inc();
const counter2 = counter;
counter.inc();
return [counter.value, counter2.value];
}

22
concept-code/eg4.vsir Normal file
View File

@@ -0,0 +1,22 @@
function @main() {
mov @Counter %Counter
sub %Counter "prototype" %_tmp0
submov "inc" @_anon0 %_tmp0
submov "prototype" %_tmp0 %Counter
new %Counter [] %counter
subcall %counter "inc" [] %ignore
subcall %counter "inc" [] %ignore
mov %counter %counter2
subcall %counter "inc" [] %ignore
sub %counter "value" %_tmp0
sub %counter2 "value" %_tmp1
mov [%_tmp0 %_tmp1] %return
}
function @Counter() {
submov "value" 0 %this
}
function @_anon0() {
sub %this "value" %_tmp0
inc %_tmp0
submov "value" %_tmp0 %this
}

View File

@@ -1,4 +1,5 @@
declare module 'value-script' {
export function staticAssert(value: boolean): asserts value;
export function lessThan(left: unknown, right: unknown): boolean;
export function inc(this: number): void;
}