An interpreter of IB pseudocode built from scratch in C++!
View Demo
·
Report Bug
·
Request Feature
Table of Contents
This project is an interpreter of the IB pseudocode language built from scratch in C++. IB pseudocode is used in the IB CS course to write simple algorithms and programs. It contains all basic procedural structures (while, for, if/else), functions, primitive types (bool, float, int) arrays and strings, and ADTs (stack, queue, linked list/collections). It is a pseudo-OOP language, as ADTs are treated as objects but the user cannot define their own objects.
The interpreter utilizes a recursive descent parser to build the abstract syntax tree, which implements runtime polymorphism to represent all structures/expressions as nodes. Types are runtime-resolved and objects utilize sum-type std::variant to store the appropriate type. Some built in functions such as constructors for ADTs and input/output are hardcoded.
This is a guide of how to build the interpreter on your machine and run an example program.
- g++ 11.3.0
- GNU make 4.3
- git 2.34.1
On Ubuntu:
$ sudo apt install git build-essential
- Clone the repo on your machine.
$ git clone https://github.com/rafaelmoschopoulos/PseudoInterp
$ cd PseudoInterp/
- Build
$ make clean
$ make
The binary can be found in the /bin folder.
The following flags are available:
-?: Prints informational message regarding usage.-v: Prints program version.-i: Sets input file.
To execute the example program, run $ ./bin/pseudointerp -i ./examples/ex1.
This program reverses the contents of a stack.
More information on syntax can be found by referring to the official IB pseudocode guide, and looking at more examples in /examples.
- Test pseudocode programs, or practice for IB tests/exams.
- Quickly implement various algorithms, as all basic ADTs are available.
- Literals
pi = 3.14 age = 10 sad = false str = "hey\teveryone\tim\ta\t\string\n" chr = '\x24' - Control flow
a = 3, b = 5 if a > b or a == 0 then output(a+b) else if a == b then output(a) else output("Foo")loop for i from 0 to 10 output(i*i) n = 0 loop while n < 10 output(n++) - Functions
method fibo(n) if n == 0 or n == 1 then return n else return fibo(n-1) + fibo(n-2) - I/O
age = 0 input(age) output("i am " + age + "years old") - Arrays and strings
A = Array(10) A = [1, 34, 23, 56, 3] str = "hello world" output(str[3] + " " + A[2]) twoD = [[1, 2], [3, 4]] output(twoD[0, 1] + twoD[1][0]) - ADTs
S = Stack(), Q = Queue(), C = Collection() S.push(2), Q.enqueue(3) if not S.isEmpty() and not Q.isEmpty() then output(S.pop() + Q.dequeue()) loop for i from 0 to 4 C.addItem(input()) loop while C.hasNext() output(C.getNext()) C.resetNext()
See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the MIT License. See LICENSE for more information.
Your Name - @raf_mos - raf.mos@princeton.edu
Project Link: https://github.com/rafaelmoschopoulos/PseudoInterp