Skip to content

Commit

Permalink
Restructure instruction values
Browse files Browse the repository at this point in the history
  • Loading branch information
canmingir committed Jan 11, 2024
1 parent 48e4422 commit 7a077c3
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 81 deletions.
14 changes: 12 additions & 2 deletions src/instruction.js → src/Instruction.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
class Instruction {
constructor(scope, statement, before, run, graph, root, derivative) {
constructor(
scope,
statement,
before,
run,
graph,
after,
derivative = true,
priority = false
) {
this.scope = scope;
this.statement = statement;
this.before = before;
this.run = run;
this.graph = graph;
this.root = root;
this.after = after;
this.derivative = derivative;
this.priority = priority;
}
}

Expand Down
12 changes: 2 additions & 10 deletions src/lang/$nuc/$ASSIGNMENT.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const $INSTANCE = require("./$INSTANCE");
const Identifier = require("../ast/Identifier");
const graph = require("../../graph");
const CLASS = require("../../nuc/CLASS");
const Instruction = require("../../instruction");
const Instruction = require("../../Instruction");

function build(kind, left, right) {
let statement = new $ASSIGNMENT();
Expand Down Expand Up @@ -91,15 +91,7 @@ class $ASSIGNMENT extends $ {
}

run(scope) {
return new Instruction(
scope,
this.$,
undefined,
true,
undefined,
undefined,
undefined
);
return new Instruction(scope, this.$, null, true, null, null, null);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/lang/$nuc/$BLOCK.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const BLOCK = require("../../nuc/BLOCK");
const BLOCK$CLASS = require("../../nuc/BLOCK$CLASS");
const $ = require("./$");
const Instruction = require("../../instruction");
const Instruction = require("../../Instruction");
const Scope = require("../../Scope");
const { v4: uuid } = require("uuid");
const _ = require("lodash");
Expand Down Expand Up @@ -77,17 +77,17 @@ class $BLOCK extends $ {
statement.class = $class;
statement.statements = this.stms;
return [
new Instruction(scope, statement, true, true, false),
new Instruction(scope, statement, false, false, true),
new Instruction(scope, statement, true, true, false, false),
new Instruction(scope, statement, false, false, true, true),
];
} else {
let statement = new BLOCK(uuid());
statement.statements = this.stms;
statement.skip = this.skp;

return [
new Instruction(scope, statement, true, true, false),
new Instruction(scope, statement, false, false, true),
new Instruction(scope, statement, true, true, false, false),
new Instruction(scope, statement, false, false, true, true),
];
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lang/$nuc/$FOR.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const $ = require("./$");
const FOR = require("../../nuc/FOR");
const Instruction = require("../../instruction");
const Instruction = require("../../Instruction");
const Identifier = require("../ast/Identifier");

function build(variable, array, statements) {
Expand All @@ -18,7 +18,7 @@ class $FOR extends $ {
statement.array = new Identifier(this.arr);
statement.statements = this.stms;

return new Instruction(scope, statement, false, true, false);
return new Instruction(scope, statement, false, true, false, false);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/lang/$nuc/$IF.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const graph = require("../../graph");
const IF = require("../../nuc/IF");
const CLASS = require("../../nuc/CLASS");
const IF$CLASS = require("../../nuc/IF$CLASS");
const Instruction = require("../../instruction");
const Instruction = require("../../Instruction");
const Expression = require("../../Expression");
const $EXPRESSION = require("./$EXPRESSION");
const Identifier = require("../ast/Identifier");
Expand Down Expand Up @@ -42,8 +42,8 @@ class $IF extends $ {
}

return [
new Instruction(scope, statement, true, true, false, null, true),
new Instruction(scope, statement, false, false, true, null, true),
new Instruction(scope, statement, true, true, false, false),
new Instruction(scope, statement, false, false, true, true),
];
}
}
Expand All @@ -54,8 +54,8 @@ class $IF extends $ {
statement.false = this.fls;

return [
new Instruction(scope, statement, true, true, false, null, true),
new Instruction(scope, statement, false, false, true, null, true),
new Instruction(scope, statement, true, true, false, false),
new Instruction(scope, statement, false, false, true, true),
];
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lang/$nuc/$INSTANCE.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const CLASS = require("../../nuc/CLASS");
const $LET = require("./$LET");
const Identifier = require("../ast/Identifier");
const random = require("../../lib/random");
const Instruction = require("../../instruction");
const Instruction = require("../../Instruction");

function build(cls, object, name, args = []) {
let statement = new $INSTANCE();
Expand Down Expand Up @@ -58,8 +58,8 @@ class $INSTANCE extends $ {
statement.name = name;
statement.object = graph.retrieve(object);
return [
new Instruction(scope, statement, true, true, false, null, true),
new Instruction(scope, statement, false, false, true, null, true),
new Instruction(scope, statement, true, true, false, false),
new Instruction(scope, statement, false, false, true, true),
];
} else {
const statement = new OBJECT(`${object}.${name}`);
Expand Down
8 changes: 4 additions & 4 deletions src/lang/$nuc/$PROPERTY.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Local = require("../../lib/local");
const FUNCTION = require("../../nuc/FUNCTION");
const Identifier = require("../ast/Identifier");
const $EXPRESSION = require("./$EXPRESSION");
const Instruction = require("../../instruction");
const Instruction = require("../../Instruction");

function build(object, name, value) {
let statement = new $PROPERTY();
Expand Down Expand Up @@ -48,16 +48,16 @@ class $PROPERTY extends $ {
statement.name = name;
statement.value = this.val;
return [
new Instruction(scope, statement, true, true, false, null, true),
new Instruction(scope, statement, false, false, true, null, true),
new Instruction(scope, statement, true, true, false, false),
new Instruction(scope, statement, false, false, true, true),
];
}

let statement = new PROPERTY(`${object}.${name}`);
statement.object = graph.retrieve(object);
statement.name = name;
statement.value = this.val;
return new Instruction(scope, statement, true, true, true);
return new Instruction(scope, statement, true, true, true, true);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/nuc/BLOCK$CLASS.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const NODE = require("./NODE");
const BLOCK$INSTANCE = require("./BLOCK$INSTANCE");
const Instruction = require("../instruction");
const Instruction = require("../Instruction");
const { v4: uuid } = require("uuid");
const Scope = require("../Scope");

Expand Down Expand Up @@ -33,10 +33,10 @@ class BLOCK$CLASS extends NODE {
instanceScope.$instance = this;

statements.push(
new Instruction(instanceScope, statement, true, true, false)
new Instruction(instanceScope, statement, true, true, false, false)
);
statements.push(
new Instruction(instanceScope, statement, false, false, true)
new Instruction(instanceScope, statement, false, false, true, true)
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/nuc/BLOCK.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const NODE = require("./NODE");
const $ = require("../lang/$nuc/$");
const Instruction = require("../instruction");
const Instruction = require("../Instruction");
const Scope = require("../Scope");

class BLOCK extends NODE {
Expand All @@ -14,7 +14,8 @@ class BLOCK extends NODE {
newScope.block = this;
return {
next: this.statements.map(
(statement) => new Instruction(newScope, statement)
(statement) =>
new Instruction(newScope, statement, null, null, null, null)
),
};
}
Expand Down
6 changes: 4 additions & 2 deletions src/nuc/DELETE.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const state = require("../state");
const graph = require("../graph");
const Instruction = require("../instruction");
const Instruction = require("../Instruction");
const Node = require("./NODE");

class DELETE {
Expand All @@ -15,7 +15,9 @@ class DELETE {

for (let node in graph.retrieve(key).next) {
const statement = graph.retrieve(node);
list.push(new Instruction(scope.root, statement, false, true, false));
list.push(
new Instruction(scope.root, statement, false, true, false, false)
);
}

return { next: list, value: true };
Expand Down
4 changes: 2 additions & 2 deletions src/nuc/FOR.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const $BLOCK = require("../lang/$nuc/$BLOCK");
const Instruction = require("../instruction");
const Instruction = require("../Instruction");
const $LET = require("../lang/$nuc/$LET");
const state = require("../state");
const graph = require("../graph");
Expand Down Expand Up @@ -35,7 +35,7 @@ class FOR {
this.index++;
}

list.push(new Instruction(scope, this, false, true, false));
list.push(new Instruction(scope, this, false, true, false, false));
return { next: list };
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/nuc/IF$CLASS.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const NODE = require("./NODE");
const _ = require("lodash");
const { v4: uuid } = require("uuid");
const Scope = require("../Scope");
const Instruction = require("../instruction");
const Instruction = require("../Instruction");

class IF$CLASS extends NODE {
run(scope) {
Expand Down Expand Up @@ -34,7 +34,7 @@ class IF$CLASS extends NODE {
instanceScope.$instance = instance;

statements.push(
new Instruction(instanceScope, statement, true, true, true, null, true)
new Instruction(instanceScope, statement, true, true, true, true)
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/nuc/IF.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const NODE = require("./NODE");
const Instruction = require("../instruction");
const Instruction = require("../Instruction");
const Scope = require("../Scope");
const state = require("../state");
const _ = require("lodash");
Expand All @@ -23,16 +23,16 @@ class IF extends NODE {
const trueStatement = _.cloneDeep(this.true);
return {
next: [
new Instruction(local, trueStatement, true, true, false),
new Instruction(local, trueStatement, false, false, true),
new Instruction(local, trueStatement, true, true, false, false),
new Instruction(local, trueStatement, false, false, true, true),
],
};
} else if (this.false) {
const falseStatement = _.cloneDeep(this.false);
return {
next: [
new Instruction(local, falseStatement, true, true, false),
new Instruction(local, falseStatement, false, false, true),
new Instruction(local, falseStatement, true, true, false, false),
new Instruction(local, falseStatement, false, false, true, true),
],
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/nuc/OBJECT$CLASS.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Id = require("../lib/identifier");
const OBJECT$INSTANCE = require("./OBJECT$INSTANCE");
const graph = require("../graph");
const Scope = require("../Scope");
const Instruction = require("../instruction");
const Instruction = require("../Instruction");

class OBJECT$CLASS extends NODE {
before() {
Expand Down Expand Up @@ -32,7 +32,7 @@ class OBJECT$CLASS extends NODE {
instanceScope.$instance = instance;

statements.push(
new Instruction(instanceScope, statement, true, true, true, null, true)
new Instruction(instanceScope, statement, true, true, true, true)
);
}

Expand Down
29 changes: 26 additions & 3 deletions src/nuc/OBJECT.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const state = require("../state");
const NODE = require("./NODE");
const Instruction = require("../instruction");
const Instruction = require("../Instruction");
const Scope = require("../Scope");
const Evaluation = require("../lang/Evaluation");
const Identifier = require("../lang/ast/Identifier");
Expand Down Expand Up @@ -69,15 +69,38 @@ class OBJECT extends NODE {
const scope = new Scope();
scope.$instance = this;

list.push(new Instruction(scope, declaration, true, true, false, true));
list.push(new Instruction(scope, declaration, false, false, true, true));
list.push(
new Instruction(
scope,
declaration,
true,
true,
false,
false,
true,
true
)
);
list.push(
new Instruction(
scope,
declaration,
false,
false,
true,
true,
true,
true
)
);
}

const expression = new Instruction(
scope,
$EXPRESSION(variable.node),
true,
true,
false,
false
);
expression.derivative = false;
Expand Down
4 changes: 2 additions & 2 deletions src/nuc/PROPERTY$CLASS.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const NODE = require("./NODE");
const PROPERTY$INSTANCE = require("./PROPERTY$INSTANCE");
const _ = require("lodash");
const Instruction = require("../instruction");
const Instruction = require("../Instruction");
const Scope = require("../Scope");

class PROPERTY$CLASS extends NODE {
Expand All @@ -27,7 +27,7 @@ class PROPERTY$CLASS extends NODE {
instanceScope.$instance = instance;

statements.push(
new Instruction(instanceScope, statement, true, true, true, null, true)
new Instruction(instanceScope, statement, true, true, true, true)
);
}

Expand Down
Loading

0 comments on commit 7a077c3

Please sign in to comment.