diff --git a/ts2panda/src/base/lreference.ts b/ts2panda/src/base/lreference.ts index b079b70bb89e2d0538d4bdff16aa07780845c735..1a89aa466263e0fa75fcca1b0c4e3ecb6527a059 100644 --- a/ts2panda/src/base/lreference.ts +++ b/ts2panda/src/base/lreference.ts @@ -20,6 +20,7 @@ import { compileDestructuring } from "../compilerUtils"; import { DiagnosticCode, DiagnosticError } from "../diagnostic"; import { getObjAndProp } from "../expression/memberAccessExpression"; import { findInnerExprOfParenthesis } from "../expression/parenthesizedExpression"; +import { findInnerExprOfPartiallyEmittedExpr } from "../expression/partiallyEmittedExpression"; import { VReg } from "../irnodes"; import * as jshelpers from "../jshelpers"; import { Scope } from "../scope"; @@ -131,8 +132,12 @@ export class LReference { let realNode: ts.Node = node; - if (ts.isParenthesizedExpression(node)) { - realNode = findInnerExprOfParenthesis(node); + if (ts.isPartiallyEmittedExpression(node)) { + realNode = findInnerExprOfPartiallyEmittedExpr(node); + } + + if (ts.isParenthesizedExpression(realNode)) { + realNode = findInnerExprOfParenthesis(realNode); } if (ts.isIdentifier(realNode)) { diff --git a/ts2panda/src/expression/partiallyEmittedExpression.ts b/ts2panda/src/expression/partiallyEmittedExpression.ts new file mode 100644 index 0000000000000000000000000000000000000000..d1521a7f51284113e7b57ea706c27842cb314feb --- /dev/null +++ b/ts2panda/src/expression/partiallyEmittedExpression.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as ts from "typescript"; + +export function findInnerExprOfPartiallyEmittedExpr(expr: ts.PartiallyEmittedExpression): ts.Expression { + while (expr.expression.kind == ts.SyntaxKind.PartiallyEmittedExpression) { + expr = expr.expression; + } + return expr.expression; +} diff --git a/ts2panda/tests/expression/partiallyemitted.test.ts b/ts2panda/tests/expression/partiallyemitted.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..770b44a073518b36008ae4c35f41da080f4d933c --- /dev/null +++ b/ts2panda/tests/expression/partiallyemitted.test.ts @@ -0,0 +1,63 @@ +import { + expect +} from 'chai'; +import 'mocha'; +import { + Returnundefined, + Sttoglobalrecord, + Tryldglobalbyname, + Lda, + VReg, + Imm, + IRNode, + Sta, + Mov, + Stobjbyname, + Dec +} from "../../src/irnodes"; +import { checkInstructions, SnippetCompiler } from "../utils/base"; +import { creatAstFromSnippet } from "../utils/asthelper" +import { PandaGen } from '../../src/pandagen'; + +describe("PartiallyEmittedExpressionTest", function () { + it("createdPartiallyEmittedExprTest", function () { + let snippetCompiler = new SnippetCompiler(); + snippetCompiler.compileAfter(`let a; let b; (a.name as string) = b`, 'test.ts'); + IRNode.pg = new PandaGen("", creatAstFromSnippet("let a; let b; (a.name as string) = b"), 0, undefined); + let insns = snippetCompiler.getGlobalInsns(); + let expected = [ + new Lda(new VReg()), + new Sttoglobalrecord(new Imm(0), 'a'), + new Lda(new VReg()), + new Sttoglobalrecord(new Imm(0), 'b'), + new Tryldglobalbyname(new Imm(1), 'a'), + new Sta(new VReg()), + new Mov(new VReg(), new VReg()), + new Tryldglobalbyname(new Imm(1), 'b'), + new Stobjbyname(new Imm(2), "name", new VReg()), + new Returnundefined() + ]; + expect(checkInstructions(insns, expected)).to.be.true; + }); + + it("nestingParenthesizedPartiallyExprTest", function () { + let snippetCompiler = new SnippetCompiler(); + snippetCompiler.compileAfter( + ` + function reindexEdgeList(e: any, u: number):void { + --(((((e) as number)) as number) as number); + } + `, 'test.ts'); + IRNode.pg = new PandaGen("", creatAstFromSnippet(""), 0, undefined); + let insns = snippetCompiler.getPandaGenByName("UnitTest.reindexEdgeList").getInsns(); + let expected = [ + new Lda(new VReg()), + new Sta(new VReg()), + new Lda(new VReg()), + new Dec(new Imm(1)), + new Sta(new VReg()), + new Returnundefined() + ]; + expect(checkInstructions(insns, expected)).to.be.true; + }); +});