fspl/design/analyzer.md

2.4 KiB

Analyzer

Package analyzer:

  • Ensures an AST is semantically correct
  • Fills in semantic information within the AST
    • Gives named types a pointer to the defined type they reference
    • Gives variables a pointer to the declaration they reference
    • Gives all expressions, including literals, a type even if it is void
    • Organizes keyed entities into maps
    • Checks to make sure there are no duplicate key names
  • Returns the resulting AST as a Tree

Types

Scoped

  • Variable (name string) entity.Declaration
  • AddVariable (entity.Declaration)

Functions, methods,

Tree

Tree acts as a semantic tree to contain entities from the entity package that have semantic information filled in.

Data

  • Raw map of names -> types
  • Raw map of names -> functions
  • Raw map of type.names -> methods
  • Completed map of names -> types
  • Completed map of names -> functions
  • Scope breadcrumb trail
  • Every expression is assigned a Type when its type is determined
  • Methods are moved into a map within their types

Methods

Analyze(parser.Tree) error

Analyze takes in an AST and analyzes it within its own context. If this method is called multiple times, it will parse all of the trees as if they were one. This method returns an error if the tree has a semantic error and cannot be turned into a proper semantic tree.

First, add all top level AST declarations to quick access maps and make sure their names are unique.

For each top level declaration, call one of the analysis functions with its name.

pushScope (Scoped)

Pushes a scope onto the scope trail

popScope ()

Removes the last scope from the scope trail

variable (string) entity.Declaration

Returns a named variable, and nil if it doesn't exist. Goes through all scopes from the top of the trail to the bottom until it finds one.

addVariable (string, entity.Declaration)

Adds a variable to the top scope.

analyzeTypedef
  • If already analyzed return what has been analyzed
  • Get typedef from raw map
  • Analyze type
  • Store analyzed type in completed map
analyzeFunction
  • If already analyzed return what has been analyzed
  • Get function from raw map
  • Analyze signature
  • Analyze expression
  • Store analyzed function in completed map
analyzeMethod
  • Analyze type
  • If already analyzed return what has been analyzed
  • Get method from raw map
  • Analyze signature
  • Analyze expression
  • Store analyzed method in type