Implementing Programming Languages for Fun and Profit with OMeta - - PowerPoint PPT Presentation

implementing programming languages for fun and profit with
SMART_READER_LITE
LIVE PREVIEW

Implementing Programming Languages for Fun and Profit with OMeta - - PowerPoint PPT Presentation

Implementing Programming Languages for Fun and Profit with OMeta Alessandro Warth Viewpoints Research Institute & UCLA Who am I? NOT David Simmons NOT Billy Idol ! ! ! programming languages STEPS ... toward the reinvention of


slide-1
SLIDE 1

Implementing Programming Languages for Fun and Profit with

OMeta

Alessandro Warth Viewpoints Research Institute & UCLA

slide-2
SLIDE 2

Who am I?

slide-3
SLIDE 3

NOT David Simmons

slide-4
SLIDE 4

NOT Billy Idol

slide-5
SLIDE 5 ! ! !

programming languages

slide-6
SLIDE 6

STEPS

... toward the reinvention of programming

slide-7
SLIDE 7

The STEPS Project

  • Goal: To create a highly useful end-

user system including...

  • operating system
  • programming environment
  • “applications”
  • graphics, sound }

personal computing

slide-8
SLIDE 8

... in under 20,000 LOC!

slide-9
SLIDE 9

Windows XP ~40 million LOC

slide-10
SLIDE 10

Squeak ~200 thousand LOC

slide-11
SLIDE 11

Why?

  • To put people in charge of their own SW

destinies

  • No single person can understand

40,000,000 LOC (~library)

  • You can “own” 20,000 LOC (~book)
slide-12
SLIDE 12
slide-13
SLIDE 13

Why? (cont’d)

  • Didactic value
  • a curriculum for university students to

learn about powerful ideas

  • may even be good for AP C.S.
slide-14
SLIDE 14

Programming Languages

  • STEPS: code size, understandability
  • Choice of programming language has a big

impact on both

  • What’s the right one for STEPS?
slide-15
SLIDE 15
slide-16
SLIDE 16

Long Lines...

  • It takes lots of time and effort to implement

a programming language

  • limits how much experimenting we can do
slide-17
SLIDE 17
  • Traditional PL

implementations are BIG

  • only have 20k LOC for

the whole thing!

Big and Bad

slide-18
SLIDE 18
slide-19
SLIDE 19

OMeta

slide-20
SLIDE 20

OMeta

an OO language for pattern matching

slide-21
SLIDE 21
  • me

t a

slide-22
SLIDE 22

JavaScript (OMeta/Squeak)

slide-23
SLIDE 23

Sun’s Lively Kernel (OMeta/COLA)

slide-24
SLIDE 24

Toylog

  • Front-end to Prolog for children
  • Runs on Squeak
  • ~70 LOC

Homer is Bart’s father. Marge is Bart’s mother. x is y’s parent if x is y’s father or

  • r x is y’s mother.

x is Bart’s parent?

slide-25
SLIDE 25

Toylog Demo

slide-26
SLIDE 26

What can OMeta do for you?

  • Make your life easier with DSLs
  • good DSLs come from people who need

them

  • not PL people
  • Make your apps scriptable by end-users
  • ... without making them learn Smalltalk
slide-27
SLIDE 27

Roadmap

  • A brief overview of OMeta
  • pattern matching
  • OO features
  • ...
  • OMeta/JS
slide-28
SLIDE 28

Traditional PL Implementation

slide-29
SLIDE 29

lex, for lexical analysis yacc, for parsing visitors, for AST transformations and code generation

Traditional PL Implementation

slide-30
SLIDE 30

Pattern Matching: A Unifying Idea!

  • lexical analysis: characters → tokens
  • parsing: tokens → parse trees
  • constant folding and other
  • ptimizations: parse trees → parse trees
  • code generation: parse trees → code
slide-31
SLIDE 31

