mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-14 15:26:52 -04:00
added a statement cache
This commit is contained in:
parent
470b8f19a9
commit
e0b8254f54
@ -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<String, Statement> 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;
|
||||
|
@ -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<String, Statement> 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user