A preprocessor that adds support for OOP, lambda, and some more upcoming things.
Syntax
Downloads
Differences between this and the old version:
- No foreach, since Lua has it's own
- None of it's own APIs, that's not something a preprocessor should have
- Classes with no constructors are non-instantiable
- No switch statement (yet)
old deprecated version
Lua Preprocessor
This preprocessor adds support for object-oriented programming, and more features.
Cons of using this preprocessor
I would like to name this project somehow, so please give me some ideas!
Lua Preprocessor
This preprocessor adds support for object-oriented programming, and more features.
Syntax
The syntax is the same as in Lua except for a few exceptions shown in this spoiler.
Ternary Operator
Instead of writing
x == 5 and "yes" or "no"the syntax has been changed to be identical to the C ternary operator.
OOP
A couple of new keywords have been added for object-oriented programming.
To create a class, use the "class" keyword:
Same for variables.
Inheritance is simple, too:
When creating instances, the constructor of each derived class will be called in the order that you specified when declaring the class before your own constructor. When creating instances, the constructor of the derived class will be called first.
Only one constructor for each class is allowed. And only one class in "extending" is allowed.
To create an instance of a class, call it like a function (just like in Python).
To create a class, use the "class" keyword:
class Test contains endBetween the "contains" and "end" key you may add functions how you would normally do with an optional "static" prefix for static functions.
Same for variables.
Inheritance is simple, too:
class Sub extending Super contains end
Only one constructor for each class is allowed. And only one class in "extending" is allowed.
To create an instance of a class, call it like a function (just like in Python).
class Test contains static SIMPLE_STR = "5"; constructor(argOne, argTwo) end static function addFive(str) return str .. Test.SIMPLE_STR; end end instance = Test("arg one", "arg two");
Includes
To include either a Lua file or a file for this preprocessor, use this:
This Lua preprocessor has some builtin includes that will be shown later.
From beta 1.15, if you ever need to use a function called "include", just put brackets and the compiler will parse it like a normal function.
include "simpleFile.lua";This can either be a relative path (That does NOT start with ./) or an absolute path.
This Lua preprocessor has some builtin includes that will be shown later.
From beta 1.15, if you ever need to use a function called "include", just put brackets and the compiler will parse it like a normal function.
Lambda
Lambda in this language is the same as in Python:
lambda argOne, argTwo: argOne + argTwo;First there is a "lambda" keyword, then the list of arguments, the colon and then an expression.
instanceof
If you want to find out whether or not a variable value is an instance of a certain class, use this:
myVariable instanceof SomeClassNote that this does not work across superclasses.
switch
The switch statement is a little different from C specification.
The above code is compiled into:
The switch statement is a little different from C specification.
switch someExpression case 1+2 then print("it's three"); case 4 then print("four"); default then print("its nor three nor four"); endOnly one case will be called. If none are called then the default case is called.
The above code is compiled into:
function _mlua_switch(t) t.case=function(self,x) local f=self[x] or self.default; if f then if type(f)=="function" then f(x,self) else error("case "..tostring(x).." not a function") end end end return t end (_mlua_switch{ [3.0]=function()print("it's three"); end, [4.0]=function()print("four"); end, default=function()print("its nor three nor four"); end, }):case(someExpression);Note (shown in the compiled version) that the cases are evaluated first.
foreach
Oh yeah, and this Lua uses semi-colons.Finally, we don't have to do that pairs() and key value nonsense.
foreach varName in someTable do end
Optimization
This preprocessor optimizes wherever it can, for an example:
x = 5 + 2; -- the above code becomes: x = 7;However the optimization feature bugs out a bit when using parentheses.
How to Compile
Download the compiler below and open up your terminal of choice.
Use this command:
The compiler will then spew out the pure Lua version. To redirect the output to a file, simply add
Replace compiled.lua with whatever name you choose.
Use this command:
java -jar preprocessor.jar -f thefile.luaReplace preprocessor.jar with the name of the compiler, and thefile.lua with your source file path.
The compiler will then spew out the pure Lua version. To redirect the output to a file, simply add
> compiled.luato the end of the command above. This will work in both UNIX-based systems and Windows.
Replace compiled.lua with whatever name you choose.
APIs
String
Added: Beta 1.1
Added: Beta 1.1
include "string"; local stringBuilder = StringBuilder(); -- You can pass a string to initialize it stringBuilder:add("Hello, "):add("World"); -- Chained commands are allowed stringBuilder:addAt(13, "!"); -- Add at a specific location stringBuilder:charAt(1); -- Get character at index (In this case "H") stringBuilder:length(); -- Get the length of a string stringBuilder:slice(2, 5); -- Substring (both indexes inclusive, so "ello")
Stream
Added: Beta 1.15
Added: Beta 1.15
include "stream"; local stream = Stream({1, 4, 2, 3}); -- Only works on array-tables stream = stream:map(lambda x: x * 3); -- Triple everything in the stream stream = stream:filter(lambda x: x % 2 == 0); -- Remove all odd values (leave all even ones) stream:forEach(lambda x: print(x)); -- Print all values stream:getCount(); -- Get amount of items in this stream stream:getArray(); -- Returns the internal array in this stream
Download
Beta 1.15: https://www.mediafir...r8dp4ykcp55s6u8
Beta 1.14: http://www.mediafire...htaslua1.14.jar
Beta 1.13: https://www.mediafir...2f4j5d8wbk8czpb
Beta 1.12: https://www.mediafir...gy58677g3knf5ga
Beta 1.1: https://www.mediafir...38qdwtorwmngdfm
Beta 1.0: http://www.mediafire...dluaproject.jar
Beta 1.14: http://www.mediafire...htaslua1.14.jar
Beta 1.13: https://www.mediafir...2f4j5d8wbk8czpb
Beta 1.12: https://www.mediafir...gy58677g3knf5ga
Beta 1.1: https://www.mediafir...38qdwtorwmngdfm
Beta 1.0: http://www.mediafire...dluaproject.jar
Changelog
Documentation: hereBeta 1.1:
- Added instanceof keyword.
- Disallow variable names that start with _ool_.
- Added foreach keyword
- Fixed include issue
Beta 1.12:
- Identifiers can now have numbers
- Function names may now contain dots (someVar.someOtherVar()).
Beta 1.13:
- setVar context now uses funcCallArgs and funcDeclArgs instead of varHeader.
- Relative paths are now allowed in the -f argument.
Beta 1.15:
- Class names can now contain dots and other things
- Added Stream API
- Documented all APIs
- Added instanceof keyword.
- Disallow variable names that start with _ool_.
- Added foreach keyword
- Fixed include issue
Beta 1.12:
- Identifiers can now have numbers
- Function names may now contain dots (someVar.someOtherVar()).
Beta 1.13:
- setVar context now uses funcCallArgs and funcDeclArgs instead of varHeader.
- Relative paths are now allowed in the -f argument.
Beta 1.15:
- Class names can now contain dots and other things
- Added Stream API
- Documented all APIs
Cons of using this preprocessor
- Hard to debug. The compiled result is all compressed into one line. Due to this, it is hard to debug.
I would like to name this project somehow, so please give me some ideas!
Edited by Midnightas, 19 November 2017 - 10:26 AM.