jland.org

LKscript - simple scripting everywhere.

About

LK is a simple but powerful scripting language that is designed to be small, fast, and easily embedded in other applications. It enables people to extend the built-in functionality of their programs, and provides a cross-platform standard library of function calls. The core LK engine, including lexical analyzer, parser, compiler, and virtual machine comprises roughly 7000 lines of ISO-standard C++ code, and is only dependent on the Standard C++ Library (STL), making it extremely lightweight and portable.

Features

Example

The standard one:

outln( 'hello, world!' );

Of course, more examples can be found in the manual.

Documentation

View the manual here.

Download

Check out the latest source code from:

https://svn.jland.org/lk

API

An example of calling the LK language engine from a C++ command-line program:

#include
#include
#include

#include

#include
#include
#include
#include
#include
#include
#include
#include
#include

void fcall_out( lk::invoke_t &cxt )
{
    LK_DOC("out", "Output data to the console.", "(...):none");
    for (size_t i=0;i         fputs( lk::to_utf8(cxt.arg(i).as_string()).c_str(), stdout );
}

void fcall_outln( lk::invoke_t &cxt )
{
    LK_DOC("outln", "Output data to the console followed by a newline.", "(...):none");
    for (size_t i=0;i         fputs( lk::to_utf8(cxt.arg(i).as_string()).c_str(), stdout );
    
    fputs( "\n", stdout );
    fflush( stdout );
}
void fcall_in( lk::invoke_t &cxt )
{
#define NBUF 2048
    LK_DOC("in", "Input text from the user.", "(none):string");
    char buf[NBUF];
    fgets( buf, NBUF-1, stdin );
    cxt.result().assign( lk::from_utf8( buf ) );    
}

int main(int argc, char *argv[])
{
    bool parse_only = false;
    bool use_vm = true;
    
    if ( argc <= 1 )
    {
        printf("no input file specified\n");
        return -1;
    }
    
    if ( argc > 2 )
    {
        if( strcmp( argv[2], "--parse" ) == 0 ) parse_only = true;
        if( strcmp( argv[2], "--eval" ) == 0 ) use_vm = false;
    }
    
    lk::input_file p( argv[1] );
    lk::parser parse( p );

    std::auto_ptr tree( parse.script() );            
    int i=0;
    while ( i < parse.error_count() )
        printf( "%s\n", parse.error(i++).c_str() );
    
    if ( tree.get() == NULL || parse.token() != lk::lexer::END)
        printf("parsing did not reach end of input\n");

    if ( tree.get() == NULL || parse.error_count() > 0 || parse.token() != lk::lexer::END )
        return -1;
    
    if ( parse_only ) return 0;
    
    lk::env_t env;
    env.register_func( fcall_in );
    env.register_func( fcall_out );
    env.register_func( fcall_outln );

    env.register_funcs( lk::stdlib_basic() );
    env.register_funcs( lk::stdlib_string() );
    env.register_funcs( lk::stdlib_math() );

    if ( use_vm )
    {
        lk::code_gen C;
        if ( C.emitasm( tree.get() ) )
        {
            std::vector<unsigned int> code;
            std::vector data;
            std::vector id;
            std::vector dbg;        

            C.bytecode( code, data, id, dbg );
            
            lk::vm V;
            V.load( code, data, id, dbg );
            V.initialize( &env );
            if ( !V.run() )
            {
                printf("vm: %s\n", (const char*)V.error().c_str());
                return -1;
            }
        }
        else
        {
            printf("codegen: %s\n", (const char*)C.error().c_str() );
            return -1;
        }
    }
    else
    {
        lk::eval ev( tree.get(), &env );
        if ( !ev.run() )
        {
            for( size_t i=0;i                 printf("eval: %s\n", (const char*) ev.get_error(i).c_str() );
            
            return -1;
        }
        
    }
        
    return 0;
}

Author

Aron P. Dobos

License

Copyright (c) 2017, Alliance for Sustainable Energy, LLC.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the Alliance for Sustainable Energy, LLC.