From c5f9472c146c45e49f7a16e52d5e3cc4bd037e6f Mon Sep 17 00:00:00 2001 From: Pankaj Doharey Date: Mon, 30 Aug 2021 15:14:15 +0530 Subject: [PATCH] Markdown annotated for syntax highlighting. --- readme.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/readme.md b/readme.md index 39807f6..e3da348 100755 --- a/readme.md +++ b/readme.md @@ -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 */ @@ -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, @@ -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 @@ -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 } } @@ -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 } } ``` @@ -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 @@ -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