24 HOURS LATER Phillip Trelford, @ptrelford 
#DDDNorth, Leeds, 2014
THIS TALK IS *NOT* 
About how to write a compiler the hard way 
All theory 
For the faint of heart
THIS TALK *IS* 
ABOUT 
Abstract Syntax Trees 
Parsing 
Domain Specific Languages 
Interpreters 
Code Generation
LANGUAGE FAMILIES 
Lisp 
C 
ML 
BASIC
LANGUAGE DESIGN (CONSIDERED 
OPTIONAL) 
Adhoc 
•PHP 
•JavaScript 
•Scala 
Copy&Delete 
•Java 
•J 
•Go 
Copy&Add 
•C# 
•F# 
•Haskell
TO THE TURTLES CODE Phillip Trelford, @ptrelford 
#DDDNorth, Leeds, 2014
TURTLE LANGUAGE 
repeat 10 
[right 36 repeat 5 
[forward 54 right 72]]
TURTLE AST 
module AST 
type arg = int 
type command = 
| Forward of arg 
| Turn of arg 
| Repeat of arg * command list
TWO GIRLS (5 & 7YRS) + TURTLE
SMALL BASIC 
SAMPLE 
Sub Init 
gw = 598 
gh = 428 
GraphicsWindow.BackgroundColor = 
"DodgerBlue" 
GraphicsWindow.Width = gw 
GraphicsWindow.Height = gh 
color = "1=Orange;2=Cyan;3=Lime;" 
size = "1=20;2=16;3=12;" 
passed = 0 
cd = "False" ' collision detected 
EndSub
SMALL BASIC AST 
/// Small Basic expression 
type expr = 
| Literal of value 
| Identifier of identifier 
| GetAt of location 
| Func of invoke 
| Neg of expr 
| Arithmetic of expr * arithmetic * expr 
| Comparison of expr * comparison * expr 
| Logical of expr * logical * expr 
/// Small Basic instruction 
type instruction = 
| Assign of assign 
| SetAt of location * expr 
| PropertySet of string * string * expr 
| Action of invoke 
| For of assign * expr * expr 
| EndFor 
| If of expr 
| ElseIf of expr 
| Else 
| EndIf 
| While of expr 
| EndWhile 
| Sub of identifier * string list 
| EndSub 
| Label of label 
| Goto of label
RESOURCES
F# KOANS 
[<Koan>] 
let SquareEvenNumbersWithPipelineOperator() = 
(* In F#, you can use the pipeline operator to get the benefit of 
the parens style with the readability of the statement style. *) 
let result = 
[0..5] 
|> List.filter isEven 
|> List.map square 
AssertEquality result __
TRYFSHARP.ORG
F# BOOKS
QUESTIONS 
Twitter: @ptrelford 
Blog: http://trelford.com/blog 
Turtle: http://fssnip.net/nN

Write Your Own Compiler in 24 Hours

  • 1.
    24 HOURS LATERPhillip Trelford, @ptrelford #DDDNorth, Leeds, 2014
  • 2.
    THIS TALK IS*NOT* About how to write a compiler the hard way All theory For the faint of heart
  • 3.
    THIS TALK *IS* ABOUT Abstract Syntax Trees Parsing Domain Specific Languages Interpreters Code Generation
  • 4.
  • 5.
    LANGUAGE DESIGN (CONSIDERED OPTIONAL) Adhoc •PHP •JavaScript •Scala Copy&Delete •Java •J •Go Copy&Add •C# •F# •Haskell
  • 6.
    TO THE TURTLESCODE Phillip Trelford, @ptrelford #DDDNorth, Leeds, 2014
  • 7.
    TURTLE LANGUAGE repeat10 [right 36 repeat 5 [forward 54 right 72]]
  • 8.
    TURTLE AST moduleAST type arg = int type command = | Forward of arg | Turn of arg | Repeat of arg * command list
  • 9.
    TWO GIRLS (5& 7YRS) + TURTLE
  • 10.
    SMALL BASIC SAMPLE Sub Init gw = 598 gh = 428 GraphicsWindow.BackgroundColor = "DodgerBlue" GraphicsWindow.Width = gw GraphicsWindow.Height = gh color = "1=Orange;2=Cyan;3=Lime;" size = "1=20;2=16;3=12;" passed = 0 cd = "False" ' collision detected EndSub
  • 11.
    SMALL BASIC AST /// Small Basic expression type expr = | Literal of value | Identifier of identifier | GetAt of location | Func of invoke | Neg of expr | Arithmetic of expr * arithmetic * expr | Comparison of expr * comparison * expr | Logical of expr * logical * expr /// Small Basic instruction type instruction = | Assign of assign | SetAt of location * expr | PropertySet of string * string * expr | Action of invoke | For of assign * expr * expr | EndFor | If of expr | ElseIf of expr | Else | EndIf | While of expr | EndWhile | Sub of identifier * string list | EndSub | Label of label | Goto of label
  • 12.
  • 13.
    F# KOANS [<Koan>] let SquareEvenNumbersWithPipelineOperator() = (* In F#, you can use the pipeline operator to get the benefit of the parens style with the readability of the statement style. *) let result = [0..5] |> List.filter isEven |> List.map square AssertEquality result __
  • 14.
  • 15.
  • 16.
    QUESTIONS Twitter: @ptrelford Blog: http://trelford.com/blog Turtle: http://fssnip.net/nN

Editor's Notes

  • #9 http://blogs.msdn.com/b/smallbasic/archive/2014/08/11/small-basic-game-programming-vertical-scrolling-game.aspx
  • #11 http://blogs.msdn.com/b/smallbasic/archive/2014/08/11/small-basic-game-programming-vertical-scrolling-game.aspx