mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-16 08:25:09 -04:00
improved args handling in generic code
This commit is contained in:
parent
ccd3f1864c
commit
e3c780e49d
@ -86,25 +86,28 @@
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>generic</string>
|
||||
<string>dataBits:=16;
|
||||
shiftBits:=int(4);
|
||||
circuit:="shift-fixed-left-inc.dig";
|
||||
if (isPresent(args)) {
|
||||
<string>if (isPresent(args)) {
|
||||
|
||||
if (args.dir="right") {
|
||||
circuit="shift-fixed-right-inc.dig";
|
||||
export circuit:="shift-fixed-right-inc.dig";
|
||||
} else {
|
||||
if (args.dir="arith") {
|
||||
circuit="shift-fixed-arith-right-inc.dig";
|
||||
export circuit:="shift-fixed-arith-right-inc.dig";
|
||||
} else {
|
||||
if (args.dir="left") {
|
||||
export circuit:="shift-fixed-left-inc.dig";
|
||||
} else {
|
||||
if (args.dir!="left") {
|
||||
panic("only \"left\", \"right\" or \"arith\" is allowed as direction, not \""+args.dir+"\"!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dataBits=args.dataBits;
|
||||
shiftBits=bitsNeededFor(args.dataBits-1);
|
||||
export shiftBits:=bitsNeededFor(args.dataBits-1);
|
||||
} else {
|
||||
// used if circuit is started as the main circuit
|
||||
export circuit:="shift-fixed-left-inc.dig";
|
||||
export dataBits:=16;
|
||||
export shiftBits:=int(4);
|
||||
}</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
|
@ -13,12 +13,13 @@
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>generic</string>
|
||||
<string>dataBits:=16;
|
||||
shift:=8;
|
||||
if (isPresent(args)) {
|
||||
dataBits = args.dataBits;
|
||||
shift = 1<<(args.shiftBits-1);
|
||||
<string>if (isPresent(args)) {
|
||||
export shift := 1<<(args.shiftBits-1);
|
||||
setCircuit(args.circuit);
|
||||
} else {
|
||||
// used if circuit is started as the main circuit
|
||||
export dataBits:=16;
|
||||
export shift:=8;
|
||||
}</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
@ -97,18 +98,18 @@ if (isPresent(args)) {
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>generic</string>
|
||||
<string>dataBits:=16;
|
||||
shiftBits:=3;
|
||||
circuit:="shift-fixed-left-inc.dig";
|
||||
if (isPresent(args)) {
|
||||
dataBits = args.dataBits;
|
||||
<string>if (isPresent(args)) {
|
||||
if (args.shiftBits=2) {
|
||||
export shift := 1;
|
||||
setCircuit(args.circuit);
|
||||
} else {
|
||||
shiftBits = args.shiftBits-1;
|
||||
circuit=args.circuit;
|
||||
export shiftBits := args.shiftBits-1;
|
||||
}
|
||||
} else {
|
||||
// used if circuit is started as the main circuit
|
||||
export dataBits:=16;
|
||||
export shiftBits:=3;
|
||||
export circuit:="shift-fixed-left-inc.dig";
|
||||
}</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
|
@ -138,9 +138,7 @@ ist als drei.}}</string>
|
||||
<entry>
|
||||
<string>generic</string>
|
||||
<string>inBits:=2;
|
||||
bits:=3;
|
||||
if (isPresent(args)) {
|
||||
bits=args.bits;
|
||||
if (args.bits>3) {
|
||||
setCircuit("GrayNode-inc.dig");
|
||||
}
|
||||
|
@ -107,14 +107,13 @@
|
||||
<elementAttributes>
|
||||
<entry>
|
||||
<string>generic</string>
|
||||
<string>inBits:=3;
|
||||
bits:=4;
|
||||
if (isPresent(args)) {
|
||||
inBits = args.inBits+1;
|
||||
bits=args.bits;
|
||||
<string>if (isPresent(args)) {
|
||||
export inBits := args.inBits+1;
|
||||
if (args.inBits<args.bits-2) {
|
||||
setCircuit("GrayNode-inc.dig");
|
||||
}
|
||||
} else {
|
||||
export inBits:=3;
|
||||
}</string>
|
||||
</entry>
|
||||
</elementAttributes>
|
||||
|
@ -69,7 +69,7 @@ public class ResolveGenerics {
|
||||
Statement genS = getStatement(gen);
|
||||
if (isCustom) {
|
||||
Context mod = new Context()
|
||||
.declareVar("args", args)
|
||||
.declareVar("args", new ArgsGetter(args))
|
||||
.declareFunc("setCircuit", new Function(1) {
|
||||
@Override
|
||||
protected Object f(Object... args) {
|
||||
@ -81,7 +81,7 @@ public class ResolveGenerics {
|
||||
ve.setGenericArgs(mod);
|
||||
} else {
|
||||
Context mod = new Context()
|
||||
.declareVar("args", args)
|
||||
.declareVar("args", new ArgsGetter(args))
|
||||
.declareVar("this", new SubstituteLibrary.AllowSetAttributes(ve.getElementAttributes()));
|
||||
genS.execute(mod);
|
||||
}
|
||||
@ -104,4 +104,23 @@ public class ResolveGenerics {
|
||||
return genS;
|
||||
}
|
||||
|
||||
private static final class ArgsGetter implements HGSMap {
|
||||
private final Context args;
|
||||
|
||||
private ArgsGetter(Context args) {
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object hgsMapGet(String key) throws HGSEvalException {
|
||||
Object v = args.hgsMapGet(key);
|
||||
if (v == null) {
|
||||
Object a = args.hgsMapGet("args");
|
||||
if (a instanceof HGSMap) {
|
||||
return ((HGSMap) a).hgsMapGet(key);
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -175,7 +175,7 @@ public class Parser {
|
||||
return lino(c -> ref.declareVar(c, initVal.value(c)));
|
||||
case EQUAL:
|
||||
if (export)
|
||||
throw newParserException("export not allowed here!");
|
||||
throw newParserException("export is only allowed at variable declaration!");
|
||||
final Expression val = parseExpression();
|
||||
if (isRealStatement) expect(SEMICOLON);
|
||||
return lino(c -> {
|
||||
@ -187,18 +187,18 @@ public class Parser {
|
||||
case ADD:
|
||||
expect(ADD);
|
||||
if (export)
|
||||
throw newParserException("export not allowed here!");
|
||||
throw newParserException("export is only allowed at variable declaration!");
|
||||
if (isRealStatement) expect(SEMICOLON);
|
||||
return lino(c -> ref.set(c, Value.toLong(ref.get(c)) + 1));
|
||||
case SUB:
|
||||
expect(SUB);
|
||||
if (export)
|
||||
throw newParserException("export not allowed here!");
|
||||
throw newParserException("export is only allowed at variable declaration!");
|
||||
if (isRealStatement) expect(SEMICOLON);
|
||||
return lino(c -> ref.set(c, Value.toLong(ref.get(c)) - 1));
|
||||
case SEMICOLON:
|
||||
if (export)
|
||||
throw newParserException("export not allowed here!");
|
||||
throw newParserException("export is only allowed at variable declaration!");
|
||||
return lino(ref::get);
|
||||
default:
|
||||
throw newUnexpectedToken(refToken);
|
||||
|
Loading…
x
Reference in New Issue
Block a user