GithubHelp home page GithubHelp logo

javicervera / leaflang Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 419 KB

A super simple interpreted language inspired by Lua and BASIC.

License: GNU General Public License v3.0

CMake 0.42% Batchfile 0.10% Shell 0.10% C++ 77.04% C 22.34%

leaflang's People

Contributors

javicervera avatar

Watchers

 avatar  avatar

leaflang's Issues

Add list type

Using hashes as lists is slow, a specific list type should be added for convenience.

Rewrite core library in C

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.

Remove need for semicolon in some cases

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.

New type suffix syntax.

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 ref

Add #CONST preprocessor directive

Support 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 missing core functions: indexed tables and memory functions.

Add functions that handle tables with numeric indexes instead of string keys:

  • SetIndexInt
  • SetIndexFloat
  • SetIndexString
  • IndexInt
  • IndexFloat
  • IndexString

Add functions to handle memory blocks:

  • Dim
  • Undim
  • Redim
  • LoadDim
  • SaveDim
  • DimSize
  • PeekByte
  • PeekShort
  • PeekInt
  • PeekFloat
  • PeekString
  • PokeByte
  • PokeShort
  • PokeInt
  • PokeFloat
  • PokeString

Add #INCLUDE preprocessor directive

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.

Write lfmake

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.

Allow JSON syntax for table creation and bracket indexing.

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.

Transpile to JavaScript

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.

Use // and /* */ for comments

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.

Add escape sequences to strings

\a: Bell
\b: Backspace
\f: Formfeed
\n: New line
\r: Carriage return
\t: Tab
\v: Vertical tab
\\: Backslash
\": Double quotes
\': Single quotes

Rename type suffixes

Use a more common syntax for defining type suffixes:

  • |i -> :Int
  • |f -> :Float
  • |s -> :String
  • |l -> :List
  • |h -> :Dict
  • |r -> :Ref
  • |w -> :Raw

Allow mixing lists and dicts when indexing

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 module SDK

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.

Add Callable support

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.

Transpile to C

Instead of a scripting language, turn Leaf into a compiled language by transpiling to C and thus producing compact and fast executables.

Add managed references

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.

Add then and do keywords.

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

Use @ suffix for reference types

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.

Add type inference

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

Add type suffix for tables

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.

Change behaviour of "and" and "or".

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"

Make language case sensitive

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.