adds a new global field in the generic components context, see #657

This commit is contained in:
hneemann 2021-02-22 15:29:07 +01:00
parent e051c5cd25
commit d83280e5bf
3 changed files with 32 additions and 20 deletions

View File

@ -37,6 +37,7 @@ public class ResolveGenerics {
*/
public static final String GEN_ARGS_KEY = "genArgs";
private static final String SETTINGS_KEY = "settings";
private static final String GLOBALS_KEY = "global";
private final HashMap<String, Statement> map;
private final HashMap<Args, CircuitHolder> circuitMap;
private final Circuit circuit;
@ -128,13 +129,14 @@ public class ResolveGenerics {
ArrayList<VisualElement> newComponents = new ArrayList<>();
ArrayList<Wire> newWires = new ArrayList<>();
Globals globals = new Globals();
for (VisualElement ve : c.getElements())
if (ve.equalsDescription(GenericCode.DESCRIPTION))
handleVisualElement(c, ve, args, newComponents, newWires);
args.lock(); // allow write only in code components
handleVisualElement(c, ve, args, newComponents, newWires, globals);
globals.lock(); // allow write only in code components
for (VisualElement ve : c.getElements())
if (!ve.equalsDescription(GenericCode.DESCRIPTION))
handleVisualElement(c, ve, args, newComponents, newWires);
handleVisualElement(c, ve, args, newComponents, newWires, globals);
c.add(newWires);
for (VisualElement ve : newComponents)
@ -143,7 +145,7 @@ public class ResolveGenerics {
return new CircuitHolder(c, args);
}
private void handleVisualElement(Circuit c, VisualElement ve, Args args, ArrayList<VisualElement> newComponents, ArrayList<Wire> newWires) throws ElementNotFoundException, NodeException {
private void handleVisualElement(Circuit c, VisualElement ve, Args args, ArrayList<VisualElement> newComponents, ArrayList<Wire> newWires, Globals globals) throws ElementNotFoundException, NodeException {
ElementAttributes elementAttributes = ve.getElementAttributes();
String gen = elementAttributes.get(Keys.GENERIC).trim();
try {
@ -153,6 +155,7 @@ public class ResolveGenerics {
boolean isCustom = elementTypeDescription instanceof ElementTypeDescriptionCustom;
Statement genS = getStatement(gen);
Context mod = createContext(c, newComponents, newWires, args)
.declareVar(GLOBALS_KEY, globals)
.declareVar("args", args);
if (isCustom) {
mod.declareFunc("setCircuit", new SetCircuitFunc(ve));
@ -197,11 +200,9 @@ public class ResolveGenerics {
*/
public static final class Args implements HGSMap {
private final Context args;
private boolean writeEnabled;
private Args(Context args) {
this.args = args;
writeEnabled = true;
}
@Override
@ -216,18 +217,6 @@ public class ResolveGenerics {
return v;
}
@Override
public void hgsMapPut(String key, Object val) throws HGSEvalException {
if (writeEnabled)
args.declareVar(key, val);
else
args.hgsMapPut(key, val);
}
private void lock() {
writeEnabled = false;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -333,7 +322,7 @@ public class ResolveGenerics {
return;
}
if (!key.equals(Context.BASE_FILE_KEY) && !key.equals(SETTINGS_KEY)) {
if (!key.equals(Context.BASE_FILE_KEY) && !key.equals(SETTINGS_KEY) && !key.equals(GLOBALS_KEY)) {
contentSet.add(key);
sb.append(key).append(":=");
if (val instanceof String) {
@ -458,4 +447,26 @@ public class ResolveGenerics {
return new SubstituteLibrary.AllowSetAttributes(elementAttributes);
}
}
private static final class Globals implements HGSMap {
private final HashMap<String, Object> map = new HashMap<>();
private boolean writeEnable = true;
@Override
public void hgsMapPut(String key, Object val) throws HGSEvalException {
if (writeEnable)
map.put(key, val);
else
throw new HGSEvalException(Lang.get("err_writeInCodeComponentsOnly"));
}
@Override
public Object hgsMapGet(String key) throws HGSEvalException {
return map.get(key);
}
public void lock() {
writeEnable = false;
}
}
}

View File

@ -1210,7 +1210,7 @@ Sind evtl. die Namen der Variablen nicht eindeutig?</string>
</string>
<string name="err_multipleGenericInitCodes">Mehrere Initialisierungscodes für die generischen Elemente.</string>
<string name="err_inGenericInitCode">Fehler bei der Analyse des generischen Initialisierungscodes.</string>
<string name="err_writeInCodeComponentsOnly">Globale Variablen können nur in Code-Komponenten beschrieben werden.</string>
<string name="err_vgaModeNotDetected_N">Videomodus wurde nicht erkannt ({0})</string>
<string name="err_ROM_noFileGivenToLoad">Es ist kein Dateiname für das automatische Neuladen verfügbar!</string>

View File

@ -1199,6 +1199,7 @@
</string>
<string name="err_multipleGenericInitCodes">Multiple initialization codes for the generic elements.</string>
<string name="err_inGenericInitCode">Error in the analysis of the generic initialization code.</string>
<string name="err_writeInCodeComponentsOnly">Global variables can only be written in code components.</string>
<string name="err_ROM_noFileGivenToLoad">There is no file name available for the automatic reload!</string>
<string name="err_virtualSignal_N_DeclaredTwiceInLine_N">Virtual signal {0} declared twice in line {1}!</string>