Skip to content

Commit

Permalink
category type update, change in grammar to separate apply out
Browse files Browse the repository at this point in the history
  • Loading branch information
lenaRB committed May 23, 2024
1 parent c80d908 commit 300a06c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
5 changes: 4 additions & 1 deletion src/main/antlr/grammar/crml.g4
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ template : 'Template' (id | user_keyword)+ '=' exp ';' ;

class_params : '(' (id '=' exp)+ ')';

operator_def : (type id | user_keyword)+ '=' ('apply' assoc=id 'on')? exp ;
operator_def : (type id | user_keyword)+ '=' apply_category? exp ;

apply_category : 'apply' assoc=id 'on';

type_def : 'type' id ('extends' type arg_list? id?)? ('{' class_var_def * '}' )? ;

Expand Down Expand Up @@ -79,6 +81,7 @@ integrate : 'integrate' exp 'on' exp;
tick : 'tick' id;

exp : sub_exp | id | constant | constructor | sum |trim | proj | period_op
//| 'apply' cat=id 'on' '(' exp ')'
| right=exp runary=right_op
| lunary=builtin_op left=exp
| left=exp binary=builtin_op right=exp
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/crml/compiler/CategoryMapping.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package crml.compiler;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import grammar.crmlParser.Category_pairContext;

public class CategoryMapping {

HashMap<String, HashMap <String, String>> categoryMap;

public CategoryMapping(){
categoryMap = new HashMap<>();
}

public void addCategory(String name, HashMap <String, String> operator_mapping) {

Expand Down
27 changes: 9 additions & 18 deletions src/main/java/crml/compiler/crmlVisitorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.aventstack.extentreports.model.Category;

import grammar.crmlBaseVisitor;
import grammar.crmlParser;
import grammar.crmlParser.Category_pairContext;
Expand Down Expand Up @@ -321,7 +319,12 @@ else if (ctx.uninstantiated_def()!=null)

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();
else throw new ParseCancellationException ("Undefined Category " + ctx.operator_def().apply_category().id().getText());

// append body
Value exp = visit(ctx.operator_def().exp());
definition.append(localFunctionCalls + "\n");
Expand All @@ -335,6 +338,9 @@ else if (ctx.uninstantiated_def()!=null)

//restore local function calls
localFunctionCalls = store_localFunctionCalls;

// restore category
current_category = null;

return new Value (definition.toString(), "Operator");
}
Expand Down Expand Up @@ -446,21 +452,6 @@ else if (ctx.uninstantiated_def()!=null)

@Override public Value visitExp(crmlParser.ExpContext ctx) {
Value right, left;

// if the expression is a category operator
if(ctx.cat!=null){
//check if the category exists
if(category_map.getCategory(ctx.cat.getText())!= null){
// set category
current_category = ctx.cat.getText();

Value res= visitExp(ctx.exp(0));

// unset category
current_category = null;
return res;
}
}

// if the expression is a constructor
if(ctx.constructor() != null) {
Expand Down

0 comments on commit 300a06c

Please sign in to comment.