Why use PM for everything?

  • Simplicity
  • Less stuff to learn (lowers learning curve)
  • Great for extensibility
  • trad. impls hard to extend
  • OMeta: every part of PL impl. (e.g.,

parsing, tree traversals, codegen) can be extended using same mechanism

slide-32
SLIDE 32

Pattern Matching

  • Other langs have PM, do we need OMeta?
  • ML-style pattern matching
  • great for tree transformations
  • not good for lexing/parsing
  • “That’s what ML-lex and ML-yacc are for!”
  • OMeta is based on PEGs
slide-33
SLIDE 33

Parsing Expression Grammars (PEGs)

  • Recognition-based foundation for describing

syntax

  • Only prioritized choice
  • no ambiguities
  • easy to understand
  • Backtracking, unlimited lookahead
  • Semantic predicates, e.g., ?[x == y]

[Ford, ‘04]

slide-34
SLIDE 34

dig ::= $0 | ... | $9 num ::= <num> <dig> | <dig> expr ::= <expr> $+ <num> | <num>

PEGs, OMeta style

slide-35
SLIDE 35

( ):d :n :d :e :n dig ::= $0 | ... | $9 num ::= <num> <dig> | <dig> expr ::= <expr> $+ <num> | <num>

PEGs, OMeta style

slide-36
SLIDE 36

