almost a real repl

This commit is contained in:
dylan 2023-05-03 15:17:27 -07:00
parent 7de521bd39
commit 1482288b0c
6 changed files with 308 additions and 35 deletions

View File

@ -4,7 +4,7 @@ import {
} from "./window.ts"; } from "./window.ts";
import { font } from "./font.ts"; import { font } from "./font.ts";
import { keyDown, keyPressed, keyReleased } from "./keyboard.ts"; import { keyDown, keyPressed, keyReleased } from "./keyboard.ts";
// import { codeSheet } from "./sheet.ts"; import { addToContext } from "./runcode.ts";
// deno-fmt-ignore // deno-fmt-ignore
const sprites = [ const sprites = [
@ -66,7 +66,11 @@ const faux = {
key_down: keyDown, key_down: keyDown,
key_pressed: keyPressed, key_pressed: keyPressed,
key_released: keyReleased, 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; export default faux;

232
font.ts
View File

@ -272,4 +272,236 @@ export const font: {[key: string]: Array<number>} = {
0, 1, 1, 1, 0, 1, 1, 1,
0, 0, 0, 0, 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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
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,
],
} }

View File

@ -1,6 +1,7 @@
import { import {
mainloop, mainloop,
frame, frame,
clearScreen,
} from "./window.ts"; } from "./window.ts";
import { codeSheet } from "./sheet.ts"; import { codeSheet } from "./sheet.ts";
import { refreshKeyboard } from "./keyboard.ts"; import { refreshKeyboard } from "./keyboard.ts";
@ -11,6 +12,8 @@ const game = codeSheet(0);
game.init(); game.init();
let mode: "play" | "edit" = "edit"; let mode: "play" | "edit" = "edit";
clearScreen();
await mainloop((_t) => { await mainloop((_t) => {
// TODO: use t // TODO: use t

38
repl.ts
View File

@ -1,11 +1,22 @@
import faux from "./builtins.ts"; import faux from "./builtins.ts";
import { getKeysPressed, shiftKeyDown, shiftMap, K } from "./keyboard.ts"; import { getKeysPressed, shiftKeyDown, shiftMap, K } from "./keyboard.ts";
import { font } from "./font.ts"; import { font } from "./font.ts";
import { runCode } from "./runcode.ts";
const lineHeight = 6;
let maxLineLen = 0;
let line = ""; let line = "";
let index = 0; let index = 0;
let y = 0;
let result = "";
const update = () => { const update = () => {
if (result) {
console.log("result:", result);
y += lineHeight; // TODO: multiply if multiline
result = "";
}
for (const key of getKeysPressed()) { for (const key of getKeysPressed()) {
let char = String.fromCharCode(key).toLowerCase(); let char = String.fromCharCode(key).toLowerCase();
if (shiftKeyDown()) { if (shiftKeyDown()) {
@ -19,10 +30,9 @@ const update = () => {
line = line.slice(0, index)+char+line.slice(index); line = line.slice(0, index)+char+line.slice(index);
index += 1; index += 1;
} else if (key === K.BACKSPACE) { } else if (key === K.BACKSPACE) {
line = line.slice(0, -1); if (index > 0) {
index -= 1; line = line.slice(0, index-1)+line.slice(index);
if (index < 0) { index -= 1;
index = 0;
} }
} else if (key === K.ARROW_LEFT) { } else if (key === K.ARROW_LEFT) {
index -= 1; index -= 1;
@ -34,14 +44,28 @@ const update = () => {
if (index > line.length) { if (index > line.length) {
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 = () => { const draw = () => {
faux.clear_screen(); if (result) {
faux.draw_rect((2+index)*4, 0, 5, 6, 3); faux.draw_rect(0, y, 4*result.length+1, lineHeight+1, 6);
faux.draw_text(0, 0, "> "+line); 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 = { export const repl = {

33
runcode.ts Normal file
View 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;
}

View File

@ -1,5 +1,5 @@
import faux from "./builtins.ts";
import { getCart } from "./cart.ts"; import { getCart } from "./cart.ts";
import { runCode, addToContext } from "./runcode.ts";
export type SheetType = "code" | "spritesheet" | "map" | "sfx" | "patterns" | "fonts"; export type SheetType = "code" | "spritesheet" | "map" | "sfx" | "patterns" | "fonts";
@ -9,30 +9,7 @@ const getSheet = (n: number) => {
export const codeSheet = (sheet: number) => { export const codeSheet = (sheet: number) => {
const code = getSheet(sheet); const code = getSheet(sheet);
try { return runCode(code);
new Function(code);
} catch (err) {
throw err;
}
const fn = new Function("context", `
with (context) {
${code}
}
`);
return fn(context);
} }
// deno-lint-ignore no-explicit-any addToContext("code_sheet", codeSheet);
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;
},
});