Skip to content

Commit

Permalink
Merge branch 'master' into feature/elseif-statement
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Dec 18, 2020
2 parents 4f70404 + e9e95fc commit d8ae713
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
24 changes: 24 additions & 0 deletions src/lexer/Lexer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,12 @@ describe('lexer', () => {
expect(f.text).to.eql('2.5e3');
});

it.only('supports larger-than-supported-precision floats to be defined with exponents', () => {
let f = Lexer.scan('2.3659475627512424e-38').tokens[0];
expect(f.kind).to.equal(TokenKind.FloatLiteral);
expect(f.text).to.eql('2.3659475627512424e-38');
});

it('allows digits before `.` to be elided', () => {
let f = Lexer.scan('.123').tokens[0];
expect(f.kind).to.equal(TokenKind.FloatLiteral);
Expand All @@ -795,6 +801,12 @@ describe('lexer', () => {
});

describe('long integer literals', () => {
it('respects \'&\' suffix', () => {
let f = Lexer.scan('1&').tokens[0];
expect(f.kind).to.equal(TokenKind.LongIntegerLiteral);
expect(f.text).to.eql('1&');
});

it('supports hexadecimal literals', () => {
let i = Lexer.scan('&hf00d&').tokens[0];
expect(i.kind).to.equal(TokenKind.LongIntegerLiteral);
Expand All @@ -815,6 +827,18 @@ describe('lexer', () => {
});

describe('integer literals', () => {
it('respects \'%\' suffix', () => {
let f = Lexer.scan('1%').tokens[0];
expect(f.kind).to.equal(TokenKind.IntegerLiteral);
expect(f.text).to.eql('1%');
});

it.only('does not allow decimal numbers to end with %', () => {
let f = Lexer.scan('1.2%').tokens[0];
expect(f.kind).to.equal(TokenKind.FloatLiteral);
expect(f.text).to.eql('1.2');
});

it('supports hexadecimal literals', () => {
let i = Lexer.scan('&hFf').tokens[0];
expect(i.kind).to.equal(TokenKind.IntegerLiteral);
Expand Down
25 changes: 7 additions & 18 deletions src/lexer/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,16 +626,13 @@ export class Lexer {
let numberOfDigits = containsDecimal ? asString.length - 1 : asString.length;
let designator = this.peek().toLowerCase();

if (numberOfDigits >= 10 && designator !== '&') {
if (numberOfDigits >= 10 && designator !== '&' && designator !== 'e') {
// numeric literals over 10 digits with no type designator are implicitly Doubles
this.addToken(TokenKind.DoubleLiteral);
return;
} else if (designator === '#') {
// numeric literals ending with "#" are forced to Doubles
this.advance();
asString = this.source.slice(this.start, this.current);
this.addToken(TokenKind.DoubleLiteral);
return;
} else if (designator === 'd') {
// literals that use "D" as the exponent are also automatic Doubles

Expand All @@ -655,15 +652,10 @@ export class Lexer {
// replace the exponential marker with a JavaScript-friendly "e"
asString = this.source.slice(this.start, this.current).replace(/[dD]/, 'e');
this.addToken(TokenKind.DoubleLiteral);
return;
}

if (designator === '!') {
} else if (designator === '!') {
// numeric literals ending with "!" are forced to Floats
this.advance();
asString = this.source.slice(this.start, this.current);
this.addToken(TokenKind.FloatLiteral);
return;
} else if (designator === 'e') {
// literals that use "E" as the exponent are also automatic Floats

Expand All @@ -680,21 +672,18 @@ export class Lexer {
this.advance();
}

asString = this.source.slice(this.start, this.current);
this.addToken(TokenKind.FloatLiteral);
return;
} else if (containsDecimal) {
// anything with a decimal but without matching Double rules is a Float
this.addToken(TokenKind.FloatLiteral);
return;
}

if (designator === '&') {
} else if (designator === '&') {
// numeric literals ending with "&" are forced to LongIntegers
asString = this.source.slice(this.start, this.current);
this.advance();
this.addToken(TokenKind.LongIntegerLiteral);

} else if (designator === '%') {
//numeric literals ending with "%" are forced to Integer
this.advance();
this.addToken(TokenKind.IntegerLiteral);
} else {
// otherwise, it's a regular integer
this.addToken(TokenKind.IntegerLiteral);
Expand Down

0 comments on commit d8ae713

Please sign in to comment.