From 37e48fb6ece2a3d6fae7dee9588c191fe040d054 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Sat, 8 Aug 2020 18:25:44 +0200 Subject: More tests and fixes; anon ref/set/list. --- data/tests/collections.fate | 151 +++++++++++++++++++++ data/tests/comparisons.fate | 48 +++++++ data/tests/include/data_types.fate | 48 +++---- data/tests/locales.fate | 19 +++ data/tests/loops.fate | 149 ++++++++++++++++++++ data/tests/references.fate | 17 +++ .../fate/v1/lang/computation/SizeOperator.java | 97 +++++++++++++ .../fate/v1/lang/meta/ComputationVisitor.java | 3 + src/core/src/tonkadur/fate/v1/lang/type/Type.java | 2 +- src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 | 16 ++- src/core/src/tonkadur/fate/v1/parser/FateParser.g4 | 118 ++++++++++++++-- .../v1/compiler/fate/v1/ComputationCompiler.java | 17 +++ .../v1/compiler/fate/v1/InstructionCompiler.java | 61 +++++---- .../wyrd/v1/compiler/fate/v1/MacroManager.java | 7 +- .../wyrd/v1/compiler/util/BinarySearch.java | 28 ++++ .../wyrd/v1/compiler/util/CountOccurrences.java | 11 +- .../tonkadur/wyrd/v1/compiler/util/InsertAt.java | 6 +- .../wyrd/v1/compiler/util/RemoveAllOf.java | 88 ++++++------ .../wyrd/v1/compiler/util/ReverseList.java | 16 +-- 19 files changed, 778 insertions(+), 124 deletions(-) create mode 100644 data/tests/collections.fate create mode 100644 data/tests/comparisons.fate create mode 100644 data/tests/locales.fate create mode 100644 data/tests/loops.fate create mode 100644 data/tests/references.fate create mode 100644 src/core/src/tonkadur/fate/v1/lang/computation/SizeOperator.java diff --git a/data/tests/collections.fate b/data/tests/collections.fate new file mode 100644 index 0000000..0c804bd --- /dev/null +++ b/data/tests/collections.fate @@ -0,0 +1,151 @@ +(fate_version 1) + +(require include/data_types.fate) + +(declare_variable int_list int_list_a) +(declare_variable int_list int_list_b) +(declare_variable int_list int_list_c) + +(declare_variable int_set int_set_a) +(declare_variable int_set int_set_b) +(declare_variable int_set int_set_c) + +(assert (= 0 (size (var int_list_a))) FAILED: size list A) +(assert (= 0 (size (var int_set_a))) FAILED: size set A) + +(add_element 3 int_list_a) +(add_element 3 int_set_a) +(assert (= 1 (size (var int_list_a))) FAILED: size list B) +(assert (= 1 (size (var int_set_a))) FAILED: size set B) + +(add_element 3 int_list_a) +(add_element 3 int_set_a) +(assert (= 2 (size (var int_list_a))) FAILED: size list C) +(assert (= 1 (size (var int_set_a))) FAILED: size set C) + +(remove_one 3 int_list_a) +(remove_one 3 int_set_a) +(assert (= 1 (size (var int_list_a))) FAILED: size list D) +(assert (= 0 (size (var int_set_a))) FAILED: size set D) + +(remove_one 3 int_list_a) +(remove_one 3 int_set_a) +(assert (= 0 (size (var int_list_a))) FAILED: size list E) +(assert (= 0 (size (var int_set_a))) FAILED: size set E) + +(remove_one 3 int_list_a) +(remove_one 3 int_set_a) +(assert (= 0 (size (var int_list_a))) FAILED: size list F) +(assert (= 0 (size (var int_set_a))) FAILED: size set F) + +(add 1 int_list_a) +(add 2 int_list_a) +(add 3 int_list_a) +(add 4 int_list_a) +(add 5 int_list_a) +(add 6 int_list_a) +(add 7 int_list_a) +(add 8 int_list_a) +(add 9 int_list_a) +(add 10 int_list_a) + +(add 3 int_set_a) +(add 5 int_set_a) +(add 2 int_set_a) +(add 8 int_set_a) +(add 4 int_set_a) +(add 6 int_set_a) +(add 10 int_set_a) +(add 1 int_set_a) +(add 9 int_set_a) +(add 7 int_set_a) + +(add 3 int_set_a) +(add 5 int_set_a) +(add 2 int_set_a) +(add 8 int_set_a) +(add 4 int_set_a) +(add 6 int_set_a) +(add 10 int_set_a) +(add 1 int_set_a) +(add 9 int_set_a) +(add 7 int_set_a) + +(assert (= 10 (size (var int_list_a))) FAILED: size list G) +(assert (= 10 (size (var int_set_a))) FAILED: size set G) + +(add 1 int_list_a) +(add 2 int_list_a) +(add 3 int_list_a) +(add 4 int_list_a) +(add 5 int_list_a) +(add 6 int_list_a) +(add 7 int_list_a) +(add 8 int_list_a) +(add 9 int_list_a) +(add 10 int_list_a) + +(assert (= 20 (size (var int_list_a))) FAILED: size list H) + +(remove_all 4 int_list_a) + +(assert (= 18 (size (var int_list_a))) FAILED: size list I) + +(remove_element_at 0 int_list_a) +(assert (= 17 (size (var int_list_a))) FAILED: size list J) + +(remove_all 1 int_list_a) +(assert (= 16 (size (var int_list_a))) FAILED: size list K) + +(clear int_list_a) +(clear int_set_a) +(assert (= 0 (size (var int_list_a))) FAILED: size list L) +(assert (= 0 (size (var int_set_a))) FAILED: size set L) + +(add 1 int_list_a) +(add 2 int_list_a) +(add 3 int_list_a) +(add 4 int_list_a) +(add 5 int_list_a) +(add 6 int_list_a) +(add 7 int_list_a) +(add 8 int_list_a) +(add 9 int_list_a) +(add 10 int_list_a) +(add 10 int_list_a) +(add 9 int_list_a) +(add 8 int_list_a) +(add 7 int_list_a) +(add 6 int_list_a) +(add 5 int_list_a) +(add 4 int_list_a) +(add 3 int_list_a) +(add 2 int_list_a) +(add 1 int_list_a) + +(remove_at 0 int_list_a) +(reverse int_list_a) +(remove_at 0 int_list_a) + +(add 1 int_list_b) +(add 2 int_list_b) +(add 3 int_list_b) +(add 4 int_list_b) +(reverse int_list_b) + +(assert (= 18 (size (var int_list_a))) FAILED: size list M) + +(remove_all 1 int_list_a) + +(assert (= 18 (size (var int_list_a))) FAILED: size list N) + +(assert (= 2 (access int_list_a 0)) FAILED: size list M_BIS (access int_list_a 0)) +(assert (= 5 (access int_list_a 3)) FAILED: size list N_BIS (access int_list_a 3)) +(assert (= 2 (count 10 int_list_a)) FAILED: size list O) +(assert (= 0 (count 99 int_list_a)) FAILED: size list P) +;;(assert (= 0 (index_of 2 int_list_a)) FAILED: size list Q) +;;(assert (= 1 (index_of 3 int_list_a)) FAILED: size list R) +(assert (not (is_member 99 int_list_a)) FAILED: size list S) +(assert (is_member 10 int_list_a) FAILED: size list T) + +(end) diff --git a/data/tests/comparisons.fate b/data/tests/comparisons.fate new file mode 100644 index 0000000..68c5ed9 --- /dev/null +++ b/data/tests/comparisons.fate @@ -0,0 +1,48 @@ +(fate_version 1) + +(assert (exactly_one (< a b) (< b a)) FAILED: string comparison A) +(assert (exactly_one (< a aa) (< aa a)) FAILED: string comparison B) + +(assert + (exactly_one (< ( a aa ) ( aa a )) (< ( aa a ) ( a aa ))) + FAILED: string comparison C +) + +(declare_ptr_type int int_ptr) + +(def_var int i) +(def_var int_ptr i_ptr) + +(def_var int j) +(def_var int_ptr j_ptr) + +(set i_ptr (ptr i)) +(set j_ptr (ptr j)) + +(assert (exactly_one + (< (var i_ptr) (var j_ptr)) + (< (var j_ptr) (var i_ptr)) + ) + FAILED: string comparison C +) + +(def_dict test_dict_t + (int a) + (int b) +) + +(def_var test_dict_t td) + +(set i_ptr (ptr td.a)) +(set j_ptr (ptr td.b)) + + +(assert + (exactly_one + (< (var i_ptr) (var j_ptr)) + (< (var j_ptr) (var i_ptr)) + ) + FAILED: string comparison D +) + +(end) diff --git a/data/tests/include/data_types.fate b/data/tests/include/data_types.fate index 3205b69..abffedb 100644 --- a/data/tests/include/data_types.fate +++ b/data/tests/include/data_types.fate @@ -20,33 +20,33 @@ (typedef sub_string sub_sub_string) (typedef sub_string alt_sub_string) -(define_list_type boolean list_boolean) -(define_list_type sub_boolean list_sub_boolean) -(define_set_type boolean set_boolean) -(define_set_type sub_boolean set_sub_boolean) -(define_ref_type boolean ref_boolean) -(define_ref_type sub_boolean ref_sub_boolean) +(define_list_type boolean boolean_list) +(define_list_type sub_boolean sub_boolean_list) +(define_set_type boolean boolean_set) +(define_set_type sub_boolean sub_boolean_set) +(define_ref_type boolean boolean_ptr) +(define_ref_type sub_boolean sub_boolean_ptr) -(define_list_type int list_int) -(define_list_type sub_int list_sub_int) -(define_set_type int set_int) -(define_set_type sub_int set_sub_int) -(define_ref_type int ref_int) -(define_ref_type sub_int ref_sub_int) +(define_list_type int int_list) +(define_list_type sub_int sub_int_list) +(define_set_type int int_set) +(define_set_type sub_int sub_int_set) +(define_ref_type int int_ptr) +(define_ref_type sub_int sub_int_ptr) -(define_list_type float list_float) -(define_list_type sub_float list_sub_float) -(define_set_type float set_float) -(define_set_type sub_float set_sub_float) -(define_ref_type float ref_float) -(define_ref_type sub_float ref_sub_float) +(define_list_type float float_list) +(define_list_type sub_float sub_float_list) +(define_set_type float float_set) +(define_set_type sub_float sub_float_set) +(define_ref_type float float_ptr) +(define_ref_type sub_float sub_float_ptr) -(define_list_type string list_int) -(define_list_type sub_string list_sub_int) -(define_set_type string set_int) -(define_set_type sub_string set_sub_int) -(define_ref_type string ref_int) -(define_ref_type sub_string ref_sub_int) +(define_list_type string string_list) +(define_list_type sub_string sub_string_list) +(define_set_type string string_set) +(define_set_type sub_string sub_string_set) +(define_ref_type string string_ptr) +(define_ref_type sub_string sub_string_ptr) (define_dict_type simple_dict (boolean boolean) diff --git a/data/tests/locales.fate b/data/tests/locales.fate new file mode 100644 index 0000000..21244d8 --- /dev/null +++ b/data/tests/locales.fate @@ -0,0 +1,19 @@ +(fate_version 1) + +大野!私はこれらの言語を話せません! + +(declare_variable int 変数) + +(for (set 変数 0) (< (var 変数) 10) (set 変数 (+ (var 変数) 1)) + 私は数を使って数えることができます:(var 変数) +) + +أوه لا! أنا لا أتحدث أيًا من هذه اللغات! + +(declare_variable int ﻢﺘﻐﻳﺭ) + +(for (set ﻢﺘﻐﻳﺭ 0) (< (var ﻢﺘﻐﻳﺭ) 10) (set ﻢﺘﻐﻳﺭ (+ (var ﻢﺘﻐﻳﺭ) 1)) + لا يهم. يمكنني فقط استخدام الأرقام للعد حتى: (var ﻢﺘﻐﻳﺭ) +) + +(end) diff --git a/data/tests/loops.fate b/data/tests/loops.fate new file mode 100644 index 0000000..db7f212 --- /dev/null +++ b/data/tests/loops.fate @@ -0,0 +1,149 @@ +(fate_version 1) + +(require include/data_types.fate) + +(declare_variable int i) +(declare_variable int test_val) +(declare_variable int test_val2) + +(set test_val 0) +(set test_val2 0) + +(for (set i 0) (< (var i) 10) (set i (+ (var i) 1)) + (set test_val (+ (var test_val) 1)) + (set test_val2 (+ (var test_val2) 1)) +) + +(assert (= (var test_val) 10) FAILED: for loop A) +(assert (= (var test_val2) 10) FAILED: for loop B) +(assert (= (var i) 10) FAILED: for loop C) + +(set i 0) + +(set test_val 0) +(set test_val2 0) + +(while (< (var i) 10) + (set test_val (+ (var test_val) 1)) + (set test_val2 (+ (var test_val2) 1)) + (set i (+ (var i) 1)) +) + +(assert (= (var test_val) 10) FAILED: while loop A) +(assert (= (var test_val2) 10) FAILED: while loop B) +(assert (= (var i) 10) FAILED: while loop C) + +(set i 0) + +(set test_val 0) +(set test_val2 0) + +(do_while (< (var i) 10) + (set test_val (+ (var test_val) 1)) + (set test_val2 (+ (var test_val2) 1)) + (set i (+ (var i) 1)) +) + +(assert (= (var test_val) 10) FAILED: do while loop A) +(assert (= (var test_val2) 10) FAILED: do while loop B) +(assert (= (var i) 10) FAILED: do while loop C) + +(set i 0) +(set test_val 0) +(set test_val2 0) + +(do_while (false) + (set test_val (+ (var test_val) 1)) + (set test_val2 (+ (var test_val2) 1)) + (set i (+ (var i) 1)) +) + +(assert (= (var test_val) 1) FAILED: do while loop D) +(assert (= (var test_val2) 1) FAILED: do while loop E) +(assert (= (var i) 1) FAILED: do while loop F) + +(declare_variable int_list int_list_a) +(declare_variable int_list int_list_b) + +(for (set i 0) (< (var i) 10) (set i (+ (var i) 1)) + (add_element 1 int_list_a) + (add_element 1 int_list_b) +) + +(set test_val 0) +(set test_val2 0) + +(foreach int_list_a a + (set test_val (+ (var test_val) (param a))) + (set test_val2 (+ (var test_val2) (param a))) +) +(assert (= (var test_val) 10) FAILED: for each loop A) +(assert (= (var test_val2) 10) FAILED: for each loop B) + + +(set test_val 0) +(set test_val2 0) + +(for (set i 0) (< (var i) 10) (set i (+ (var i) 1)) + (set test_val (+ (var test_val) 1)) + (break) + (set test_val2 (+ (var test_val2) 1)) +) +(assert (= (var test_val) 1) FAILED: break loop A) +(assert (= (var test_val2) 0) FAILED: for each loop B) + +(set i 0) + +(set test_val 0) +(set test_val2 0) + +(while (< (var i) 10) + (set test_val (+ (var test_val) 1)) + (break) + (set test_val2 (+ (var test_val2) 1)) + (set i (+ (var i) 1)) +) +(assert (= (var test_val) 1) FAILED: break while loop A) +(assert (= (var test_val2) 0) FAILED: break while loop B) +(assert (= (var i) 0) FAILED: break while loop C) + +(set i 0) + +(set test_val 0) +(set test_val2 0) + +(do_while (< (var i) 10) + (set test_val (+ (var test_val) 1)) + (break) + (set test_val2 (+ (var test_val2) 1)) + (set i (+ (var i) 1)) +) +(assert (= (var test_val) 1) FAILED: break do while loop A) +(assert (= (var test_val2) 0) FAILED: break do while loop B) +(assert (= (var i) 0) FAILED: break do while loop C) + +(set test_val 0) + +(foreach int_list_a a + (set test_val (+ (var test_val) (param a))) + (break) + (set test_val2 (+ (var test_val2) (param a))) +) +(assert (= (var test_val) 1) FAILED: break foreach loop A) +(assert (= (var test_val2) 0) FAILED: break foreach loop B) + +(clear int_list_b) + +(foreach int_list_a a + (add_element (param a) int_list_b) + (for (set i 0) (< (var i) 1) (set i (+ (var i) 1)) + (add_element (var i) int_list_b) + ) + (do_while (< (var i) 10) + (break) + ) +) + +(assert (= (size int_list_b) (* 2 (size int_list_a))) FAILED: inner break) + +(end) diff --git a/data/tests/references.fate b/data/tests/references.fate new file mode 100644 index 0000000..cc30154 --- /dev/null +++ b/data/tests/references.fate @@ -0,0 +1,17 @@ +(fate_version 1) + +(require include/data_types.fate) + +(def_var (ptr int) alloc_i) + +(set alloc_i (new int)) +(set (at alloc_i) 2) + +(assert (= (at (var alloc_i)) 2) FAILED: set at A) + +(def_var (ptr (ptr int)) i_ptr_ptr) +(set i_ptr_ptr (ptr alloc_i)) +(set (at (at i_ptr_ptr)) 4) +(assert (= (at (var alloc_i)) 4) FAILED: set at B) + +(end) diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/SizeOperator.java b/src/core/src/tonkadur/fate/v1/lang/computation/SizeOperator.java new file mode 100644 index 0000000..9882b31 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/SizeOperator.java @@ -0,0 +1,97 @@ +package tonkadur.fate.v1.lang.computation; + +import tonkadur.error.ErrorManager; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; + +public class SizeOperator extends Computation +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final Computation collection; + + /***************************************************************************/ + /**** PROTECTED ************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + protected SizeOperator + ( + final Origin origin, + final Computation collection + ) + { + super(origin, Type.INT); + + this.collection = collection; + } + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + public static SizeOperator build + ( + final Origin origin, + final Computation collection + ) + throws InvalidTypeException + { + + if (!(collection.get_type() instanceof CollectionType)) + { + ErrorManager.handle + ( + new InvalidTypeException + ( + collection.get_origin(), + collection.get_type(), + Type.COLLECTION_TYPES + ) + ); + } + + return new SizeOperator(origin, collection); + } + + /**** Accessors ************************************************************/ + @Override + public void get_visited_by (final ComputationVisitor cv) + throws Throwable + { + cv.visit_size_operator(this); + } + + public Computation get_collection () + { + return collection; + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb = new StringBuilder(); + + sb.append(origin.toString()); + sb.append("(SizeOperator"); + sb.append(System.lineSeparator()); + sb.append(System.lineSeparator()); + + sb.append("collection:"); + sb.append(System.lineSeparator()); + sb.append(collection.toString()); + + sb.append(")"); + + return sb.toString(); + } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java b/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java index 203289e..d2c4ff0 100644 --- a/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java +++ b/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java @@ -40,6 +40,9 @@ public interface ComputationVisitor public void visit_index_of_operator (final IndexOfOperator n) throws Throwable; + public void visit_size_operator (final SizeOperator n) + throws Throwable; + public void visit_macro_value_call (final MacroValueCall n) throws Throwable; diff --git a/src/core/src/tonkadur/fate/v1/lang/type/Type.java b/src/core/src/tonkadur/fate/v1/lang/type/Type.java index 71bf900..f848047 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/Type.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/Type.java @@ -52,7 +52,7 @@ public class Type extends DeclaredEntity INT = new Type(base, null, "int"); LIST = new Type(base, null, "list"); REF = new Type(base, null, "ref"); - RICH_TEXT = new Type(base, null, "rich text"); + RICH_TEXT = new Type(base, null, "text"); SET = new Type(base, null, "set"); STRING = new Type(base, null, "string"); diff --git a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 index 2759c92..2690cdf 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 @@ -16,7 +16,7 @@ R_PAREN: ')'; ABS_KW: L_PAREN 'abs'('olute'?) SEP+; ACCESS_KW: L_PAREN 'access' SEP+; -ADD_KW: L_PAREN 'add' SEP+; +ADD_KW: L_PAREN 'add'(US'element')? SEP+; AND_KW: L_PAREN ('and'|'/\\') SEP+; ASSERT_KW: L_PAREN 'assert' SEP+; AT_KW: L_PAREN 'at' SEP+; @@ -27,10 +27,10 @@ COND_KW: L_PAREN 'cond' SEP+; COUNT_KW: L_PAREN 'count' SEP+; DECLARE_ALIAS_TYPE_KW: L_PAREN ((('declare'|'define'|'def')US('sub'US)?'type')|'typedef') SEP+; -DECLARE_DICT_TYPE_KW: L_PAREN ('declare'|'define'|'def')US'dict'(US'type')? SEP+; +DECLARE_DICT_TYPE_KW: L_PAREN ('declare'|'define'|'def')US('dict'|'struct')(US'type')? SEP+; DECLARE_EVENT_TYPE_KW: L_PAREN ('declare'|'define'|'def')US'event'(US'type')? SEP+; DECLARE_LIST_TYPE_KW: L_PAREN ('declare'|'define'|'def')US'list'(US'type')? SEP+; -DECLARE_REF_TYPE_KW: L_PAREN ('declare'|'define'|'def')US'ref'(US'type')? SEP+; +DECLARE_REF_TYPE_KW: L_PAREN ('declare'|'define'|'def')US(('ref'('erence'?))|'ptr'|'pointer')(US'type')? SEP+; DECLARE_SET_TYPE_KW: L_PAREN ('declare'|'define'|'def')US'set'(US'type')? SEP+; DECLARE_TEXT_EFFECT_KW: L_PAREN ('declare'|'define'|'def')US'text'US'effect' SEP+; DECLARE_VARIABLE_KW: L_PAREN ('declare'|'define'|'def')US'var'('iable')? SEP+; @@ -71,22 +71,26 @@ MODULO_KW: L_PAREN ('modulo'|'%'|'mod') SEP+; NEWLINE_KW: L_PAREN 'newline)'; NEW_KW: L_PAREN ('new'|'reserve'|'create') SEP+; NOT_KW: L_PAREN ('not'|'~'|'!') SEP+; -ONE_IN_KW: L_PAREN 'one'US'in' SEP+; +ONE_IN_KW: L_PAREN ('exactly'US)?'one'(US'in')? SEP+; OR_KW: L_PAREN ('or'|'\\/') SEP+; +RICH_TEXT_KW: L_PAREN (('rich'US)?'text') SEP+; PARAMETER_KW: L_PAREN ('param'|'parameter'|'par') SEP+; PLAYER_CHOICE_KW: L_PAREN ('choice'|'user'US'choice'|'player'US'choice') SEP+; PLUS_KW: L_PAREN ('plus'|'+') SEP+; POWER_KW: L_PAREN ('power'|'^'|'**'|'pow') SEP+; RANDOM_KW: L_PAREN ('random'|'rand'|'rnd') SEP+; -REF_KW: L_PAREN 'ref' SEP+; +REF_KW: L_PAREN (('ref'('erence'?))|'ptr') SEP+; REMOVE_ALL_KW: L_PAREN 'remove'US'all' SEP+; +REVERSE_KW: L_PAREN 'reverse'(US'list')? SEP+; REMOVE_ONE_KW: L_PAREN 'remove'US'one' SEP+; -REMOVE_AT_KW: L_PAREN ('remove'US'at'|'rm'|'del'|'delete') SEP+; +REMOVE_AT_KW: L_PAREN ('remove'US('elem'('ent')?US)?'at'|'rm'|'del'|'delete') SEP+; REQUIRE_EXTENSION_KW: L_PAREN 'require'US'extension' SEP+; REQUIRE_KW: L_PAREN 'require' SEP+; SEQUENCE_KW: L_PAREN 'seq'('uence')? SEP+; SET_FIELDS_KW: L_PAREN 'set'US'fields' SEP+; SET_KW: L_PAREN 'set' SEP+; +LIST_KW: L_PAREN 'list' SEP+; +SIZE_KW: L_PAREN 'size' SEP+; SWITCH_KW: L_PAREN 'switch' SEP+; TIMES_KW: L_PAREN ('times'|'*') SEP+; TRUE_KW: L_PAREN 'true)'; diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index ecf0c0d..22915e8 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -675,6 +675,20 @@ returns [Instruction result] ); } + | REVERSE_KW value_reference WS* R_PAREN + { + $result = + ReverseList.build + ( + CONTEXT.get_origin_at + ( + ($REVERSE_KW.getLine()), + ($REVERSE_KW.getCharPositionInLine()) + ), + ($value_reference.result) + ); + } + | SET_KW value_reference WS+ value WS* R_PAREN { $result = @@ -1333,7 +1347,7 @@ text returns [RichTextNode result]: sentence { - $result = ($sentence.result); + $result = ValueToRichText.build(($sentence.result)); } | ENABLE_TEXT_EFFECT_KW WORD WS+ paragraph WS* R_PAREN @@ -1431,7 +1445,7 @@ catch [final Throwable e] } sentence -returns [RichTextNode result] +returns [Computation result] @init { final StringBuilder string_builder = new StringBuilder(); @@ -1451,17 +1465,14 @@ returns [RichTextNode result] )* { $result = - ValueToRichText.build + Constant.build_string ( - Constant.build_string + CONTEXT.get_origin_at ( - CONTEXT.get_origin_at - ( - ($first_word.getLine()), - ($first_word.getCharPositionInLine()) - ), - string_builder.toString() - ) + ($first_word.getLine()), + ($first_word.getCharPositionInLine()) + ), + string_builder.toString() ); } ; @@ -1493,6 +1504,68 @@ returns [Type result] ($WORD.text) ); } + + | REF_KW type WS* R_PAREN + { + final Origin start_origin; + + start_origin = + CONTEXT.get_origin_at + ( + ($REF_KW.getLine()), + ($REF_KW.getCharPositionInLine()) + ); + + $result = + new RefType + ( + start_origin, + ($type.result), + ("anonymous (" + ($type.result).get_name() + ") ref type") + ); + } + + | SET_KW type WS* R_PAREN + { + final Origin start_origin; + + start_origin = + CONTEXT.get_origin_at + ( + ($SET_KW.getLine()), + ($SET_KW.getCharPositionInLine()) + ); + + $result = + CollectionType.build + ( + start_origin, + ($type.result), + true, + ("anonymous (" + ($type.result).get_name() + ") set type") + ); + } + + | LIST_KW type WS* R_PAREN + { + final Origin start_origin; + + start_origin = + CONTEXT.get_origin_at + ( + ($LIST_KW.getLine()), + ($LIST_KW.getCharPositionInLine()) + ); + + $result = + CollectionType.build + ( + start_origin, + ($type.result), + false, + ("anonymous (" + ($type.result).get_name() + ") list type") + ); + } ; catch [final Throwable e] { @@ -1849,6 +1922,20 @@ returns [Computation result]: ); } + | SIZE_KW value_reference WS* R_PAREN + { + $result = + SizeOperator.build + ( + CONTEXT.get_origin_at + ( + ($SIZE_KW.getLine()), + ($SIZE_KW.getCharPositionInLine()) + ), + ($value_reference.result) + ); + } + | NEW_KW WORD WS* R_PAREN { final Origin origin; @@ -2097,7 +2184,12 @@ returns [Computation result] /* TODO: temporarily disable an compiler error category */ } - | L_PAREN WS+ paragraph WS* R_PAREN + | L_PAREN WS+ sentence WS* R_PAREN + { + $result = ($sentence.result); + } + + | RICH_TEXT_KW paragraph WS* R_PAREN { $result = ($paragraph.result); } @@ -2403,7 +2495,7 @@ returns [Reference result] ); } - | ACCESS_KW value_reference value R_PAREN + | ACCESS_KW value_reference WS+ value R_PAREN { $result = Access.build 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 c9740f8..ae89702 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 @@ -1102,6 +1102,23 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor System.err.println("[P] Unknown Fate operator '" + fate_op_name+ "'."); } } + @Override + public void visit_size_operator + ( + final tonkadur.fate.v1.lang.computation.SizeOperator n + ) + throws Throwable + { + final ComputationCompiler cc; + + cc = new ComputationCompiler(compiler); + + n.get_collection().get_visited_by(cc); + + result_as_computation = new Size(cc.get_ref()); + + cc.release_variables(); + } @Override public void visit_index_of_operator diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java index d05a280..3ae8a3a 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java @@ -767,6 +767,17 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor final Computation value_of_index; final Type member_type; + /* + * (declare_variable int index) + * (declare_variable int collection_size) + * + * (set index 0) + * (set collection_size (size collection)) + * + * (declare_variable current_value) + * + * ... + */ cc = new ComputationCompiler(compiler); new_body = new ArrayList(); @@ -801,20 +812,6 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor current_value ); - for - ( - final tonkadur.fate.v1.lang.meta.Instruction fate_instr: n.get_body() - ) - { - final InstructionCompiler ic; - - ic = new InstructionCompiler(compiler); - - fate_instr.get_visited_by(ic); - - new_body.add(ic.get_result()); - } - new_body.add ( new SetValue @@ -832,6 +829,20 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ) ); + for + ( + final tonkadur.fate.v1.lang.meta.Instruction fate_instr: n.get_body() + ) + { + final InstructionCompiler ic; + + ic = new InstructionCompiler(compiler); + + fate_instr.get_visited_by(ic); + + new_body.add(ic.get_result()); + } + new_body.add ( new SetValue(index, Operation.plus(Constant.ONE, value_of_index)) @@ -1414,16 +1425,17 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor elem_cc = new ComputationCompiler(compiler); collection_cc = new ComputationCompiler(compiler); + collection_size = compiler.anonymous_variables().reserve(Type.INT); + + n.get_element().get_visited_by(elem_cc); + n.get_collection().get_visited_by(collection_cc); + elem = compiler.anonymous_variables().reserve ( elem_cc.get_computation().get_type() ); - collection_size = compiler.anonymous_variables().reserve(Type.INT); - - n.get_element().get_visited_by(elem_cc); - n.get_collection().get_visited_by(collection_cc); if (elem_cc.has_init()) { @@ -1567,12 +1579,6 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor elem_cc = new ComputationCompiler(compiler); collection_cc = new ComputationCompiler(compiler); - elem = - compiler.anonymous_variables().reserve - ( - elem_cc.get_computation().get_type() - ); - collection_size = compiler.anonymous_variables().reserve(Type.INT); found = compiler.anonymous_variables().reserve(Type.BOOLEAN); index = compiler.anonymous_variables().reserve(Type.INT); @@ -1580,6 +1586,13 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor n.get_element().get_visited_by(elem_cc); n.get_collection().get_visited_by(collection_cc); + elem = + compiler.anonymous_variables().reserve + ( + elem_cc.get_computation().get_type() + ); + + if (elem_cc.has_init()) { result.add(elem_cc.get_init()); diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/MacroManager.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/MacroManager.java index fe2ab67..436c221 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/MacroManager.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/MacroManager.java @@ -27,6 +27,8 @@ public class MacroManager return; } + + wild_parameters.put(name, ref); } public void remove_wild_parameter (final String name) @@ -74,7 +76,10 @@ public class MacroManager if (result == null) { - result = context_stack.peek().get(parameter); + if (!context_stack.isEmpty()) + { + result = context_stack.peek().get(parameter); + } if (result == null) { diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/BinarySearch.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/BinarySearch.java index f2c8572..741b4e9 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/BinarySearch.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/BinarySearch.java @@ -37,6 +37,7 @@ public class BinarySearch * (declare_variable .midval) * * (set result_found false) + * (set result_index 0) * (set .bot 0) * (set .top (- collection_size 1)) * @@ -104,6 +105,7 @@ public class BinarySearch value_of_top = new ValueOf(top); value_of_midval = new ValueOf(midval); + result.add(new SetValue(result_index, Constant.ZERO)); result.add(new SetValue(result_was_found, Constant.FALSE)); result.add(new SetValue(bot, Constant.ZERO)); result.add @@ -230,6 +232,32 @@ public class BinarySearch ) ); + /* Without this, you'll replace the value and move it to the right, + * regardless of where you 'target' stands in relation to it. + */ + result.add + ( + If.generate + ( + anonymous_variables, + assembler, + Operation.and + ( + Operation.and + ( + Operation.not(new ValueOf(result_was_found)), + Operation.greater_than(target, value_of_midval) + ), + Operation.greater_than(collection_size, Constant.ZERO) + ), + new SetValue + ( + result_index, + Operation.plus(value_of_result_index, Constant.ONE) + ) + ) + ); + anonymous_variables.release(bot); anonymous_variables.release(top); anonymous_variables.release(midval); diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/CountOccurrences.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/CountOccurrences.java index 5987c61..a6e817a 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/CountOccurrences.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/CountOccurrences.java @@ -70,7 +70,10 @@ public class CountOccurrences result.add(new SetValue(count, Constant.ZERO)); result.add(new SetValue(index, collection_size)); - while_body.add(new SetValue(index, Operation.minus(index, Constant.ONE))); + while_body.add + ( + new SetValue(index, Operation.minus(value_of_index, Constant.ONE)) + ); while_body.add ( If.generate @@ -90,7 +93,11 @@ public class CountOccurrences ), target ), - new SetValue(count, Operation.plus(count, Constant.ONE)) + new SetValue + ( + count, + Operation.plus(new ValueOf(count), Constant.ONE) + ) ) ); diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/InsertAt.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/InsertAt.java index 48debc0..c634685 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/InsertAt.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/InsertAt.java @@ -35,7 +35,7 @@ public class InsertAt * (set .end collection_size) * * .index .end) + * (< .index .end) * * (set .prev (- (val .collection_size) 1)) * (set collection[.end] (val collection[.prev])) @@ -107,7 +107,7 @@ public class InsertAt while_body.add(new SetValue(end, value_of_prev)); /* - * (while (> .index .end) + * (while (< .index .end) * (set .prev (- (val .collection_size) 1)) * (set collection[.end] (val collection[.prev])) * (set .end (val .prev)) @@ -119,7 +119,7 @@ public class InsertAt ( anonymous_variables, assembler, - Operation.greater_than(value_of_index, value_of_end), + Operation.less_than(value_of_index, value_of_end), assembler.merge(while_body) ) ); diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAllOf.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAllOf.java index bd30ff1..23bea3a 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAllOf.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/RemoveAllOf.java @@ -39,18 +39,18 @@ public class RemoveAllOf * * * (var .found) 0) - * - * (set - * collection[.index] - * (var collection[(+ (var .index) (var .found))]) - * ) + * (set + * collection[.index] + * (var collection[(+ (var .index) (var .found))]) + * ) * > - * (set index ((val .index) + 1)) + * (set index (+ (val .index) 1)) * ) * * (while (> (var .found) 0) @@ -69,8 +69,7 @@ public class RemoveAllOf ) { final List result; - final List while_body0, while_body1, if_false_body; - final List if_false_true_body; + final List while_body0, while_body1, if_body; final Type element_type; final Ref index, found, end; final Computation value_of_index, value_of_found, value_of_end; @@ -81,8 +80,7 @@ public class RemoveAllOf while_body0 = new ArrayList(); while_body1 = new ArrayList(); - if_false_body = new ArrayList(); - if_false_true_body = new ArrayList(); + if_body = new ArrayList(); element_type = element.get_type(); @@ -122,50 +120,35 @@ public class RemoveAllOf * (set .end (- (var .end) 1)) * ) */ - if_false_true_body.add + if_body.add ( new SetValue(found, Operation.plus(value_of_found, Constant.ONE)) ); - if_false_true_body.add + if_body.add ( - new SetValue(found, Operation.minus(value_of_found, Constant.ONE)) + new SetValue(end, Operation.minus(value_of_end, Constant.ONE)) ); - if_false_body.add + while_body0.add ( + /* (var .found) 0) + * (set + * collection[.index] + * (var collection[(+ (var .index) (var .found))]) + * ) + * > */ - if_false_body.add - ( - new SetValue - ( - collection_at_index, - new RelativeRef - ( - collection, - new Cast - ( - Operation.plus(value_of_index, value_of_found), - Type.STRING - ), - element_type - ) - ) - ); - while_body0.add ( If.generate @@ -173,10 +156,27 @@ public class RemoveAllOf anonymous_variables, assembler, value_of_found_greater_than_0, - assembler.merge(if_false_body) + new SetValue + ( + collection_at_index, + new ValueOf + ( + new RelativeRef + ( + collection, + new Cast + ( + Operation.plus(value_of_index, value_of_found), + Type.STRING + ), + element_type + ) + ) + ) ) ); + /* (set index (+ (val .index) 1)) */ while_body0.add ( new SetValue(index, Operation.plus(value_of_index, Constant.ONE)) @@ -188,11 +188,13 @@ public class RemoveAllOf ( anonymous_variables, assembler, + /* (var .found) 0) */ value_of_found_greater_than_0, assembler.merge(while_body1) ) diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/util/ReverseList.java b/src/core/src/tonkadur/wyrd/v1/compiler/util/ReverseList.java index 226e86a..a393fc8 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/util/ReverseList.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/util/ReverseList.java @@ -39,8 +39,8 @@ public class ReverseList * (set .buffer collection[.top]) * (set collection[.top] collection[.bot]) * (set collection[.bot] .buffer) - * (set .bot (+ 1 (var .bot))) - * (set .top (- 1 (var .top))) + * (set .bot (+ (var .bot) 1)) + * (set .top (- (var .top) 1)) * ) */ public static Instruction generate @@ -111,16 +111,16 @@ public class ReverseList new SetValue(collection_at_bot, new ValueOf(buffer)) ); - /* (set .bot (+ 1 (var .bot))) */ + /* (set .bot (+ (var .bot) 1)) */ while_body.add ( - new SetValue(bot, Operation.plus(Constant.ONE, value_of_bot)) + new SetValue(bot, Operation.plus(value_of_bot, Constant.ONE)) ); - /* (set .top (- 1 (var .top))) */ + /* (set .top (- (var .top) 1)) */ while_body.add ( - new SetValue(top, Operation.minus(Constant.ONE, value_of_top)) + new SetValue(top, Operation.minus(value_of_top, Constant.ONE)) ); /* @@ -128,8 +128,8 @@ public class ReverseList * (set .buffer collection[.top]) * (set collection[.top] collection[.bot]) * (set collection[.bot] .buffer) - * (set .bot (+ 1 (var .bot))) - * (set .top (- 1 (var .top))) + * (set .bot (+ (var .bot) 1)) + * (set .top (- (var .top) 1)) * ) */ result.add -- cgit v1.2.3-70-g09d2