A fully-featured interpreter for the Cambridge International AS & A Level Computer Science (9618) pseudocode language.
This project implements an interpreter for the pseudocode specification used in Cambridge International AS & A Level Computer Science examinations. It supports all major language features including:
- Variables and constants
- All primitive data types (INTEGER, REAL, STRING, CHAR, BOOLEAN, DATE)
- Arrays (1D and 2D)
- Records (composite data types)
- Selection (IF/THEN/ELSE, CASE)
- Iteration (FOR, WHILE, REPEAT)
- Procedures and functions
- Parameter passing (BYVAL, BYREF)
- File handling
- Object-oriented programming (classes, inheritance)
- Built-in functions
brew tap andrinoff/cambridge
brew install cambridge
brew install cambridge-lsp # Optinal, Language Server Protocolsnap install cambridgeI build all binaries, and they are available here
git clone https://github.com/andrinoff/cambridge-lang.git
cd cambridge-lang
go build -o cambridge ./cmd/cambridge# Run a pseudocode file
./cambridge run program.pseudo
# Start interactive REPL
./cambridge repl
# Show version
./cambridge version
# Show help
./cambridge helpDECLARE Name : STRING
DECLARE Age : INTEGER
DECLARE Height : REAL
DECLARE IsStudent : BOOLEAN
CONSTANT PI = 3.14159
CONSTANT GREETING = "Hello"
Name <- "Alice"
Age <- 17
| Type | Description | Example |
|---|---|---|
| INTEGER | Whole numbers | 42, -17 |
| REAL | Floating-point numbers | 3.14, -0.5 |
| STRING | Text strings | "Hello" |
| CHAR | Single character | 'A' |
| BOOLEAN | True/False | TRUE, FALSE |
| DATE | Date values | - |
// 1D Array
DECLARE Numbers : ARRAY[1:10] OF INTEGER
Numbers[1] <- 100
// 2D Array
DECLARE Matrix : ARRAY[1:3, 1:3] OF INTEGER
Matrix[1, 2] <- 5
// IF statement
IF Score >= 90 THEN
Grade <- "A"
ELSE
Grade <- "B"
ENDIF
// CASE statement
CASE OF DayNumber
1 : OUTPUT "Monday"
2 : OUTPUT "Tuesday"
6, 7 : OUTPUT "Weekend"
OTHERWISE : OUTPUT "Midweek"
ENDCASE
// FOR loop
FOR i <- 1 TO 10
OUTPUT i
NEXT i
// FOR with STEP
FOR i <- 10 TO 0 STEP -2
OUTPUT i
NEXT i
// WHILE loop
WHILE Count < 10
Count <- Count + 1
ENDWHILE
// REPEAT loop
REPEAT
INPUT Value
UNTIL Value > 0
// Procedure
PROCEDURE Greet(Name : STRING)
OUTPUT "Hello, ", Name
ENDPROCEDURE
CALL Greet("World")
// Function
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTION
Result <- Square(5)
// BYREF parameter
PROCEDURE Swap(BYREF A : INTEGER, BYREF B : INTEGER)
DECLARE Temp : INTEGER
Temp <- A
A <- B
B <- Temp
ENDPROCEDURE
TYPE Student
DECLARE Name : STRING
DECLARE Age : INTEGER
DECLARE Grade : CHAR
ENDTYPE
DECLARE MyStudent : Student
MyStudent.Name <- "Alice"
MyStudent.Age <- 17
// Writing to a file
OPENFILE "data.txt" FOR WRITE
WRITEFILE "data.txt", "Hello, World!"
CLOSEFILE "data.txt"
// Reading from a file
OPENFILE "data.txt" FOR READ
WHILE NOT EOF("data.txt")
READFILE "data.txt", Line
OUTPUT Line
ENDWHILE
CLOSEFILE "data.txt"
// Appending to a file
OPENFILE "data.txt" FOR APPEND
WRITEFILE "data.txt", "New line"
CLOSEFILE "data.txt"
CLASS Animal
PRIVATE Name : STRING
PUBLIC PROCEDURE NEW(GivenName : STRING)
Name <- GivenName
ENDPROCEDURE
PUBLIC FUNCTION GetName() RETURNS STRING
RETURN Name
ENDFUNCTION
ENDCLASS
CLASS Dog INHERITS Animal
PUBLIC PROCEDURE Speak()
OUTPUT GetName(), " says Woof!"
ENDPROCEDURE
ENDCLASS
DECLARE MyDog : Dog
MyDog <- NEW Dog("Buddy")
CALL MyDog.Speak()
| Function | Description | Example |
|---|---|---|
LENGTH(s) |
Returns string length | LENGTH("Hello") → 5 |
LEFT(s, n) |
Returns leftmost n characters | LEFT("Hello", 2) → "He" |
RIGHT(s, n) |
Returns rightmost n characters | RIGHT("Hello", 2) → "lo" |
MID(s, start, len) |
Returns substring | MID("Hello", 2, 3) → "ell" |
UCASE(c) |
Converts to uppercase | UCASE('a') → 'A' |
LCASE(c) |
Converts to lowercase | LCASE('A') → 'a' |
| Function | Description | Example |
|---|---|---|
ASC(c) |
Returns ASCII value | ASC('A') → 65 |
CHR(n) |
Returns character for ASCII value | CHR(65) → 'A' |
| Function | Description | Example |
|---|---|---|
INT(x) |
Returns integer part | INT(3.7) → 3 |
RAND(n) |
Random real 0 to n | RAND(10) → 7.23 |
ROUND(x, p) |
Rounds to p decimal places | ROUND(3.456, 2) → 3.46 |
ABS(n) |
Absolute value | ABS(-5) → 5 |
SQRT(n) |
Square root | SQRT(16) → 4 |
POW(b, e) |
Power (b^e) | POW(2, 3) → 8 |
| Function | Description | Example |
|---|---|---|
NUM_TO_STR(n) |
Number to string | NUM_TO_STR(42) → "42" |
STR_TO_NUM(s) |
String to number | STR_TO_NUM("42") → 42 |
| Function | Description |
|---|---|
EOF(filename) |
Returns TRUE if at end of file |
| Operator | Description |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division (returns REAL) |
DIV |
Integer division |
MOD |
Modulus (remainder) |
| Operator | Description |
|---|---|
= |
Equal to |
<> |
Not equal to |
< |
Less than |
> |
Greater than |
<= |
Less than or equal |
>= |
Greater than or equal |
| Operator | Description |
|---|---|
AND |
Logical AND |
OR |
Logical OR |
NOT |
Logical NOT |
| Operator | Description |
|---|---|
& |
Concatenation |
See the examples/ directory for complete example programs:
hello.pseudo- Hello Worldvariables.pseudo- Variables and constantsselection.pseudo- IF and CASE statementsloops.pseudo- FOR, WHILE, REPEAT loopsfunctions.pseudo- Procedures and functionsarrays.pseudo- Array operationsstrings.pseudo- String manipulationrecords.pseudo- Record typesoop.pseudo- Object-oriented programmingfileio.pseudo- File handling
This interpreter is based on the Cambridge International AS & A Level Computer Science (9618) pseudocode specification. For the official specification, see:
MIT License