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
+2 -3
View File
@@ -49,14 +49,13 @@ gcc -c src/runtime/runtime_error.c -o temp/runtime/runtime_error.o $FLAGS
gcc -c src/runtime/runtime.c -o temp/runtime/runtime.o $FLAGS gcc -c src/runtime/runtime.c -o temp/runtime/runtime.o $FLAGS
gcc -c src/runtime/o_nfunc.c -o temp/runtime/o_nfunc.o $FLAGS gcc -c src/runtime/o_nfunc.c -o temp/runtime/o_nfunc.o $FLAGS
gcc -c src/runtime/o_func.c -o temp/runtime/o_func.o $FLAGS gcc -c src/runtime/o_func.c -o temp/runtime/o_func.o $FLAGS
gcc -c src/runtime/o_staticmap.c -o temp/runtime/o_staticmap.o $FLAGS
mkdir temp/builtins mkdir temp/builtins
gcc -c src/builtins/basic.c -o temp/builtins/basic.o $FLAGS gcc -c src/builtins/basic.c -o temp/builtins/basic.o $FLAGS
gcc -c src/builtins/file.c -o temp/builtins/file.o $FLAGS gcc -c src/builtins/file.c -o temp/builtins/file.o $FLAGS
gcc -c src/builtins/math.c -o temp/builtins/math.o $FLAGS gcc -c src/builtins/math.c -o temp/builtins/math.o $FLAGS
gcc -c src/o_staticmap.c -o temp/o_staticmap.o $FLAGS
rm -rf build rm -rf build
mkdir build mkdir build
@@ -82,7 +81,6 @@ ar rcs build/libnoja-runtime.a \
build/libnoja-objects.a build/libnoja-objects.a
gcc src/main.c \ gcc src/main.c \
temp/o_staticmap.o \
temp/utils/hash.o \ temp/utils/hash.o \
temp/utils/stack.o \ temp/utils/stack.o \
temp/utils/source.o \ temp/utils/source.o \
@@ -101,6 +99,7 @@ gcc src/main.c \
temp/runtime/runtime_error.o \ temp/runtime/runtime_error.o \
temp/runtime/o_nfunc.o \ temp/runtime/o_nfunc.o \
temp/runtime/o_func.o \ temp/runtime/o_func.o \
temp/runtime/o_staticmap.o \
temp/builtins/basic.o \ temp/builtins/basic.o \
temp/builtins/file.o \ temp/builtins/file.o \
temp/builtins/math.o \ temp/builtins/math.o \
+5 -4
View File
@@ -5,15 +5,16 @@ b[2] = 10;
print(b, '\n'); print(b, '\n');
fun toArray(buffer) fun toArray(buffer) {
{
i = 0; i = 0;
r = []; r = [];
while i < count(buffer):
{ while i < count(buffer): {
r[i] = buffer[i]; r[i] = buffer[i];
i = i+1; i = i+1;
} }
return r; return r;
} }
-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');
+2 -2
View File
@@ -5,8 +5,8 @@ print('Start\n');
i = 0; i = 0;
n = 1000; n = 1000;
while i < n: while i < n: {
{
1 + 1 * 1; 1 + 1 * 1;
i = i + 1; i = i + 1;
+5 -5
View File
@@ -2,8 +2,8 @@
stack = {count: 0, head: none}; stack = {count: 0, head: none};
fun push(stack, value) fun push(stack, value) {
{
node = {prev: none, item: value}; node = {prev: none, item: value};
node.prev = stack.head; node.prev = stack.head;
@@ -11,10 +11,10 @@ fun push(stack, value)
stack.count = stack.count + 1; stack.count = stack.count + 1;
} }
fun pop(stack) fun pop(stack) {
{
if stack.head == none: if stack.head == none:
return none; # return it; ? return none;
value = stack.head.item; value = stack.head.item;
stack.head = stack.head.prev; stack.head = stack.head.prev;
+1 -15
View File
@@ -3,18 +3,4 @@ B = ', ';
C = 'world'; C = 'world';
D = strcat(A, B, C); D = strcat(A, B, C);
fun append_esclamation_mark(A, n) { print(D, '\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');
-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);
+1 -1
View File
@@ -1,2 +1,2 @@
#include "../o_staticmap.h" #include "../runtime/o_staticmap.h"
extern const StaticMapSlot bins_basic[]; extern const StaticMapSlot bins_basic[];
+1 -1
View File
@@ -1,2 +1,2 @@
#include "../o_staticmap.h" #include "../runtime/o_staticmap.h"
extern const StaticMapSlot bins_file[]; extern const StaticMapSlot bins_file[];
+1 -1
View File
@@ -1,2 +1,2 @@
#include "../o_staticmap.h" #include "../runtime/o_staticmap.h"
extern const StaticMapSlot bins_math[]; extern const StaticMapSlot bins_math[];
+88 -93
View File
@@ -2,11 +2,11 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "o_staticmap.h"
#include "compiler/parse.h" #include "compiler/parse.h"
#include "compiler/compile.h" #include "compiler/compile.h"
#include "runtime/runtime.h" #include "runtime/runtime.h"
#include "runtime/runtime_error.h" #include "runtime/runtime_error.h"
#include "runtime/o_staticmap.h"
#include "builtins/basic.h" #include "builtins/basic.h"
static const char usage[] = static const char usage[] =
@@ -16,11 +16,6 @@ static const char usage[] =
" $ noja dis file.noja\n" " $ noja dis file.noja\n"
" $ noja dis inline \"print('some noja code');\"\n"; " $ noja dis inline \"print('some noja code');\"\n";
static _Bool interpret_file(const char *file);
static _Bool interpret_code(const char *code);
static _Bool disassemble_file(const char *file);
static _Bool disassemble_code(const char *code);
static void print_error(const char *type, Error *error) static void print_error(const char *type, Error *error)
{ {
if(type == NULL) if(type == NULL)
@@ -47,93 +42,6 @@ static void print_error(const char *type, Error *error)
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
int main(int argc, char **argv)
{
assert(argc > 0);
if(argc == 1)
{
// $ noja
fprintf(stderr, "Error: Incorrect usage.\n\n");
fprintf(stderr, usage);
return -1;
}
if(!strcmp(argv[1], "run"))
{
Error error;
Error_Init(&error);
if(argc == 2)
{
Error_Report(&error, 0, "Missing source file");
print_error(NULL, &error);
Error_Free(&error);
return -1;
}
_Bool r;
if(!strcmp(argv[2], "inline"))
{
if(argc == 3)
{
Error_Report(&error, 0, "Missing source string");
print_error(NULL, &error);
Error_Free(&error);
return -1;
}
r = interpret_code(argv[3]);
}
else
r = interpret_file(argv[2]);
return r ? 0 : -1;
}
if(!strcmp(argv[1], "dis"))
{
Error error;
Error_Init(&error);
if(argc == 2)
{
Error_Report(&error, 0, "Missing source file");
print_error(NULL, &error);
Error_Free(&error);
return -1;
}
_Bool r;
if(!strcmp(argv[2], "inline"))
{
if(argc == 3)
{
Error_Report(&error, 0, "Missing source string");
print_error(NULL, &error);
Error_Free(&error);
return -1;
}
r = disassemble_code(argv[3]);
}
else
r = disassemble_file(argv[2]);
return r ? 0 : -1;
}
if(!strcmp(argv[1], "help"))
{
fprintf(stdout, usage);
return 0;
}
fprintf(stderr, "Error: Incorrect usage.\n\n");
fprintf(stderr, usage);
return -1;
}
static _Bool interpret(Source *src) static _Bool interpret(Source *src)
{ {
// Compile the code. This section transforms // Compile the code. This section transforms
@@ -375,3 +283,90 @@ static _Bool disassemble_code(const char *code)
Source_Free(src); Source_Free(src);
return r; return r;
} }
int main(int argc, char **argv)
{
assert(argc > 0);
if(argc == 1)
{
// $ noja
fprintf(stderr, "Error: Incorrect usage.\n\n");
fprintf(stderr, usage);
return -1;
}
if(!strcmp(argv[1], "run"))
{
Error error;
Error_Init(&error);
if(argc == 2)
{
Error_Report(&error, 0, "Missing source file");
print_error(NULL, &error);
Error_Free(&error);
return -1;
}
_Bool r;
if(!strcmp(argv[2], "inline"))
{
if(argc == 3)
{
Error_Report(&error, 0, "Missing source string");
print_error(NULL, &error);
Error_Free(&error);
return -1;
}
r = interpret_code(argv[3]);
}
else
r = interpret_file(argv[2]);
return r ? 0 : -1;
}
if(!strcmp(argv[1], "dis"))
{
Error error;
Error_Init(&error);
if(argc == 2)
{
Error_Report(&error, 0, "Missing source file");
print_error(NULL, &error);
Error_Free(&error);
return -1;
}
_Bool r;
if(!strcmp(argv[2], "inline"))
{
if(argc == 3)
{
Error_Report(&error, 0, "Missing source string");
print_error(NULL, &error);
Error_Free(&error);
return -1;
}
r = disassemble_code(argv[3]);
}
else
r = disassemble_file(argv[2]);
return r ? 0 : -1;
}
if(!strcmp(argv[1], "help"))
{
fprintf(stdout, usage);
return 0;
}
fprintf(stderr, "Error: Incorrect usage.\n\n");
fprintf(stderr, usage);
return -1;
}
+5
View File
@@ -0,0 +1,5 @@
#ifndef NOJAFUNC_H
#define NOJAFUNC_H
#include "runtime.h"
Object *Object_FromNojaFunction(Runtime *runtime, Executable *exe, int index, int argc, Object *closure, Heap *heap, Error *error);
#endif
+5
View File
@@ -0,0 +1,5 @@
#ifndef NOJAFUNC_H
#define NOJAFUNC_H
#include "runtime.h"
Object *Object_FromNativeFunction(Runtime *runtime, Object *(*callback)(Runtime*, Object**, unsigned int, Error*), int argc, Heap *heap, Error *error);
#endif
@@ -27,9 +27,10 @@
*/ */
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "o_nfunc.h"
#include "o_staticmap.h" #include "o_staticmap.h"
#include "utils/defs.h" #include "../utils/defs.h"
#include "objects/objects.h" #include "../objects/objects.h"
typedef struct { typedef struct {
Object base; Object base;
@@ -1,8 +1,8 @@
#ifndef STATICMAP_H #ifndef STATICMAP_H
#define STATICMAP_H #define STATICMAP_H
#include "objects/objects.h" #include "../objects/objects.h"
#include "runtime/runtime.h" #include "runtime.h"
typedef enum { typedef enum {
SM_END, SM_END,
+1 -33
View File
@@ -1,6 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "../utils/defs.h" #include "../utils/defs.h"
#include "../utils/stack.h" #include "../utils/stack.h"
#include "o_func.h"
#include "runtime.h" #include "runtime.h"
#define MAX_FRAME_STACK 16 #define MAX_FRAME_STACK 16
@@ -26,39 +27,6 @@ struct xRuntime {
Heap *heap; Heap *heap;
}; };
CallStackScanner *CallStackScanner_New(Runtime *runtime)
{
return runtime->frame;
}
_Bool CallStackScanner_Next(CallStackScanner **scanner, Object **locals, Object **closure, Executable **exe, int *index)
{
assert(scanner != NULL);
if(*scanner == NULL)
return 0;
{
Frame *frame = *scanner;
if(exe)
*exe = frame->exe;
if(index)
*index = frame->index;
if(locals)
*locals = frame->locals;
if(closure)
*closure = frame->closure;
}
(*scanner) = ((Frame*) (*scanner))->prev;
return 1;
}
Stack *Runtime_GetStack(Runtime *runtime) Stack *Runtime_GetStack(Runtime *runtime)
{ {
return Stack_Copy(runtime->stack, 1); return Stack_Copy(runtime->stack, 1);
+1 -12
View File
@@ -5,9 +5,8 @@
#include "../utils/stack.h" #include "../utils/stack.h"
#include "../objects/objects.h" #include "../objects/objects.h"
#include "../common/executable.h" #include "../common/executable.h"
typedef struct xRuntime Runtime; typedef struct xRuntime Runtime;
typedef void CallStackScanner; typedef struct xSnapshot Snapshot;
Runtime* Runtime_New(int stack_size, int heap_size, void *callback_userp, _Bool (*callback_addr)(Runtime*, void*)); Runtime* Runtime_New(int stack_size, int heap_size, void *callback_userp, _Bool (*callback_addr)(Runtime*, void*));
Runtime* Runtime_New2(int stack_size, Heap *heap, _Bool free_heap, void *callback_userp, _Bool (*callback_addr)(Runtime*, void*)); Runtime* Runtime_New2(int stack_size, Heap *heap, _Bool free_heap, void *callback_userp, _Bool (*callback_addr)(Runtime*, void*));
void Runtime_Free(Runtime *runtime); void Runtime_Free(Runtime *runtime);
@@ -19,18 +18,8 @@ Object* Runtime_GetBuiltins(Runtime *runtime);
void Runtime_SetBuiltins(Runtime *runtime, Object *builtins); void Runtime_SetBuiltins(Runtime *runtime, Object *builtins);
int Runtime_GetCurrentIndex(Runtime *runtime); int Runtime_GetCurrentIndex(Runtime *runtime);
Executable *Runtime_GetCurrentExecutable(Runtime *runtime); Executable *Runtime_GetCurrentExecutable(Runtime *runtime);
typedef struct xSnapshot Snapshot;
Snapshot *Snapshot_New(Runtime *runtime); Snapshot *Snapshot_New(Runtime *runtime);
void Snapshot_Free(Snapshot *snapshot); void Snapshot_Free(Snapshot *snapshot);
void Snapshot_Print(Snapshot *snapshot, FILE *fp); void Snapshot_Print(Snapshot *snapshot, FILE *fp);
CallStackScanner *CallStackScanner_New(Runtime *runtime);
_Bool CallStackScanner_Next(CallStackScanner **scanner, Object **locals, Object **closure, Executable **exe, int *index);
Object *run(Runtime *runtime, Error *error, Executable *exe, int index, Object *closure, Object **argv, int argc); Object *run(Runtime *runtime, Error *error, Executable *exe, int index, Object *closure, Object **argv, int argc);
Object* Object_FromNativeFunction(Runtime *runtime, Object *(*callback)(Runtime*, Object**, unsigned int, Error*), int argc, Heap *heap, Error *error);
Object *Object_FromNojaFunction(Runtime *runtime, Executable *exe, int index, int argc, Object *closure, Heap *heap, Error *error);
#endif #endif