You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 20, 2022. It is now read-only.
The scope of this coursework is to implement a compiler for a source language thathas no run-time errors but may issue run-time warnings.
Source Language
The source language for the compiler is chosen to be a subset of TypeScript. TypeScriptis an open-source programming language, which is a strict syntactical superset of JavaScriptand adds static typing to the language [1], as opposed to JavaScript’s no static type nature [2].Due to the open-source nature of it, access to its language specification is public. Along withthis, since the language is widely used, running [3] and comparing original code during tests[4] will be made easier. The subset chosen is described in the Description of Source Languagesection.
Target Language
The target language chosen is C. This was mainly chosen due to the low-level natureof the language, allowing for complex algorithms to be executed at close to machine clock.Also, development is made easier due to existing familiarity with the language from previouslyassessed coursework, most notably CS1PR16 C/C++ labs and CS1FC16 sorting coursework inC.
Implementation Language
The language chosen for the compiler is also TypeScript. This was done to build morefamiliarity with the programming language, with a view of using it for future projects (mostnotably, the upcoming final year project). Also, with the rather simple nature of the chosensubset of the language, and the performance of the development machines, some efficiencyis traded for greater flexibility of the compiler.
Description of Source Language
Description
For the compiler to be implemented and tested, the source language is defined. SinceTypeScript has a very large grammar, a subset of it is chosen and defined.
The main feature required in the subset is output. TypeScript allows standard outputto the command-line using itsconsole.log(content) function. As a result, both consoleand log are added as reserved keywords for the compiler to detect output to stdout. In thiscase, content can be either a constant or a variable, as seen in Figure 1 and Figure 2respectively.
console.log("Hello, world!")
Figure 1. Standard Output with Constant Example
letnum:number=3console.log(num)
Figure 2. Standard Output with Identifier Example
Along with this, the let keyword is added to the reserved list, for use in variabledeclarations. TypeScript allows multiple types of variables to be declared, and the main oneschosen in the subset are string and number types. Although in Typescript the number typesare stored as floating-point values [5], they will be restricted to integers for this application.Along with this, a constant may be added to the declaration to assign a value to the newlydeclared variable.
Examples of valid declaration statements are shown in Figure 3.
letnum:numberletstr:string
Figure 3. Declare Statements
Examples of valid declare and assign statements are shown in Figure 4.
letnum:number=3letstr:string="Hello, world!"
Figure 4. Declare and Assign Statements
Assignment may be performed outside of the declaration, as seen in Figure 5.
letstr:stringstr="Hello, world!"
Figure 5. Assign Statements with Constants
Previous assignment statements use constants as the value. Valid assignmentstatements also allow identifiers to be used, as seen in Figure 6.