Skip to content

Commit bdba34a

Browse files
committed
tie scalar WIP
1 parent 7f809b9 commit bdba34a

File tree

2 files changed

+108
-14
lines changed

2 files changed

+108
-14
lines changed

src/main/java/org/perlonjava/runtime/RuntimeScalar.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public int getInt() {
238238
case GLOB -> 1; // Assuming globs are truthy, so 1
239239
case REGEX -> 1; // Assuming regexes are truthy, so 1
240240
case JAVAOBJECT -> value != null ? 1 : 0;
241-
case TIED_SCALAR -> tiedFetch().getInt();
241+
case TIED_SCALAR -> TieScalar.tiedFetch(this).getInt();
242242
default -> Overload.numify(this).getInt();
243243
};
244244
}
@@ -255,7 +255,7 @@ public long getLong() {
255255
case GLOB -> 1L;
256256
case REGEX -> 1L;
257257
case JAVAOBJECT -> value != null ? 1L : 0L;
258-
case TIED_SCALAR -> tiedFetch().getLong();
258+
case TIED_SCALAR -> TieScalar.tiedFetch(this).getLong();
259259
default -> Overload.numify(this).getLong();
260260
};
261261
}
@@ -272,7 +272,7 @@ public double getDouble() {
272272
case GLOB -> 1.0;
273273
case REGEX -> 1.0;
274274
case JAVAOBJECT -> value != null ? 1.0 : 0.0;
275-
case TIED_SCALAR -> tiedFetch().getDouble();
275+
case TIED_SCALAR -> TieScalar.tiedFetch(this).getDouble();
276276
default -> Overload.numify(this).getDouble();
277277
};
278278
}
@@ -292,7 +292,7 @@ public boolean getBoolean() {
292292
case GLOB -> true;
293293
case REGEX -> true;
294294
case JAVAOBJECT -> value != null;
295-
case TIED_SCALAR -> tiedFetch().getBoolean();
295+
case TIED_SCALAR -> TieScalar.tiedFetch(this).getBoolean();
296296
default -> Overload.boolify(this).getBoolean();
297297
};
298298
}
@@ -332,18 +332,10 @@ public RuntimeScalar addToScalar(RuntimeScalar scalar) {
332332
return scalar.set(this);
333333
}
334334

