Skip to content

Commit

Permalink
obj() now defines the scope as a function
Browse files Browse the repository at this point in the history
  • Loading branch information
mageowl committed Oct 18, 2022
1 parent 648f5bf commit 237b6d1
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 31 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}
</style>
<script src="src/main.js" type="module"></script>
<script type="text/f-script" src="test/helloWorld.func"></script>
<script type="text/f-script" src="test/elementTest.func"></script>
</head>
<body>
<h1>F-Script Language <span>v1.0.8</span></h1>
Expand Down
22 changes: 13 additions & 9 deletions src/defaultModules/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isWeb } from "../process.js";
import { Scope } from "../scope.js";
class HTMLElementScope extends Scope {
htmlEl;
type;
type = "Block";
body = [];
scope = this;
destroyed = false;
Expand Down Expand Up @@ -32,28 +32,32 @@ class HTMLElementScope extends Scope {
el.setAttribute(name.value, value.value);
}
});
this.localFunctions.set("text", {
this.localFunctions.set("get", {
type: "js",
run(text) {
run(property) {
if (self.destroyed)
error("Trying to edit a destroyed element.", "Web");
el.innerText = text.value;
let value = el[property.value];
return {
type: typeof value === "number" ? "NumberLiteral" : "StringLiteral",
value
};
}
});
this.localFunctions.set("appendText", {
this.localFunctions.set("text", {
type: "js",
run(text) {
if (self.destroyed)
error("Trying to edit a destroyed element.", "Web");
el.innerText += text.value;
el.innerText = text.value;
}
});
this.localFunctions.set("self", {
this.localFunctions.set("appendText", {
type: "js",
run(text) {
if (self.destroyed)
error("Trying to access a destroyed element.", "Web");
return self;
error("Trying to edit a destroyed element.", "Web");
el.innerText += text.value;
}
});
this.localFunctions.set("add", {
Expand Down
23 changes: 14 additions & 9 deletions src/defaultModules/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Scope } from "../scope.js";

class HTMLElementScope extends Scope {
htmlEl: HTMLElement;
type: FNodeType;
type: FNodeType = "Block";
body: FNode[] = [];
scope = this;
destroyed = false;
Expand Down Expand Up @@ -45,6 +45,19 @@ class HTMLElementScope extends Scope {
}
});

this.localFunctions.set("get", {
type: "js",
run(property: FNodeValue): FNodeValue {
if (self.destroyed) error("Trying to edit a destroyed element.", "Web");

let value = el[property.value];
return {
type: typeof value === "number" ? "NumberLiteral" : "StringLiteral",
value
};
}
});

this.localFunctions.set("text", {
type: "js",
run(text: FNodeValue) {
Expand All @@ -60,14 +73,6 @@ class HTMLElementScope extends Scope {
el.innerText += text.value;
}
});
this.localFunctions.set("self", {
type: "js",
run(text: FNodeValue) {
if (self.destroyed)
error("Trying to access a destroyed element.", "Web");
return self;
}
});

this.localFunctions.set("add", {
type: "js",
Expand Down
15 changes: 13 additions & 2 deletions src/executer.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,12 @@ runtime.localFunctions.set("num", {
});
runtime.localFunctions.set("obj", {
type: "js",
run(memory, data, yieldFunction) {
run(memoryRaw, data, yieldFunction) {
let memory = execute(memoryRaw, data);
let block = yieldFunction;
if (memory.type !== "MemoryLiteral") {
error(`The first parameter for obj must be a memory literal. Instead, I got a ${memory.type}`, "Type");
}
function check() {
if (block.type === "FunctionCall") {
block = execute(yieldFunction, data);
Expand All @@ -230,7 +234,14 @@ runtime.localFunctions.set("obj", {
error(`Yield to obj must be a block. Instead, I got a ${block.type}`, "Type");
}
check();
data.scope.childScopes.set(memory.value, execute(block, { ...data, returnScope: true }));
let scope = execute(block, { ...data, returnScope: true });
memory.slot.scope.childScopes.set(memory.slot.name, scope);
memory.slot.set({
type: "js",
run() {
return scope;
}
});
}
});
runtime.localFunctions.set("if", {
Expand Down
27 changes: 20 additions & 7 deletions src/executer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { error } from "./error.js";
import { Scope } from "./scope.js";
import { Scope, Slot } from "./scope.js";
import { isWeb } from "./process.js";
import { getConsoleEl } from "./defaultModules/web.js";
import { FCallData } from "./interfaces.js";
import { FCallData, FNodeAny, FNodeMemory, FNodeValue } from "./interfaces.js";

function stringify(node) {
if (!node) return;
Expand Down Expand Up @@ -260,9 +260,17 @@ runtime.localFunctions.set("num", {

runtime.localFunctions.set("obj", {
type: "js",
run(memory, data, yieldFunction) {
run(memoryRaw: FNodeAny, data, yieldFunction) {
let memory: FNodeMemory = execute(memoryRaw, data);
let block = yieldFunction;

if (memory.type !== "MemoryLiteral") {
error(
`The first parameter for obj must be a memory literal. Instead, I got a ${memory.type}`,
"Type"
);
}

function check() {
if (block.type === "FunctionCall") {
block = execute(yieldFunction, data);
Expand All @@ -279,10 +287,15 @@ runtime.localFunctions.set("obj", {

check();

data.scope.childScopes.set(
memory.value,
execute(block, { ...data, returnScope: true })
);
let scope = execute(block, { ...data, returnScope: true });
memory.slot.scope.childScopes.set(memory.slot.name, scope);

memory.slot.set({
type: "js",
run() {
return scope;
}
});
}
});

Expand Down
8 changes: 7 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FTokenType } from "./enums";
import { Scope } from "./scope";
import { Scope, Slot } from "./scope";

export interface FToken {
type: FTokenType;
Expand Down Expand Up @@ -36,6 +36,11 @@ export interface FNodeValue extends FNode {
value: any;
}

export interface FNodeMemory extends FNode {
type: "MemoryLiteral";
slot: Slot;
}

export interface FNodeBlock extends FNode {
type: "Program" | "Block" | "ParameterBlock";
body: FNodeAny[];
Expand All @@ -46,6 +51,7 @@ export type FNodeAny =
| FNodeBlock
| FNodeFunctionCall
| FNodeValue
| FNodeMemory
| null;

export interface FCallData {
Expand Down
2 changes: 1 addition & 1 deletion src/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Scope {
this.returnValue = value;
}
}
class Slot {
export class Slot {
scope;
name;
used;
Expand Down
2 changes: 1 addition & 1 deletion src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Scope {
}
}

class Slot {
export class Slot {
scope: Scope;
name: string;
used: boolean;
Expand Down
8 changes: 8 additions & 0 deletions test/elementTest.func
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!web>
web.useElementAsConsole("fscript-logs");

obj(<console>) -> web.getElement("#fscript-logs");
obj(<can>) -> web.createElement("span");
can.text("OP POND");
can.setAttr("style", "font-weight:bold;");
console.add(can);
1 change: 1 addition & 0 deletions test/helloWorld.func
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ web.useElementAsConsole("fscript-logs");

def(<lessThan>) -> {
<!math>

def(<diff>) -> sub(param(0), param(1));
def(<less>) -> false;

Expand Down

0 comments on commit 237b6d1

Please sign in to comment.