lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


An initial preview version is now available in Ravi. Simple tests like
these work:

https://github.com/dibyendumajumdar/ravi/blob/master/tests/comptests/inputs/54_embed_C.lua
https://github.com/dibyendumajumdar/ravi/blob/master/tests/comptests/inputs/55_embed_C.lua

Feedback welcome.

Thanks and Regards
Dibyendu

---------- Forwarded message ---------
From: Dibyendu Majumdar <mobile@majumdar.org.uk>
Date: Sun, 26 Sept 2021 at 12:53
Subject: Embedding C in Ravi
To: Lua mailing list <lua-l@lists.lua.org>


Hi

Over the years some projects have appeared that use Lua like syntax
but actually implement a C like language underneath. What if instead
we just allow C code to be embedded?

With the new compiler project for Ravi I have been experimenting with
allowing a subset of C code to be embedded in Ravi. This is made
possibly because the compiler generates C intermediate code anyway.

The idea is that there will be two new language keywords: C__decl and C__unsafe.

C__decl will only be allowed at top level and can be used to declare C types.

C__unsafe will allow embedding C code snippets.

C__unsafe takes variables as arguments - these will be made available
to the C code snippet. I am calling these bind variables as they bind
to the Ravi variables in scope.

The actual C code is provided as a string in both cases.

Example:

C__decl [[
typedef struct MyStruct {
    int a;
} MyStruct;
]]

function demo(u: Userdata)

    C__unsafe(u) [[
        MyStruct *s = (MyStruct *) u.ptr;
    ]]

end

I have a wiki page with these ideas:
https://github.com/dibyendumajumdar/ravi-compiler/wiki/Embedding-C

Any thoughts or feedback welcome.

Regards
Dibyendu