Skip to content

Commit 002fba3

Browse files
committed
Initial parseInt using loads, see #8
1 parent 8968108 commit 002fba3

File tree

10 files changed

+1108
-6
lines changed

10 files changed

+1108
-6
lines changed

src/compiler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,6 +2836,7 @@ export class Compiler extends DiagnosticEmitter {
28362836
compileLiteralExpression(expression: LiteralExpression, contextualType: Type): ExpressionRef {
28372837
switch (expression.literalKind) {
28382838
// case LiteralKind.ARRAY:
2839+
// return this.compileStaticArray(...);
28392840

28402841
case LiteralKind.FLOAT: {
28412842
var floatValue = (<FloatLiteralExpression>expression).value;
@@ -2903,6 +2904,10 @@ export class Compiler extends DiagnosticEmitter {
29032904
: this.module.createI32(stringOffset.lo);
29042905
}
29052906

2907+
compileStaticArray(elementType: Type, expressions: Expression): ExpressionRef {
2908+
throw new Error("not implemented");
2909+
}
2910+
29062911
compileNewExpression(expression: NewExpression, contextualType: Type): ExpressionRef {
29072912
var resolved = this.program.resolveExpression(expression.expression, this.currentFunction); // reports
29082913
if (resolved) {

std/assembly/string.ts

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,100 @@ function isWhiteSpaceOrLineTerminator(c: u16): bool {
322322
}
323323
}
324324

325-
// @binding(CALL, [ STRING, PASS_THRU ], PASS_THRU)
326-
export function parseInt(str: string, radix: i32 = 10): f64 {
327-
throw new Error("not implemented");
325+
const enum CharCode {
326+
PLUS = 0x2B,
327+
MINUS = 0x2D,
328+
_0 = 0x30,
329+
_1 = 0x31,
330+
_2 = 0x32,
331+
_3 = 0x33,
332+
_4 = 0x34,
333+
_5 = 0x35,
334+
_6 = 0x36,
335+
_7 = 0x37,
336+
_8 = 0x38,
337+
_9 = 0x39,
338+
A = 0x41,
339+
B = 0x42,
340+
O = 0x4F,
341+
X = 0x58,
342+
Z = 0x5a,
343+
a = 0x61,
344+
b = 0x62,
345+
o = 0x6F,
346+
x = 0x78,
347+
z = 0x7A
348+
}
349+
350+
export function parseInt(str: String, radix: i32 = 0): i64 {
351+
var len = str.length;
352+
var ptr = changetype<usize>(str) + HEAD;
353+
if (!len)
354+
return 0; // (NaN)
355+
var code = <i32>load<u16>(ptr);
356+
357+
// determine sign
358+
var sign: i64;
359+
if (code == CharCode.MINUS) {
360+
if (!--len)
361+
return 0; // (NaN)
362+
code = <i32>load<u16>(ptr += 2);
363+
sign = -1;
364+
} else if (code == CharCode.PLUS) {
365+
if (!--len)
366+
return 0; // (NaN)
367+
code = <i32>load<u16>(ptr += 2);
368+
sign = 1;
369+
} else
370+
sign = 1;
371+
372+
// determine radix
373+
if (!radix) {
374+
if (code == CharCode._0 && len > 2) {
375+
switch (<i32>load<u16>(ptr + 2)) {
376+
377+
case CharCode.B:
378+
case CharCode.b:
379+
ptr += 4; len -= 2;
380+
radix = 2;
381+
break;
382+
383+
case CharCode.O:
384+
case CharCode.o:
385+
ptr += 4; len -= 2;
386+
radix = 8;
387+
break;
388+
389+
case CharCode.X:
390+
case CharCode.x:
391+
ptr += 4; len -= 2;
392+
radix = 16;
393+
break;
394+
395+
default:
396+
radix = 10;
397+
}
398+
} else radix = 10;
399+
}
400+
401+
// calculate value
402+
var num: i64 = 0;
403+
while (len--) {
404+
code = <i32>load<u16>(ptr);
405+
if (code >= CharCode._0 && code <= CharCode._9)
406+
code -= CharCode._0;
407+
else if (code >= CharCode.A && code <= CharCode.Z)
408+
code -= CharCode.A - 10;
409+
else if (code >= CharCode.a && code <= CharCode.z)
410+
code -= CharCode.a - 10;
411+
else
412+
return sign * num;
413+
if (code >= radix)
414+
return sign * num;
415+
num = (num * radix) + code;
416+
ptr += 2;
417+
}
418+
return sign * num;
328419
}
329420

330421
// @binding(CALL, [ STRING ], PASS_THRU)

tests/compiler/std/array.wast

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4073,6 +4073,7 @@
40734073
FUNCTION_PROTOTYPE: std:string/String.__eq
40744074
CLASS_PROTOTYPE: String
40754075
FUNCTION_PROTOTYPE: std:string/isWhiteSpaceOrLineTerminator
4076+
ENUM: std:string/CharCode
40764077
FUNCTION_PROTOTYPE: std:string/parseInt
40774078
FUNCTION_PROTOTYPE: parseInt
40784079
FUNCTION_PROTOTYPE: std:string/parseFloat

tests/compiler/std/carray.wast

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@
280280
FUNCTION_PROTOTYPE: std:string/String.__eq
281281
CLASS_PROTOTYPE: String
282282
FUNCTION_PROTOTYPE: std:string/isWhiteSpaceOrLineTerminator
283+
ENUM: std:string/CharCode
283284
FUNCTION_PROTOTYPE: std:string/parseInt
284285
FUNCTION_PROTOTYPE: parseInt
285286
FUNCTION_PROTOTYPE: std:string/parseFloat

tests/compiler/std/heap.wast

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,7 @@
28892889
FUNCTION_PROTOTYPE: std:string/String.__eq
28902890
CLASS_PROTOTYPE: String
28912891
FUNCTION_PROTOTYPE: std:string/isWhiteSpaceOrLineTerminator
2892+
ENUM: std:string/CharCode
28922893
FUNCTION_PROTOTYPE: std:string/parseInt
28932894
FUNCTION_PROTOTYPE: parseInt
28942895
FUNCTION_PROTOTYPE: std:string/parseFloat

tests/compiler/std/new.wast

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@
230230
FUNCTION_PROTOTYPE: std:string/String.__eq
231231
CLASS_PROTOTYPE: String
232232
FUNCTION_PROTOTYPE: std:string/isWhiteSpaceOrLineTerminator
233+
ENUM: std:string/CharCode
233234
FUNCTION_PROTOTYPE: std:string/parseInt
234235
FUNCTION_PROTOTYPE: parseInt
235236
FUNCTION_PROTOTYPE: std:string/parseFloat

tests/compiler/std/set.wast

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2798,6 +2798,7 @@
27982798
FUNCTION_PROTOTYPE: std:string/String.__eq
27992799
CLASS_PROTOTYPE: String
28002800
FUNCTION_PROTOTYPE: std:string/isWhiteSpaceOrLineTerminator
2801+
ENUM: std:string/CharCode
28012802
FUNCTION_PROTOTYPE: std:string/parseInt
28022803
FUNCTION_PROTOTYPE: parseInt
28032804
FUNCTION_PROTOTYPE: std:string/parseFloat

0 commit comments

Comments
 (0)