summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2021-04-02 21:47:15 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2021-04-02 21:47:15 +0200
commit6b224c3d53920effdc8cff5846cfc49b4a358a19 (patch)
tree24b7310d38bbce4388d10ff7358b98a76e8272c5 /src
parent11daf8c58b8a0dd4f4c9babe2660d3779a04465a (diff)
Memory allocation is now an instruction.
Diffstat (limited to 'src')
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/computation/New.java56
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/instruction/Allocate.java112
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/instruction/Free.java6
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java3
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java3
-rw-r--r--src/core/src/tonkadur/fate/v1/parser/FateLexer.g46
-rw-r--r--src/core/src/tonkadur/fate/v1/parser/FateParser.g441
-rw-r--r--src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java11
-rw-r--r--src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java44
9 files changed, 184 insertions, 98 deletions
diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/New.java b/src/core/src/tonkadur/fate/v1/lang/computation/New.java
deleted file mode 100644
index 000d74a..0000000
--- a/src/core/src/tonkadur/fate/v1/lang/computation/New.java
+++ /dev/null
@@ -1,56 +0,0 @@
-package tonkadur.fate.v1.lang.computation;
-
-import tonkadur.parser.Origin;
-
-import tonkadur.fate.v1.lang.type.PointerType;
-import tonkadur.fate.v1.lang.type.Type;
-
-import tonkadur.fate.v1.lang.meta.ComputationVisitor;
-import tonkadur.fate.v1.lang.meta.Computation;
-
-public class New extends Computation
-{
- /***************************************************************************/
- /**** MEMBERS **************************************************************/
- /***************************************************************************/
- protected final Type target_type;
-
- /***************************************************************************/
- /**** PROTECTED ************************************************************/
- /***************************************************************************/
- /**** Constructors *********************************************************/
- public New (final Origin origin, final Type t)
- {
- super(origin, new PointerType(origin, t, "auto generated"));
- this.target_type = t;
- }
-
- /***************************************************************************/
- /**** PUBLIC ***************************************************************/
- /***************************************************************************/
- /**** Accessors ************************************************************/
- @Override
- public void get_visited_by (final ComputationVisitor cv)
- throws Throwable
- {
- cv.visit_new(this);
- }
-
- public Type get_target_type ()
- {
- return target_type;
- }
-
- /**** Misc. ****************************************************************/
- @Override
- public String toString ()
- {
- final StringBuilder sb = new StringBuilder();
-
- sb.append("(New ");
- sb.append(target_type.get_name());
- sb.append(") ");
-
- return sb.toString();
- }
-}
diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Allocate.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Allocate.java
new file mode 100644
index 0000000..32c7241
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Allocate.java
@@ -0,0 +1,112 @@
+package tonkadur.fate.v1.lang.instruction;
+
+import java.util.Collections;
+
+import tonkadur.error.ErrorManager;
+
+import tonkadur.parser.Origin;
+import tonkadur.parser.ParsingError;
+
+import tonkadur.fate.v1.error.InvalidTypeException;
+
+import tonkadur.fate.v1.lang.type.Type;
+import tonkadur.fate.v1.lang.type.PointerType;
+
+import tonkadur.fate.v1.lang.meta.InstructionVisitor;
+import tonkadur.fate.v1.lang.meta.Instruction;
+import tonkadur.fate.v1.lang.meta.Reference;
+
+public class Allocate extends Instruction
+{
+ /***************************************************************************/
+ /**** MEMBERS **************************************************************/
+ /***************************************************************************/
+ protected final Reference target;
+ protected final Type allocated_type;
+
+ /***************************************************************************/
+ /**** PROTECTED ************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ protected Allocate
+ (
+ final Origin origin,
+ final Type allocated_type,
+ final Reference target
+ )
+ {
+ super(origin);
+
+ this.allocated_type = allocated_type;
+ this.target = target;
+ }
+
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ public static Allocate build (final Origin origin, final Reference target)
+ throws ParsingError
+ {
+ final Type target_type;
+
+ target_type = target.get_type();
+
+ if (target_type instanceof PointerType)
+ {
+ return
+ new Allocate
+ (
+ origin,
+ ((PointerType) target_type).get_referenced_type(),
+ target
+ );
+ }
+
+ ErrorManager.handle
+ (
+ new InvalidTypeException
+ (
+ origin,
+ target_type,
+ Collections.singletonList
+ (
+ new PointerType(origin, Type.ANY, "Any Pointer")
+ )
+ )
+ );
+
+ return new Allocate(origin, Type.ANY, target);
+ }
+
+ /**** Accessors ************************************************************/
+ @Override
+ public void get_visited_by (final InstructionVisitor iv)
+ throws Throwable
+ {
+ iv.visit_allocate(this);
+ }
+
+ public Reference get_target ()
+ {
+ return target;
+ }
+
+ public Type get_allocated_type ()
+ {
+ return allocated_type;
+ }
+
+ /**** Misc. ****************************************************************/
+ @Override
+ public String toString ()
+ {
+ final StringBuilder sb = new StringBuilder();
+
+ sb.append("(Allocate ");
+ sb.append(target.toString());
+ sb.append(")");
+
+ return sb.toString();
+ }
+}
diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Free.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Free.java
index 4e9a1f2..65e6e6f 100644
--- a/src/core/src/tonkadur/fate/v1/lang/instruction/Free.java
+++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Free.java
@@ -26,11 +26,7 @@ public class Free extends Instruction
/**** PUBLIC ***************************************************************/
/***************************************************************************/
/**** Constructors *********************************************************/
- public Free
- (
- final Origin origin,
- final Reference value_reference
- )
+ public Free (final Origin origin, final Reference value_reference)
{
super(origin);
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 8e57746..7ae4a60 100644
--- a/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java
+++ b/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java
@@ -22,9 +22,6 @@ public interface ComputationVisitor
public void visit_access_as_reference (final AccessAsReference n)
throws Throwable;
- public void visit_new (final New n)
- throws Throwable;
-
public void visit_cast (final Cast 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 922fbc1..06c8525 100644
--- a/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java
+++ b/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java
@@ -32,6 +32,9 @@ public interface InstructionVisitor
public void visit_done (final Done n)
throws Throwable;
+ public void visit_allocate (final Allocate n)
+ throws Throwable;
+
public void visit_free (final Free n)
throws Throwable;
diff --git a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4
index b333202..63517db 100644
--- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4
+++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4
@@ -107,7 +107,7 @@ SAFE_INDEXED_MERGE_TO_SET_KW : L_PAREN (('safe'US'indexed')|('indexed'US'safe'))
SAFE_IMP_MERGE_KW : L_PAREN 'safe'US'merge!' SEP+;
SAFE_IMP_INDEXED_MERGE_KW : L_PAREN (('indexed'US'safe')|('safe'US'indexed'))US'merge!' SEP+;
NEWLINE_KW: L_PAREN 'newline)';
-NEW_KW: L_PAREN ('new'|'reserve'|'create') SEP+;
+ALLOCATE_KW: L_PAREN (('alloc''ate'?)|'malloc'|'new')'!'? SEP+;
NOT_KW: L_PAREN ('not'|'~'|'!') SEP+;
ONE_IN_KW: L_PAREN ('exactly'US)?'one'(US'in')? SEP+;
OR_KW: L_PAREN ('or'|'\\/') SEP+;
@@ -150,7 +150,7 @@ SORT_KW: L_PAREN 'sort' SEP+;
IMP_SORT_KW: L_PAREN 'sort!' SEP+;
SET_FIELDS_KW: L_PAREN 'set'US'fields' SEP+;
IMP_SET_FIELDS_KW: L_PAREN 'set'US'fields!' SEP+;
-SET_KW: L_PAREN 'set'(US(('val''ue'?)|('var''iable'?)))? SEP+;
+SET_KW: L_PAREN 'set'(US(('val''ue'?)|('var''iable'?)))?'!'? SEP+;
SUB_LIST_KW: L_PAREN 'sub'US'list' SEP+;
IMP_SUB_LIST_KW: L_PAREN 'sub'US'list!' SEP+;
LIST_KW: L_PAREN 'list' SEP+;
@@ -185,4 +185,4 @@ WORD: ((~([ \t\r\n()]))|'(lp)'|'(rp)'|'(sp)')+
);
};
-COMMENT: WS* ';' .*? '\n' -> channel(HIDDEN);
+COMMENT: WS* ';;' .*? '\n' -> channel(HIDDEN);
diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4
index fd0dd14..011a9b9 100644
--- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4
+++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4
@@ -1462,6 +1462,21 @@ returns [Instruction result]
);
}
+ | ALLOCATE_KW value_reference WS* R_PAREN
+ {
+ final Origin origin;
+
+ origin =
+ CONTEXT.get_origin_at
+ (
+ ($ALLOCATE_KW.getLine()),
+ ($ALLOCATE_KW.getCharPositionInLine())
+ );
+
+ $result = Allocate.build(origin, ($value_reference.result));
+ }
+
+
| FREE_KW value_reference WS* R_PAREN
{
$result =
@@ -3665,25 +3680,6 @@ returns [Computation result]:
($value_reference.result)
);
}
-
- | NEW_KW WORD WS* R_PAREN
- {
- final Origin origin;
-
- origin =
- CONTEXT.get_origin_at
- (
- ($WORD.getLine()),
- ($WORD.getCharPositionInLine())
- );
-
- $result =
- new New
- (
- origin,
- WORLD.types().get(origin, ($WORD.text))
- );
- }
;
catch [final Throwable e]
{
@@ -4026,7 +4022,12 @@ catch [final Throwable e]
non_text_value
returns [Computation result]
:
- IF_ELSE_KW cond=non_text_value WS+
+ STRING_KW sentence WS* R_PAREN
+ {
+ $result = $sentence.result;
+ }
+
+ | IF_ELSE_KW cond=non_text_value WS+
if_true=value WS+
if_false=value WS*
R_PAREN
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 70d3ed1..3508d56 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
@@ -1404,17 +1404,6 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor
}
@Override
- public void visit_new
- (
- final tonkadur.fate.v1.lang.computation.New n
- )
- throws Throwable
- {
- result_as_computation =
- new New(TypeCompiler.compile(compiler, n.get_target_type()));
- }
-
- @Override
public void visit_default
(
final tonkadur.fate.v1.lang.computation.Default n
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 8597920..b186682 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
@@ -27,6 +27,7 @@ import tonkadur.wyrd.v1.lang.computation.IfElseComputation;
import tonkadur.wyrd.v1.lang.computation.Size;
import tonkadur.wyrd.v1.lang.computation.GetLastChoiceIndex;
import tonkadur.wyrd.v1.lang.computation.ValueOf;
+import tonkadur.wyrd.v1.lang.computation.New;
import tonkadur.wyrd.v1.lang.instruction.AddTextOption;
import tonkadur.wyrd.v1.lang.instruction.AddEventOption;
@@ -2104,6 +2105,49 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor
}
@Override
+ public void visit_allocate
+ (
+ final tonkadur.fate.v1.lang.instruction.Allocate n
+ )
+ throws Throwable
+ {
+ final ComputationCompiler cc;
+ final Address target;
+
+ cc = new ComputationCompiler(compiler);
+
+ n.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 "
+ + n.get_target()
+ + ") did not compile to a address."
+ );
+ }
+
+ result.add
+ (
+ new SetValue
+ (
+ target,
+ new New(TypeCompiler.compile(compiler, n.get_allocated_type()))
+ )
+ );
+
+ cc.release_registers(result);
+ }
+
+ @Override
public void visit_while (final tonkadur.fate.v1.lang.instruction.While n)
throws Throwable
{