summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-08-15 18:15:15 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-08-15 18:15:15 +0200
commit3a910e2df9bb71e0a1601e3be9367278bf6f897f (patch)
treec211f646c87a2459ad921ee9e9d8f71a037cdf84
parent9db1108195fe6c2572005c3eb338c14b445fee3b (diff)
Fixes issue with computational switch.
-rw-r--r--data/examples/monster_battle/include/progress.fate2
-rw-r--r--data/tests/conditionals.fate197
-rw-r--r--src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java151
3 files changed, 298 insertions, 52 deletions
diff --git a/data/examples/monster_battle/include/progress.fate b/data/examples/monster_battle/include/progress.fate
index 3db8884..12b35d0 100644
--- a/data/examples/monster_battle/include/progress.fate
+++ b/data/examples/monster_battle/include/progress.fate
@@ -2,4 +2,4 @@
(require types/tag.fate)
-(global (list tag) progress)
+(global (set tag) progress)
diff --git a/data/tests/conditionals.fate b/data/tests/conditionals.fate
index 82168ac..0728cbc 100644
--- a/data/tests/conditionals.fate
+++ b/data/tests/conditionals.fate
@@ -1,5 +1,6 @@
(fate_version 1)
+(global (list int) test_list)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; COMPUTATIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -45,58 +46,154 @@
)
FAILED: computation cond D
)
+(assert
+ (cond
+ ((false) (false))
+ ((true)
+ (cond
+ ((false) (false))
+ ((true) (not (is_member 3 test_list)))
+ ((true) (false))
+ )
+ )
+ ((true) (false))
+ )
+ FAILED: computation cond E
+)
+(assert
+ (cond
+ ((false) (false))
+ ((false) (false))
+ ((true)
+ (cond
+ ((false) (false))
+ ((true) (not (is_member 3 test_list)))
+ ((true) (false))
+ )
+ )
+ )
+ FAILED: computation cond F
+)
;; COMPUTATION SWITCH
-;; TODO: re-enable. Currently not implemented in Wyrd compiler
-;;(assert
-;; (switch 3
-;; (0 (false))
-;; (1 (false))
-;; (3 (true))
-;; (2 (false))
-;; (false)
-;; )
-;; FAILED: computation switch A
-;;)
-;;(assert
-;; (switch 3
-;; (0 (false))
-;; (1 (false))
-;; (2 (false))
-;; (true)
-;; )
-;; FAILED: computation switch B
-;;)
-;;(assert
-;; (switch 3
-;; (0 (false))
-;; (1 (false))
-;; (2 (false))
-;; (switch 2
-;; (0 (false))
-;; (1 (false))
-;; (2 (true))
-;; (false)
-;; )
-;; )
-;; FAILED: computation switch C
-;;)
-;;(assert
-;; (switch 3
-;; (0 (false))
-;; (1 (false))
-;; (2
-;; (switch 1
-;; (0 (false))
-;; (2 (false))
-;; (1 (true))
-;; (false)
-;; )
-;; )
-;; (false)
-;; )
-;; FAILED: computation switch D
-;;)
+(assert
+ (switch 3
+ (0 (false))
+ (1 (false))
+ (3 (true))
+ (2 (false))
+ (false)
+ )
+ FAILED: computation switch A
+)
+
+(assert
+ (switch 3
+ (0 (false))
+ (1 (false))
+ (2 (false))
+ (3 (true))
+ (false)
+ )
+ FAILED: computation switch A BIS
+)
+(assert
+ (switch 3
+ (0 (false))
+ (1 (false))
+ (2 (false))
+ (true)
+ )
+ FAILED: computation switch B
+)
+(assert
+ (switch 3
+ (0 (false))
+ (1 (false))
+ (2 (false))
+ (switch 2
+ (0 (false))
+ (1 (false))
+ (2 (true))
+ (false)
+ )
+ )
+ FAILED: computation switch C
+)
+(assert
+ (switch 2
+ (0 (false))
+ (1 (false))
+ (2
+ (switch 1
+ (0 (false))
+ (2 (false))
+ (1 (true))
+ (false)
+ )
+ )
+ (false)
+ )
+ FAILED: computation switch D
+)
+(assert
+ (switch 4
+ (0 (false))
+ (1 (false))
+ (4
+ (switch 44
+ (0 (false))
+ (2 (false))
+ (true)
+ )
+ )
+ (false)
+ )
+ FAILED: computation switch D BIS
+)
+(assert
+ (switch 4
+ (0 (false))
+ (1 (false))
+ (4 (not (is_member 3 test_list)))
+ (false)
+ )
+ FAILED: computation switch D BIS BIS
+)
+(assert
+ (switch 4
+ (4 (not (is_member 3 test_list)))
+ (0 (false))
+ (1 (false))
+ (false)
+ )
+ FAILED: computation switch D BIS BIS BIS
+)
+(assert
+ (switch 4
+ (0 (false))
+ (1 (false))
+ (3 (false))
+ (not (is_member 3 test_list))
+ )
+ FAILED: computation switch D BIS BIS BIS BIS
+)
+(assert
+ (switch 1
+ (0 (false))
+ (1
+ (switch 2
+ (0 (false))
+ (1 (false))
+ (2 (true))
+ (false)
+ )
+ )
+ (2 (false))
+ (false)
+ )
+ FAILED: computation switch E
+)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; INSTRUCTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java
index 575af47..0c5d49b 100644
--- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java
+++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java
@@ -1213,7 +1213,156 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor
)
throws Throwable
{
- /* TODO */
+ final ComputationCompiler target_cc, default_cc;
+ final List<Cons<ComputationCompiler, ComputationCompiler>> cc_list;
+ boolean is_safe;
+
+ target_cc = new ComputationCompiler(compiler);
+ default_cc = new ComputationCompiler(compiler);
+
+ n.get_target().get_visited_by(target_cc);
+
+ target_cc.generate_address();
+
+ assimilate(target_cc);
+
+ cc_list = new ArrayList<Cons<ComputationCompiler, ComputationCompiler>>();
+ is_safe = true;
+
+ n.get_default().get_visited_by(default_cc);
+
+ for
+ (
+ final Cons
+ <
+ tonkadur.fate.v1.lang.meta.Computation,
+ tonkadur.fate.v1.lang.meta.Computation
+ >
+ branch:
+ n.get_branches()
+ )
+ {
+ final ComputationCompiler candidate_cc, val_cc;
+
+ candidate_cc = new ComputationCompiler(compiler);
+ val_cc = new ComputationCompiler(compiler);
+
+ branch.get_car().get_visited_by(candidate_cc);
+ branch.get_cdr().get_visited_by(val_cc);
+
+ is_safe = is_safe && !candidate_cc.has_init() && !val_cc.has_init();
+
+ reserved_registers.addAll(candidate_cc.reserved_registers);
+ reserved_registers.addAll(val_cc.reserved_registers);
+
+ cc_list.add(new Cons(candidate_cc, val_cc));
+ }
+
+ is_safe = is_safe && !default_cc.has_init();
+ reserved_registers.addAll(default_cc.reserved_registers);
+
+ Collections.reverse(cc_list);
+
+ if (is_safe)
+ {
+ final Iterator<Cons<ComputationCompiler, ComputationCompiler>> it;
+
+ it = cc_list.iterator();
+
+ result_as_computation = default_cc.get_computation();
+
+ while (it.hasNext())
+ {
+ final Cons<ComputationCompiler, ComputationCompiler> next;
+
+ next = it.next();
+
+ result_as_computation =
+ new IfElseComputation
+ (
+ Operation.equals
+ (
+ next.get_car().get_computation(),
+ target_cc.get_computation()
+ ),
+ next.get_cdr().get_computation(),
+ result_as_computation
+ );
+ }
+ }
+ else
+ {
+ final Iterator<Cons<ComputationCompiler, ComputationCompiler>> it;
+ final Register result;
+ Cons<ComputationCompiler, ComputationCompiler> next;
+ List<Instruction> new_value, new_cond;
+ Instruction prev_branch;
+
+ it = cc_list.iterator();
+
+
+ new_value = new ArrayList<Instruction>();
+
+ if (default_cc.has_init())
+ {
+ new_value.add(default_cc.get_init());
+ }
+
+ result = reserve(default_cc.get_computation().get_type());
+ result_as_address = result.get_address();
+
+ new_value.add
+ (
+ new SetValue(result_as_address, default_cc.get_computation())
+ );
+
+ prev_branch = compiler.assembler().merge(new_value);
+
+ while (it.hasNext())
+ {
+ next = it.next();
+
+ new_value = new ArrayList<Instruction>();
+ new_cond = new ArrayList<Instruction>();
+
+ if (next.get_car().has_init())
+ {
+ new_cond.add(next.get_car().get_init());
+ }
+
+ if (next.get_cdr().has_init())
+ {
+ new_value.add(next.get_cdr().get_init());
+ }
+
+ new_value.add
+ (
+ new SetValue(result_as_address, next.get_cdr().get_computation())
+ );
+
+ new_cond.add
+ (
+ IfElse.generate
+ (
+ compiler.registers(),
+ compiler.assembler(),
+ Operation.equals
+ (
+ next.get_car().get_computation(),
+ target_cc.get_computation()
+ ),
+ compiler.assembler().merge(new_value),
+ prev_branch
+ )
+ );
+
+ prev_branch = compiler.assembler().merge(new_cond);
+ }
+
+ init_instructions.add(prev_branch);
+
+ result_as_computation = result.get_value();
+ }
}
@Override