Making A Compiler

From bibbleWiki
Jump to navigation Jump to search

Introduction

This page is how someone went about build a compiler and is here for reference

Steps

  • Write example file
  • Read contents of file
  • Parse Lines
  • Parse Tokens in
  • Decide on Node Types
  • Decide what AST should look like
  • Create Nodes

Parsing Context

We made a context to store

  • type e.g. integers
  • variables e.g. a, b

Types

This is the storing of which types would be valid in our code

Variables

This is the storing of which variable are in our code. Against each variable we store which type and which variable e.g. a : integer = 69

Things I learnt

Abstract Syntax Tree

Continuation vs Recursion

Original Implementation

From the start we had recursion where we parse expression which in turn parses an expression.

for (;;) {
  Error err = parse_expr(context, contents_it, &contents_it, expression);
   while ((err = lex_advance(&current_token, &token_length, end)).type == 
   ERROR_NONE) {
...
     err = parse_expr(context, current_token.end, end, assigned_expr);
...
      
   }
}

Next Implementation

Typical Flow Control

  • Jump goto, while, functions
  • Conditional (if/then/else)
  • Early Exit (break, continue, skip)
  • Exception handling (try/throw/catch)
  • Lazy evaluation (delay/force)
  • Threading (thread create,stop, yield)
  • Generator (yield)