Improving repl more
This commit is contained in:
parent
dca54e76ec
commit
2a7003b443
32
builtins.ts
32
builtins.ts
@ -1,10 +1,12 @@
|
|||||||
import {
|
import {
|
||||||
setPixelsInRect,
|
setPixelsInRect,
|
||||||
clearScreen,
|
clearScreen,
|
||||||
|
fillRect,
|
||||||
} 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 { addToContext } from "./runcode.ts";
|
import { addToContext } from "./runcode.ts";
|
||||||
|
import { resetRepl } from "./repl.ts";
|
||||||
|
|
||||||
// deno-fmt-ignore
|
// deno-fmt-ignore
|
||||||
const sprites = [
|
const sprites = [
|
||||||
@ -40,33 +42,33 @@ const sprites = [
|
|||||||
],
|
],
|
||||||
]
|
]
|
||||||
|
|
||||||
const drawSprite = (x: number, y: number, spr: number) => {
|
export const drawSprite = (x: number, y: number, spr: number) => {
|
||||||
setPixelsInRect(x, y, 8, sprites[spr]);
|
setPixelsInRect(x, y, 8, sprites[spr]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const drawChar = (x: number, y: number, char: string) => {
|
export const drawChar = (x: number, y: number, char: string) => {
|
||||||
setPixelsInRect(x, y, 4, font[char]);
|
setPixelsInRect(x, y, 4, font[char]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const drawText = (x: number, y: number, text: string) => {
|
export const drawText = (x: number, y: number, text: string) => {
|
||||||
[...text].forEach((char, i) => {
|
[...text].forEach((char, i) => {
|
||||||
drawChar(x+4*i, y, char);
|
drawChar(x+4*i, y, char);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const drawRect = (x: number, y: number, w: number, h: number, color: number) => {
|
|
||||||
setPixelsInRect(x, y, w, Array(w*h).fill(color));
|
|
||||||
}
|
|
||||||
|
|
||||||
const faux = {
|
const faux = {
|
||||||
clear_screen: clearScreen,
|
cls: () => {
|
||||||
draw_sprite: drawSprite,
|
resetRepl();
|
||||||
draw_text: drawText,
|
clearScreen();
|
||||||
draw_rect: drawRect,
|
},
|
||||||
key_down: keyDown,
|
spr: drawSprite,
|
||||||
key_pressed: keyPressed,
|
txt: drawText,
|
||||||
key_released: keyReleased,
|
rect: fillRect,
|
||||||
|
// key_down: keyDown,
|
||||||
|
// key_pressed: keyPressed,
|
||||||
|
// key_released: keyReleased,
|
||||||
log: console.log,
|
log: console.log,
|
||||||
|
JSON: JSON,
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const key in faux) {
|
for (const key in faux) {
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"sheet_type": "code",
|
"sheet_type": "code",
|
||||||
"value": "x = code_sheet(1);\nreturn {init: () => {y = 0}, update: () => {y += speed; if (y > 127) {y = -6}}, draw: () => {clear_screen(); draw_text(x, y, 'hello world')}}"
|
"value": "x = code_sheet(1);\nreturn ({init: () => {y = 0}, update: () => {y += speed; if (y > 127) {y = -6}}, draw: () => {clear_screen(); draw_text(x, y, 'hello world')}})"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"sheet_type": "code",
|
"sheet_type": "code",
|
||||||
"value": "speed = 2; return 8"
|
"value": "speed = 2; return (8)"
|
||||||
}
|
}
|
||||||
]
|
]
|
2
font.ts
2
font.ts
@ -181,7 +181,7 @@ export const font: {[key: string]: Array<number>} = {
|
|||||||
0, 1, 0, 1,
|
0, 1, 0, 1,
|
||||||
0, 1, 0, 1,
|
0, 1, 0, 1,
|
||||||
0, 1, 0, 1,
|
0, 1, 0, 1,
|
||||||
0, 1, 1, 0,
|
0, 0, 1, 1,
|
||||||
0, 0, 0, 0,
|
0, 0, 0, 0,
|
||||||
],
|
],
|
||||||
"V": [
|
"V": [
|
||||||
|
@ -61,7 +61,7 @@ export const shiftMap = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addEventListener("keydown", (evt) => {
|
addEventListener("keydown", (evt) => {
|
||||||
console.log("keydown", evt.key, evt.key.charCodeAt(0));
|
// console.log("keydown", evt.key, evt.key.charCodeAt(0));
|
||||||
const key = evt.key.charCodeAt(0);
|
const key = evt.key.charCodeAt(0);
|
||||||
const isRepeat = keyboard.has(key) && keyboard.get(key)?.held!;
|
const isRepeat = keyboard.has(key) && keyboard.get(key)?.held!;
|
||||||
keyboard.set(key, {
|
keyboard.set(key, {
|
||||||
@ -72,7 +72,7 @@ addEventListener("keydown", (evt) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
addEventListener("keyup", (evt) => {
|
addEventListener("keyup", (evt) => {
|
||||||
console.log("keyup", evt.key, evt.key.charCodeAt(0));
|
// console.log("keyup", evt.key, evt.key.charCodeAt(0));
|
||||||
const key = evt.key.charCodeAt(0);
|
const key = evt.key.charCodeAt(0);
|
||||||
keyboard.set(key, {
|
keyboard.set(key, {
|
||||||
first: false,
|
first: false,
|
||||||
|
53
repl.ts
53
repl.ts
@ -1,8 +1,8 @@
|
|||||||
import faux from "./builtins.ts";
|
import { drawText} 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";
|
import { addToContext, evalCode } from "./runcode.ts";
|
||||||
import { clearScreen } from "./window.ts";
|
import { clearScreen, fillRect } from "./window.ts";
|
||||||
|
|
||||||
const lineHeight = 6;
|
const lineHeight = 6;
|
||||||
|
|
||||||
@ -21,6 +21,25 @@ export const resetRepl = () => {
|
|||||||
index = 0;
|
index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const print = (arg: unknown) => {
|
||||||
|
textLinesAbove.push(...String(arg).split("\n").flatMap((line)=>{
|
||||||
|
const wrap = [];
|
||||||
|
while (line.length) {
|
||||||
|
wrap.push(line.slice(0,32));
|
||||||
|
line = line.slice(32);
|
||||||
|
}
|
||||||
|
return wrap;
|
||||||
|
}));
|
||||||
|
textLinesAbove = textLinesAbove.slice(-20);
|
||||||
|
}
|
||||||
|
|
||||||
|
const printVal = (arg: unknown) => {
|
||||||
|
print(JSON.stringify(arg));
|
||||||
|
}
|
||||||
|
|
||||||
|
addToContext("print", print);
|
||||||
|
addToContext("printVal", printVal);
|
||||||
|
|
||||||
const update = () => {
|
const update = () => {
|
||||||
for (const key of getKeysPressed()) {
|
for (const key of getKeysPressed()) {
|
||||||
let char = String.fromCharCode(key).toLowerCase();
|
let char = String.fromCharCode(key).toLowerCase();
|
||||||
@ -66,19 +85,19 @@ const update = () => {
|
|||||||
currentLine = lineAtHistory;
|
currentLine = lineAtHistory;
|
||||||
index = currentLine.length;
|
index = currentLine.length;
|
||||||
} else if (key === K.ENTER) {
|
} else if (key === K.ENTER) {
|
||||||
history.push(currentLine);
|
if (currentLine) {
|
||||||
historyIndex = history.length;
|
history.push(currentLine);
|
||||||
textLinesAbove.push("> "+currentLine);
|
historyIndex = history.length;
|
||||||
|
}
|
||||||
|
print("> "+currentLine);
|
||||||
try {
|
try {
|
||||||
const ran = runCode('return '+currentLine);
|
const ran = evalCode(currentLine);
|
||||||
const resultString = ran?.toString?.() ?? null;
|
if (ran !== undefined) {
|
||||||
if (resultString !== null) {
|
printVal(ran);
|
||||||
textLinesAbove.push(...resultString.split("\n"))
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
textLinesAbove.push(...(err.name+":\n"+err.message).split("\n"))
|
print(err.name+":\n"+err.message);
|
||||||
}
|
}
|
||||||
textLinesAbove = textLinesAbove.slice(-20);
|
|
||||||
maxLineLen = 0;
|
maxLineLen = 0;
|
||||||
currentLine = "";
|
currentLine = "";
|
||||||
index = 0;
|
index = 0;
|
||||||
@ -89,8 +108,8 @@ const update = () => {
|
|||||||
|
|
||||||
const drawTextAbove = () => {
|
const drawTextAbove = () => {
|
||||||
textLinesAbove.forEach((line, i) => {
|
textLinesAbove.forEach((line, i) => {
|
||||||
faux.draw_rect(0, 1+i*lineHeight, 4*(line.length+1)+1, lineHeight+1, 0);
|
fillRect(0, 1+i*lineHeight, 4*(line.length+1)+1, lineHeight+1, 0);
|
||||||
faux.draw_text(-1, 1+i*lineHeight, line);
|
drawText(-1, 1+i*lineHeight, line);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,9 +118,9 @@ const draw = () => {
|
|||||||
|
|
||||||
drawTextAbove();
|
drawTextAbove();
|
||||||
|
|
||||||
faux.draw_rect(0, 1+textLinesAbove.length*lineHeight, 4*(2+maxLineLen+1)+1, lineHeight+1, 0);
|
fillRect(0, 1+textLinesAbove.length*lineHeight, 4*(2+maxLineLen+1)+1, lineHeight+1, 0);
|
||||||
faux.draw_rect((2+index)*4, textLinesAbove.length*lineHeight+1, 4, lineHeight-1, 3);
|
fillRect((2+index)*4, textLinesAbove.length*lineHeight+1, 4, lineHeight-1, 3);
|
||||||
faux.draw_text(-1, 1+textLinesAbove.length*lineHeight, "> "+currentLine);
|
drawText(-1, 1+textLinesAbove.length*lineHeight, "> "+currentLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const repl = {
|
export const repl = {
|
||||||
|
16
runcode.ts
16
runcode.ts
@ -1,5 +1,7 @@
|
|||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
const G: any = {};
|
const G: any = {
|
||||||
|
eval: eval,
|
||||||
|
};
|
||||||
const context = new Proxy(G, {
|
const context = new Proxy(G, {
|
||||||
get: (target, prop) => {
|
get: (target, prop) => {
|
||||||
return target[prop];
|
return target[prop];
|
||||||
@ -27,6 +29,18 @@ export const runCode = (code: string) => {
|
|||||||
return fn(context);
|
return fn(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const evalCode = (code: string) => {
|
||||||
|
try {
|
||||||
|
return runCode(`return eval("(${code.replaceAll('"', '\\"')})");`);
|
||||||
|
} catch (err) {
|
||||||
|
if (err.name === "SyntaxError") {
|
||||||
|
return runCode(`return eval("${code.replaceAll('"', '\\"')}");`);
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
export const addToContext = (name: string, value: any) => {
|
export const addToContext = (name: string, value: any) => {
|
||||||
G[name] = value;
|
G[name] = value;
|
||||||
|
@ -153,6 +153,10 @@ export const setPixelsInRect = (x: number, y: number, w: number, pixels: Array<n
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const fillRect = (x: number, y: number, w: number, h: number, color: number) => {
|
||||||
|
setPixelsInRect(x, y, w, Array(w*h).fill(color));
|
||||||
|
}
|
||||||
|
|
||||||
export const clearScreen = () => {
|
export const clearScreen = () => {
|
||||||
allPixelColors.fill(0);
|
allPixelColors.fill(0);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user