Ternaire Value

This commit is contained in:
jeffcheasey88 2023-07-09 20:33:54 +02:00
parent 5ad4f66c8a
commit 73a093cf4d
2 changed files with 53 additions and 4 deletions

View file

@ -242,15 +242,33 @@ public class JavaParser extends Parser<JavaFile> {
};
BiFunction<JavaElement, TokenValidator, JavaElement> END_BIVALUE = (element, validator) -> {
//create new value from parent & created single value
Value v = new BiValue((Value)element, validator.getBag().<Bag>get("?").get(), validator.getBag().get("type"));
Value origin = (Value)element;
Value right = validator.getBag().<Bag>get("?").get();
if(right.get() != null) right = right.get();
if(right instanceof TriValue){
TriValue last = (TriValue)right;
last.getCondition().switchInto(null);
BiValue condition = new BiValue(origin, last.getCondition(), validator.getBag().get("type"));
TriValue result = new TriValue(condition, last.success(), last.fail());
origin.switchInto(result);
validator.getBag().set(result);
return result;
}
Value v = new BiValue(origin, right, validator.getBag().get("type"));
origin.switchInto(v);
validator.getBag().set(v);
return v;
};
BiFunction<JavaElement, TokenValidator, JavaElement> END_TRIVALUE = (element, validator) -> {
//same but for val ? val : val
return new TriValue();
System.out.println("\t"+element);
System.out.println("\t"+validator.getBag());
Bag bag = validator.getBag();
Value origin = (Value)element;
Value v = new TriValue(origin, bag.<Bag>get("true").get(), bag.<Bag>get("false").get());
origin.switchInto(v);
validator.getBag().set(v);
return v;
};
StateTree<JavaElement> value_q0 = new StateTree<>();
StateTree<JavaElement> value_q22 = new StateTree<>();
@ -275,7 +293,7 @@ public class JavaParser extends Parser<JavaFile> {
StateTree<JavaElement> value_q26 = value_q22.then(LAMBDA_10);
value_q26.then(new RedirectStateTree<>(value_q0,(bag) -> "?")).<JavaElement>end(END_BIVALUE); // !
value_q26.then(LAMBDA_8).then(new RedirectStateTree<>(value_q0,(bag) -> "?")).<JavaElement>end(END_BIVALUE); // !=
value_q22.then(LAMBDA_11).then(new RedirectStateTree<>(value_q0,(bag) -> "?")).then(LAMBDA_12).then(new RedirectStateTree<>(value_q0,(bag) -> "?")).<JavaElement>end(END_TRIVALUE); //? :
value_q22.then(LAMBDA_11).then(new RedirectStateTree<>(value_q0,(bag) -> "true")).then(LAMBDA_12).then(new RedirectStateTree<>(value_q0,(bag) -> "false")).<JavaElement>end(END_TRIVALUE); //? :
value_q22.then(LAMBDA_13).then(new RedirectStateTree<>(value_q0,(bag) -> "?")).<JavaElement>end(END_BIVALUE); // ~
StateTree<JavaElement> value_q29 = value_q22.then(LAMBDA_14);
value_q29.then(new RedirectStateTree<>(value_q0,(bag) -> "?")).<JavaElement>end(END_BIVALUE); // +

View file

@ -16,6 +16,7 @@ public class Value extends JavaElement{
}
public void switchInto(Value value){
System.out.println("\tswitch "+this+" onto "+value);
this.value = value;
}
@ -51,8 +52,38 @@ public class Value extends JavaElement{
public static class TriValue extends Value{
public TriValue(){
private Value condition;
private Value success;
private Value fail;
public TriValue(Value condition, Value success, Value fail){
this.condition = condition;
this.success = success;
this.fail = fail;
System.out.println(this);
}
public void setCondition(Value value){
this.condition = value;
}
public Value getCondition(){
return this.condition;
}
public Value success(){
return this.success;
}
public Value fail(){
return this.fail;
}
@Override
public String toString(){
return "[TriValue "+condition+" ? "+success+" : "+fail+"]";
}
}
}