Almost completed phase 2 of the Semantic Analysis
Today I built a wide variety of things. But 3 specific features.
-
visit(node: StatementNode): void
This function parses all of my nodes. It covers variable assignment, sprite declaration, etc etc. It has many jobs, including entering scopes, declaring bodies, and following each AST-node stack recursively. Some are left stubbed out, as I spent most of my time on #2: -
inferType(expression: ExpressionNode): InternalType
This function is ~200loc. It covers a wide variety of cases, all pertaining to the type inference of an expression AST. It not only infers the type, but recursively builds an internal model of what the user has written. So a type oflist<dict<num, str>>becomes the stored internal representation of:
{
kind: "list",
element: {
kind: "dict",
key: {
kind: "primitive",
name: "num"
},
value: {
kind: "primitive",
name: "str"
}
}
}
Though it is a lot more complex than that, that’s what it boils down to. And lots and lots and lots of error statements.
-
InternalTypesitself
I built a system that stores the type state, with a few extra helper functions: isAssignable and typeToString. The former recursively traces both types and ensures total match. This means that a variable of typetocould be assigned to a value of typefrom. The latter is just a pretty-print function for pretty errors to see what types were being looked at/expected at certain parts of the code.
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.