Beariish/little

tried to fix compilation, then the demo is broken.

Opened this issue · 0 comments

i'm using gcc 7.5.0 on ubuntu 18.04.
fixed some errors and warnings in favor of c99 on *nix, then the demo is broken almost everywhere.
not sure if i broke something.
i love the minimal design and the overall style, though with a bit further personal preference on the language.
but it got in my way when i wanted to firstly fix it for "likely compile anywhere".

diff --git a/src/little.c b/src/little.c
index cb918d9..9bd3200 100644
--- a/src/little.c
+++ b/src/little.c
@@ -139,14 +139,14 @@ double lt_get_number(lt_Value v)
 static void _lt_tokenize_error(lt_VM* vm, const char* module, uint16_t line, uint16_t col, const char* message)
 {
 	char sprint_buf[128];
-	sprintf_s(sprint_buf, 128, "%s|%d:%d: %s", module, line, col, message);
+	snprintf(sprint_buf, 128, "%s|%d:%d: %s", module, line, col, message);
 	lt_error(vm, sprint_buf);
 }
 
 static void _lt_parse_error(lt_VM* vm, const char* module, lt_Token* t, const char* message)
 {
 	char sprint_buf[128];
-	sprintf_s(sprint_buf, 128, "%s|%d:%d: %s", module, t->line, t->col, message);
+	snprintf(sprint_buf, 128, "%s|%d:%d: %s", module, t->line, t->col, message);
 	lt_error(vm, sprint_buf);
 }
 
@@ -174,21 +174,22 @@ void lt_runtime_error(lt_VM* vm, const char* message)
 
 	lt_Frame* topmost = &vm->callstack[vm->depth - 1];
 	lt_DebugInfo* info = _lt_get_debuginfo(topmost->callee);
-	lt_DebugLoc loc = _lt_get_location(info, topmost->ip - (lt_Op*)topmost->code->data);
+	lt_DebugLoc loc = _lt_get_location(info, *topmost->ip - (lt_Op*)topmost->code->data);
 
 	const char* name = "<unknown>";
 	if (info) name = info->module_name;
 
-	uint32_t len = sprintf_s(sprint_buf, 1024, "%s|%d:%d: %s\ntraceback:", name, loc.line, loc.col, message);
+	int len = snprintf(sprint_buf, 1024, "%s|%d:%d: %s\ntraceback:", name, loc.line, loc.col, message);
 	for (uint32_t i = vm->depth - 1; i >= 0; --i)
 	{
 		lt_Frame* frame = &vm->callstack[i];
 		lt_DebugInfo* info = _lt_get_debuginfo(frame->callee);
-		lt_DebugLoc loc = _lt_get_location(info, frame->ip - (lt_Op*)frame->code->data);
+		lt_DebugLoc loc = _lt_get_location(info, *frame->ip - (lt_Op*)frame->code->data);
 
 		const char* name = "<unknown>";
 		if (info) name = info->module_name;
-		len = sprintf_s(sprint_buf + len, 1024 - len, "\n(%s|%d:%d)", name, loc.line, loc.col);
+		--len;
+		len += snprintf(sprint_buf + len, 1024 - len, "\n(%s|%d:%d)", name, loc.line, loc.col);
 	}
 
 	lt_error(vm, sprint_buf);
@@ -372,7 +373,7 @@ lt_Tokenizer lt_tokenize(lt_VM* vm, const char* source, const char* mod_name)
 					lt_Literal newlit;
 					newlit.type = LT_TOKEN_STRING_LITERAL;
 					newlit.string = vm->alloc(length + 1);
-					strncpy_s(newlit.string, length + 1, start, length);
+					memcpy(newlit.string, start, length);
 					newlit.string[length] = 0;
 
 					lt_buffer_push(vm, &t.literal_buffer, &newlit);
@@ -483,7 +484,7 @@ lt_Tokenizer lt_tokenize(lt_VM* vm, const char* source, const char* mod_name)
 						lt_Identifier newid;
 						newid.num_references = 1;
 						newid.name = vm->alloc(length + 1);
