Skip to content

Commit

Permalink
Different immutabilities (#172)
Browse files Browse the repository at this point in the history
* separate constant values from immutable values

* add a test
  • Loading branch information
JeanJPNM committed Apr 30, 2023
1 parent cd75edd commit d8c4266
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
8 changes: 6 additions & 2 deletions compiler/src/handlers/VariableDeclaration.ts
Expand Up @@ -98,7 +98,11 @@ const DeclareIdentifier: TDeclareHandler<es.Identifier> = (
handler(init, inst) {
if (kind === "const" && !init)
throw new CompilerError("Constants must be initialized.", node);
if (kind === "const" && init?.mutability === EMutability.constant) {
if (
kind === "const" &&
(init?.mutability === EMutability.constant ||
init?.mutability === EMutability.immutable)
) {
scope.set(identifier, init);
return [null, inst];
} else {
Expand All @@ -112,7 +116,7 @@ const DeclareIdentifier: TDeclareHandler<es.Identifier> = (
);
pipeInsts(value["="](scope, init), inst);
}
if (kind === "const") value.mutability = EMutability.constant;
if (kind === "const") value.mutability = EMutability.immutable;
return [null, inst];
}
},
Expand Down
7 changes: 5 additions & 2 deletions compiler/src/macros/Memory.ts
Expand Up @@ -106,14 +106,17 @@ export class MemoryBuilder extends MacroFunction {
"The memory size must be a number literal or a store."
);

if (size.mutability === EMutability.constant) {
if (
size.mutability === EMutability.constant ||
size.mutability === EMutability.immutable
) {
return [new MemoryMacro(cell, size), []];
}

const name = extractOutName(out) ?? scope.makeTempName();
const store = new StoreValue(`${name}.&len`);
const [, inst] = store["="](scope, size);
store.mutability = EMutability.constant;
store.mutability = EMutability.immutable;
return [new MemoryMacro(cell, store), inst];
});
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/macros/commands/Fetch.ts
@@ -1,5 +1,5 @@
import { InstructionBase } from "../../instructions";
import { EMutability, IValue } from "../../types";
import { IValue } from "../../types";
import { ObjectValue, StoreValue } from "../../values";
import { createOverloadNamespace } from "../util";

Expand All @@ -17,7 +17,7 @@ export class Fetch extends ObjectValue {
buildCount: { args: ["team", "block"] },
},
handler(scope, overload, out, team, ...rest) {
const output = StoreValue.from(scope, out, EMutability.constant);
const output = StoreValue.from(scope, out);

const params: (IValue | string)[] = ["0", "@conveyor"];

Expand Down
6 changes: 5 additions & 1 deletion compiler/src/types.ts
Expand Up @@ -252,10 +252,14 @@ export enum EMutability {
* but it's still not safe from mutations coming from the runtime.
*/
readonly,
/** Constant values, won't change during execution */
/** Compile-time constants.*/
constant,
/** An immutable value that hasn't been initialized yet */
init,
/** A value unknown at compile-time
* that will not change after initialization
*/
immutable,
}

export interface IValue extends IValueOperators {
Expand Down
11 changes: 11 additions & 0 deletions compiler/test/in/switch_immutability.js
@@ -0,0 +1,11 @@
const a = Math.rand(10);

const b = 2;

switch (b) {
case a:
print("is a");
break;
case 2:
print("is b");
}
7 changes: 7 additions & 0 deletions compiler/test/out/switch_immutability.mlog
@@ -0,0 +1,7 @@
op rand a:1:6 10
jump 3 strictEqual 2 a:1:6
jump 5 always
print "is a"
jump 6 always
print "is b"
end

0 comments on commit d8c4266

Please sign in to comment.