-
Notifications
You must be signed in to change notification settings - Fork 0
/
foasm.js
109 lines (94 loc) · 3.27 KB
/
foasm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
(function(Scratch) {
"use strict";
const { BlockType, ArgumentType } = Scratch;
function splitFoasm(rawcode){
let splitcode = rawcode.split(";");
let code = [];
for (const line of splitcode) {
code.push(line.split(" "));
}
return code;
}
class Foasm {
getInfo() {
return {
id: "foasm",
name: "Flufi OS assembly",
color1: "#ad1090",
blocks: [
{
opcode: "runFoasm",
blockType: BlockType.REPORTER,
text: "Run Foasm [code] with params [params]",
arguments: {
code: { type: ArgumentType.STRING, defaultValue: "" },
params: { type: ArgumentType.NUMBER, defaultValue: "{}" }
}
},
{
opcode: "splitFoasm",
blockType: BlockType.REPORTER,
text: "Split Foasm [code]",
arguments: {
code: { type: ArgumentType.STRING, defaultValue: "set out hello :3;out @out" }
}
}
],
menus: {}
};
}
splitFoasm({ code }) {
return splitFoasm(code);
}
runFoasm({ code, params }) {
if (typeof code !== 'object') {return ""}
function Eval(param) {
if (param[0] == "@") {
return mem[param.slice(1)];
}
return param;
}
let labels = {};
let sindex = 0;
while (sindex < code.length) {
let line = code[sindex];
if (line[0] == "lbl") {
let v = line.concat();
v.splice(0, 2);
labels[v.join(" ")] = sindex
}
sindex ++;
}
let mem = {};
let index = 0;
while (index < code.length) {
let line = code[index];
switch (line[0]) {
case "set":
let v = line.concat();
v.splice(0, 2);
mem[line[1]] = v.join(" ");
break
case "del":
mem[line[1]] = null;
break
case "out":
return Eval(line[1]);
case "lbl": break
case "jmp":
let jmp_l = line.concat();
jmp_l.splice(0, 1).join(" ");
console.log(labels,jmp_l);
if (Object.keys(jmp_l)) {
}
break
default:
console.error("unknown cmd '" + line[0] + "'");
break
}
index ++;
}
}
}
Scratch.extensions.register(new Foasm());
})(Scratch);