Skip to content

Resolver

Riley Peel edited this page Jan 19, 2025 · 1 revision

What it does

The resolver's job is to walk through the AST and perform static analysis on it, meaning that it will look for errors such as: variables not existing, using a variable in it's own initializer, using return outside of a function, etc.

How it does it

The resolver inherits from Statement.Visitor and Expression.Visitor to walk through the AST. It keeps track of variable state per-scope as well as code context (e.g. global, block). It then uses the tracked state to determine if the code is invalid.

public object? VisitIdentifierNameExpression(IdentifierName identifierName)
{
    var scope = _scopes.LastOrDefault();
    var name = identifierName.Token.Text;
    if (scope != null && scope.TryGetValue(name, out var value) && value == false)
    {
        Diagnostics.Error(DiagnosticCode.H010, $"Cannot read variable '{name}' in it's own initializer", identifierName.Token);
        return null;
    }
    if (!IsDefined(identifierName.Token))
    {
        Diagnostics.Error(DiagnosticCode.H011, $"Cannot find name '{name}'", identifierName.Token);
        return null;
    }

    return null;
}
Clone this wiki locally