-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSymbol.java
More file actions
180 lines (164 loc) · 7.07 KB
/
Symbol.java
File metadata and controls
180 lines (164 loc) · 7.07 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package org.perlonjava.runtime.perlmodule;
import org.perlonjava.backend.jvm.EmitterMethodCreator;
import org.perlonjava.runtime.runtimetypes.*;
import static org.perlonjava.runtime.runtimetypes.RuntimeContextType.SCALAR;
/**
* The Symbol class provides functionalities for symbol manipulation in a Perl-like environment.
* It extends PerlModuleBase to leverage module initialization and method registration.
*/
public class Symbol extends PerlModuleBase {
/**
* Constructor for Symbol.
* Initializes the module with the name "Symbol".
*/
public Symbol() {
super("Symbol");
}
/**
* Static initializer to set up the Symbol module.
* This method initializes the exporter and defines the symbols that can be exported.
*/
public static void initialize() {
Symbol symbol = new Symbol();
symbol.initializeExporter();
symbol.defineExport("EXPORT", "gensym", "ungensym", "qualify", "qualify_to_ref");
symbol.defineExport("EXPORT_OK", "delete_package", "geniosym");
try {
// Register methods with their respective signatures
symbol.registerMethod("gensym", "");
symbol.registerMethod("ungensym", "$");
symbol.registerMethod("qualify_to_ref", "$;$");
symbol.registerMethod("qualify", "$;$");
symbol.registerMethod("delete_package", "$");
symbol.registerMethod("geniosym", "");
} catch (NoSuchMethodException e) {
System.err.println("Warning: Missing Symbol method: " + e.getMessage());
}
}
/**
* Creates a new anonymous glob and returns a reference to it.
* This is equivalent to Perl's Symbol::gensym().
*
* @param args The arguments passed to the method.
* @param ctx The context in which the method is called.
* @return A RuntimeList containing a reference to a new anonymous glob.
*/
public static RuntimeList gensym(RuntimeArray args, int ctx) {
// Create a unique anonymous glob
String globName = "Symbol::GEN" + EmitterMethodCreator.classCounter++;
RuntimeGlob glob = new RuntimeGlob(globName);
// Return a reference to the glob (not the glob itself)
RuntimeScalar globRef = new RuntimeScalar();
globRef.type = RuntimeScalarType.GLOBREFERENCE;
globRef.value = glob;
return globRef.getList();
}
/**
* Placeholder for the ungensym functionality.
*
* @param args The arguments passed to the method.
* @param ctx The context in which the method is called.
* @return A RuntimeList.
*/
public static RuntimeList ungensym(RuntimeArray args, int ctx) {
if (args.size() != 1) {
throw new IllegalStateException("Bad number of arguments for ungensym()");
}
// Placeholder for ungensym functionality
return new RuntimeScalar().getList();
}
/**
* Placeholder for the delete_package functionality.
*
* @param args The arguments passed to the method.
* @param ctx The context in which the method is called.
* @return A RuntimeList.
*/
public static RuntimeList delete_package(RuntimeArray args, int ctx) {
if (args.size() != 1) {
throw new IllegalStateException("Bad number of arguments for delete_package()");
}
// Placeholder for delete_package functionality
return new RuntimeScalar().getList();
}
/**
* Creates a new anonymous IO handle.
* Equivalent to Perl's Symbol::geniosym() — creates an anonymous glob,
* initializes its IO slot, and returns a reference to it.
*
* @param args The arguments passed to the method.
* @param ctx The context in which the method is called.
* @return A RuntimeList containing a reference to a new anonymous glob with initialized IO.
*/
public static RuntimeList geniosym(RuntimeArray args, int ctx) {
// Create a unique anonymous glob (same as gensym)
String globName = "Symbol::GEN" + EmitterMethodCreator.classCounter++;
RuntimeGlob glob = new RuntimeGlob(globName);
// Initialize the IO slot (equivalent to Perl's: select(select $sym))
// The IO slot is already initialized by RuntimeGlob constructor (this.IO = new RuntimeScalar())
// Return a reference to the glob
RuntimeScalar globRef = new RuntimeScalar();
globRef.type = RuntimeScalarType.GLOBREFERENCE;
globRef.value = glob;
return globRef.getList();
}
/**
* Qualifies a symbol name with a package name.
* If no package name is provided, defaults to the caller's package.
*
* @param args The arguments passed to the method.
* @param ctx The context in which the method is called.
* @return A RuntimeList containing the qualified symbol name.
*/
public static RuntimeList qualify(RuntimeArray args, int ctx) {
if (args.size() < 1 || args.size() > 2) {
throw new IllegalStateException("Bad number of arguments for qualify()");
}
RuntimeScalar object = args.get(0);
RuntimeScalar packageName = null;
if (args.size() > 1) {
packageName = args.get(1);
} else {
RuntimeList callerList = RuntimeCode.caller(new RuntimeList(), SCALAR);
packageName = callerList.scalar();
}
RuntimeScalar result;
// System.out.println("qualify " + object + " :: " + packageName + " type:" + object.type);
if (!object.isString()) {
result = object;
} else {
// System.out.println("qualify normalizeVariableName");
result = new RuntimeScalar(NameNormalizer.normalizeVariableName(object.toString(), packageName.toString()));
}
RuntimeList list = new RuntimeList();
list.elements.add(result);
return list;
}
/**
* Qualifies a symbol name with a package name and returns a glob reference.
*
* @param args The arguments passed to the method.
* @param ctx The context in which the method is called.
* @return A RuntimeList containing the qualified glob reference.
*/
public static RuntimeList qualify_to_ref(RuntimeArray args, int ctx) {
if (args.size() < 1 || args.size() > 2) {
throw new IllegalStateException("Bad number of arguments for qualify_to_ref()");
}
RuntimeScalar object = qualify(args, ctx).scalar();
RuntimeScalar result;
if (!object.isString()) {
// Already a glob reference or similar — return as-is
result = object;
} else {
// Create a named RuntimeGlob and return a GLOBREFERENCE to it.
// This mirrors Perl's \*{name}: the caller gets a reference whose
// hash slot (and other slots) delegate to the global symbol table.
result = new RuntimeGlob(object.toString()).createReference();
}
// System.out.println("qualify_to_ref returns " + result.type);
RuntimeList list = new RuntimeList();
list.elements.add(result);
return list;
}
}