Skip to content
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

Support integer type declaration char #254

Merged
merged 2 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions src/lexer/Lexer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,12 @@ describe('lexer', () => {
});

describe('long integer literals', () => {
it('respects \'&\' suffix', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Not directly impacted by this code, but the test for this was missing so I added it.

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 @@ -812,6 +818,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
23 changes: 6 additions & 17 deletions src/lexer/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,13 +629,10 @@ export class Lexer {
if (numberOfDigits >= 10 && designator !== '&') {
// 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 === '!') {
Copy link
Member Author

Choose a reason for hiding this comment

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

I combined several separate if statements into one continuous if...else if...else if... block. This cleaned up the code by removing several return statements and making it easier to follow the flow.

// 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 === '%') {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the core of the change. we just needed a separate if block for handling %

//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