335-
public RuntimeScalar tiedFetch() {
336-
throw new PerlCompilerException("not implemented: tied FETCH on scalar");
337-
}
338-
339-
public RuntimeScalar tiedStore() {
340-
throw new PerlCompilerException("not implemented: tied STORE on scalar");
341-
}
342-
343335
// Setters
344336
public RuntimeScalar set(RuntimeScalar value) {
345337
if (this.type == TIED_SCALAR) {
346-
return tiedStore();
338+
return TieScalar.tiedStore(this);
347339
}
348340
this.type = value.type;
349341
this.value = value.value;
@@ -412,7 +404,7 @@ public String toString() {
412404
case GLOB -> value == null ? "" : value.toString();
413405
case REGEX -> value.toString();
414406
case JAVAOBJECT -> value.toString();
415-
case TIED_SCALAR -> tiedFetch().toString();
407+
case TIED_SCALAR -> TieScalar.tiedFetch(this).toString();
416408
default -> Overload.stringify(this).toString();
417409
};
418410
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package org.perlonjava.runtime;
2+
3+
/**
4+
* TieScalar provides support for Perl's tie mechanism for scalar variables.
5+
*
6+
* <p>In Perl, the tie mechanism allows scalar variables to have their operations
7+
* intercepted and handled by custom classes. When a scalar is tied, all operations
8+
* on that scalar (fetching values, storing values, etc.) are delegated to methods
9+
* in the tie handler object.</p>
10+
*
11+
* <p>This class provides static methods that are called when operations are
12+
* performed on tied scalars. The actual tie handler implementation should
13+
* implement the {@code TieHandler} interface (to be implemented).</p>
14+
*
15+
* <p><b>Implementation Status:</b> This is currently a stub implementation.
16+
* The full tie mechanism requires:
17+
* <ul>
18+
* <li>Adding a TIED type constant to RuntimeScalarType</li>
19+
* <li>Creating a TieHandler interface with FETCH, STORE, UNTIE methods</li>
20+
* <li>Updating all RuntimeScalar operations to check for TIED type</li>
21+
* <li>Implementing tie(), untie(), and tied() methods on RuntimeScalar</li>
22+
* </ul>
23+
* </p>
24+
*
25+
* @see RuntimeScalar
26+
*/
27+
public class TieScalar {
28+
29+
/**
30+
* Fetches the value from a tied scalar variable.
31+
*
32+
* <p>This method is called whenever the value of a tied scalar is accessed.
33+
* It should delegate to the FETCH method of the tie handler object associated
34+
* with the scalar.</p>
35+
*
36+
* <p>In Perl, this corresponds to operations like:
37+
* <pre>{@code
38+
* my $value = $tied_scalar; # Calls FETCH
39+
* print $tied_scalar; # Calls FETCH
40+
* if ($tied_scalar) { ... } # Calls FETCH
41+
* }</pre>
42+
* </p>
43+
*
44+
* @param runtimeScalar the tied scalar whose value is being fetched
45+
* @return the value returned by the tie handler's FETCH method
46+
* @throws PerlCompilerException currently throws as not yet implemented
47+
*
48+
* TODO: Implement by:
49+
* 1. Extract the TieHandler from runtimeScalar.value
50+
* 2. Call handler.FETCH()
51+
* 3. Return the result
52+
*/
53+
public static RuntimeScalar tiedFetch(RuntimeScalar runtimeScalar) {
54+
// TODO: Implementation pattern:
55+
// TieHandler handler = (TieHandler) runtimeScalar.value;
56+
// return handler.FETCH();
57+
throw new PerlCompilerException("not implemented: tied FETCH on scalar");
58+
}
59+
60+
/**
61+
* Stores a value into a tied scalar variable.
62+
*
63+
* <p>This method is called whenever a value is assigned to a tied scalar.
64+
* It should delegate to the STORE method of the tie handler object associated
65+
* with the scalar.</p>
66+
*
67+
* <p>In Perl, this corresponds to operations like:
68+
* <pre>{@code
69+
* $tied_scalar = 42; # Calls STORE with 42
70+
* $tied_scalar = "hello"; # Calls STORE with "hello"
71+
* $tied_scalar++; # Calls FETCH, then STORE
72+
* }</pre>
73+
* </p>
74+
*
75+
* @param runtimeScalar the tied scalar being assigned to
76+
* @return the scalar after the store operation (typically returns runtimeScalar)
77+
* @throws PerlCompilerException currently throws as not yet implemented
78+
*
79+
* TODO: Implement by:
80+
* 1. Extract the TieHandler from runtimeScalar.value
81+
* 2. Extract the new value (needs to be passed as parameter)
82+
* 3. Call handler.STORE(newValue)
83+
* 4. Return runtimeScalar
84+
*
85+
* Note: The method signature will likely need to change to accept the value
86+
* being stored: tiedStore(RuntimeScalar runtimeScalar, RuntimeScalar newValue)
87+
*/
88+
public static RuntimeScalar tiedStore(RuntimeScalar runtimeScalar) {
89+
// TODO: Implementation pattern:
90+
// TieHandler handler = (TieHandler) runtimeScalar.value;
91+
// handler.STORE(newValue);
92+
// return runtimeScalar;
93+
throw new PerlCompilerException("not implemented: tied STORE on scalar");
94+
}
95+
96+
// TODO: Additional methods to implement:
97+
// - tiedUntie(RuntimeScalar runtimeScalar) - called by untie()
98+
// - tiedDestroy(RuntimeScalar runtimeScalar) - called when scalar goes out of scope
99+
//
100+
// For special operations that modify the scalar (chomp, chop, auto-increment):
101+
// - These will need to FETCH the value, modify it, then STORE it back
102+
}

0 commit comments

Comments
 (0)