MAGES

Florian Rappl, iQuest

MAGES

Building a Compiler in .NET

Cover MAGES

Florian Rappl

Solution Architect

  • IoT / Embedded Computing
  • .NET Communication Systems
  • Web Technologies

Writer and consultant

  • Microsoft MVP for C# / Development Tools
  • Active contributions to open-source projects
  • Articles for CodeProject, SitePoint, tuts+, ...

Agenda

  1. Roslyn and MAGES
  2. Compiler Construction 101
  3. Interactive Demo

A long time ago ...

Roslyn Release Build 2015

Roslyn

Roslyn: Overview

Roslyn Overview

Roslyn: API

Roslyn API

Roslyn: Manipulation Layers

Roslyn Manipulation Layers

MAGES

Why?

SineRider

Why?

Why?

Languages

Language Specification (Excerpt)

comparison_operator ::= '>' | '>=' | '<=' | '<' | '==' | '~=' | '&&' | '||'
arithmetic_operator ::= '+' | '-' | '*' | '/' | '\' | '%' | '^'
computing_operator ::= arithmetic_operator | pipe_operator
binary_operator ::= comparison_operator | computing_operator
binary ::= expr space* binary_operator space* expr
explicit_multiplication ::= expr space* '*' space* expr
implicit_multiplication ::= expr space* (identifier | literal)
multiplication ::= explicit_multiplication | implicit_multiplication
property ::= (identifier | string) space* colon space* expr space*
properties ::= property (comma space* property)* comma? space*
object ::= 'new' space* '{' space* properties? '}'
member ::= expr space* '.' space* identifier
block_stmt ::= '{' space* (stmt space*)* '}'

Compilers

Compiler Principle

Compiler Domo

Compiler Principle (C/C++)

Compiler Stage

Compiler Principle (VM)

Compiler Stage

Parsers

Basics

  • Stages:
    1. Stream Processing (Decoding)
    2. Tokenization (Lexical Analysis)
    3. AST Generation (Syntax Analysis)
    4. Checking (Semantic Analysis)
  • We have statements and expressions
  • Statements are triggered via convention (e.g., keywords)
  • Expressions consist of operands and operators

Abstract Syntax Trees

Simple AST

Abstract Syntax Trees

Complex AST

Visitor Pattern


public interface IWalkable
{
    void Accept(ITreeWalker visitor);
}
public interface ITreeWalker
{
    void Visit(VarStatement statement);
    void Visit(BlockStatement statement);
    void Visit(BinaryExpression expression);
    void Visit(FunctionExpression expression);
    // ...
}
			

Precedence Climbing

Precedence Climbing

Virtual Machines

What is a Virtual Machine?

A virtual machine (VM) is an emulation of a computer system.

Dinstinguish between:

  • System virtual machines
  • Process virtual machines
Process VMs are designed to execute computer programs in a platform-independent environment.
SWM> il("3 + 1 * 2 * 4 + 5")
const 1074266112
const 1072693248
const 1073741824
const 20234383
getc 2
const 1074790400
const 20234383
getc 2
const 20234383
getc 2
const 1075052544
const 20234383
getc 2
SWM> il("f = (x) => sin(x) * cos(x)")
newfunc start
arg 0 x
args args
gets x
gets sin
getc 1
gets x
gets cos
getc 1
const 20234383
getc 2
newfunc end
sets f

Demo

Thanks for your attention

  • Feel free to contact me