Merge branch 'globalVars'

This commit is contained in:
hneemann 2021-02-23 18:32:37 +01:00
commit 040c5e689e
4 changed files with 66 additions and 29 deletions

View File

@ -69,6 +69,7 @@ for (i:=0;i<sh;i++) {
addWire(x-1,i+5,x-1,2);
addWire(x-1,2,x,2);
}
global.shift:=sh;
o:=addComponent("Out",sh*5+2,0);
o.Bits=args.dataBits;
@ -86,7 +87,7 @@ o.Label=&quot;Out&quot;;</string>
</entry>
<entry>
<string>generic</string>
<string>this.Bits=bitsNeededFor(args.dataBits)-1;</string>
<string>this.Bits=global.shift;</string>
</entry>
</elementAttributes>
<pos x="-60" y="100"/>
@ -100,9 +101,8 @@ o.Label=&quot;Out&quot;;</string>
</entry>
<entry>
<string>generic</string>
<string>sh:=bitsNeededFor(args.dataBits)-1;
this.&apos;Input Splitting&apos;=&quot;&quot;+sh;
this.&apos;Output Splitting&apos;=&quot;1*&quot;+sh;</string>
<string>this.&apos;Input Splitting&apos;=&quot;&quot;+global.shift;
this.&apos;Output Splitting&apos;=&quot;1*&quot;+global.shift;</string>
</entry>
<entry>
<string>Output Splitting</string>

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,7 +129,25 @@ public class ResolveGenerics {
ArrayList<VisualElement> newComponents = new ArrayList<>();
ArrayList<Wire> newWires = new ArrayList<>();
for (VisualElement ve : c.getElements()) {
Globals globals = new Globals();
for (VisualElement ve : c.getElements())
if (ve.equalsDescription(GenericCode.DESCRIPTION)) {
handleVisualElement(c, ve, args, newComponents, newWires, globals);
globals.lock(); // allow write only in first code component
}
globals.lock(); // allow write only in code components
for (VisualElement ve : c.getElements())
if (!ve.equalsDescription(GenericCode.DESCRIPTION))
handleVisualElement(c, ve, args, newComponents, newWires, globals);
c.add(newWires);
for (VisualElement ve : newComponents)
c.add(ve);
return new CircuitHolder(c, args);
}
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 {
@ -138,6 +157,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));
@ -152,12 +172,6 @@ public class ResolveGenerics {
throw new NodeException(Lang.get("err_evaluatingGenericsCode_N_N", ve, gen), e);
}
}
c.add(newWires);
for (VisualElement ve : newComponents)
c.add(ve);
return new CircuitHolder(c, args);
}
private Context createContext(Circuit circuit, ArrayList<VisualElement> newComponents, ArrayList<Wire> newWires, Args args) throws NodeException {
try {
@ -310,7 +324,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) {
@ -435,4 +449,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>