vladfolts / oberonjs

Oberon 07 compiler (written in JavaScript and translates to JavaScript)
MIT License
132 stars 22 forks source link

Integer arithmetics. #19

Closed valexey closed 11 years ago

valexey commented 11 years ago

We should use integer conversion in all arithmetics operations (with integer numbers). Example:

MODULE Test;
IMPORT JS;
PROCEDURE Do*;
 VAR x: INTEGER;
BEGIN
 x:=07FFFFFFFH + 1;
 x:=0FFFFFFFFH * 2;
END Do;

BEGIN
 Do
END Test.

Now compiles to:

var Test = function (JS){

function Do(){
    var x = 0;
    x = 2147483647 + 1;
    x = 4294967295 * 2;
}
Do();
return {
    Do: Do
}
}(this);

But correct is:

var Test = function (JS){

function Do(){
    var x = 0;
    x = (2147483647 + 1) | 0;
    x = (4294967295 * 2) | 0;
}
Do();
return {
    Do: Do
}
}(this);