summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-08-27 22:11:01 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-08-27 22:11:01 +0200
commit84c040f10ddfc66d75ce7e7459f4c92e1592238b (patch)
treef8aecc690fad772d9b9f324032f0f6786f468826
parent6aec0ce0fb005457b53633b9599998117a1003cf (diff)
Adds the code for the Let computation.
Still not in the parser, though.
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/computation/Let.java89
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java3
-rw-r--r--src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java53
3 files changed, 145 insertions, 0 deletions
diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/Let.java b/src/core/src/tonkadur/fate/v1/lang/computation/Let.java
new file mode 100644
index 0000000..f5be874
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/computation/Let.java
@@ -0,0 +1,89 @@
+package tonkadur.fate.v1.lang.computation;
+
+import java.util.List;
+
+import tonkadur.functional.Cons;
+
+import tonkadur.parser.Origin;
+
+import tonkadur.fate.v1.lang.meta.ComputationVisitor;
+import tonkadur.fate.v1.lang.meta.Computation;
+
+import tonkadur.fate.v1.lang.Variable;
+
+import tonkadur.fate.v1.lang.type.Type;
+
+public class Let extends Computation
+{
+ /***************************************************************************/
+ /**** MEMBERS **************************************************************/
+ /***************************************************************************/
+ protected final Computation computation;
+ protected final List<Cons<Variable, Computation>> assignments;
+
+ /***************************************************************************/
+ /**** PROTECTED ************************************************************/
+ /***************************************************************************/
+
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ public Let
+ (
+ final Origin origin,
+ final List<Cons<Variable, Computation>> assignments,
+ final Computation computation
+ )
+ {
+ super(origin, computation.get_type());
+
+ this.computation = computation;
+ this.assignments = assignments;
+ }
+
+ /**** Accessors ************************************************************/
+ @Override
+ public void get_visited_by (final ComputationVisitor cv)
+ throws Throwable
+ {
+ cv.visit_let(this);
+ }
+
+ public Computation get_computation ()
+ {
+ return computation;
+ }
+
+ public List<Cons<Variable, Computation>> get_assignments ()
+ {
+ return assignments;
+ }
+
+ /**** Misc. ****************************************************************/
+ @Override
+ public String toString ()
+ {
+ final StringBuilder sb = new StringBuilder();
+
+ sb.append("(Let (");
+
+ for (final Cons<Variable, Computation> assignment: assignments)
+ {
+ sb.append("(");
+ sb.append(assignment.get_car().get_type());
+ sb.append(" ");
+ sb.append(assignment.get_car().get_name());
+ sb.append(" ");
+ sb.append(assignment.get_cdr().toString());
+ sb.append(")");
+ }
+ sb.append(") ");
+
+ sb.append(computation.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 32fcee7..09b9135 100644
--- a/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java
+++ b/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java
@@ -49,6 +49,9 @@ public interface ComputationVisitor
public void visit_lambda_evaluation (final LambdaEvaluation n)
throws Throwable;
+ public void visit_let (final Let n)
+ throws Throwable;
+
public void visit_newline (final Newline n)
throws Throwable;
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 5a06201..94e013d 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
@@ -1643,6 +1643,59 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor
}
@Override
+ public void visit_let
+ (
+ final tonkadur.fate.v1.lang.computation.Let n
+ )
+ throws Throwable
+ {
+ final Collection<String> names;
+
+ names = new ArrayList<String>();
+
+ for
+ (
+ final
+ Cons
+ <
+ tonkadur.fate.v1.lang.Variable,
+ tonkadur.fate.v1.lang.meta.Computation
+ >
+ a:
+ n.get_assignments()
+ )
+ {
+ final String name;
+ final Register r;
+ final ComputationCompiler cc;
+
+ name = a.get_car().get_name();
+ r = reserve(TypeCompiler.compile(compiler, a.get_car().get_type()));
+
+ compiler.registers().bind(name, r);
+ names.add(name);
+
+ cc = new ComputationCompiler(compiler);
+
+ a.get_cdr().get_visited_by(cc);
+
+ assimilate(cc);
+
+ init_instructions.add
+ (
+ new SetValue(r.get_address(), cc.get_computation())
+ );
+ }
+
+ n.get_computation().get_visited_by(this);
+
+ for (final String name: names)
+ {
+ compiler.registers().unbind(name);
+ }
+ }
+
+ @Override
public void visit_variable_reference
(
final tonkadur.fate.v1.lang.computation.VariableReference n