-						strncpy_s(newid.name, length + 1, start, length);
+						memcpy(newid.name, start, length);
 						newid.name[length] = 0;
 
 						lt_buffer_push(vm, &t.identifier_buffer, &newid);
@@ -692,7 +693,7 @@ lt_Scope* _lt_parse_block(lt_VM* vm, lt_Parser* p, lt_Token* start, lt_Buffer* d
 			lt_Identifier newid;
 			newid.num_references = 1;
 			newid.name = vm->alloc(len + 1);
-			strncpy_s(newid.name, len + 1, FOR_ITER_NAME, len);
+			memcpy(newid.name, FOR_ITER_NAME, len);
 			newid.name[len] = 0;
 
 			lt_buffer_push(vm, &p->tkn->identifier_buffer, &newid);
diff --git a/src/little.h b/src/little.h
index 2a793ec..05f7217 100644
--- a/src/little.h
+++ b/src/little.h
@@ -365,7 +365,7 @@ typedef void (*lt_ErrorFn)(struct lt_VM* vm, const char*);
 #define LT_DEDUP_TABLE_SIZE 64
 #endif
 
-typedef struct {
+struct lt_VM {
 	lt_Buffer heap;
 	lt_Buffer keepalive;
 
@@ -386,7 +386,8 @@ typedef struct {
 
 	void* error_buf;
 	uint8_t generate_debug;
-} lt_VM;
+};
+typedef struct lt_VM lt_VM;
 
 lt_VM* lt_open(lt_AllocFn alloc, lt_FreeFn free, lt_ErrorFn error);
 void lt_destroy(lt_VM* vm);
diff --git a/src/little_std.c b/src/little_std.c
index 33a07c4..29ca987 100644
--- a/src/little_std.c
+++ b/src/little_std.c
@@ -19,29 +19,30 @@ void ltstd_open_all(lt_VM* vm)
 char* ltstd_tostring(lt_VM* vm, lt_Value val)
 {
     char scratch[256];
-    uint8_t len = 0;
+    int len = 0;
 
-    if (LT_IS_NUMBER(val)) len = sprintf_s(scratch, 256, "%f", LT_GET_NUMBER(val));
-    if (LT_IS_NULL(val)) len = sprintf_s(scratch, 256, "null");
-    if (LT_IS_TRUE(val)) len = sprintf_s(scratch, 256, "true");
-    if (LT_IS_FALSE(val)) len = sprintf_s(scratch, 256, "false");
-    if (LT_IS_STRING(val)) len = sprintf_s(scratch, 256, "%s", lt_get_string(vm, val));;
+    if (LT_IS_NUMBER(val)) len = snprintf(scratch, 256, "%f", LT_GET_NUMBER(val));
+    if (LT_IS_NULL(val)) len = snprintf(scratch, 256, "null");
+    if (LT_IS_TRUE(val)) len = snprintf(scratch, 256, "true");
+    if (LT_IS_FALSE(val)) len = snprintf(scratch, 256, "false");
+    if (LT_IS_STRING(val)) len = snprintf(scratch, 256, "%s", lt_get_string(vm, val));;
 
     if (LT_IS_OBJECT(val))
     {
         lt_Object* obj = LT_GET_OBJECT(val);
         switch (obj->type)
         {
-        case LT_OBJECT_CHUNK: len = sprintf_s(scratch, 256, "chunk 0x%llx", (uintptr_t)obj); break;
-        case LT_OBJECT_CLOSURE: len = sprintf_s(scratch, 256, "closure 0x%llx | %d upvals", (uintptr_t)LT_GET_OBJECT(obj->closure.function), obj->closure.captures.length); break;
-        case LT_OBJECT_FN: len = sprintf_s(scratch, 256, "function 0x%llx", (uintptr_t)obj); break;
-        case LT_OBJECT_TABLE: len = sprintf_s(scratch, 256, "table 0x%llx", (uintptr_t)obj); break;
-        case LT_OBJECT_ARRAY: len = sprintf_s(scratch, 256, "array | %d", lt_array_length(val)); break;
-        case LT_OBJECT_NATIVEFN: len = sprintf_s(scratch, 256, "native 0x%llx", (uintptr_t)obj); break;
+        case LT_OBJECT_CHUNK: len = snprintf(scratch, 256, "chunk %p", obj); break;
+        case LT_OBJECT_CLOSURE: len = snprintf(scratch, 256, "closure %p | %d upvals", LT_GET_OBJECT(obj->closure.function), obj->closure.captures.length); break;
+        case LT_OBJECT_FN: len = snprintf(scratch, 256, "function %p", obj); break;
+        case LT_OBJECT_TABLE: len = snprintf(scratch, 256, "table %p", obj); break;
+        case LT_OBJECT_ARRAY: len = snprintf(scratch, 256, "array | %d", lt_array_length(val)); break;
+        case LT_OBJECT_NATIVEFN: len = snprintf(scratch, 256, "native %p", obj); break;
         }
     }
 
-    char* str = vm->alloc(len + 1);
+    if (len > 256) len = 256;
+    char* str = vm->alloc(len);
     memcpy(str, scratch, len);
     str[len] = 0;
 
@@ -275,7 +276,7 @@ static uint8_t _lt_array_last(lt_VM* vm, uint8_t argc)
     lt_Value arr = lt_pop(vm);
     if (!LT_IS_ARRAY(arr)) lt_runtime_error(vm, "Expected argument to array.last to be array!");
 
-    lt_push(vm, lt_array_at(arr, lt_array_length(arr) - 1));
+    lt_push(vm, *lt_array_at(arr, lt_array_length(arr) - 1));
     return 1;
 }
 
@@ -441,19 +442,19 @@ static uint8_t _lt_string_format(lt_VM* vm, uint8_t argc)
                 {
                 case 'd': case 'i': {
                     fmtbuf[fmtloc++] = *format++; fmtbuf[fmtloc] = 0;
-                    o_idx += sprintf_s(output + o_idx, 1024 - o_idx, fmtbuf, (int32_t)LT_GET_NUMBER(*(vm->top - argc + current_arg++)));
+                    o_idx += snprintf(output + o_idx, 1024 - o_idx, fmtbuf, (int32_t)LT_GET_NUMBER(*(vm->top - argc + current_arg++)));
                 } break;
                 case 'o': case 'u': case 'x': case 'X': {
                     fmtbuf[fmtloc++] = *format++; fmtbuf[fmtloc] = 0;
-                    o_idx += sprintf_s(output + o_idx, 1024 - o_idx, fmtbuf, (uint32_t)LT_GET_NUMBER(*(vm->top - argc + current_arg++)));
+                    o_idx += snprintf(output + o_idx, 1024 - o_idx, fmtbuf, (uint32_t)LT_GET_NUMBER(*(vm->top - argc + current_arg++)));
                 } break;
                 case 'e': case 'E': case 'f': case 'g': case 'G': {
                     fmtbuf[fmtloc++] = *format++; fmtbuf[fmtloc] = 0;
-                    o_idx += sprintf_s(output + o_idx, 1024 - o_idx, fmtbuf, LT_GET_NUMBER(*(vm->top - argc + current_arg++)));
+                    o_idx += snprintf(output + o_idx, 1024 - o_idx, fmtbuf, LT_GET_NUMBER(*(vm->top - argc + current_arg++)));
                 } break;
                 case 's': {
                     fmtbuf[fmtloc++] = *format++; fmtbuf[fmtloc] = 0;
-                    o_idx += sprintf_s(output + o_idx, 1024 - o_idx, fmtbuf, lt_get_string(vm, *(vm->top - argc + current_arg++)));
+                    o_idx += snprintf(output + o_idx, 1024 - o_idx, fmtbuf, lt_get_string(vm, *(vm->top - argc + current_arg++)));
                 } break;
                 default:
                     fmtbuf[fmtloc++] = *format++;
cat says ???!
dog says ???!
mouse says ???!
monkey says ???!
This should only appear once!
Hello I am  array | 4