PLT COMS 4115 Empath Jeremy Posner Nalini Kartha Sampada Sonalkar - - PowerPoint PPT Presentation

plt coms 4115
SMART_READER_LITE
LIVE PREVIEW

PLT COMS 4115 Empath Jeremy Posner Nalini Kartha Sampada Sonalkar - - PowerPoint PPT Presentation

PLT COMS 4115 Empath Jeremy Posner Nalini Kartha Sampada Sonalkar William Mee Empath A language for modeling digital pets. Finite state machine based Compiled Static scoping Dog Example Dog State Transition


slide-1
SLIDE 1

PLT COMS 4115

Empath

 Jeremy Posner  Nalini Kartha  Sampada Sonalkar  William Mee

slide-2
SLIDE 2

Empath

A language for modeling digital pets.

 Finite state machine based  Compiled  Static scoping

slide-3
SLIDE 3

Dog Example

slide-4
SLIDE 4

Dog – State Transition Diagram

D e a d

H u n g r y W h i n i n g W a g g i n g T a i l

h a p p i n e s s < = 3 h a p p i n e s s = = 1 0 h u n g e r > = 8 h u n g e r > = 8 h u n g e r @ m i n

P u p p y

H u n g r y N e e d s W a l k B a r k i n g

t i m e S i n c e L a s t W a l k > = 1 1 5 t i m e S i n c e L a s t W a l k = = 3 0 h u n g e r > = 8 h u n g e r > = 8 h u n g e r = = 0

A d u l t

m i l k D r u n k > 5 0 & & a g e > = 1 0 a g e > = M A X _ A G E | | h u n g e r @ m a x s t a r v e ( ) / b u r y ( )

slide-5
SLIDE 5

Dog – The Program

entity Dog label "mutt" { range [0:10] hunger = 5; float age = 0.0; function void onClockTick() { hunger++; age+=0.2; } event feed(int quantity) { hunger-=quantity; } trigger starve() { if (hunger>16) return true; else return false; } function void bury() { output("the dog has been buried"); } state init DogPuppy, DogAdult, DogDead; transition DogPuppy to DogDead if (starve()) / bury() ; DogPuppy label "puppy" { range [0:100] milkDrunk=0; ....... } }

slide-6
SLIDE 6

Special Constructs

 states & transitions  range datatype & @max, @min operators  trigger functions  event functions  onEntry & onExit  onClockTick & tick keyword

slide-7
SLIDE 7

Execution Semantics

 Transitions in

  • utermost FSM

evaluated first

 Preemptive

slide-8
SLIDE 8

Architecture

Empath Code Compiled Program Empath Runtime Empath UI

Java Compiler Parser Lexer SSA Walker CodeGen Walker CodeGen Templates

tokens ast ast sym table .java .class

slide-9
SLIDE 9

Static Semantic Analysis

 Type checking  Declaration of variables,

functions, and states

 Multiple initial states  Consistency of function calls  Function definitions  Consistency of statements and

expressions

int a = 5; string b; b = a; state egg,smallBiter; state init bigBiter, egg; function void dummy(int a, float b, string c){ a++; } function void test() { int x = 10; float y = 2.3; string z = null; dummy(x ,z , y ); }

slide-10
SLIDE 10

Static Semantic Analysis

 Restriction on triggers  Transition definition

–fromState, toState –condition –action

entity trig { int age; trigger t1() { boolean x; age--; age %= 10; x = (age > 5); return x; } } entity crocodile { int age; state egg,smallBiter; state init bigBiter; transition x to y if (age+5); }

slide-11
SLIDE 11

Symbol Table

 Single namespace  Hierarchy

–Function local var –State definition

name = “Dog” label = “mutt” var hunger range [0:10] 5 func onClockTick void state DogPuppy state DogAdult state DogDead name = “DogPuppy” transitionList var happiness int 10 func onEntry void state WaggingTail state Whining state Hungry DogPuppy, DogDead, starve() DogAdult, DogDead, (age>=MAX_AGE || hunger@max)

