-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.java
More file actions
109 lines (99 loc) · 2.39 KB
/
Memory.java
File metadata and controls
109 lines (99 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// COMS22201: Memory allocation for strings
import java.util.ArrayList;
import java.util.HashMap;
import java.io.*;
public class Memory {
static ArrayList<Byte> memory = new ArrayList<Byte>();
static HashMap<String, Integer> variableTable = new HashMap<String, Integer>();
static HashMap<String, Integer> realVariableTable = new HashMap<String, Integer>();
static int a = Memory.allocateString("true");
static int b = Memory.allocateString("false");
static public int allocateVar(String text) {
Integer addr = variableTable.get(text);
if (addr != null) {
return addr;
} else {
while(memory.size() % 4 != 0) {
allocateString("");
}
addr = memory.size();
variableTable.put(text,addr);
for (int i = 4; i> 0; i--) {
memory.add(new Byte(text, 0));
}
return addr;
}
}
static public int allocateRealVar(String text) {
Integer addr = realVariableTable.get(text);
if (addr != null) {
return addr;
} else {
while(memory.size() % 4 != 0) {
allocateString("");
}
addr = memory.size();
realVariableTable.put(text,addr);
for (int i = 4; i> 0; i--) {
memory.add(new Byte(text, 0));
}
return addr;
}
}
static public int realOrInt(String text) {
Integer real = realVariableTable.get(text);
Integer intnum = variableTable.get(text);
if (real != null && intnum == null){
return 0;
} else if (real == null && intnum != null){
return 1;
} else {
return 2;
}
}
static public int allocateString(String text)
{
int addr = memory.size();
int size = text.length();
for (int i=0; i<size; i++) {
memory.add(new Byte("", text.charAt(i)));
}
memory.add(new Byte("", 0));
return addr;
}
static public void dumpData(PrintStream o)
{
Byte b;
String s;
int c;
int size = memory.size();
for (int i=0; i<size; i++) {
b = memory.get(i);
c = b.getContents();
if (c >= 32) {
s = String.valueOf((char)c);
}
else {
s = ""; // "\\"+String.valueOf(c);
}
o.println("DATA "+c+" ; "+s+" "+b.getName());
}
}
}
class Byte {
String varname;
int contents;
Byte(String n, int c)
{
varname = n;
contents = c;
}
String getName()
{
return varname;
}
int getContents()
{
return contents;
}
}