diff --git a/src/main/java/de/neemann/digital/draw/library/ElementTypeDescriptionCustom.java b/src/main/java/de/neemann/digital/draw/library/ElementTypeDescriptionCustom.java index 8108a5214..4d7199698 100644 --- a/src/main/java/de/neemann/digital/draw/library/ElementTypeDescriptionCustom.java +++ b/src/main/java/de/neemann/digital/draw/library/ElementTypeDescriptionCustom.java @@ -25,7 +25,6 @@ import de.neemann.digital.lang.Lang; import java.io.File; import java.io.IOException; -import java.util.HashMap; import java.util.TreeSet; /** @@ -36,7 +35,7 @@ public final class ElementTypeDescriptionCustom extends ElementTypeDescription { private static final int MAX_DEPTH = 30; private final File file; private final Circuit circuit; - private final HashMap map; + private final StatementCache statementCache; private String description; private NetList netList; private boolean isCustom = true; @@ -53,7 +52,7 @@ public final class ElementTypeDescriptionCustom extends ElementTypeDescription { super(file.getName(), (ElementFactory) null, circuit.getInputNames()); this.file = file; this.circuit = circuit; - map = new HashMap<>(); + statementCache=new StatementCache(); setShortName(file.getName()); addAttribute(Keys.ROTATE); addAttribute(Keys.LABEL); @@ -130,10 +129,8 @@ public final class ElementTypeDescriptionCustom extends ElementTypeDescription { if (args == null) { String argsCode = containingVisualElement.getElementAttributes().get(Keys.GENERIC); try { - Statement s = getStatement(argsCode); + Statement s = statementCache.getStatement(argsCode); args = new Context(); - if (containingVisualElement.getGenericArgs() != null) - args.declareVar("args", containingVisualElement.getGenericArgs()); s.execute(args); } catch (HGSEvalException | ParserException | IOException e) { final NodeException ex = new NodeException(Lang.get("err_evaluatingGenericsCode_N_N", containingVisualElement, argsCode), e); @@ -150,7 +147,7 @@ public final class ElementTypeDescriptionCustom extends ElementTypeDescription { try { if (!gen.isEmpty()) { boolean isCustom = library.getElementType(ve.getElementName(), ve.getElementAttributes()).isCustom(); - Statement genS = getStatement(gen); + Statement genS = statementCache.getStatement(gen); if (isCustom) { Context mod = new Context() .declareVar("args", args) @@ -182,15 +179,6 @@ public final class ElementTypeDescriptionCustom extends ElementTypeDescription { return new ModelCreator(circuit, library, true, new NetList(netList, errorVisualElement), subName, depth, errorVisualElement); } - private Statement getStatement(String code) throws IOException, ParserException { - Statement genS = map.get(code); - if (genS == null) { - genS = new Parser(code).parse(false); - map.put(code, genS); - } - return genS; - } - @Override public boolean isCustom() { return isCustom; diff --git a/src/main/java/de/neemann/digital/draw/library/StatementCache.java b/src/main/java/de/neemann/digital/draw/library/StatementCache.java new file mode 100644 index 000000000..6a705729e --- /dev/null +++ b/src/main/java/de/neemann/digital/draw/library/StatementCache.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 Helmut Neemann. + * Use of this source code is governed by the GPL v3 license + * that can be found in the LICENSE file. + */ +package de.neemann.digital.draw.library; + +import de.neemann.digital.hdl.hgs.Parser; +import de.neemann.digital.hdl.hgs.ParserException; +import de.neemann.digital.hdl.hgs.Statement; + +import java.io.IOException; +import java.util.HashMap; + +/** + * Implements a cache for statements + */ +public class StatementCache { + private final HashMap map; + + /** + * Creates a new instance + */ + public StatementCache() { + map = new HashMap<>(); + } + + /** + * Gets the analysed statements. + * If the statements are not present in the cache, they are generated. + * + * @param code the code + * @return the statements + * @throws IOException IOException + * @throws ParserException ParserException + */ + public Statement getStatement(String code) throws IOException, ParserException { + Statement genS = map.get(code); + if (genS == null) { + genS = new Parser(code).parse(false); + map.put(code, genS); + } + return genS; + } + +}