forked from skulpt/skulpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.js
163 lines (144 loc) · 4.89 KB
/
int.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* jslint nomen: true, bitwise: true */
/* global Sk: true */
// Takes a JavaScript string and returns a number using the
// parser and negater functions (for int/long right now)
//
// parser should take a string that is a postive number which only
// contains characters that are valid in the given base and a base and
// return a number
//
// negater should take a number and return its negation
//
// fname is a string containing the function name to be used in error
// messages
Sk.str2number = function (s, base, parser, negater, fname) {
"use strict";
var origs = s,
neg = false,
i,
ch,
val;
// strip whitespace from ends
// s = s.trim();
s = s.replace(/^\s+|\s+$/g, "");
// check for minus sign
if (s.charAt(0) === "-") {
neg = true;
s = s.substring(1);
}
// check for plus sign
if (s.charAt(0) === "+") {
s = s.substring(1);
}
if (base === undefined) {
base = 10;
} // default radix is 10, not dwim
if (base < 2 || base > 36) {
if (base !== 0) {
throw new Sk.builtin.ValueError(fname + "() base must be >= 2 and <= 36");
}
}
if (s.substring(0, 2).toLowerCase() === "0x") {
if (base === 16 || base === 0) {
s = s.substring(2);
base = 16;
} else if (base < 34) {
throw new Sk.builtin.ValueError("invalid literal for " + fname + "() with base " + base + ": '" + origs + "'");
}
} else if (s.substring(0, 2).toLowerCase() === "0b") {
if (base === 2 || base === 0) {
s = s.substring(2);
base = 2;
} else if (base < 12) {
throw new Sk.builtin.ValueError("invalid literal for " + fname + "() with base " + base + ": '" + origs + "'");
}
} else if (s.substring(0, 2).toLowerCase() === "0o") {
if (base === 8 || base === 0) {
s = s.substring(2);
base = 8;
} else if (base < 25) {
throw new Sk.builtin.ValueError("invalid literal for " + fname + "() with base " + base + ": '" + origs + "'");
}
} else if (s.charAt(0) === "0") {
if (s === "0") {
return 0;
}
if (base === 8 || base === 0) {
base = 8;
}
}
if (base === 0) {
base = 10;
}
if (s.length === 0) {
throw new Sk.builtin.ValueError("invalid literal for " + fname + "() with base " + base + ": '" + origs + "'");
}
// check all characters are valid
for (i = 0; i < s.length; i = i + 1) {
ch = s.charCodeAt(i);
val = base;
if ((ch >= 48) && (ch <= 57)) {
// 0-9
val = ch - 48;
} else if ((ch >= 65) && (ch <= 90)) {
// A-Z
val = ch - 65 + 10;
} else if ((ch >= 97) && (ch <= 122)) {
// a-z
val = ch - 97 + 10;
}
if (val >= base) {
throw new Sk.builtin.ValueError("invalid literal for " + fname + "() with base " + base + ": '" + origs + "'");
}
}
// parse number
val = parser(s, base);
if (neg) {
val = negater(val);
}
return val;
};
Sk.builtin.int_ = function (x, base) {
"use strict";
var val;
if ((x !== undefined) && (!Sk.builtin.checkString(x) && !Sk.builtin.checkNumber(x))) {
if (x instanceof Sk.builtin.bool) {
x = Sk.builtin.asnum$(x);
} else {
throw new Sk.builtin.TypeError("int() argument must be a string or a number, not '" + Sk.abstr.typeName(x) + "'");
}
}
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.nmber.threshold$) || (val < -Sk.builtin.nmber.threshold$)) {
// Too big for int, convert to long
return new Sk.builtin.lng(x, base);
}
return new Sk.builtin.nmber(val, Sk.builtin.nmber.int$);
}
if (base !== undefined) {
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$);
}
x = Sk.builtin.asnum$(x);
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;
Sk.builtin.int_.$defaults = [ new Sk.builtin.nmber(10, Sk.builtin.nmber.int$) ];
Sk.builtin.int_.prototype.tp$name = "int";
Sk.builtin.int_.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj("int", Sk.builtin.int_);