Simple Pascal interpreter written in Swift

Pascal interpreter written in Swift

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

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)

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



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 our in the Playground or on the unit tests .


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.

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


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


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.


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;
if number > 1 then
    Factorial := number * Factorial(number-1)
    Factorial := 1

result := Factorial(6);

Github责编内容来自:Github (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合技术 » Simple Pascal interpreter written in Swift

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录