Skip to content

Commit

Permalink
Merge pull request #53 from lenaRB/period-operators
Browse files Browse the repository at this point in the history
Period operators
  • Loading branch information
lenaRB committed Dec 15, 2023
2 parents 1b9ef2b + 2ea4f99 commit a2e71db
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
15 changes: 11 additions & 4 deletions src/main/antlr/grammar/crml.g4
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,31 @@ integrate : 'integrate' exp 'on' exp;
tick : 'tick' id;

exp : sub_exp | id | constant | constructor | sum |trim | proj | period_op
| uright=user_keyword right=exp | left=exp ubinary=user_keyword right=exp | left=exp uleft=user_keyword
| left=exp binary=builtin_op right=exp | right=exp runary=builtin_op | lunary=builtin_op left=exp
| right=exp runary=right_op
| lunary=builtin_op left=exp
| left=exp binary=builtin_op right=exp
| uright=user_keyword right=exp
| left=exp ubinary=user_keyword right=exp
| left=exp uleft=user_keyword

| 'element' | 'terminate' | when_exp | exp 'at' at=exp
| integrate | tick |crml_component_reference | if_exp | set_def | 'evaluate' exp ;

if_exp : 'if' if_e=exp 'then' then_e=exp ('else' else_e=exp);

constructor : 'new' type exp;

period_op : ('['| ']') exp ',' exp ('['| ']') ;
period_op : lb=('['| ']') exp ',' exp rb=('['| ']') ;

op : builtin_op|user_keyword
;

right_op : 'start' | 'end';

builtin_op : 'and' | '*' | '+' | '-' | '/' | 'with' | 'master' | 'on' | 'filter'
| '<=' | '<' | '>=' | '>' | '<>' | 'par' | '==' |
'pre' | 'not'| '-' | 'card' | 'or' | '^' |
'start' | 'end' | 'mod' |
'mod' |
'exp' | 'log' | 'log10' |
'cos' |'acos' | 'sin' | 'asin' ;

Expand Down
34 changes: 30 additions & 4 deletions src/main/java/crml/compiler/crmlVisitorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,17 +478,21 @@ else if (ctx.uninstantiated_def()!=null)
left = visit(ctx.left);
Value result = apply_lunary_op(ctx.builtin_op().getText(), left);
return result;
} else if(ctx.runary!= null) {
}

if(ctx.runary!= null) {
right = visit(ctx.right);
Value result = apply_runary_op(ctx.builtin_op().getText(), right);
Value result = apply_runary_op(ctx.right_op().getText(), right);
return result;
}

// if the expression is in parenthesis
if(ctx.sub_exp()!=null)
return visit(ctx.sub_exp().exp());


if(ctx.period_op()!=null){
return visit(ctx.period_op());
}

// expression is a tick
if(ctx.tick() != null) {
Expand Down Expand Up @@ -528,10 +532,32 @@ private UserOperatorCall reconstructUserOperator(ExpContext ctx, String string,
}

@Override
public Value visitSet_def(crmlParser.Set_defContext cxt) {
public Value visitSet_def(crmlParser.Set_defContext ctx) {
return new Value ("Set", "{}", true);
}

@Override
public Value visitPeriod_op(crmlParser.Period_opContext ctx) {

//String periodType = types_mapping.get("Period");

//TODO add typechecking

Value left = visit(ctx.exp(0));
Value right = visit(ctx.exp(0));

Boolean lborder = (ctx.lb.getText().equals("["));
Boolean rborder = (ctx.rb.getText().equals("]"));

String code =
"CRMLtoModelica.Types.CRMLPeriod(left=" + left.contents +
", right=" + right.contents +
",lb=" +lborder.toString() +
",rb=" +rborder.toString()+");";

return new Value (code, "Period", false);
}

@Override
public Value visitId(crmlParser.IdContext ctx){
VariableData.VariableType v_type = variableTable.getVariableInfo(ctx.getText());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
model CountInside is {
// Filter clock ticks inside a time period
Operator [ Clock ] Clock C 'inside' Period P
= C filter (tick C >= P start) and (tick C <= P end);
= C filter ((tick C >= P start) and (tick C <= P end));

// Operators on clocks
// Count the occurrences of events inside a time period
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
model CountInside_no_ext is {
// Filter clock ticks inside a time period
Operator [ Clock ] Clock C 'inside' Period P
= C filter (tick C >= P start) and (tick C <= P end);
= C filter ((tick C >= P start) and (tick C <= P end));

// Operators on clocks
// Count the occurrences of events inside a time period
Expand Down

0 comments on commit a2e71db

Please sign in to comment.