Skip to content

Commit ffaa4c0

Browse files
committed
➖ ᴛᴏᴋᴇɴsᴛʀᴇᴀᴍ.ᴊs — Simplify function (peekTypeEquals)
1 parent 2462ea5 commit ffaa4c0

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

playground/static/js/ast/parsers/function.parser.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class ASTFunctionParser {
2828

2929
this.stream.expect('fn');
3030

31-
if (!this.stream.peekTypeEquals("ID"))
31+
if (qrtTypeOf(this.stream.peek()) !== "ID")
3232
return this.stream.error(Errors.AST.Fn_MissingIdentifier);
3333

3434
func.name = this.stream.pop();
@@ -42,11 +42,12 @@ export class ASTFunctionParser {
4242
return this.stream.error(Errors.AST.Fn_MalformedArgs);
4343

4444
while (!this.stream.next(")")) {
45-
if (!this.stream.peekTypeEquals(["TYPE", "ID"]))
45+
if (qrtTypeOf(this.stream.peek()) !== 'TYPE'
46+
&& qrtTypeOf(this.stream.peek()) !== 'ID')
4647
return this.stream.error(Errors.TYPECHECK.Fn_InvalidArgType);
4748

4849
const name = this.stream.pop();
49-
if (!this.stream.peekTypeEquals("ID"))
50+
if (qrtTypeOf(this.stream.peek()) !== 'ID')
5051
return this.stream.error(Errors.AST.Fn_InvalidArgName);
5152

5253
const value = this.stream.pop();
@@ -58,7 +59,9 @@ export class ASTFunctionParser {
5859
}
5960

6061
parseReturnType(func) {
61-
if (this.stream.peekTypeEquals(["ID", "TYPE"])) { // TODO: use RTTypeOf
62+
if (qrtTypeOf(this.stream.peek()) === 'TYPE'
63+
|| qrtTypeOf(this.stream.peek()) === 'ID')
64+
{
6265
func.returnType = this.stream.pop();
6366
return true;
6467
}

playground/static/js/ast/parsers/variable.parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class ASTVariableParser {
1616
if (!this.stream.expect('let'))
1717
return this.stream.error(Errors.AST.Let_MalformedDeclaration);
1818

19-
if (!this.stream.peekTypeEquals('ID'))
19+
if (qrtTypeOf(this.stream.peek()) !== 'ID')
2020
return this.stream.error(Errors.AST.Let_MissingIdentifier);
2121

2222
variable.name = this.stream.pop();
@@ -25,7 +25,7 @@ export class ASTVariableParser {
2525

2626
// Type declaration
2727
if (this.stream.next(':')) {
28-
if (!this.stream.peekTypeEquals("TYPE"))
28+
if (qrtTypeOf(this.stream.peek()) !== 'TYPE')
2929
return this.stream.error(Errors.TYPECHECK.Let_InvalidType);
3030

3131
variable.varType = this.stream.pop();

playground/static/js/compiler/util/tokenstream.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ export class TokenStream {
1010
this.index = 0;
1111
}
1212

13-
peekTypeEquals(type) {
14-
if (!Array.isArray(type))
15-
return qrtTypeOf(this.peek()) === type;
16-
17-
return type.some(t => this.peekTypeEquals(t));
18-
}
19-
2013
pop() {
2114
return this.tokens[this.index++];
2215
}

playground/static/js/tokens/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ export function qrtTypeOf(s) {
5858
return t;
5959
}
6060

61+
window.qrtTypeOf = qrtTypeOf;
62+

0 commit comments

Comments
 (0)