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.
- 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).
- Python 3.10+ (Recommended to run via
uvor standard python 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
Note: CSPy does not support comments. The
#comments shown here are purely for demonstration.
x <- 10
str <- "Hello"
is_valid <- TRUE
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)
# 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)
}
PROCEDURE add(a, b) {
RETURN(a + b)
}
sum <- add(5, 10)
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
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)
Contributions, issues, and feature requests are welcome! Planned improvements include:
- Detailed error reporting (Line/Column numbers).
- Interactive REPL mode.
Apache License 2.0 OR MIT License