Skip to content

Commit

Permalink
Merge pull request #1 from Tdev95/Resolve
Browse files Browse the repository at this point in the history
AST, CST2AST & Resolve
  • Loading branch information
tdev-student committed Dec 4, 2019
2 parents 09baec7 + 9cbe214 commit 8a7a6f6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/.settings
/examples/*.html
/examples/*.js

/.metadata/
30 changes: 25 additions & 5 deletions src/AST.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,36 @@ module AST

data AForm(loc src = |tmp:///|)
= form(str name, list[AQuestion] questions)
;
;

data AQuestion(loc src = |tmp:///|)
;
= question(str q, AId id, AType \type, list[AExpr] expr)
| cond(AExpr c, list[AQuestion] \if, list[AQuestion] \else)
;

data AExpr(loc src = |tmp:///|)
= ref(AId id)
data AExpr(loc src = |tmp:///|)
= brackets(AExpr expr)
| not(AExpr expr)
| divide(AExpr expr1, AExpr expr2)
| multiply(AExpr expr1, AExpr expr2)
| add(AExpr expr1, AExpr expr2)
| subtract(AExpr expr1, AExpr expr2)
| less(AExpr expr1, AExpr expr2)
| gtr(AExpr expr1, AExpr expr2)
| leq(AExpr expr1, AExpr expr2)
| geq(AExpr expr1, AExpr expr2)
| eq(AExpr expr1, AExpr expr2)
| neq(AExpr expr1, AExpr expr2)
| and(AExpr expr1, AExpr expr2)
| or(AExpr expr1, AExpr expr2)
| ref(AId id)
| integer(int n)
| boolean(str \bool)
;

data AId(loc src = |tmp:///|)
= id(str name);

data AType(loc src = |tmp:///|);
data AType(loc src = |tmp:///|)
= \type(str \type);

42 changes: 36 additions & 6 deletions src/CST2AST.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import AST;

import ParseTree;
import String;
import IO;

/*
* Implement a mapping from concrete syntax trees (CSTs) to abstract syntax trees (ASTs)
Expand All @@ -18,23 +19,52 @@ import String;

AForm cst2ast(start[Form] sf) {
Form f = sf.top; // remove layout before and after form
return form("", [], src=f@\loc);
return form("<f.name>", [cst2ast(q) | q <- f.questions], src=f@\loc);
}
AQuestion cst2ast(Question q) {
throw "Not yet implemented";
switch(q) {
case (Question)`<Str name> <Id i> : <Type t>`:
return question("<name>", id("<i>", src=i@\loc), cst2ast(t), [], src=q@\loc);
case (Question)`<Str name> <Id i> : <Type t> = <Expr e>`:
return question("<name>", id("<i>", src=i@\loc), cst2ast(t), [cst2ast(e)], src=q@\loc);
case (Question)`if ( <Expr expr> ) { <Question* x0> }`:
return cond(cst2ast(expr), [cst2ast(q2) | q2 <- x0], [], src=q@\loc);
case (Question)`if ( <Expr expr> ) { <Question* x0> } else { <Question* x1>}`:
return cond(cst2ast(expr), [cst2ast(q2) | q2 <- x0], [cst2ast(q2) |Question q2 <- x1], src=q@\loc);
}
throw "Not yet implemented <q>";
}
AExpr cst2ast(Expr e) {
switch (e) {
case (Expr)`<Id x>`: return ref("<x>", src=x@\loc);

// etc.
case (Expr)`(<Expr expr>)`: return brackets(cst2ast(expr), src=e@\loc);
case (Expr)`! <Expr expr>` : return not(cst2ast(expr), src=e@\loc);
case (Expr)`<Expr expr1> / <Expr expr2>`: return divide(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Expr expr1> * <Expr expr2>`: return multiply(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Expr expr1> + <Expr expr2>`: return add(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Expr expr1> - <Expr expr2>`: return subtract(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Expr expr1> \< <Expr expr2>`: return less(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Expr expr1> \> <Expr expr2>`: return gtr(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Expr expr1> \<= <Expr expr2>`: return leq(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Expr expr1> \>= <Expr expr2>`: return geq(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Expr expr1> == <Expr expr2>`: return eq(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Expr expr1> != <Expr expr2>`: return neq(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Expr expr1> && <Expr expr2>`: return and(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Expr expr1> || <Expr expr2>`: return or(cst2ast(expr1), cst2ast(expr2), src=e@\loc);
case (Expr)`<Id x>`: return ref(id("<x>", src=x@\loc), src=x@\loc);
case (Expr)`<Int i>`: return integer(toInt("<i>"), src=e@\loc);
case (Expr)`<Bool b>`: return boolean(b, src=e@\loc);
default: throw "Unhandled expression: <e>";
}
}
AType cst2ast(Type t) {
switch(t) {
case (Type)`boolean`: return \type("boolean", src = t@\loc);
case (Type)`integer`: return \type("integer", src = t@\loc);
}
throw "Not yet implemented";
}
}
4 changes: 2 additions & 2 deletions src/Resolve.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ RefGraph resolve(AForm f) = <us, ds, us o ds>
when Use us := uses(f), Def ds := defs(f);

Use uses(AForm f) {
return {};
return {Use(<id.src, id.name>) | /ref(AId id) := f};
}

Def defs(AForm f) {
return {};
return {Def(<id.name, id.src>) | /question(str _, AId id, AType _, list[AExpr] _) := f};
}
5 changes: 3 additions & 2 deletions src/Syntax.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ extend lang::std::Id;
*/

start syntax Form
= "form" Id "{" Question* "}";
= "form" Id name "{" Question* questions "}";

// TODO: question, computed question, block, if-then-else, if-then
syntax Question
= Str Id ":" Type ( "=" Expr )?
| "if" "(" Expr ")" "{" Question* "}" ( "else" "{" Question* "}" )?
| "if" "(" Expr ")" "{" Question* "}"
| "if" "(" Expr ")" "{" Question* "}" "else" "{" Question* "}"
;

// TODO: +, -, *, /, &&, ||, !, >, <, <=, >=, ==, !=, literals (bool, int, str)
Expand Down

0 comments on commit 8a7a6f6

Please sign in to comment.