mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-22 11:55:15 -04:00
allows to set the inverter config in generic code, see #622
This commit is contained in:
parent
d0e79ec9b5
commit
ffa0b01e32
@ -12,6 +12,7 @@ import de.neemann.digital.draw.elements.VisualElement;
|
||||
import de.neemann.digital.draw.library.ElementLibrary;
|
||||
import de.neemann.digital.draw.library.ElementNotFoundException;
|
||||
import de.neemann.digital.draw.library.LibraryInterface;
|
||||
import de.neemann.digital.draw.model.InverterConfig;
|
||||
import de.neemann.digital.draw.shapes.ShapeFactory;
|
||||
import de.neemann.digital.hdl.hgs.*;
|
||||
import de.neemann.digital.lang.Lang;
|
||||
@ -229,6 +230,11 @@ public class SubstituteLibrary implements LibraryInterface {
|
||||
return (int) l;
|
||||
} else if (expectedClass == Color.class && val instanceof Number) {
|
||||
return new Color(((Number) val).intValue());
|
||||
} else if (expectedClass == InverterConfig.class && val instanceof java.util.List) {
|
||||
InverterConfig.Builder b = new InverterConfig.Builder();
|
||||
for (Object i : (java.util.List) val)
|
||||
b.add(i.toString());
|
||||
return b.build();
|
||||
} else if (expectedClass == Rotation.class && val instanceof Number) {
|
||||
int r = ((Number) val).intValue();
|
||||
return new Rotation(r % 4);
|
||||
|
@ -57,7 +57,7 @@ public class Context implements HGSMap {
|
||||
BUILT_IN.put("loadHex", new FunctionLoadHex());
|
||||
BUILT_IN.put("sizeOf", new Func(1, args -> Value.toArray(args[0]).hgsArraySize()));
|
||||
BUILT_IN.put("newMap", new Func(0, args -> new HashMap()));
|
||||
BUILT_IN.put("newList", new Func(0, args -> new ArrayList()));
|
||||
BUILT_IN.put("newList", new Func(-1, args -> new ArrayList<>(Arrays.asList(args))));
|
||||
}
|
||||
|
||||
private final Context parent;
|
||||
@ -692,4 +692,5 @@ public class Context implements HGSMap {
|
||||
public int hashCode() {
|
||||
return Objects.hash(parent, map);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -331,6 +331,17 @@ public class ParserTest extends TestCase {
|
||||
assertEquals(7L, l.get(1));
|
||||
}
|
||||
|
||||
public void testParseTemplateArray2() throws IOException, ParserException, HGSEvalException {
|
||||
Context c = exec("<? a:=newList(1,7); print(a[1], \",\" ,sizeOf(a)); ?>;");
|
||||
assertEquals("7,2;", c.toString());
|
||||
Object lo = c.getVar("a");
|
||||
assertTrue(lo instanceof List);
|
||||
List l = (List) lo;
|
||||
assertEquals(2, l.size());
|
||||
assertEquals(1L, l.get(0));
|
||||
assertEquals(7L, l.get(1));
|
||||
}
|
||||
|
||||
public void testParseTemplateMap() throws IOException, ParserException, HGSEvalException {
|
||||
Context c = exec("<? m:=newMap(); m.test:=newMap(); m.test.val:=7; print(m.test.val); ?>;");
|
||||
assertEquals("7;", c.toString());
|
||||
|
@ -51,8 +51,8 @@ public class TestExamples extends TestCase {
|
||||
*/
|
||||
public void testTestExamples() throws Exception {
|
||||
File examples = new File(Resources.getRoot(), "/dig/test");
|
||||
assertEquals(206, new FileScanner(this::check).scan(examples));
|
||||
assertEquals(192, testCasesInFiles);
|
||||
assertEquals(207, new FileScanner(this::check).scan(examples));
|
||||
assertEquals(193, testCasesInFiles);
|
||||
}
|
||||
|
||||
/**
|
||||
|
95
src/test/resources/dig/test/generics/inverterConfig.dig
Normal file
95
src/test/resources/dig/test/generics/inverterConfig.dig
Normal file
@ -0,0 +1,95 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<circuit>
|
||||
<version>1</version>
|
||||
<attributes>
|
||||
<entry>
|
||||
<string>isGeneric</string>
|
||||
<boolean>true</boolean>
|
||||
</entry>
|
||||
</attributes>
|
||||
<visualElements>
|
||||
<visualElement>
|
||||
<elementName>In</elementName>
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>Label</string>
|
||||
<string>D</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="-40" y="-100"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>Out</elementName>
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>Label</string>
|
||||
<string>Q</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="80" y="-100"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>D_FF</elementName>
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>generic</string>
|
||||
<string>this.inverterConfig=newList("D");</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="0" y="-100"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>GenericInitCode</elementName>
|
||||
<elementAttributes/>
|
||||
<pos x="-80" y="-40"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>Clock</elementName>
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>Label</string>
|
||||
<string>C</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="-40" y="-60"/>
|
||||
</visualElement>
|
||||
<visualElement>
|
||||
<elementName>Testcase</elementName>
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>Testdata</string>
|
||||
<testData>
|
||||
<dataString>C D Q
|
||||
c 0 1
|
||||
c 1 0
|
||||
</dataString>
|
||||
</testData>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
<pos x="20" y="-40"/>
|
||||
</visualElement>
|
||||
</visualElements>
|
||||
<wires>
|
||||
<wire>
|
||||
<p1 x="-40" y="-100"/>
|
||||
<p2 x="-20" y="-100"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="60" y="-100"/>
|
||||
<p2 x="80" y="-100"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="-40" y="-60"/>
|
||||
<p2 x="-20" y="-60"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="-20" y="-80"/>
|
||||
<p2 x="0" y="-80"/>
|
||||
</wire>
|
||||
<wire>
|
||||
<p1 x="-20" y="-80"/>
|
||||
<p2 x="-20" y="-60"/>
|
||||
</wire>
|
||||
</wires>
|
||||
<measurementOrdering/>
|
||||
</circuit>
|
Loading…
x
Reference in New Issue
Block a user