Skip to content

Commit

Permalink
impl: local variables
Browse files Browse the repository at this point in the history
  • Loading branch information
gnlow committed Oct 11, 2023
1 parent 85cc26c commit d5facc7
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/class/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ export interface Message {

export interface Function_ {
id: string;
block: string;
type: "normal" | "value"
localVariables: {
name: string
value: number
id: string
}[]
useLocalVariables: boolean
content: Script;
}

Expand Down
41 changes: 31 additions & 10 deletions src/util/projectToJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ export const projectToJs =
)
+ "\n\n"
+ project.functions.map(
({id, content}) => {
const expr = scriptToExpressions(content)
({id, content, localVariables}) => {
const expr = functionToArrow(
content[0][0],
localVariables.map(
({id}) => `let v_${id}` as Expression
)
)
return `Entry.func_${id} = ${expr}`
}
).join("\n")
Expand Down Expand Up @@ -67,9 +72,6 @@ export const eventHandlerToFunction =
]
)
}
if (event?.type == "function_create") {
return functionToArrow(event)
}
}

export const getFunctionArgs =
Expand All @@ -94,17 +96,26 @@ export const getFunctionArgs =
}

export const functionToArrow =
({params, statements}: Block) =>
cg.arrow(
({params, statements}: Block, injectBefore: Expression[] = []) => {
return cg.arrow(
[
...getFunctionArgs(params[0] as Block),
"obj" as Expression,
],
statements[0].map(
blockToExpression
)
[
...injectBefore,
...statements[0].map(
blockToExpression
),
...(
params[3]
? ["return " + blockToExpression(params[3]) as Expression]
: []
)
]
)
.replaceAll(`"$obj$"`, "obj") as Expression
}

export const blockGroupToArrow =
(blockGroup: Block[]) =>
Expand Down Expand Up @@ -137,6 +148,16 @@ export const blockToExpression =
|| block.type.startsWith("booleanParam_")
)
return block.type as Expression

if (block.type == "set_func_variable")
return `v_${
block.params[0]
} = ${
blockToExpression(block.params[1]!)
}` as Expression

if (block.type == "get_func_variable")
return `v_${block.params[0]}` as Expression

