From 73a093cf4ddf8e36dcba546652f524b00046f12d Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Sun, 9 Jul 2023 20:33:54 +0200 Subject: [PATCH] Ternaire Value --- .../peeratcode/parser/java/JavaParser.java | 24 ++++++++++++-- .../peeratcode/parser/java/Value.java | 33 ++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java index 9c14d1c..81af0fd 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/JavaParser.java @@ -242,15 +242,33 @@ public class JavaParser extends Parser { }; BiFunction END_BIVALUE = (element, validator) -> { //create new value from parent & created single value - Value v = new BiValue((Value)element, validator.getBag().get("?").get(), validator.getBag().get("type")); Value origin = (Value)element; + Value right = validator.getBag().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 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.get("true").get(), bag.get("false").get()); + origin.switchInto(v); + validator.getBag().set(v); + return v; }; StateTree value_q0 = new StateTree<>(); StateTree value_q22 = new StateTree<>(); @@ -275,7 +293,7 @@ public class JavaParser extends Parser { StateTree value_q26 = value_q22.then(LAMBDA_10); value_q26.then(new RedirectStateTree<>(value_q0,(bag) -> "?")).end(END_BIVALUE); // ! value_q26.then(LAMBDA_8).then(new RedirectStateTree<>(value_q0,(bag) -> "?")).end(END_BIVALUE); // != - value_q22.then(LAMBDA_11).then(new RedirectStateTree<>(value_q0,(bag) -> "?")).then(LAMBDA_12).then(new RedirectStateTree<>(value_q0,(bag) -> "?")).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")).end(END_TRIVALUE); //? : value_q22.then(LAMBDA_13).then(new RedirectStateTree<>(value_q0,(bag) -> "?")).end(END_BIVALUE); // ~ StateTree value_q29 = value_q22.then(LAMBDA_14); value_q29.then(new RedirectStateTree<>(value_q0,(bag) -> "?")).end(END_BIVALUE); // + diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Value.java b/src/be/jeffcheasey88/peeratcode/parser/java/Value.java index 4df347c..e563ea9 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Value.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Value.java @@ -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+"]"; } } }