|
1 |
| -# TODO: Document repo |
| 1 | +# TypeScript Compiler |
| 2 | + |
| 3 | +This `README.md` contains high-level detail |
| 4 | +about the coursework and language |
| 5 | +from the submitted report. |
| 6 | + |
| 7 | +See individual `.md`s for detail on the: |
| 8 | +* [Lexical Analyser ](./docs/lexical-analyser.md) |
| 9 | +* [Parser ](./docs/parser.md) |
| 10 | +* [Code Generator ](./docs/code-generator.md) |
| 11 | +* [Code Optimiser ](./docs/optimiser.md) |
| 12 | + |
| 13 | +## Introduction |
| 14 | + |
| 15 | +The scope of this coursework is to implement a compiler for a source language that |
| 16 | +has no run-time errors but may issue run-time warnings. |
| 17 | + |
| 18 | +### Source Language |
| 19 | +The source language for the compiler is chosen to be a subset of TypeScript. TypeScript |
| 20 | +is an open-source programming language, which is a strict syntactical superset of JavaScript |
| 21 | +and adds static typing to the language [1], as opposed to JavaScript’s no static type nature [2]. |
| 22 | +Due to the open-source nature of it, access to its language specification is public. Along with |
| 23 | +this, since the language is widely used, running [3] and comparing original code during tests |
| 24 | +[4] will be made easier. The subset chosen is described in the Description of Source Language |
| 25 | +section. |
| 26 | + |
| 27 | +### Target Language |
| 28 | +The target language chosen is C. This was mainly chosen due to the low-level nature |
| 29 | +of the language, allowing for complex algorithms to be executed at close to machine clock. |
| 30 | +Also, development is made easier due to existing familiarity with the language from previously |
| 31 | +assessed coursework, most notably CS1PR16 C/C++ labs and CS1FC16 sorting coursework in |
| 32 | +C. |
| 33 | + |
| 34 | +### Implementation Language |
| 35 | +The language chosen for the compiler is also TypeScript. This was done to build more |
| 36 | +familiarity with the programming language, with a view of using it for future projects (most |
| 37 | +notably, the upcoming final year project). Also, with the rather simple nature of the chosen |
| 38 | +subset of the language, and the performance of the development machines, some efficiency |
| 39 | +is traded for greater flexibility of the compiler. |
| 40 | + |
| 41 | +## Description of Source Language |
| 42 | + |
| 43 | +### Description |
| 44 | +For the compiler to be implemented and tested, the source language is defined. Since |
| 45 | +TypeScript has a very large grammar, a subset of it is chosen and defined. |
| 46 | + |
| 47 | +The main feature required in the subset is output. TypeScript allows standard output |
| 48 | +to the command-line using its `console.log(content)` function. As a result, both console |
| 49 | +and log are added as reserved keywords for the compiler to detect output to stdout. In this |
| 50 | +case, content can be either a constant or a variable, as seen in Figure 1 and Figure 2 |
| 51 | +respectively. |
| 52 | + |
| 53 | +```ts |
| 54 | + console.log("Hello, world!") |
| 55 | +``` |
| 56 | +*Figure 1. Standard Output with Constant Example* |
| 57 | +```ts |
| 58 | + let num: number = 3 |
| 59 | + console.log(num) |
| 60 | +``` |
| 61 | +*Figure 2. Standard Output with Identifier Example* |
| 62 | + |
| 63 | +Along with this, the let keyword is added to the reserved list, for use in variable |
| 64 | +declarations. TypeScript allows multiple types of variables to be declared, and the main ones |
| 65 | +chosen in the subset are string and number types. Although in Typescript the number types |
| 66 | +are stored as floating-point values [5], they will be restricted to integers for this application. |
| 67 | +Along with this, a constant may be added to the declaration to assign a value to the newly |
| 68 | +declared variable. |
| 69 | + |
| 70 | +Examples of valid declaration statements are shown in Figure 3. |
| 71 | +```ts |
| 72 | + let num: number |
| 73 | + let str: string |
| 74 | +``` |
| 75 | +*Figure 3. Declare Statements* |
| 76 | +
|
| 77 | +Examples of valid declare and assign statements are shown in Figure 4. |
| 78 | +```ts |
| 79 | + let num: number = 3 |
| 80 | + let str: string = "Hello, world!" |
| 81 | +``` |
| 82 | +*Figure 4. Declare and Assign Statements* |
| 83 | + |
| 84 | +Assignment may be performed outside of the declaration, as seen in Figure 5. |
| 85 | +```ts |
| 86 | + let str: string |
| 87 | + str = "Hello, world!" |
| 88 | +``` |
| 89 | +*Figure 5. Assign Statements with Constants* |
| 90 | + |
| 91 | +Previous assignment statements use constants as the value. Valid assignment |
| 92 | +statements also allow identifiers to be used, as seen in Figure 6. |
| 93 | +```ts |
| 94 | + let str: string |
| 95 | + let hello: string = "Hello, world!" |
| 96 | + str = hello |
| 97 | +``` |
| 98 | +*Figure 6. Assign Statements with Identifiers* |
| 99 | + |
| 100 | +Arithmetic may be performed in assignment statements as well on number typed |
| 101 | +variables, as seen in Figure 7. |
| 102 | +```ts |
| 103 | + let a: number = 1 |
| 104 | + let b: number = 2 |
| 105 | + let c: number |
| 106 | + c = a + b |
| 107 | +``` |
| 108 | +*Figure 7. Assign Statements with Arithmetic* |
| 109 | + |
| 110 | +Along with addition, other available arithmetic operations in the subset are: |
| 111 | +subtraction, multiplication, incrementation, decrementation. |
0 commit comments