-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiteralTable.java
More file actions
109 lines (95 loc) · 4.4 KB
/
LiteralTable.java
File metadata and controls
109 lines (95 loc) · 4.4 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
/*******************************************************************
* LiteralTable Class *
* *
* PROGRAMMER: Emily Culp *
* COURSE: CS340 - Programming Language Design *
* DATE: 11/12/2024 *
* REQUIREMENT: Manage literals for the interpreter *
* *
* DESCRIPTION: *
* This class manages a table of literals, allowing for the *
* addition, retrieval, and management of integer literals used *
* in the interpreter. Each literal is assigned a unique ID, and *
* the table provides functionality to access literal values *
* based on their IDs. If the literal already exists, the same ID *
* will be returned to ensure consistency. This helps maintain *
* efficient tracking of literals during program execution. *
* *
* COPYRIGHT: This code is copyright (C) 2024 Emily Culp and Dean *
* Zeller. *
* *
* CREDITS: This code was written with the help of ChatGPT. *
*******************************************************************/
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class LiteralTable{
private final Map<Integer, Object> literalTable = new HashMap<>();
private int nextLiteralID = 900;
private Map<String, Token> tokens;
public LiteralTable(){
tokens = new HashMap<>();
}
public int addLiteral(Object value){
if(!literalTable.containsValue(value)){
int literalID = nextLiteralID++;
literalTable.put(literalID, value);
System.out.println("Added literal with ID: " +literalID);
return literalID;
}else{
for(Map.Entry<Integer, Object> entry : literalTable.entrySet()){
if(entry.getValue().equals(value)){
System.out.println("Literal already exists, returning ID: " +entry.getKey());
return entry.getKey();
}
}
}
return -1;
}
public int getLiteralID(Object value){
for(Map.Entry<Integer, Object> entry : literalTable.entrySet()){
if(entry.getValue().equals(value)){
return entry.getKey();
}
}
return -1;
}
public boolean containsValue(Object value){
return literalTable.containsValue(value);
}
public Object getLiteralValue(String operand) {
// Assuming the operand is a literal represented as a string, e.g., "10"
try {
// Check if the operand is a valid integer
int value = Integer.parseInt(operand);
return value; // Return the parsed integer value
} catch (NumberFormatException e1) {
try {
// Check if the operand is a valid double
double value = Double.parseDouble(operand);
return value; // Return the parsed double value
} catch (NumberFormatException e2) {
// Handle other literal types or return null
// If it's not a number, it could be a variable or some other kind of literal
return null;
}
}
}
public Map<Integer, Object> getLiteralTable(){
return literalTable;
}
/**********************************************************
* METHOD: printTable() *
* DESCRIPTION: *
* Prints the contents of the literal table to the console.*
* This includes each literal ID and its corresponding *
* value, displaying the current state of the table. *
**********************************************************/
public void printTable() {
System.out.println();
System.out.println("Literal Table:");
for (Map.Entry<Integer, Object> entry : literalTable.entrySet()) {
System.out.println("ID: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
}