Skip to main content

A pseudocode-to-Python transpiler based on the format specified in CIE IGCSE (Syllabus 0478)

Project description

Dudocode

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'll 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:

Getting started

  1. Dudocode is built on top of Python. If you do not have Python, please download and install it here.

  2. Add your Python installation to PATH.

  3. Dudocode is available as a PyPI package. To download and install the latest version of Dudocode, run

    pip install dudocode
    
  4. Interact with the dudo CLI in terminal. See documentation for help, or try out the examples in demos.

Notepad++ Integration

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. It's recognised file endings are .ddo and .notcode.

Demos

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.

Hello World

Pseudocode:

OUTPUT "Hello World!"

Output:

Hello World!

Triangular Stars

Pseudocode:

INPUT NumRows
FOR i  0 TO NumRows
    FOR j  0 TO i
        OUTPUT '*'
    NEXT j
    OUTPUT '\n'
NEXT i

Output:

> 10
*
**
***
****
*****
******
*******
********
*********
**********
***********

Arithmetic

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

Sieve of Eratosthenes

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 

Recursion

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

File Reading and Writing

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

Documentation

dudo

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.

dudo run

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

Quick Reference

This section contains snippets of common pseudocode patterns, taken from the CIE IGCSE specification.

Data types

  • INTEGER
  • REAL
  • CHAR
  • STRING
  • BOOLEAN

Array Declaration

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>

Control Flow

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

Loops

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

Procedure Declaration

Without arguments

PROCEDURE <identifier>
    <statements>
ENDPROCEDURE

With arguments

PROCEDURE <identifier>(<param1>:<datatype>, <param2>:<datatype>...)
    <statements>
ENDPROCEDURE

Function Declaration

FUNCTION <identifier>(<param1>:<datatype>, <param2>:<datatype>...) RETURNS <data type>
    <statements>
ENDFUNCTION

Filestream Operations

OPENFILE <filename> FOR READ
READFILE <filename>, <variable>

OPENFILE <filename> FOR WRITE
WRITEFILE <filename>, <variable>

CLOSEFILE <filename>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dudocode-0.6.1.tar.gz (32.1 kB view hashes)

Uploaded Source

Built Distribution

dudocode-0.6.1-py3-none-any.whl (39.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page