GithubHelp home page GithubHelp logo

lydiandy / little Goto Github PK

View Code? Open in Web Editor NEW

This project forked from beariish/little

0.0 0.0 0.0 68 KB

A small, easily embedded language implemented in a single .c file

License: MIT License

C 100.00%

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

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.