• Stars
    star
    281
  • Rank 147,023 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple Swift interpreter for the Pascal language inspired by the Let’s Build A Simple Interpreter article series.

Pascal interpreter written in Swift

License: MIT Swift Version Twitter

Simple Swift interpreter for the Pascal language inspired by the Let’s Build A Simple Interpreter article series.

Playground

What is implemented

  • standard types (integer, real, boolean, string)
  • arithmetic expressions
  • function calls
  • procedure calls
  • recursion
  • loops (for, repet until, while)
  • logical conditions (if)
  • standard Pascal functions (writeln, write, readln, read, random)
  • one-dimensional arrays

There are a few sample Pascal programs in the Examples directory, like a simple number guessing game and a factorial computation.

Scructure

Lexer

The Lexer reads the Pascal program as String (a sequence of characters) and converts it into a sequence of Tokens. You can see the result by trying it out in the Playground or on the unit tests.

Lexer

Parser

The Parser reads the sequence of tokens produced by the Lexer and builds an Abstract Syntax Tree representation (AST for short) of the Pascal program according to the grammar.

You can see what the AST looks like in the unit tests or in the Playground where you can also use the printTree() method on any AST to see its visual representation printed into the console.

Parser

Semantic analyzer

The Semantic analyzer does static semantic checks on the Pascal program AST. It currently checks if all the used variables are declared beforehand and if there are any duplicate declarations. The result of semantic analysis is a Symbol table that holds all the symbols used by a Pascal program, currently built in types (Integer, Real, Boolean, String) and declared variable names.

Implemented checks

  • Check if a variable was declared with a known type (Integer, Real)
  • Check if a variable was declared before usage
  • Check if variable is not declared more than once
  • Check if a procedure was declared
  • Check if a procedure is called with the correct number of parameters

Interpreter

The Interpreter reads the AST representing the Pascal program from Parser and interprets it by walking the AST recursively. It can handle basic Pascal programs.

At the end of the Pascal program interpretation you can check the resulting memory state (see unit tests) or print it in the Playground using printState().

Try it out

CLI

When you build the SPI project in the workspace you will get command line utility that can run any Pascal program given as argument, as shown in the GIF at the top of this README.

Playground

There is a Swift playground in the project where you can try out the lexer, parser and the interpreter. The Playground interprets then following Pascal program defining and calling a factorial function

program Main;
var result: integer;

function Factorial(number: Integer): Integer;
begin
if number > 1 then
    Factorial := number * Factorial(number-1)
else
    Factorial := 1
end;

begin
writeln('Factorial');
result := Factorial(6);
writeln(result)
end.

Playground

Author

Igor Kulman - [email protected]

License

This project is licensed under the MIT License - see the LICENSE file for details

More Repositories

1

iOSLocalizationEditor

Simple macOS editor app to help you manage iOS and macOS app localizations by allowing you to edit all the translations side by side
Swift
1,374
star
2

ChangeMenuBarColor

Simple utility to change macOS Big Sur and Monterey menu bar color by appending a solid color or gradient rectangle to a wallpaper image
Swift
911
star
3

iOSSampleApp

Sample iOS app demonstrating Coordinators, Dependency Injection, MVVM, Binding
Swift
715
star
4

rpi-thermometer

WebUI for a Raspberry Pi thermometer
JavaScript
49
star
5

AppStoreCrawler

AppStore and Google Play crawler written in F#
XML
18
star
6

Kulman.WPA81.BaseRestService

Base class for a Windows Phone 8.1 XAML, Windows 8.1 and Windows 10 Universal REST service implementation
C#
18
star
7

ExcelPackageF

ExcelPackageF is a simple F# wrapper over the EPPlus library.
F#
15
star
8

FOAASClient

Call Fuck Off As A Service in .NET
C#
14
star
9

QRReader.WPA81

Sample QR code reader app for Windows Phone 8.1
C#
11
star
10

dotfiles

Basic configuration I use on my macOS machines.
Shell
10
star
11

coding-journal

Source code for my programming blog located at https://blog.kulman.sk
CSS
8
star
12

CaliburnDemoWinRT

Sample code for my Developing Windows Store apps with Caliburn Micro, Unity and Fody series of articles.
C#
8
star
13

WebConsole

C# scripting console for a ASP.NET MVC project base on Roslyn
JavaScript
8
star
14

SimpleObservable

Very simple Observable and Publisher implementation for iOS apps.
Swift
7
star
15

xcode-templates

Xcode file templates sample for https://blog.kulman.sk/creating-your-own-xcode-templates/
7
star
16

thinkserver

Configuration for my home server running on an old Thinkpad T440s
Shell
6
star
17

AutoCompleteBox

AutoCompleteBox for WinRT (C# and XAML)
C#
6
star
18

Kulman.UWP

Collection of utilities and services for UWP apps
C#
4
star
19

AutoSensitivity

AutoSensitivity allows you to define different mouse sensitivities (speeds) for your touchpad and mouse and automatically switch between them (based on mouse connect / disconnect).
C#
4
star
20

Kulman.WP8

Collection of utilities and services for Windows Phone 8 and Windows Phone 8.1 Silverlight apps
C#
3
star
21

CodeGenerationSample

Sample Xcode project for the "Generating boilerplate Swift code with GYB" blog post
Python
3
star
22

GRDBCipher

Project showing how to build GRDB.swift with SQLCipher using Cocoapods to get fat frameworks to use in an app
Shell
3
star
23

lunchbuddy-bot

Slack bot providing daily menus for configured restaurants.
JavaScript
3
star
24

CaliburnWP8AppVSIX

Windows Phone 8 application template with Caliburn.Micro and Fody
C#
2
star
25

NumericPasswordBox

NumericPasswordBox for Universal Apps (Windows Phone 8.1 and Windows 8.1)
C#
2
star
26

Kulman.WPA81

Collection of utilities and services for Windows Phone 8.1 XAML apps
C#
2
star
27

RemoveUnusedResources

Command line utility for removing unused resources from Windows Phone RESX files
C#
2
star
28

oauth-winrt

A Windows Phone 8. / Windows 8.1 port of the OAuth Sample Library for C# from oauth.googlecode.com
C#
1
star
29

Kulman.WinRT

Collection of useful utilities and components for WinRT applications.
C#
1
star
30

rpi-temp-module

Node module for fetching data from the DS18B20 temperature sensor on Raspberry Pi
JavaScript
1
star
31

Kulman.WPA81.HighlightTextBox

A custom TextBlock for Windows Phone 8.1 and Windows 8.1 that allows letter highlighting
C#
1
star
32

SampleMessagePopup

Code samples for my Dialog helper for Universal Apps the easy way blog post, inspired by Joost van Schaik's blog post A behavior to show a MessageDialog from a MVVMLight viewmodel in Universal apps–with callbacks.
C#
1
star