Skip to main content

Custom Syntax for Python Language -- CSPLang

Project description

banner

CSPLang – Custom Syntax for Python Language

CSPLang is a compact custom syntax designed for Python. It’s a toy language that works by transforming each line of CSP code into Python code, then executing it. The focus is on writing less code but doing more work.

Think about it this way:

What if you run real python, but write it in a bit different way?

Basically, CSP doesn’t block you from using Python’s full ecosystem; it just gives you a syntactic sugar wrapper so your loops, functions, conditionals, and variables can be written in a shorter or more “DSL-like” style.


Developer

I am Muhammad Abubakar Siddique Ansari, a passionate developer in Data Science and AI. I love creating utilities, exploring new programming ideas, and building simple yet powerful tools.

I am currently (2026) a 1st-year ICS student at KIPS College, Punjab – Pakistan. Portfolio: https://ansari-codes.github.io/portfolio


Installation

  1. Clone the repo:
git clone https://github.com/Ansari-Codes/custom-syntax-for-python.git
cd custom-syntax-for-python
pip install -e .
  1. Via pip:
pip install csp_lang
  1. Pip installation using git:
pip install git+https://github.com/Ansari-Codes/custom-syntax-for-python.git

Key Principles

  1. Compact Syntax: Write less code for the same Python functionality.
  2. Readable & Straightforward: Easy to understand even if you are new to Python.
  3. Direct Python Integration: Most Python functions, utilities, and modules work without changes.

Syntax Overview

1. Comments & Strings

Single-line and multi-line comments are supported:

# Single-line comment

% 
This is a
multi-line comment
%

Strings

Strings use backticks for both mutliline or single line. Example:

string <- `This is single line string`
print(string)
string <- `This is multiline
string spaning many lines long long
long long text`
print(string)
string <- f`Formatted string!
2 + 2 = {2 + 2}`
print(string)

2. Built-in Python Functions

Python functions and utilities work as-is.

print(10)       # Works exactly like Python
len([1,2,3])    # Python’s len function

3. Variables

Use <- as the assignment operator:

name <- "Muhammad"
age <- 15
x <- 5 + 3

4. Conditionals

CSP replaces if, elif, and else with a simple, readable syntax:

x <- 5

%
IMPORTANT: Don;t use comments after colon of the conditions
EXAMPLE:
    1. Correct code:
    x>0:
    2. Buggy code:
    x>0: #comment
%

# if x > 0
x > 0:
    print("x is positive")
# elif x == 5
: x == 5:
    print("x is exactly 5")
# else
:
    print("other")

The : indicates the start of a condition block, and : alone is treated as else.

5. Loops

While Loop

Use >> for while loops:

%
IMPORTANT: Don;t use comments after colon of the condition
EXAMPLE:
    1. Correct code:
    x > 0:
    2. Buggy code:
    x > 0: #comment
%

x <- 3
>> x > 0:
    print(x)
    x <- x - 1

For Loop

Use => with : replacing in:

%
IMPORTANT: Don;t use comments after colon of the i:iterable:
EXAMPLE:
    1. Correct code:
    x:range(5):

    2. Buggy code:
    x:range(5): #comment
%

=> i:[1,2,3]:
    print(i)

=> j:range(5):
    print(j)

i is the loop variable, and iterable can be any Python iterable.

6. Functions

Functions are defined using $ and [] instead of def and ():

%
IMPORTANT: Don;t use comments after colon of the functio definition
EXAMPLE:
    1. Correct code:
    $add[a, b]:

    2. Buggy code:
    $add[a, b]: #comment
%

$add[a, b]:
    -> a + b   # return statement

$greet[name="User"]:
    print(f"Hello {name}")
print(add(1, 1))

Call functions normally:

print(add(2, 3))
greet()
greet("Muhammad")

7. Imports

Import modules as in Python:

import math as mt
from math import sqrt

If you have tqdm installed, for example:

from tqdm import tqdm
from time import sleep
=> i:tqdm(range(30)):
    sleep(0.1)

Currently, importing other .csp files is not supported.


File Format

CSP files use the .csp extension.


CLI Usage

You can run CSP programs or transpile them to Python:

# Run directly
csp run program.csp

# Transpile to Python file
csp transpile program.csp -o program.py
python program.py

# run tests
csp test

Example Program

# variable
x <- 3

# function definition
$fun[a]:
    # python functions
    print(f`Start {a}`)
    # while loop
    >> a > 0:
        # for loop
        => i:[1,2]:
            print(`Loop`, i)
        a <- a - 1
    # return
    -> `done`

print(fun(x))

Output:

Start 3
Loop 1
Loop 2
Loop 1
Loop 2
Loop 1
Loop 2
done

Logo

logo

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

csp_lang-0.1.2.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

csp_lang-0.1.2-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: csp_lang-0.1.2.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for csp_lang-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f0b4a76367c1bb46a753bef56d1c7e3fae967d82e478621e67e9d88a803e62fb
MD5 c436b7d5637e0c1db5251cec4637042d
BLAKE2b-256 710b26ae9d3b9062c92a8961cfbcb9bf5cd0cd63d6f5fe2ee9cd307d6953ace8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: csp_lang-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for csp_lang-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4f39cdafc208858910626dadea6fbfb8c4dd27f9f6a2b0ff0c3b679a8c258c72
MD5 15435ed8ade688a31626ebb92ced4550
BLAKE2b-256 ddd44cadc2eb55487bc0d3291d5a2806e409f65dccee087b7957c24514f842f0

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