Skip to content

Commit

Permalink
support varying within operator signature
Browse files Browse the repository at this point in the history
  • Loading branch information
lenaRB committed May 23, 2024
1 parent 80f1422 commit 4960ecf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/main/antlr/grammar/crml.g4
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ string : STRING;

IDENT : NONDIGIT ( DIGIT | NONDIGIT )* ;

USER_KEYWORD : '\'' (NONDIGIT|SYMBOL) (NONDIGIT|' '|SYMBOL)* '\'';
USER_KEYWORD : '\'' (NONDIGIT|SYMBOL) (NONDIGIT|' '|SYMBOL|DIGIT)* '\'';

fragment CAPS : 'A' .. 'Z' ;
fragment LOWCASE : 'a' .. 'z' ;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/crml/compiler/Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ enum Type {
Boolean is_return_set;
String temp_var_name; // sets a name to avoid variable names like '==0'

String associated_category = null;

public Signature() {
variable_names = new Vector<String>();
variable_types = new Vector<String>();
Expand Down Expand Up @@ -106,4 +108,12 @@ public Signature(String name, List<String> params, List<String> param_names, Str
this.temp_var_name = out_var_name;
}

public void setCategory(String name){
associated_category=name;
}

public String getCategory(){
return associated_category;
}

}
37 changes: 22 additions & 15 deletions src/main/java/crml/compiler/crmlVisitorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,14 @@ else if(ctx.structure_type()!=null)
//TOFIX check that operators being mapped actually exist
for(Category_pairContext i : ctx.category_pair()){
ctg_pairs.put(i.op(0).getText(), i.op(1).getText());
System.out.println(i.op(0).getText() + " : " + i.op(1).getText() + "\n");
}
category_map.addCategory(ctx.id().getText(), ctg_pairs);
System.err.println("Added category " + ctx.id().getText() + "\n");

return new Value("", "Category");
}

// TODO move to association
/*@Override public Value visitAssociation(crmlParser.AssociationContext ctx) {
category_map.add_association(ctx.c_set.getText(), ctx.c_op_name.getText(), ctx.c_name.getText());
return new Value("", "Association");
}*/

@Override public Value visitClass_def(crmlParser.Class_defContext ctx) {
StringBuffer buffer = new StringBuffer();
Value val;
Expand All @@ -221,9 +218,7 @@ else if(ctx.structure_type()!=null)
prefix+=".";

prefix += ctx.id(0).getText();




buffer.append("model "+ ctx.id(0).getText());
// parse class variables
if (ctx.class_var_def()!= null)
Expand All @@ -234,7 +229,6 @@ else if(ctx.structure_type()!=null)
buffer.append(val.contents);
}


if (ctx.type()!=null) {
buffer.append(" extends " + ctx.type().getText());

Expand Down Expand Up @@ -316,15 +310,17 @@ else if (ctx.uninstantiated_def()!=null)
sig.variable_types.add(mtype);
i++;
}

user_operators.put(modelName.toString(), sig);

//check for Category
if(ctx.operator_def().apply_category()!=null)
if(category_map.getCategory(ctx.operator_def().apply_category().id().getText())!= null)
current_category = ctx.operator_def().apply_category().id().getText();
sig.setCategory(ctx.operator_def().apply_category().id().getText());
else throw new ParseCancellationException ("Undefined Category " + ctx.operator_def().apply_category().id().getText());


user_operators.put(modelName.toString(), sig);


// append body
Value exp = visit(ctx.operator_def().exp());
definition.append(localFunctionCalls + "\n");
Expand Down Expand Up @@ -483,6 +479,8 @@ else if (ctx.uninstantiated_def()!=null)
op = category_map.getCategory(current_category).get(ctx.builtin_op().getText());
if (op==null) op = ctx.builtin_op().getText();

System.out.println("Category: " + current_category + " og_op : " + ctx.builtin_op().getText()+ " og_op : "+ op);

if (ctx.binary!=null){
left = visit(ctx.left);
right = visit(ctx.right);
Expand All @@ -500,6 +498,7 @@ else if (ctx.uninstantiated_def()!=null)
op = category_map.getCategory(current_category).get(ctx.runary.getText());
if (op==null) op = ctx.runary.getText();

System.out.println("Category: " + current_category + " og_op : " + ctx.runary.getText()+ " og_op : "+ op);
right = visit(ctx.right);
Value result = apply_runary_op(op, right);
return result;
Expand All @@ -523,7 +522,9 @@ else if (ctx.uninstantiated_def()!=null)
if(current_category!=null) // we check if we should apply the category
op = category_map.getCategory(current_category).get("'"+uc.name+"'");
if (op==null) op = "'"+uc.name+"'";
return apply_user_operator(op, uc.args);


return apply_user_operator(op, uc.args);
}

// expression is a tick
Expand Down Expand Up @@ -675,12 +676,16 @@ public Value visitIf_exp(crmlParser.If_expContext ctx) {


private Value apply_user_operator(String op, List<ExpContext> exp) {

String previous_category = null;
// check if the operator is defined
Signature sign = user_operators.get(op);
if (sign== null)
throw new ParseCancellationException("User operator undefined : " + op + "\n");

if (sign.getCategory() != null){
previous_category = current_category;
current_category = sign.getCategory();
}
String name=op.substring(0, op.length()-1).replace(".", "_")+counter+'\'';

String res;
Expand All @@ -698,6 +703,8 @@ private Value apply_user_operator(String op, List<ExpContext> exp) {

localFunctionCalls.append(res);
counter++;

current_category = previous_category; // restore
return new Value (name+ ".out", sign.return_type);

}
Expand Down

0 comments on commit 4960ecf

Please sign in to comment.