Skip to content

Commit da1949a

Browse files
committed
Pass test 95.
Implement nqp::isclass, nqp::findcclass, nqp::findnotcclass.
1 parent db1a1c1 commit da1949a

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

TODO

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ We need to write tests (if they don't already exist in t/nqp)
66

77
- curlexpad
88
- die_s
9-
- findnotcclass - EASY
109
- getcodename
1110
- getlexdyn
12-
- iscclass - EASY
1311
- nfafromstatelist
1412
- nfarunalt
1513
- nfarunproto - MEDIUM (Look at t/nqp/74-nfa.t)

src/vm/js/QAST/Compiler.nqp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,11 @@ class QAST::OperationsJS {
967967
}
968968
});
969969

970+
971+
add_simple_op('findcclass', $T_INT, [$T_INT, $T_STR, $T_INT, $T_INT]);
972+
add_simple_op('findnotcclass', $T_INT, [$T_INT, $T_STR, $T_INT, $T_INT]);
973+
add_simple_op('iscclass', $T_INT, [$T_INT, $T_STR, $T_INT]);
974+
970975
# TODO consider/handle if lexotic is not the topmost thing in a block
971976
# TODO implement returning from nested block
972977
add_op('lexotic', sub ($comp, $node, :$want) {

src/vm/js/bin/run_tests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
22
# 19 and 30 where moved out as they were parrot specific, 52,54 is missing, we can't pass 49 till we are bootstraped
3-
prove "$@" -e './nqp-js' t/nqp/{01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,20,21,22,23,25,26,27,28,33,35,36,37,38,39,40,41,42,46,47,48,51,53,55,56,57,58,59,63,64,65,68,69,70,71,75,76,79,81,83,88,89,90,91,92,93,94}* t/js/getcomp-js.t t/qast/02*
3+
prove "$@" -e './nqp-js' t/nqp/{01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,20,21,22,23,25,26,27,28,33,35,36,37,38,39,40,41,42,46,47,48,51,53,55,56,57,58,59,63,64,65,68,69,70,71,75,76,79,81,83,88,89,90,91,92,93,94,95}* t/js/getcomp-js.t t/qast/02*

src/vm/js/nqp-runtime/core.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,72 @@ var sha1 = require('sha1');
360360
op.sha1 = function(text) {
361361
return sha1(text).toUpperCase();
362362
};
363+
364+
function boolish(bool) {
365+
return bool ? 1 : 0;
366+
}
367+
368+
function iscclass(cclass, target, offset) {
369+
if (offset < 0 || offset >= target.length) return 0;
370+
switch (cclass) {
371+
//ANY
372+
case 65535: return 1;
373+
//UPPERCASE
374+
case 1: return boolish(!/^\d|_/.test(target[offset]) && /\w/.test(target[offset]) && target[offset] == target[offset].toUpperCase());
375+
//LOWERCASE
376+
case 2: return boolish(!/^\d|_/.test(target[offset]) && /\w/.test(target[offset]) && target[offset] == target[offset].toLowerCase());
377+
//ALPHABETIC
378+
case 4: return boolish(!/^\d|_/.test(target[offset]) && /\w/.test(target[offset]));
379+
//NUMERIC
380+
case 8: return boolish(/^\d/.test(target[offset]));
381+
//HEXADECIMAL
382+
case 16: return boolish(/^[0-9a-fA-F]/.test(target[offset]));
383+
//WHITESPACE
384+
case 32: return boolish('\n\u000b\f\r\u0085\u2028\u2029\t \u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'.indexOf(target[offset]) != -1);
385+
//BLANK
386+
case 256: return boolish(target[offset] == ' ' || target[offset] == '\t'); //HACK - not all such chars
387+
//CONTROL
388+
case 512: return boolish('\n\t\r'.indexOf(target[offset]) != -1); //HACK - not all such chars
389+
//PUNCTUATION
390+
case 1024: return boolish(/[.,;?!]/.test(target[offset])); //HACK
391+
//ALPHANUMERIC
392+
case 2048: return boolish(/^\w/.test(target[offset]) && target[offset] != '_');
393+
//NEWLINE
394+
case 4096: return boolish(target[offset] == '\n' || target[offset] == '\r' || target[offset] == '\u0085'); //HACK
395+
//WORD
396+
case 8192: return boolish(/^\w/.test(target[offset]));
397+
default: throw 'cclass ' + cclass + ' not yet implemented';
398+
}
399+
}
400+
401+
op.iscclass = function(cclass, target, offset) {
402+
return iscclass(cclass, target, offset);
403+
};
404+
405+
op.findcclass = function(cclass, target, offset, count) {
406+
var end = offset + count;
407+
end = target.length < end ? target.length : end;
408+
409+
for (var pos = offset; pos < end; pos++) {
410+
if (iscclass(cclass, target, pos) > 0) {
411+
return pos;
412+
}
413+
}
414+
415+
return end;
416+
};
417+
418+
op.findnotcclass = function(cclass, target, offset, count) {
419+
var end = offset + count;
420+
end = target.length < end ? target.length : end;
421+
422+
for (var pos = offset; pos < end; pos++) {
423+
if (iscclass(cclass, target, pos) == 0) {
424+
return pos;
425+
}
426+
}
427+
428+
return end;
429+
};
430+
431+

0 commit comments

Comments
 (0)