mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Add some ir examples
This commit is contained in:
16
concept-code/eg1.ts
Normal file
16
concept-code/eg1.ts
Normal 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
12
concept-code/eg1.vsir
Normal 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
9
concept-code/eg2.ts
Normal 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
8
concept-code/eg2.vsir
Normal 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
14
concept-code/eg3.ts
Normal 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
20
concept-code/eg3.vsir
Normal 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
17
concept-code/eg4.ts
Normal 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
22
concept-code/eg4.vsir
Normal 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
|
||||
}
|
||||
1
concept-code/valueScript.d.ts
vendored
1
concept-code/valueScript.d.ts
vendored
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user