Tree Top

30
TreeTop (uy version) ++[>++[>++[>++[>++<-] <-] <-] <-] >>>><+++++[>+ +++++++++<-] >. <+++[>++++++<-] >+. <+++++++[>+ +<-] >. <+[>-<-] >-. <+++ [>---------------------------<-] >. <+++++++++++ ++++++++++++++++++++++++++++++[>++<-] >. <+++ [>--<-] >. <+++++++[>++<-] >. <+++++++++++++ [>------<-] >. <+++[>----<-] >. <+++++++++++++++ ++++++++++++++++++++++++++[>++<-] >+. <++[>--<-] >. <+++++++++++++[>------<-] >-. <+++++++[>+++++ +++++++<-] >. <+++[>----<-] >. <++[>++++++++<-] >. <+++++++++++[>--------<-] >. <+++++++[>++++++ ++++<-] >. <+++[>+++<-] >. <++[>+<-] >+. <++++++ +++++++++++++++++++++++++++++++++++[>--<-] >. <+ ++++++++++[>++++++<-] >+. <+++[>++++<-] >. <+[>- <-] >-. <+[>+<-] >.

description

Tree Top at RubyConfUy

Transcript of Tree Top

Page 1: Tree Top

TreeTop (uy version)TreeTop (uy version)

++[>++[>++[>++[>++<-] <-] <-] <-] >>>><+++++[>++++++++++<-] >. <+++[>++++++<-] >+.

<+++++++[>++<-] >. <+[>-<-] >-. <+++[>---------------------------<-] >. <+++++++++++++++++++++++++++++++++++++++++[>++<-] >. <+++[>--<-] >. <+++++++[>++<-] >. <+++++++++++++[>------<-] >. <+++[>----<-] >. <+++++++++++

++++++++++++++++++++++++++++++[>++<-] >+. <++[>--<-] >. <+++++++++++++[>------<-] >-. <+++++++[>++++++++++++<-] >. <+++[>----<-] >. <++[>++++++++<-] >. <++++++++++

+[>--------<-] >. <+++++++[>++++++++++<-] >. <+++[>+++<-] >. <++[>+<-] >+. <+++++++++++++++++++++++++++++++++++++++++[>--<-] >. <+++++++++++[>++++++<-] >+. <+++

[>++++<-] >. <+[>-<-] >-. <+[>+<-] >.

++[>++[>++[>++[>++<-] <-] <-] <-] >>>><+++++[>++++++++++<-] >. <+++[>++++++<-] >+.

<+++++++[>++<-] >. <+[>-<-] >-. <+++[>---------------------------<-] >. <+++++++++++++++++++++++++++++++++++++++++[>++<-] >. <+++[>--<-] >. <+++++++[>++<-] >. <+++++++++++++[>------<-] >. <+++[>----<-] >. <+++++++++++

++++++++++++++++++++++++++++++[>++<-] >+. <++[>--<-] >. <+++++++++++++[>------<-] >-. <+++++++[>++++++++++++<-] >. <+++[>----<-] >. <++[>++++++++<-] >. <++++++++++

+[>--------<-] >. <+++++++[>++++++++++<-] >. <+++[>+++<-] >. <++[>+<-] >+. <+++++++++++++++++++++++++++++++++++++++++[>--<-] >. <+++++++++++[>++++++<-] >+. <+++

[>++++<-] >. <+[>-<-] >-. <+[>+<-] >.

Page 2: Tree Top

The gloryThe glory

Page 3: Tree Top

DSL (embedded)DSL (embedded)

Domain.specific do result.should be 2end

Page 4: Tree Top

L?L?

In order to write a languageAs a Ruby programmer

I want to have an easy and powerful tool

Page 5: Tree Top

Write a lang? WTF?Write a lang? WTF?

In fact, you do it all the time.Open files, parse stuff, turn it into strings,

numbers, arrays and link it to code.Like when you wrote your first csv parser.

(yeah, i know you did it...)

You just didn’t had the right tool.

Page 6: Tree Top

BrainFuckBrainFuck

3000 slots

00 00 00 00 00 00 00 00 00

Page 7: Tree Top

BrainFuckBrainFuck

