diff --git a/builtins.ts b/builtins.ts index 5432817..0ae281b 100644 --- a/builtins.ts +++ b/builtins.ts @@ -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; \ No newline at end of file diff --git a/font.ts b/font.ts index 3f0e5e7..90f7f41 100644 --- a/font.ts +++ b/font.ts @@ -272,4 +272,236 @@ export const font: {[key: string]: Array} = { 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, + ], } \ No newline at end of file diff --git a/index.ts b/index.ts index 69496f0..75acb52 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,7 @@ import { mainloop, frame, + clearScreen, } from "./window.ts"; import { codeSheet } from "./sheet.ts"; import { refreshKeyboard } from "./keyboard.ts"; @@ -11,6 +12,8 @@ const game = codeSheet(0); game.init(); let mode: "play" | "edit" = "edit"; + +clearScreen(); await mainloop((_t) => { // TODO: use t diff --git a/repl.ts b/repl.ts index 8ec35df..df972c5 100644 --- a/repl.ts +++ b/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); - index -= 1; - if (index < 0) { - index = 0; + if (index > 0) { + line = line.slice(0, index-1)+line.slice(index); + index -= 1; } } 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 = { diff --git a/runcode.ts b/runcode.ts new file mode 100644 index 0000000..4ea0a58 --- /dev/null +++ b/runcode.ts @@ -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; +} \ No newline at end of file diff --git a/sheet.ts b/sheet.ts index f683e30..3a45030 100644 --- a/sheet.ts +++ b/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; - }, -}); \ No newline at end of file +addToContext("code_sheet", codeSheet); \ No newline at end of file