reorganized build-ins and implement a circular queue as an example

This commit is contained in:
cozis
2022-08-18 19:01:26 +02:00
parent b5b3a3e315
commit 782985e8b7
36 changed files with 486 additions and 259 deletions
+1 -1
View File
@@ -9,4 +9,4 @@ add10 = makeAdder(10);
add20 = makeAdder(20);
assert(add10(1) == 11);
assert(add20(1) == 21);
assert(add20(1) == 21);
+48
View File
@@ -0,0 +1,48 @@
fun dummy() {}
Func = type(dummy);
NFunc = type(print); # The function type is not a build-in at the moment,
# but we can define it manually.
fun copyList(list: List) {
list2 = [];
i = 0;
while i < count(list): {
list2[i] = list[i];
i = i+1;
}
return list2;
}
fun numericLess(a: int | float, b: int | float)
return a < b;
fun bubbleSort(list: List, less: NFunc | Func = numericLess) {
list2 = copyList(list);
do {
swapped = false;
i = 0;
while i < count(list2)-1 and not swapped: {
if less(list2[i+1], list2[i]): {
swapped = true;
tmp = list2[i+1];
list2[i+1] = list2[i];
list2[i] = tmp;
}
i = i + 1;
}
} while swapped;
return list2;
}
unordered_list = [3, 2, 1, 6, -2];
ordered_list = bubbleSort(unordered_list);
print(ordered_list, '\n'); # [-2, 1, 2, 3, 6]
-10
View File
@@ -1,10 +0,0 @@
fun x(a, b, c)
print('a = ', a, '\nb = ', b, '\nc = ', c, '\n\n');
x(1, 2, 3, 4, 5);
x(1, 2, 3, 4);
x(1, 2, 3);
x(1, 2);
x(1);
x();
-40
View File
@@ -1,40 +0,0 @@
fun copy(L) {
L2 = [];
i = 0;
while i < count(L): {
L2[i] = L[i];
i = i + 1;
}
return L2;
}
fun bubbleSort(L, less) {
if less == none:
fun less(a, b) return a < b;
L = copy(L);
do {
swapped = false;
i = 0;
while i < count(L)-1 and not swapped: {
if less(L[i+1], L[i]): {
swapped = true;
tmp = L[i+1];
L[i+1] = L[i];
L[i] = tmp;
}
i = i + 1;
}
} while swapped;
return L;
}
print(bubbleSort([3, 2, 1, 6, 0-2]));
-21
View File
@@ -1,21 +0,0 @@
b = newBuffer(16);
b[2] = 10;
print(b, '\n');
fun toArray(buffer) {
i = 0;
r = [];
while i < count(buffer): {
r[i] = buffer[i];
i = i+1;
}
return r;
}
print(toArray(b), '\n');
-6
View File
@@ -1,6 +0,0 @@
a = [];
i = 0;
while(i<100000000):{
a[i]= "ciao mondo mmododooddododododododododododdodododododd";
i = i+1;
}
-1
View File
@@ -1 +0,0 @@
a = b = c;
+142
View File
@@ -0,0 +1,142 @@
# This script implements a circular queue and
# it's relative tests!
fun newCircularQueue(T: Type = int, max: int = 3) {
if T == None:
error("Invalid type");
if max <= 0:
error("Maximum queue size must be positive");
list = [];
i = 0;
while i < max: {
list[i] = none;
i = i+1;
}
return {
max: max,
head: 0,
tail: 0,
list: list,
type: T
};
}
fun getSize(queue: Map) {
size = none;
if queue.tail == queue.head: {
if queue.list[queue.tail] == none: {
size = 0;
} else {
size = queue.max;
}
} else if queue.tail < queue.head: {
size = queue.head
- queue.tail;
} else {
size = queue.head
+ queue.max
- queue.tail;
}
return size;
}
fun append(queue: Map, value) {
if type(value) != queue.type:
error("Invalid type");
size = getSize(queue);
queue.list[queue.head] = value;
if size == queue.max: {
queue.tail = queue.tail+1;
if queue.tail == queue.max:
queue.tail = 0;
}
queue.head = queue.head+1;
if queue.head == queue.max:
queue.head = 0;
}
fun pop(queue: Map) {
if getSize(queue) == 0:
return none;
value = queue.list[queue.tail];
queue.list[queue.tail] = none;
queue.tail = queue.tail+1;
if queue.tail == queue.max:
queue.tail = 0;
return value;
}
{
utils = import("../utils.noja");
queue = newCircularQueue(int, 3);
assert(getSize(queue) == 0);
assert(queue.head == 0);
assert(queue.tail == 0);
append(queue, 1);
assert(queue.head == 1);
assert(queue.tail == 0);
assert(getSize(queue) == 1);
assert(utils.compareAny(queue.list, [1, none, none]));
append(queue, 2);
assert(queue.head == 2);
assert(queue.tail == 0);
assert(getSize(queue) == 2);
assert(utils.compareAny(queue.list, [1, 2, none]));
append(queue, 3);
assert(queue.head == 0);
assert(queue.tail == 0);
assert(getSize(queue) == 3);
assert(utils.compareAny(queue.list, [1, 2, 3]));
append(queue, 4);
assert(queue.head == 1);
assert(queue.tail == 1);
assert(getSize(queue) == 3);
assert(utils.compareAny(queue.list, [4, 2, 3]));
v = pop(queue);
assert(v == 2);
assert(queue.head == 1);
assert(queue.tail == 2);
assert(getSize(queue) == 2);
assert(utils.compareAny(queue.list, [4, none, 3]));
v = pop(queue);
assert(v == 3);
assert(queue.head == 1);
assert(queue.tail == 0);
assert(getSize(queue) == 1);
assert(utils.compareAny(queue.list, [4, none, none]));
v = pop(queue);
assert(v == 4);
assert(queue.head == 1);
assert(queue.tail == 1);
assert(getSize(queue) == 0);
assert(utils.compareAny(queue.list, [none, none, none]));
v = pop(queue);
assert(v == none);
assert(queue.head == 1);
assert(queue.tail == 1);
assert(getSize(queue) == 0);
assert(utils.compareAny(queue.list, [none, none, none]));
}
@@ -1,10 +1,15 @@
# Implementation of a stack using a linked list.
fun Stack_New(T)
fun Stack_New(T) {
if T == None:
return none, "The type can't be None";
return { count: 0, head: none, type: T };
}
fun Stack_Push(stack: Map, value) {
assert(stack.type != None);
if type(value) != stack.type:
error("Invalid type");
@@ -19,7 +24,7 @@ fun Stack_Pop(stack: Map) {
if stack.head == none:
return none;
value = stack.head.item;
stack.head = stack.head.prev;
stack.count = stack.count - 1;
-14
View File
@@ -1,14 +0,0 @@
fun fail_at_depth(depth) {
fun fail(current_depth) {
assert(current_depth < depth);
fail(current_depth + 1);
}
fail(0);
}
fail_at_depth(9);
+2 -2
View File
@@ -1,2 +1,2 @@
a = 1;
myName = "Francesco";
myAge = 25;
+9 -3
View File
@@ -1,4 +1,10 @@
vars, err = import("examples/imported.noja");
print("vars=[", vars, "]\n");
print("err=[", err, "]\n");
vars, err = import("imported.noja");
if vars == none: {
print("L'import è fallito!! (", err, ")\n");
return none;
}
me = {name: vars.myName, age: vars.myAge};
print(me, "\n");
+2 -2
View File
@@ -149,7 +149,7 @@ fun parseString(context: Map) {
while context.cur < count(context.str)
and context.str[context.cur] != '"': {
# TODO: Support special characters such as \n, \t ecc
buffer = strcat(buffer, context.str[context.cur]);
buffer = string.cat(buffer, context.str[context.cur]);
context.cur = context.cur + 1;
}
@@ -228,7 +228,7 @@ fun parseValue(context: Map) {
val, err = parseFalse(context);
else {
val = none;
err = strcat("Unexepected character \"", c, "\" where a value was expected");
err = string.cat("Unexepected character \"", c, "\" where a value was expected");
}
return val, err;
}
+1 -1
View File
@@ -1,5 +1,5 @@
json = import("json.noja");
utils = import("utils.noja");
utils = import("../utils.noja");
assert(json.parse("null") == none);
assert(json.parse("true") == true);
-5
View File
@@ -1,5 +0,0 @@
l = [1, 2, 3, 4];
print('The list contains ', count(l), ' items.\n');
print('The list is: ', l, '.\n');
+3 -1
View File
@@ -1,4 +1,6 @@
# This script reads the contents of the specified
# folder, and then prints all of the containd files.
dirname = '.';
dir = files.openDir(dirname);
-19
View File
@@ -1,19 +0,0 @@
# This script creates a lot of objects, so it can be used to stress the garbage collector.
print('Start\n');
i = 0;
n = 10000000;
while i < n: {
1 + 1 * 1;
i = i + 1;
fun printPercent()
print(100.0 * i / n, '%\n');
printPercent();
}
print('End\n');
+13 -6
View File
@@ -1,8 +1,15 @@
# This script reads the contents of a file.
name = 'examples/algorithms/bubble_sort.noja';
buff = buffer.new(1024);
handle, err = files.openFile(name, files.READ);
if handle == none:
error(err);
n = files.read(handle, buff);
resl = buffer.toString(buffer.sliceUp(buff, 0, n));
buff = newBuffer(1024);
name = 'examples/bubble_sort.noja';
hdle = files.openFile(name, files.READ);
n = files.read(hdle, buff);
resl = bufferToString(sliceBuffer(buff, 0, n));
print('Read ', n, ' bytes.\n');
print(resl);
print(resl);
-6
View File
@@ -1,6 +0,0 @@
A = 'Hello';
B = ', ';
C = 'world';
D = strcat(A, B, C);
print(D, '\n');