javicervera / leaflang Goto Github PK
View Code? Open in Web Editor NEWA super simple interpreted language inspired by Lua and BASIC.
License: GNU General Public License v3.0
A super simple interpreted language inspired by Lua and BASIC.
License: GNU General Public License v3.0
Because "table" is a common term already used in database contexts.
Using hashes as lists is slow, a specific list type should be added for convenience.
In the future, compiler and interpreter will be separated, and interpreter will be written in C. Having the core library written in C already will simplify this task, as it will be used in the interpreter.
The language currently requires a new line or semicolon in some circumstances, like to separate blocks in conditionals, loops and functions. It should only be required at the end of a statement.
Instead of using a unique symbol for each data type, suffixes will be specified using the |
operator followed by a letter indicating the type:
|i
: Int|f
: Real (float)|s
: String|l
: List|h
: Hash|r
: Ref|w
: Weak refSupport for defining constants with the following syntax:
#CONST name[type] = value
They will be replaced by the preprocessor in the token list by their value.
Add functions that handle tables with numeric indexes instead of string keys:
Add functions to handle memory blocks:
Struct blocks should require statement ends, so constructs like this:
IF cond ... ELSE ... END
Will be written like this:
IF cond : ... : ELSE : ... : END
Add a preprocessor with support for #INCLUDE to load additional source files. These files must be tokenized and added to the main list of tokens, so the parsed receives a single block.
The path to the filename will be relative to the main source file, and not to the current source file.
Because expressions like var! = ...
look like the "not equal" expression from other languages.
The leaf
tool should only translate to C. The purpose of lfmake
would be to first call leaf
to get a C source file, and then use GCC to compile it into a binary. The original .c source file should be deleted after finishing.
It should also be possible to run programs directly with lfmake
, like this:
lfmake -r file.lf
This should also delete the binary after running.
This tool should be written in Leaf directly.
Tables should be allowed to be specified like this:
// Object-like table
user = {
"firstName": "Javi",
"lastName": "Cervera",
"age": 39,
"money": 124.36,
"contact": {
"address": "...",
"phone": 100100100,
"email": "[email protected]"
}
}
// Array-like table
data = [1, 50.5, "Hello", user]
When using array-like syntax to define the table, zero-based indexing is used for the keys of the table.
Then, we could use bracket indexing to retrieve the data. After the brackets, type suffixing should be used, to specify the type of the data retrieved:
userName = user["name"]$ + " " + user["lastName"]$
userAge = user["age"]%
userMoney = user["money"]#
userAddress = user["contact"]["address"]$
dataMessage = data[2]$
If type suffix is omitted, a table will be assumed, to allow for a more readable multi-index syntax.
Dot property syntax could also be considered:
userName = user.name$ + " " + user.lastName$
userAge = user.age%
userMoney = user.money#
userAddress = user.contact.address$
Assignment operations should also work with this syntax:
user.name = "John"
Indexing should be possible with both strings and integers.
Once this is added, the functions in the core library that fulfill the same role should be removed.
I should consider transpiling to JavaScript instead of Lua, so that Leafprograms run natively on the web and they could be run on the desktop by using Node.
An alternative approach for the desktop which is more interesting would be to package applications using Electron, or maybe use QuickJS.
Right now, only single like comments starting with '
are supported (something taken from BASIC). I should move to // ...
style single comments, and add multiline comments with /* ... */
syntax.
\a
: Bell
\b
: Backspace
\f
: Formfeed
\n
: New line
\r
: Carriage return
\t
: Tab
\v
: Vertical tab
\\
: Backslash
\"
: Double quotes
\'
: Single quotes
Use a more common syntax for defining type suffixes:
|i
-> :Int
|f
-> :Float
|s
-> :String
|l
-> :List
|h
-> :Dict
|r
-> :Ref
|w
-> :Raw
Right now, when a dict is indexed, all indices in the chain are assumed to operate on a dict. Same goes for lists. It should be possible to identify whether the last returned object was a list or a dict based on the type of the index used (int or string, respectively), so things like this should be allowed:
list = [{
"names": ["John", "Laura"]
}]
Print(list[0]["names"][1]|s)
Add support for loading external modules to the interpreter. These modules will be dynamic libraries with an entry point function InitModule which will receive a pointer to the following function:
int RegisterFunction(const char* name, int returnType, int paramTypes[]);
The builtin core module must be registered with this API (although inside the main binary instead of a separate lib).
If a function is already registered, replace it with the new version (make sure that they have the same parameters).
To load modules in the script, a #LOAD
preprocessor directive needs to be added.
While SUB is more common in BASIC languages, I prefer to use DEF, as done in Python and Ruby, for example.
Callable support was removed with the backend switch from Lua to C, but it should be added again (polymorphism support in the language depends on it).
Probably, a compiler flag could be defined to indicate that we don't want callable support, since disabling it should reduce exe size.
This will allow to call functions before their definition.
Instead of a scripting language, turn Leaf into a compiled language by transpiling to C and thus producing compact and fast executables.
Right now, weak references can be used (identified by the suffix @
), but memory management is manual in those (a function to deallocate the memory must be used). There should be a managed reference type (probably represented by the suffix ^
) which works in the same way as tables, with an automatic reference counting mechanism.
NOTE: Make sure that null
can be assigned to both managed and weak references.
It would make code more readable (especially if collapsing blocks into a single line) to adopt this Lua feature. With this, the header of an if
statements should be:
if condition then
A while
loop would be:
while condition do
And a for
loop:
for var = min to max do
Right now, numeric ids are being used to reference resources. I want to support a reference type defined with the @
suffix and the use of this in the core library.
Initially, !
suffix was considered, but @
would be better because expressions like var! = ...
look like the "not equal" expression from other languages.
The type of variable should be inferred if not specified with a suffix on initialisation, like here:
text = "Hello, world!"
The variable text
will be declared as a string.
With this change, type suffixes will not be allowed on assignment. When explicit type wants to be specified, it will be allowed to use type suffixes in an expression to be used for casting/conversion of the value (expressions won't be able to be casted from/to references and tables).
This would allow things like this:
text = 120$ // 120 is converted to string, so text contains "120"
number = "120"% // "120" is converted to int, so number contains 120
With the values 1 and 0 respectively.
Tables should be a specific type, and they should be garbage collected, while reference should have cleanup functions to deallocate the resource.
The !
symbol could be used as table suffix:
Foo(DimTable())
function Foo(table!)
// ...
end
Also, tables should use automatic reference counting. So the following code:
table = DimTable()
Should transpile to Lua as:
new_value = DimTable(); if table ~= new_value then _dec_ref(table); table = _inc_ref(new_value) end
And at the end of a function, all local tables except the one being returned should decrement their references.
UndimTable()
should be removed from the standard library, as they will be deleted automatically.
Current behavior:
"" and "" // false
"a" and "" // false
"" and "b" // false
"a" and "b" // true
"" or "" // false
"a" or "" // true
"" or "b" // true
"a" or "b" // true
Suggested behavior:
"" and "" // ""
"a" and "" // ""
"" and "b" // ""
"a" and "b" // "b"
"" or "" // ""
"a" or "" // "a"
"" or "b" // "b"
"a" or "b" // "a"
Currently, an empty string is generated, but it should output null
.
This is going to make the language more consistent, considering that table keys are case sensitive and that is has been proposed to allow dot access syntax in #15.
While :
was the usual statement terminator in BASIC languages, I prefer to use ;
, which is more universally used (also by Lua, which is a big inspiration).
To use |f
suffix and avoid confusion with |r
, that will be used for strong references.
Until now, it was the default if omitted.
It's longer than DEF, but it reads nicer to me.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.