Sunday, April 22, 2012

Movimentum - Building the model cont'd

After the things and their anchors, we build the steps. For the time tracking, we keep a global variable in the parser that is set or incremented, depending on the format of the @ directive:

    time
      : '@' n=number         { _currentTime = n; }
      | '@' '+' n=number     { _currentTime += n; }
      ;
   
I therefore changed the creation of a Step in the script rule to

          ...
          )*        { sts.Add(new Step(_currentTime, cs)); }
          ...

The constraint rule creates on of the three constraint types:

    constraint returns [Constraint result]
      : v1=vectorexpr
        '='
        v2=vectorexpr
        ';'     { result = new VectorEqualityConstraint(v1, v2); }
      | i=IDENT
        '='
        v=scalarexpr
        ';'     { result = new ScalarEqualityConstraint(

                                   $i.Text, v); }
      | i=IDENT {{ ScalarInequalityOperator op = 0; }}
        ( '<'   { op = ScalarInequalityOperator.LE; }
        | '>'   { op = ScalarInequalityOperator.GE; }
        | '<='  { op = ScalarInequalityOperator.LT; }
        | '>='  { op = ScalarInequalityOperator.GT; }
        )
        s=scalarexpr
        ';'     { result = new ScalarInequalityConstraint(

                                   $i.Text, op, s); }
      ;
In a similar way, we build vector expression. Here is the top rule for a vector expression:

    vectorexpr returns [VectorExpr result]
      : v=vectorexpr2        { result = v; }
        (                    {{ BinaryVectorOperator op = 0; }}
          ( '+'              {{ op = BinaryVectorOperator.PLUS; }}
          | '-'              {{ op = BinaryVectorOperator.MINUS; }}
          )
          v=vectorexpr2      { result = new BinaryVectorExpr(
                                                 result, op, v); }
        )*
      ;

All the rest is similar and, overall, simple. The only change I made is to Anchor: Originally, I wanted to pass in a ready-made Thing. But this would require to move the Thing collection somewhere where we can access it - I defer this to some later semantic phase of the interpreter.

The complete grammar file with actions is here.

No comments:

Post a Comment