mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-01-12 15:08:07 -05:00
142 lines
2.0 KiB
Plaintext
142 lines
2.0 KiB
Plaintext
func eg1() {
|
|
method add(y) {
|
|
this += y;
|
|
}
|
|
|
|
t := 100;
|
|
|
|
t::add(7);
|
|
|
|
// t: 107
|
|
|
|
foo := { bar: 100 };
|
|
|
|
foo.bar::add(7);
|
|
|
|
// foo: { bar: 107 }
|
|
|
|
return 'done';
|
|
};
|
|
|
|
func eg2() {
|
|
interface Foo {
|
|
inc() {
|
|
this.value++;
|
|
}
|
|
};
|
|
|
|
f := Foo({ value: 5 });
|
|
log.info f.value; // 5
|
|
|
|
f:inc();
|
|
log.info f.value; // 6
|
|
|
|
interface Bar {
|
|
inc() {
|
|
this.value = 'lulz';
|
|
}
|
|
}
|
|
|
|
f = Bar(f);
|
|
|
|
f:inc();
|
|
log.info f.value; // 'lulz'
|
|
|
|
return 'done';
|
|
};
|
|
|
|
func eg3() {
|
|
method postInc() {
|
|
old := this;
|
|
this++;
|
|
return old; // Error: method cannot return because it mutates target
|
|
}
|
|
|
|
y := 3;
|
|
|
|
log.info y::postInc(); // 3?
|
|
log.info y; // 4?
|
|
|
|
return 'done';
|
|
};
|
|
|
|
func eg4() {
|
|
interface Fun {
|
|
double() {
|
|
this *= 2;
|
|
}
|
|
}
|
|
|
|
x := Fun(3);
|
|
x:double();
|
|
log.info x; // Fun(6)
|
|
log.info x:this; // 6
|
|
|
|
return 'done';
|
|
}
|
|
|
|
func eg5() {
|
|
interface BinaryTree {
|
|
static empty() => BinaryTree(null);
|
|
|
|
static init(x) => BinaryTree({
|
|
value: x,
|
|
left: BinaryTree:empty(),
|
|
right: BinaryTree:empty(),
|
|
});
|
|
|
|
insert(x) {
|
|
if (this == null) {
|
|
this = BinaryTree:init(x);
|
|
return;
|
|
}
|
|
|
|
if (x < this.value) {
|
|
this.left:insert(x);
|
|
} else {
|
|
this.right:insert(x);
|
|
}
|
|
}
|
|
|
|
contains(x) {
|
|
if (this == null) {
|
|
return false;
|
|
}
|
|
|
|
if (this.value == x) {
|
|
return true;
|
|
}
|
|
|
|
return switch (this.value < x) {
|
|
true => this.left:contains(x);
|
|
false => this.right:contains(x);
|
|
};
|
|
}
|
|
}
|
|
|
|
tree := BinaryTree:empty();
|
|
tree:insert(1);
|
|
tree:insert(2);
|
|
tree:insert(3);
|
|
|
|
log.info tree; /*
|
|
BinaryTree({
|
|
value: 1,
|
|
left: BinaryTree(null),
|
|
right: BinaryTree({
|
|
value: 2
|
|
left: BinaryTree(null),
|
|
right: BinaryTree({
|
|
value: 3,
|
|
left: BinaryTree(null),
|
|
right: BinaryTree(null),
|
|
}),
|
|
}),
|
|
})
|
|
*/
|
|
|
|
return 'done';
|
|
}
|
|
|
|
return 'done';
|