reorganizing code
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
#include "../o_staticmap.h"
|
||||
#include "../runtime/o_staticmap.h"
|
||||
extern const StaticMapSlot bins_basic[];
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
#include "../o_staticmap.h"
|
||||
#include "../runtime/o_staticmap.h"
|
||||
extern const StaticMapSlot bins_file[];
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
#include "../o_staticmap.h"
|
||||
#include "../runtime/o_staticmap.h"
|
||||
extern const StaticMapSlot bins_math[];
|
||||
+89
-94
@@ -2,11 +2,11 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "o_staticmap.h"
|
||||
#include "compiler/parse.h"
|
||||
#include "compiler/compile.h"
|
||||
#include "runtime/runtime.h"
|
||||
#include "runtime/runtime_error.h"
|
||||
#include "runtime/o_staticmap.h"
|
||||
#include "builtins/basic.h"
|
||||
|
||||
static const char usage[] =
|
||||
@@ -16,11 +16,6 @@ static const char usage[] =
|
||||
" $ noja dis file.noja\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)
|
||||
{
|
||||
if(type == NULL)
|
||||
@@ -47,93 +42,6 @@ static void print_error(const char *type, Error *error)
|
||||
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)
|
||||
{
|
||||
// Compile the code. This section transforms
|
||||
@@ -374,4 +282,91 @@ static _Bool disassemble_code(const char *code)
|
||||
|
||||
Source_Free(src);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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 <assert.h>
|
||||
#include "o_nfunc.h"
|
||||
#include "o_staticmap.h"
|
||||
#include "utils/defs.h"
|
||||
#include "objects/objects.h"
|
||||
#include "../utils/defs.h"
|
||||
#include "../objects/objects.h"
|
||||
|
||||
typedef struct {
|
||||
Object base;
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef STATICMAP_H
|
||||
#define STATICMAP_H
|
||||
|
||||
#include "objects/objects.h"
|
||||
#include "runtime/runtime.h"
|
||||
#include "../objects/objects.h"
|
||||
#include "runtime.h"
|
||||
|
||||
typedef enum {
|
||||
SM_END,
|
||||
+1
-33
@@ -1,6 +1,7 @@
|
||||
#include <stdlib.h>
|
||||
#include "../utils/defs.h"
|
||||
#include "../utils/stack.h"
|
||||
#include "o_func.h"
|
||||
#include "runtime.h"
|
||||
|
||||
#define MAX_FRAME_STACK 16
|
||||
@@ -26,39 +27,6 @@ struct xRuntime {
|
||||
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)
|
||||
{
|
||||
return Stack_Copy(runtime->stack, 1);
|
||||
|
||||
+5
-16
@@ -5,9 +5,8 @@
|
||||
#include "../utils/stack.h"
|
||||
#include "../objects/objects.h"
|
||||
#include "../common/executable.h"
|
||||
|
||||
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_New2(int stack_size, Heap *heap, _Bool free_heap, void *callback_userp, _Bool (*callback_addr)(Runtime*, void*));
|
||||
void Runtime_Free(Runtime *runtime);
|
||||
@@ -19,18 +18,8 @@ Object* Runtime_GetBuiltins(Runtime *runtime);
|
||||
void Runtime_SetBuiltins(Runtime *runtime, Object *builtins);
|
||||
int Runtime_GetCurrentIndex(Runtime *runtime);
|
||||
Executable *Runtime_GetCurrentExecutable(Runtime *runtime);
|
||||
|
||||
typedef struct xSnapshot Snapshot;
|
||||
Snapshot *Snapshot_New(Runtime *runtime);
|
||||
void Snapshot_Free(Snapshot *snapshot);
|
||||
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* 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);
|
||||
Snapshot *Snapshot_New(Runtime *runtime);
|
||||
void Snapshot_Free(Snapshot *snapshot);
|
||||
void Snapshot_Print(Snapshot *snapshot, FILE *fp);
|
||||
Object *run(Runtime *runtime, Error *error, Executable *exe, int index, Object *closure, Object **argv, int argc);
|
||||
#endif
|
||||
Reference in New Issue
Block a user