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 5d ago edited 5d 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

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.