building languages with kotlin

Post on 07-Feb-2017

277 Views

Category:

Software

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Building Languages using Kotlin

Lyon Kotlin User Group - Federico Tomassetti

Hi, I am Federico

Once upon I time…

● Got a PhD in Language Engineering

● Done research in Italy & Germany

● Worked at TripAdvisor

● Worked at Groupon

Now I am an independent consultant on

Language Engineering. I design and build:

● Parsers

● Interpreters

● Compilers

● Editors

Why building languages?

The language or notation we are using to express or record our thoughts, are the major factors determining what we can think or express at all!

Edsger W. Dijkstra - The Humble Programmer

What pieces do we need

1. Parser2. Validator3. Compiler/Interpreter4. Editor

Building languages is expensive

Language Workbenches

Good if:1) You are ok with the constraints

2) You invest tons of time learning time3) Dependencies are not an issue

Why Kotlin?

Because creating this stuff is complex

and we need K-superpowers to

manage doing that!

State Machines

Turned off Turned on

Low speed High speed

EmergencyStop

IncreaseSpeed

IncreaseSpeed

LowerSpeed

LowerSpeed

off

on

Our example (1/2)statemachine mySm

input lowSpeedThroughtput: Intinput highSpeedThroughtput: Int

var totalProduction = 0

event turnOffevent turnOnevent speedUpevent speedDownevent emergencyStopevent doNothing

Our example (2/2)start state turnedOff { on turnOn -> turnedOn}

state lowSpeed { on entry { totalProduction = totalProduction + lowSpeedThroughtput print("Producing " + lowSpeedThroughtput + " elements (total "+totalProduction+")") } on speedDown -> turnedOn on speedUp -> highSpeed on doNothing -> lowSpeed}

… more states …

ModelCode

ValidationInterpreterCompiler

The process

Building a model

12

3Tokens

ParseTree

AbstractSyntaxTree

Tokens

Let’s see the code

Want more?

Code: https://github.com/ftomassetti/smlang-exampleBook: https://leanpub.com/create_languagesBlog: https://tomassetti.me

top related