Skip to content

Commit

Permalink
Add value support to LoopFor
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaltby committed Apr 10, 2024
1 parent 3eb1333 commit 369d863
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 30 deletions.
86 changes: 86 additions & 0 deletions src/lib/compiler/scriptBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4507,6 +4507,25 @@ extern void __mute_mask_${symbol};
this._addNL();
};

variablesScriptValueOperation = (
setVariable: string,
operation: ScriptBuilderRPNOperation,
value: ScriptValue,
) => {
this._addComment(`Variables ${operation}`);
const [rpnOps, fetchOps] = precompileScriptValue(
optimiseScriptValue(value)
);
const localsLookup = this._performFetchOperations(fetchOps);
const rpn = this._rpn();
rpn.refVariable(setVariable)
this._performValueRPN(rpn, rpnOps, localsLookup);
rpn.operator(operation);
rpn.refSetVariable(setVariable);
rpn.stop();
this._addNL();
};

variableRandomOperation = (
variable: string,
operation: ScriptBuilderRPNOperation,
Expand Down Expand Up @@ -5396,6 +5415,73 @@ extern void __mute_mask_${symbol};
this._addNL();
};

ifVariableCompareScriptValue = (
variable: string,
operator: ScriptBuilderComparisonOperator,
value: ScriptValue,
truePath: ScriptEvent[] | ScriptBuilderPathFunction = [],
falsePath: ScriptEvent[] | ScriptBuilderPathFunction = []
) => {
const [rpnOps, fetchOps] = precompileScriptValue(
optimiseScriptValue(value)
);

this._addComment(`If Variable ${operator} Value`);

const trueLabel = this.getNextLabel();
const endLabel = this.getNextLabel();

if (rpnOps.length === 1 && rpnOps[0].type === "number") {
this._ifVariableConst(operator, variable, rpnOps[0].value, trueLabel, 0);
} else if (rpnOps.length === 1 && rpnOps[0].type === "direction") {
this._ifVariableCmpVariable(operator, variable, rpnOps[0].value, trueLabel, 0);
} else {
this._addComment(`-- Calculate value`);
const localsLookup = this._performFetchOperations(fetchOps);
const ifValueRef = this._declareLocal("if_value", 3, true);
const rpn = this._rpn();
this._performValueRPN(rpn, rpnOps, localsLookup);
rpn.refSet(ifValueRef).stop();
this._ifVariableCmpVariable(operator, variable, ifValueRef, trueLabel, 0);
}

this._compilePath(falsePath);
this._jump(endLabel);
this._label(trueLabel);
this._compilePath(truePath);
this._label(endLabel);
this._addNL();
};

ifScriptValue = (
value: ScriptValue,
truePath: ScriptEvent[] | ScriptBuilderPathFunction = [],
falsePath: ScriptEvent[] | ScriptBuilderPathFunction = []
) => {
const [rpnOps, fetchOps] = precompileScriptValue(
optimiseScriptValue(value)
);
const localsLookup = this._performFetchOperations(fetchOps);
const ifValueRef = this._declareLocal("if_value", 3, true);
this._addComment(`If`);

this._addComment(`-- Calculate value`);
const rpn = this._rpn();
this._performValueRPN(rpn, rpnOps, localsLookup);
rpn.refSet(ifValueRef).stop();

const trueLabel = this.getNextLabel();
const endLabel = this.getNextLabel();
this._addComment(`If`);
this._ifVariableConst(".NE", ifValueRef, 0, trueLabel, 0);
this._compilePath(falsePath);
this._jump(endLabel);
this._label(trueLabel);
this._compilePath(truePath);
this._label(endLabel);
this._addNL();
};

ifVariableBitwiseValue = (
variable: string,
operator: ScriptBuilderRPNOperation,
Expand Down
46 changes: 16 additions & 30 deletions src/lib/events/eventLoopFor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const autoLabel = (fetchArg) => {
comparison: fetchArg("comparison"),
to: fetchArg("to"),
operation: fetchArg("operation"),
val: fetchArg("val"),
val: fetchArg("value"),
});
};

Expand All @@ -29,14 +29,12 @@ const fields = [
key: "from",
label: l10n("FIELD_FROM"),
description: l10n("FIELD_FROM_FOR_DESC"),
type: "union",
types: ["number", "variable"],
defaultType: "number",
type: "value",
min: -32768,
max: 32767,
defaultValue: {
number: 0,
variable: "LAST_VARIABLE",
type: "number",
value: 0,
},
},
{
Expand All @@ -50,14 +48,12 @@ const fields = [
key: "to",
label: l10n("FIELD_TO"),
description: l10n("FIELD_TO_FOR_DESC"),
type: "union",
types: ["number", "variable"],
defaultType: "number",
type: "value",
min: -32768,
max: 32767,
defaultValue: {
number: 10,
variable: "LAST_VARIABLE",
type: "number",
value: 10,
},
},
],
Expand All @@ -77,14 +73,12 @@ const fields = [
key: "value",
label: l10n("FIELD_VALUE"),
description: l10n("FIELD_VALUE_FOR_DESC"),
type: "union",
types: ["number", "variable"],
defaultType: "number",
type: "value",
min: -32768,
max: 32767,
defaultValue: {
number: 1,
variable: "LAST_VARIABLE",
type: "number",
value: 1,
},
},
],
Expand All @@ -102,12 +96,9 @@ const compile = (input, helpers) => {
labelGoto,
getNextLabel,
compileEvents,
ifVariableValue,
ifVariableCompare,
variableValueOperation,
variablesOperation,
variableSetToValue,
variableCopy,
ifVariableCompareScriptValue,
variablesScriptValueOperation,
variableSetToScriptValue,
} = helpers;
const comparisonLookup = {
"==": ".EQ",
Expand All @@ -129,16 +120,11 @@ const compile = (input, helpers) => {
const operation = operationLookup[input.operation];

const loopId = getNextLabel();
let ifOp = input.to.type === "number" ? ifVariableValue : ifVariableCompare;
let performOp =
input.value.type === "number" ? variableValueOperation : variablesOperation;
let setOp = input.from.type === "number" ? variableSetToValue : variableCopy;

setOp(input.variable, input.from.value);
variableSetToScriptValue(input.variable, input.from);
labelDefine(loopId);
ifOp(input.variable, comparison, input.to.value, () => {
ifVariableCompareScriptValue(input.variable, comparison, input.to, () => {
compileEvents(input.true);
performOp(input.variable, operation, input.value.value);
variablesScriptValueOperation(input.variable, operation, input.value);
labelGoto(loopId);
});
};
Expand Down

0 comments on commit 369d863

Please sign in to comment.