Skip to content
Open
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
18 changes: 9 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ You are writing a three-pass compiler for a simple programming language into
a small assembly language.

## The programming language has this syntax:
```
```lex
function ::= '[' arg-list ']' expression

arg-list ::= /* nothing */
Expand All @@ -27,11 +27,11 @@ a small assembly language.
Variables are strings of alphabetic characters. Numbers are strings of
decimal digits representing integers. So, for example, a function which
computes a^2 + b^2 might look like:
```
```js
[ a b ] a*a + b*b
```
A function which computes the average of two numbers might look like:
```
```js
[ first second ] (first + second) / 2
```
You need write a three-pass compiler. All test cases will be valid programs,
Expand All @@ -41,7 +41,7 @@ The first pass will be the method pass1 which takes a string representing a
function in the original programming language and will return a JSON object
that represents that Abstract Syntax Tree. The Abstract Syntax Tree must
use the following representations:
```
```js
{ 'op': '+', 'a': a, 'b': b } // add subtree a to subtree b
{ 'op': '-', 'a': a, 'b': b } // subtract subtree b from subtree a
{ 'op': '*', 'a': a, 'b': b } // multiply subtree a by subtree b
Expand All @@ -51,7 +51,7 @@ use the following representations:
```
Note: arguments are indexed from zero. So, for example, the function
[ xx yy ] ( xx + yy ) / 2 would look like:
```
```js
{ 'op': '/', 'a': { 'op': '+', 'a': { 'op': 'arg', 'n': 0 },
'b': { 'op': 'arg', 'n': 1 } },
'b': { 'op': 'imm', 'n': 2 } }
Expand All @@ -61,13 +61,13 @@ output from pass1 and return a new Abstract Syntax Tree (with the same format)
with all constant expressions reduced as much as possible.

So, if for example, the function is [ x ] x + 2*5, the result of pass1 would be:
```
```js
{ 'op': '+', 'a': { 'op': 'arg', 'n': 0 },
'b': { 'op': '*', 'a': { 'op': 'imm', 'n': 2 },
'b': { 'op': 'imm', 'n': 5 } } }
```
This would be passed into pass2 which would return:
```
```js
{ 'op': '+', 'a': { 'op': 'arg', 'n': 0 },
'b': { 'op': 'imm', 'n': 10 } }
```
Expand All @@ -78,7 +78,7 @@ a stack, and an array of input arguments.

The result of a function is expected to be in R0. The processor supports the
following instructions:
```
```assembly
"LD.I n" // load the constant value n into R0
"LD.M n" // load the n-th input argument into R0
"SWAP" // swap R0 and R1
Expand All @@ -91,7 +91,7 @@ following instructions:
```
So, one possible return value from pass3 given the Abstract Syntax Tree shown
above from pass2 is:
```
```js
[ "LD.I 10", "SWAP", "LD.M 0", "ADD" ]
```
# Solutions
Expand Down