Skip to main content

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.

AP CSP Exam Reference Sheet

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.

✨ Features

  • Standard Syntax Compliance: Supports variable assignment (<-), arithmetic operations, and logical comparisons.
  • Control Flow: Full support for IF/ELSE, REPEAT TIMES, REPEAT UNTIL, and FOR EACH loops.
  • 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 PROCEDURE definitions with parameter passing, local scoping, and RETURN statements.
  • Built-in Library: Includes DISPLAY, INPUT, RANDOM, APPEND, INSERT, REMOVE, and LENGTH.
  • Zero Dependencies: Built entirely with the Python standard library (dataclasses, enum, sys, os, random).

🚀 Quick Start

Requirements

  • Python 3.10+ (Recommended to run via uv or standard python interpreter)

Running the Interpreter

Assuming your code file is named script.csp - you can name it whatever you like.

  1. Using standard Python:

    python -m cspy script.csp
    
  2. Using uv (recommended):

    uvx apcspy 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:

  1. Lexer: Tokenizes the source code, handling composite symbols (e.g., <=, <-) and keywords.
  2. Parser: Generates an Abstract Syntax Tree (AST) using Python dataclasses for a clean and memory-efficient structure.
  3. Interpreter: Traverses the AST using the Visitor Pattern.
    • Maintains an Environment Stack to handle local variables and recursion depth.
    • Enforces strict type checks and AP CSP-specific memory behaviors (e.g., list copying).

📝 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


Download files

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

Source Distribution

apcspy-0.1.2.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

apcspy-0.1.2-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file apcspy-0.1.2.tar.gz.

File metadata

  • Download URL: apcspy-0.1.2.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","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

Hashes for apcspy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b1fdf29be3bbb9c0f62bc765d317e62cb5ec8a7f7654d0b355699bc5c05c9d46
MD5 a03755a88182eb8083ba9acc6d7e376e
BLAKE2b-256 b64ad1e9531461c24ea0f929a9c418e35e60b3a376ce9dfcc8ce672ce0b718e7

See more details on using hashes here.

File details

Details for the file apcspy-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: apcspy-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","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

Hashes for apcspy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 873cadfdc7e7bc79d9075de8db7ea7250a85568aac5d3e7c10dcc09f3cc5642d
MD5 d7c436fd957a677508b1b01002d4df97
BLAKE2b-256 e751aee4668ba5c1b4253c8a3666ff7d81e517bb6d0ed4705e4246812d34465b

See more details on using hashes here.

Supported by

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