almost a real repl
This commit is contained in:
parent
7de521bd39
commit
1482288b0c
@ -4,7 +4,7 @@ import {
|
||||
} from "./window.ts";
|
||||
import { font } from "./font.ts";
|
||||
import { keyDown, keyPressed, keyReleased } from "./keyboard.ts";
|
||||
// import { codeSheet } from "./sheet.ts";
|
||||
import { addToContext } from "./runcode.ts";
|
||||
|
||||
// deno-fmt-ignore
|
||||
const sprites = [
|
||||
@ -66,7 +66,11 @@ const faux = {
|
||||
key_down: keyDown,
|
||||
key_pressed: keyPressed,
|
||||
key_released: keyReleased,
|
||||
// code_sheet: codeSheet,
|
||||
log: console.log,
|
||||
};
|
||||
|
||||
for (const key in faux) {
|
||||
addToContext(key, faux[key as keyof typeof faux]);
|
||||
}
|
||||
|
||||
export default faux;
|
232
font.ts
232
font.ts
@ -272,4 +272,236 @@ export const font: {[key: string]: Array<number>} = {
|
||||
0, 1, 1, 1,
|
||||
0, 0, 0, 0,
|
||||
],
|
||||
"(": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
],
|
||||
")": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 0, 0,
|
||||
],
|
||||
"[": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 1,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 1,
|
||||
],
|
||||
"]": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 1, 0,
|
||||
],
|
||||
"{": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 1,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 1,
|
||||
],
|
||||
"}": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 1,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 1, 0,
|
||||
],
|
||||
":": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
],
|
||||
";": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 0, 0,
|
||||
],
|
||||
"'": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
],
|
||||
'"': [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 0, 1,
|
||||
0, 1, 0, 1,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
],
|
||||
"1": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 1, 1,
|
||||
],
|
||||
"2": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 0, 1,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 1, 1,
|
||||
],
|
||||
"3": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 1, 1,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
0, 1, 1, 0,
|
||||
],
|
||||
"4": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 0, 1,
|
||||
0, 1, 0, 1,
|
||||
0, 1, 1, 1,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 0, 1,
|
||||
],
|
||||
"5": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 1, 1,
|
||||
0, 1, 0, 0,
|
||||
0, 1, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
0, 1, 1, 0,
|
||||
],
|
||||
"6": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 1, 1,
|
||||
0, 1, 0, 1,
|
||||
0, 1, 1, 1,
|
||||
],
|
||||
"7": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 1, 1,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
],
|
||||
"8": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 1, 1,
|
||||
0, 1, 0, 1,
|
||||
0, 1, 1, 1,
|
||||
0, 1, 0, 1,
|
||||
0, 1, 1, 1,
|
||||
],
|
||||
"9": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 1, 1,
|
||||
0, 1, 0, 1,
|
||||
0, 1, 1, 1,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 0, 0,
|
||||
],
|
||||
"0": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 1, 1,
|
||||
0, 1, 0, 1,
|
||||
0, 1, 0, 1,
|
||||
0, 1, 0, 1,
|
||||
0, 1, 1, 1,
|
||||
],
|
||||
"+": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 1, 1,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
],
|
||||
"-": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 1, 1, 1,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
],
|
||||
"_": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 1, 1, 1,
|
||||
],
|
||||
"`": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
],
|
||||
"~": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 1,
|
||||
0, 1, 1, 1,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
],
|
||||
"/": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 1, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
],
|
||||
"?": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 1, 1,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 1, 1,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
],
|
||||
"\\": [
|
||||
0, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 0, 1,
|
||||
],
|
||||
"|": [
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 1, 0,
|
||||
],
|
||||
}
|
3
index.ts
3
index.ts
@ -1,6 +1,7 @@
|
||||
import {
|
||||
mainloop,
|
||||
frame,
|
||||
clearScreen,
|
||||
} from "./window.ts";
|
||||
import { codeSheet } from "./sheet.ts";
|
||||
import { refreshKeyboard } from "./keyboard.ts";
|
||||
@ -12,6 +13,8 @@ game.init();
|
||||
|
||||
let mode: "play" | "edit" = "edit";
|
||||
|
||||
clearScreen();
|
||||
|
||||
await mainloop((_t) => {
|
||||
// TODO: use t
|
||||
if (mode === "play") {
|
||||
|
36
repl.ts
36
repl.ts
@ -1,11 +1,22 @@
|
||||
import faux from "./builtins.ts";
|
||||
import { getKeysPressed, shiftKeyDown, shiftMap, K } from "./keyboard.ts";
|
||||
import { font } from "./font.ts";
|
||||
import { runCode } from "./runcode.ts";
|
||||
|
||||
const lineHeight = 6;
|
||||
|
||||
let maxLineLen = 0;
|
||||
let line = "";
|
||||
let index = 0;
|
||||
let y = 0;
|
||||
let result = "";
|
||||
|
||||
const update = () => {
|
||||
if (result) {
|
||||
console.log("result:", result);
|
||||
y += lineHeight; // TODO: multiply if multiline
|
||||
result = "";
|
||||
}
|
||||
for (const key of getKeysPressed()) {
|
||||
let char = String.fromCharCode(key).toLowerCase();
|
||||
if (shiftKeyDown()) {
|
||||
@ -19,10 +30,9 @@ const update = () => {
|
||||
line = line.slice(0, index)+char+line.slice(index);
|
||||
index += 1;
|
||||
} else if (key === K.BACKSPACE) {
|
||||
line = line.slice(0, -1);
|
||||
if (index > 0) {
|
||||
line = line.slice(0, index-1)+line.slice(index);
|
||||
index -= 1;
|
||||
if (index < 0) {
|
||||
index = 0;
|
||||
}
|
||||
} else if (key === K.ARROW_LEFT) {
|
||||
index -= 1;
|
||||
@ -34,14 +44,28 @@ const update = () => {
|
||||
if (index > line.length) {
|
||||
index = line.length;
|
||||
}
|
||||
} else if (key === K.ENTER) {
|
||||
const ran = runCode('return '+line);
|
||||
console.log('ran:', ran);
|
||||
result = ran?.toString?.();
|
||||
maxLineLen = 0;
|
||||
line = "";
|
||||
index = 0;
|
||||
y += lineHeight;
|
||||
}
|
||||
}
|
||||
maxLineLen = Math.max(maxLineLen, line.length);
|
||||
}
|
||||
|
||||
const draw = () => {
|
||||
faux.clear_screen();
|
||||
faux.draw_rect((2+index)*4, 0, 5, 6, 3);
|
||||
faux.draw_text(0, 0, "> "+line);
|
||||
if (result) {
|
||||
faux.draw_rect(0, y, 4*result.length+1, lineHeight+1, 6);
|
||||
faux.draw_text(0, y, result);
|
||||
} else {
|
||||
faux.draw_rect(0, y, 4*(2+maxLineLen+1)+1, lineHeight+1, 6);
|
||||
faux.draw_rect((2+index)*4, y+1, 4, lineHeight-1, 3);
|
||||
faux.draw_text(0, y, "> "+line);
|
||||
}
|
||||
}
|
||||
|
||||
export const repl = {
|
||||
|
33
runcode.ts
Normal file
33
runcode.ts
Normal file
@ -0,0 +1,33 @@
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const G: any = {};
|
||||
const context = new Proxy(G, {
|
||||
get: (target, prop) => {
|
||||
return target[prop];
|
||||
},
|
||||
set: (target, prop, value) => {
|
||||
target[prop] = value;
|
||||
return true;
|
||||
},
|
||||
has: () => {
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
export const runCode = (code: string) => {
|
||||
try {
|
||||
new Function(code);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
const fn = new Function("context", `
|
||||
with (context) {
|
||||
${code}
|
||||
}
|
||||
`);
|
||||
return fn(context);
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export const addToContext = (name: string, value: any) => {
|
||||
G[name] = value;
|
||||
}
|
29
sheet.ts
29
sheet.ts
@ -1,5 +1,5 @@
|
||||
import faux from "./builtins.ts";
|
||||
import { getCart } from "./cart.ts";
|
||||
import { runCode, addToContext } from "./runcode.ts";
|
||||
|
||||
export type SheetType = "code" | "spritesheet" | "map" | "sfx" | "patterns" | "fonts";
|
||||
|
||||
@ -9,30 +9,7 @@ const getSheet = (n: number) => {
|
||||
|
||||
export const codeSheet = (sheet: number) => {
|
||||
const code = getSheet(sheet);
|
||||
try {
|
||||
new Function(code);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
const fn = new Function("context", `
|
||||
with (context) {
|
||||
${code}
|
||||
}
|
||||
`);
|
||||
return fn(context);
|
||||
return runCode(code);
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const G: any = {...faux, faux, log: console.log, code_sheet: codeSheet};
|
||||
const context = new Proxy(G, {
|
||||
get: (target, prop) => {
|
||||
return target[prop];
|
||||
},
|
||||
set: (target, prop, value) => {
|
||||
target[prop] = value;
|
||||
return true;
|
||||
},
|
||||
has: () => {
|
||||
return true;
|
||||
},
|
||||
});
|
||||
addToContext("code_sheet", codeSheet);
|
Loading…
x
Reference in New Issue
Block a user