All Projects → igorkulman → Swiftpascalinterpreter

igorkulman / Swiftpascalinterpreter

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

Programming Languages

swift
15916 projects
pascal
1382 projects
grammar
57 projects

Projects that are alternatives of or similar to Swiftpascalinterpreter

pascal-interpreter
A simple interpreter for a large subset of Pascal language written for educational purposes
Stars: ✭ 21 (-92.22%)
Mutual labels:  parse, interpreter, ast, lexer
Lioness
The Lioness Programming Language
Stars: ✭ 155 (-42.59%)
Mutual labels:  ast, lexer, interpreter
Libpypa
libpypa is a Python parser implemented in pure C++
Stars: ✭ 172 (-36.3%)
Mutual labels:  ast, lexer, parse
Snapdragon
snapdragon is an extremely pluggable, powerful and easy-to-use parser-renderer factory.
Stars: ✭ 180 (-33.33%)
Mutual labels:  ast, lexer, parse
Parser
A lexer and parser for GraphQL in .NET
Stars: ✭ 163 (-39.63%)
Mutual labels:  ast, lexer, parse
Cub
The Cub Programming Language
Stars: ✭ 198 (-26.67%)
Mutual labels:  ast, lexer, interpreter
color-math
Expressions to manipulate colors.
Stars: ✭ 18 (-93.33%)
Mutual labels:  parse, interpreter
asmdot
[Unstable] Fast, zero-copy and lightweight (Arm | Mips | x86) assembler in (C | C++ | C# | Go | Haskell | Javascript | Nim | OCaml | Python | Rust).
Stars: ✭ 23 (-91.48%)
Mutual labels:  parse, ast
fayrant-lang
Simple, interpreted, dynamically-typed programming language
Stars: ✭ 30 (-88.89%)
Mutual labels:  interpreter, lexer
astutils
Bare essentials for building abstract syntax trees, and skeleton classes for PLY lexers and parsers.
Stars: ✭ 13 (-95.19%)
Mutual labels:  parse, ast
ocean
Programming language that compiles into a x86 ELF executable.
Stars: ✭ 164 (-39.26%)
Mutual labels:  ast, lexer
malluscript
A simple,gentle,humble scripting language for mallus, based on malayalam memes.
Stars: ✭ 112 (-58.52%)
Mutual labels:  interpreter, lexer
Own-Programming-Language-Tutorial
Репозиторий курса "Как создать свой язык программирования"
Stars: ✭ 95 (-64.81%)
Mutual labels:  interpreter, lexer
snapdragon-lexer
Converts a string into an array of tokens, with useful methods for looking ahead and behind, capturing, matching, et cetera.
Stars: ✭ 19 (-92.96%)
Mutual labels:  parse, lexer
abstract-syntax-tree
A library for working with abstract syntax trees.
Stars: ✭ 77 (-71.48%)
Mutual labels:  parse, ast
pyccolo
Declarative instrumentation for Python.
Stars: ✭ 70 (-74.07%)
Mutual labels:  interpreter, ast
monkey
The Monkey Programming Language & Interpreter written in PHP.
Stars: ✭ 21 (-92.22%)
Mutual labels:  interpreter, lexer
bredon
A modern CSS value compiler in JavaScript
Stars: ✭ 39 (-85.56%)
Mutual labels:  ast, lexer
MonkeyLang.jl
"Writing an Interpreter in GO" and "Writing a Compiler in GO" in Julia.
Stars: ✭ 30 (-88.89%)
Mutual labels:  interpreter, lexer
aria
Expressive, noiseless, interpreted, toy programming language
Stars: ✭ 40 (-85.19%)
Mutual labels:  interpreter, lexer

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].