Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check #5

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

40 changes: 35 additions & 5 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) | Question q2 <- x0], [cst2ast(q2) | Question q2 <- x1], src=q@\loc);
}

throw "Not yet implemented <q>";
}

AExpr cst2ast(Expr e) {
switch (e) {
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);

// etc.
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";
}
}
114 changes: 107 additions & 7 deletions src/Check.rsc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module Check

import IO;
import AST;
import Resolve;
import Message; // see standard library
import Set;

data Type
= tint()
Expand All @@ -17,18 +18,62 @@ alias TEnv = rel[loc def, str name, str label, Type \type];
// To avoid recursively traversing the form, use the `visit` construct
// or deep match (e.g., `for (/question(...) := f) {...}` )
TEnv collect(AForm f) {
return {};
return {< q.src, id.name, label, AType2Type(t)> | /q:question(str label, AId id, AType t, _) := f};
}

Type AType2Type(AType at){
switch(at){
case \type("boolean"): return tbool();
case \type("integer"): return tint();

default: {return tunknown();}
}
}

set[Message] check(AForm f, TEnv tenv, UseDef useDef) {
return {};
set[Message] check(AForm f, TEnv tenv, UseDef useDef){
set[Message] msgs = {};
for(/q:question(str _, AId _, AType _, list[AExpr] _) := f){
msgs += check(q, tenv, useDef);
}
return msgs;
}

// - produce an error if there are declared questions with the same name but different types.
// - duplicate labels should trigger a warning
// - the declared type computed questions should match the type of the expression.

set[Message] check(AQuestion q, TEnv tenv, UseDef useDef) {
return {};
set[Message] msgs = {};
// obtain label
str label;
Type tp;
list[AExpr] aexpr = [];
for(/question(str l, _, AType t, list[AExpr] axprs) := q){
label = l;
tp = AType2Type(t);
aexpr = axprs;
}
// same label twice
list[str] labels = [label | /<_, _, label, _> := tenv];

if(labels != [label]){
msgs += warning("<q.src>" + "Duplicate label");
}

// same label, different types
set[Type] types = {t | <_, _, label, Type t> := tenv};
if(types != {AType2Type(q.\type)}){
msgs += error("<q.src>" + "Same label, different type");
}

// type of expression != type of question
if(aexpr != []){
Type etp = typeOf(aexpr[0], tenv, useDef);
if(etp != tunknown() && etp != tp){
msgs += error("<q.src>" + "Type of expression does not match type of question");
}
}
return msgs;
}

// Check operand compatibility with operators.
Expand All @@ -49,11 +94,66 @@ set[Message] check(AExpr e, TEnv tenv, UseDef useDef) {

Type typeOf(AExpr e, TEnv tenv, UseDef useDef) {
switch (e) {
case ref(id(_, src = loc u)):

// etc.
case brackets(AExpr ex):
return typeOf(ex, tenv, useDef);
case not(AExpr ex):
return tbool();
case divide(AExpr expr1, AExpr expr2): {
return tunknown();
}
case multiply(AExpr expr1, AExpr expr2): {
return tint();
}
case add(AExpr expr1, AExpr expr2): {
return tint();
}
// case add(AExpr ex1, AExpr ex2): {
// if((typeOf(ex1, tenv, useDef) == tint())
// && (typeOf(ex2, tenv, useDef) == tint())){
// return tint();
// } else {
// return tunknown();
// }
//}
case subtract(AExpr expr1, AExpr expr2): {
return tint();
}
case less(AExpr expr1, AExpr expr2): {
return tbool();
}
case gtr(AExpr expr1, AExpr expr2): {
return tbool();
}
case leq(AExpr expr1, AExpr expr2): {
return tbool();
}
case geq(AExpr expr1, AExpr expr2): {
return tbool();
}
case eq(AExpr expr1, AExpr expr2): {
return tbool();
}
case neq(AExpr expr1, AExpr expr2): {
return tbool();
}
case and(AExpr expr1, AExpr expr2): {
return tbool();
}
case or(AExpr expr1, AExpr expr2): {
return tbool();
}
case ref(str x, src = loc u):

if (<u, loc d> <- useDef, <d, x, _, Type t> <- tenv) {
return t;
}
// etc.
case integer(int n):
return tint();
case boolean(str \bool):
return tbool();

}
return tunknown();
}
Expand Down
Loading