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