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

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

License

NotificationsYou must be signed in to change notification settings

igorkulman/SwiftPascalInterpreter

Repository files navigation

License: MITSwift VersionTwitter

Simple Swift interpreter for the Pascal language inspired by theLet’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 theExamples directory, like a simplenumber guessing game and afactorial computation.

Scructure

Lexer

TheLexer reads the Pascal program asString (a sequence of characters) and converts it into a sequence ofTokens. You can see the result by trying it out in the Playground or on theunit tests.

Lexer

Parser

TheParser reads the sequence of tokens produced by the Lexer and builds anAbstract Syntax Tree representation (AST for short) of the Pascal program according to thegrammar.

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

Parser

Semantic analyzer

TheSemantic 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 aSymbol 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

TheInterpreter 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 (seeunit tests) or print it in the Playground usingprintState().

Try it out

CLI

When you build theSPI 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;functionFactorial(number: Integer): Integer;beginif number >1then    Factorial := number * Factorial(number-1)else    Factorial :=1end;beginwriteln('Factorial');result := Factorial(6);writeln(result)end.

Playground

Author

Igor Kulman -igor@kulman.sk

License

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp