Replicating syntaxes

Is it possible to replace luau syntaxes with different ones? Like turn this

local Message = "I am vanilla luau with no changes!"
local DoPrint = true

if DoPrint then
   print(Message)
end

to this

let Message = "I am luau but with different syntaxes!"
let DoPrint = true

if (DoPrint === true) { -- === is optional but it's a nice change to do too.
  print(Message)
}

In short, no, it’s not possible to replace the syntax of Luau with a completely different syntax. Luau has a specific syntax that is defined by the language designers, and changing it would require modifying the Lua parser, which is not recommended.

However, you can use a Lua transpiler that converts one syntax to another. A transpiler is a tool that takes code written in one programming language and converts it into code written in another programming language while preserving its functionality. There are some Lua transpilers available that can convert Lua code to JavaScript, TypeScript, and other languages.

For example, you can use a Lua-to-JavaScript transpiler like Moonscript or Luatrans to convert Luau code to JavaScript code. This would allow you to write Luau code using JavaScript-like syntax, but keep in mind that the resulting code may not be idiomatic and may not take advantage of Luau-specific features.

1 Like

Aw, thought it was possible due to how “open” luau is becoming and considering it’s open source too.

1 Like

Ah, I wish. You’re able to utilize luau of course, but sadly not replacing syntaxes.

That being said, Luau does allow for some flexibility in syntax. For example, it allows for the use of semicolons at the end of statements, even though they are not required. Additionally, Luau has some built-in syntactic sugar to make certain operations more convenient, such as the “and” and “or” operators. However, the core syntax of Luau is still required to be used.

1 Like

The syntactical sugars of Lua (not just Luau) are probably one of the most intriguing features the language has to offer and I encourage more people to try them out!

At some point, it will eventually make Lua(u) look like a different language, if that’s the intention of the OP. Here are some more examples:

--double-bracket string notation to make string stand out
debug.profilebegin [===[Cool looking banner]===]

--omitted parentheses for chained function calls
wrapper "a name for something" {"A table"} {"Another table"} [[A string]]

--recursive method call (method returns self), looks like an off-side rule language
object
    :method "thing"
    :method "another thing"
    :method "also a thing"

--Luau already looks like TypeScript; you can do some pretty crazy stuff
export type A<B, C, D...> = {D: (B, D...) -> (B, ...C)}
local foo: A<number, string, string, number> = {
	D = function(num: number, str: string, num2: number): (number, ...string)
		return 1, str, str.."hi"
	end
}
1 Like

This is a great example! However I do want to address a few things.

Syntactic Sugars

The statement that "Lua’s syntactic sugars are one of the most intriguing features of the language" is generally accepted by the Lua programming community. The use of syntactic sugar can make Lua code more readable and easier to write, especially for certain operations.

However, it’s important to note that syntactic sugar is an optional feature of the language and that Lua’s core syntax remains the same.

With that being said, the provided example does show how syntactic sugar “can make Lua code look different”, it’s important to remember that this is just a stylistic choice and NOT a replacement of Lua’s core syntax.

To get an idea

-- Example of using syntactic sugar to create a new table
local myTable = {
name = "John",
age = 30,
hobbies = {"reading", "swimming", "gaming"}
}

-- Example of using the core syntax to create a new table
local myTable = {}
myTable.name = "John"
myTable.age = 30
myTable.hobbies = {"reading", "swimming", "gaming"}

In the example above, we can see that both the core syntax and syntactic sugar are used to create a new table in Roblox Lua. The first example uses syntactic sugar to create a new table with pre-defined properties, while the second example uses the core syntax to create an empty table and then assigns properties to it.

The core syntax is still being used to define and assign properties to the table. Syntactic sugar is simply a way of writing code that is more concise and easier to read, but it still ultimately relies on the core syntax to function. While syntactic sugar can make certain operations more convenient, it is not always the best choice for every situation, and it may not be as flexible or fine-grained as the core syntax.

Previous Example

--double-bracket string notation to make string stand out
debug.profilebegin [===[Cool looking banner]===]

--omitted parentheses for chained function calls
wrapper "a name for something" {"A table"} {"Another table"} [[A string]]

--recursive method call (method returns self), looks like an off-side rule language
object
    :method "thing"
    :method "another thing"
    :method "also a thing"

--Luau already looks like TypeScript; you can do some pretty crazy stuff
export type A<B, C, D...> = {D: (B, D...) -> (B, ...C)}
local foo: A<number, string, string, number> = {
	D = function(num: number, str: string, num2: number): (number, ...string)
		return 1, str, str.."hi"
	end
}

While there are some instances of syntactic sugar being used, they are still ultimately relying on lua’s core syntax.

For example, the double-bracket string notation, which is a form of syntactic sugar, is still utilizing lua’s core syntax to define a string literal. Similarly, the omitted parentheses for chained function calls and the recursive method call are using syntactic sugar to make the code more readable, but they are still relying on the core syntax of the language to define and call functions.

Overall, the use of syntactic sugar in Lua is a matter of personal preference.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.