beariish / little Goto Github PK
View Code? Open in Web Editor NEWA small, easily embedded language implemented in a single .c file
License: MIT License
A small, easily embedded language implemented in a single .c file
License: MIT License
Are there, like, garbage collector callbacks?
Like, If you have a function from the C side that allocates something and passes it to the VM, but then the garbage collector destroys the reference from the side of the VM, how could the C side know that the reference was destroyed by GC?
This is the line:
Line 396 in 3aac5c1
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
The macro LT_IS_NULL(x) should use LT_VALUE_NULL?
Hello. When compiling little
on Linux I noticed several issues here.
Compatibility issues with sprintf_s(...)
. This function only exists in Windows and the equivalent (for Unix-based systems) is snprintf(...)
. Windows do have snprintf(...)
(see this) so using that function reasonably increase compatibility between two systems. Another function, called strncpy_s(...)
is again, Windows specific. However, strncpy(...)
can do exactly the same job except it will not have numberOfElements
param (which seems to me unreasonable and confusing to use).
There are several areas in code, I saw casting is unreasonable and does a totally different job. Most of them are incompatible pointer casts and at line 349; it's representing pointer insider pointer and comparing or doing arithmetic work will yield invalid operands error. I'm not sure which compiler you've used that ignores this thing.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.