This repository contains the source code for Dudocode, a pseudocode-to-Python transpiler based on the format specified in CIE IGCSE (Syllabus 0478). It allows you to run pseudocode source files, as well as executing pseudocode interactively in the console.
The pseudocode syntax is specified in this PDF (downloaded from the official CIE IGCSE website). Dudocode supports all of these documented features! Yep, finally filestream operations too, following the v0.6 update.
Note that the arrow assignment operator (←
) can be replaced with <-
for easier typing.
Dudocode's transpilation workflow is heavily reliant on trilobyte. This is a custom text-pattern parsing engine which I built primarily for this project and for processing code. Nevertheless, it has a very general syntax and has a wide range of potential use cases. I decided to build Trilobyte not least because I was bored of Regex (I've seen enough transpiler projects out there that use this), but also because I thought it would be fun (and it was!). Trilobyte ended up using a custom 'keypoints'-based algorithm which I devised. There are still some bugs with Trilobyte, but all of its features used in Dudocode seem to be stable.
Contents:
-
Dudocode is built on top of Python. If you do not have Python, please download and install it here.
-
Add your Python installation to PATH.
-
Dudocode is available as a PyPI package. To download and install the latest version of Dudocode, run
pip install dudocode
-
Interact with the
dudo
CLI in terminal. See documentation for help, or try out the examples in demos.
I have created a User Defined Language file to aid you in coding with pseudocode. Download notepadpp_udl_dudocode.xml
, and import it into Notepad via Language -> Define your language... -> Import
. This language file supports syntax highlighting, code folding, and auto-completion for all of Dudocode's features. Its recognised file endings are .ddo
and .notcode
.
Try running the following pseudocode programs if you're not sure how to get started!
In all the examples below, any output line that starts with an >
denotes where user input is required.
Pseudocode:
OUTPUT "Hello World!"
Output:
Hello World!
Pseudocode:
INPUT NumRows
FOR i ← 0 TO NumRows
FOR j ← 0 TO i
OUTPUT '*'
NEXT j
OUTPUT '\n'
NEXT i
Output:
> 10
*
**
***
****
*****
******
*******
********
*********
**********
***********
Pseudocode:
OUTPUT "Enter a number: "
INPUT NumA
OUTPUT "Enter another number: "
INPUT NumB
NumA <- REAL(NumA)
NumB <- REAL(NumB)
OUTPUT "Enter operator: "
INPUT Operator
// Awesome CASE statements are supported by Dudocode!
CASE OF Operator
"add": OUTPUT NumA + NumB
"sub": OUTPUT NumA - NumB
"mul": OUTPUT NumA * NumB
"div": OUTPUT NumA / NumB
"mod": OUTPUT MOD(NumA, NumB)
OTHERWISE OUTPUT "Unknown operator"
ENDCASE
Output:
> Enter a number: 14
> Enter another number: 7
> Enter operator: mod
0.0
Pseudocode:
INPUT Limit
DECLARE IsPrime : ARRAY[2:Limit] OF BOOLEAN
// Initialise array
FOR Number ← 2 TO Limit
IsPrime[Number] ← TRUE
NEXT Number
FOR Number ← 2 TO Limit
IF IsPrime[Number] = TRUE
THEN
// Print Number if it is prime
OUTPUT Number, " "
// Then mark all its multiples as not prime
FOR Multiple ← 2 TO DIV(Limit, Number)
IsPrime[Number * Multiple] ← FALSE
NEXT Multiple
ENDIF
NEXT Number
Output:
> 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Pseudocode:
FUNCTION Factorial(Num:INTEGER) RETURNS INTEGER
IF Num = 0 OR Num = 1
THEN
RETURN 1
ELSE
RETURN Num * Factorial(Num - 1)
ENDIF
ENDFUNCTION
INPUT Number
OUTPUT Factorial(Number), "\n"
Output:
> 20
2432902008176640000
Suppose you have a file called inp.txt
, and you wanna copy its contents to out.txt
:
Pseudocode:
OPENFILE inp.txt FOR READ
READFILE inp.txt, Text
CLOSEFILE inp.txt
OUTPUT "Text from file: ", Text, "\n"
OPENFILE out.txt FOR WRITE
WRITEFILE out.txt, Text
CLOSEFILE out.txt
Dudo is Dudocode's versatile CLI, allowing you to convert and run pseudocode source files. It also supports interactive pseudocode execution.
The following commands may be run on the command line:
usage: dudo [-h] [-v] {run} ...
Dudocode is a Pseudocode interpreter that transpiles pseudocode to Python.
positional arguments:
{run} Dudocode subcommands (use `dudo` without any commands to launch interactive console)
run Run pseudocode source files with Dudocode
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
To launch the interactive console, simply run dudo
.
The dudo run
subcommand deals with transpiling and running pseudocode source files:
usage: dudo run [-h] [-d] [-p] [-s] [-o OUT] [-q] [-v] path
positional arguments:
path path to Dudocode source code
optional arguments:
-h, --help show this help message and exit
-d, --dudo print the source Dudocode
-p, --py print the transpiled Python program
-s, --save save the transpiled Python program
-o OUT, --out OUT path to saved Python program when flag `--save` is passed (if not specified, this defaults to that of the input file, but with `.py` as file extension)
-q, --quiet does not run the transpiled Python program
-v, --verbose print stupid comments while transpiling
This section contains snippets of common pseudocode patterns, taken from the CIE IGCSE specification.
INTEGER
REAL
CHAR
STRING
BOOLEAN
1D
DECLARE <identifier> : ARRAY[<l1>:<u1>, <l2>:<u2>] OF <data type>
2D
DECLARE <identifier> : ARRAY[<l1>:<u1>, <l2>:<u2>] OF <data type>
nD
DECLARE <identifier> : ARRAY[<l1>:<u1>, <l2>:<u2>, ..., <ln>:<un>] OF <data type>
Note the use of 2 spaces instead of 4 in some of these indentations.
Simple IF statement
IF <condition>
THEN
<statements>
ENDIF
IF-ELSE statement
IF <condition>
THEN
<statements>
ELSE
<statements>
ENDIF
CASE statement (without default)
CASE OF <identifier>
<value 1> : <statement>
<value 2> : <statement>
...
ENDCASE
CASE statement (with default)
CASE OF <identifier>
<value 1> : <statement>
<value 2> : <statement>
...
OTHERWISE <statement>
ENDCASE
FOR loop
FOR <identifier> ← <value1> TO <value2> STEP <increment>
<statements>
NEXT <identifier>
REPEAT-UNTIL loop
REPEAT
<Statements>
UNTIL <condition>
WHILE loop
WHILE <condition> DO
<statements>
ENDWHILE
Without arguments
PROCEDURE <identifier>
<statements>
ENDPROCEDURE
With arguments
PROCEDURE <identifier>(<param1>:<datatype>, <param2>:<datatype>...)
<statements>
ENDPROCEDURE
FUNCTION <identifier>(<param1>:<datatype>, <param2>:<datatype>...) RETURNS <data type>
<statements>
ENDFUNCTION
OPENFILE <filename> FOR READ
READFILE <filename>, <variable>
OPENFILE <filename> FOR WRITE
WRITEFILE <filename>, <variable>
CLOSEFILE <filename>