summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2021-08-11 10:02:05 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2021-08-11 10:02:05 +0200
commit52ce8dce00fbb8879f24537f6cfb12c4d6a9a825 (patch)
tree6f9140b3f2c27fc74d6a39b56c24b6f78d2c6e72 /src/core
parentcbc02bb477154f7a03dfccde424b3b7738faa195 (diff)
Adds Wyrd generic instruction compilers.
Diffstat (limited to 'src/core')
-rw-r--r--src/core/src/tonkadur/wyrd/v1/Base.java6
-rw-r--r--src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java8
-rw-r--r--src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/GenericInstructionCompiler.java106
-rw-r--r--src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/generic/AllocateCompiler.java75
4 files changed, 194 insertions, 1 deletions
diff --git a/src/core/src/tonkadur/wyrd/v1/Base.java b/src/core/src/tonkadur/wyrd/v1/Base.java
index 739cf3a..c111ef2 100644
--- a/src/core/src/tonkadur/wyrd/v1/Base.java
+++ b/src/core/src/tonkadur/wyrd/v1/Base.java
@@ -3,6 +3,7 @@ package tonkadur.wyrd.v1;
import tonkadur.TonkadurPlugin;
import tonkadur.wyrd.v1.compiler.fate.v1.computation.GenericComputationCompiler;
+import tonkadur.wyrd.v1.compiler.fate.v1.instruction.GenericInstructionCompiler;
public class Base
{
@@ -12,5 +13,10 @@ public class Base
(
GenericComputationCompiler.class
);
+
+ TonkadurPlugin.register_as_loadable_superclass
+ (
+ GenericInstructionCompiler.class
+ );
}
}
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 97b212a..80f0e29 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
@@ -72,6 +72,8 @@ import tonkadur.wyrd.v1.compiler.util.Sort;
import tonkadur.wyrd.v1.compiler.util.SubList;
import tonkadur.wyrd.v1.compiler.util.While;
+import tonkadur.wyrd.v1.compiler.fate.v1.instruction.GenericInstructionCompiler;
+
public class InstructionCompiler
implements tonkadur.fate.v1.lang.meta.InstructionVisitor
{
@@ -3942,6 +3944,10 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor
)
throws Throwable
{
- // TODO: implement.
+ final InstructionCompiler ic;
+
+ ic = GenericInstructionCompiler.handle(compiler, n);
+
+ this.result.addAll(ic.result);
}
}
diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/GenericInstructionCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/GenericInstructionCompiler.java
new file mode 100644
index 0000000..936a83a
--- /dev/null
+++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/GenericInstructionCompiler.java
@@ -0,0 +1,106 @@
+package tonkadur.wyrd.v1.compiler.fate.v1.instruction;
+
+import java.util.Map;
+import java.util.HashMap;
+
+import tonkadur.wyrd.v1.compiler.fate.v1.Compiler;
+import tonkadur.wyrd.v1.compiler.fate.v1.InstructionCompiler;
+
+public abstract class GenericInstructionCompiler extends InstructionCompiler
+{
+ protected static final Map<Class, Class> COMPILERS;
+
+ static
+ {
+ COMPILERS = new HashMap<Class, Class>();
+ }
+
+ public static void register (final Class c)
+ {
+ try
+ {
+ COMPILERS.put
+ (
+ (Class) c.getDeclaredMethod("get_target_class").invoke
+ (
+ /* object = */ null
+ ),
+ c
+ );
+ }
+ catch (final Throwable t)
+ {
+ System.err.println
+ (
+ "[F] Could not get target class for Wyrd Generic Instruction"
+ + " Compiler '"
+ + c.getName()
+ + "':"
+ );
+
+ t.printStackTrace();
+ }
+ }
+
+ public static InstructionCompiler handle
+ (
+ final Compiler compiler,
+ final tonkadur.fate.v1.lang.instruction.GenericInstruction instruction
+ )
+ throws Throwable
+ {
+ final Class registered_class;
+ final GenericInstructionCompiler gic;
+
+ registered_class = COMPILERS.get(instruction.getClass());
+
+ if (registered_class == null)
+ {
+ System.err.println
+ (
+ "[F] No Wyrd compilation process registered for generic Fate"
+ + " instruction \""
+ + instruction.getClass().getName()
+ + "\"."
+ );
+
+ System.err.println("Registered compilers:");
+
+ for (final Class c: COMPILERS.keySet())
+ {
+ System.err.println
+ (
+ "- "
+ + COMPILERS.get(c).getName()
+ + " for "
+ + c.getName()
+ + "."
+ );
+ }
+
+ System.exit(-1);
+ }
+
+ gic =
+ (GenericInstructionCompiler)
+ registered_class.getDeclaredConstructor(Compiler.class).newInstance
+ (
+ compiler
+ );
+
+ gic.compile(instruction);
+
+ return gic;
+ }
+
+ protected abstract void compile
+ (
+ final tonkadur.fate.v1.lang.instruction.GenericInstruction instruction
+ )
+ throws Throwable;
+
+ protected GenericInstructionCompiler (final Compiler compiler)
+ {
+ super(compiler);
+ }
+}
diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/generic/AllocateCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/generic/AllocateCompiler.java
new file mode 100644
index 0000000..dbf4a98
--- /dev/null
+++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/instruction/generic/AllocateCompiler.java
@@ -0,0 +1,75 @@
+package tonkadur.wyrd.v1.compiler.fate.v1.instruction.generic;
+
+import tonkadur.fate.v1.lang.instruction.generic.Allocate;
+
+import tonkadur.wyrd.v1.lang.computation.Address;
+import tonkadur.wyrd.v1.lang.computation.New;
+
+import tonkadur.wyrd.v1.lang.instruction.SetValue;
+
+import tonkadur.wyrd.v1.compiler.fate.v1.Compiler;
+import tonkadur.wyrd.v1.compiler.fate.v1.TypeCompiler;
+import tonkadur.wyrd.v1.compiler.fate.v1.ComputationCompiler;
+
+import tonkadur.wyrd.v1.compiler.fate.v1.instruction.GenericInstructionCompiler;
+
+
+public class AllocateCompiler extends GenericInstructionCompiler
+{
+ public static Class get_target_class ()
+ {
+ return Allocate.class;
+ }
+
+ public AllocateCompiler (final Compiler compiler)
+ {
+ super(compiler);
+ }
+
+ public void compile
+ (
+ final tonkadur.fate.v1.lang.instruction.GenericInstruction instruction
+ )
+ throws Throwable
+ {
+ final Allocate source;
+ final ComputationCompiler cc;
+ final Address target;
+
+ source = (Allocate) instruction;
+
+ cc = new ComputationCompiler(compiler);
+
+ source.get_target().get_visited_by(cc);
+
+ if (cc.has_init())
+ {
+ result.add(cc.get_init());
+ }
+
+ target = cc.get_address();
+
+ if (target == null)
+ {
+ System.err.println
+ (
+ "[P] Argument in (allocate "
+ + source.get_target()
+ + ") did not compile to a address."
+ );
+
+ System.exit(-1);
+ }
+
+ result.add
+ (
+ new SetValue
+ (
+ target,
+ new New(TypeCompiler.compile(compiler, source.get_allocated_type()))
+ )
+ );
+
+ cc.release_registers(result);
+ }
+}