private getUniqeName : () ==> seqofchar
getUniqeName() == let res = uid in (uid := uid + 1; return"v" ^ util.iToS(res)) postRESULTnotinsetrng context;
private getUniqeSimpleName : () ==> seqofchar
getUniqeSimpleName() ==
( whiletruedo
( let name : seqofchar = getUniqeName() -- Use it as a source of new string in if (name notinsetdom context) then return name
);
return""
) postRESULTnotinsetdom context;
public Compile : seqofchar * SimpleSpecification ==> GiraffeSpecification
Compile(programName, spec) == let name : GiraffeIdentifier = new GiraffeIdentifierImpl(programName),
defs : seqof SimpleDefinition = spec.getDefs(),
functionz : setof GiraffeMethodDefinition = {Compile(defs(i))
| i insetinds defs
& isofclass(SimpleFunctionDefinition, defs(i))},
classDef : GiraffeClassDefinition = new GiraffeClassDefinitionImpl(name, functionz) in
(typeDefs := [defs(i) | i insetinds defs & isofclass(SimpleTypeDefinition, defs(i))]; returnnew GiraffeSpecificationImpl(classDef)) prelen programName > 0 and programName(1) notinset {'0','1','2','3','4','5','6','7','8','9'};
public Compile : SimpleFunctionDefinition ==> GiraffeMethodDefinition
Compile(func) == let name : GiraffeIdentifier = new GiraffeIdentifierImpl(func.getName().getName()),
defs : seqof SimpleParameter = func.getParams(),
params : seqof GiraffeParameter = [Compile(defs(i))
| i insetinds defs],
type : GiraffeType = Compile(GetType(func.getBody())),
body : seqof GiraffeStatement = varDecls ^ [new GiraffeReturnStatementImpl(Compile(func.getBody()))] in (varDecls := []; returnnew GiraffeMethodDefinitionImpl(name, params, type, body)) pre varDecls = [] and context = {|->} post varDecls = [] and context = {|->};
public Compile : SimpleParameter ==> GiraffeParameter
Compile(param) == let name : GiraffeIdentifier = new GiraffeIdentifierImpl(param.getName().getName()),
type : GiraffeType = Compile(param.getType()) inreturnnew GiraffeParameterImpl(type, name);
public Compile : SimpleType ==> [GiraffeType]
Compile(type) == ifisofclass(SimpleIdentifier, type) then let t : SimpleIdentifier = type inreturn Compile(GetBasicType(t))--new GiraffeIdentifierImpl(t.getName()) else let t : SimpleBasicType = type in cases t.name: "INT" -> return GiraffeBasicType`INT, others -> returnnil end postRESULT <> nil;
public Compile : SimpleExpression ==> [GiraffeExpression]
Compile(exp) == casestrue:
(isofclass(SimpleIntegerLiteralExpression, exp)) -> let e : SimpleIntegerLiteralExpression = exp inreturnnew GiraffeIntegerLiteralExpressionImpl(e.getValue()),
(isofclass(SimpleBinaryExpression, exp)) -> let e : SimpleBinaryExpression = exp inreturn Compile(e.getOp(), e.getLhs(), e.getRhs()),
(isofclass(SimpleCasesExpression, exp)) -> let e : SimpleCasesExpression = exp inreturn CompileCases(e),
(isofclass(SimpleVariableExpression, exp)) -> let e : SimpleVariableExpression = exp,
name : SimpleIdentifier = e.getName() inreturnnew GiraffeVariableExpressionImpl(new GiraffeIdentifierImpl(context(name.getName()))),
(isofclass(SimpleLetExpression, exp)) -> let e : SimpleLetExpression = exp inreturn CompileLet(e),
(isofclass(SimpleIfExpression, exp)) -> let e : SimpleIfExpression = exp inreturn CompileIf(e), others -> returnnil end postRESULT <> nil;
public CompileCases : SimpleCasesExpression ==> GiraffeExpression
CompileCases(e) == let testVarName : SimpleIdentifier = new SimpleIdentifierImpl(getUniqeSimpleName()),
testVar : SimpleVariableExpression = new SimpleVariableExpressionImpl(testVarName),
testVarAss : SimpleLocalDefinition = new SimpleLocalDefinitionImpl(testVarName, e.getTest()),
letBody : SimpleExpression = if e.getAlts() = [] then
e.getDeflt() else let first : SimpleCaseAlternative = hd e.getAlts(),
ifTest : SimpleBinaryExpression = new SimpleBinaryExpressionImpl(testVar, SimpleBinaryOperator`EQUALS, first.getTest()),
rest : seqof SimpleCaseAlternative = tl e.getAlts(),
elsIfs : seqof SimpleElseIfExpression = [new SimpleElseIfExpressionImpl(new SimpleBinaryExpressionImpl(testVar, SimpleBinaryOperator`EQUALS, rest(i).getTest()), rest(i).getExp()) | i insetinds rest] in new SimpleIfExpressionImpl(ifTest, first.getExp(), elsIfs, e.getDeflt()) inreturn Compile(new SimpleLetExpressionImpl([testVarAss], letBody)) prenot e.hasDeflt(); -- Empty defaults not allowed as we do not want to implement runtime errors. -- not operator used due to a bug in ASTGEN
public CompileLet : SimpleLetExpression ==> GiraffeExpression
CompileLet(letExp) == let oldContext : mapseqofchartoseqofchar = context in (for x in letExp.getDefs() do let name : seqofchar = x.getName().getName(),
newName : seqofchar = getUniqeName(),
type : SimpleType = GetType(x.getValue()),
gType : GiraffeType = Compile(type),
gName : GiraffeIdentifier = new GiraffeIdentifierImpl(newName),
gValue : GiraffeExpression = Compile(x.getValue()),
gStm : GiraffeVariableDeclStatement = new GiraffeVariableDeclStatementImpl(gType, gName, gValue) in (context := context ++ {name |-> newName};
varDecls := varDecls ^ [gStm];); let body : GiraffeExpression = Compile(letExp.getBody()) in (context := oldContext; return body;)) pre letExp.getDefs() <> [];
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.