From dc79c96f63d166cc25aec563cdac278fef7692ff Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Fri, 25 Jun 2021 13:05:12 +0200 Subject: [PATCH] more settings --- ibfi-ts/.gitignore | 3 + ibfi-ts/README.md | 61 ++++++---------- ibfi-ts/package.json | 2 +- ibfi-ts/src/App.scss | 19 ++++- ibfi-ts/src/App.tsx | 3 +- ibfi-ts/src/brainfuck/Interpreter.ts | 100 ++++++++++++++++---------- ibfi-ts/src/components/CodeInput.tsx | 46 ++++++++++-- ibfi-ts/src/components/RunDisplay.tsx | 48 ++++++++++++- ibfi-ts/src/components/Runner.tsx | 71 +++++++++++------- ibfi-ts/src/presets.json | 9 +++ 10 files changed, 246 insertions(+), 116 deletions(-) create mode 100644 ibfi-ts/src/presets.json diff --git a/ibfi-ts/.gitignore b/ibfi-ts/.gitignore index 8996736..4d29575 100644 --- a/ibfi-ts/.gitignore +++ b/ibfi-ts/.gitignore @@ -8,6 +8,9 @@ # testing /coverage +# production +/build + # misc .DS_Store .env.local diff --git a/ibfi-ts/README.md b/ibfi-ts/README.md index b58e0af..1ff769f 100644 --- a/ibfi-ts/README.md +++ b/ibfi-ts/README.md @@ -1,46 +1,25 @@ -# Getting Started with Create React App +# Interactive Brainfuck Interpreter in React TS -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). +This is an interactive interpreter for the brainfuck programming language. +It provides simple brainfuck execution, along with a view of the memory, the memory pointer, the current state of the +code and more. -## Available Scripts +It is great for debugging brainfuck programs, including a memory view, program view, the location of the pointers, the +ability to directly edit memory. +You can step manually through the program, or set the execution speed to whatever speed fits best. +This interpreter also has the ability to set breakpoints using the • symbol. This means you can let your code run fast and +stopping it at any point in your program to see what went wrong. -In the project directory, you can run: +## Features +* brainfuck execution including IO +* memory view +* code state view +* manual stepping +* breakpoints +* error messages -### `yarn start` -Runs the app in the development mode.\ -Open [http://localhost:3000](http://localhost:3000) to view it in the browser. - -The page will reload if you make edits.\ -You will also see any lint errors in the console. - -### `yarn test` - -Launches the test runner in the interactive watch mode.\ -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. - -### `yarn build` - -Builds the app for production to the `build` folder.\ -It correctly bundles React in production mode and optimizes the build for the best performance. - -The build is minified and the filenames include the hashes.\ -Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `yarn eject` - -**Note: this is a one-way operation. Once you `eject`, you can’t go back!** - -If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. - -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. - -You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). +### Future features +* seeing ASCII characters in memory +* fast excecution mode (no debugging info) +* better speed control \ No newline at end of file diff --git a/ibfi-ts/package.json b/ibfi-ts/package.json index fbd4649..3ee8ce1 100644 --- a/ibfi-ts/package.json +++ b/ibfi-ts/package.json @@ -24,7 +24,7 @@ "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject", - "deploy": "gh-pages -d build" + "deploy": "yarn build && gh-pages -d build" }, "eslintConfig": { "extends": [ diff --git a/ibfi-ts/src/App.scss b/ibfi-ts/src/App.scss index ea759ab..82e8143 100644 --- a/ibfi-ts/src/App.scss +++ b/ibfi-ts/src/App.scss @@ -38,6 +38,7 @@ $medium-color: #78787f; .code-display-wrapper { max-width: 80vw; + font-family: monospace; span { word-wrap: break-word; @@ -55,6 +56,15 @@ $medium-color: #78787f; min-width: 60px; text-align: center; } + + .array-set-value-field { + min-width: 50px; + max-width: 100px; + height: 40px; + color: $light-color; + font-size: 30px; + background-color: $main-color-brighter; + } } .bf-run { @@ -65,6 +75,11 @@ $medium-color: #78787f; width: 200px; } + .small-speed-button { + height: 40px; + width: 40px; + } + .program-input-area { resize: none; width: 80vw; @@ -72,8 +87,8 @@ $medium-color: #78787f; font-size: 30px; } - .error { - background-color: #664242FF; + .info { + background-color: #579ca7; } } diff --git a/ibfi-ts/src/App.tsx b/ibfi-ts/src/App.tsx index 28f65f3..0b2efd7 100644 --- a/ibfi-ts/src/App.tsx +++ b/ibfi-ts/src/App.tsx @@ -10,7 +10,7 @@ function App() { const [running, setRunning] = useState(false); const outHandler = useCallback((char: number) => { - setOut(out => out + String.fromCharCode(char)) + setOut(oldOut => oldOut + String.fromCharCode(char)) }, []); const runHandler = (run: boolean) => { @@ -21,7 +21,6 @@ function App() { } const inputHandler = (code: string, options: CodeOptions) => setInput([code, options]); - return (
{ diff --git a/ibfi-ts/src/brainfuck/Interpreter.ts b/ibfi-ts/src/brainfuck/Interpreter.ts index fd4971e..32cf9f9 100644 --- a/ibfi-ts/src/brainfuck/Interpreter.ts +++ b/ibfi-ts/src/brainfuck/Interpreter.ts @@ -12,15 +12,17 @@ export default class Interpreter { private readonly _inHandler: InHandler; private readonly _outHandler: OutHandler; - private readonly _errorHandler: ErrorHandler; - constructor(input: [string, CodeOptions], outHandler: OutHandler, inHandler: InHandler, errorHandler: ErrorHandler) { + private readonly _options: CodeOptions; + + constructor(input: [string, CodeOptions], outHandler: OutHandler, inHandler: InHandler) { const buf = new ArrayBuffer(32000); this._array = new Uint8Array(buf); this._pointer = 0; + this._options = input[1]; if (input[1].minify) { - this._code = minify(input[0]) + this._code = this.minify(input[0]) } else { this._code = input[0]; } @@ -28,7 +30,6 @@ export default class Interpreter { this._programCounter = 0; this._inHandler = inHandler; this._outHandler = outHandler; - this._errorHandler = errorHandler; } public next() { @@ -44,8 +45,7 @@ export default class Interpreter { break; case '<': if (this._pointer === 0) { - this._errorHandler("Cannot wrap left"); - break; + throw new Error("Cannot wrap left"); } this._pointer--; break; @@ -53,45 +53,66 @@ export default class Interpreter { this._outHandler(this.value); break; case ',': - try { - this._array[this._pointer] = this._inHandler(); - } catch { - this._programCounter--; - this._errorHandler("Could not read input, trying again next time.") - } + this.input(); break; case '[': - if (this.value === 0) { - let level = 0; - while (this.lastInstruction !== ']' || level > -1) { - this._programCounter++; - if (this.lastInstruction === '[') level++; - else if (this.lastInstruction === ']') level--; - } - } + this.loopForwards(); break; case ']': - if (this.value !== 0) { - let level = 0; - while (this.lastInstruction !== '[' || level > -1) { - this._programCounter--; - if (this.lastInstruction === '[') level--; - else if (this.lastInstruction === ']') level++; - } + this.loopBackwards(); + break; + case '•': + if (this._options.enableBreakpoints) { + throw new Error("Breakpoint reached"); } break; case undefined: this._pointer = this._code.length; - console.warn("reached end"); break; - default: { + default: + break; + } + } + + private loopForwards() { + if (this.value === 0) { + let level = 0; + while (this.lastInstruction !== ']' || level > -1) { + this._programCounter++; + if (this._programCounter > this._code.length) { + throw new Error("Reached end of code while searching ']'"); + } + if (this.lastInstruction === '[') level++; + else if (this.lastInstruction === ']') level--; } } - console.log(`char: ${this.code[this.programCounter - 1]} pointer: ${this.pointer} value: ${this.array[this.pointer]}`) + } + + private loopBackwards() { + if (this.value !== 0) { + let level = 0; + while (this.lastInstruction !== '[' || level > -1) { + this._programCounter--; + if (this._programCounter < 0) { + throw new Error("Reached start of code while searching '['"); + } + if (this.lastInstruction === '[') level--; + else if (this.lastInstruction === ']') level++; + } + } + } + + + private input() { + try { + this._array[this._pointer] = this._inHandler(); + } catch { + this._programCounter--; + } } public prev() { - + // - } get reachedEnd(): boolean { @@ -121,11 +142,18 @@ export default class Interpreter { get programCounter(): number { return this._programCounter; } + + private minify(code: string): string { + const CHARS = ['+', '-', '<', '>', '.', ',', '[', ']']; + if (this._options.enableBreakpoints) { + CHARS.push('•'); + } + + return code.split("") + .filter(c => CHARS.includes(c)) + .join(""); + } } -const CHARS = ['+', '-', '<', '>', '.', ',', '[', ']']; -const minify = (code: string): string => - code.split("") - .filter(c => CHARS.includes(c)) - .join(""); + diff --git a/ibfi-ts/src/components/CodeInput.tsx b/ibfi-ts/src/components/CodeInput.tsx index 393635e..7a7c1d8 100644 --- a/ibfi-ts/src/components/CodeInput.tsx +++ b/ibfi-ts/src/components/CodeInput.tsx @@ -1,7 +1,10 @@ import React, {useState} from 'react'; +import presets from "../presets.json"; export interface CodeOptions { - minify?: boolean + minify?: boolean, + directStart?: boolean, + enableBreakpoints?: boolean } interface CodeInputProps { @@ -15,10 +18,8 @@ const CodeInput = ({code, setInput}: CodeInputProps) => { const [codeOptions, setCodeOptions] = useState({}); - const setStart = () => { - setInput( - "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.", - codeOptions); + const setPreset = (name: keyof typeof presets) => () => { + setInput(presets[name], codeOptions); } const changeMinify = (e: React.ChangeEvent) => { @@ -26,6 +27,16 @@ const CodeInput = ({code, setInput}: CodeInputProps) => { setInput(code, codeOptions); } + const changeStart = (e: React.ChangeEvent) => { + setCodeOptions(old => ({...old, directStart: e.target.checked})) + setInput(code, codeOptions); + } + + const changeBreakpoint = (e: React.ChangeEvent) => { + setCodeOptions(old => ({...old, enableBreakpoints: e.target.checked})) + setInput(code, codeOptions); + } + return (
@@ -34,14 +45,35 @@ const CodeInput = ({code, setInput}: CodeInputProps) => { setFontSize(+v.target.value)}/> - + + + + + + + + + + +