Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

TypeScript to C Compiler, implementing lexical analysis, parsing and optimisation, written in TypeScript

NotificationsYou must be signed in to change notification settings

andrejusk/typescript-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ThisREADME.md contains high-level detailabout the coursework and languagefrom the submitted report.

See individual.mds for detail on the:

Usage

Install dependencies:

  • npm i
  • A form of g++ in ENV (e.g. MinGW)

For hello world:

npm start .\test\scripts\hello-world.ts

Or, in general:

npm start <script>

Introduction

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.

letstr:stringlethello:string="Hello, world!"str=hello

Figure 6. Assign Statements with Identifiers

Arithmetic may be performed in assignment statements as well on number typedvariables, as seen in Figure 7.

leta:number=1letb:number=2letc:numberc=a+b

Figure 7. Assign Statements with Arithmetic

Along with addition, other available arithmetic operations in the subset are:subtraction, multiplication, incrementation, decrementation.

About

TypeScript to C Compiler, implementing lexical analysis, parsing and optimisation, written in TypeScript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp