plsm-ts/examples/test.plsm
2025-03-04 20:54:46 +01:00

71 lines
992 B
Plaintext

module test;
import stdlib/io;
fun main -> {
io.println(foo() + 10);
}
trait Expr {
visit<T>(visitor: Visitor<T>): T;
}
trait ExprVisitor<T> {
visitString(string: String): T;
}
trait List<T : any> {
size: int;
get(index: int): T;
set(index: int, value: T): void;
}
struct LinkedList<T : any> {
head: T?;
tail: LinkedList<T>?;
init -> {
self.head = null;
self.tail = null;
}
}
impl List<T> for LinkedList<T> {
size -> {
if self.head == null {
return 0;
}
if self.tail == null {
return 1;
}
return 1 + self.tail.size();
}
get(index) -> {
if index == 0 {
return self.head;
}
return self.tail.get(index - 1);
}
set(index, value) -> {
if index == 0 {
self.head = value;
return;
}
if self.tail == null {
raise StringError(
"LinkedList<T>.set(...): index out of bounds",
);
}
self.tail.set(index - 1, value,);
}
}
fun foo -> "Hello World!";