This document summarizes Phillip Trelford's 2014 talk at NDCLondon about creating domain specific languages and compilers. It discusses abstract syntax trees, parsing, interpreters, and code generation. It provides examples of turtle and Small Basic languages and their abstract syntax trees. It also mentions that the speaker created a basic C# compiler in 24 days to demonstrate that compilers can be written quickly without extensive experience. The document encourages attendees to learn from online resources like F# Koans and TryFSharp.org, and recommends buying related books.
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. C# COMPILER IN 24 DAYS
Oh Yes! Neil Danson (aka The Do)
Now, I didn’t do any compiler
theory in uni, I’ve never written a
compiler before and I’ve only
ever looked at Reflection Emit in
anger. But despite these
limitations I have a fairly
capable C# 0.5 compiler (in that
it is a subset of the C# 1
compiler). Which means you can
do it too – it took me less than a
month.
14. 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 __