Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,9 @@ rel[loc from, PathRole r, loc to] getPaths(rel[MODID from, PathRole r, MODID to]
return paths;
}

int closureCounter = 0;
private int closureCounter = 0;

int nextClosure(){
private int nextClosure(){
counter = closureCounter;
closureCounter += 1;
return counter;
Expand All @@ -503,3 +503,5 @@ int nextClosure(){
void resetClosureCounter(){
closureCounter = 0;
}

str generateClosureName() = "$CLOSURE_<nextClosure()>";
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ void collect(current: (Expression) `( <Expression expression> )`, Collector c){

// ---- closure

str generateClosureName() = "$CLOSURE_<nextClosure()>";

void collect(current: (Expression) `<Type returnType> <Parameters parameters> { <Statement+ statements> }`, Collector c){
// TODO: experimental check
//if(!isEmpty(c.getScopeInfo(loopScope())) || inPatternScope(c)){
Expand Down Expand Up @@ -252,7 +250,6 @@ void collectClosure(Expression current, Type returnType, Parameters parameters,

alwaysSucceeds = all(pat <- formals, pat is typedVariable && /(Statement) `fail <Target _>;` := stats);
if(!alwaysSucceeds) dt.canFail = true;

c.defineInScope(parentScope, clos_name, functionId(), current, dt);

if(!returnsViaAll && "<returnType>" != "void"){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ alias FUNID = loc; // function identification

bool isFunctionId(loc l) = l.scheme == "rascal+function";

str getFunctionName(FUNID fuid){
assert isFunctionId(fuid);
k = findLast(fuid.path, "/");
return fuid.path[k+1..];
}

alias CONSID = loc; // constructor identification

bool isConstructorId(loc l) = l.scheme == "rascal+constructor";

alias FLDID = loc; // field identification

bool isFieldId(loc l) = l.scheme == "rascal+field";
bool isFieldId(loc l) = l.scheme == "rascal+field";


alias ADTID = loc; // ADT
bool isADTId(loc l) = l.scheme == "rascal+adt";
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public PathConfig getAllSrcREPOPathConfig(bool keep = false) {
REPO + "typepal/src",
LSP_REPO + "src/main/rascal/library",
LSP_REPO + "src/main/rascal/lsp",
LSP_REPO + "src/test/rascal",
//LSP_REPO + "src/test/rascal",
REPO + "php-analysis/src/main/rascal"
],
[ ],
Expand Down
24 changes: 22 additions & 2 deletions src/org/rascalmpl/compiler/lang/rascalcore/compile/Examples/A.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,25 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
}
module lang::rascalcore::compile::Examples::A

extend lang::rascalcore::compile::Examples::B;

data Symbol
= \sort(str name)
| \adt(str name, list[Symbol] parameters)
;
bool subtype(Symbol::\sort(_), Symbol::\adt("Tree", _)) = true;


// import List;

// @javaClass{org.rascalmpl.library.Prelude}
// public java map[&K, set[&V]] index(lrel[&K, &V] R);



// list[&T] sort(list[&T] lst) =
// sort(lst, bool (&T a, &T b) { return a < b; } );

// @javaClass{org.rascalmpl.library.Prelude}
// java list[&T] sort(list[&T] l, bool (&T a, &T b) less) ;


Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,3 @@ POSSIBILITY OF SUCH DAMAGE.
@bootstrapParser
module lang::rascalcore::compile::Examples::Tst6

data Symbol
= \label(str name, Symbol symbol);

Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ public void translateToplevel((Toplevel) `<Declaration decl>`) {
// -- variable declaration ------------------------------------------

void translateDecl(d: (Declaration) `<Tags tags> <Visibility visibility> <Type tp> <{Variable ","}+ variables> ;`) {
str module_name = asUnqualifiedName(getRascalModuleName());
enterFunctionScope("<module_name>_init");
// str module_name = asUnqualifiedName(getRascalModuleName());
// enterFunctionScope("<module_name>_init");
MODID mid = moduleName2moduleId(getRascalModuleName());
enterFunctionScope(mid + "/init");
for(var <- variables){
unescapedVarName = unescapeName("<var.name>");
addVariableToModule(muModuleVar(getType(tp), unescapedVarName));
if(var is initialized) {
init_code = translate(var.initial);
asg = muAssign( muVar(unescapedVarName, getRascalModuleNameUnderscores(), -1, filterOverloads(getType(tp), {variableId()}), variableId()), init_code);
asg = muAssign( muVar(unescapedVarName, mid, -1, filterOverloads(getType(tp), {variableId()}), variableId()), init_code);
addVariableInitializationToModule(asg);
}
}
Expand Down Expand Up @@ -146,16 +148,17 @@ private void generateGettersForAdt(AType adtType, loc module_scope, set[AType] c
kwfield2cons += <kwFieldName, kwType, consType>;
//str fuid = getGetterNameForKwpField(consType, kwFieldName);
str getterName = unescapeAndStandardize("$getkw_<adtName>_<consName>_<kwFieldName>");
getterId = |rascal+kwfield:///| + getterName;

if(getterName notin generated_getters){
generated_getters += getterName;

getterType = afunc(kwType, [consType], []);
consVar = muVar(consName, getterName, 0, consType, constructorId());
consVar = muVar(consName, getterId, 0, consType, constructorId());

defExpCode = promoteVarsToFieldReferences(translate(defaultExp), consType, consVar, kw.definingModule);
body = muReturn1(kwType, muIfElse(muIsKwpConstructorDefined(consVar, kwFieldName), muGetKwFieldFromConstructor(kwType, consVar, kwFieldName), defExpCode));
addFunctionToModule(muFunction(getterName, getterName, getterType, [consVar], [], [], "", false, true, false, {}, {}, {}, getModuleScope(), [], (), body));
addFunctionToModule(muFunction(getterName, getterId, getterType, [consVar], [], [], |global-scope:///|, false, true, false, {}, {}, {}, getModuleScope(), [], (), body));
} else {
println("In generated_getters: <getterName>");

Expand All @@ -171,10 +174,11 @@ private void generateGettersForAdt(AType adtType, loc module_scope, set[AType] c
conses = kwfield2cons[kwFieldName];
//str fuid = getGetterNameForKwpField(adtType, kwFieldName);
str getterName = unescapeAndStandardize("$getkw_<adtName>_<kwFieldName>");
getterId = |rascal+kwfield:///| + getterName;

returnType = lubList(conses<0>);
getterType = afunc(returnType, [adtType], []);
adtVar = muVar(adtName, getterName, 0, adtType, dataId());
adtVar = muVar(adtName, getterId, 0, adtType, dataId());
failCode = muFailReturn(returnType);
for(Keyword kw <- common_keyword_fields, kw has defaultExp, isContainedIn(kw.defaultExp.src, module_scope, l2p)){
kwType = kw.fieldType;
Expand All @@ -191,7 +195,7 @@ private void generateGettersForAdt(AType adtType, loc module_scope, set[AType] c
]
+ failCode
);
addFunctionToModule(muFunction(getterName, getterName, getterType, [adtVar], [], [], "", false, true, false, {}, {}, {}, getModuleScope(), [], (), body));
addFunctionToModule(muFunction(getterName, getterId, getterType, [adtVar], [], [], |global-scope:///|, false, true, false, {}, {}, {}, getModuleScope(), [], (), body));
}

/*
Expand All @@ -208,16 +212,17 @@ private void generateGettersForAdt(AType adtType, loc module_scope, set[AType] c

//str fuid = getGetterNameForKwpField(adtType, kwFieldName);
str getterName = unescapeAndStandardize("$getkw_<adtName>_<kwFieldName>");
getterId = |rascal+kwfield:///| + getterName;
if(getterName == "$getkw_Tree_message"){ // TODO: remove when annotations are gone
continue;
}

getterType = afunc(kwType, [adtType], []);
adtVar = muVar(getterName, getterName, 0, adtType, variableId());
adtVar = muVar(getterName, getterId, 0, adtType, variableId());

defExprCode = promoteVarsToFieldReferences(translate(defaultExp), adtType, adtVar, kw.definingModule);
body = muReturn1(kwType, muIfElse(muIsKwpConstructorDefined(adtVar, kwFieldName), muGetKwFieldFromConstructor(kwType, adtVar, kwFieldName), defExprCode));
addFunctionToModule(muFunction(getterName, getterName, getterType, [adtVar], [], [], "", false, true, false, {}, {}, {}, getModuleScope(), [], (), body));
addFunctionToModule(muFunction(getterName, getterId, getterType, [adtVar], [], [], |global-scope:///|, false, true, false, {}, {}, {}, getModuleScope(), [], (), body));
}


Expand All @@ -241,52 +246,35 @@ private set[TypeVar] getTypeVarsinFunction(FunctionDeclaration fd){

public void translateFunctionDeclaration(FunctionDeclaration fd){
//println("r2mu: Compiling \uE007[<fd.signature.name>](<fd.src>)");
inScope = topFunctionScope();
funsrc = fd.src;
useTypeParams = getTypeVarsinFunction(fd);
enterFunctionDeclaration(funsrc, !isEmpty(useTypeParams));

try {
ttags = translateTags(fd.tags);
tmods = translateModifiers(fd.signature.modifiers);
// if(ignoreTest(ttags)){
// // The type checker does not generate type information for ignored functions
// addFunctionToModule(muFunction("$ignored_<prettyPrintName(fd.signature.name)>_<fd.src.offset>",
// prettyPrintName(fd.signature.name),
// afunc(abool(),[],[]),
// [],
// [],
// [],
// inScope,
// false,
// true,
// false,
// {},
// {},
// {},
// fd.src,
// tmods,
// ttags,
// muReturn1(abool(), muCon(false))));
// return;
// }

if(ttags["ignore"]? || ttags["ignoreCompiler"]?){
return;
}
inScope = topFunctionScope();
funsrc = fd.src;
funId = getLogicalLoc(funsrc);
useTypeParams = getTypeVarsinFunction(fd);
enterFunctionDeclaration(funId, !isEmpty(useTypeParams));

fname = prettyPrintName(fd.signature.name);
ftype = getFunctionType(funsrc);
ftype = getFunctionType(funId);
resultType = ftype.ret;
bool isVarArgs = ftype.varArgs;
nformals = size(ftype.formals);
fuid = convert2fuid(funsrc);

enterFunctionScope(fuid);
enterFunctionScope(funId);

//// Keyword parameters
lrel[str name, AType atype, MuExp defaultExp] kwps = translateKeywordParameters(fd.signature.parameters);

my_btscopes = getBTScopesParams([ft | ft <- fd.signature.parameters.formals.formals], fname);
mubody = muBlock([]);
if(ttags["javaClass"]?){
params = [ muVar(ftype.formals[i].alabel, fuid, i, ftype.formals[i], formalId()) | i <- [ 0 .. nformals] ];
mubody = muReturn1(resultType, muCallJava("<fd.signature.name>", ttags["javaClass"], ftype, params, fuid));
params = [ muVar(ftype.formals[i].alabel, funId, i, ftype.formals[i], formalId()) | i <- [ 0 .. nformals] ];
mubody = muReturn1(resultType, muCallJava("<fd.signature.name>", ttags["javaClass"], ftype, params, funId));
} else if(fd is \default){ // function declaration with statements
body_code = [ translate(stat, my_btscopes) | stat <- fd.body.statements ];
if(isVoidAType(ftype.ret)) body_code += muReturn0();
Expand All @@ -313,12 +301,12 @@ public void translateFunctionDeclaration(FunctionDeclaration fd){
}

leaveSignatureSection();
externals = getExternalRefs(tbody, fuid, formalVars);
extendedFormalVars = getExtendedFunctionFormals(fd.src, fuid);
externals = getExternalRefs(tbody, funId, formalVars);
extendedFormalVars = getExtendedFunctionFormals(funId, funId);
localRefs = getLocalRefs(tbody);

addFunctionToModule(muFunction(prettyPrintName(fd.signature.name),
fuid,
funId,
ftype,
formalVars,
extendedFormalVars,
Expand All @@ -329,7 +317,7 @@ public void translateFunctionDeclaration(FunctionDeclaration fd){
isMemo,
externals,
localRefs,
getKeywordParameterRefs(tbody, fuid),
getKeywordParameterRefs(tbody, funId),
fd.src,
tmods,
ttags,
Expand Down Expand Up @@ -395,18 +383,18 @@ public set[MuExp] getLocalRefs(MuExp exp)
/*
* Get all variables that have been introduced outside the given function scope
*/
public set[MuExp] getExternalRefs(MuExp exp, str fuid, list[MuExp] formals){
public set[MuExp] getExternalRefs(MuExp exp, loc fuid, list[MuExp] formals){
formalNames = {f.name | f <- formals};
res = { v1 | /v:muVar(str name2, str fuid2, int pos, AType t, IdRole idRole) := exp, pos >= 0, name2 notin formalNames, fuid2 != fuid, fuid2 != "", t1 := unsetRec(t, "alabel"), v1 := v[atype=t1]};
res = { v1 | /v:muVar(str name2, loc fuid2, int pos, AType t, IdRole idRole) := exp, pos >= 0, name2 notin formalNames, fuid2 != fuid, fuid2 != |rascal:///|, t1 := unsetRec(t, "alabel"), v1 := v[atype=t1]};
//println("getExternalRefs, <fuid>, <formals>: <res>");
return res;
}
/*
* Get all keyword variables that have introduced outside the given function scope
*/
public set[MuExp] getKeywordParameterRefs(MuExp exp, str fuid){
public set[MuExp] getKeywordParameterRefs(MuExp exp, loc fuid){
//println("getKeywordParameterRefs(<exp>, <fuid>)");
res = { unsetRec(v, "alabel") | /v:muVarKwp(str _, str fuid2, AType _) := exp, fuid2 != fuid };
res = { unsetRec(v, "alabel") | /v:muVarKwp(str _, loc fuid2, AType _) := exp, fuid2 != fuid };
//println("getKeywordParameterRefs =\> <res>");
return res;
}
Expand Down Expand Up @@ -451,7 +439,7 @@ private MuExp returnFromFunction(MuExp body, AType ftype, list[MuExp] formalVars
parameters = { unset(par, "alabel") | /par:aparameter(name, bound) := ftype };

if(!isEmpty(parameters) && /muReturn1(_,_) := res){
str fuid = topFunctionScope();
loc fuid = topFunctionScope();
result = muTmpIValue(nextTmp("result"), fuid, ftype.ret);
res = visit(res){
case muReturn1(_, e) => muBlock([ muConInit(result, e),
Expand All @@ -468,7 +456,7 @@ private MuExp returnFromFunction(MuExp body, AType ftype, list[MuExp] formalVars

private MuExp functionBody(MuExp body, AType ftype, list[MuExp] formalVars, bool isMemo){
if(isMemo){
str fuid = topFunctionScope();
loc fuid = topFunctionScope();
result = muTmpIValue(nextTmp("result"), fuid, avalue());
return muCheckMemo(ftype, formalVars, body);
} else {
Expand All @@ -480,7 +468,7 @@ public tuple[list[MuExp] formalVars, MuExp funBody] translateFunction(str fname,
// Create a loop label to deal with potential backtracking induced by the formal parameter patterns

list[Pattern] formalsList = [f | f <- formals];
str fuid = topFunctionScope();
loc fuid = topFunctionScope();
my_btscopes = getBTScopesParams(formalsList, fname);
parameters = { unset(par, "alabel") | /par:aparameter(name, bound) := ftype };

Expand Down
Loading