Writing a Programming Language From Scratch - Part 3
In the previous post on this series, we went over parsing let declarations (let <name> = <expr>) and wrapping expressions in statements. This set us up for extending our grammar further - in this part, we’ll add identifier expressions then compile our AST to bytecode and execute it in a stack VM. Alright, let’s move on to adding our new AST node type, VarExpr: /// ast.hpp struct Expr { virtual ~Expr() = default; }; struct VarExpr : Expr { std::string name; explicit VarExpr(std::string name) : name { std::move(name) } { } }; struct Literal : Expr { ... }; Here we go, nice and simple. VarExpr owns a std::string which refers to an identifier in the source code. Let’s parse this now, but where should we do it? Since it’s an expression, and we’re expecting the token Ident, it should be handled in parse_primary: ...