KEMBAR78
LIL Presentation | PDF
The LIL Language
What is LIL
What is LIL
ī€Š LIL is a small programming language
ī€Š LIL stands for Little Interpreted Language
ī€Š Focused on being an extension language
ī€Š Highly dynamic
ī€Š Minimal syntax
ī€Š Easy to embed
LIL is small
ī€Š A small core set of functions (about 50)
ī€Š String handling
ī€Š Lists
ī€Š Flow control
ī€Š etc
ī€Š A pair of .c and .h files
ī€Š About 3000 lines of C source code
ī€Š Could be smaller, but had to be usable :-)
ī€Š The ā€lilā€ executable is ~40K (Linux x86)
Extension languages
ī€Š A language to write extensions for a host
program
ī€Š Extensions depend on the program but most
complex programs can be extended via scripts
ī€Š A wizard for an office application
ī€Š The logic for a game character
ī€Š Responses for web requests in a server
ī€Š A text editor script to convert a set of ā€#defineā€ lines
to ā€caseā€ lines for C code which return a string with
the constant's name without some prefix
ī€Š I need this more often than i would want...
LIL is an extension language
ī€Š A host can extend the language by providing
new functions
ī€Š in LIL everything is done via functions, including
control structures like if, for, while, etc
ī€Š a function can execute code passed as a function
argument, set a new local or global variable, create
new execution environments, etc
ī€Š Listen to callbacks for I/O, errors, exit requests,
writing and reading to variables, etc
ī€Š I/O callbacks can be used to sandbox script
ī€Š Variable callbacks can expose host variables
LIL is highly dynamic
ī€Š No separate compilation and execution step
ī€Š The runtime can be modified at any time
ī€Š New variables can be introduced
ī€Š New functions can be defined – or redefined
ī€Š Functions can be renamed – including natives
ī€Š The runtime can be inspected via reflection
ī€Š All code and data is stored as strings and
converted to the appropriate type when needed
ī€Š Code can be generated and is evaluated lazyly
ī€Š String functions can be used with, e.g. lists
LIL has minimal syntax
ī€Š Everything follows a simple format:
word1Ā word2Ā word3 … wordn
ī€Š For commands the first word is the function name
and the rest are the function arguments
ī€Š Words are separated by space, but they can
contain special characters and/or be inside
special quotes (single quotes, double quotes,
braces and brackets)
ī€Š Words can contain spaces or other special
characters, including – if escaped properly –
characters with a special meaning for LIL
LIL has minimal syntax (cont.)
ī€Š Single and double quotes can be used to
include spaces and/or escaping characters
ī€Š A command inside [ and ] is executed and its
result is used in place of the […]
ī€Š Anything inside { and } is used as-is
ī€Š however LIL will count the {s and }s so that they can
be nested
ī€Š $ followed by a word evalutes to a variable read
ī€Š this can be changed at runtime to do other things
ī€Š And that is it. See LIL's readme.txt for details
LIL is easy to embed
ī€Š Uses a readable and permissive license: zlib
ī€Š Written in C89 with few common C99 features
ī€Š Can be used as a shared or static library
ī€Š or just drop the lil.c and lil.h pair in your project
ī€Š Small API to learn with few simple calls needed
for practical use
ī€Š Can be used with C and C++ directly and from
languages which support C shared libraries
ī€Š Import LIL is a project that provides bindings for
Free Pascal and .NET (and others in the future)
Some examples
LIL language examples
LIL examples
ī€Š Mandatory hello world
printĀ Hello,Ā world
ī€Š Notice how quotes are not needed
ī€Š Error-try-catch
tryĀ {Ā ifĀ stuffĀ {Ā errorĀ ā€BadĀ stuffā€Ā }Ā }Ā {Ā printĀ 
GotĀ anĀ error:Ā [reflectĀ error]Ā }
ī€Š Factorial of 10
printĀ [[funcĀ {x}Ā {Ā ifĀ [exprĀ $xĀ ==Ā 1]Ā {Ā returnĀ 1Ā 
}Ā {Ā returnĀ [exprĀ [[reflectĀ name]Ā [exprĀ $xĀ Ā­Ā 1]]Ā 
*Ā $x]Ā }}]Ā 10]
LIL examples (cont.)
#Ā printĀ allĀ knownĀ functionsĀ withĀ nameĀ lengths
#Ā lessĀ thanĀ fiveĀ characters
setĀ knownĀ­funcsĀ [reflectĀ funcs]
setĀ predicateĀ {[lengthĀ $x]Ā <Ā 5}
setĀ smallĀ­funcsĀ [filterĀ $knownĀ­funcsĀ $predicate]
printĀ "FunctionsĀ withĀ smallĀ names:"
foreachĀ $smallĀ­funcsĀ {printĀ "Ā Ā $i"}
C API examples
ī€Š Create and destroy a LIL runtime
lil_tĀ lilĀ =Ā lil_new();
/*Ā doĀ stuffĀ hereĀ */
lil_free(lil);
ī€Š Run some LIL code
lil_free_value(lil_parse(lil,ā€printĀ hiā€,0,0));
ī€Š Register a custom function
lil_register(lil,Ā ā€newfuncā€,Ā fnc_newfunc);
ī€Š fnc_newfunc is defined in the next slide
C API examples (cont.)
/*Ā myfuncĀ nativeĀ functionĀ */
LILCALLBACKĀ lil_value_tĀ fnc_myfunc(
Ā Ā Ā Ā lil_tĀ lil,Ā Ā Ā Ā Ā Ā Ā Ā Ā /*Ā lilĀ runtimeĀ */
Ā Ā Ā Ā size_tĀ argc,Ā Ā Ā Ā Ā Ā Ā /*Ā numberĀ ofĀ argumentsĀ */
Ā Ā Ā Ā lil_value_t*Ā argv)Ā /*Ā argumentsĀ arrayĀ */
{
Ā Ā Ā Ā size_tĀ i;
Ā Ā Ā Ā printf(ā€I'veĀ gotĀ %iĀ arguments:ā€,Ā (int)argc);
Ā Ā Ā Ā forĀ (i=0;Ā i<argc;Ā i++)
Ā Ā Ā Ā Ā Ā Ā Ā printf(ā€Ā Ā '%s'nā€,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā lil_to_string(argv[i]));
Ā Ā Ā Ā returnĀ lil_alloc_string(ā€myfuncĀ result!ā€);
}
print ā€That was itā€
That was it
More information
ī€Š LIL at github: https://github.com/badsector/lil
ī€Š Latest source code
ī€Š Wiki
ī€Š Issue db
ī€Š The readme.txt file in LIL's source code
ī€Š Everything about LIL
ī€Š Syntax
ī€Š Reference
ī€Š Import LIL: https://github.com/badsector/implil
ī€Š Bindings for Free Pascal, .NET and maybe others

LIL Presentation

  • 1.
  • 2.
    What is LIL ī€ŠLIL is a small programming language ī€Š LIL stands for Little Interpreted Language ī€Š Focused on being an extension language ī€Š Highly dynamic ī€Š Minimal syntax ī€Š Easy to embed
  • 3.
    LIL is small ī€ŠA small core set of functions (about 50) ī€Š String handling ī€Š Lists ī€Š Flow control ī€Š etc ī€Š A pair of .c and .h files ī€Š About 3000 lines of C source code ī€Š Could be smaller, but had to be usable :-) ī€Š The ā€lilā€ executable is ~40K (Linux x86)
  • 4.
    Extension languages ī€Š Alanguage to write extensions for a host program ī€Š Extensions depend on the program but most complex programs can be extended via scripts ī€Š A wizard for an office application ī€Š The logic for a game character ī€Š Responses for web requests in a server ī€Š A text editor script to convert a set of ā€#defineā€ lines to ā€caseā€ lines for C code which return a string with the constant's name without some prefix ī€Š I need this more often than i would want...
  • 5.
    LIL is anextension language ī€Š A host can extend the language by providing new functions ī€Š in LIL everything is done via functions, including control structures like if, for, while, etc ī€Š a function can execute code passed as a function argument, set a new local or global variable, create new execution environments, etc ī€Š Listen to callbacks for I/O, errors, exit requests, writing and reading to variables, etc ī€Š I/O callbacks can be used to sandbox script ī€Š Variable callbacks can expose host variables
  • 6.
    LIL is highlydynamic ī€Š No separate compilation and execution step ī€Š The runtime can be modified at any time ī€Š New variables can be introduced ī€Š New functions can be defined – or redefined ī€Š Functions can be renamed – including natives ī€Š The runtime can be inspected via reflection ī€Š All code and data is stored as strings and converted to the appropriate type when needed ī€Š Code can be generated and is evaluated lazyly ī€Š String functions can be used with, e.g. lists
  • 7.
    LIL has minimalsyntax ī€Š Everything follows a simple format: word1Ā word2Ā word3 … wordn ī€Š For commands the first word is the function name and the rest are the function arguments ī€Š Words are separated by space, but they can contain special characters and/or be inside special quotes (single quotes, double quotes, braces and brackets) ī€Š Words can contain spaces or other special characters, including – if escaped properly – characters with a special meaning for LIL
  • 8.
    LIL has minimalsyntax (cont.) ī€Š Single and double quotes can be used to include spaces and/or escaping characters ī€Š A command inside [ and ] is executed and its result is used in place of the […] ī€Š Anything inside { and } is used as-is ī€Š however LIL will count the {s and }s so that they can be nested ī€Š $ followed by a word evalutes to a variable read ī€Š this can be changed at runtime to do other things ī€Š And that is it. See LIL's readme.txt for details
  • 9.
    LIL is easyto embed ī€Š Uses a readable and permissive license: zlib ī€Š Written in C89 with few common C99 features ī€Š Can be used as a shared or static library ī€Š or just drop the lil.c and lil.h pair in your project ī€Š Small API to learn with few simple calls needed for practical use ī€Š Can be used with C and C++ directly and from languages which support C shared libraries ī€Š Import LIL is a project that provides bindings for Free Pascal and .NET (and others in the future)
  • 10.
  • 11.
    LIL examples ī€Š Mandatoryhello world printĀ Hello,Ā world ī€Š Notice how quotes are not needed ī€Š Error-try-catch tryĀ {Ā ifĀ stuffĀ {Ā errorĀ ā€BadĀ stuffā€Ā }Ā }Ā {Ā printĀ  GotĀ anĀ error:Ā [reflectĀ error]Ā } ī€Š Factorial of 10 printĀ [[funcĀ {x}Ā {Ā ifĀ [exprĀ $xĀ ==Ā 1]Ā {Ā returnĀ 1Ā  }Ā {Ā returnĀ [exprĀ [[reflectĀ name]Ā [exprĀ $xĀ Ā­Ā 1]]Ā  *Ā $x]Ā }}]Ā 10]
  • 12.
  • 13.
    C API examples ī€ŠCreate and destroy a LIL runtime lil_tĀ lilĀ =Ā lil_new(); /*Ā doĀ stuffĀ hereĀ */ lil_free(lil); ī€Š Run some LIL code lil_free_value(lil_parse(lil,ā€printĀ hiā€,0,0)); ī€Š Register a custom function lil_register(lil,Ā ā€newfuncā€,Ā fnc_newfunc); ī€Š fnc_newfunc is defined in the next slide
  • 14.
    C API examples(cont.) /*Ā myfuncĀ nativeĀ functionĀ */ LILCALLBACKĀ lil_value_tĀ fnc_myfunc( Ā Ā Ā Ā lil_tĀ lil,Ā Ā Ā Ā Ā Ā Ā Ā Ā /*Ā lilĀ runtimeĀ */ Ā Ā Ā Ā size_tĀ argc,Ā Ā Ā Ā Ā Ā Ā /*Ā numberĀ ofĀ argumentsĀ */ Ā Ā Ā Ā lil_value_t*Ā argv)Ā /*Ā argumentsĀ arrayĀ */ { Ā Ā Ā Ā size_tĀ i; Ā Ā Ā Ā printf(ā€I'veĀ gotĀ %iĀ arguments:ā€,Ā (int)argc); Ā Ā Ā Ā forĀ (i=0;Ā i<argc;Ā i++) Ā Ā Ā Ā Ā Ā Ā Ā printf(ā€Ā Ā '%s'nā€, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā lil_to_string(argv[i])); Ā Ā Ā Ā returnĀ lil_alloc_string(ā€myfuncĀ result!ā€); }
  • 15.
    print ā€That wasitā€ That was it
  • 16.
    More information ī€Š LILat github: https://github.com/badsector/lil ī€Š Latest source code ī€Š Wiki ī€Š Issue db ī€Š The readme.txt file in LIL's source code ī€Š Everything about LIL ī€Š Syntax ī€Š Reference ī€Š Import LIL: https://github.com/badsector/implil ī€Š Bindings for Free Pascal, .NET and maybe others