Skip to content

Commit 5603857

Browse files
committed
Add Infinity/NaN literals, fix toAST
1 parent 501f6dc commit 5603857

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

src/ches.es6

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ if (!module.parent) {
9393

9494
Boolean: require('./literals/boolean'),
9595
Nil: require('./literals/nil'),
96+
NaN: require('./literals/nan'),
97+
Infinity: require('./literals/infinity'),
9698

9799
// Things to do with variables
98100
Property: require('./parsers/property'),

src/literals/infinity.es6

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import CheddarPrimitive from './primitive';
2+
import * as CheddarError from '../consts/err';
3+
4+
import {ClassType} from '../consts/types';
5+
6+
export default class CheddarInfinityToken extends CheddarPrimitive {
7+
exec() {
8+
if (this.curchar === 'I' &&
9+
this.Code[this.Index + 1] === 'n' &&
10+
this.Code[this.Index + 1] === 'f' &&
11+
this.Code[this.Index + 1] === 'i' &&
12+
this.Code[this.Index + 1] === 'n' &&
13+
this.Code[this.Index + 1] === 'i' &&
14+
this.Code[this.Index + 1] === 't' &&
15+
this.Code[this.Index + 2] === 'y') {
16+
this.Index += 8;
17+
return this.close();
18+
}
19+
20+
return this.close(CheddarError.EXIT_NOTFOUND);
21+
}
22+
23+
get Type() { return ClassType.Number }
24+
}

src/literals/nan.es6

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import CheddarPrimitive from './primitive';
2+
import * as CheddarError from '../consts/err';
3+
4+
import {ClassType} from '../consts/types';
5+
6+
export default class CheddarNilToken extends CheddarPrimitive {
7+
exec() {
8+
if (this.curchar === 'N' &&
9+
this.Code[this.Index + 1] === 'a' &&
10+
this.Code[this.Index + 2] === 'N') {
11+
this.Index += 3;
12+
return this.close();
13+
}
14+
15+
return this.close(CheddarError.EXIT_NOTFOUND);
16+
}
17+
18+
get Type() { return ClassType.Number }
19+
}

src/tok/lex.es6

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ export default class CheddarLexer {
88
this._Tokens = [];
99
}
1010

11-
toAST() {
11+
toAST(padding = '') {
12+
let result = padding + this.constructor.name.replace(/^Cheddar/g, '') + '\n';
13+
14+
padding = padding.replace(/$/, '|').replace(/$/, ' ');
15+
1216
let node = this,
13-
tokens = this._Tokens;
17+
tokens = this._Tokens,
18+
newPadding = padding + '├';
1419

1520
while (tokens.length === 1 && tokens[0].isExpression) {
1621
node = tokens[0];
@@ -22,12 +27,19 @@ export default class CheddarLexer {
2227
tokens = tokens[0]._Tokens;
2328
}
2429

25-
return node.constructor.name.replace(/^Cheddar/g, '') + '\n' + tokens.map(t => typeof t === 'string' ? "'" + t + "'" : t)
26-
.map(t => t.toAST ? t.toAST() : t.toString())
27-
.join(tokens.every(o => !(o instanceof CheddarLexer)) ? ' ' : '\n')
28-
.replace(/^/gm, ' │')
29-
.replace(/^ (?! [])/gm, ' ├ ');
30-
//.replace(/^((?: [^└├│┬])*)├/gm, '└'); //wait only the last one
30+
let separator = tokens.some(token => token instanceof CheddarLexer) ? '\n' : ' ';
31+
if (separator === ' ')
32+
result += padding + '└';
33+
34+
for (let item of tokens.slice(0, -1))
35+
result += (item.toAST ? item.toAST(newPadding) : typeof item === 'string' ? "'" + item + "'" : item) + separator;
36+
37+
if (tokens.length) {
38+
let item = tokens[tokens.length - 1];
39+
result += item.toAST ? item.toAST(padding + '└') : typeof item === 'string' ? "'" + item + "'" : item;
40+
}
41+
42+
return result;
3143
}
3244

3345
getChar() {

0 commit comments

Comments
 (0)