slide-12
SLIDE 12

Code Generation and Runtime

Empath Code Compiled Program Empath Runtime Empath UI

Java Compiler Parser Lexer SSA Walker CodeGen Walker CodeGen Templates

tokens ast ast sym table .java .class

slide-13
SLIDE 13

Generated Code

 Java's object structure – A square peg for a

round hole.

– Transitions need to morph source to target – The entity itself should never change

 The generated code falls into two categories:

– Entity

 Contains code to populate State Tree

– State Classes

 One for each state  Each State Class extends the parent State Class

slide-14
SLIDE 14

Code Generation

Combination of two approaches:

 Second “code generation” tree walk  Template language produces .java files

slide-15
SLIDE 15

Code Generation Walker

 Second “collapsing” walk of AST  Enhancement of symbol table  Empath functions become strings of Java code  Important transformations

“int incr(int x) {return x++;}”

slide-16
SLIDE 16

Code Generation Walker Transformations

f unct i on voi d onCl ockTi ck( ) { i f ( t i ck 2) { hunger ++; age += 0. 2; } el s e { happi nes s - - ; } } publ i c voi d onCl ockTi ck( i nt t i ck) { i f ( ( ( t i ck%2) ==0) ) { hunger . i ncr em ent Val ue( ) ; age += 0. 2; } el s e { happi nes s . decr em ent Val ue( ) ; } s uper . onCl ockTi ck( t i ck) ; }

slide-17
SLIDE 17

Code Generation Templates

 Templates used to generate .java files  Uses the String Template project  Target code easy to generate (by design)  Just two templates  Populates templates from symbol table

slide-18
SLIDE 18

Code Generation 2: Template Language

publ i c cl as s $cl as s $ ext ends $s uper cl as s $ { $var i abl e: { pr ot ect ed s t at i c $i t $; } ; s epar at or =" \ n" $ publ i c $cl as s $ ( ) { $i f ( s t at e_l abel ) $ s et Label ( " $s t at e_l abel $" ) ; $endi f $ } } publ i c cl as s DogPuppy ext ends Dog { pr ot ect ed s t at i c Range m i l kDr unk=new Range( 0, 100, 0) ; publ i c DogPuppy ( ) { s et Label ( " puppy" ) ; } }

Symbol Table Template Template Generated .java File

slide-19
SLIDE 19

Runtime Environment

User Interface Runtime Env. EmpathEntity Object Current State Object State Tree State Classes

slide-20
SLIDE 20

The State Tree

Dog Puppy Adult Whining Hungry Wagging Tail Dead Needs Walk Hungry Barking

slide-21
SLIDE 21

Trigger & Transition Handling

 Runtime signals Entity to evaluate transitions  Entity calls current State Tree node's evaluate

method

  • State Tree Node loops through all of the current

state's triggers

  • Returns either the State Class for a new state or null

 Entity instantiates new State Class, replacing

former State.

 Repeats instantiation for init states as needed

slide-22
SLIDE 22

Automated Testing

EmpathLexerTest: lexical analysis EmpathParserTest: parser LineNumberTest: testing of error reporting FuncSSATest: ssa for functions StateTransTest: ssa for state transitions CodeGenWalkerTest: func code generation

slide-23
SLIDE 23

Lessons Learned: Technical

Infrastructure (cvs, ide) was important Learned about compilers, ASTs etc Learned non-compiler subjects too Test-orientated development Our language was unexpectedly ambitious

slide-24
SLIDE 24

Lessons Learned: Team Work

Divided work well Could work independently Team came together Quality team Difficult to coordinate Differing work styles Differing commitment to project Pressure from other courses and outside

slide-25
SLIDE 25

Credits

Jeremy: architecture, target code, user interface, runtime Nalini: lang design, parser, walker, ssa, unit testing, presentation Sampada: lang design, parser, walker, unit testing, functional code gen, documentation Will: lexer, code templates, testing, project management, infrastructure