From 8af50f3d60e82338bcae4d2dbec9e568f3cec4f9 Mon Sep 17 00:00:00 2001 From: peze Date: Thu, 16 Nov 2023 17:20:19 +0800 Subject: [PATCH] fix the new keyword error --- lib/semantic.js | 9 +++++++-- test/semantic.test.js | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/semantic.js b/lib/semantic.js index 94b6f0d..67dd689 100644 --- a/lib/semantic.js +++ b/lib/semantic.js @@ -1155,6 +1155,11 @@ class TypeChecker { keyType: _basic('string'), valueType: this.getParameterType(type.valueType, moduleName) }; + } else if (type.type === 'iterator' || type.type === 'asyncIterator') { + return { + type: type.type, + valueType: this.getParameterType(type.valueType, moduleName) + }; } else if (type.tag === Tag.TYPE) { return _basic(type.lexeme); } else if (type.tag === Tag.ID && type.lexeme.startsWith('$')) { @@ -1687,8 +1692,8 @@ class TypeChecker { const leftType = this.getExprType(ast.left, env); this.visitExpr(ast.right, env); const rightType = this.getExprType(ast.right, env); - if(ast.type !== 'eq' && !(leftType.type === 'basic' && isNumber(leftType.name)) || - !(rightType.type === 'basic' && isNumber(rightType.name))) { + if(ast.type !== 'eq' && (!(leftType.type === 'basic' && isNumber(leftType.name)) || + !(rightType.type === 'basic' && isNumber(rightType.name)))) { this.error(`${ast.type} can only operate number type, ` + `but left: ${display(leftType)} and right: ${display(rightType)}`, ast); } diff --git a/test/semantic.test.js b/test/semantic.test.js index b9a5e34..75602b2 100644 --- a/test/semantic.test.js +++ b/test/semantic.test.js @@ -1247,6 +1247,18 @@ describe('semantic', function () { }); it('iterator should ok', function () { + expect(function () { + parse(` + static function getIterator(it: iterator[string]): iterator[string] { + return it; + } + + static function useIterator(it: iterator[string]): void{ + getIterator(it); + } +`, '__filename'); + }).to.not.throwException(); + expect(function () { parse(` function getIterator(it: iterator[string]): void { @@ -2389,6 +2401,17 @@ describe('semantic', function () { return 'false'; }`, '__filename'); }).to.not.throwException(); + + expect(function () { + parse(`static function callOSS(): string { + var a = 'string'; + var b = 'string'; + if(a == b) { + return 'true'; + } + return 'false'; + }`, '__filename'); + }).to.not.throwException(); expect(function () { parse(`static function callOSS(): boolean {