minor cleanups

This commit is contained in:
cozis
2022-08-17 19:54:28 +02:00
parent 6ba7fd21ef
commit e4bc4fe253
3 changed files with 30 additions and 32 deletions
+5 -2
View File
@@ -1,6 +1,8 @@
fun isSpace(c: String) fun isSpace(c: String)
return c == " " or c == "\t" or c == "\n"; return c == " "
or c == "\t"
or c == "\n";
fun isDigit(c: String) { fun isDigit(c: String) {
u = unicode(c); u = unicode(c);
@@ -125,7 +127,7 @@ fun parseIntegerOrFloating(context: Map) {
# Is the first digit sequence followed by a dot # Is the first digit sequence followed by a dot
# and then one or more digits? # and then one or more digits?
k = skip(context, isDigit); k = skip(context, isDigit);
if k+1 < count(context.str) and context.str[k] == "." and isDigit(context.str[k+1]): if k+1 < count(context.str) and context.str[k] == "." and isDigit(context.str[k+1]):
# It's a floating point value, then! # It's a floating point value, then!
@@ -232,6 +234,7 @@ fun parseValue(context: Map) {
} }
fun parse(str: String) { fun parse(str: String) {
context = {str: str, cur: 0}; context = {str: str, cur: 0};
skipSpaces(context); skipSpaces(context);
val, err = parseValue(context); val, err = parseValue(context);
+1 -1
View File
@@ -59,4 +59,4 @@ assert(z == 1);
assert(w == none); assert(w == none);
Stack_Print(stack); Stack_Print(stack);
print("\n"); print("\n");
+24 -29
View File
@@ -56,10 +56,10 @@ static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object
{ {
UNUSED(argc); UNUSED(argc);
ASSERT(argc == 1); ASSERT(argc == 1);
Heap *heap = Runtime_GetHeap(runtime); Heap *heap = Runtime_GetHeap(runtime);
ASSERT(heap != NULL); ASSERT(heap != NULL);
Object *o_path = argv[0]; Object *o_path = argv[0];
const char *path; const char *path;
size_t path_len; size_t path_len;
@@ -69,18 +69,18 @@ static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object
Error_Report(error, 0, "Argument #%d is not a string", 1); Error_Report(error, 0, "Argument #%d is not a string", 1);
return -1; return -1;
} }
int n; int n;
path = Object_ToString(o_path, &n, Runtime_GetHeap(runtime), error); path = Object_ToString(o_path, &n, Runtime_GetHeap(runtime), error);
if (path == NULL) if (path == NULL)
return -1; return -1;
ASSERT(n >= 0); ASSERT(n >= 0);
path_len = (size_t) n; path_len = (size_t) n;
} }
char full_path[1024]; char full_path[1024];
if(path[0] == '/') { if(path[0] == '/') {
if(path_len >= sizeof(full_path)) { if(path_len >= sizeof(full_path)) {
Error_Report(error, 1, "Internal buffer is too small"); Error_Report(error, 1, "Internal buffer is too small");
@@ -102,7 +102,7 @@ static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object
memcpy(full_path + written, path, path_len); memcpy(full_path + written, path, path_len);
full_path[written + path_len] = '\0'; full_path[written + path_len] = '\0';
} }
Error sub_error; Error sub_error;
Error_Init(&sub_error); Error_Init(&sub_error);
Source *src = Source_FromFile(full_path, &sub_error); Source *src = Source_FromFile(full_path, &sub_error);
@@ -121,7 +121,7 @@ static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object
rets[1] = o_err; rets[1] = o_err;
return 2; return 2;
} }
CompilationErrorType errtyp; CompilationErrorType errtyp;
Executable *exe = compile(src, &sub_error, &errtyp); Executable *exe = compile(src, &sub_error, &errtyp);
if(exe == NULL) { if(exe == NULL) {
@@ -149,29 +149,26 @@ static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object
} }
Object *sub_rets[8]; Object *sub_rets[8];
int retc = run(runtime, &sub_error, exe, 0, NULL, NULL, 0, sub_rets);
if(retc < 0)
{ {
int retc = run(runtime, &sub_error, exe, 0, NULL, NULL, 0, sub_rets); const char *errname = "Runtime";
if(retc < 0) // Snapshot?
{ UNUSED(errname);
const char *errname = "Runtime";
// Snapshot?
UNUSED(errname);
Object *o_none = Object_NewNone(heap, error); Object *o_none = Object_NewNone(heap, error);
if(o_none == NULL) if(o_none == NULL) return -1;
return -1;
Object *o_err = Object_FromString(sub_error.message, -1, heap, error); Object *o_err = Object_FromString(sub_error.message, -1, heap, error);
if(o_err == NULL) if(o_err == NULL) return -1;
return -1;
Error_Free(&sub_error); Error_Free(&sub_error);
rets[0] = o_none; rets[0] = o_none;
rets[1] = o_err; rets[1] = o_err;
return 2; return 2;
} }
ASSERT(retc == 1); ASSERT(retc == 1);
}
Error_Free(&sub_error); Error_Free(&sub_error);
rets[0] = sub_rets[0]; rets[0] = sub_rets[0];
return 1; return 1;
@@ -505,7 +502,6 @@ void bins_basic_init(StaticMapSlot slots[])
} }
StaticMapSlot bins_basic[] = { StaticMapSlot bins_basic[] = {
{ "Type", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { "Type", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "None", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { "None", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
{ "int", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ }, { "int", SM_TYPE, .as_type = NULL /* Until bins_basic_init is called */ },
@@ -519,7 +515,6 @@ StaticMapSlot bins_basic[] = {
{ "math", SM_SMAP, .as_smap = bins_math, }, { "math", SM_SMAP, .as_smap = bins_math, },
{ "files", SM_SMAP, .as_smap = bins_files, }, { "files", SM_SMAP, .as_smap = bins_files, },
// { "net", SM_SMAP, .as_smap = bins_net, },
{ "import", SM_FUNCT, .as_funct = bin_import, .argc = 1, }, { "import", SM_FUNCT, .as_funct = bin_import, .argc = 1, },
{ "newBuffer", SM_FUNCT, .as_funct = bin_newBuffer, .argc = 1 }, { "newBuffer", SM_FUNCT, .as_funct = bin_newBuffer, .argc = 1 },