A compiler with very basic capabilities written in Swift

Overview

BasicCompiler

A compiler with very basic capabilities written in Swift. This project is not intended for real-world use; it's implemented just for fun.

The desired language syntax is described in the following EBNF:

prg               = prgHeader varDefs "begin" statementSeq "end" "."
prgHeader         = "program" identifier ";"
varDefs           = [ "var" varSeq { varSeq } ]
varSeq            = identifier { "," identifier } : type ";"
type              = "integer" | "string"
statementSeq      = { ( simpleAssignment | complexAssignment) ";" }
simpleAssignment  = identifier ":=" operand ";"
complexAssignment = identifier ":=" operand mathOP operand ";"
operand           = identifier | number
mathOp            = "+" | "-"

Prerequisites

  • Install the lates version of Swift. Please follow the instructions.
  • Install the lates version of Make. Please follow the instructions
  • Install the lates version of Ubuntu. Please follow the instructions

Instructions

  1. Download or clone the project.
  2. Go to the root folder and run cd BasicCompiler.
  3. Run make all to build the package.
  4. Run make test to test the package.

Build-time error handling

BasicCompiler is capable of handling all the errors mentioned below by providing user-readable messages:

  • Parser errors

    • When the lexeme exceeds the predefined max lexeme length, with additional info (line, offset)
    • When a string literal is not closed, with additional info (line, offset)
    • When an unsupported symbol appears, with additional info (line, offset, unupported_symbol)
    • When an invalid lexeme is detected, with additional info (line, offset, invalid_lexeme)
  • Compiler errors

    • When expected to find the end of the program but found another token instead, with additional info (unexpected_token)
    • When expected to find a token but found the end of the program
    • When an unexpected (wrong) token has been found, with additional info (unexpected_token)
    • When a variable is declared more than once, with additional info (variable_name)
    • When trying to use a variable without declaring it, with additional info (variable_name)
    • When trying to assign a variable with a wrong type, with additional info (expected_type, given_type)
    • When trying to use an unsupported type, with additional info (unsupported_type)
    • When trying to use an uninitialized variable, with additional info (variable_name)
  • File errors

    • When the source file is not provided
    • When the provided source file doesn't exist
    • When the provided source file can't be read
    • When the output file can't be written
  • Other errors

    • All other errors, with auto-generated messages

Optimizations

If both operands of the complexAssignment are literals, the value will be calculated at build-time, and the result will be assigned as simpleAssignment.

Disclaimer

Alternatively, generate the assembly file swift run BasicCompiler $(INPUT_FILE) then compile and run it using an online IDE to check the results.

You might also like...
NetFun-Backend - BFF layer (written in Swift)

NetFun-Backend BFF layer (written in Swift). One can setup BFF using Core Classess (which is already added as a dependency of this package. See Packag

KnockToReact is an iOS library written in Swift and Objective-C that brings an exclusive feature to interact with users just by receiving and recognizing "knocks" in the device.

KnockToReact is an iOS library written in Swift and Objective-C that brings an exclusive feature to interact with users just by receiving and recognizing "knocks" in the device.

A usermanager written in swift 3.0 saves you from hassle of saving your active user session.

SwiftUserManager A usermanager written in swift 3.0 saves you from hassle of saving your active user session. Call api and give the json to MOProfile

IBSKit - an Xcode Fat Framework written in Swift 5
IBSKit - an Xcode Fat Framework written in Swift 5

IBSKit framework is designed to solve everyday tasks that any iOS developer faces when developing a new project.

TTipBoxView is a simple and flexible UI component fully written in Swift
TTipBoxView is a simple and flexible UI component fully written in Swift

TTipBoxView is a simple and flexible UI component fully written in Swift. It is developed to help you create a hint/prompt view quickly, saving your time and avoiding having to write many lines of codes.

AuroraEditor is a IDE built by the community, for the community, and written in Swift for the best native performance and feel for macOS.
AuroraEditor is a IDE built by the community, for the community, and written in Swift for the best native performance and feel for macOS.

AuroraEditor AuroraEditor is a IDE built by the community, for the community, and written in Swift for the best native performance and feel for macOS.

Owl is a portable Wayland compositor written in Objective-C, using Cocoa as its backend.

Owl is a portable Wayland compositor written in Objective-C, using Cocoa as its backend. Owl primarily targets Mac OS X, but also supports a varie

iOS's Stocks App clone written in React Native for demo purpose (available both iOS and Android).
iOS's Stocks App clone written in React Native for demo purpose (available both iOS and Android).

FinanceReactNative iOS's Stocks App clone written in React Native for demo purpose (available both iOS and Android). Data is pulled from Yahoo Finance

A simple To Do application written in React Native

Example To Do List application in react-native Requirements, install as needed: React Native: $ npm i -g react-native-cli watchman: $ brew install wa

Owner
null
Demo app to demonstrate native blur capabilities

Blurrable Demo app to demonstrate native blur capabilities. You can customize th

Mariya Pankova 0 Dec 18, 2021
Joplin - an open source note taking and to-do application with synchronization capabilities for Windows, macOS, Linux, Android and iOS. Forum: https://discourse.joplinapp.org/

JoplinĀ® is a free, open source note taking and to-do application, which can handle a large number of notes organised into notebooks. The notes are sea

Laurent 33.7k Dec 30, 2022
Mybook swiftui - A Facebook UI Clone with some capabilities like like/unlike, comment, scroll, show stories etc

mybook_swiftui ?? Trying to create a Facebook UI Clone with some capabilities li

mogolAyberk 1 Apr 16, 2022
A very simplistic state machine system for Swift while mainly used with Raylib on Swift

A very simplistic state machine system for Swift while mainly used with Raylib on Swift

Conifer Coniferoslav 2 Dec 12, 2022
This is a basic mobile app that allows the user to tap a button to change the color of a label.

MYAPP App Description `This is a basic mobile app that allows the user to tap a button to change the color of a label. App Walk-though Required User S

null 0 Nov 27, 2021
SyntaxTree - This code attempts to make basic syntax trees in the Xcode console

SyntaxTree This code attempts to make basic syntax trees in the Xcode console. I

Yash 0 Feb 12, 2022
Delightful code generation for OpenAPI specs for Swift written in Swift

Create API Delightful code generation for OpenAPI specs for Swift written in Swi

Alexander Grebenyuk 286 Dec 23, 2022
A thread safe throttle written in Swift

SwiftThrottle - A thread safe throttle written in Swift licensed under MIT. Introduction This throttle is intended to prevent the program from crashing

Lakr Aream 6 Jan 3, 2023
This project is a minimalistic Pomodoro timer for OS X written in Swift

Minimalistic Pomodoro for OSX This project is a minimalistic Pomodoro timer for OS X written in Swift started by @bengsfort, and substantially enhance

Kushal Shingote 2 Nov 17, 2021
An interpreter for Lox written in Swift.

slox An interpreter for Lox written in Swift. Provided for with an MIT License.

Manuel Werder 1 Nov 8, 2021