Skip to content

Commit

Permalink
Merge branch 'master' of github.com:skulpt/skulpt
Browse files Browse the repository at this point in the history
  • Loading branch information
bnmnetp committed Jun 20, 2014
2 parents 6e2be54 + 2588c55 commit 6ac5a4c
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 79 deletions.
2 changes: 1 addition & 1 deletion doc/static/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ $(function () {
//evaluate it if nessecary
lines.push("evaluationresult = " + lines.pop());
//print the result if not None
lines.push("if not evaluationresult == None: print evaluationresult");
lines.push("if not evaluationresult == None: print repr(evaluationresult)");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion repl/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ while (true) {
//evaluate it if nessecary
lines.push("evaluationresult = " + lines.pop());
//print the result if not None
lines.push("if not evaluationresult == None: print evaluationresult");
lines.push("if not evaluationresult == None: print repr(evaluationresult)");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Sk.abstr.numOpAndPromote = function(a, b, opfn)
{
var ans = opfn(a, b);
// todo; handle float Removed RNL (bugs in lng, and it should be a question of precision, not magnitude -- this was just wrong)
if ( (ans > Sk.builtin.lng.threshold$ || ans < -Sk.builtin.lng.threshold$) && Math.floor(ans) === ans) {
if ( (ans > Sk.builtin.nmber.threshold$ || ans < -Sk.builtin.nmber.threshold$) && Math.floor(ans) === ans) {
return [Sk.builtin.lng.fromInt$(a), Sk.builtin.lng.fromInt$(b)];
} else
return ans;
Expand Down
2 changes: 1 addition & 1 deletion src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ function parsenumber(c, s, lineno)
}

// Convert to long
if (val > Sk.builtin.lng.threshold$
if (val > Sk.builtin.nmber.threshold$
&& Math.floor(val) === val
&& (s.indexOf('e') === -1 && s.indexOf('E') === -1))
{
Expand Down
105 changes: 52 additions & 53 deletions src/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ Sk.builtin.asnum$ = function(a) {
return a.toInt$();
}
if (a.constructor === Sk.builtin.biginteger) {
if ((a.trueCompare(new Sk.builtin.biginteger(Sk.builtin.lng.threshold$)) > 0)
|| (a.trueCompare(new Sk.builtin.biginteger(-Sk.builtin.lng.threshold$)) < 0)) {
if ((a.trueCompare(new Sk.builtin.biginteger(Sk.builtin.nmber.threshold$)) > 0)
|| (a.trueCompare(new Sk.builtin.biginteger(-Sk.builtin.nmber.threshold$)) < 0)) {
return a.toString();
}
return a.intValue();
Expand Down Expand Up @@ -174,21 +174,21 @@ Sk.builtin.asnum$nofloat = function(a) {
}
goog.exportSymbol("Sk.builtin.asnum$nofloat", Sk.builtin.asnum$nofloat);

Sk.builtin.round = function round(number, ndigits)
{
Sk.builtin.round = function round(number, ndigits) {
var result, multiplier;

Sk.builtin.pyCheckArgs("round", arguments, 1, 2);

if (!Sk.builtin.checkNumber(number)) {
throw new Sk.builtin.TypeError("a float is required");
throw new Sk.builtin.TypeError("a float is required");
}

if ((ndigits !== undefined) && !Sk.misceval.isIndex(ndigits)) {
throw new Sk.builtin.TypeError("'" + Sk.abstr.typeName(ndigits) + "' object cannot be interpreted as an index");
};
}

if (ndigits === undefined) {
ndigits = 0;
};
}

number = Sk.builtin.asnum$(number);
ndigits = Sk.misceval.asIndex(ndigits);
Expand Down Expand Up @@ -926,71 +926,70 @@ Sk.builtin.hasattr = function hasattr(obj,attr) {
Sk.builtin.pow = function pow(a, b, c) {
Sk.builtin.pyCheckArgs("pow", arguments, 2, 3);

if (c instanceof Sk.builtin.none)
if (c instanceof Sk.builtin.none) {
c = undefined;
}

var a_num = Sk.builtin.asnum$(a);
var b_num = Sk.builtin.asnum$(b);
var c_num = Sk.builtin.asnum$(c);

if (!Sk.builtin.checkNumber(a) || !Sk.builtin.checkNumber(b))
{
if (c === undefined)
{
throw new Sk.builtin.TypeError("unsupported operand type(s) for pow(): '" + Sk.abstr.typeName(a) + "' and '" + Sk.abstr.typeName(b) + "'");
}
else
{
throw new Sk.builtin.TypeError("unsupported operand type(s) for pow(): '" + Sk.abstr.typeName(a) + "', '" + Sk.abstr.typeName(b) + "', '" + Sk.abstr.typeName(c) + "'");
}
if (c === undefined)
{
throw new Sk.builtin.TypeError("unsupported operand type(s) for pow(): '" + Sk.abstr.typeName(a) + "' and '" + Sk.abstr.typeName(b) + "'");
}
throw new Sk.builtin.TypeError("unsupported operand type(s) for pow(): '" + Sk.abstr.typeName(a) + "', '" + Sk.abstr.typeName(b) + "', '" + Sk.abstr.typeName(c) + "'");
}
if (a_num < 0 && b.skType === Sk.builtin.nmber.float$)
{
throw new Sk.builtin.ValueError("negative number cannot be raised to a fractional power");
throw new Sk.builtin.ValueError("negative number cannot be raised to a fractional power");
}

if (c === undefined)
{
var res = Math.pow(a_num, b_num);
if ((a.skType === Sk.builtin.nmber.float$ || b.skType === Sk.builtin.nmber.float$) || (b_num < 0))
{
return new Sk.builtin.nmber(res, Sk.builtin.nmber.float$);
}
else if (a instanceof Sk.builtin.lng || b instanceof Sk.builtin.lng)
{
return new Sk.builtin.lng(res);
}
else
{
return new Sk.builtin.nmber(res, Sk.builtin.nmber.int$);
}
if ((a.skType === Sk.builtin.nmber.float$ || b.skType === Sk.builtin.nmber.float$) || (b_num < 0))
{
return new Sk.builtin.nmber(Math.pow(a_num, b_num), Sk.builtin.nmber.float$);
}

var left = new Sk.builtin.nmber(a_num, Sk.builtin.nmber.int$);
var right = new Sk.builtin.nmber(b_num, Sk.builtin.nmber.int$);
var res = left.nb$power(right);

if (a instanceof Sk.builtin.lng || b instanceof Sk.builtin.lng)
{
return new Sk.builtin.lng(res);
}

return res;
}
else
{
if (!Sk.builtin.checkInt(a) || !Sk.builtin.checkInt(b) || !Sk.builtin.checkInt(c))
{
throw new Sk.builtin.TypeError("pow() 3rd argument not allowed unless all arguments are integers");
}
if (b_num < 0)
{
throw new Sk.builtin.TypeError("pow() 2nd argument cannot be negative when 3rd argument specified");
}
if (!Sk.builtin.checkInt(a) || !Sk.builtin.checkInt(b) || !Sk.builtin.checkInt(c))
{
throw new Sk.builtin.TypeError("pow() 3rd argument not allowed unless all arguments are integers");
}
if (b_num < 0)
{
throw new Sk.builtin.TypeError("pow() 2nd argument cannot be negative when 3rd argument specified");
}

if ((a instanceof Sk.builtin.lng || b instanceof Sk.builtin.lng || c instanceof Sk.builtin.lng)
|| (Math.pow(a_num, b_num) === Infinity))
{
// convert a to a long so that we can use biginteger's modPowInt method
a = new Sk.builtin.lng(a);
return a.nb$power(b, c);
}
else
{
var ret = new Sk.builtin.nmber(Math.pow(a_num, b_num), Sk.builtin.nmber.int$);
return ret.nb$remainder(c);
}
if ((a instanceof Sk.builtin.lng || b instanceof Sk.builtin.lng || c instanceof Sk.builtin.lng)
|| (Math.pow(a_num, b_num) === Infinity))
{
// convert a to a long so that we can use biginteger's modPowInt method
a = new Sk.builtin.lng(a);
return a.nb$power(b, c);
}
else
{
var ret = new Sk.builtin.nmber(Math.pow(a_num, b_num), Sk.builtin.nmber.int$);
return ret.nb$remainder(c);
}
}

}
};

Sk.builtin.quit = function quit(msg) {
var s = new Sk.builtin.str(msg).v;
Expand Down
13 changes: 9 additions & 4 deletions src/int.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Sk.builtin.int_ = function (x, base) {
if (x instanceof Sk.builtin.str) {
base = Sk.builtin.asnum$(base);
val = Sk.str2number(x.v, base, parseInt, function (x) { return -x; }, "int");
if ((val > Sk.builtin.lng.threshold$) || (val < -Sk.builtin.lng.threshold$)) {
if ((val > Sk.builtin.nmber.threshold$) || (val < -Sk.builtin.nmber.threshold$)) {
// Too big for int, convert to long
return new Sk.builtin.lng(x, base);
}
Expand All @@ -130,17 +130,22 @@ Sk.builtin.int_ = function (x, base) {
throw new Sk.builtin.TypeError("int() can't convert non-string with explicit base");
}

if (x === undefined || x === Sk.builtin.none) {
x = 0;
}

if (x instanceof Sk.builtin.lng) {
if (x.cantBeInt()) {
return new Sk.builtin.lng(x);
}
return new Sk.builtin.nmber(x.toInt$(), Sk.builtin.nmber.int$);
}

// sneaky way to do truncate, floor doesn't work < 0, round doesn't work on the .5> side
// bitwise ops convert to 32bit int in the "C-truncate-way" we want.
x = Sk.builtin.asnum$(x);
return new Sk.builtin.nmber(x | 0, Sk.builtin.nmber.int$);
if (x > Sk.builtin.nmber.threshold$ || x < -Sk.builtin.nmber.threshold$) {
return new Sk.builtin.lng(x);
}
return new Sk.builtin.nmber(parseInt(x, base), Sk.builtin.nmber.int$);
};
Sk.builtin.int_.co_varnames = [ "base" ];
Sk.builtin.int_.co_numargs = 2;
Expand Down
6 changes: 3 additions & 3 deletions src/long.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ Sk.builtin.lng.prototype.tp$name = "long";
Sk.builtin.lng.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj('long', Sk.builtin.lng);

// Threshold to determine when types should be converted to long
Sk.builtin.lng.threshold$ = Math.pow(2, 53);
//Sk.builtin.lng.threshold$ = Sk.builtin.nmber.threshold$;

Sk.builtin.lng.MAX_INT$ = new Sk.builtin.lng(Sk.builtin.lng.threshold$);
Sk.builtin.lng.MIN_INT$ = new Sk.builtin.lng(-Sk.builtin.lng.threshold$);
Sk.builtin.lng.MAX_INT$ = new Sk.builtin.lng(Sk.builtin.nmber.threshold$);
Sk.builtin.lng.MIN_INT$ = new Sk.builtin.lng(-Sk.builtin.nmber.threshold$);

Sk.builtin.lng.prototype.cantBeInt = function () {
return (this.longCompare(Sk.builtin.lng.MAX_INT$) > 0) || (this.longCompare(Sk.builtin.lng.MIN_INT$) < 0);
Expand Down
4 changes: 2 additions & 2 deletions src/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Sk.builtin.nmber = function(x, skType) /* number is a reserved word */
if (skType !== undefined)
result.skType = skType;
if (skType === Sk.builtin.nmber.int$)
if (result.v > Sk.builtin.nmber.threshold$ || result.v < -Sk.builtin.nmber.threshold$)
if (result.v > Sk.builtin.nmber.threshold$ || result.v < -Sk.builtin.nmber.threshold$ - 1)
return new Sk.builtin.lng(x);
return result;
} else if (x instanceof Sk.builtin.lng) {
Expand Down Expand Up @@ -71,7 +71,7 @@ Sk.builtin.nmber.prototype.tp$name = "number";
Sk.builtin.nmber.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj('number', Sk.builtin.nmber);

// Threshold to determine when types should be converted to long
Sk.builtin.nmber.threshold$ = Math.pow(2, 53);
Sk.builtin.nmber.threshold$ = Math.pow(2, 53) - 1;
Sk.builtin.nmber.float$ = "float";
Sk.builtin.nmber.int$ = "int";

Expand Down
2 changes: 1 addition & 1 deletion src/tuple.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Sk.builtin.tuple.prototype.tp$hash = function()
}
x += 97531;
if (x === -1) x = -2;
return new Sk.builtin.nmber(x, Sk.builtin.nmber.int$);
return new Sk.builtin.nmber(x | 0, Sk.builtin.nmber.int$);
};

Sk.builtin.tuple.prototype.sq$repeat = function(n)
Expand Down
2 changes: 1 addition & 1 deletion test/run/t521.py.real
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-1215826407472719 <type 'int'>
1524613553 <type 'int'>
12 changes: 1 addition & 11 deletions test/run/t534.py.symtab
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Sym_type: module
Sym_name: top
Sym_lineno: 0
Sym_nested: False
Sym_haschildren: False
Sym_haschildren: True
-- Identifiers --
name: E
Expand Down Expand Up @@ -237,14 +238,3 @@ name: e
namespaces: [
]
name: object
is_referenced: True
is_imported: False
is_parameter: False
is_global: True
is_declared_global: False
is_local: False
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
21 changes: 21 additions & 0 deletions test/run/t534.trans
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,32 @@ Module(body=[ClassDef(name='U',
values=[Call(func=Name(id='U',
ctx=Load()),
args=[],
values=[Call(func=Name(id='int',
ctx=Load()),
args=[Num(n=3999999999.0)],
keywords=[],
starargs=None,
kwargs=None)],
nl=True),
Print(dest=None,
values=[Call(func=Name(id='type',
ctx=Load()),
args=[Call(func=Name(id='int',
ctx=Load()),
args=[Call(func=Name(id='pow',
ctx=Load()),
args=[Num(n=2),
Num(n=32)],
keywords=[],
starargs=None,
kwargs=None)],
keywords=[],
starargs=None,
kwargs=None)],
keywords=[],
starargs=None,
kwargs=None)],
nl=True)])
values=[UnaryOp(op=USub(),
operand=Call(func=Name(id='U',
ctx=Load()),
Expand Down
3 changes: 3 additions & 0 deletions test/run/t539.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
print type(int(3999999999.0))
print int(3999999999.0)
print type(int(pow(2,53) + 2))
3 changes: 3 additions & 0 deletions test/run/t539.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<type 'int'>
3999999999
<type 'long'>
3 changes: 3 additions & 0 deletions test/run/t539.py.real.force
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<type 'int'>
3999999999
<type 'int'>
31 changes: 31 additions & 0 deletions test/run/t539.py.symtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Sym_type: module
Sym_name: top
Sym_lineno: 0
Sym_nested: False
Sym_haschildren: False
-- Identifiers --
name: int
is_referenced: True
is_imported: False
is_parameter: False
is_global: True
is_declared_global: False
is_local: False
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
name: pow
is_referenced: True
is_imported: False
is_parameter: False
is_global: True
is_declared_global: False
is_local: False
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
name: type
Empty file added test/run/t539.symtab
Empty file.
Loading

0 comments on commit 6ac5a4c

Please sign in to comment.