Commits

Lionel Flandrin committed bd65a7c

Added modulo operator

Comments (0)

Files changed (6)

   return *this;
 }
 
+Integer &Integer::operator%=(const Integer &i)
+{
+  mpz_mod(this->v, this->v, i.v);
+
+  return *this;
+}
+
 Integer &Integer::operator<<=(const Integer &i)
 {
   unsigned long s;
     Integer &operator&=(const Integer &);
     Integer &operator|=(const Integer &);
     Integer &operator^=(const Integer &);
+    Integer &operator%=(const Integer &);
     Integer &operator<<=(const Integer &);
     Integer &operator>>=(const Integer &);
 

tests/expected-output.txt

 10000
 10
 10
+0
+1
+2
 1
 0
 0
 u   = 0.000001
 var = 43
           ^^^^^ division by 0
+              ^^^^^^^ division by 0
           ^^^^^^^^^^^^^^^^ division by 0
 5
          ^ syntax error
 10 ** 4
 abs(10)
 abs(-10)
+50 % 10
+51 % 10
+0xf6 % 4
 
 # boolean
 1 & 1
 
 # Error handling
 42 + 1 / 0
+42 + 1 % (1 - 1)
 42 + 2 / ( 4 - 5 + 1)
 1 + ( 1  + 3
 )
 \<\< { return Parser::token::LSHIFT; }
 \>\> { return Parser::token::RSHIFT; }
 
-[-+*/:|^&~,=] { return *yytext; }
+[-+*/:|^&~,=%] { return *yytext; }
 
 "("           { PUSH_STATE(inparens); return *yytext; }
 <inparens>")" { POP_STATE(); return *yytext; }
       YYERROR;
     }
   }
+  | exp '%' exp {
+     if (!is_integer(this->lexer, $1, @1)) {
+      delete $3;
+      YYERROR;
+    }
+    if (!is_integer(this->lexer, $3, @3)) {
+      delete $1;
+      YYERROR;
+    }
+    if (*$3 == 0) {
+      delete $1;
+      delete $3;
+      this->error(@3, "division by 0");
+      YYERROR;
+    }
+
+    $$ = $1;
+    $$->as_int() %= $3->as_int();
+    delete $3;
+  }
   | '-' exp %prec NEG { $$ = $2; $$->neg(); }
   | '+' exp %prec NEG { $$ = $2; }
   | exp POW exp {