Skip to content

Commit ad983f3

Browse files
committed
Created IPopFrame
1 parent 2f908a0 commit ad983f3

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

  • src/main/java/org/jamplate/glucose/instruction/memory/frame
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2021 Cufy
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jamplate.glucose.instruction.memory.frame;
17+
18+
import org.jamplate.memory.Memory;
19+
import org.jamplate.model.Environment;
20+
import org.jamplate.model.Instruction;
21+
import org.jamplate.model.Tree;
22+
import org.jetbrains.annotations.NotNull;
23+
import org.jetbrains.annotations.Nullable;
24+
25+
import java.util.Objects;
26+
27+
/**
28+
* An instruction that pops the last frame.
29+
* <br><br>
30+
* Memory Visualization:
31+
* <pre>
32+
* ...[...][...]
33+
* ...[...]
34+
* </pre>
35+
*
36+
* @author LSafer
37+
* @version 0.3.0
38+
* @since 0.3.0 ~2021.07.11
39+
*/
40+
public class IPopFrame implements Instruction {
41+
/**
42+
* An instance of this instruction.
43+
*
44+
* @since 0.3.0 ~2021.07.11
45+
*/
46+
@NotNull
47+
public static final IPopFrame INSTANCE = new IPopFrame();
48+
49+
@SuppressWarnings("JavaDoc")
50+
private static final long serialVersionUID = 849942142548986258L;
51+
52+
/**
53+
* A reference of this instruction in the source code.
54+
*
55+
* @since 0.3.0 ~2021.07.11
56+
*/
57+
@Nullable
58+
protected final Tree tree;
59+
60+
/**
61+
* Construct a new instruction that pops the last frame.
62+
*
63+
* @since 0.3.0 ~2021.07.11
64+
*/
65+
public IPopFrame() {
66+
this.tree = null;
67+
}
68+
69+
/**
70+
* Construct a new instruction that pops the last frame.
71+
*
72+
* @param tree a reference for the constructed instruction in the source code.
73+
* @throws NullPointerException if the given {@code tree} is null.
74+
* @since 0.3.0 ~2021.07.11
75+
*/
76+
public IPopFrame(@NotNull Tree tree) {
77+
Objects.requireNonNull(tree, "tree");
78+
this.tree = tree;
79+
}
80+
81+
@Override
82+
public void exec(@NotNull Environment environment, @NotNull Memory memory) {
83+
Objects.requireNonNull(environment, "environment");
84+
Objects.requireNonNull(memory, "memory");
85+
86+
memory.popFrame();
87+
}
88+
89+
@Nullable
90+
@Override
91+
public Tree getTree() {
92+
return this.tree;
93+
}
94+
95+
@NotNull
96+
@Override
97+
public Instruction optimize(int mode) {
98+
return mode < 0 ? IPopFrame.INSTANCE : new IPopFrame(new Tree(this.tree));
99+
}
100+
}

0 commit comments

Comments
 (0)