reorganizing code

This commit is contained in:
cozis
2022-03-12 23:17:44 +01:00
parent de11b968fb
commit 2dcfbd194e
18 changed files with 141 additions and 335 deletions
+11 -10
View File
@@ -5,16 +5,17 @@ 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;
fun toArray(buffer) {
i = 0;
r = [];
while i < count(buffer): {
r[i] = buffer[i];
i = i+1;
}
return r;
}
print(toArray(b), '\n');
-36
View File
@@ -1,36 +0,0 @@
port = 8080;
addr = "127.0.0.1";
fd = net.socket(net.AF_INET, net.SOCK_STREAM, 0);
if fd < 0:
{
print('Failed to create socket.\n');
return none;
}
sin_addr = {};
if net.inet_aton(addr, sin_addr) == 0:
{
print('Bad address string format.\n');
return none;
}
if net.connect(fd, { sin_family: net.AF_INET, sin_port: net.htons(port), sin_addr: sin_addr }) != 0:
{
print('Failed to connect.\n');
return none;
}
print('Connected!\n');
buffer = newBuffer(16);
buffer[0] = 9;
buffer[1] = 7;
buffer[2] = 255;
n = net.send(fd, buffer, 0);
print('Sent ', n, ' bytes: ', buffer, '\n');
+1
View File
@@ -0,0 +1 @@
print('Hello, world!\n');
+8 -8
View File
@@ -5,15 +5,15 @@ print('Start\n');
i = 0;
n = 1000;
while i < n:
{
1 + 1 * 1;
i = i + 1;
while i < n: {
fun printPercent()
print(100.0 * i / n, '%\n');
1 + 1 * 1;
i = i + 1;
fun printPercent()
print(100.0 * i / n, '%\n');
printPercent();
}
printPercent();
}
print('End\n');
+5 -5
View File
@@ -2,8 +2,8 @@
stack = {count: 0, head: none};
fun push(stack, value)
{
fun push(stack, value) {
node = {prev: none, item: value};
node.prev = stack.head;
@@ -11,10 +11,10 @@ fun push(stack, value)
stack.count = stack.count + 1;
}
fun pop(stack)
{
fun pop(stack) {
if stack.head == none:
return none; # return it; ?
return none;
value = stack.head.item;
stack.head = stack.head.prev;
+1 -15
View File
@@ -3,18 +3,4 @@ B = ', ';
C = 'world';
D = strcat(A, B, C);
fun append_esclamation_mark(A, n) {
if n == none:
n = 1;
i = 0;
while i < n: {
A = strcat(A, '!');
i = i + 1;
}
return A;
}
print(append_esclamation_mark(D, 3), '\n');
print(D, '\n');
-108
View File
@@ -1,108 +0,0 @@
# Implementation of a basic TCP server that listen for connections and prints received bytes.
fun startServer(callbacks, port, backlog)
{
# Handle default arguments.
{
if callbacks == none:
callbacks = {};
if port == none:
port = 8000;
if backlog == none:
backlog = 5;
}
fd = net.socket(net.AF_INET, net.SOCK_STREAM, 0);
if fd < 0:
{
print('Failed to create socket.\n');
return none;
}
if net.bind(fd, {
sin_family: net.AF_INET,
sin_port: net.htons(port),
sin_addr: {
s_addr: net.htonl(net.INADDR_ANY)
}
}) != 0:
{
print('Failed to bind.\n');
return none;
}
if net.listen(fd, backlog) != 0:
{
print('Failed to listen.\n');
return none;
}
{
on_datain = callbacks.on_datain;
on_connect = callbacks.on_connect;
on_disconnect = callbacks.on_disconnect;
if on_datain == none:
fun on_datain()
none;
if on_connect == none:
fun on_connect()
none;
if on_disconnect == none:
fun on_disconnect()
none;
}
buffer = newBuffer(1024);
while true:
{
client_addr = {};
client_fd = net.accept(fd, client_addr);
if client_fd < 0:
{
print('Warning! Failed to accept!\n');
continue;
}
addr_str = net.inet_ntoa(client_addr.sin_addr);
on_connect(addr_str);
do
{
n = net.recv(client_fd, buffer, 0);
if n < 0:
print('Warning! Failed to read from socket!\n');
else if n == 0:
on_disconnect(addr_str);
else
on_datain(addr_str, sliceBuffer(buffer, 0, n));
}
while n > 0;
}
}
fun on_connect(addr)
{
print(addr, ' connected.\n');
}
fun on_datain(addr, data)
{
print('Received ', count(data), ' bytes from ', addr, ': ', data, '\n');
}
fun on_disconnect(addr)
{
print(addr, ' disconnected.\n');
}
startServer({on_connect: on_connect, on_datain: on_datain, on_disconnect: on_disconnect}, 8080);