fspl/design/compiler.md

3.1 KiB

Main

Package main:

  • Parses CLI arguments
  • Displays help
  • Compiles file(s)
  • Displays any errors in a readable way

Subroutines

Compile (inputFiles ...string, outputFile string) error

  • Create AST
  • For each input file:
    • Feed file into AST
    • If error, print error and terminate
  • Feed AST from parser into analyzer
  • If error, print error and terminate
  • Open temporary IR output pipe
  • Feed semantic tree from analyzer and the output pipe into generator
  • If error, print error and terminate
  • Invoke LLVM on temporary IR output pipe, instruct it to output to output file
  • Close termporary IR output pipe

Entity

Package entity defines types to represent language entities, as well as some convenience methods for dealing with them.

Types

  • TopLevel
    • Function
    • Typedef
    • Method
  • Type
    • TypeNamed
    • TypePointer
    • TypeArray
    • TypeStruct
    • TypeInterface
  • Expression
    • LiteralInt
    • LiteralFloat
    • LiteralArray
    • LiteralStruct
    • Variable
    • Declaration
    • Call
    • Subscript
    • Dereference
    • Reference
    • ValueCast
    • BitCast
    • Operation
    • Block
    • MemberAccess
    • IfElse
    • Loop
    • Break
    • Return
    • Assignment
  • Member
  • Signature

Parser

Package parser parses data in an input io.Reader into an AST.

Types

Tree

Tree acts as an AST to contain top-level declarations from the entity package.

Methods

Parse(name string, file io.Reader) error

Parse takes in a file name, and an io.Reader and parses its contents into the tree. The name is only used for error reporting purposes, this method does not open any files. It returns an error if the syntax read from the input reader is erroneous and cannot be parsed.

ParseFile(name string) error

ParseFile is like Parse, except it automatically opens and closes the file specified by the given name.

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

Tree

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

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.

Generator

Package generator turns semantic trees into LLVM IR and outputs it to an io.Writer.

Subroutines

Generate (analyzer.Tree, io.Writer) error

Generate takes in a semantic tree and writes corresponding LLVM IR to the given io.Writer. It returns an error in case there is something wrong with the semantic tree that prevents the code generation process from occurring.