M
Modular6mo ago
Ethan

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
Muthu (முத்து)
quick question - how did you generate the parser in Mojo ? is there some kind of parser generator or just use recursive descent ?
Ethan
EthanOP6mo ago
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.
ModularBot
ModularBot6mo ago
Congrats @Ethan, you just advanced to level 6!
Muthu (முத்து)
thanks, thats awesome to know!
curiosity.fan
curiosity.fan6mo ago
@Ethan what language does your toy compiler compile ?
Ethan
EthanOP6mo ago
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.
sora
sora6mo ago
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.
struct TokenGenerator:
...

@staticmethod
fn `match`[reg: StringLiteral](s: String, pos: Int) -> Optional[String]:
try:
var re = Python.import_module("re")
var name = re.compile(reg)
return str(name.`match`(s, pos).group(0))
except e:
return None
struct TokenGenerator:
...

@staticmethod
fn `match`[reg: StringLiteral](s: String, pos: Int) -> Optional[String]:
try:
var re = Python.import_module("re")
var name = re.compile(reg)
return str(name.`match`(s, pos).group(0))
except e:
return None
Ethan
EthanOP6mo ago
I do want it to raise a TokenError when, for example, there is unmatch parens. Nice suggestion tho.
sora
sora6mo ago
If your error is not resumable, you could use abort for error reporting I guess
Want results from more Discord servers?
Add your server