mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-17 17:04:42 -04:00
Merge branch 'globalVars'
This commit is contained in:
commit
040c5e689e
@ -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="Out";</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="Out";</string>
|
||||
</entry>
|
||||
<entry>
|
||||
<string>generic</string>
|
||||
<string>sh:=bitsNeededFor(args.dataBits)-1;
|
||||
this.'Input Splitting'=""+sh;
|
||||
this.'Output Splitting'="1*"+sh;</string>
|
||||
<string>this.'Input Splitting'=""+global.shift;
|
||||
this.'Output Splitting'="1*"+global.shift;</string>
|
||||
</entry>
|
||||
<entry>
|
||||
<string>Output Splitting</string>
|
||||
|
@ -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,30 +129,17 @@ public class ResolveGenerics {
|
||||
ArrayList<VisualElement> newComponents = new ArrayList<>();
|
||||
ArrayList<Wire> newWires = new ArrayList<>();
|
||||
|
||||
for (VisualElement ve : c.getElements()) {
|
||||
ElementAttributes elementAttributes = ve.getElementAttributes();
|
||||
String gen = elementAttributes.get(Keys.GENERIC).trim();
|
||||
try {
|
||||
if (!gen.isEmpty()) {
|
||||
ElementTypeDescription elementTypeDescription = library.getElementType(ve.getElementName(), elementAttributes);
|
||||
|
||||
boolean isCustom = elementTypeDescription instanceof ElementTypeDescriptionCustom;
|
||||
Statement genS = getStatement(gen);
|
||||
Context mod = createContext(c, newComponents, newWires, args)
|
||||
.declareVar("args", args);
|
||||
if (isCustom) {
|
||||
mod.declareFunc("setCircuit", new SetCircuitFunc(ve));
|
||||
genS.execute(mod);
|
||||
} else {
|
||||
mod.declareVar("this", new SubstituteLibrary.AllowSetAttributes(elementAttributes));
|
||||
genS.execute(mod);
|
||||
}
|
||||
elementAttributes.putToCache(GEN_ARGS_KEY, mod);
|
||||
}
|
||||
} catch (HGSEvalException | ParserException | IOException e) {
|
||||
throw new NodeException(Lang.get("err_evaluatingGenericsCode_N_N", ve, gen), e);
|
||||
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);
|
||||
@ -159,6 +147,32 @@ public class ResolveGenerics {
|
||||
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 {
|
||||
if (!gen.isEmpty()) {
|
||||
ElementTypeDescription elementTypeDescription = library.getElementType(ve.getElementName(), elementAttributes);
|
||||
|
||||
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));
|
||||
genS.execute(mod);
|
||||
} else {
|
||||
mod.declareVar("this", new SubstituteLibrary.AllowSetAttributes(elementAttributes));
|
||||
genS.execute(mod);
|
||||
}
|
||||
elementAttributes.putToCache(GEN_ARGS_KEY, mod);
|
||||
}
|
||||
} catch (HGSEvalException | ParserException | IOException e) {
|
||||
throw new NodeException(Lang.get("err_evaluatingGenericsCode_N_N", ve, gen), e);
|
||||
}
|
||||
}
|
||||
|
||||
private Context createContext(Circuit circuit, ArrayList<VisualElement> newComponents, ArrayList<Wire> newWires, Args args) throws NodeException {
|
||||
try {
|
||||
Context context = new Context();
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user