joncatanio / cannoli

Cannoli Programming Language
MIT License
771 stars 20 forks source link

Add Meta Data to AST nodes #5

Open joncatanio opened 6 years ago

joncatanio commented 6 years ago

Fields like lineno would be really beneficial to have in the AST so the compiler can report better error messages. Doing this in rust is a little more complicated since we can't pattern match structs and there isn't really a way to extend a class like one would in Java. We could apply traits to structs/enums but this only allows us to create similar functions not fields. The best way to achieve this is use a combination of enums and structs. An encapsulating struct can provide common fields with a variant field that represents the AST enum that is currently used.

enum AstNode {
   stmt: Statement,
   expr: Expression,
   ...
}

struct Node {
   lineno: usize,
   ...,
   variant: AstNode
} 

This is just a simple example that allows us to avoid adding common fields to each enum arm.