Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add register machine evaluator (5.4 SICP JS) #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions doc/register-machine-evaluator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# CS4215 T1 GC for Register Machine

## Overview

This implements a register machine evaluator for Source §1, as described in [<https://sicp.comp.nus.edu.sg/chapters/109#top>](Chapter 5.4) of SICP JS. It adopts the evaluator from Chapter 4.1 into machine instructions.

Register machine code format is found [<https://github.com/source-academy/source-programs/wiki/Register-Machine-Code-Format>](in this wiki).

## Usage

Example:

```js
set_program_to_run("1 + 3 * 4;"); // This parses the program and loads it into the register machine
start(m); // This runs the register machine
get_register_contents(m, "val"); // The output is in the register named "val"
```

## Testing

Test cases are provided.

Install dependencies by running `yarn`.

Run tests by executing `yarn test`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set_register_contents(cl, "curr_node", pair(pair(1, 2), pair(3, pair(4, 5))));
start(cl);
get_register_contents(cl, "num_leaves");

// 5
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set_register_contents(cl, "curr_node", 1);
start(cl);
get_register_contents(cl, "num_leaves");

// 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set_register_contents(cl, "curr_node", null);
start(cl);
get_register_contents(cl, "num_leaves");

// 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("1 + 1;");

start(m);

get_register_contents(m, "val");

// 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("1.4 / 2.3 + 70.4 * 18.3;");

start(m);

get_register_contents(m, "val");

// 1288.9286956521742
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("(1 + 3) * 4;");

start(m);

get_register_contents(m, "val");

// 16
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("1 + 3 * 4;");

start(m);

get_register_contents(m, "val");

// 13
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("! (1 === 1);");

start(m);

get_register_contents(m, "val");

// false
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("const foo = 1; foo;");

start(m);

get_register_contents(m, "val");

// 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("true;");

start(m);

get_register_contents(m, "val");

// true
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("1;");

start(m);

get_register_contents(m, "val");

// 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("function add(a, b) {return a + b;} add(1, 3);");

start(m);

get_register_contents(m, "val");

// 4
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set_program_to_run(
"function factorial(n) { return n === 1 ? 1 : n * factorial(n - 1);} factorial(4);"
);

start(m);

get_register_contents(m, "val");

// 24
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("function foo() {return 1;} foo();");

start(m);

get_register_contents(m, "val");

// 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("function foo() {return 1;}");

start(m);

get_register_contents(m, "val");

// 'ok'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("function foo() {return 1;} 2 + 2; foo(); 2 - 3;");

start(m);

get_register_contents(m, "val");

// -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("-12 - 8; 2;");

start(m);

get_register_contents(m, "val");

// 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("'hello' + ' ' + 'world';");

start(m);

get_register_contents(m, "val");

// 'hello world'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_program_to_run("(! (1 === 1)) ? 1 : 2;");

start(m);

get_register_contents(m, "val");

// 2
Loading