├── COMPARISON.md ├── LICENSE ├── README.md ├── pom.xml └── src ├── main ├── antlr4 │ └── me │ │ └── saharnooby │ │ └── luajssyntax │ │ └── LuaJSSyntax.g4 └── java │ └── me │ └── saharnooby │ └── luajssyntax │ ├── LuaJSToLua.java │ ├── LuaJSToLuaConverter.java │ ├── Main.java │ ├── exception │ └── InvalidSyntaxException.java │ └── util │ ├── HashUtil.java │ └── Printer.java └── test ├── java └── me │ └── saharnooby │ └── luajssyntax │ ├── ConverterTest.java │ ├── ErrorTest.java │ ├── OptimizationTest.java │ ├── TestGenerator.java │ └── util │ └── HashUtilTest.java └── resources ├── me └── saharnooby │ └── luajssyntax │ ├── ArrowFunctionLiteral.txt │ ├── Bitwise.txt │ ├── Break.txt │ ├── ChainCalls.txt │ ├── Comparison.txt │ ├── Compound.txt │ ├── Concat.txt │ ├── Continue.txt │ ├── DoWhile.txt │ ├── For.txt │ ├── ForIn.txt │ ├── ForOf.txt │ ├── ForOptimized.txt │ ├── FunctionLiteral.txt │ ├── Globals.txt │ ├── If.txt │ ├── Increment.txt │ ├── Label.txt │ ├── Lists.txt │ ├── Literals.txt │ ├── Locals.txt │ ├── Logic.txt │ ├── Math.txt │ ├── Numbers.txt │ ├── OOP.txt │ ├── Parenthesis.txt │ ├── Power.txt │ ├── Priorities.txt │ ├── PrioritiesBitwise.txt │ ├── PrioritiesUnary.txt │ ├── Require.txt │ ├── Return.txt │ ├── Strings.txt │ ├── Tables.txt │ ├── Ternary.txt │ ├── Throw.txt │ ├── TryCatch.txt │ └── Unary.txt └── test_module.lua /COMPARISON.md: -------------------------------------------------------------------------------- 1 | # Comparison with vanilla Lua 2 | 3 | Note that comments and indents are not preserved during the conversion, they were added to 4 | the Lua code manually. 5 | 6 |
LuaJS | Lua (converted and formatted) |
---|---|
9 | 10 | ```javascript 11 | // One-line comment 12 | /* Multi-line 13 | comment */ 14 | 15 | // Semicolons are optional 16 | print('semicolon'); 17 | print('no semicolon') 18 | 19 | // Global variables 20 | gx, gy = 1, 2 21 | gz = 3 22 | 23 | // Local variables 24 | let lx, ly = 4, 5 25 | 26 | // Boolean literals and nil literal 27 | let literals = [nil, true, false] 28 | 29 | // String literals 30 | let sx = "string with escaped characters:\r\n \u0123 \x01 \0" 31 | let sy = 'single quotes\r\n' 32 | 33 | // String concatenation 34 | // Non-string values are automatically wrapped into tostring 35 | let concatResult = 'values are: (' .. [1, 2, 3] .. ', ' .. 123 .. ')' 36 | 37 | // Lists 38 | let list = [1, 2, ['nested list', 3, 4]] 39 | 40 | // Tables 41 | let table = {key: 'value', nested: {x: 1, y: 2}, ['expression' .. ' as a key']: 3} 42 | ``` 43 | 44 | | 45 |46 | 47 | ```lua 48 | -- One-line comment 49 | --[[ Multi-line 50 | comment ]]-- 51 | 52 | -- Semicolons are optional 53 | print('semicolon'); 54 | print('no semicolon') 55 | 56 | -- Global variables 57 | gx, gy = 1, 2 58 | gz = 3 59 | 60 | -- Local variables 61 | local lx, ly = 4, 5 62 | 63 | -- Boolean literals and nil literal 64 | local literals = {nil, true, false} 65 | 66 | -- String literals 67 | local sx = "string with escaped characters:\r\n ģ \x01 \0" 68 | local sy = 'single quotes\r\n' 69 | 70 | -- String concatenation 71 | -- Non-string values are automatically wrapped into tostring 72 | local concatResult = 'values are: (' .. tostring({1, 2, 3}) .. ', ' .. tostring(123) .. ')' 73 | 74 | -- Lists 75 | local list = {1, 2, {'nested list', 3, 4}} 76 | 77 | -- Tables 78 | local table = {key='value', nested={x=1, y=2}, ['expression' .. ' as a key']=3} 79 | ``` 80 | 81 | | 82 |
LuaJS | Lua (converted and formatted) |
---|---|
90 | 91 | ```javascript 92 | function literalFunction() { 93 | return 1 94 | } 95 | 96 | function literalFunctionWithArgs(x, y) { 97 | return x + y 98 | } 99 | 100 | // Arrow functions 101 | let f = () => 1 102 | 103 | let fWithArg = x => x * 10 104 | 105 | let fBlock = x => { 106 | return x * 10 107 | } 108 | 109 | let fWithTwoArgs = (x, y) => x + y 110 | ``` 111 | 112 | | 113 |114 | 115 | ```lua 116 | function literalFunction() 117 | return 1 118 | end 119 | 120 | function literalFunctionWithArgs(x, y) 121 | return x + y 122 | end 123 | 124 | -- Arrow functions 125 | local f = function() return 1 end 126 | 127 | local fWithArg = function(x) return x * 10 end 128 | 129 | local fBlock = function(x) 130 | return x * 10 131 | end 132 | 133 | local fWithTwoArgs = function(x, y) return x + y end 134 | ``` 135 | 136 | | 137 |
LuaJS | Lua (converted and formatted) |
---|---|
145 | 146 | ```javascript 147 | // if statement, while loop and break statement 148 | let counter = 10 149 | while (true) { 150 | print(counter) 151 | 152 | counter-- 153 | 154 | if (counter == 0) { 155 | break 156 | } 157 | } 158 | 159 | // do-while loop 160 | counter = 10 161 | do { 162 | print(counter) 163 | 164 | counter-- 165 | } while(counter > 1) 166 | 167 | // continue statement and for loop (initialization; condition; action after iteration) 168 | for (let i = 0; i < 10; i++) { 169 | print('i is ' .. i) 170 | 171 | if (i % 2 != 0) { 172 | continue 173 | } 174 | 175 | print('i is even') 176 | } 177 | 178 | 179 | 180 | 181 | // for-in loop 182 | let table = {a: 1, b: 2, c: 3} 183 | 184 | for (k in pairs(table)) { 185 | print('key only: ' .. k) 186 | } 187 | 188 | for (k, v in pairs(table)) { 189 | print('key and value: ' .. k .. ', ' .. v) 190 | } 191 | 192 | // for-of loop, iterates over ipairs 193 | let list = [1, 2, 3] 194 | 195 | for (v of list) { 196 | print('element is ' .. v) 197 | } 198 | 199 | for (v, i of list) { 200 | print('element is ' .. v .. ', index is ' .. i) 201 | } 202 | 203 | // Labels and goto 204 | let result = 10 205 | while (true) { 206 | if (result == 1) { 207 | goto someLabel 208 | } 209 | 210 | result-- 211 | } 212 | 213 | someLabel: print(result) 214 | ``` 215 | 216 | | 217 |218 | 219 | ```lua 220 | -- if statement, while loop and break statement 221 | local counter = 10 222 | while (true) do 223 | print(counter) 224 | 225 | counter = counter - 1 226 | 227 | if (counter == 0) then 228 | break 229 | end 230 | end 231 | 232 | -- do-while loop 233 | counter = 10 234 | repeat 235 | print(counter) 236 | 237 | counter = counter - 1 238 | until (not (counter > 1)) 239 | 240 | -- continue statement and for loop (initialization; condition; action after iteration) 241 | do 242 | local i = 0; 243 | while (i < 10) do 244 | print('i is ' .. tostring(i)) 245 | if (i % 2 ~= 0) then 246 | goto continueLabel 247 | end 248 | print('i is even') 249 | ::continueLabel:: 250 | i = i + 1 251 | end 252 | end 253 | 254 | -- for-in loop 255 | local table = {a=1, b=2, c=3} 256 | 257 | for k in pairs(table) do 258 | print('key only: ' .. tostring(k)) 259 | end 260 | 261 | for k, v in pairs(table) do 262 | print('key and value: ' .. tostring(k) .. ', ' .. tostring(v)) 263 | end 264 | 265 | -- for-of loop, iterates over ipairs 266 | local list = {1, 2, 3} 267 | 268 | for _, v in ipairs(list) do 269 | print('element is ' .. tostring(v)) 270 | end 271 | 272 | for i, v in ipairs(list) do 273 | print('element is ' .. tostring(v) .. ', index is ' .. tostring(i)) 274 | end 275 | 276 | -- Labels and goto 277 | local result = 10 278 | while (true) do 279 | if (result == 1) then 280 | goto someLabel 281 | end 282 | result = result - 1 283 | end 284 | 285 | ::someLabel:: 286 | print(result) 287 | ``` 288 | 289 | | 290 |
LuaJS | Lua (converted and formatted) |
---|---|
298 | 299 | ```javascript 300 | try { 301 | if (1 > 10) { 302 | throw 'unexpected math behavior!' 303 | } 304 | } catch (e) { 305 | print('some error has occurred: ' .. e) 306 | } 307 | 308 | 309 | 310 | 311 | ``` 312 | 313 | | 314 |315 | 316 | ```lua 317 | do 318 | local res_c3409912_0, e_c3409912_0 = pcall(function() 319 | if (1 > 10) then 320 | error('unexpected math behavior!') 321 | end 322 | end) 323 | if not res_c3409912_0 then 324 | local e = e_c3409912_0 325 | print('some error has occurred: ' .. tostring(e)) 326 | end 327 | end 328 | ``` 329 | 330 | | 331 |
LuaJS | Lua (converted and formatted) |
---|---|
339 | 340 | ```javascript 341 | // Math operators: +, -, *, /, %, ** (power operator) 342 | let mathResult = 100 + 3 ** 2 343 | 344 | // Logical operators: &&, || 345 | let conditionA = 10 > 1 346 | let conditionB = 1 != 3 347 | if (conditionA && conditionB) { 348 | print('all ok') 349 | } 350 | 351 | // Comparison operators: >, <, >=, <=, ==, != 352 | if (1 != 10) { 353 | print('all ok') 354 | } 355 | 356 | // Bitwise operators: & (and), | (or), ^ (xor), << (shift to left), >> (shift to right) 357 | // This evaluates to 5: 358 | let bitwiseResult = 1 & 2 | 4 359 | 360 | // Unary operators: - (negation), ! (logical negation), ~ (bitwise not), # (length operator) 361 | let notResult = ~4 362 | let length = #[1, 2, 3] 363 | 364 | // Compound assignment statements (works with all operators) 365 | let compound = 100 366 | compound *= 10 367 | compound += 5 368 | 369 | // Increment and decrement statements 370 | let incremented = 1 371 | incremented++ 372 | 373 | // Ternary operator 374 | let ternaryResult = 2 > 1 ? 'two is more than one' : 'something is wrong' 375 | ``` 376 | 377 | | 378 |379 | 380 | ```lua 381 | -- Math operators: +, -, *, /, %, ** (power operator) 382 | local mathResult = 100 + 3 ^ 2 383 | 384 | -- Logical operators: &&, || 385 | local conditionA = 10 > 1 386 | local conditionB = 1 ~= 3 387 | if (conditionA and conditionB) then 388 | print('all ok') 389 | end 390 | 391 | -- Comparison operators: >, <, >=, <=, ==, != 392 | if (1 ~= 10) then 393 | print('all ok') 394 | end 395 | 396 | -- Bitwise operators: & (and), | (or), ^ (xor), << (shift to left), >> (shift to right) 397 | -- This evaluates to 5: 398 | local bitwiseResult = bit32.bor(bit32.band(1, 2), 4) 399 | 400 | -- Unary operators: - (negation), ! (logical negation), ~ (bitwise not), # (length operator) 401 | local notResult = bit32.bnot(4) 402 | local length = #{1, 2, 3} 403 | 404 | -- Compound assignment statements (works with all operators) 405 | local compound = 100 406 | compound = compound * 10 407 | compound = compound + 5 408 | 409 | -- Increment and decrement statements 410 | local incremented = 1 411 | incremented = incremented + 1 412 | 413 | -- Ternary operator 414 | local ternaryResult = (2 > 1) and ('two is more than one') or ('something is wrong') 415 | ``` 416 | 417 | | 418 |
LuaJS | Lua (converted and formatted) |
---|---|
426 | 427 | ```javascript 428 | SomeClass = {} 429 | 430 | function SomeClass::new() { 431 | object = {} 432 | self.__index = self 433 | return setmetatable(object, self) 434 | } 435 | 436 | function SomeClass::printSomething() { 437 | print('something') 438 | } 439 | 440 | let someObject = SomeClass::new() 441 | someObject::printSomething() 442 | ``` 443 | 444 | | 445 |446 | 447 | ```lua 448 | SomeClass = {} 449 | 450 | function SomeClass:new() 451 | object = {} 452 | self.__index = self 453 | return setmetatable(object, self) 454 | end 455 | 456 | function SomeClass:printSomething() 457 | print('something') 458 | end 459 | 460 | local someObject = SomeClass:new() 461 | someObject:printSomething() 462 | ``` 463 | 464 | | 465 |
LuaJS | Lua |
---|---|
29 | 30 | ```javascript 31 | /* A simple division function. */ 32 | let divide = (x, y) => { 33 | if (y == 0) { 34 | throw "Divisor is zero" 35 | } 36 | return x / y 37 | } 38 | 39 | try { 40 | print('Result is ' .. divide(10, 0)) 41 | } catch (e) { 42 | print('Error: ' .. e) 43 | } 44 | 45 | ``` 46 | 47 | | 48 |49 | 50 | ```lua 51 | --[[ A simple division function. ]]-- 52 | local divide = function(x, y) 53 | if (y == 0) then 54 | error("Divisor is zero") 55 | end 56 | return x / y 57 | end 58 | 59 | local res, e = pcall(function() 60 | print('Result is ' .. tostring(divide(10, 0))) 61 | end) 62 | if not res then 63 | print('Error: ' .. tostring(e)) 64 | end 65 | ``` 66 | 67 | | 68 |