Empath Slides - cs.columbia.edu
Transcript of Empath Slides - cs.columbia.edu
![Page 1: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/1.jpg)
PLT COMS 4115
Empath
Jeremy Posner Nalini Kartha Sampada Sonalkar William Mee
![Page 2: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/2.jpg)
Empath
A language for modeling digital pets.
Finite state machine based Compiled Static scoping
![Page 3: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/3.jpg)
Dog Example
![Page 4: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/4.jpg)
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 gT 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 al k > = 1 1 5
t i m e S i n c e L a s t W al 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 ( )
![Page 5: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/5.jpg)
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;
.......
}
}
![Page 6: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/6.jpg)
Special Constructs
states & transitions range datatype & @max, @min operators trigger functions event functions onEntry & onExit onClockTick & tick keyword
![Page 7: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/7.jpg)
Execution Semantics
Transitions in outermost FSM evaluated first
Preemptive
![Page 8: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/8.jpg)
Architecture
EmpathCode
CompiledProgram
EmpathRuntime
EmpathUI
Java Compiler
Parser
Lexer
SSA Walker
CodeGen Walker
CodeGen Templates
tokens
ast
ast
sym table
.java
.class
![Page 9: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/9.jpg)
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 );
}
![Page 10: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/10.jpg)
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);}
![Page 11: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/11.jpg)
Symbol Table
Single namespace Hierarchy
–Function local var–State definition
name = “Dog”label = “mutt”
var hunger range [0:10] 5
func onClockTick void
state DogPuppystate DogAdultstate DogDead
name = “DogPuppy”
transitionList
var happiness int 10
func onEntry void
state WaggingTailstate Whiningstate Hungry
DogPuppy, DogDead, starve()DogAdult, DogDead, (age>=MAX_AGE || hunger@max)
![Page 12: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/12.jpg)
Code Generation and Runtime
EmpathCode
CompiledProgram
EmpathRuntime
EmpathUI
Java Compiler
Parser
Lexer
SSA Walker
CodeGen Walker
CodeGen Templates
tokens
ast
ast
sym table
.java
.class
![Page 13: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/13.jpg)
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
![Page 14: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/14.jpg)
Code Generation
Combination of two approaches: Second “code generation” tree walk Template language produces .java files
![Page 15: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/15.jpg)
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++;}”
![Page 16: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/16.jpg)
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 ement Val ue( ) ;
age += 0. 2;
} el s e {
happi nes s . decr ement Val ue( ) ;
}
s uper . onCl ockTi ck( t i ck) ;
}
![Page 17: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/17.jpg)
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
![Page 18: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/18.jpg)
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 mi l kDr unk=new Range( 0, 100, 0) ;
publ i c DogPuppy ( ) { s et Label ( " puppy" ) ;
}
}
SymbolTable
TemplateTemplate Generated .java File
![Page 19: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/19.jpg)
Runtime Environment
UserInterface
RuntimeEnv.
EmpathEntityObject
Current StateObject
StateTree
StateClasses
![Page 20: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/20.jpg)
The State Tree
Dog
Puppy Adult
Whining HungryWagging
Tail
Dead
NeedsWalk
HungryBarking
![Page 21: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/21.jpg)
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
![Page 22: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/22.jpg)
Automated Testing
EmpathLexerTest: lexical analysisEmpathParserTest: parserLineNumberTest: testing of error reportingFuncSSATest: ssa for functionsStateTransTest: ssa for state transitionsCodeGenWalkerTest: func code generation
![Page 23: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/23.jpg)
Lessons Learned: Technical
Infrastructure (cvs, ide) was importantLearned about compilers, ASTs etcLearned non-compiler subjects tooTest-orientated development
Our language was unexpectedly ambitious
![Page 24: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/24.jpg)
Lessons Learned: Team Work
Divided work well Could work independentlyTeam came together Quality team
Difficult to coordinate Differing work stylesDiffering commitment to projectPressure from other courses and outside
![Page 25: Empath Slides - cs.columbia.edu](https://reader031.fdocuments.us/reader031/viewer/2022021010/6203cfe8e188b1234d162f08/html5/thumbnails/25.jpg)
Credits
Jeremy: architecture, target code, user interface, runtimeNalini: lang design, parser, walker, ssa, unit testing, presentationSampada: lang design, parser, walker, unit testing, functional code gen, documentationWill: lexer, code templates, testing, project management, infrastructure