Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit a87b75b

Browse files
authored
Merge pull request #4 from andrejusk/hotfix/docs
Add documentation to repo
2 parents 48ec462 + e8fc612 commit a87b75b

18 files changed

+371
-1
lines changed

README.md

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,111 @@
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.

docs/code-generator.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Code Generator
2+
3+
The code generator takes a syntax tree and converts it into the target language. This
4+
produces the end result of the compiler, which can be executed.
5+
6+
The implementation of the code generator involves recursively traversing the syntax
7+
tree and generating code in pre-defined base cases. These range from the C file import
8+
headers to variable declarations and arithmetic operations.
9+
10+
Raw code generated from previous examples can be seen in Figure 17 and Figure 18.
11+
12+
```C
13+
#include <stdio.h>
14+
int main() {
15+
printf("%s", "Hello, world!");
16+
return 0;
17+
}
18+
```
19+
*Figure 17. Code Generated from Figure 12 (A1.1. hello-world.ts)*
20+
21+
```C
22+
#include <stdio.h>
23+
int main() {
24+
int a = 1;
25+
int b = 2;
26+
int c;
27+
c = (a + b);
28+
printf("%d", c);
29+
return 0;
30+
}
31+
```
32+
*Figure 18. Code Generated from Figure 13 (A1.2. number-addition.ts)*

docs/img/lex-10.png

29.9 KB
Loading

docs/img/lex-9.png

11.6 KB
Loading

docs/img/opt-19.png

9.67 KB
Loading

docs/img/opt-21.png

23.6 KB
Loading

docs/img/parse-12.png

10.3 KB
Loading

docs/img/parse-13.png

35.6 KB
Loading

docs/img/parse-14-1.png

2.39 KB
Loading

docs/img/parse-14-2.png

2.5 KB
Loading

0 commit comments

Comments
 (0)