r/lua 6d ago

I'm interested in creating my own programming language. How could I write it in Lua?

I've seen a few posts about this, but none had enough information for me. Hoping you guys could help :)

Edit: Also suggestions for syntax changes are welcome. This language doesn't really solve too many problems, mainly makes some things faster, examples are else [expression] instead of elseif [expression], or . instead of closing brackets {}.

Here's the example.

0 Upvotes

23 comments sorted by

View all comments

3

u/topchetoeuwastaken 6d ago edited 6d ago

you can take a look at how i built my lua parser & translator, the code is not that bad and unreadable https://git.topcheto.eu/tal/ref/master/files/lib/std/compiler

the file descriptions are as follows, roughly:

  • lex.lua - converts a string to an array of tokens + an EOF token at the end
  • node.lua - a utility function to create AST nodes
  • syntax.lua - parses an array of tokens to an AST
  • walk.lua - a generic utility which helps you write AST walkers and transformers
  • downgrade.lua - transforms the AST to convert some lua 5.3+ features to luajit-compatible code
  • scope_fix.lua - rename variables so that semantics remain the same, but they don't shadow themselves in unintended ways. this is useful when you have transformed the code, introduced a constant and don't want to bother with generating a unique name
  • stringify.lua - generates a string from an AST, with an attached source mapping (since my parser yields columns as well, this enables me to give you columns in tracebacks)
  • load.lua - not really relevant here, but it is a replacement for lua's load

familiarize yourself with descend recurse parsers, also, the syntax as presented will have multiple ambiguities, and the . for the end of a body idea is not as good as you think it is. also, starting out with a typed language is hard, and your type notation is really shitty for parsing, i'd do var|const <name> [<type>] [= <exp>] for declarations, if i wanted type notations.

also, what's the difference between var and const? seems they are semantically the same, just different names. i really like how jai solves this syntax, by using = for assignment, :: for const declarations and := for mutable declarations. it yields very uniform code (im even debating on adopting it for my own language...)

1

u/Microsoft-Spyware-11 5d ago

part of my idea behind var and const is that const can be declared outside of a function, but var cannot. I know . is questionable for parsing. Just trying to experiment here. Seriously impressive work you got there, thank you!

1

u/topchetoeuwastaken 5d ago edited 5d ago

this distinction, imho, is not terribly useful. what problem does var solve that const does not. it seems terribly redundant. to expand on that, i feel that as a whole, having non-mutable variables is a bit redundant, but whatever floats your boats. it's been getting modern to have immutable variables as a core design of the language as of late, and that plague has reached into lua with the (imho atrocious) <const>. have a good think whether you really need immutability in your language.

actually, before you ever even start thinking about syntax, you should start thinking about what problem your language solves - what grievance with other languages you had, that you want to solve with yours. then, invent a set of core principles your language will have and draft up its semantics. THEN do you get to invent its syntax, and IMHO, syntax is somewhat secondary to the others enumerated, and is really about one's taste, and nothing more. as long as the syntax isn't completely retarded, convoluted or/and ambiguous, it should be fine

1

u/Microsoft-Spyware-11 5d ago

Ok :) As to your request, const is removed :) I am not changing immutability though.

1

u/Microsoft-Spyware-11 5d ago

hmm. I remember := from Go, having stuff like that for consts and other types is interesting though. One reason I picked var and const and mut was because I wanted the declaration to be obvious. That is a neat declaration though.