From 524f03bf0837299b544ff5176e60ce375adfd69b Mon Sep 17 00:00:00 2001 From: Pawel Kowalski Date: Sat, 30 Sep 2017 19:11:04 +0200 Subject: [PATCH 01/20] refactor map for use in a generator --- app.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 54a1fb5..b28a57c 100644 --- a/app.js +++ b/app.js @@ -104,11 +104,22 @@ throw Error("At least one expression required in begin block."); } const [_, ...exps] = x; - return exps.map(exp => yield * evaluate(exp, env)).slice(-1)[0]; + return exps.map(exp => evaluate(exp, env)).slice(-1)[0]; } else { // Function call (no special form) const func_name = typeof x[0] === "string" ? x[0] : ""; - const [func, ...args] = x.map(exp => yield * evaluate(exp, env)); + + // because we're yield'ing map is not allowed... + // + // const [func, ...args] = x.map(exp => yield * evaluate(exp, env)); + // + var evald = [] + for (var i=0; i Date: Sat, 30 Sep 2017 19:22:29 +0200 Subject: [PATCH 02/20] other map refactored --- app.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index b28a57c..d676aaa 100644 --- a/app.js +++ b/app.js @@ -104,7 +104,16 @@ throw Error("At least one expression required in begin block."); } const [_, ...exps] = x; - return exps.map(exp => evaluate(exp, env)).slice(-1)[0]; + + // because we're yield'ing map is not allowed... + // + // return exps.map(exp => evaluate(exp, env)).slice(-1)[0]; + // + var last_evald = undefined; + for (var i=0; i"; From d36c5be22135ee8f5a3f14b8e3a777cb5b30e312 Mon Sep 17 00:00:00 2001 From: Pawel Kowalski Date: Sat, 30 Sep 2017 22:10:43 +0200 Subject: [PATCH 03/20] very first working impl of a debugger --- app.js | 64 ++++++++++++++++++++++++++++++++++++++++++------------ index.html | 3 ++- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/app.js b/app.js index d676aaa..19f0452 100644 --- a/app.js +++ b/app.js @@ -98,6 +98,8 @@ } return env[x]; } else if (x instanceof Number) { + x.doing = true; + yield x; return x.valueOf(); } else if (x[0].valueOf() === "begin") { if (x.length < 2) { @@ -201,7 +203,8 @@ global_env: global_env, result: undefined, error: false, - debug: true + debug: true, + eval_gen: undefined, }, computed: { parenBalance: function() { @@ -209,6 +212,33 @@ this.input.split(")").length; } }, + methods: { + step: function() { + this.result = undefined; + var {value: result, done} = this.eval_gen.next(); + + // tell vue to update token list + let token_idx = this.tokens.findIndex(el => el.id == result.id); + Vue.set(this.tokens, token_idx, result); + if (!done) { + return result; + } + + // we're done stepping: reset eval_gen to null + this.eval_gen = null; + + // return final result + if (result instanceof Array) { + const pprint = tree => tree instanceof Array ? + "(" + tree.map(pprint).join(" ") + ")" : tree; + this.result = pprint(result.slice(0, -1)); + } else if (typeof result === "function") { + this.result = "native function: " + result.name; + } else { + this.result = result; + } + } + }, watch: { input: function(val) { this.ast = []; @@ -219,20 +249,26 @@ this.ast = parse(this.tokens.slice()); this.env = Object.create(this.global_env); - let evaluate_gen = evaluate(this.ast, this.env); - while (!done) { - var {value: result, done} = evaluate_gen.next(); - } + // let evaluate_gen = evaluate(this.ast, this.env); + // while (!done) { + // var {value: result, done} = evaluate_gen.next(); + // console.log(result); + // } + this.eval_gen = evaluate(this.ast, this.env); + // while (!done) { + // var {value: result, done} = evaluate_gen.next(); + // console.log(result); + // } - if (result instanceof Array) { - const pprint = tree => tree instanceof Array ? - "(" + tree.map(pprint).join(" ") + ")" : tree; - this.result = pprint(result.slice(0, -1)); - } else if (typeof result === "function") { - this.result = "native function: " + result.name; - } else { - this.result = result; - } + // if (result instanceof Array) { + // const pprint = tree => tree instanceof Array ? + // "(" + tree.map(pprint).join(" ") + ")" : tree; + // this.result = pprint(result.slice(0, -1)); + // } else if (typeof result === "function") { + // this.result = "native function: " + result.name; + // } else { + // this.result = result; + // } } catch (error) { this.error = error; } diff --git a/index.html b/index.html index 94f3043..73cfb7d 100644 --- a/index.html +++ b/index.html @@ -17,8 +17,9 @@

Source

)

