Yoho 🔥: A toy compiler written in mojo
Hello Mojocians. I'm writing a toy compiler in mojo.
https://github.com/YichengDWu/yoho
GitHub
GitHub - YichengDWu/yoho: A compiler written in Mojo 🔥 and generate...
A compiler written in Mojo 🔥 and generates RISC-V assembly - YichengDWu/yoho
9 Replies
quick question - how did you generate the parser in Mojo ? is there some kind of parser generator or just use recursive descent ?
Yes there's a parser generator, see https://github.com/YichengDWu/yoho/blob/main/src/grammar/parser_generator.mojo. The algorithm is called Packrat parsing and very similar to recursive decent.
When I need to update the grammar I modify the test/calc.gram file and run 'make parser' defined in Makefile.
Congrats @Ethan, you just advanced to level 6!
thanks, thats awesome to know!
@Ethan what language does your toy compiler compile ?
I want it to compile mojo so that the tokenizer and parser can be something useful to the mojo community. But to be clear it will only compile a subset of mojo like C.
You could introduce a
match
function, replace raise
with abort
so you can remove raises
(almost) everywhere. The errors are never caught anyways, and it doesn't make sense to have funcs returning optionals also raise.
I do want it to raise a TokenError when, for example, there is unmatch parens. Nice suggestion tho.
If your error is not resumable, you could use
abort
for error reporting I guess