Recent

Author Topic: How to compile Lily an embedded language into a .so/.dll to link with Pascal?  (Read 4352 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
I am experimenting with in embedding small interpreters in Pascal and I am currently looking at Lily - https://github.com/jesserayadkins/lily.

It has two files showing examples of how the interpreter is invoked - https://github.com/jesserayadkins/lily/blob/master/run/lily.c and

Code: [Select]
{
    int argc_offset;
    process_args(argc, argv, &argc_offset);
    if (to_process == NULL)
        usage();

    lily_options *options = lily_new_default_options();
    if (gc_start != -1)
        options->gc_start = gc_start;
    if (gc_multiplier != -1)
        options->gc_multiplier = gc_multiplier;

    options->argc = argc - argc_offset;
    options->argv = argv + argc_offset;

    lily_parse_state *parser = lily_new_parse_state(options);
    lily_lex_mode mode = (do_tags ? lm_tags : lm_no_tags);

    int result;
    if (is_file == 1)
        result = lily_parse_file(parser, mode, to_process);
    else
        result = lily_parse_string(parser, "[cli]", mode, to_process);

    if (result == 0) {
        fputs(lily_build_error_message(parser), stderr);
        exit(EXIT_FAILURE);
    }

    lily_free_parse_state(parser);
    lily_free_options(options);
    exit(EXIT_SUCCESS);
}


Code: [Select]
static int lily_handler(request_rec *r)
{
    if (strcmp(r->handler, "lily"))
        return DECLINED;

    r->content_type = "text/html";

    lily_options *options = lily_new_default_options();
    options->data = r;
    options->html_sender = (lily_html_sender) ap_rputs;
    options->allow_sys = 0;

    lily_parse_state *parser = lily_new_parse_state(options);
    lily_register_package(parser, "server", apache_server_dynaload_table,
            lily_apache_server_loader);

    lily_parse_file(parser, lm_tags, r->filename);

    lily_free_parse_state(parser);
    lily_free_options(options);

    return OK;
}

In both instances the key aspect  involves creating a lily_parse_state, a lily_lex_mode and executing a lily_parse_file or a lily_parse_string with it.

Since the language is not created in C it looks like I need to translate the necessary headers to from C to Pascal, and use that create the Pascal program that will use the lily.so or lily.dll as is done with Lua and Javascript.

Any ideas on how to go about that? I don't think the developer has implemented building it as .dll or .so but I would like to give it a try.

« Last Edit: July 10, 2016, 03:02:42 pm by vfclists »
Lazarus 3.0/FPC 3.2.2

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Since the language is not created in C it looks like I need to translate the necessary headers to from C to Pascal, and use that create the Pascal program that will use the lily.so or lily.dll as is done with Lua and Javascript.

Any ideas on how to go about that? I don't think the developer has implemented building it as .dll or .so but I would like to give it a try.
http://wiki.lazarus.freepascal.org/Creating_bindings_for_C_libraries
The build system creates liblily.a which you can link to statically or convert into .so / .dll easily:
Code: [Select]
$ gcc -shared -o liblily.so -Wl,--whole-archive lib/liblily.a -Wl,--no-whole-archive
I did it from cmake build directory. Of course, you have to build liblily.a first.

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
http://wiki.lazarus.freepascal.org/Creating_bindings_for_C_libraries
The build system creates liblily.a which you can link to statically or convert into .so / .dll easily:
Code: [Select]
$ gcc -shared -o liblily.so -Wl,--whole-archive lib/liblily.a -Wl,--no-whole-archive
I did it from cmake build directory. Of course, you have to build liblily.a first.

What I need to know which part is compiled in Pascal and which part is comiled in C. lily.c appears to contain what I need, but it also clear than I don't need process_args and usage.

So in if I compile a pascal program what files and code do I have to add to it, in addition to the translated headers?

I compiled the lily.c program quite easily for Windows and have it running, but how do create the lily.dll/so.?

Can you show me your example?
Lazarus 3.0/FPC 3.2.2

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
What I need to know which part is compiled in Pascal and which part is comiled in C. lily.c appears to contain what I need, but it also clear than I don't need process_args and usage.
That file is the lily interpreter, if you only follow it, you only make the interpreter again, not really embedding it.
So in if I compile a pascal program what files and code do I have to add to it, in addition to the translated headers?
Depends on what you want to do, the API is big and undocumented. There are only two examples extending the core library, mod_lily and postgres, but none existent that actually embeds the library in another environment. You have to figure out yourself which functions to call to perform certain functionality.
I compiled the lily.c program quite easily for Windows and have it running, but how do create the lily.dll/so.?

Can you show me your example?
I showed you my gcc command. No idea if it directly applies to whatever C compiler for Windows you use, building shared library is not standardized among C compilers.

EDIT:
Found something that might put you off: https://github.com/jesserayadkins/lily/issues/228
Better use existing languages that already proven to work, Lua is damn small and just as powerful if not more powerful.
« Last Edit: July 10, 2016, 11:33:37 pm by Leledumbo »

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?

EDIT:
Found something that might put you off: https://github.com/jesserayadkins/lily/issues/228
Better use existing languages that already proven to work, Lua is damn small and just as powerful if not more powerful.

I know the language is not yet fully developed, my aim is to know how these systems work. There is wren. https://github.com/munificent/wren and duktape https://github.com/svaarala/duktape to try out as well, but Lily is somewhat different in its design.
Lazarus 3.0/FPC 3.2.2

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
I know the language is not yet fully developed, my aim is to know how these systems work. There is wren. https://github.com/munificent/wren and duktape https://github.com/svaarala/duktape to try out as well, but Lily is somewhat different in its design.
Well... with the author even claims that it's not possible to embed officially yet, you're pretty much relying on fragile code. Go ahead if you're ready.

 

TinyPortal © 2005-2018