• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import {
17    expect
18} from 'chai';
19import 'mocha';
20import {
21    Dec,
22    Inc,
23    Returnundefined,
24    Sttoglobalrecord,
25    Tonumeric,
26    Tryldglobalbyname,
27    Trystglobalbyname,
28    Imm,
29    Ldai,
30    Sta,
31    VReg,
32    Lda,
33    IRNode
34} from "../../src/irnodes";
35import { checkInstructions, compileMainSnippet } from "../utils/base";
36import { creatAstFromSnippet } from "../utils/asthelper";
37import { PandaGen } from '../../src/pandagen';
38
39describe("PostfixOperationsTest", function () {
40    it("let i = 0; i++", function () {
41        let insns = compileMainSnippet("let i = 5; i++");
42        IRNode.pg = new PandaGen("foo", creatAstFromSnippet(`let i = 5; i++`), 0, undefined);
43        let i = new VReg();
44        let temp = new VReg();
45        let expected = [
46            new Ldai(new Imm(5)),
47            new Sttoglobalrecord(new Imm(0), 'i'),
48            new Tryldglobalbyname(new Imm(1), 'i'),
49            new Sta(temp),
50            new Lda(temp),
51            new Inc(new Imm(2)),
52            new Trystglobalbyname(new Imm(3), 'i'),
53            new Lda(i),
54            new Tonumeric(new Imm(4), ),
55            new Returnundefined()
56        ];
57        expect(checkInstructions(insns, expected)).to.be.true;
58    });
59
60    it("let i = 0; i--", function () {
61        let insns = compileMainSnippet("let i = 5; i--");
62        IRNode.pg = new PandaGen("foo", creatAstFromSnippet(`let i = 5; i--`), 0, undefined);
63        let i = new VReg();
64        let temp = new VReg();
65        let expected = [
66            new Ldai(new Imm(5)),
67            new Sttoglobalrecord(new Imm(0), 'i'),
68            new Tryldglobalbyname(new Imm(1), 'i'),
69            new Sta(temp),
70            new Lda(temp),
71            new Dec(new Imm(2)),
72            new Trystglobalbyname(new Imm(3), 'i'),
73            new Lda(i),
74            new Tonumeric(new Imm(4)),
75            new Returnundefined()
76        ];
77        expect(checkInstructions(insns, expected)).to.be.true;
78    });
79});
80
81
82