Skip to content

Adds NaN and Infinity literals #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
"cheese",
"food"
],
"version": "0.2.2",
"version": "0.2.3",
"dependencies": {},
"bin": {
"cheddar-parser": "dist/ches.js"
2 changes: 2 additions & 0 deletions src/ches.es6
Original file line number Diff line number Diff line change
@@ -93,6 +93,8 @@ if (!module.parent) {

Boolean: require('./literals/boolean'),
Nil: require('./literals/nil'),
NaN: require('./literals/nan'),
Infinity: require('./literals/infinity'),

// Things to do with variables
Property: require('./parsers/property'),
24 changes: 24 additions & 0 deletions src/literals/infinity.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import CheddarPrimitive from './primitive';
import * as CheddarError from '../consts/err';

import {ClassType} from '../consts/types';

export default class CheddarInfinityToken extends CheddarPrimitive {
exec() {
if (this.curchar === 'I' &&
this.Code[this.Index + 1] === 'n' &&
this.Code[this.Index + 1] === 'f' &&
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @ETHproductions said, i don't think these should all be the same. You can also use this.jumpLiteral("Infinity") which returns false if not a match, truthy otherwise

this.Code[this.Index + 1] === 'i' &&
this.Code[this.Index + 1] === 'n' &&
this.Code[this.Index + 1] === 'i' &&
this.Code[this.Index + 1] === 't' &&
this.Code[this.Index + 2] === 'y') {
this.Index += 8;
return this.close();
}

return this.close(CheddarError.EXIT_NOTFOUND);
}

get Type() { return ClassType.Number }
}
19 changes: 19 additions & 0 deletions src/literals/nan.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import CheddarPrimitive from './primitive';
import * as CheddarError from '../consts/err';

import {ClassType} from '../consts/types';

export default class CheddarNilToken extends CheddarPrimitive {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be CheddarNaNToken instead of CheddarNilToken

exec() {
if (this.curchar === 'N' &&
this.Code[this.Index + 1] === 'a' &&
this.Code[this.Index + 2] === 'N') {
this.Index += 3;
return this.close();
}

return this.close(CheddarError.EXIT_NOTFOUND);
}

get Type() { return ClassType.Number }
}
28 changes: 20 additions & 8 deletions src/tok/lex.es6
Original file line number Diff line number Diff line change
@@ -8,9 +8,14 @@ export default class CheddarLexer {
this._Tokens = [];
}

toAST() {
toAST(padding = '') {
let result = padding + this.constructor.name.replace(/^Cheddar/g, '') + '\n';

padding = padding.replace(/├$/, '|').replace(/└$/, ' ');

let node = this,
tokens = this._Tokens;
tokens = this._Tokens,
newPadding = padding + '├';

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

return node.constructor.name.replace(/^Cheddar/g, '') + '\n' + tokens.map(t => typeof t === 'string' ? "'" + t + "'" : t)
.map(t => t.toAST ? t.toAST() : t.toString())
.join(tokens.every(o => !(o instanceof CheddarLexer)) ? ' ' : '\n')
.replace(/^/gm, ' │')
.replace(/^ │(?! [└├│┬])/gm, ' ├ ');
//.replace(/^((?: [^└├│┬])*)├/gm, '└'); //wait only the last one
let separator = tokens.some(token => token instanceof CheddarLexer) ? '\n' : ' ';
if (separator === ' ')
result += padding + '└';

for (let item of tokens.slice(0, -1))
result += (item.toAST ? item.toAST(newPadding) : typeof item === 'string' ? "'" + item + "'" : item) + separator;

if (tokens.length) {
let item = tokens[tokens.length - 1];
result += item.toAST ? item.toAST(padding + '└') : typeof item === 'string' ? "'" + item + "'" : item;
}

return result;
}

getChar() {