Skip to content

Commit

Permalink
Add support for parse and eval (#6)
Browse files Browse the repository at this point in the history
Currently, we can only run deterministic programs via
the `driver_loop`. Programs must be entered into a prompt,
which is a little cumbersome for large programs.

Let's add support for evaluating non deterministic programs
via `parse_and_eval`.

To do this, we can define a global `try_again` function.
Within the success continuation, we can re-assign the value
of `try_again` to a function that will supply the next value.
  • Loading branch information
anubh-v authored Feb 6, 2020
1 parent 280b0bf commit d21ea32
Showing 1 changed file with 25 additions and 41 deletions.
66 changes: 25 additions & 41 deletions evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,27 +841,29 @@ function setup_environment() {

const the_global_environment = setup_environment();

function parse_and_eval(str) {
return eval_toplevel(parse(str));
}

/*
examples:
parse_and_eval("1;");
parse_and_eval("1 + 1;");
parse_and_eval("1 + 3 * 4;");
parse_and_eval("(1 + 3) * 4;");
parse_and_eval("1.4 / 2.3 + 70.4 * 18.3;");

parse_and_eval("true;");
parse_and_eval("! (1 === 1);");
parse_and_eval("(! (1 === 1)) ? 1 : 2;");
/* parse and eval */
function no_current_problem() {
display("// There is no current problem");
}

parse_and_eval("'hello' + ' ' + 'world';");
let try_again = no_current_problem;

parse_and_eval("function factorial(n) { return n === 1 ? 1 : n * factorial(n - 1);} factorial(4);");
function parse_and_eval(input) {
const program_block = make_block(parse(input));
ambeval(program_block,
the_global_environment,
(val, next_alternative) => {
display(output_prompt + user_print(val));
try_again = next_alternative;
},
() => {
display("There are no more values of:");
display(input);
try_again = no_current_problem;
});
}

*/

/* THE READ-EVAL-PRINT LOOP */

Expand All @@ -877,34 +879,16 @@ function user_print(object) {
: stringify(object);
}

function read_eval_print_loop(history) {
const prog = prompt("History:" + history +
"\n\n" + "Enter next: ");
if (prog === null) {
display("session has ended");
} else {
const res = parse_and_eval(prog);
read_eval_print_loop(history + "\n" +
stringify(prog) + " ===> " +
stringify(user_print(res)));
}
}

/*
read_eval_print_loop("");
*/

const input_prompt = "// Amb-Eval input:";

const output_prompt = "// Amb-Eval value:";
const input_prompt = "input:";
const output_prompt = "result:";
function driver_loop() {
function internal_loop(try_again) {
const input = prompt(input_prompt);
if (input === "try-again") {
try_again();
} else {
const program_block = make_block(parse(input));
display("// Starting a new problem ");
display("Starting a new problem ");
ambeval(program_block,
the_global_environment,
// ambeval success
Expand All @@ -914,17 +898,17 @@ function driver_loop() {
},
// ambeval failure
() => {
display("// There are no more values of " +
display("There are no more values of " +
user_print(input));
return driver_loop();
});
}
}
return internal_loop(
() => {
display("// There is no current problem");
display("There is no current problem");
return driver_loop();
});
}

driver_loop();
//driver_loop();

0 comments on commit d21ea32

Please sign in to comment.