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 Diagram P u p p y A d u l t t i m e S i n c e L a s t W a h a p p i n e s s < = 3 l k > = 1 1 5 W a g g i n g W h i n i n g B a r k i n g N e e d s h a p p i n e s s = = 1 0 T a i l W a l k 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 @ m i n h u n g e r = = 0 h u n g e r > = 8 h u n g e r > = 8 h u n g e r > = 8 h u n g e r > = 8 m i l k D r u n k > 5 0 & & a g e > = 1 0 H u n g r y H u n g r y a g e > = M A X _ A G E s t a r v e ( ) / b u r y ( ) | | h u n g e r @ m a x D e a d
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; ....... } }
Special Constructs states & transitions range datatype & @max, @min operators trigger functions event functions onEntry & onExit onClockTick & tick keyword
Execution Semantics Transitions in outermost FSM evaluated first Preemptive
Architecture Empath Code Empath UI Lexer tokens Parser ast Empath SSA Walker Runtime ast CodeGen Walker sym table CodeGen Templates Compiled .java Program Java Compiler .class
Static Semantic Analysis Type checking Declaration of variables, function void dummy(int a, state egg,smallBiter; int a = 5; functions, and states float b, string c){ state init bigBiter, egg; string b; a++; b = a; Multiple initial states } function void test() { int x = 10; Consistency of function calls float y = 2.3; string z = null; Function definitions dummy(x ,z , y ); } Consistency of statements and expressions
Static Semantic Analysis Restriction on triggers entity crocodile { entity trig { int age; int age; Transition definition trigger t1() { state egg,smallBiter; boolean x; state init bigBiter; – fromState, toState transition x to y if (age+5); age--; – condition } age %= 10; x = (age > 5); return x; – action } }
Symbol Table name = “Dog” label = “mutt” var hunger Single namespace range [0:10] 5 func onClockTick void Hierarchy state DogPuppy name = “DogPuppy” – Function local var state DogAdult state DogDead transitionList – State definition var happiness int 10 DogPuppy, DogDead, func onEntry void starve() DogAdult, DogDead, state WaggingTail (age>=MAX_AGE || state Whining hunger@max) state Hungry
Code Generation and Runtime Empath Code Empath UI Lexer tokens Parser ast Empath SSA Walker Runtime ast CodeGen Walker sym table CodeGen Templates Compiled .java Program Java Compiler .class
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
Code Generation Combination of two approaches: Second “code generation” tree walk Template language produces .java files
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++;}”
Code Generation Walker Transformations publ i c voi d onCl ockTi ck( i nt t i ck) { f unct i on voi d onCl ockTi ck( ) { i f ( ( ( t i ck%2) ==0) ) { i f ( t i ck 2) { hunger . i ncr em ent Val ue( ) ; hunger ++; age += 0. 2; age += 0. 2; } el s e { } el s e { happi nes s . decr em ent Val ue( ) ; happi nes s - - ; } } s uper . onCl ockTi ck( t i ck) ; } }
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
Code Generation 2: Template Language publ i c cl as s DogPuppy ext ends publ i c cl as s $cl as s $ ext ends Dog { $s uper cl as s $ { pr ot ect ed s t at i c Range m i l kDr unk=new Range( 0, 100, 0) ; $var i abl e: { pr ot ect ed s t at i c $i t $; } ; s epar at or =" \ n" $ publ i c DogPuppy ( ) { s et Label ( " puppy" ) ; Symbol publ i c $cl as s $ ( ) { } Table $i f ( s t at e_l abel ) $ } s et Label ( " $s t at e_l abel $" ) ; $endi f $ } } Template Template Generated .java File
Runtime Environment User Current State State Interface Object Classes Runtime EmpathEntity State Env. Object Tree
The State Tree Dog Puppy Adult Dead Wagging Needs Whining Hungry Barking Hungry Tail Walk
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
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
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
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
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
Recommend
More recommend