Programming progressing

Programming progressing
March 18, 2010
Jon, Leni & Johannes
1
Architectural considerations
How do we get from Simple AST to
Giraffe AST
Two choices were on our minds
Visitor pattern w/recursive traversal
Ext. class w/recursive traversal
March 18, 2010
2
Visitor Pattern class diagram
March 18, 2010
3
Visitor pattern Sequence Diag.
March 18, 2010
4
Ex. Class w/ recursive traversal
Consists of mutually recursive operations
Signatures of the form:
Compile : SimpleX ==> GiraffeY
One operation for each class in Simple
Each is responsible for returning the corrosponding
code of SimpleX in Giraffe
March 18, 2010
5
Ex. Class w/ recursive traversal
Top-Down traversal:
Simple program:
function x() == 13
Simple AST:
SimpleSpecification
SimpleFunctionDefinition
SimpleIntegerLiteralExp…
March 18, 2010
6
Ex. Class w/ recursive traversal
Code example of Compile(SimpleFunctionDefinition)
public Compile : SimpleFunctionDefinition ==> GiraffeMethodDefinition
Compile(func) ==
let name : GiraffeIdentifier = new GiraffeIdentifierImpl(func.getName().getName()),
defs : seq of SimpleParameter = func.getParams(),
params : seq of GiraffeParameter = [Compile(defs(i)) | i in set inds defs],
type : GiraffeType = GiraffeBasicType`INT,--Compile(GetType(func.getBody())),
body : seq of GiraffeStatement = [new GiraffeReturnStatementImpl
(Compile(func.getBody()))]
in return new GiraffeMethodDefinitionImpl(name, params, type, body);
March 18, 2010
7
Pros and Cons
Visitor pattern
Ext. Class
Pros
Works well on AST
Is easily extended
No ”ugly” isofclass()
Cons
AST to be compatible
More complicated to grasp
Pros
Simple
No change on AST interface
Cons
Lots of ugly ”isofclass”
Which way to go?
We chose external class because AST didn’t have to be modified,
easy to understand and programme (time an important factor).
March 18, 2010
8
Sequence
public CompileIf : SimpleIfExpression -> GiraffeIfExpression
CompileIf(selif) ==
let
gTest : GiraffeExpression = Compile(selif.getTest()),
gThen : GiraffeExpression = Compile(selif.getThn()),
gElse : GiraffeExpression = Compile(selif.getEse()),
gElif : seq of GiraffeElseIfExpression =
[CompileElseIf(selif.getElif()(i)) | i in set inds selif.getElif()]
in
new GiraffeIfExpressionImpl(gTest,gThen,gElif,gElse);
March 18, 2010
9
Isofclass & case
public Compile : SimpleType ==> GiraffeType
Compile(type) ==
if isofclass(SimpleIdentifier, type) then
let t : SimpleIdentifier = type
in
return new GiraffeIdentifierImpl(t.getName())
else
let t : SimpleBasicType = type
in
cases t.name:
"INT" -> return GiraffeBasicType`INT,
"REAL" -> return GiraffeBasicType`DOUBLE,
"BOOL" -> return GiraffeBasicType`BOOL,
"NAT" -> return GiraffeBasicType`INT
end;
March 18, 2010
10
SimpleLetExpression AST->
GiraffeVariableDeclStatement AST
Simple AST
LetExpression ::
defs : seq of LocalDefinition
body : Expression;
LocalDefinition ::
name : Identifier
value : Expression;
March 18, 2010
Giraffe AST
VariableDeclStatement ::
name : Identifier
type : Type
value : Expression;
11
Case in VDM
public Compile : SimpleExpression ==> GiraffeExpression
Compile(exp) ==
cases true:
(isofclass(SimpleRealLiteralExpression, exp)) -> let e :
SimpleRealLiteralExpression = exp
in return new
GiraffeDoubleLiteralExpressionImpl(e.getValue()),
(isofclass(SimpleIntegerLiteralExpression, exp)) -> let e :
SimpleIntegerLiteralExpression = ex
in return new
GiraffeIntegerLiteralExpressionImpl(e.getValue()),
…
end;
March 18, 2010
12
Language differences
Convert Simple into Java equals headache
Easy 1:1 conversion just a sweet illusion
Ex.: case in Simple in resembles switch in Giraffe(Java subset)
Simple snippet:
Cases testexp
exp -> exp,
exp -> exp,
others -> exp
Sweet illusion but beware:
Java snippet
Switch(testexp) {
case exp: statement;
case exp:statement;
default:statement;
}
Simple testexp = nat, real, bool, int
Java testexp = char, byte, short, int
Simple alternatives exp accepts any kind of exp, Java accepts only constant exp.
March 18, 2010
13
Language differences cont’d
Instinctive solution: convert into if/else in Giraffe.
Good idea but put if/else into Simple Let exp to avoid side effects
Simple snippet
Let x = testexp
In if(x = altexp) then exp
else if(x = altexp) then exp
else exp
March 18, 2010
Giraffe snippet
Final <testexp type> x = testexp;
if(x == altexp) { exp;}
else if(x == altexp){exp;}
else {exp;}
14
Test backend – 6 Steps
First 3 steps
SimpleParser
Compiler.vdmpp
Codegen.vdmpp
Simple Code
Java Code
Simple AST
March 18, 2010
Giraffe AST
15
Test backend – 6 Steps
Last 3 steps
Write to disk
Java Compiler
Java VM
Value
Java code
March 18, 2010
Java Source File
Java Class File
16
Test backend
Compare the return code with 42
Works for very simple Simple programs like:
function x() == 42
Correct translation can be checked like:
function x() == 10+40-8
function x() == if 10=8 then 32 else 42
March 18, 2010
17