><+-.,[]

00 00 00 00 00 00 00 00 00

Page 8: Tree Top

BrainFuckBrainFuck

>00 00 00 00 00 00 00 00 00

Page 9: Tree Top

BrainFuckBrainFuck

<00 00 00 00 00 00 00 00 00

Page 10: Tree Top

BrainFuckBrainFuck

+11 00 00 00 00 00 00 00 00

Page 11: Tree Top

BrainFuckBrainFuck

-00 00 00 00 00 00 00 00 00

Page 12: Tree Top

BrainFuckBrainFuck

,00 00 00 00 00 00 00 00 00

Page 13: Tree Top

BrainFuckBrainFuck

,00 00 00 00 00 00 00 00 00

BB

Page 14: Tree Top

BrainFuckBrainFuck

,6666 00 00 00 00 00 00 00 00

Page 15: Tree Top

BrainFuckBrainFuck

.6666 00 00 00 00 00 00 00 00

BB

Page 16: Tree Top

BrainFuckBrainFuck

[]6666 00 00 00 00 00 00 00 00

Page 17: Tree Top

BrainFuckBrainFuck

[>+]6666 11 11 11 11 11 11 11 11

(now some live coding...)

Page 18: Tree Top

CFGCFGFormally defined by Noam

Chomsky in the 50s.(Yeah, that weird talking guy from the World Social Forum.

For your info he is a great linguist.)

LEX, Yacc

Idea: Generates a language

Page 19: Tree Top

CFGCFG

‘if(‘ exp ‘)’ stmt ‘else’ stmt | ‘if(’ exp ‘)’ stmt

oror

Page 20: Tree Top

CFGCFG

// Wich alert will be executed?

if(false) if(false) alert(“Don’t say ‘the second if alert’.”)else alert(“Say ‘the else alert’. Say it!!!”)

Page 21: Tree Top

CFGCFGGotcha!!!

None.=> undefined

Generally, languages prefer to associate else to the nearest if. But this has to be made in a “meta” rule.

Page 23: Tree Top

PEGPEGParsing Expression Grammars

Brian Ford paper 2004: http://pdos.csail.mit.edu/~baford/packrat/

Priorities, Not Ambiguities / (slash operator)

(a / a b) is different of (a b / a)Syntactic Predicates (& and !)

Regular Expression LikeNo left recursion.

Idea: Recognizes a language

Page 24: Tree Top

PEGPEG

Result:- linear time parsing using memoization- one and only one valid parse tree- no dangling else cases- much easier to learn and useDrawbacks- still new- memory eater- not suitable for natural language processing

Page 25: Tree Top

PEGPEG

‘if(‘ exp ‘)’ stmt ‘else’ stmt / ‘if(’ exp ‘)’ stmt

priority operatorpriority operator

Page 26: Tree Top

ProjectsProjectsarithmetic + lambda calculus + sql

gherkin (cucumber)

mail (rails 3 standard)

0xCOFFEE (full lang compiled to LLVM)many more

html, css, json, odp, scheme, apache conf, trxl, lolcode, brainfuck,sloc, hairball (haml), vcard, hapag, dcf, lordbishop (names)

Page 27: Tree Top

BeyondBeyond

Compile with LLVM(look at 0xCOFFEE)

Compile to Parrot?Implement Ruby?

Watch Citrus

Page 28: Tree Top

Other langsOther langs

Rats! JavaPetitParser SmallTalk

ppeg, pyPEG, pyparsing PythonPerl6 rules Perl6 (native!)

Frisby, Leafy Haskell

Page 29: Tree Top

CSV by the wayCSV by the way

grammar Csv rule file (line [\n])* line end rule line (value ',')* value end rule value [^,]+ endend

Page 30: Tree Top

+++[>+++[>+++++++<-] <-] >><+++[>++++++<-] >. <+++[>++++++++++++<-] >. <++[>--------<-] >. <+++++

++[>++<-] >. <+[>+<-] >. <+++++[>--<-] >-. <+++[>++<-] >. <+[>-<-] >. <++[>++<-] >+. <+++++++++++++

[>----<-] >.

@joaomilho . mailee.merailer.im/chuck-norris