Quantcast
Channel: Question and Answer » trees
Viewing all articles
Browse latest Browse all 36

Clean Abstract Syntax Tree

$
0
0

I’m writing a toy compiler for fun.

Basically, my problem is that I don’t want to clutter the AST with stuff like debug information (symbol tokens, locations of tokens, etc) as well as data that the semantic analyzer computes.

For example, the semantic analyzer does some type inference and the result type is stored back into the Node type. It looks something like this:

/// A variable declaration in my language looks like this:
/// var x = 10
struct VarDec: Statement {
    var varKeyword: Keyword
    var varName: Id
    var assignment: Symbol
    var initializer: Expr
    var type: Optional<Type>
}

The main problem here is that now the node type has a state, type inference has either been done or not. This makes it hard to reason about. The other thing is that the whole AST gets cluttered with those tokens that are only really needed for error messages.

It has been suggested that I could create another representation of the AST just for semantic analysis that links back to the AST but that seems like a lot of work and redundant code…

Does anybody have an idea how I can clean up this code without having to create multiple trees?


Viewing all articles
Browse latest Browse all 36

Trending Articles