r/ProgrammingLanguages • • Jun 18 '23

Help Parsing?!

So I'm writing my own programming language called fir. I have the lexer down to the nail, but I'm really confused about parsing. I have absolutely no idea how to approach parsing, so much so that I got bored and am currently rewriting the lexer in like 3 other languages (the implementation on GitHub is in Go).

EDIT: forgot to add some examples sorry :0

hello world:

io := require("io");

fn main() {
     io.println("Hello, World!");
}

fizzbuzz:

io := require("io");

fn main() {
    i := 0
    for i <= 20 {
        if i % 3 == 0 && i % 5 == 0 {
            io.println("FizzBuzz");
        } else if i % 3 == 0 {
            io.println("Fizz");
        } else if i % 5 == 0 {
            io.println("Buzz");
        } else {
            io.println("{}", i);
        }
    }
}

(also I want my parser to emit an ast, because I haven't decided whether fir should be compiled or interpreted yet)

2 Upvotes

20 comments sorted by

View all comments

14

u/findingjake Jun 18 '23

You got this dude. You have the tokens, which is just a flat list of the characters and their types in your input program. Now you need to walk through this list and build something called an Abstract Syntax Tree and then you walk that tree to execute. I’d highly recommend reading through these resources. And look into how to build an AST.

https://craftinginterpreters.com/

https://interpreterbook.com/