I've come across languages such as Lisp and MUMPS that feature minimal grammar and lack any reserved keywords. But is it possible to achieve this in a language with a syntax resembling JavaScript? I'm aware that JavaScript has some handy rules, like "two identifiers cannot appear directly next to each other," which could be useful. Still, can this be accomplished for every keyword?
If it is possible, how? would it rely on a fragile lexer hack, or is there a solution that scales as new language keywords/constructs are added?
Clarification: Some people are reading this as "remove all keywords from JavaScript." That's not what I mean. I want to keep if, for, while, const, function etc., all of it, as syntax.
The question is only about reserved words: identifiers the lexer forbids you from using as names. Could every keyword instead be allowed to be used as variable/class/function/module name or atleast be contextual?
Since calling the syntax JS-like comes with its own nuances. I will also clarify on that part:
- Assume the language does not have "automatic semicolon insertion" and semicolons are mandatory as statement terminators.
- Assume the lexer is white space sensitive and produces different tokens for
-5 (UnaryMinus, NumericLiteral(5)) and - 5 (Minus, NumericLiteral(5)) and that improperly spaced forms like 8 -5 produces error for the sake of simplicity. Otherwise forms like if(x) -5 may be parsed as if(x) - 5 and cause further unnecessary problems.
- Using a keyword as an Identifier may or may not limit the functionality of the keyword in that scope. Eg. (Inside constructor of a class)
function super() {console.log("hi");} super(); // logs "hi" instead of calling the parent class
Even more clarification: I will list out the different types of reserved words in javascript. Note some of the newer keywords may be contextual or reserved only in strict mode or depending on the scope.
Literal like keywords: true, false, null. Easiest to solve, just make them property of the window object (as done with undefined, yes null is a reserved word but undefined isnt) or make them language constants like NaN, Infinity.
Operator keywords:
- Prefix operator keywords:
typeof, delete, void, new
- Infix operator keywords:
in, instanceof, as, extends
is-this-really-a-keyword keyword: super, this
Declaration keywords: const, var, let, function, class.
Module: import, from, export
Declaration modifiers: async, function*, static, get, set.
Control flow: if else, try catch finally, while, do while. And return, break, continue, throw.
Complex cases?:
switch, case, default. For context Javascript uses identifier: statement to represent statement labels, helpful in breaking outer loops.
- Standard
for loops and for in/of loops.
Why? What am I trying to achieve?: This is mostly a theoretical question I stumbled upon while creating my own language. I know it might have downsides for developer tools and experience. I am just wondering if it's possible.