From 07cd3e85d28031c36d7eb0608e32c27131286435 Mon Sep 17 00:00:00 2001 From: rahularya Date: Wed, 3 Jul 2019 03:11:55 -0700 Subject: [PATCH] feat: Implement Scheme tree debug + autodraw functionality --- src/languages/python/utils/debugPrefix.js | 9 +++++++++ src/languages/scheme/utils/debugPrefix.js | 1 + src/languages/scheme/web/brython_suffix.py | 22 +++++++++++++++------- src/languages/scheme/web/communication.js | 8 ++++---- src/renderer/components/File.js | 12 ++---------- src/renderer/utils/dispatch.js | 10 ++++++++++ 6 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 src/languages/python/utils/debugPrefix.js create mode 100644 src/languages/scheme/utils/debugPrefix.js diff --git a/src/languages/python/utils/debugPrefix.js b/src/languages/python/utils/debugPrefix.js new file mode 100644 index 0000000..fd034f2 --- /dev/null +++ b/src/languages/python/utils/debugPrefix.js @@ -0,0 +1,9 @@ +export default `# these lines stub out the debugging functions you have available +def draw(something): pass +def autodraw(): pass +def disableAutodraw(): pass +def visualize(): pass +def editor(): pass + +# your code is below +`; diff --git a/src/languages/scheme/utils/debugPrefix.js b/src/languages/scheme/utils/debugPrefix.js new file mode 100644 index 0000000..9cf3c27 --- /dev/null +++ b/src/languages/scheme/utils/debugPrefix.js @@ -0,0 +1 @@ +export default ""; diff --git a/src/languages/scheme/web/brython_suffix.py b/src/languages/scheme/web/brython_suffix.py index bdd556f..4491e70 100644 --- a/src/languages/scheme/web/brython_suffix.py +++ b/src/languages/scheme/web/brython_suffix.py @@ -9,11 +9,11 @@ _launchtext = """CS61A Scheme Web Interpreter -------------------------------------------------------------------------------- Welcome to the 61A Scheme web interpreter! -Source for this interpreter is restricted. +The source for this interpreter is restricted, but you'll build it yourself as your Scheme Project! -[in the future] To visualize a list, call (draw ). -[in the future] To draw list visualizations automatically, call (autodraw). -[in the future] To view an environment diagram of your entire program, call (visualize). +To visualize a list, call (draw ). +To draw list visualizations automatically, call (autodraw). +To view an environment diagram of your entire program, call (visualize). To launch an editor associated with your console, call (editor). """ @@ -44,9 +44,15 @@ def exit(data): firstLine = True -@builtin("editor") -def editor(): - print("EDITOR: ") +def record_exec(code, wrap): + if wrap: + out = "(ignore-error\n" + for line in code.split("\n"): + out += " " + line + "\n" + out += ")\n" + record_exec(out, False) + else: + print("EXEC: " + code) frame = create_global_frame() @@ -94,8 +100,10 @@ def run_expr(expr): ret = scheme_eval(expr, frame) if ret is not None: print(ret) + record_exec(str(expr), False) except Exception as err: handle_error(frame) + record_exec(str(expr), True) if isinstance(err, RuntimeError): print('Error: maximum recursion depth exceeded') else: diff --git a/src/languages/scheme/web/communication.js b/src/languages/scheme/web/communication.js index 7b610d6..132a807 100644 --- a/src/languages/scheme/web/communication.js +++ b/src/languages/scheme/web/communication.js @@ -4,8 +4,8 @@ import { err, exit, sendAndExit } from "../../../web/webBackend.js"; import { interactProcess } from "../../../main/processes.js"; // import webConsole from "./web_console.py"; -import interpreter from "./IGNORE_needed.py"; -// import transpiledInterpreter from "!!raw-loader!./IGNORE_scheme_transpiled.js"; +// import interpreter from "./IGNORE_needed.py"; +import transpiledInterpreter from "!!raw-loader!./IGNORE_scheme_transpiled.js"; import runPyScript from "../../../web/runPython.js"; export default async function receive(arg) { @@ -29,7 +29,7 @@ export default async function receive(arg) { } async function runScmCode(key, code) { - await runPyScript(key, interpreter, []); - // await runPyScript(key, transpiledInterpreter, { transpiled: true }); + // await runPyScript(key, interpreter, []); + await runPyScript(key, transpiledInterpreter, { transpiled: true }); interactProcess(key, code); } diff --git a/src/renderer/components/File.js b/src/renderer/components/File.js index f0c6f2f..8f79514 100644 --- a/src/renderer/components/File.js +++ b/src/renderer/components/File.js @@ -5,7 +5,7 @@ import { send, sendNoInteract } from "../utils/communication.js"; import { SAVE_FILE, SHOW_ERROR_DIALOG, SHOW_SAVE_DIALOG } from "../../common/communicationEnums.js"; import { PYTHON, SCHEME, SQL } from "../../common/languages.js"; import { - Debugger, + Debugger, debugPrefix, format, generateDebugTrace, runCode, runFile, } from "../utils/dispatch.js"; @@ -125,16 +125,8 @@ export default class File extends React.Component { } }; - // TODO: Make language agnostic! Probably move to lang/web folder at some point debugExecutedCode = async () => { - // language=Python - const TEMPLATE_CODE = "# these lines stub out the debugging functions you have available\n" - + "def draw(something): pass\n" - + "def autodraw(): pass\n" - + "def disableAutodraw(): pass\n" - + "def visualize(): pass\n" - + "def editor(): pass\n\n" - + "# your code is below\n"; + const TEMPLATE_CODE = debugPrefix(this.identifyLanguage()); const code = TEMPLATE_CODE + this.state.executedCode.join("\n"); const debugData = await generateDebugTrace(this.identifyLanguage())(code); if (debugData.success) { diff --git a/src/renderer/utils/dispatch.js b/src/renderer/utils/dispatch.js index c376532..bfdf18e 100644 --- a/src/renderer/utils/dispatch.js +++ b/src/renderer/utils/dispatch.js @@ -7,6 +7,8 @@ import { runPyCode, runPyFile } from "../../languages/python/utils/run.js"; import { runScmCode, runScmFile } from "../../languages/scheme/utils/run.js"; import SchemeDebugger from "../../languages/scheme/components/SchemeDebugger.js"; import PythonTutorDebug from "../../languages/python/components/PythonTutorDebug.js"; +import pyDebugPrefix from "../../languages/python/utils/debugPrefix.js"; +import scmDebugPrefix from "../../languages/scheme/utils/debugPrefix.js"; export function format(language) { const options = { @@ -47,3 +49,11 @@ export function Debugger(language) { }; return options[language]; } + +export function debugPrefix(language) { + const options = { + [PYTHON]: pyDebugPrefix, + [SCHEME]: scmDebugPrefix, + }; + return options[language]; +}