From 87b9279f325872615cb11adfad7dbf9c2d35b712 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Thu, 13 Aug 2020 20:45:34 +0200 Subject: Adding most of the Fate stuff. --- .../v1/error/DuplicateLocalVariableException.java | 51 +++ .../fate/v1/error/NotAValueMacroException.java | 53 --- .../fate/v1/error/NotInAMacroException.java | 41 -- .../fate/v1/error/UnknownParameterException.java | 68 ---- .../v1/error/UnknownVariableScopeException.java | 55 --- src/core/src/tonkadur/fate/v1/lang/Macro.java | 167 -------- src/core/src/tonkadur/fate/v1/lang/Sequence.java | 10 +- src/core/src/tonkadur/fate/v1/lang/Variable.java | 21 +- .../src/tonkadur/fate/v1/lang/VariableScope.java | 88 ----- src/core/src/tonkadur/fate/v1/lang/World.java | 29 +- .../fate/v1/lang/computation/LambdaEvaluation.java | 204 ++++++++++ .../fate/v1/lang/computation/LambdaExpression.java | 83 ++++ .../fate/v1/lang/computation/MacroValueCall.java | 193 --------- .../v1/lang/computation/ParameterReference.java | 53 --- .../tonkadur/fate/v1/lang/instruction/Done.java | 37 ++ .../fate/v1/lang/instruction/LocalVariable.java | 57 +++ .../fate/v1/lang/instruction/MacroCall.java | 175 --------- .../fate/v1/lang/instruction/SequenceCall.java | 24 +- .../fate/v1/lang/instruction/SequenceJump.java | 73 ++++ .../fate/v1/lang/meta/ComputationVisitor.java | 8 +- .../fate/v1/lang/meta/InstructionVisitor.java | 12 +- .../tonkadur/fate/v1/lang/meta/TypedEntryList.java | 111 ------ .../tonkadur/fate/v1/lang/meta/VariableList.java | 72 ++++ .../src/tonkadur/fate/v1/lang/type/LambdaType.java | 163 ++++++++ src/core/src/tonkadur/fate/v1/lang/type/Type.java | 3 + src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 | 12 +- src/core/src/tonkadur/fate/v1/parser/FateParser.g4 | 157 ++------ .../wyrd/v1/compiler/fate/v1/Compiler.java | 36 +- .../v1/compiler/fate/v1/ComputationCompiler.java | 389 +++++++++--------- .../v1/compiler/fate/v1/InstructionCompiler.java | 437 ++++++++++----------- .../wyrd/v1/compiler/fate/v1/MacroManager.java | 24 +- .../wyrd/v1/compiler/fate/v1/TypeCompiler.java | 8 +- .../v1/compiler/util/AnonymousVariableManager.java | 147 ------- .../wyrd/v1/compiler/util/BinarySearch.java | 65 ++- .../src/tonkadur/wyrd/v1/compiler/util/Clear.java | 31 +- .../wyrd/v1/compiler/util/CountOccurrences.java | 37 +- .../wyrd/v1/compiler/util/FunctionContext.java | 9 - .../src/tonkadur/wyrd/v1/compiler/util/If.java | 4 +- .../src/tonkadur/wyrd/v1/compiler/util/IfElse.java | 4 +- .../tonkadur/wyrd/v1/compiler/util/InsertAt.java | 50 +-- .../wyrd/v1/compiler/util/IterativeSearch.java | 20 +- .../src/tonkadur/wyrd/v1/compiler/util/NOP.java | 4 +- .../wyrd/v1/compiler/util/RemoveAllOf.java | 85 ++-- .../tonkadur/wyrd/v1/compiler/util/RemoveAt.java | 52 +-- .../wyrd/v1/compiler/util/ReverseList.java | 71 ++-- .../src/tonkadur/wyrd/v1/compiler/util/While.java | 6 +- .../compiler/util/registers/RegisterContext.java | 140 ++++--- .../compiler/util/registers/RegisterManager.java | 63 ++- .../util/registers/StackableRegisterContext.java | 183 ++++----- src/core/src/tonkadur/wyrd/v1/lang/Register.java | 68 ++++ src/core/src/tonkadur/wyrd/v1/lang/Variable.java | 42 -- src/core/src/tonkadur/wyrd/v1/lang/World.java | 19 +- .../tonkadur/wyrd/v1/lang/computation/Address.java | 61 +++ .../src/tonkadur/wyrd/v1/lang/computation/Ref.java | 61 --- .../wyrd/v1/lang/computation/RelativeAddress.java | 60 +++ .../wyrd/v1/lang/computation/RelativeRef.java | 60 --- .../tonkadur/wyrd/v1/lang/computation/Size.java | 6 +- .../tonkadur/wyrd/v1/lang/computation/ValueOf.java | 2 +- .../tonkadur/wyrd/v1/lang/instruction/Remove.java | 14 +- .../wyrd/v1/lang/instruction/SetValue.java | 14 +- .../wyrd/v1/lang/meta/ComputationVisitor.java | 4 +- 61 files changed, 1935 insertions(+), 2361 deletions(-) create mode 100644 src/core/src/tonkadur/fate/v1/error/DuplicateLocalVariableException.java delete mode 100644 src/core/src/tonkadur/fate/v1/error/NotAValueMacroException.java delete mode 100644 src/core/src/tonkadur/fate/v1/error/NotInAMacroException.java delete mode 100644 src/core/src/tonkadur/fate/v1/error/UnknownParameterException.java delete mode 100644 src/core/src/tonkadur/fate/v1/error/UnknownVariableScopeException.java delete mode 100644 src/core/src/tonkadur/fate/v1/lang/Macro.java delete mode 100644 src/core/src/tonkadur/fate/v1/lang/VariableScope.java create mode 100644 src/core/src/tonkadur/fate/v1/lang/computation/LambdaEvaluation.java create mode 100644 src/core/src/tonkadur/fate/v1/lang/computation/LambdaExpression.java delete mode 100644 src/core/src/tonkadur/fate/v1/lang/computation/MacroValueCall.java delete mode 100644 src/core/src/tonkadur/fate/v1/lang/computation/ParameterReference.java create mode 100644 src/core/src/tonkadur/fate/v1/lang/instruction/Done.java create mode 100644 src/core/src/tonkadur/fate/v1/lang/instruction/LocalVariable.java delete mode 100644 src/core/src/tonkadur/fate/v1/lang/instruction/MacroCall.java create mode 100644 src/core/src/tonkadur/fate/v1/lang/instruction/SequenceJump.java delete mode 100644 src/core/src/tonkadur/fate/v1/lang/meta/TypedEntryList.java create mode 100644 src/core/src/tonkadur/fate/v1/lang/meta/VariableList.java create mode 100644 src/core/src/tonkadur/fate/v1/lang/type/LambdaType.java delete mode 100644 src/core/src/tonkadur/wyrd/v1/compiler/util/AnonymousVariableManager.java create mode 100644 src/core/src/tonkadur/wyrd/v1/lang/Register.java delete mode 100644 src/core/src/tonkadur/wyrd/v1/lang/Variable.java create mode 100644 src/core/src/tonkadur/wyrd/v1/lang/computation/Address.java delete mode 100644 src/core/src/tonkadur/wyrd/v1/lang/computation/Ref.java create mode 100644 src/core/src/tonkadur/wyrd/v1/lang/computation/RelativeAddress.java delete mode 100644 src/core/src/tonkadur/wyrd/v1/lang/computation/RelativeRef.java diff --git a/src/core/src/tonkadur/fate/v1/error/DuplicateLocalVariableException.java b/src/core/src/tonkadur/fate/v1/error/DuplicateLocalVariableException.java new file mode 100644 index 0000000..fb168ef --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/error/DuplicateLocalVariableException.java @@ -0,0 +1,51 @@ +package tonkadur.fate.v1.error; + +import tonkadur.error.ErrorLevel; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.fate.v1.lang.Variable; + +public class DuplicateLocalVariableException extends ParsingError +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final Variable original_var; + protected final Variable new_var; + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + public DuplicateLocalVariableException + ( + final Variable original_var, + final Variable new_var + ) + { + super(ErrorLevel.ERROR, ErrorCategory.INVALID_USE, new_var.get_origin()); + + this.original_var = original_var; + this.new_var = new_var; + } + + @Override + public String toString () + { + final StringBuilder sb = new StringBuilder(); + + sb.append(origin.toString()); + sb.append(" "); + sb.append(error_category.toString()); + sb.append(System.lineSeparator()); + + sb.append("Local variable name '"); + sb.append(original_var.get_name()); + sb.append("' already in use. It was originally reserved at "); + sb.append(original_var.get_origin().get_location().toString()); + sb.append("."); + + return sb.toString(); + } +} diff --git a/src/core/src/tonkadur/fate/v1/error/NotAValueMacroException.java b/src/core/src/tonkadur/fate/v1/error/NotAValueMacroException.java deleted file mode 100644 index b235ad9..0000000 --- a/src/core/src/tonkadur/fate/v1/error/NotAValueMacroException.java +++ /dev/null @@ -1,53 +0,0 @@ -package tonkadur.fate.v1.error; - -import tonkadur.error.ErrorLevel; - -import tonkadur.parser.Origin; -import tonkadur.parser.ParsingError; - -public class NotAValueMacroException extends ParsingError -{ - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final String macro_name; - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - public NotAValueMacroException (final Origin origin, final String macro_name) - { - super - ( - ErrorLevel.ERROR, - ErrorCategory.INVALID_USE, - origin - ); - - this.macro_name = macro_name; - } - - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append(origin.toString()); - sb.append(" "); - sb.append(error_category.toString()); - sb.append(System.lineSeparator()); - - sb.append("Macro '"); - sb.append(macro_name); - sb.append - ( - "' is not defined as a single value and thus cannot be used as one." - ); - sb.append(System.lineSeparator()); - sb.append("Does it contain instructions?"); - sb.append(System.lineSeparator()); - sb.append("Is it a sequence of multiple values?"); - - return sb.toString(); - } -} diff --git a/src/core/src/tonkadur/fate/v1/error/NotInAMacroException.java b/src/core/src/tonkadur/fate/v1/error/NotInAMacroException.java deleted file mode 100644 index 2bb1ed2..0000000 --- a/src/core/src/tonkadur/fate/v1/error/NotInAMacroException.java +++ /dev/null @@ -1,41 +0,0 @@ -package tonkadur.fate.v1.error; - -import tonkadur.error.ErrorLevel; - -import tonkadur.parser.Origin; -import tonkadur.parser.ParsingError; - -public class NotInAMacroException extends ParsingError -{ - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - public NotInAMacroException (final Origin origin) - { - super - ( - ErrorLevel.ERROR, - ErrorCategory.INVALID_USE, - origin - ); - } - - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append(origin.toString()); - sb.append(" "); - sb.append(error_category.toString()); - sb.append(System.lineSeparator()); - - sb.append("This can only be done/used in a macro."); - - return sb.toString(); - } -} diff --git a/src/core/src/tonkadur/fate/v1/error/UnknownParameterException.java b/src/core/src/tonkadur/fate/v1/error/UnknownParameterException.java deleted file mode 100644 index 6e09bee..0000000 --- a/src/core/src/tonkadur/fate/v1/error/UnknownParameterException.java +++ /dev/null @@ -1,68 +0,0 @@ -package tonkadur.fate.v1.error; - -import tonkadur.error.ErrorLevel; - -import tonkadur.parser.Origin; -import tonkadur.parser.ParsingError; - -import tonkadur.fate.v1.lang.meta.TypedEntryList; - -public class UnknownParameterException extends ParsingError -{ - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final TypedEntryList available_parameters; - protected final String parameter_name; - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - public UnknownParameterException - ( - final Origin origin, - final String parameter_name, - final TypedEntryList available_parameters - ) - { - super - ( - ErrorLevel.ERROR, - ErrorCategory.UNKNOWN, - origin - ); - - this.parameter_name = parameter_name; - this.available_parameters = available_parameters; - } - - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append(origin.toString()); - sb.append(" "); - sb.append(error_category.toString()); - sb.append(System.lineSeparator()); - - sb.append("Unknown parameter '"); - sb.append(parameter_name); - sb.append("'. "); - sb.append(System.lineSeparator()); - sb.append("Available parameters:'"); - - for - ( - final TypedEntryList.TypedEntry param: - available_parameters.get_entries() - ) - { - sb.append(System.lineSeparator()); - sb.append("- "); - sb.append(param.get_name()); - } - - return sb.toString(); - } -} diff --git a/src/core/src/tonkadur/fate/v1/error/UnknownVariableScopeException.java b/src/core/src/tonkadur/fate/v1/error/UnknownVariableScopeException.java deleted file mode 100644 index 129a789..0000000 --- a/src/core/src/tonkadur/fate/v1/error/UnknownVariableScopeException.java +++ /dev/null @@ -1,55 +0,0 @@ -package tonkadur.fate.v1.error; - -import tonkadur.error.ErrorLevel; - -import tonkadur.parser.Origin; -import tonkadur.parser.ParsingError; - -import tonkadur.fate.v1.lang.VariableScope; - -public class UnknownVariableScopeException extends ParsingError -{ - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final String name; - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - public UnknownVariableScopeException - ( - final Origin origin, - final String name - ) - { - super(ErrorLevel.ERROR, ErrorCategory.INVALID_USE, origin); - this.name = name; - } - - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append(origin.toString()); - sb.append(" "); - sb.append(error_category.toString()); - sb.append(System.lineSeparator()); - - sb.append("Unknown variable scope '"); - sb.append(name); - sb.append("'."); - sb.append(System.lineSeparator()); - sb.append("Available fields are:"); - - for (final String scope: VariableScope.get_available_scopes()) - { - sb.append(System.lineSeparator()); - sb.append("- "); - sb.append(scope); - } - - return sb.toString(); - } -} diff --git a/src/core/src/tonkadur/fate/v1/lang/Macro.java b/src/core/src/tonkadur/fate/v1/lang/Macro.java deleted file mode 100644 index c4c4dc4..0000000 --- a/src/core/src/tonkadur/fate/v1/lang/Macro.java +++ /dev/null @@ -1,167 +0,0 @@ -package tonkadur.fate.v1.lang; - -import java.util.ArrayList; -import java.util.List; - -import tonkadur.parser.Origin; - -import tonkadur.fate.v1.lang.meta.DeclaredEntity; -import tonkadur.fate.v1.lang.meta.Instruction; -import tonkadur.fate.v1.lang.meta.TypedEntryList; -import tonkadur.fate.v1.lang.meta.Computation; - -import tonkadur.fate.v1.lang.type.Type; - -import tonkadur.fate.v1.lang.instruction.Display; -import tonkadur.fate.v1.lang.instruction.InstructionList; - -import tonkadur.fate.v1.lang.computation.Cast; -import tonkadur.fate.v1.lang.computation.ValueToRichText; - -public class Macro extends DeclaredEntity -{ - @Override - public /* static */ String get_type_name () - { - return "Macro"; - } - - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final Instruction root; - protected final TypedEntryList parameters; - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - - /**** Constructors *********************************************************/ - public Macro - ( - final Origin origin, - final Instruction root, - final TypedEntryList parameters, - final String name - ) - { - super(origin, name); - - this.root = root; - this.parameters = parameters; - } - - /**** Accessors ************************************************************/ - public TypedEntryList get_parameters () - { - return parameters; - } - - public Instruction get_root () - { - return root; - } - - public List get_signature () - { - final List result; - - result = new ArrayList(); - - for (final TypedEntryList.TypedEntry entry: parameters.get_entries()) - { - result.add(entry.get_type()); - } - - return result; - } - - public Computation get_value_node_representation () - { - final Cast result_cast; - InstructionList root_as_il; - Instruction instr; - Computation result; - - if (!(root instanceof InstructionList)) - { - return null; - } - - root_as_il = (InstructionList) root; - - if (root_as_il.get_instructions().size() != 1) - { - return null; - } - - instr = root_as_il.get_instructions().get(0); - - if (!(instr instanceof Display)) - { - return null; - } - - result = ((Display) instr).get_content(); - - if (!(result instanceof ValueToRichText)) - { - return result; - } - - result = ((ValueToRichText) result).get_value(); - - if (!(result instanceof Cast)) - { - return result; - } - - result_cast = (Cast) result; - - if (result_cast.is_autogenerated()) - { - return result_cast.get_parent(); - } - else - { - return result; - } - } - /**** Compatibility ********************************************************/ - - /* - * This is for the very special case where a type is used despite not being - * even a sub-type of the expected one. Using this rather expensive function, - * the most restrictive shared type will be returned. If no such type exists, - * the ANY time is returned. - */ - @Override - public DeclaredEntity generate_comparable_to (final DeclaredEntity de) - { - return null; - } - - - /**** Misc. ****************************************************************/ - @Override - public boolean is_incompatible_with_declaration (final DeclaredEntity de) - { - return true; - } - - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append("(Macro "); - sb.append(name); - sb.append(")"); - - return sb.toString(); - } - - /***************************************************************************/ - /**** PROTECTED ************************************************************/ - /***************************************************************************/ -} diff --git a/src/core/src/tonkadur/fate/v1/lang/Sequence.java b/src/core/src/tonkadur/fate/v1/lang/Sequence.java index 5b97e5c..34b1dc5 100644 --- a/src/core/src/tonkadur/fate/v1/lang/Sequence.java +++ b/src/core/src/tonkadur/fate/v1/lang/Sequence.java @@ -30,6 +30,7 @@ public class Sequence extends DeclaredEntity /**** MEMBERS **************************************************************/ /***************************************************************************/ protected final Instruction root; + protected final List signature; /***************************************************************************/ /**** PUBLIC ***************************************************************/ @@ -40,12 +41,14 @@ public class Sequence extends DeclaredEntity ( final Origin origin, final Instruction root, - final String name + final String name, + final List signature ) { super(origin, name); this.root = root; + this.signature = signature; } /**** Accessors ************************************************************/ @@ -54,6 +57,11 @@ public class Sequence extends DeclaredEntity return root; } + public List get_signature () + { + return signature; + } + /**** Compatibility ********************************************************/ /* diff --git a/src/core/src/tonkadur/fate/v1/lang/Variable.java b/src/core/src/tonkadur/fate/v1/lang/Variable.java index b81d2bf..3a8b761 100644 --- a/src/core/src/tonkadur/fate/v1/lang/Variable.java +++ b/src/core/src/tonkadur/fate/v1/lang/Variable.java @@ -23,7 +23,6 @@ public class Variable extends DeclaredEntity new Variable ( Origin.BASE_LANGUAGE, - VariableScope.ANY, Type.ANY, /* * Use of a space necessary to avoid conflicting with a user created @@ -48,7 +47,6 @@ public class Variable extends DeclaredEntity /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ - protected final VariableScope scope; protected final Type type; /***************************************************************************/ @@ -59,14 +57,12 @@ public class Variable extends DeclaredEntity public Variable ( final Origin origin, - final VariableScope scope, final Type type, final String name ) { super(origin, name); - this.scope = scope; this.type = type; } @@ -76,15 +72,9 @@ public class Variable extends DeclaredEntity return type; } - public VariableScope get_scope () - { - return scope; - } - @Override public DeclaredEntity generate_comparable_to (final DeclaredEntity de) { - final VariableScope new_scope; final Type new_type; final Variable v; @@ -95,10 +85,9 @@ public class Variable extends DeclaredEntity v = (Variable) de; - new_scope = scope.generate_compatible_with(v.scope); new_type = (Type) type.generate_comparable_to(v.type); - return new Variable(origin, new_scope, new_type, name); + return new Variable(origin, new_type, name); } /**** Misc. ****************************************************************/ @@ -111,11 +100,7 @@ public class Variable extends DeclaredEntity v = (Variable) de; - return - ( - (!scope.equals(v.scope)) - || !type.can_be_used_as(v.type) - ); + return !type.can_be_used_as(v.type); } return true; @@ -127,8 +112,6 @@ public class Variable extends DeclaredEntity final StringBuilder sb = new StringBuilder(); sb.append("("); - sb.append(scope.toString()); - sb.append(" "); sb.append(type.get_name()); sb.append(" Variable "); sb.append(name); diff --git a/src/core/src/tonkadur/fate/v1/lang/VariableScope.java b/src/core/src/tonkadur/fate/v1/lang/VariableScope.java deleted file mode 100644 index 7f42fa6..0000000 --- a/src/core/src/tonkadur/fate/v1/lang/VariableScope.java +++ /dev/null @@ -1,88 +0,0 @@ -package tonkadur.fate.v1.lang; - -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.HashSet; - -/* - * Yes, it *could* have been an enum. Except that you can't extend enums, making - * them inadequate for anything that has even the slightest chance of needing to - * be extended at some point in the future. In other words, they're inadequate - * for anything but very rare corner cases (days in week, for example). - * Tonkadur wants extension support, ergo, no enums in Tonkadur. - * - * The use of a collection to decode them stems from the same reason (can't - * override static methods). - */ -public class VariableScope -{ - public static final VariableScope ANY; - - protected static final Map from_name; - public static final VariableScope LOCAL; - public static final VariableScope GLOBAL; - - static - { - from_name = new HashMap(); - - ANY = new VariableScope("unknown scope", null); - GLOBAL = new VariableScope("global", null); - LOCAL = new VariableScope("local", GLOBAL); - } - - public static VariableScope value_of (final String string) - { - return from_name.get(string); - } - - public static Set get_available_scopes () - { - return from_name.keySet(); - } - - protected final String name; - protected final Set more_restrictive_than; - - protected VariableScope (final String name, final VariableScope parent) - { - this.name = name; - - more_restrictive_than = new HashSet(); - - if (parent != null) - { - more_restrictive_than.addAll(parent.more_restrictive_than); - more_restrictive_than.add(parent); - } - - from_name.put(name, this); - } - - public VariableScope generate_compatible_with (final VariableScope other) - { - if (other.equals(this)) - { - return this; - } - - if (other.more_restrictive_than.contains(this)) - { - if (this.more_restrictive_than.contains(other)) - { - return ANY; - } - - return other; - } - - return this; - } - - @Override - public String toString () - { - return name; - } -} diff --git a/src/core/src/tonkadur/fate/v1/lang/World.java b/src/core/src/tonkadur/fate/v1/lang/World.java index 7b15f3f..a7da2f0 100644 --- a/src/core/src/tonkadur/fate/v1/lang/World.java +++ b/src/core/src/tonkadur/fate/v1/lang/World.java @@ -31,14 +31,13 @@ public class World protected final Set loaded_files; protected final Set required_extensions; - protected final Map> sequence_calls; + protected final Map> sequence_uses; protected final Map extension_value_nodes; protected final Map extension_instructions; protected final Map extension_first_level_instructions; protected final DeclarationCollection event_collection; - protected final DeclarationCollection macro_collection; protected final DeclarationCollection sequence_collection; protected final DeclarationCollection text_effect_collection; protected final DeclarationCollection type_collection; @@ -56,7 +55,7 @@ public class World loaded_files = new HashSet(); required_extensions = new HashSet(); - sequence_calls = new HashMap>(); + sequence_uses = new HashMap>(); extension_value_nodes = new HashMap(); extension_instructions = new HashMap(); extension_first_level_instructions = @@ -64,7 +63,6 @@ public class World event_collection = new DeclarationCollection(Event.value_on_missing()); - macro_collection = new DeclarationCollection(null); sequence_collection = new DeclarationCollection(null); text_effect_collection = @@ -115,18 +113,18 @@ public class World /**** Sequence Calls ****/ public void add_sequence_call (final Origin origin, final String sequence) { - List list_of_calls; + List list_of_uses; - list_of_calls = sequence_calls.get(sequence); + list_of_uses = sequence_uses.get(sequence); - if (list_of_calls == null) + if (list_of_uses == null) { - list_of_calls = new ArrayList(); + list_of_uses = new ArrayList(); - sequence_calls.put(sequence, list_of_calls); + sequence_uses.put(sequence, list_of_uses); } - list_of_calls.add(origin); + list_of_uses.add(origin); } /**** Extension Stuff ****/ @@ -155,11 +153,6 @@ public class World return event_collection; } - public DeclarationCollection macros () - { - return macro_collection; - } - public DeclarationCollection sequences () { return sequence_collection; @@ -198,7 +191,7 @@ public class World is_sane = true; - is_sane = assert_sane_sequence_calls() & is_sane; + is_sane = assert_sane_sequence_uses() & is_sane; return is_sane; } @@ -284,7 +277,7 @@ public class World } } - protected boolean assert_sane_sequence_calls () + protected boolean assert_sane_sequence_uses () throws UnknownSequenceException { boolean is_sane; @@ -294,7 +287,7 @@ public class World for ( final Map.Entry> entry: - sequence_calls.entrySet() + sequence_uses.entrySet() ) { if (!sequences().has(entry.getKey())) diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/LambdaEvaluation.java b/src/core/src/tonkadur/fate/v1/lang/computation/LambdaEvaluation.java new file mode 100644 index 0000000..9bdc731 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/LambdaEvaluation.java @@ -0,0 +1,204 @@ +package tonkadur.fate.v1.lang.computation; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; + +import tonkadur.functional.Merge; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.error.IncompatibleTypeException; +import tonkadur.fate.v1.error.IncomparableTypeException; +import tonkadur.fate.v1.error.InvalidArityException; +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.LambdaType; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.Reference; + +public class LambdaEvaluation extends Computation +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final Reference lambda_function; + protected final List parameters; + + /***************************************************************************/ + /**** PROTECTED ************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + protected LambdaEvaluation + ( + final Origin origin, + final Reference lambda_function, + final List parameters, + final Type act_as + ) + { + super(origin, act_as); + + this.lambda_function = lambda_function; + this.parameters = parameters; + } + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + public static LambdaEvaluation build + ( + final Origin origin, + final Reference reference, + final List parameters + ) + throws Throwable + { + final Type var_type; + final LambdaType lambda_type; + final List signature; + + var_type = reference.get_type(); + + if (!(var_type instanceof LambdaType)) + { + ErrorManager.handle + ( + new InvalidTypeException + ( + origin, + var_type, + Collections.singleton(Type.LAMBDA) + ) + ); + + return null; + } + + lambda_type = (LambdaType) var_type; + + signature = lambda_type.get_signature(); + + if (parameters.size() != signature.size()) + { + ErrorManager.handle + ( + new InvalidArityException + ( + origin, + parameters.size(), + signature.size(), + signature.size() + ) + ); + } + + (new Merge() + { + @Override + public Boolean risky_lambda (final Type t, final Computation p) + throws ParsingError + { + if ((t == null) || (p == null)) + { + return Boolean.FALSE; + } + else + { + final Type hint; + + if (p.get_type().can_be_used_as(t)) + { + return Boolean.TRUE; + } + + if (p.get_type().try_merging_with(t) != null) + { + return Boolean.TRUE; + } + + ErrorManager.handle + ( + new IncompatibleTypeException + ( + p.get_origin(), + p.get_type(), + t + ) + ); + + hint = (Type) p.get_type().generate_comparable_to(t); + + if (hint.equals(Type.ANY)) + { + ErrorManager.handle + ( + new IncomparableTypeException + ( + p.get_origin(), + p.get_type(), + t + ) + ); + } + + return Boolean.FALSE; + } + } + }).risky_merge(signature, parameters); + + return + new LambdaEvaluation + ( + origin, + reference, + parameters, + lambda_type.get_return_type() + ); + } + + /**** Accessors ************************************************************/ + @Override + public void get_visited_by (final ComputationVisitor cv) + throws Throwable + { + cv.visit_lambda_evaluation(this); + } + + public Reference get_lambda_function_reference () + { + return lambda_function; + } + + public List get_parameters () + { + return parameters; + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb = new StringBuilder(); + + sb.append("(LambdaEvaluation ("); + sb.append(lambda_function.toString()); + + for (final Computation param: parameters) + { + sb.append(" "); + sb.append(param.toString()); + } + + sb.append("))"); + + return sb.toString(); + } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/LambdaExpression.java b/src/core/src/tonkadur/fate/v1/lang/computation/LambdaExpression.java new file mode 100644 index 0000000..6c99aa4 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/LambdaExpression.java @@ -0,0 +1,83 @@ +package tonkadur.fate.v1.lang.computation; + +import java.util.List; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Computation; + +import tonkadur.fate.v1.lang.Variable; + +public class LambdaExpression extends Computation +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final Computation function; + protected final List parameters; + + /***************************************************************************/ + /**** PROTECTED ************************************************************/ + /***************************************************************************/ + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + public LambdaExpression + ( + final Origin origin, + final List parameters, + final Computation function + ) + { + super(origin, function.get_type()); + + this.function = function; + this.parameters = parameters; + } + + /**** Accessors ************************************************************/ + @Override + public void get_visited_by (final ComputationVisitor cv) + throws Throwable + { + cv.visit_lambda_expression(this); + } + + public Computation get_lambda_function () + { + return function; + } + + public List get_parameters () + { + return parameters; + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb = new StringBuilder(); + + sb.append("(LambdaExpression ("); + + for (final Variable param: parameters) + { + sb.append("("); + sb.append(param.get_type()); + sb.append(" "); + sb.append(param.get_name()); + sb.append(")"); + } + sb.append(") "); + + sb.append(function.toString()); + + sb.append(")"); + + return sb.toString(); + } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/MacroValueCall.java b/src/core/src/tonkadur/fate/v1/lang/computation/MacroValueCall.java deleted file mode 100644 index 5e4cdfd..0000000 --- a/src/core/src/tonkadur/fate/v1/lang/computation/MacroValueCall.java +++ /dev/null @@ -1,193 +0,0 @@ -package tonkadur.fate.v1.lang.computation; - -import java.util.ArrayList; -import java.util.List; - -import tonkadur.parser.Origin; -import tonkadur.parser.ParsingError; - -import tonkadur.functional.Merge; - -import tonkadur.error.ErrorManager; - -import tonkadur.fate.v1.error.IncompatibleTypeException; -import tonkadur.fate.v1.error.IncomparableTypeException; -import tonkadur.fate.v1.error.InvalidArityException; -import tonkadur.fate.v1.error.NotAValueMacroException; - -import tonkadur.fate.v1.lang.Macro; - -import tonkadur.fate.v1.lang.type.Type; - -import tonkadur.fate.v1.lang.meta.ComputationVisitor; -import tonkadur.fate.v1.lang.meta.Computation; - -public class MacroValueCall extends Computation -{ - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final Macro macro; - protected final Computation act_as; - protected final List parameters; - - /***************************************************************************/ - /**** PROTECTED ************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - protected MacroValueCall - ( - final Origin origin, - final Macro macro, - final List parameters, - final Computation act_as - ) - { - super(origin, act_as.get_type()); - - this.macro = macro; - this.parameters = parameters; - this.act_as = act_as; - } - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - public static MacroValueCall build - ( - final Origin origin, - final Macro macro, - final List parameters - ) - throws Throwable - { - Computation act_as; - final List signature; - - act_as = macro.get_value_node_representation(); - - if (act_as == null) - { - ErrorManager.handle - ( - new NotAValueMacroException(origin, macro.get_name()) - ); - } - - signature = macro.get_signature(); - - if (parameters.size() != signature.size()) - { - ErrorManager.handle - ( - new InvalidArityException - ( - origin, - parameters.size(), - signature.size(), - signature.size() - ) - ); - } - - (new Merge() - { - @Override - public Boolean risky_lambda (final Type t, final Computation p) - throws ParsingError - { - if ((t == null) || (p == null)) - { - return Boolean.FALSE; - } - else - { - final Type hint; - - if (p.get_type().can_be_used_as(t)) - { - return Boolean.TRUE; - } - - if (p.get_type().try_merging_with(t) != null) - { - return Boolean.TRUE; - } - - ErrorManager.handle - ( - new IncompatibleTypeException - ( - p.get_origin(), - p.get_type(), - t - ) - ); - - hint = (Type) p.get_type().generate_comparable_to(t); - - if (hint.equals(Type.ANY)) - { - ErrorManager.handle - ( - new IncomparableTypeException - ( - p.get_origin(), - p.get_type(), - t - ) - ); - } - - return Boolean.FALSE; - } - } - }).risky_merge(signature, parameters); - - return new MacroValueCall(origin, macro, parameters, act_as); - } - - /**** Accessors ************************************************************/ - @Override - public void get_visited_by (final ComputationVisitor cv) - throws Throwable - { - cv.visit_macro_value_call(this); - } - - public Macro get_macro () - { - return macro; - } - - public Computation get_actual_value_node () - { - return act_as; - } - - public List get_parameters () - { - return parameters; - } - - /**** Misc. ****************************************************************/ - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append("(MacroValueCall ("); - sb.append(macro.get_name()); - - for (final Computation param: parameters) - { - sb.append(" "); - sb.append(param.toString()); - } - - sb.append("))"); - - return sb.toString(); - } -} diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/ParameterReference.java b/src/core/src/tonkadur/fate/v1/lang/computation/ParameterReference.java deleted file mode 100644 index 6266c7c..0000000 --- a/src/core/src/tonkadur/fate/v1/lang/computation/ParameterReference.java +++ /dev/null @@ -1,53 +0,0 @@ -package tonkadur.fate.v1.lang.computation; - -import tonkadur.parser.Origin; - -import tonkadur.fate.v1.lang.meta.ComputationVisitor; -import tonkadur.fate.v1.lang.meta.Reference; - -import tonkadur.fate.v1.lang.type.Type; - -public class ParameterReference extends Reference -{ - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - public ParameterReference - ( - final Origin origin, - final Type reported_type, - final String parameter_name - ) - { - super(origin, reported_type, parameter_name); - } - - /**** Accessors ************************************************************/ - @Override - public void get_visited_by (final ComputationVisitor cv) - throws Throwable - { - cv.visit_parameter_reference(this); - } - - /**** Misc. ****************************************************************/ - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append(origin.toString()); - sb.append("(ParameterReference ("); - sb.append(type.get_name()); - sb.append(") "); - sb.append(name); - sb.append(")"); - - return sb.toString(); - } -} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Done.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Done.java new file mode 100644 index 0000000..ada3b7c --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Done.java @@ -0,0 +1,37 @@ +package tonkadur.fate.v1.lang.instruction; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Instruction; + +public class Done extends Instruction +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + public Done (final Origin origin) + { + super(origin); + } + + /**** Accessors ************************************************************/ + @Override + public void get_visited_by (final InstructionVisitor iv) + throws Throwable + { + iv.visit_done(this); + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + return "(Done)"; + } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/LocalVariable.java b/src/core/src/tonkadur/fate/v1/lang/instruction/LocalVariable.java new file mode 100644 index 0000000..7a87d7d --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/LocalVariable.java @@ -0,0 +1,57 @@ +package tonkadur.fate.v1.lang.instruction; + +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Instruction; + +import tonkadur.fate.v1.lang.Variable; + +public class LocalVariable extends Instruction +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final Variable variable; + + /***************************************************************************/ + /**** PROTECTED ************************************************************/ + /***************************************************************************/ + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + protected LocalVariable (final Variable variable) + { + super(variable.get_origin()); + + this.variable = variable; + } + + + /**** Accessors ************************************************************/ + @Override + public void get_visited_by (final InstructionVisitor iv) + throws Throwable + { + iv.visit_local_variable(this); + } + + public Variable get_variable () + { + return variable; + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb = new StringBuilder(); + + sb.append("(LocalVariable "); + sb.append(variable.toString()); + + sb.append(")"); + + return sb.toString(); + } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/MacroCall.java b/src/core/src/tonkadur/fate/v1/lang/instruction/MacroCall.java deleted file mode 100644 index c3c3b44..0000000 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/MacroCall.java +++ /dev/null @@ -1,175 +0,0 @@ -package tonkadur.fate.v1.lang.instruction; - -import java.util.ArrayList; -import java.util.List; - -import tonkadur.parser.Origin; -import tonkadur.parser.ParsingError; - -import tonkadur.functional.Merge; - -import tonkadur.error.ErrorManager; - -import tonkadur.fate.v1.error.IncompatibleTypeException; -import tonkadur.fate.v1.error.IncomparableTypeException; -import tonkadur.fate.v1.error.InvalidArityException; - -import tonkadur.fate.v1.lang.Macro; - -import tonkadur.fate.v1.lang.type.Type; - -import tonkadur.fate.v1.lang.meta.InstructionVisitor; -import tonkadur.fate.v1.lang.meta.Instruction; -import tonkadur.fate.v1.lang.meta.Computation; - -public class MacroCall extends Instruction -{ - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final Macro macro; - protected final List parameters; - - /***************************************************************************/ - /**** PROTECTED ************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - protected MacroCall - ( - final Origin origin, - final Macro macro, - final List parameters - ) - { - super(origin); - - this.macro = macro; - this.parameters = parameters; - } - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - public static MacroCall build - ( - final Origin origin, - final Macro macro, - final List parameters - ) - throws Throwable - { - final List type_checks; - final List signature; - - signature = macro.get_signature(); - - if (parameters.size() != signature.size()) - { - ErrorManager.handle - ( - new InvalidArityException - ( - origin, - parameters.size(), - signature.size(), - signature.size() - ) - ); - } - - (new Merge() - { - @Override - public Boolean risky_lambda (final Type t, final Computation p) - throws ParsingError - { - if ((t == null) || (p == null)) - { - return Boolean.FALSE; - } - else - { - final Type hint; - - if (p.get_type().can_be_used_as(t)) - { - return Boolean.TRUE; - } - - if (p.get_type().try_merging_with(t) != null) - { - return Boolean.TRUE; - } - - ErrorManager.handle - ( - new IncompatibleTypeException - ( - p.get_origin(), - p.get_type(), - t - ) - ); - - hint = (Type) p.get_type().generate_comparable_to(t); - - if (hint.equals(Type.ANY)) - { - ErrorManager.handle - ( - new IncomparableTypeException - ( - p.get_origin(), - p.get_type(), - t - ) - ); - } - - return Boolean.FALSE; - } - } - }).risky_merge(signature, parameters); - - return new MacroCall(origin, macro, parameters); - } - - /**** Accessors ************************************************************/ - @Override - public void get_visited_by (final InstructionVisitor iv) - throws Throwable - { - iv.visit_macro_call(this); - } - - public Macro get_macro () - { - return macro; - } - - public List get_parameters () - { - return parameters; - } - - /**** Misc. ****************************************************************/ - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append("(MacroCall ("); - sb.append(macro.get_name()); - - for (final Computation param: parameters) - { - sb.append(" "); - sb.append(param.toString()); - } - - sb.append("))"); - - return sb.toString(); - } -} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceCall.java b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceCall.java index 6ea696d..c65f490 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceCall.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceCall.java @@ -1,7 +1,10 @@ package tonkadur.fate.v1.lang.instruction; +import java.util.List; + import tonkadur.parser.Origin; +import tonkadur.fate.v1.lang.meta.Computation; import tonkadur.fate.v1.lang.meta.InstructionVisitor; import tonkadur.fate.v1.lang.meta.Instruction; @@ -10,17 +13,24 @@ public class SequenceCall extends Instruction /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ + protected final List parameters; protected final String sequence_name; /***************************************************************************/ /**** PUBLIC ***************************************************************/ /***************************************************************************/ /**** Constructors *********************************************************/ - public SequenceCall (final Origin origin, final String sequence_name) + public SequenceCall + ( + final Origin origin, + final String sequence_name, + final List parameters + ) { super(origin); this.sequence_name = sequence_name; + this.parameters = parameters; } /**** Accessors ************************************************************/ @@ -36,6 +46,11 @@ public class SequenceCall extends Instruction return sequence_name; } + public List get_parameters () + { + return parameters; + } + /**** Misc. ****************************************************************/ @Override public String toString () @@ -44,6 +59,13 @@ public class SequenceCall extends Instruction sb.append("(SequenceCall "); sb.append(sequence_name); + + for (final Computation c: parameters) + { + sb.append(" "); + sb.append(c.toString()); + } + sb.append(")"); return sb.toString(); diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceJump.java b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceJump.java new file mode 100644 index 0000000..fb95d84 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceJump.java @@ -0,0 +1,73 @@ +package tonkadur.fate.v1.lang.instruction; + +import java.util.List; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.lang.meta.Computation; +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Instruction; + +public class SequenceJump extends Instruction +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final List parameters; + protected final String sequence_name; + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + public SequenceJump + ( + final Origin origin, + final String sequence_name, + final List parameters + ) + { + super(origin); + + this.sequence_name = sequence_name; + this.parameters = parameters; + } + + /**** Accessors ************************************************************/ + @Override + public void get_visited_by (final InstructionVisitor iv) + throws Throwable + { + iv.visit_sequence_jump(this); + } + + public String get_sequence_name () + { + return sequence_name; + } + + public List get_parameters () + { + return parameters; + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb = new StringBuilder(); + + sb.append("(SequenceJump "); + sb.append(sequence_name); + + for (final Computation c: parameters) + { + sb.append(" "); + sb.append(c.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 d2c4ff0..0b992cb 100644 --- a/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java +++ b/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java @@ -43,7 +43,10 @@ public interface ComputationVisitor public void visit_size_operator (final SizeOperator n) throws Throwable; - public void visit_macro_value_call (final MacroValueCall n) + public void visit_lambda_expression (final LambdaExpression n) + throws Throwable; + + public void visit_lambda_evaluation (final LambdaEvaluation n) throws Throwable; public void visit_newline (final Newline n) @@ -55,9 +58,6 @@ public interface ComputationVisitor public void visit_paragraph (final Paragraph n) throws Throwable; - public void visit_parameter_reference (final ParameterReference n) - throws Throwable; - public void visit_ref_operator (final RefOperator n) throws Throwable; diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java b/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java index 3f4f973..afaa151 100644 --- a/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java +++ b/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java @@ -17,6 +17,9 @@ public interface InstructionVisitor public void visit_end (final End n) throws Throwable; + public void visit_done (final Done n) + throws Throwable; + public void visit_free (final Free n) throws Throwable; @@ -62,9 +65,6 @@ public interface InstructionVisitor public void visit_instruction_list (final InstructionList n) throws Throwable; - public void visit_macro_call (final MacroCall n) - throws Throwable; - public void visit_player_choice (final PlayerChoice n) throws Throwable; @@ -80,6 +80,12 @@ public interface InstructionVisitor public void visit_sequence_call (final SequenceCall n) throws Throwable; + public void visit_sequence_jump (final SequenceJump n) + throws Throwable; + + public void visit_local_variable (final LocalVariable n) + throws Throwable; + public void visit_set_value (final SetValue n) throws Throwable; } diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/TypedEntryList.java b/src/core/src/tonkadur/fate/v1/lang/meta/TypedEntryList.java deleted file mode 100644 index d9cee24..0000000 --- a/src/core/src/tonkadur/fate/v1/lang/meta/TypedEntryList.java +++ /dev/null @@ -1,111 +0,0 @@ -package tonkadur.fate.v1.lang.meta; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import tonkadur.parser.Origin; - -import tonkadur.error.ErrorManager; - -import tonkadur.fate.v1.error.DuplicateFieldException; - -import tonkadur.fate.v1.lang.type.Type; - -public class TypedEntryList -{ - protected final Map as_map; - protected final List as_list; - - public TypedEntryList () - { - as_map = new HashMap(); - as_list = new ArrayList(); - } - - public void remove (final String name) - { - TypedEntry previous_entry; - - previous_entry = as_map.get(name); - - as_list.remove(previous_entry); - as_map.remove(name); - } - - public void add - ( - final Origin origin, - final Type type, - final String name - ) - throws DuplicateFieldException - { - TypedEntry previous_entry; - - previous_entry = as_map.get(name); - - if (previous_entry != null) - { - ErrorManager.handle - ( - new DuplicateFieldException - ( - origin, - previous_entry.get_origin(), - name - ) - ); - } - - previous_entry = new TypedEntry(origin, type, name); - - as_map.put(name, previous_entry); - as_list.add(previous_entry); - } - - public List get_entries () - { - return as_list; - } - - public Map as_map () - { - return as_map; - } - - public static class TypedEntry - { - protected final Origin origin; - protected final Type type; - protected final String name; - - protected TypedEntry - ( - final Origin origin, - final Type type, - final String name - ) - { - this.origin = origin; - this.type = type; - this.name = name; - } - - public String get_name () - { - return name; - } - - public Type get_type () - { - return type; - } - - public Origin get_origin () - { - return origin; - } - } -} diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/VariableList.java b/src/core/src/tonkadur/fate/v1/lang/meta/VariableList.java new file mode 100644 index 0000000..dbf9286 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/meta/VariableList.java @@ -0,0 +1,72 @@ +package tonkadur.fate.v1.lang.meta; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import tonkadur.parser.Origin; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.error.DuplicateFieldException; + +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.Variable; + +public class VariableList +{ + protected final Map as_map; + protected final List as_list; + + public VariableList () + { + as_map = new HashMap(); + as_list = new ArrayList(); + } + + public void remove (final String name) + { + final Variable previous_entry; + + previous_entry = as_map.get(name); + + as_list.remove(previous_entry); + as_map.remove(name); + } + + public void add (final Variable entry) + throws DuplicateFieldException + { + final Variable previous_entry; + + previous_entry = as_map.get(entry.get_name()); + + if (previous_entry != null) + { + ErrorManager.handle + ( + new DuplicateFieldException + ( + entry.get_origin(), + previous_entry.get_origin(), + entry.get_name() + ) + ); + } + + as_map.put(entry.get_name(), entry); + as_list.add(entry); + } + + public List get_entries () + { + return as_list; + } + + public Map as_map () + { + return as_map; + } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/type/LambdaType.java b/src/core/src/tonkadur/fate/v1/lang/type/LambdaType.java new file mode 100644 index 0000000..0ca9c48 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/type/LambdaType.java @@ -0,0 +1,163 @@ +package tonkadur.fate.v1.lang.type; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.lang.meta.DeclaredEntity; + +public class LambdaType extends Type +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final List signature; + protected final Type return_type; + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + + /**** Constructors *********************************************************/ + public LambdaType + ( + final Origin origin, + final Type return_type, + final String name, + final List signature + ) + { + super(origin, null, name); + + this.signature = signature; + this.return_type = return_type; + } + + /**** Accessors ************************************************************/ + public Type get_return_type () + { + return return_type; + } + + public List get_signature () + { + return signature; + } + + /**** Compatibility ********************************************************/ + @Override + public boolean can_be_used_as (final Type t) + { + if (t instanceof LambdaType) + { + final Iterator i0, i1; + final LambdaType lt; + + lt = (LambdaType) t; + + if + ( + signature.size() != lt.signature.size() + || !return_type.can_be_used_as(lt.return_type) + ) + { + return false; + } + + i0 = signature.iterator(); + i1 = lt.signature.iterator(); + + while(i0.hasNext()) + { + if (!i0.next().can_be_used_as(i1.next())) + { + return false; + } + } + + return true; + } + + return false; + } + + /* + * This is for the very special case where a type is used despite not being + * even a sub-type of the expected one. Using this rather expensive function, + * the most restrictive shared type will be returned. If no such type exists, + * the ANY time is returned. + */ + @Override + public DeclaredEntity generate_comparable_to (final DeclaredEntity de) + { + final Iterator i0, i1; + final List resulting_signature; + final Type resulting_return_type; + final LambdaType lt; + + if (!(de instanceof LambdaType)) + { + return Type.ANY; + } + + lt = (LambdaType) de; + + if (lt.signature.size() != signature.size()) + { + return Type.ANY; + } + + resulting_signature = new ArrayList(); + resulting_return_type = + (Type) return_type.generate_comparable_to(lt.return_type); + + i0 = signature.iterator(); + i1 = lt.signature.iterator(); + + while(i0.hasNext()) + { + resulting_signature.add + ( + (Type) i0.next().generate_comparable_to(i1.next()) + ); + } + + return + new LambdaType + ( + get_origin(), + resulting_return_type, + name, + resulting_signature + ); + } + + @Override + public Type get_act_as_type () + { + return Type.LAMBDA; + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb = new StringBuilder(); + + sb.append("(Lambda "); + sb.append(return_type.toString()); + sb.append(" ("); + + for (final Type t: signature) + { + sb.append(t.get_name()); + } + + sb.append("))::"); + sb.append(name); + + return sb.toString(); + } +} 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 f848047..92ff859 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/Type.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/Type.java @@ -24,6 +24,7 @@ public class Type extends DeclaredEntity public static final Type DICT; public static final Type FLOAT; public static final Type INT; + public static final Type LAMBDA; public static final Type LIST; public static final Type REF; public static final Type RICH_TEXT; @@ -50,6 +51,7 @@ public class Type extends DeclaredEntity DICT = new Type(base, null, "dict"); FLOAT = new Type(base, null, "float"); INT = new Type(base, null, "int"); + LAMBDA = new Type(base, null, "lambda"); LIST = new Type(base, null, "list"); REF = new Type(base, null, "ref"); RICH_TEXT = new Type(base, null, "text"); @@ -62,6 +64,7 @@ public class Type extends DeclaredEntity ALL_TYPES.add(DICT); ALL_TYPES.add(FLOAT); ALL_TYPES.add(INT); + ALL_TYPES.add(LAMBDA); ALL_TYPES.add(LIST); ALL_TYPES.add(REF); ALL_TYPES.add(RICH_TEXT); diff --git a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 index 2690cdf..a4cdb76 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 @@ -29,12 +29,8 @@ 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'|'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'('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+; -DEFINE_MACRO_KW: L_PAREN ('declare'|'define'|'def')US'macro' SEP+; DEFINE_SEQUENCE_KW: L_PAREN ('declare'|'define'|'def')US'seq'('uence')? SEP+; DIVIDE_KW: L_PAREN ('divide'|'/'|'div') SEP+; DO_WHILE_KW: L_PAREN ('do'US'while') SEP+; @@ -56,7 +52,6 @@ GREATER_EQUAL_THAN_KW: L_PAREN ('greater'US'equal'US'than'|'>='|'ge') SEP+; GREATER_THAN_KW: L_PAREN ('greater'US'than'|'>'|'gt') SEP+; IF_ELSE_KW: L_PAREN ('if'US'else') SEP+; IF_KW: L_PAREN 'if' SEP+; -IMACRO_KW: L_PAREN 'macro' SEP+; IMPLIES_KW: L_PAREN ('implies'|'=>'|'->') SEP+; INCLUDE_KW: L_PAREN 'include' SEP+; INDEX_OF_KW: L_PAREN ('index'US'of') SEP+; @@ -66,6 +61,8 @@ LOWER_THAN_KW: L_PAREN ('lower'US'than'|'<'|'lt') SEP+; MINUS_KW: L_PAREN ('minus'|'-') SEP+; MIN_KW: L_PAREN ('min'('imum'?)) SEP+; MAX_KW: L_PAREN ('max'('imum'?)) SEP+; +LAMBDA_KW: L_PAREN 'lambda' SEP+; +EVAL_KW: L_PAREN 'eval'('uate'?) SEP+; CLAMP_KW: L_PAREN ('clamp') SEP+; MODULO_KW: L_PAREN ('modulo'|'%'|'mod') SEP+; NEWLINE_KW: L_PAREN 'newline)'; @@ -74,7 +71,6 @@ NOT_KW: L_PAREN ('not'|'~'|'!') 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+; @@ -94,9 +90,11 @@ SIZE_KW: L_PAREN 'size' SEP+; SWITCH_KW: L_PAREN 'switch' SEP+; TIMES_KW: L_PAREN ('times'|'*') SEP+; TRUE_KW: L_PAREN 'true)'; +DONE_KW: L_PAREN 'done)'; VAL_KW: L_PAREN ('val'|'value') SEP+; VARIABLE_KW: L_PAREN ('variable'|'var') SEP+; -VMACRO_KW: L_PAREN 'vmacro' SEP+; +VISIT_KW: L_PAREN ('call'|'visit')(US(('seq'('uence'?))|('proc'('edure'?))))? SEP+; +CONTINUE_AS_KW: L_PAREN (('continue'US('as'|'to'|'with'))|('jump'(US'to')?)|('go'US'to')|'exec')(US(('seq'('uence'?))|('proc'('edure'?))))? SEP+; WHILE_KW: L_PAREN 'while' SEP+; WORD: (~([ \t\r\n()])|'\\)'|'\\(')+; diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index 22915e8..312ffef 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -27,9 +27,7 @@ options import tonkadur.fate.v1.error.IllegalReferenceNameException; import tonkadur.fate.v1.error.InvalidTypeException; - import tonkadur.fate.v1.error.NotInAMacroException; import tonkadur.fate.v1.error.UnknownExtensionContentException; - import tonkadur.fate.v1.error.UnknownParameterException; import tonkadur.fate.v1.error.UnknownVariableScopeException; import tonkadur.fate.v1.lang.*; @@ -43,8 +41,7 @@ options { Context CONTEXT; World WORLD; - TypedEntryList PARAMETERS; - List> ODD_VARS; + Deque> LOCAL_VARIABLES; int BREAKABLE_LEVELS; } @@ -56,9 +53,10 @@ fate_file [Context context, World world] { CONTEXT = context; WORLD = world; - PARAMETERS = null; - ODD_VARS = new ArrayList>(); + LOCAL_VARIABLES = new ArrayDeque>(); BREAKABLE_LEVELS = 0; + + LOCAL_VARIABLES.push(new HashMap()); } : WS* FATE_VERSION_KW WORD WS* R_PAREN WS* @@ -100,6 +98,18 @@ returns [List result] first_level_fate_instr: DEFINE_SEQUENCE_KW new_reference_name + ( + L_PAREN WS+ variable_list WS* R_PAREN + { + final Map variable_map; + + variable_map = new HashMap(); + + variable_map.putAll(($variable_list.result).as_map()); + + LOCAL_VARIABLES.push(variable_map); + } + ) pre_sequence_point=WS+ general_fate_sequence WS* @@ -131,27 +141,17 @@ first_level_fate_instr: sequence_origin, ($general_fate_sequence.result) ), - ($new_reference_name.result) + ($new_reference_name.result), + ($variable_list.result) ); WORLD.sequences().add(new_sequence); + LOCAL_VARIABLES.pop(); } | DECLARE_VARIABLE_KW type WS+ - name=new_reference_name - WS* - R_PAREN - { - final Origin start_origin, type_origin; - final Variable new_variable; - - start_origin = - CONTEXT.get_origin_at - ( - ($DECLARE_VARIABLE_KW.getLine()), - ($DECLARE_VARIABLE_KW.getCharPositionInLine()) ); new_variable = @@ -2498,8 +2498,6 @@ returns [Reference result] | ACCESS_KW value_reference WS+ value R_PAREN { $result = - Access.build - ( CONTEXT.get_origin_at ( ($ACCESS_KW.getLine()), @@ -2510,10 +2508,10 @@ returns [Reference result] ); } - | VARIABLE_KW WORD WS* R_PAREN + | (WORD | (VARIABLE_KW WORD WS* R_PAREN)) { final Origin target_var_origin; - final Variable target_var; + Variable target_var; final String[] subrefs; subrefs = ($WORD.text).split("\\."); @@ -2525,119 +2523,12 @@ returns [Reference result] ($WORD.getCharPositionInLine()) ); - target_var = WORLD.variables().get(target_var_origin, subrefs[0]); - - $result = - new VariableReference - ( - CONTEXT.get_origin_at - ( - ($VARIABLE_KW.getLine()), - ($VARIABLE_KW.getCharPositionInLine()) - ), - target_var - ); + target_var = LOCAL_VARIABLE.peekFirst().get(subrefs[0]); - if (subrefs.length > 1) + if (target_var == null) { - final List subrefs_list; - - subrefs_list = new ArrayList(Arrays.asList(subrefs)); - - subrefs_list.remove(0); - - $result = - FieldReference.build - ( - target_var_origin, - ($result), - subrefs_list - ); + target_var = WORLD.variables().get(target_var_origin, subrefs[0]); } - } - - | PARAMETER_KW WORD WS* R_PAREN - { - final Origin origin; - - origin = - CONTEXT.get_origin_at - ( - ($PARAMETER_KW.getLine()), - ($PARAMETER_KW.getCharPositionInLine()) - ); - - if (PARAMETERS == null) - { - ErrorManager.handle - ( - new NotInAMacroException(origin) - ); - } - else - { - final TypedEntryList.TypedEntry param; - final String[] subrefs; - - subrefs = ($WORD.text).split("\\."); - - param = PARAMETERS.as_map().get(subrefs[0]); - - if (param == null) - { - ErrorManager.handle - ( - new UnknownParameterException(origin, subrefs[0], PARAMETERS) - ); - - $result = new ParameterReference(origin, Type.ANY, ($WORD.text)); - } - else - { - $result = - new ParameterReference - ( - origin, - param.get_type(), - ($WORD.text) - ); - - if (subrefs.length > 1) - { - final List subrefs_list; - - subrefs_list = new ArrayList(Arrays.asList(subrefs)); - - subrefs_list.remove(0); - - $result = - FieldReference.build - ( - origin, - ($result), - subrefs_list - ); - } - } - } - } - - | WORD - { - final Origin target_var_origin; - final Variable target_var; - final String[] subrefs; - - subrefs = ($WORD.text).split("\\."); - - target_var_origin = - CONTEXT.get_origin_at - ( - ($WORD.getLine()), - ($WORD.getCharPositionInLine()) - ); - - target_var = WORLD.variables().get(target_var_origin, subrefs[0]); $result = new VariableReference diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/Compiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/Compiler.java index 037a705..c88b8be 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/Compiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/Compiler.java @@ -1,15 +1,14 @@ package tonkadur.wyrd.v1.compiler.fate.v1; -import tonkadur.wyrd.v1.compiler.util.AnonymousVariableManager; +import tonkadur.wyrd.v1.compiler.util.RegisterManager; import tonkadur.wyrd.v1.compiler.util.InstructionManager; -import tonkadur.wyrd.v1.lang.Variable; +import tonkadur.wyrd.v1.lang.Register; import tonkadur.wyrd.v1.lang.World; public class Compiler { - protected final AnonymousVariableManager anonymous_variables; - protected final MacroManager macro_manager; + protected final RegisterManager registers; protected final InstructionManager assembler; protected final World wyrd_world; @@ -27,13 +26,13 @@ public class Compiler compiler.compile_extensions(fate_world); compiler.compile_types(fate_world); - compiler.compile_variables(fate_world); + compiler.compile_registers(fate_world); compiler.compile_main_sequence(fate_world); compiler.compile_sequences(fate_world); - compiler.add_anonymous_variables(); + compiler.add_registers(); return compiler.wyrd_world; } @@ -43,7 +42,7 @@ public class Compiler this.wyrd_world = wyrd_world; macro_manager = new MacroManager(); - anonymous_variables = new AnonymousVariableManager(); + registers = new RegisterManager(); assembler = new InstructionManager(); } @@ -74,7 +73,7 @@ public class Compiler } } - protected void compile_variables + protected void compile_registers ( final tonkadur.fate.v1.lang.World fate_world ) @@ -82,11 +81,11 @@ public class Compiler { for ( - final tonkadur.fate.v1.lang.Variable variable: - fate_world.variables().get_all() + final tonkadur.fate.v1.lang.Register register: + fate_world.registers().get_all() ) { - VariableCompiler.compile(this, variable); + RegisterCompiler.compile(this, register); } } @@ -119,12 +118,17 @@ public class Compiler ); } - protected void add_anonymous_variables () + protected void add_registers () throws Throwable { - for (final Variable variable: anonymous_variables.get_all_variables()) + for (final DictType type: registers.get_context_structures_types()) { - wyrd_world.add_variable(variable); + wyrd_world.add_dict_type(type); + } + + for (final Register register: registers.get_base_registers()) + { + wyrd_world.add_register(register); } } @@ -133,9 +137,9 @@ public class Compiler return wyrd_world; } - public AnonymousVariableManager anonymous_variables () + public RegisterManager registers () { - return anonymous_variables; + return registers; } public InstructionManager assembler () 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 7a9d223..afd53c2 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 @@ -33,18 +33,18 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor { protected final Compiler compiler; protected final List init_instructions; - protected final List reserved_variables; + protected final Collection reserved_registers; protected boolean instructions_were_generated; protected Computation result_as_computation; - protected Ref result_as_ref; + protected Address result_as_address; public ComputationCompiler (final Compiler compiler) { this.compiler = compiler; - reserved_variables = new ArrayList(); + reserved_registers = new ArrayList(); init_instructions = new ArrayList(); - result_as_ref = null; + result_as_address = null; result_as_computation = null; instructions_were_generated = false; } @@ -74,69 +74,68 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor } else { - result_as_computation = new ValueOf(result_as_ref); + result_as_computation = new ValueOf(result_as_address); return result_as_computation; } } - public Ref get_ref () + public Address get_address () { - if (result_as_ref == null) + if (result_as_address == null) { System.err.println("[P] Missing generate_ref()!"); } - return result_as_ref; + return result_as_address; } - public void generate_ref () + public void generate_address () { - if ((!instructions_were_generated) && (result_as_ref == null)) + if ((!instructions_were_generated) && (result_as_address == null)) { - final Ref result; + final Address result; - result = reserve(result_as_computation.get_type()); + result = reserve(result_as_computation.get_type()).get_address(); init_instructions.add ( new SetValue(result, result_as_computation) ); - - result_as_ref = result; + result_as_address = result; } } - public void release_variables () + public void release_registers () { - for (final Ref ref: reserved_variables) + for (final Register reg: reserved_registers) { - compiler.anonymous_variables().release(ref); + compiler.registers().release(reg); } } protected void assimilate (final ComputationCompiler cc) { init_instructions.addAll(cc.init_instructions); - reserved_variables.addAll(cc.reserved_variables); + reserved_registers.addAll(cc.reserved_registers); } - protected Ref reserve (final Type t) + protected Register reserve (final Type t) { - final Ref result; + final Register result; - result = compiler.anonymous_variables().reserve(t); + result = compiler.registers().reserve(t); - reserved_variables.add(result); + reserved_registers.add(result); return result; } @Override - public void visit_at_reference + public void visit_at_address ( - final tonkadur.fate.v1.lang.computation.AtReference n + final tonkadur.fate.v1.lang.computation.AtAddress n ) throws Throwable { @@ -148,17 +147,17 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor assimilate(n_cc); - result_as_ref = - new Ref + result_as_address = + new Address ( n_cc.get_computation(), TypeCompiler.compile ( compiler, ( - (tonkadur.fate.v1.lang.type.RefType) + (tonkadur.fate.v1.lang.type.AddressType) n.get_parent().get_type() - ).get_referenced_type() + ).get_addressd_type() ) ); } @@ -225,8 +224,8 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor is_safe = is_safe && !cond_cc.has_init() && !val_cc.has_init(); - reserved_variables.addAll(cond_cc.reserved_variables); - reserved_variables.addAll(val_cc.reserved_variables); + reserved_registers.addAll(cond_cc.reserved_registers); + reserved_registers.addAll(val_cc.reserved_registers); cc_list.add(new Cons(cond_cc, val_cc)); } @@ -259,7 +258,8 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor else { final Iterator> it; - final Ref result; + final Register result_register; + final Address result_address; Cons next; List new_value, new_cond; Instruction prev_branch; @@ -269,6 +269,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor next = it.next(); result = reserve(next.get_cdr().get_computation().get_type()); + result_as_address = result.get_address(); new_value = new ArrayList(); @@ -277,7 +278,10 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor new_value.add(next.get_cdr().get_init()); } - new_value.add(new SetValue(result, next.get_cdr().get_computation())); + new_value.add + ( + new SetValue(result_as_address, next.get_cdr().get_computation()) + ); prev_branch = compiler.assembler().merge(new_value); @@ -300,14 +304,14 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor new_value.add ( - new SetValue(result, next.get_cdr().get_computation()) + new SetValue(result_as_address, next.get_cdr().get_computation()) ); new_cond.add ( IfElse.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), next.get_car().get_computation(), compiler.assembler().merge(new_value), @@ -320,7 +324,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor init_instructions.add(prev_branch); - result_as_computation = new ValueOf(result); + result_as_computation = result.get_value(); } } @@ -354,7 +358,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor n.get_collection().get_visited_by(collection_compiler); n.get_element().get_visited_by(element_compiler); - collection_compiler.generate_ref(); + collection_compiler.generate_address(); assimilate(collection_compiler); assimilate(element_compiler); @@ -367,7 +371,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ).is_set() ) { - final Ref was_found, index, element; + final Register was_found, index, element; was_found = reserve(Type.BOOLEAN); index = reserve(Type.INT); @@ -375,62 +379,72 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor init_instructions.add ( - new SetValue(element, element_compiler.get_computation()) + new SetValue + ( + element.get_address(), + element_compiler.get_computation() + ) ); init_instructions.add ( BinarySearch.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - new ValueOf(element), - new Size(collection_compiler.get_ref()), - collection_compiler.get_ref(), - was_found, - index + element.get_value(), + new Size(collection_compiler.get_address()), + collection_compiler.get_address(), + was_found.get_address(), + index.get_address() ) ); result_as_computation = new IfElseComputation ( - new ValueOf(was_found), + was_found.get_value(), Constant.ONE, Constant.ZERO ); } else { - final Ref element; + final Register result, element; + + result = reserve(Type.INT); - result_as_ref = reserve(Type.INT); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); element = reserve(element_compiler.get_computation().get_type()); init_instructions.add ( - new SetValue(element, element_compiler.get_computation()) + new SetValue + ( + element.get_address(), + element_compiler.get_computation() + ) ); init_instructions.add ( CountOccurrences.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - new ValueOf(element), - new Size(collection_compiler.get_ref()), - collection_compiler.get_ref(), - result_as_ref + element.get_value(), + new Size(collection_compiler.get_address()), + collection_compiler.get_address(), + result_as_address ) ); - result_as_computation = new ValueOf(result_as_ref); } } @Override - public void visit_field_reference + public void visit_field_address ( - final tonkadur.fate.v1.lang.computation.FieldReference n + final tonkadur.fate.v1.lang.computation.FieldAddress n ) throws Throwable { @@ -442,10 +456,10 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor assimilate(n_cc); - result_as_ref = - new RelativeRef + result_as_address = + new RelativeAddress ( - n_cc.get_ref(), + n_cc.get_address(), new Constant(Type.STRING, n.get_field_name()), TypeCompiler.compile ( @@ -487,10 +501,10 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor * on the interpreter. * * Instead, we just convert the ifelse into an instruction-based - * equivalent and store the result in an anonymous variable to be used + * equivalent and store the result in an anonymous register to be used * here. */ - final Ref if_else_result; + final Register if_else_result; final List if_true_branch; final List if_false_branch; @@ -511,12 +525,20 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor if_true_branch.add ( - new SetValue(if_else_result, if_true_cc.get_computation()) + new SetValue + ( + if_else_result.get_address(), + if_true_cc.get_computation() + ) ); if_false_branch.add ( - new SetValue(if_else_result, if_false_cc.get_computation()) + new SetValue + ( + if_else_result.get_addresss(), + if_false_cc.get_computation() + ) ); if (cond_cc.has_init()) @@ -528,7 +550,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ( IfElse.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), cond_cc.get_computation(), compiler.assembler().merge(if_true_branch), @@ -536,11 +558,12 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ) ); - reserved_variables.addAll(cond_cc.reserved_variables); - reserved_variables.addAll(if_true_cc.reserved_variables); - reserved_variables.addAll(if_false_cc.reserved_variables); + reserved_registers.addAll(cond_cc.reserved_registers); + reserved_registers.addAll(if_true_cc.reserved_registers); + reserved_registers.addAll(if_false_cc.reserved_registers); - result_as_computation = new ValueOf(if_else_result); + result_as_computation = if_else_result.get_value(); + result_as_address = if_else_result.get_address(); } else { @@ -565,6 +588,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ) throws Throwable { + final Register result; final ComputationCompiler collection_compiler, element_compiler; collection_compiler = new ComputationCompiler(compiler); @@ -578,7 +602,9 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor assimilate(collection_compiler); assimilate(element_compiler); - result_as_ref = reserve(Type.BOOLEAN); + result = reserve(Type.BOOLEAN); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); if ( @@ -588,95 +614,63 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ).is_set() ) { - final Ref index, element; + final Register index, element; index = reserve(Type.INT); element = reserve(element_compiler.get_computation().get_type()); init_instructions.add ( - new SetValue(element, element_compiler.get_computation()) + new SetValue + ( + element.get_address(), + element_compiler.get_computation() + ) ); init_instructions.add ( BinarySearch.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - new ValueOf(element), - new Size(collection_compiler.get_ref()), - collection_compiler.get_ref(), - result_as_ref, - index + element.get_value(), + new Size(collection_compiler.get_address()), + collection_compiler.get_address(), + result_as_address, + index.get_address(); ) ); - result_as_computation = new ValueOf(result_as_ref); } else { - final Ref index, element; + final Register index, element; index = reserve(Type.INT); element = reserve(element_compiler.get_computation().get_type()); - result_as_ref = reserve(Type.BOOLEAN); - init_instructions.add ( - new SetValue(element, element_compiler.get_computation()) + new SetValue + ( + element.get_address(), + element_compiler.get_computation() + ) ); init_instructions.add ( IterativeSearch.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - new ValueOf(element), - new Size(collection_compiler.get_ref()), - collection_compiler.get_ref(), - result_as_ref, - index + element.get_value(), + new Size(collection_compiler.get_address()), + collection_compiler.get_address(), + result_as_address, + index.get_address() ) ); - - result_as_computation = new ValueOf(result_as_ref); - } - } - - @Override - public void visit_macro_value_call - ( - final tonkadur.fate.v1.lang.computation.MacroValueCall n - ) - throws Throwable - { - final List parameters; - - parameters = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation fate_computation: - n.get_parameters() - ) - { - final ComputationCompiler cc; - - cc = new ComputationCompiler(compiler); - - fate_computation.get_visited_by(cc); - - cc.generate_ref(); - - assimilate(cc); - - parameters.add(cc.get_ref()); } - - compiler.macros().push(n.get_macro(), parameters); - n.get_actual_value_node().get_visited_by(this); - compiler.macros().pop(); } @Override @@ -789,13 +783,16 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ) ) { - final Ref candidate; - final Computation value_of_candidate; + final Register candidate; candidate = reserve(operands.get(0).get_type()); - value_of_candidate = new ValueOf(candidate); + result_as_address = candidate.get_address(); + result_as_computation = candidate.get_value(); - init_instructions.add(new SetValue(candidate, operands.get(0))); + init_instructions.add + ( + new SetValue(result_as_address, operands.get(0)) + ); for (final Computation operand: operands) { @@ -803,18 +800,16 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ( new SetValue ( - candidate, + result_as_address, new IfElseComputation ( - Operation.less_than(value_of_candidate, operand), - value_of_candidate, + Operation.less_than(result_as_computation, operand), + result_as_computation, operand ) ) ); } - - result_as_computation = value_of_candidate; } else if ( @@ -824,13 +819,16 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ) ) { - final Ref candidate; - final Computation value_of_candidate; + final Register candidate; candidate = reserve(operands.get(0).get_type()); - value_of_candidate = new ValueOf(candidate); + result_as_address = candidate.get_address(); + result_as_computation = candidate.get_value(); - init_instructions.add(new SetValue(candidate, operands.get(0))); + init_instructions.add + ( + new SetValue(result_as_address, operands.get(0)) + ); for (final Computation operand: operands) { @@ -838,18 +836,16 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ( new SetValue ( - candidate, + result_as_address, new IfElseComputation ( - Operation.greater_than(value_of_candidate, operand), - value_of_candidate, + Operation.greater_than(result_as_computation, operand), + result_as_computation, operand ) ) ); } - - result_as_computation = value_of_candidate; } else if ( @@ -889,18 +885,19 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ) { final Type t; - final Ref candidate; - final Computation value_of_candidate; + final Register candidate; + final Computation result_as_computation; t = operands.get(2).get_type(); candidate = reserve(t); - value_of_candidate = new ValueOf(candidate); + result_as_address = candidate.get_address(); + result_as_computation = candidate.get_value(); init_instructions.add ( new SetValue ( - candidate, + result_as_address, new IfElseComputation ( Operation.greater_than(operands.get(2), operands.get(1)), @@ -913,17 +910,15 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ( new SetValue ( - candidate, + result_as_address, new IfElseComputation ( - Operation.less_than(value_of_candidate, operands.get(0)), + Operation.less_than(result_as_computation, operands.get(0)), operands.get(0), - value_of_candidate + result_as_computation ) ) ); - - result_as_computation = value_of_candidate; } else if ( @@ -1105,6 +1100,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor System.err.println("[P] Unknown Fate operator '" + fate_op_name+ "'."); } } + @Override public void visit_size_operator ( @@ -1120,7 +1116,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor assimilate(cc); - result_as_computation = new Size(cc.get_ref()); + result_as_computation = new Size(cc.get_address()); } @Override @@ -1131,11 +1127,14 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor throws Throwable { final ComputationCompiler elem_cc, collection_cc; - final Ref result, result_found; + final Register result, result_found; result = reserve(Type.INT); result_found = reserve(Type.BOOLEAN); + result_as_address = result.get_address(); + result_as_computation = result.get_value(); + elem_cc = new ComputationCompiler(compiler); collection_cc = new ComputationCompiler(compiler); @@ -1149,17 +1148,15 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ( IterativeSearch.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), elem_cc.get_computation(), - new Size(collection_cc.get_ref()), - collection_cc.get_ref(), - result_found, - result + new Size(collection_cc.get_address()), + collection_cc.get_address(), + result_found.get_address(), + result_as_address ) ); - - result_as_ref = result; } @Override @@ -1180,22 +1177,22 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ) throws Throwable { - final ComputationCompiler i_cc, n_cc; + final ComputationCompiler extra_address_cc, base_address_cc; - n_cc = new ComputationCompiler(compiler); - i_cc = new ComputationCompiler(compiler); + base_address_cc = new ComputationCompiler(compiler); + extra_address_cc = new ComputationCompiler(compiler); - n.get_parent().get_visited_by(n_cc); - n.get_index().get_visited_by(i_cc); + n.get_parent().get_visited_by(base_address_cc); + n.get_index().get_visited_by(extra_address_cc); - assimilate(n_cc); - assimilate(i_cc); + assimilate(base_address_cc); + assimilate(extra_address_cc); - result_as_ref = - new RelativeRef + result_as_address = + new RelativeAddress ( - n_cc.get_ref(), - new Cast(i_cc.get_computation(), Type.STRING), + base_address_cc.get_address(), + new Cast(extra_address_cc.get_computation(), Type.STRING), TypeCompiler.compile ( compiler, @@ -1249,19 +1246,9 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor } @Override - public void visit_parameter_reference - ( - final tonkadur.fate.v1.lang.computation.ParameterReference n - ) - throws Throwable - { - result_as_ref = compiler.macros().get_parameter_ref(n.get_name()); - } - - @Override - public void visit_ref_operator + public void visit_address_operator ( - final tonkadur.fate.v1.lang.computation.RefOperator n + final tonkadur.fate.v1.lang.computation.AddressOperator n ) throws Throwable { @@ -1273,7 +1260,7 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor assimilate(n_cc); - result_as_computation = n_cc.result_as_ref; + result_as_computation = n_cc.get_address() } @Override @@ -1339,15 +1326,41 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor } @Override + public void visit_lambda_expression + ( + final tonkadur.fate.v1.lang.computation.LambdaExpression n + ) + throws Throwable + { + /* TODO */ + } + + @Override + public void visit_lambda_evaluation + ( + final tonkadur.fate.v1.lang.computation.LambdaEvaluation n + ) + throws Throwable + { + /* TODO */ + } + + @Override public void visit_variable_reference ( final tonkadur.fate.v1.lang.computation.VariableReference n ) throws Throwable { - result_as_ref = - compiler.world().get_variable(n.get_variable().get_name()).get_ref(); + final Register register; + + register = + compiler.registers().get_register + ( + n.get_variable().get_name() + ); - result_as_computation = new ValueOf(result_as_ref); + result_as_address = register.get_address(); + result_as_computation = register.get_value(); } } 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 3bbde05..f088c1d 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 @@ -19,8 +19,8 @@ import tonkadur.wyrd.v1.lang.type.MapType; import tonkadur.wyrd.v1.lang.computation.Cast; import tonkadur.wyrd.v1.lang.computation.Constant; import tonkadur.wyrd.v1.lang.computation.Operation; -import tonkadur.wyrd.v1.lang.computation.Ref; -import tonkadur.wyrd.v1.lang.computation.RelativeRef; +import tonkadur.wyrd.v1.lang.computation.Address; +import tonkadur.wyrd.v1.lang.computation.RelativeAddress; import tonkadur.wyrd.v1.lang.computation.Size; import tonkadur.wyrd.v1.lang.computation.ValueOf; @@ -34,7 +34,6 @@ import tonkadur.wyrd.v1.lang.instruction.ResolveChoices; import tonkadur.wyrd.v1.lang.instruction.SetPC; import tonkadur.wyrd.v1.lang.instruction.SetValue; -import tonkadur.wyrd.v1.compiler.util.AnonymousVariableManager; import tonkadur.wyrd.v1.compiler.util.BinarySearch; import tonkadur.wyrd.v1.compiler.util.InsertAt; import tonkadur.wyrd.v1.compiler.util.If; @@ -85,58 +84,54 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor * * ) */ - final ComputationCompiler element_compiler, reference_compiler; - final Ref element, collection, collection_size; - final Ref element_found, element_index; + final ComputationCompiler element_compiler, address_compiler; + final Address element, collection; + final Register collection_size, element_found, element_index; final Type element_type; - final Computation value_of_element, value_of_collection_size; element_compiler = new ComputationCompiler(compiler); ae.get_element().get_visited_by(element_compiler); + element_compiler.generate_address(); + if (element_compiler.has_init()) { result.add(element_compiler.get_init()); } element_type = element_compiler.get_computation().get_type(); - element = compiler.anonymous_variables().reserve(element_type); - result.add(new SetValue(element, element_compiler.get_computation())); - element_compiler.release_variables(); + element = element_compiler.get_address(); - reference_compiler = new ComputationCompiler(compiler); - ae.get_collection().get_visited_by(reference_compiler); + address_compiler = new ComputationCompiler(compiler); + ae.get_collection().get_visited_by(address_compiler); - if (reference_compiler.has_init()) + if (address_compiler.has_init()) { - result.add(reference_compiler.get_init()); + result.add(address_compiler.get_init()); } - collection = reference_compiler.get_ref(); - - element_found = compiler.anonymous_variables().reserve(Type.BOOLEAN); - element_index = compiler.anonymous_variables().reserve(Type.INT); - collection_size = compiler.anonymous_variables().reserve(Type.INT); + collection = address_compiler.get_address(); - value_of_element = new ValueOf(element); - value_of_collection_size = new ValueOf(collection_size); + element_found = compiler.registers().reserve(Type.BOOLEAN); + element_index = compiler.registers().reserve(Type.INT); + collection_size = compiler.registers().reserve(Type.INT); result.add ( - new SetValue(collection_size, new Size(collection)) + new SetValue(collection_size.get_address(), new Size(collection)) ); result.add ( BinarySearch.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - value_of_element, - value_of_collection_size, + new ValueOf(element), + collection_size.get_value(), collection, - element_found, - element_index + element_found.get_address(), + element_index.get_address() ) ); @@ -144,27 +139,27 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( If.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - Operation.not(new ValueOf(element_found)), + Operation.not(element_found.get_value()), InsertAt.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - element_index, - value_of_element, - value_of_collection_size, + element_index.get_address(), + new ValueOf(element), + collection_size.get_value(), collection ) ) ); - compiler.anonymous_variables().release(element); - compiler.anonymous_variables().release(element_found); - compiler.anonymous_variables().release(element_index); - compiler.anonymous_variables().release(collection_size); + compiler.registers().release(element_found); + compiler.registers().release(element_index); + compiler.registers().release(collection_size); - reference_compiler.release_variables(); + element_compiler.release_variables(); + address_compiler.release_variables(); } protected void add_element_to_list @@ -179,25 +174,25 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor * * Wyrd: * (set - * (relative_ref collection ( (cast int string (size collection)) )) + * (relative_address collection ( (cast int string (size collection)) )) * (element) * ) */ - final Ref collection_as_ref; - final ComputationCompiler element_compiler, reference_compiler; + final Address collection_as_address; + final ComputationCompiler element_compiler, address_compiler; element_compiler = new ComputationCompiler(compiler); ae.get_element().get_visited_by(element_compiler); - reference_compiler = new ComputationCompiler(compiler); + address_compiler = new ComputationCompiler(compiler); - ae.get_collection().get_visited_by(reference_compiler); + ae.get_collection().get_visited_by(address_compiler); - if (reference_compiler.has_init()) + if (address_compiler.has_init()) { - result.add(reference_compiler.get_init()); + result.add(address_compiler.get_init()); } if (element_compiler.has_init()) @@ -205,18 +200,18 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor result.add(element_compiler.get_init()); } - collection_as_ref = reference_compiler.get_ref(); + collection_as_address = address_compiler.get_address(); result.add ( new SetValue ( - new RelativeRef + new RelativeAddress ( - collection_as_ref, + collection_as_address, new Cast ( - new Size(collection_as_ref), + new Size(collection_as_address), Type.STRING ), element_compiler.get_computation().get_type() @@ -225,11 +220,21 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ) ); - reference_compiler.release_variables(); + address_compiler.release_variables(); element_compiler.release_variables(); } @Override + public void visit_local_variable + ( + final tonkadur.fate.v1.lang.instruction.LocalVariable n + ) + throws Throwable + { + /* TODO */ + } + + @Override public void visit_add_element ( final tonkadur.fate.v1.lang.instruction.AddElement ae @@ -314,32 +319,32 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor * * Wyrd: */ - final ComputationCompiler reference_compiler; - final Ref collection_ref; + final ComputationCompiler address_compiler; + final Address collection_address; - reference_compiler = new ComputationCompiler(compiler); + address_compiler = new ComputationCompiler(compiler); - c.get_collection().get_visited_by(reference_compiler); + c.get_collection().get_visited_by(address_compiler); - if (reference_compiler.has_init()) + if (address_compiler.has_init()) { - result.add(reference_compiler.get_init()); + result.add(address_compiler.get_init()); } - collection_ref = reference_compiler.get_ref(); + collection_address = address_compiler.get_address(); result.add ( Clear.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - new Size(collection_ref), - collection_ref + new Size(collection_address), + collection_address ) ); - reference_compiler.release_variables(); + address_compiler.release_variables(); } public void visit_reverse_list @@ -353,32 +358,32 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor * * Wyrd: */ - final ComputationCompiler reference_compiler; - final Ref collection_ref; + final ComputationCompiler address_compiler; + final Address collection_address; - reference_compiler = new ComputationCompiler(compiler); + address_compiler = new ComputationCompiler(compiler); - n.get_collection().get_visited_by(reference_compiler); + n.get_collection().get_visited_by(address_compiler); - if (reference_compiler.has_init()) + if (address_compiler.has_init()) { - result.add(reference_compiler.get_init()); + result.add(address_compiler.get_init()); } - collection_ref = reference_compiler.get_ref(); + collection_address = address_compiler.get_address(); result.add ( ReverseList.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - new Size(collection_ref), - collection_ref + new Size(collection_address), + collection_address ) ); - reference_compiler.release_variables(); + address_compiler.release_variables(); } @Override @@ -413,7 +418,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor * > * > */ - Ref anon; + Address anon; final List < Cons @@ -442,14 +447,14 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor n.get_target().get_visited_by(target_cc); - target_cc.generate_ref(); + target_cc.generate_address(); if (target_cc.has_init()) { result.add(target_cc.get_init()); } - anon = target_cc.get_ref(); + anon = target_cc.get_address(); value_of_anon = new ValueOf(anon); for @@ -481,7 +486,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( IfElse.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), Operation.equals(value_of_anon, cc.get_computation()), ic.get_result(), @@ -527,26 +532,26 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor throws Throwable { final ComputationCompiler cc; - final Ref target; + final Address target; cc = new ComputationCompiler(compiler); - n.get_reference().get_visited_by(cc); + n.get_address().get_visited_by(cc); if (cc.has_init()) { result.add(cc.get_init()); } - target = cc.get_ref(); + target = cc.get_address(); if (target == null) { System.err.println ( "[P] Argument in (free " - + n.get_reference() - + ") did not compile to a reference." + + n.get_address() + + ") did not compile to a address." ); } @@ -591,7 +596,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( While.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), cc.get_init(), cc.get_computation(), @@ -609,7 +614,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( While.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), cc.get_computation(), compiler.assembler().merge(body) @@ -661,13 +666,13 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( While.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), compiler.assembler().merge(pre_cond_instructions), cc.get_computation(), NOP.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler() ) ), @@ -724,7 +729,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( While.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), cc.get_init(), cc.get_computation(), @@ -742,7 +747,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( While.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), cc.get_computation(), compiler.assembler().merge(body) @@ -763,9 +768,8 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor final String end_of_loop_label; final ComputationCompiler cc; final List new_body; - final Ref index, current_value, collection_size; - final Ref collection; - final Computation value_of_index; + final Register index, collection_size, current_value; + final Address collection; final Type member_type; /* @@ -782,8 +786,8 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor cc = new ComputationCompiler(compiler); new_body = new ArrayList(); - index = compiler.anonymous_variables().reserve(Type.INT); - collection_size = compiler.anonymous_variables().reserve(Type.INT); + index = compiler.registers().reserve(Type.INT); + collection_size = compiler.registers().reserve(Type.INT); result.add(new SetValue(index, Constant.ZERO)); @@ -794,36 +798,33 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor result.add(cc.get_init()); } - collection = cc.get_ref(); + collection = cc.get_address(); - result.add(new SetValue(collection_size, new Size(collection))); - - value_of_index = new ValueOf(index); + result.add + ( + new SetValue(collection_size.get_address(), new Size(collection)) + ); member_type = ((MapType) collection.get_target_type()).get_member_type(); - current_value = compiler.anonymous_variables().reserve(member_type); + current_value = compiler.registers().reserve(member_type); end_of_loop_label = compiler.assembler().generate_label(""); compiler.assembler().push_context_label("breakable", end_of_loop_label); - compiler.macros().add_wild_parameter - ( - n.get_parameter_name(), - current_value - ); + compiler.registers().bind(current_value, n.get_parameter_name()); new_body.add ( new SetValue ( - current_value, + current_value.get_address(), new ValueOf ( - new RelativeRef + new RelativeAddress ( collection, - new Cast(value_of_index, Type.STRING), + new Cast(index.get_value(), Type.STRING), member_type ) ) @@ -846,7 +847,11 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor new_body.add ( - new SetValue(index, Operation.plus(Constant.ONE, value_of_index)) + new SetValue + ( + index.get_address(), + Operation.plus(Constant.ONE, index.get_value()) + ) ); result.add @@ -855,24 +860,24 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( While.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), Operation.less_than ( - value_of_index, - new ValueOf(collection_size) + index.get_value(), + collection_size.get_value() ), compiler.assembler.merge(new_body) ), end_of_loop_label ) ); - compiler.macros().remove_wild_parameter(n.get_parameter_name()); + compiler.registers().unbind(n.get_parameter_name()); compiler.assembler().pop_context_label("breakable"); - compiler.anonymous_variables().release(index); - compiler.anonymous_variables().release(current_value); - compiler.anonymous_variables().release(collection_size); + compiler.registers().release(index); + compiler.registers().release(current_value); + compiler.registers().release(collection_size); } @Override @@ -883,18 +888,18 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor throws Throwable { final ComputationCompiler index_cc, collection_cc; - final Ref collection, collection_size; - final Computation value_of_collection_size; + final Address collection; + final Register collection_size; index_cc = new ComputationCompiler(compiler); collection_cc = new ComputationCompiler(compiler); - collection_size = compiler.anonymous_variables().reserve(Type.INT); + collection_size = compiler.registers().reserve(Type.INT); n.get_index().get_visited_by(index_cc); n.get_collection().get_visited_by(collection_cc); - index_cc.generate_ref(); + index_cc.generate_address(); if (index_cc.has_init()) { @@ -906,25 +911,26 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor result.add(collection_cc.get_init()); } - collection = collection_cc.get_ref(); - - value_of_collection_size = new ValueOf(collection_size); + collection = collection_cc.get_address(); - result.add(new SetValue(collection_size, new Size(collection))); + result.add + ( + new SetValue(collection_size.get_address(), new Size(collection)) + ); result.add ( RemoveAt.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - index_cc.get_ref(), - value_of_collection_size, + index_cc.get_address(), + collection_size.get_value(), collection ) ); - compiler.anonymous_variables().release(collection_size); + compiler.registers().release(collection_size); index_cc.release_variables(); collection_cc.release_variables(); @@ -980,7 +986,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( NOP.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler() ) ); @@ -1014,7 +1020,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( IfElse.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), cc.get_computation(), ic.get_result(), @@ -1111,7 +1117,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( IfElse.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), cc.get_computation(), if_true_ic.get_result(), @@ -1152,7 +1158,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( If.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), cc.get_computation(), if_true_ic.get_result() @@ -1191,54 +1197,13 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor } @Override - public void visit_macro_call + public void visit_done ( - final tonkadur.fate.v1.lang.instruction.MacroCall n + final tonkadur.fate.v1.lang.instruction.Done n ) throws Throwable { - /* - * Fate: (macro c0 ... cn) - * - * Wyrd - */ - final List cc_list; - final List parameters; - - cc_list = new ArrayList(); - parameters = new ArrayList(); - - for - ( - final tonkadur.fate.v1.lang.meta.Computation fate_computation: - n.get_parameters() - ) - { - final ComputationCompiler cc; - - cc = new ComputationCompiler(compiler); - - fate_computation.get_visited_by(cc); - - cc.generate_ref(); - - if (cc.has_init()) - { - result.add(cc.get_init()); - } - - cc_list.add(cc); - parameters.add(cc.get_ref()); - } - - compiler.macros().push(n.get_macro(), parameters); - n.get_macro().get_root().get_visited_by(this); - compiler.macros().pop(); - - for (final ComputationCompiler cc: cc_list) - { - cc.release_variables(); - } + /* TODO */ } @Override @@ -1421,22 +1386,18 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor * > */ final ComputationCompiler elem_cc, collection_cc; - final Ref elem, collection_size, collection; + final Register collection_size; + final Address elem, collection; elem_cc = new ComputationCompiler(compiler); collection_cc = new ComputationCompiler(compiler); - collection_size = compiler.anonymous_variables().reserve(Type.INT); + collection_size = compiler.registers().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() - ); - + elem_cc.generate_address(); if (elem_cc.has_init()) { @@ -1448,13 +1409,10 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor result.add(collection_cc.get_init()); } - collection = collection_cc.get_ref(); + collection = collection_cc.get_address(); - result.add(new SetValue(elem, elem_cc.get_computation())); result.add(new SetValue(collection_size, new Size(collection))); - elem_cc.release_variables(); - if ( ( @@ -1464,10 +1422,10 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ) { final Computation value_of_elem, value_of_collection_size; - final Ref index, found; + final Register index, found; - index = compiler.anonymous_variables().reserve(Type.INT); - found = compiler.anonymous_variables().reserve(Type.BOOLEAN); + index = compiler.registers().reserve(Type.INT); + found = compiler.registers().reserve(Type.BOOLEAN); value_of_elem = new ValueOf(elem); value_of_collection_size = new ValueOf(collection_size); @@ -1476,36 +1434,38 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( BinarySearch.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - value_of_elem, + new ValueOf(elem), value_of_collection_size, collection, - found, - index + found.get_address(), + index.get_address() ) ); + elem_cc.release_variables(); + result.add ( If.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - new ValueOf(found), + found.get_value(), RemoveAt.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - index, + index.get_address(), value_of_collection_size, collection ) ) ); - compiler.anonymous_variables().release(index); - compiler.anonymous_variables().release(found); + compiler.registers().release(index); + compiler.registers().release(found); } else { @@ -1513,19 +1473,20 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( RemoveAllOf.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), new ValueOf(elem), new ValueOf(collection_size), collection ) ); + + elem_cc.release_variables(); } collection_cc.release_variables(); - compiler.anonymous_variables().release(elem); - compiler.anonymous_variables().release(collection_size); + compiler.registers().release(collection_size); } @Override @@ -1573,26 +1534,21 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor * ) */ final ComputationCompiler elem_cc, collection_cc; - final Ref elem, collection_size, found, index; - final Ref collection; + final Register collection_size, found, index; + final Address elem, collection; final Computation value_of_collection_size; elem_cc = new ComputationCompiler(compiler); collection_cc = new ComputationCompiler(compiler); - collection_size = compiler.anonymous_variables().reserve(Type.INT); - found = compiler.anonymous_variables().reserve(Type.BOOLEAN); - index = compiler.anonymous_variables().reserve(Type.INT); + collection_size = compiler.registers().reserve(Type.INT); + found = compiler.registers().reserve(Type.BOOLEAN); + index = compiler.registers().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() - ); - + elem_cc.generate_address(); if (elem_cc.has_init()) { @@ -1604,7 +1560,8 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor result.add(collection_cc.get_init()); } - collection = collection_cc.get_ref(); + elem = elem_cc.get_address(); + collection = collection_cc.get_address(); value_of_collection_size = new ValueOf(collection_size); @@ -1625,13 +1582,13 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( BinarySearch.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), new ValueOf(elem), value_of_collection_size, collection, - found, - index + found.get_address(), + index.get_address() ) ); } @@ -1641,40 +1598,40 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ( IterativeSearch.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), new ValueOf(elem), value_of_collection_size, collection, - found, - index + found.get_address(), + index.get_address() ) ); } - compiler.anonymous_variables().release(elem); + compiler.registers().release(elem); result.add ( If.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), new ValueOf(found), RemoveAt.generate ( - compiler.anonymous_variables(), + compiler.registers(), compiler.assembler(), - index, + index.get_address(), value_of_collection_size, collection ) ) ); - compiler.anonymous_variables().release(index); - compiler.anonymous_variables().release(found); - compiler.anonymous_variables().release(collection_size); + compiler.registers().release(index); + compiler.registers().release(found); + compiler.registers().release(collection_size); collection_cc.release_variables(); } @@ -1686,6 +1643,16 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor ) throws Throwable { + /* TODO */ + } + + @Override + public void visit_sequence_jump + ( + final tonkadur.fate.v1.lang.instruction.SequenceJump n + ) + throws Throwable + { /* * Fate: (sequence_call string) * Wyrd: (set_pc