Sol - A C-Like Language that Compiles to Lua

Started by NihilisticPuffin, Oct 18, 2023, 06:25 AM

Previous topic - Next topic

Should table indexing in Sol start at 0 or 1

Zero (0)
2 (100%)
One (1)
0 (0%)

Total Members Voted: 2

Sol - A C-like language inspired by JavaScript that compiles to Lua

Disclaimer: The current version of Sol is more a proof of concept than an complete language. Future versions will bring more features and better documentation

Comments:
    Single line comments start with '//' and continue until the end of the line
    Multi line comments start with '/*' and end with '*/'

Literals
  • Numbers are written with or without decimals: 100, 1.6, 0.9
  • Strings are text, written within double or single quotes: 'Hello, World!', "Hello, World!"

Functions
    Functions are declared using the 'fn' keyword
fn sum(a, b) {
    return a+b
}


GitHub

Install:
wget run https://raw.githubusercontent.com/NihilisticPuffin/Sol/main/installer.lua

Usage:
sol.lua input.sol [-o output] [--log]

Example Sol Code
// C Style Comments
/*
    And Block Comments too
*/
local name = 'Allen'
age = 21
fn makeGreeting() {
    // Supports String Interpolation
    return "Hello, my name is ${name} and I am ${age} years old!"
}
greeting = makeGreeting()
print(greeting)

Lua Output
local name = 'Allen'
age = 21
function makeGreeting()
    return string.format("Hello, my name is %s and I am %s years old!", name, age)
end
greeting = makeGreeting()
print(greeting)

Added Ternary Operator:
// Sol Code
a = 6
b = a == 5 ? 'A is 5' : 'A is not 5'

-- Lua equivalent
a = 6
b = (function() if a == 5 then return 'A is 5' else return 'A is not 5' end end)()

  • Added Table Support
  • Added default parameters ( fn func(param = 7) {} )

Example:
tbl = {5, 7}

fn say(msg = "Hello") {
    print(msg)
}

say() // Prints "Hello"
say("How are you?") // Prints "How are you?"


Added operator overloading

Example:
operator + (l, r) {
    if (type(l) == 'string' || type(r) == 'string'){
        return '${l}${r}'
    }
    return l+r
}

print(type(5+6), 5+6)     // number 11
print(type('5'+6), '5'+6) // string 56

  • Added #import preprocessor directive
  • Added #define preprocessor directive

Examples:
//math.sol
PI = 3.14

// Sol
#import 'math.sol'
#define µ ' * 0.000001'
print(PI)
if var < 5µ {}

-- Lua
PI = 3.14
print(PI)
if var < 5 * 0.000001 then
end