feat: add first hard example
Hard example tests all actual language features. For now its: - Classes - Methods - Recursive calls - Object move and borrowing
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
class Node
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
Node next;
|
||||||
|
|
||||||
|
fn append(int val) Node
|
||||||
|
{
|
||||||
|
if (this.next == null)
|
||||||
|
{
|
||||||
|
Node tail = new Node;
|
||||||
|
tail.value = val;
|
||||||
|
tail.next = null;
|
||||||
|
this.next = tail;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.next = this.next.append(val);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sum() int
|
||||||
|
{
|
||||||
|
if (this.next == null)
|
||||||
|
return this.value;
|
||||||
|
|
||||||
|
return this.value + this.next.sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn printList()
|
||||||
|
{
|
||||||
|
printline(this.value);
|
||||||
|
if (this.next != null)
|
||||||
|
this.next.printList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn createList(int count) Node
|
||||||
|
{
|
||||||
|
if (count <= 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
Node head = new Node;
|
||||||
|
head.value = count;
|
||||||
|
head.next = createList(count - 1);
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
printline("===== Test 1: factory & recursive escaping =====");
|
||||||
|
Node list = createList(3);
|
||||||
|
printline("Elements:");
|
||||||
|
list.printList();
|
||||||
|
|
||||||
|
printline("===== Test 2: mutation & methods =====");
|
||||||
|
int total = list.sum();
|
||||||
|
printline("Sum of elements:");
|
||||||
|
printline(total);
|
||||||
|
|
||||||
|
if (total == 16)
|
||||||
|
printline("Tests passed!");
|
||||||
|
else printline("Tests failed");
|
||||||
|
|
||||||
Reference in New Issue
Block a user