AP CSP Pseudo-code Interpreter
Project description
CSPy - AP CSP Pseudo-code Interpreter
A pure Python interpreter for the AP Computer Science Principles (AP CSP) 2026 exam reference pseudo-code. It fully implements the standard syntax, including 1-based list indexing, procedures, recursion, and the standard library functions.
Note: This is just a final project for my dual-credit course. The code is total spaghetti and it severely lacks error handling. I might update it later, but probably won't.
Special thanks to Gemini 3 Pro for the invaluable assistance 🤓
✨ Features
- Standard Syntax Compliance: Supports variable assignment (
<-), arithmetic operations, and logical comparisons. - Control Flow: Full support for
IF/ELSE,REPEAT TIMES,REPEAT UNTIL, andFOR EACHloops. - AP CSP Specific Behavior:
- 1-Based Indexing: Lists are indexed starting from 1.
- Copy by Value: List assignment (
a <- b) creates an independent copy of the list, strictly adhering to the AP CSP exam mental model (modifying the new list does not affect the original).
- Procedures & Recursion: Supports
PROCEDUREdefinitions with parameter passing, local scoping, andRETURNstatements. - Built-in Library: Includes
DISPLAY,INPUT,RANDOM,APPEND,INSERT,REMOVE, andLENGTH. - Zero Dependencies: Built entirely with the Python standard library (
dataclasses,enum,sys,os,random).
🚀 Quick Start
Requirements
- Python 3.10+ (Recommended to run via
uvor standard python interpreter)
Running the Interpreter
Assuming your code file is named script.csp - you can name it whatever you like.
-
Using standard Python:
python -m cspy script.csp
-
Using
uv(recommended):uv run -m cspy script.csp
📖 Syntax Guide
Note: CSPy does not support comments. The
#comments shown here are purely for demonstration.
Variables & Assignment
x <- 10
str <- "Hello"
is_valid <- TRUE
List Operations
Note: Indexes start at
1.
# Definition
nums <- [10, 20, 30]
# Access & Modification
val <- nums[1] # Gets the first element (10)
nums[2] <- 99 # Modifies the second element
# Built-in List Functions
APPEND(nums, 40) # [10, 99, 30, 40]
INSERT(nums, 1, 5) # [5, 10, 99, 30, 40]
REMOVE(nums, 2) # Removes the element at index 2
len <- LENGTH(nums)
Control Flow
# Conditionals
IF (x > 5) {
DISPLAY("Big")
} ELSE {
DISPLAY("Small")
}
# Loops
REPEAT 3 TIMES {
DISPLAY("Hello")
}
REPEAT UNTIL (x = 10) {
x <- x + 1
}
# Iteration
FOR EACH item IN nums {
DISPLAY(item)
}
Procedures
PROCEDURE add(a, b) {
RETURN(a + b)
}
sum <- add(5, 10)
🛠️ Architecture
The interpreter follows a classic Recursive Descent architecture:
- Lexer: Tokenizes the source code, handling composite symbols (e.g.,
<=,<-) and keywords. - Parser: Generates an Abstract Syntax Tree (AST) using Python
dataclassesfor a clean and memory-efficient structure. - Interpreter: Traverses the AST using the Visitor Pattern.
- Maintains an
Environment Stackto handle local variables and recursion depth. - Enforces strict type checks and AP CSP-specific memory behaviors (e.g., list copying).
- Maintains an
📝 Example
Recursive Fibonacci Sequence:
PROCEDURE fib(n) {
IF (n <= 2) {
RETURN(1)
}
RETURN(fib(n - 1) + fib(n - 2))
}
DISPLAY("Fibonacci of 10 is:")
result <- fib(10)
DISPLAY(result)
🤝 Contributing
Contributions, issues, and feature requests are welcome! Planned improvements include:
- Detailed error reporting (Line/Column numbers).
- Interactive REPL mode.
📄 License
Apache License 2.0 OR MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file apcspy-0.1.1.tar.gz.
File metadata
- Download URL: apcspy-0.1.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07f020f18f22c3144b0da1d79f5078f45ef97fc457e62138a4056d422a7bd292
|
|
| MD5 |
af9423f0fc9fe8c1a2e26a1c6f168dfe
|
|
| BLAKE2b-256 |
38f05baead54d2187a27fae1bbd0f46ee3741b317a02ab022f5ce29264d973ae
|
File details
Details for the file apcspy-0.1.1-py3-none-any.whl.
File metadata
- Download URL: apcspy-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f5de5762252f4eed5c0eb02c44da548cbd7a95ddaf29a657b43bfd4d4b4745f
|
|
| MD5 |
457bfb2bf5050577a4abb5a80d0d4e68
|
|
| BLAKE2b-256 |
aaeacfd16bba85ca8bc66955358f4981618ac10750f043873507a4a311175fff
|