return cg.call(
"Entry." + block.type as Expression,
Expand Down
5 changes: 5 additions & 0 deletions test/fib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions test/fib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"objects": [
{
"id": "ctqk",
"name": "새 오브젝트2",
"script": "[[{\"id\":\"hxah\",\"x\":29,\"y\":74,\"type\":\"when_run_button_click\",\"params\":[null],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]},{\"id\":\"dxel\",\"x\":0,\"y\":0,\"type\":\"dialog\",\"params\":[{\"id\":\"22qv\",\"x\":0,\"y\":0,\"type\":\"func_zlc2\",\"params\":[{\"id\":\"89jb\",\"x\":0,\"y\":0,\"type\":\"text\",\"params\":[10],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]},\"speak\",null],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}]]",
"objectType": "sprite",
"rotateMethod": "free",
"scene": "7dwq",
"sprite": {
"pictures": [
{
"id": "nrzz",
"dimension": {
"width": 960,
"height": 540,
"scaleX": 0.5,
"scaleY": 0.5
},
"fileurl": "/lib/entry-js/images/_1x1.png",
"name": "새그림",
"imageType": "png"
}
],
"sounds": []
},
"selectedPictureId": "nrzz",
"lock": false,
"entity": {
"x": 0,
"y": 0,
"regX": 480,
"regY": 270,
"scaleX": 0.5,
"scaleY": 0.5,
"rotation": 0,
"direction": 90,
"width": 960,
"height": 540,
"font": "undefinedpx ",
"visible": true
}
}
],
"scenes": [{ "id": "7dwq", "name": "장면 1" }],
"variables": [
{
"name": "초시계",
"id": "brih",
"visible": false,
"value": 0,
"variableType": "timer",
"isCloud": false,
"isRealTime": false,
"cloudDate": false,
"object": null,
"x": 134,
"y": -70
},
{
"name": " 대답 ",
"id": "1vu8",
"visible": false,
"value": "0",
"variableType": "answer",
"isCloud": false,
"isRealTime": false,
"cloudDate": false,
"object": null,
"x": 150,
"y": -100
}
],
"messages": [],
"functions": [
{
"id": "zlc2",
"type": "value",
"localVariables": [{ "name": "지역변수", "value": 0, "id": "zlc2_wmnl" }],
"useLocalVariables": true,
"content": "[[{\"id\":\"2yt2\",\"x\":50,\"y\":30,\"type\":\"function_create_value\",\"params\":[{\"id\":\"45lo\",\"x\":0,\"y\":0,\"type\":\"function_field_label\",\"params\":[\"fib\",{\"id\":\"2wjt\",\"x\":0,\"y\":0,\"type\":\"function_field_string\",\"params\":[{\"id\":\"ec1c\",\"x\":0,\"y\":0,\"type\":\"stringParam_08l7\",\"params\":[null],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]},null],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":false,\"assemble\":false,\"extensions\":[]}],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":false,\"assemble\":true,\"extensions\":[]},null,null,{\"id\":\"alsf\",\"x\":0,\"y\":0,\"type\":\"get_func_variable\",\"params\":[\"zlc2_wmnl\",null],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}],\"statements\":[[{\"id\":\"0bj0\",\"x\":0,\"y\":0,\"type\":\"if_else\",\"params\":[{\"id\":\"68n0\",\"x\":0,\"y\":0,\"type\":\"boolean_basic_operator\",\"params\":[{\"id\":\"mgwf\",\"x\":0,\"y\":0,\"type\":\"stringParam_08l7\",\"params\":[],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]},\"LESS_OR_EQUAL\",{\"id\":\"tldf\",\"x\":0,\"y\":0,\"type\":\"text\",\"params\":[\"2\"],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]},null,null],\"statements\":[[{\"id\":\"u8q3\",\"x\":0,\"y\":0,\"type\":\"set_func_variable\",\"params\":[\"zlc2_wmnl\",{\"id\":\"eq9l\",\"x\":0,\"y\":0,\"type\":\"text\",\"params\":[\"1\"],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]},null],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}],[{\"id\":\"wg6u\",\"x\":0,\"y\":0,\"type\":\"set_func_variable\",\"params\":[\"zlc2_wmnl\",{\"id\":\"fkgl\",\"x\":0,\"y\":0,\"type\":\"calc_basic\",\"params\":[{\"id\":\"n61a\",\"x\":0,\"y\":0,\"type\":\"func_zlc2\",\"params\":[{\"id\":\"rdka\",\"x\":0,\"y\":0,\"type\":\"calc_basic\",\"params\":[{\"id\":\"emxy\",\"x\":0,\"y\":0,\"type\":\"stringParam_08l7\",\"params\":[null],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]},\"MINUS\",{\"id\":\"2flu\",\"x\":0,\"y\":0,\"type\":\"number\",\"params\":[\"1\"],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]},\"PLUS\",{\"id\":\"czxx\",\"x\":0,\"y\":0,\"type\":\"func_zlc2\",\"params\":[{\"id\":\"geww\",\"x\":0,\"y\":0,\"type\":\"calc_basic\",\"params\":[{\"id\":\"8uqz\",\"x\":0,\"y\":0,\"type\":\"stringParam_08l7\",\"params\":[null],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]},\"MINUS\",{\"id\":\"d59y\",\"x\":0,\"y\":0,\"type\":\"number\",\"params\":[\"2\"],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]},null],\"statements\":[],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}]],\"movable\":null,\"deletable\":1,\"emphasized\":false,\"readOnly\":null,\"copyable\":true,\"assemble\":true,\"extensions\":[]}]],\"movable\":null,\"deletable\":false,\"emphasized\":false,\"readOnly\":null,\"copyable\":false,\"assemble\":true,\"extensions\":[]}]]"
}
],
"tables": [],
"speed": 60,
"interface": { "menuWidth": 280, "canvasWidth": 480, "object": "ctqk" },
"expansionBlocks": [],
"aiUtilizeBlocks": [],
"hardwareLiteBlocks": [],
"externalModules": [],
"externalModulesLite": []
}
3 changes: 2 additions & 1 deletion test/proj.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ const projTest = (name: string) =>
})

projTest("proj1")
projTest("proj2")
projTest("proj2")
projTest("fib")

0 comments on commit d5facc7

Please sign in to comment.