GithubHelp home page GithubHelp logo

little's Introduction

little - tiny bytecode language

little is a small, fast, easily embeddable language implemented in C.


var speak = fn(animal) {
    if animal is "cat" { return "meow" }
    elseif animal is "dog" { return "woof" }
    elseif animal is "mouse" { return "squeak" }
    return "???"
}

var animals = [ "cat", "dog", "mouse", "monkey" ]
for animal in array.each(animals) {
    io.print(string.format("%s says %s!", animal, speak(animal)))
}

Feature Overview

  • Tiny implementation - core langauge is <2500 sloc in a single .h/.c pair
  • Light embedding - compiles down to less than 20kb, 3 API calls to get started
  • Reasonably fast for realtime applications
  • Low memory footprint with simple mark and sweep garbage collector
  • Supports null, numbers, booleans, strings, functions, closures, arrays, tables, and native procedures
  • Optional, consise stdlib - an extra ~1000 sloc
  • Supports 32- and 64-bit, and will likely compile anywhere!
  • Feature-rich C api to integrate and interact with the VM

Simple embedding example

#include "little.h"
#include "little_std.h"

// this is called if the vm encounters an error, letting us react
void my_error_callback(lt_VM* vm, const char* msg)
{
    printf("LT ERROR: %s\n", msg);
}

int main(char** argv, int argc)
{
    lt_VM* vm = lt_open(malloc, free, my_error_callback);                    // open new VM
    ltstd_open_all(vm);                                                      // register stdlib
                   
    const char* my_source_code = ...                                         // read source from file/stream/string

    uint16_t n_return = lt_dostring(vm, my_source_code, "my_module")         // run code as "my_module" 
    if(n_return) printf("LT RETURNED: %s", ltstd_tostring(vm, lt_pop(vm)));  // if our code returns, print the result
}

Links


Known issues

  • Recursion is broken, sorry
    • just write better code

Potential improvements

  • Drop the AST for single pass compiler
    • but only if it actually makes the impl smaller
  • precomputed goto/jumptable vm instruction dispatch
  • copy fewer strings probably

Contribution

Feel free to open an issue or pull request if you feel you have something meaninfgul to add, but keep in mind the language is minimalist by design, so any merging will be very carefully picked


License

Please see LICENSE for details

little's People

Contributors

beariish avatar paulbraetz avatar rxi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

little's Issues

tried to fix compilation, then the demo is broken.

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

A question regarding garbage collection

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?

Several issues

Hello. When compiling little on Linux I noticed several issues here.

  1. 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).

  2. 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.