+ All Categories
Home > Software > Building languages with Kotlin

Building languages with Kotlin

Date post: 07-Feb-2017
Category:
Upload: federico-tomassetti
View: 278 times
Download: 4 times
Share this document with a friend
14
Building Languages using Kotlin Lyon Kotlin User Group - Federico Tomassetti
Transcript
Page 1: Building languages with Kotlin

Building Languages using Kotlin

Lyon Kotlin User Group - Federico Tomassetti

Page 2: Building languages with Kotlin

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

Page 3: Building languages with Kotlin

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

Page 4: Building languages with Kotlin

What pieces do we need

1. Parser2. Validator3. Compiler/Interpreter4. Editor

Page 5: Building languages with Kotlin

Building languages is expensive

Page 6: Building languages with Kotlin

Language Workbenches

Good if:1) You are ok with the constraints

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

Page 7: Building languages with Kotlin

Why Kotlin?

Because creating this stuff is complex

and we need K-superpowers to

manage doing that!

Page 8: Building languages with Kotlin

State Machines

Turned off Turned on

Low speed High speed

EmergencyStop

IncreaseSpeed

IncreaseSpeed

LowerSpeed

LowerSpeed

off

on

Page 9: Building languages with Kotlin

Our example (1/2)statemachine mySm

input lowSpeedThroughtput: Intinput highSpeedThroughtput: Int

var totalProduction = 0

event turnOffevent turnOnevent speedUpevent speedDownevent emergencyStopevent doNothing

Page 10: Building languages with Kotlin

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 …

Page 11: Building languages with Kotlin

ModelCode

ValidationInterpreterCompiler

The process

Page 12: Building languages with Kotlin

Building a model

12

3Tokens

ParseTree

AbstractSyntaxTree

Page 13: Building languages with Kotlin

Tokens

Let’s see the code

Page 14: Building languages with Kotlin

Want more?

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


Recommended