Made progress on the AST
parent
32d290b743
commit
705249bfae
@ -0,0 +1,87 @@
|
||||
module AST
|
||||
|
||||
data AProgram(loc src = |tmp:///|)
|
||||
= program(AId id, list[AConst] consts, list[AVar] vars, list[ASubProg] subProgs, ABlock main)
|
||||
;
|
||||
|
||||
data AConst(loc src = |tmp:///|)
|
||||
= const(AId id, AType \type, AValue \value)
|
||||
;
|
||||
data AVar(loc src = |tmp:///|)
|
||||
= var(AId id, AType \type)
|
||||
;
|
||||
|
||||
data ASubProg(loc src = |tmp:///|)
|
||||
= function(AId id, list[AParam] parameters, AType \returnType, list[AVar] locals, ABlock body)
|
||||
| procedure(AId id, list[AParam] parameters, list[AVar] locals, ABlock body)
|
||||
;
|
||||
|
||||
data AParam(loc src = |tmp:///|)
|
||||
= referenceParam(AId id, AType \type)
|
||||
| valueParam(AId id, AType \type)
|
||||
;
|
||||
|
||||
data AType(loc src = |tmp:///|)
|
||||
= simpleType(ABasicType \basic)
|
||||
| arrayType(ABasicType \basic)
|
||||
;
|
||||
data ABasicType(loc src = |tmp:///|)
|
||||
= integerType()
|
||||
| realType()
|
||||
;
|
||||
|
||||
data ABlock(loc src = |tmp:///|)
|
||||
= block(list[AStatement] body)
|
||||
;
|
||||
data AStatement(loc src = |tmp:///|)
|
||||
= assignment(ALhs lhs, AExpr res)
|
||||
| call(ASubProgCall subProg)
|
||||
| skip()
|
||||
| blockStatement(ABlock block)
|
||||
| \if(AGuard guard, AStatement ifBody, AStatement elseBody)
|
||||
| \while(AGuard guard, AStatement body)
|
||||
;
|
||||
|
||||
data ASubProgCall(loc src = |tmp:///|)
|
||||
= call(AId id, list[AExpr] args)
|
||||
| readln(list[AId] ids)
|
||||
| writeln(list[AExpr] args)
|
||||
;
|
||||
|
||||
data AGuard(loc src = |tmp:///|)
|
||||
= lessThan(AExpr left, AExpr right)
|
||||
| lessThanEqual(AExpr left, AExpr right)
|
||||
| greaterThan(AExpr left, AExpr right)
|
||||
| greaterThanEqual(AExpr left, AExpr right)
|
||||
| equal(AExpr left, AExpr right)
|
||||
| not(AGuard inner)
|
||||
| and(AGuard left, AGuard right)
|
||||
| or(AGuard left, AGuard right)
|
||||
;
|
||||
|
||||
data ALhs(loc src = |tmp:///|)
|
||||
= simple(AId id)
|
||||
| indexed(AId id, AExpr index)
|
||||
;
|
||||
|
||||
data AExpr(loc src = |tmp:///|)
|
||||
= ref(AId id)
|
||||
| index(AId id, AExpr index)
|
||||
| slice(AId id, AExpr \start, AExpr end)
|
||||
| val(AValue val)
|
||||
| call(list[AExpr] args)
|
||||
| add(AExpr left, AExpr right)
|
||||
| sub(AExpr left, AExpr right)
|
||||
| mul(AExpr left, AExpr right)
|
||||
| div(AExpr left, AExpr right)
|
||||
| \mod(AExpr left, AExpr right)
|
||||
| intDiv(AExpr left, AExpr right)
|
||||
| negate(AExpr inner)
|
||||
;
|
||||
|
||||
data AValue(loc src = |tmp:///|)
|
||||
= integer(int n)
|
||||
| \real(real n)
|
||||
;
|
||||
data AId(loc src = |tmp:///|)
|
||||
= id(str name);
|
@ -0,0 +1,118 @@
|
||||
module CST2AST
|
||||
|
||||
import Syntax;
|
||||
import AST;
|
||||
import String;
|
||||
import Exception;
|
||||
|
||||
// TODO: Add source
|
||||
|
||||
// -------------------------------- Top level program -----------------------
|
||||
|
||||
AProgram cst2ast(start[Program] program) {
|
||||
// Remove layout
|
||||
Program p = program.top;
|
||||
return cst2ast(p);
|
||||
}
|
||||
|
||||
AProgram cst2ast(Program p)
|
||||
= program(
|
||||
cst2ast(p.name),
|
||||
cst2ast(p.consts),
|
||||
cst2ast(p.vars),
|
||||
cst2ast(p.subProgs),
|
||||
cst2ast(p.main)
|
||||
);
|
||||
|
||||
AId cst2ast(Id name)
|
||||
= id("<name>");
|
||||
|
||||
// -------------------------------------- Declarations -----------------------
|
||||
|
||||
list[AConst] cst2ast(ConstDecl* consts)
|
||||
= [cst2ast(const) | const <- consts];
|
||||
|
||||
AConst cst2ast(ConstDecl cDecl)
|
||||
= const(cst2ast(cDecl.id), inferType(cDecl.\value), cst2ast(cDecl.\value));
|
||||
|
||||
list[AVar] cst2ast(VarDecl* varDecls)
|
||||
= [aVar | varDecl <- varDecls, aVar <- cst2ast(varDecl)];
|
||||
list[AVar] cst2ast(VarDecl varDecl)
|
||||
= [var(cst2ast(id), cst2ast(varDecl.\type)) | id <- varDecl.ids];
|
||||
|
||||
list[ASubProg] cst2ast(SubProgDecl* subProgs)
|
||||
= [cst2ast(subProg) | subProg <- subProgs];
|
||||
ASubProg cst2ast((SubProgDecl)`<FunctionDecl f>`)
|
||||
= function(
|
||||
cst2ast(f.id),
|
||||
cst2ast(f.params),
|
||||
simpleType(cst2ast(f.returnType)),
|
||||
cst2ast(f.locals),
|
||||
cst2ast(f.body)
|
||||
);
|
||||
ASubProg cst2ast((SubProgDecl)`<ProcedureDecl p>`)
|
||||
= procedure(
|
||||
cst2ast(p.id),
|
||||
cst2ast(p.params),
|
||||
cst2ast(p.locals),
|
||||
cst2ast(p.body)
|
||||
);
|
||||
|
||||
list[AParam] cst2ast(Parameters? optParams)
|
||||
= [param | params <- optParams, param <- cst2ast(params)];
|
||||
list[AParam] cst2ast(Parameters params);
|
||||
|
||||
// ---------------------------- Statement lists -----------------------------
|
||||
|
||||
ABlock cst2ast(CompoundStatement body)
|
||||
= block([
|
||||
aStatement
|
||||
| statementList <- body.statementList,
|
||||
aStatement <- cst2ast(statementList)
|
||||
]);
|
||||
|
||||
list[AStatement] cst2ast(StatementList sList)
|
||||
= [cst2ast(sList.first)]
|
||||
+ [cst2ast(statement) | item <- sList.rest, /Statement statement := item];
|
||||
|
||||
// ----------------------------- Statements ----------------------------------
|
||||
|
||||
AStatement cst2ast((Statement)`<Lhs lhs> := <Expr e>`)
|
||||
= assignment(cst2ast(lhs), cst2ast(e));
|
||||
|
||||
AStatement cst2ast((Statement)`<SubProgCall subProgCall>`)
|
||||
= call(cst2ast(subProgCall));
|
||||
|
||||
AStatement cst2ast((Statement)`<CompoundStatement compoundStatement>`)
|
||||
= blockStatement(cst2ast(compoundStatement));
|
||||
|
||||
AStatement cst2ast((Statement)`skip`)
|
||||
= skip();
|
||||
|
||||
AStatement cst2ast((Statement)`if <Guard g> then <Statement ifStmt> else <Statement elseStmt>`)
|
||||
= \if(cst2ast(g), cst2ast(ifStmt), cst2ast(elseStmt));
|
||||
|
||||
AStatement cst2ast((Statement)`while <Guard g> do <Statement body>`)
|
||||
= \while(cst2ast(g), cst2ast(body));
|
||||
|
||||
// -------------------------------- Calls ------------------------------------
|
||||
ASubProgCall cst2ast(SubProgCall call);
|
||||
|
||||
// --------------------------------- Lhs ------------------------------------
|
||||
ALhs cst2ast(Lhs lhs);
|
||||
|
||||
// ------------------------------ Expressions --------------------------------
|
||||
|
||||
AExpr cst2ast(Expr e);
|
||||
AGuard cst2ast(Guard g);
|
||||
|
||||
AValue cst2ast((Number)`<Integer n>`) = integer(toInt("<n>"));
|
||||
AValue cst2ast((Number)`<Real n>`) = \real(toReal("<n>"));
|
||||
|
||||
// ----------------------------- Types ---------------------------------------
|
||||
|
||||
AType cst2ast(TypeSpec \type);
|
||||
ABasicType cst2ast(BasicType \type);
|
||||
|
||||
AType inferType((Number)`<Integer _>`) = simpleType(integerType());
|
||||
AType inferType((Number)`<Real _>`) = simpleType(realType());
|
@ -0,0 +1,26 @@
|
||||
program robin;
|
||||
|
||||
const cover = 1;
|
||||
|
||||
const robins_skill = 0;
|
||||
|
||||
const dogukan = 2;
|
||||
|
||||
var player : integer;
|
||||
|
||||
{ Prints 1 if Robin wins, 0 if it's a draw, -1 if robin loses }
|
||||
procedure smash(at : integer; opponents_skill : integer) ;
|
||||
BEGIN
|
||||
if (robins_skill > opponents_skill) then
|
||||
writeln(1)
|
||||
else
|
||||
if (robins_skill = opponents_skill) then
|
||||
writeln(0)
|
||||
else
|
||||
writeln(-1)
|
||||
END;
|
||||
|
||||
BEGIN
|
||||
player := dogukan;
|
||||
smash(cover, player)
|
||||
END.
|
Loading…
Reference in New Issue