{{error.name}}: {{error.message}}

From 22be3a5fbf88678b908e802bb870b558271c382a Mon Sep 17 00:00:00 2001 From: Pawel Kowalski Date: Sat, 30 Sep 2017 22:23:22 +0200 Subject: [PATCH 04/20] mark the function aswell --- app.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 19f0452..4c89103 100644 --- a/app.js +++ b/app.js @@ -96,6 +96,8 @@ if (typeof res === "undefined") { throw Error("Variable '" + x + "' not found"); } + x.doing = true; + yield x; return env[x]; } else if (x instanceof Number) { x.doing = true; @@ -277,7 +279,7 @@ }); // Example input: - vm.input = `(+ 2 2)`; + vm.input = `(+ (+ 2 2) 5)`; vm.input_ = `(define abs (lambda (a) (if (> a 0) a (- 0 a)))) (define avg (lambda (a b) (/ (+ a b) 2) )) From ef9f5451480d105f6d40869f3ad1f9799a7291da Mon Sep 17 00:00:00 2001 From: Pawel Kowalski Date: Sat, 30 Sep 2017 22:24:21 +0200 Subject: [PATCH 05/20] cleanup --- app.js | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/app.js b/app.js index 4c89103..db85c39 100644 --- a/app.js +++ b/app.js @@ -251,26 +251,7 @@ this.ast = parse(this.tokens.slice()); this.env = Object.create(this.global_env); - // let evaluate_gen = evaluate(this.ast, this.env); - // while (!done) { - // var {value: result, done} = evaluate_gen.next(); - // console.log(result); - // } this.eval_gen = evaluate(this.ast, this.env); - // while (!done) { - // var {value: result, done} = evaluate_gen.next(); - // console.log(result); - // } - - // if (result instanceof Array) { - // const pprint = tree => tree instanceof Array ? - // "(" + tree.map(pprint).join(" ") + ")" : tree; - // this.result = pprint(result.slice(0, -1)); - // } else if (typeof result === "function") { - // this.result = "native function: " + result.name; - // } else { - // this.result = result; - // } } catch (error) { this.error = error; } From 9f48482c3c66fe2861f52445b7b2187ee1cb9a8e Mon Sep 17 00:00:00 2001 From: Pawel Kowalski Date: Sat, 30 Sep 2017 23:09:31 +0200 Subject: [PATCH 06/20] start and finish function call --- app.js | 32 ++++++++++++++++++++++++-------- index.html | 2 +- styles.css | 8 ++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index db85c39..24552a0 100644 --- a/app.js +++ b/app.js @@ -96,7 +96,7 @@ if (typeof res === "undefined") { throw Error("Variable '" + x + "' not found"); } - x.doing = true; + x.started = true; yield x; return env[x]; } else if (x instanceof Number) { @@ -127,15 +127,23 @@ // const [func, ...args] = x.map(exp => yield * evaluate(exp, env)); // var evald = [] + var first_tok, last_tok; for (var i=0; i el.id == result.id); - Vue.set(this.tokens, token_idx, result); - if (!done) { - return result; + if (true) { + var {value: result, done} = this.eval_gen.next(); + + // tell vue to update token list + let token_idx = this.tokens.findIndex(el => el.id == result.id); + Vue.set(this.tokens, token_idx, result); + if (!done) { + return result; + } + } else { + while (!done) { + var {value: result, done} = this.eval_gen.next(); + console.log(result); + } } // we're done stepping: reset eval_gen to null diff --git a/index.html b/index.html index 73cfb7d..81cd053 100644 --- a/index.html +++ b/index.html @@ -27,7 +27,7 @@

Result

Tokens

diff --git a/styles.css b/styles.css index 20ab5d8..ce79225 100644 --- a/styles.css +++ b/styles.css @@ -76,6 +76,14 @@ span.tokendone { line-height: 2.2; } +span.tokenstarted { + border: 2px darkblue solid; + background-color: #B2E6AD; + padding: 2px 5px; + border-radius: 4px; + line-height: 2.2; +} + ul.env { display: inline-block; list-style: none; From 4eac6a82f98c1457d4c8de419666dc150af16bb6 Mon Sep 17 00:00:00 2001 From: Pawel Kowalski Date: Sat, 30 Sep 2017 23:10:50 +0200 Subject: [PATCH 07/20] hide step label --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index 81cd053..fc729fc 100644 --- a/index.html +++ b/index.html @@ -18,7 +18,7 @@

Source

{{error.name}}: {{error.message}}