( ):d :n :d :e :n => [d digitValue] => [n * 10 + d] => [{#plus. e. n}] dig ::= $0 | ... | $9 num ::= <num> <dig> | <dig> expr ::= <expr> $+ <num> | <num>

PEGs, OMeta style

slide-37
SLIDE 37

Increasing Generality

  • PEGs operate on streams of characters
  • OMeta operates on streams of objects
  • anything matches any one object
  • strings, e.g., ‘hello’
  • symbols, e.g,. #ans
  • numbers, e.g., 42
  • “listy” objects, e.g., {‘hello’ #ans 42 {}}
slide-38
SLIDE 38

Example: evaluating parse trees

eval ::= {#plus <eval>:x <eval>:y} => [x + y] | <anything>:n ?[n isNumber] => [n]

{#plus. {#plus. 1. 2}. 3} → 6

slide-39
SLIDE 39

dig ::= ($0 | ... | $9):d => [d digitValue] num ::= <num>:n <dig>:d => [n * 10 + d] | <dig> expr ::= <expr>:e $+ <num>:n => [{#plus. e. n}] | <num>

slide-40
SLIDE 40

OMeta is Object-Oriented

dig ::= ($0 | ... | $9):d => [d digitValue] num ::= <num>:n <dig>:d => [n * 10 + d] | <dig> expr ::= <expr>:e $+ <num>:n => [{#plus. e. n}] | <num>

slide-41
SLIDE 41

OMeta is Object-Oriented

dig ::= ($0 | ... | $9):d => [d digitValue] num ::= <num>:n <dig>:d => [n * 10 + d] | <dig> expr ::= <expr>:e $+ <num>:n => [{#plus. e. n}] | <num>

MyLang

slide-42
SLIDE 42

anything ::= ... ...

OMeta

OMeta is Object-Oriented

dig ::= ($0 | ... | $9):d => [d digitValue] num ::= <num>:n <dig>:d => [n * 10 + d] | <dig> expr ::= <expr>:e $+ <num>:n => [{#plus. e. n}] | <num>

MyLang

slide-43
SLIDE 43

anything ::= ... ...

OMeta

OMeta is Object-Oriented

dig ::= ($0 | ... | $9):d => [d digitValue] num ::= <num>:n <dig>:d => [n * 10 + d] | <dig> expr ::= <expr>:e $+ <num>:n => [{#plus. e. n}] | <num>

MyLang

expr ::= <expr>:e $- <num>:n => [{#minus. e. n}] | <super #expr>

MyLang++

slide-44
SLIDE 44

Parameterized rules

digit ::= $0 | $1 | $2 | $3 | $4 | $5 | $6 | $7 | $8 | $9

slide-45
SLIDE 45

Parameterized rules

digit ::= $0 | $1 | $2 | $3 | $4 | $5 | $6 | $7 | $8 | $9 range :a :b ::= <anything>:x ?[x >= a] ?[x <= b] => [x]

slide-46
SLIDE 46

Parameterized rules

digit ::= $0 | $1 | $2 | $3 | $4 | $5 | $6 | $7 | $8 | $9 digit ::= <range $0 $9> range :a :b ::= <anything>:x ?[x >= a] ?[x <= b] => [x]

slide-47
SLIDE 47

Higher-order rules

formals ::= <name> ($, <name>)* args ::= <expr> ($, <expr>)*

slide-48
SLIDE 48

Higher-order rules

formals ::= <name> ($, <name>)* args ::= <expr> ($, <expr>)* listOf :p ::= <apply p> ($, <apply p>)*

slide-49
SLIDE 49

Higher-order rules

formals ::= <name> ($, <name>)* args ::= <expr> ($, <expr>)* formals ::= <listOf #name> args ::= <listOf #expr> listOf :p ::= <apply p> ($, <apply p>)*

slide-50
SLIDE 50

OMetaJS = OMeta + JavaScript

OMeta Parser JS Parser

slide-51
SLIDE 51

OMetaJS = OMeta + JavaScript

OMeta Parser JS Parser OMetaJS Parser

slide-52
SLIDE 52

OMetaJS = OMeta + JavaScript

OMeta Parser JS Parser OMetaJS Parser

slide-53
SLIDE 53

OMetaJS = OMeta + JavaScript

OMeta Parser JS Parser OMetaJS Parser

slide-54
SLIDE 54

OMetaJS = OMeta + JavaScript

OMeta Parser JS Parser OMetaJS Parser

  • duplicated effort
  • versioning problem
slide-55
SLIDE 55

Foreign rule invocation

  • Lend input stream to another grammar
  • Compose multiple grammars w/o worrying

about name clashes

  • meta OMetaJSParser {
  • metajs ::= <foreign OMetaParser #grammar>

| <foreign JSParser #stmt> }

slide-56
SLIDE 56

This OMeta, That OMeta

  • Several versions of OMeta:
  • OMeta/Squeak
  • OMeta/COLA
  • OMeta/JS
  • ...
  • Slightly different syntaxes
  • Use different languages for semantic actions

and predicates

slide-57
SLIDE 57

OMeta/JS

slide-58
SLIDE 58

JavaScript Workspace

  • Takashi

Yamamiya’s handy work

  • Inspiration for OMeta/JS
  • Workspace-style interface for JavaScript
  • Runs inside the web browser,

works like a Wiki

slide-59
SLIDE 59

JavaScript

  • Dynamic language
  • First-class functions (closures)
  • Late-bound
  • eval()
  • Huge performance improvements lately
  • new webkit runs at “.5 Squeaks”
slide-60
SLIDE 60

Plus... IT’S EVERYWHERE!

slide-61
SLIDE 61

ASSEMBLY LANGUAGE

slide-62
SLIDE 62
slide-63
SLIDE 63

Switch to web browser

slide-64
SLIDE 64
slide-65
SLIDE 65

OMeta/JS Forget Guitar Hero... I could be the next Dan Ingalls!

slide-66
SLIDE 66

For more info...

  • DLS’07 Paper
  • OMeta Mailing List

http://vpri.org/mailman/listinfo/ometa

  • OMeta/JS wiki

http://jarrett.cs.ucla.edu/ometa-js

  • Ask questions now
slide-67
SLIDE 67

THE END

slide-68
SLIDE 68

Selected Related Work

  • Parsing Expression Grammars [Ford, ‘04]
  • LISP70 Pattern Matcher [T

esler et al., ‘73]

  • Parser combinator libraries [Hutton, ‘92],

[Bracha’07]

  • “Modular Syntax” [Grimm, ’06]