Skip to main content
# pybasic

A structured BASIC interpreter written in Python.

```plain

usage: pybasic.py [-h] [-a] [-s AST_PATH] [program_name]

Execute pybasic programs, or start an REPL session.

positional arguments:

program_name The path of the source program to execute. If not
specified, an REPL session will be started.

optional arguments:
-h, --help show this help message and exit
-a, --ast Execute a binary abstract syntax tree file rather than
a source program. This will be ignored in REPL mode.
-s AST_PATH, --save AST_PATH
Save the binary abstract syntax tree of the source
program to the given path. The source program will not
be executed. This will be ignored in REPL mode.
```

#### expressions

Supported expressions:

- `<expression>`: Math expressions. Support operators: `+`, `-`, `*`,
`/`, `\`(exact division), `MOD`
- `<rel_expression>`: Logic expressions. Support operators: `AND`,
`OR`, `NOT`, `=`(equals to), `<>`

Please notice `=` in `<rel_expression>` is different from `=` in assignment statement.

Literal values and function calls are also treated as `<expression>`. For example:

```basic
123 ' 123
123.5 ' 123.5
"123!" ' "123!"
{1, 2, 3} ' [1, 2, 3]
SQR(5) ' 2.23606797749979
```

#### assignments

Use `LET <id> = <expression>` or simply `<id> = <expression>`.

Declarations are not required, for a variable are defined immediately before it is assigned for the first time. However, you can still use `DIM` to create an array filled with initial values. For example:

```basic
DIM A(5) AS INTEGER
PRINT A ' [0, 0, 0, 0, 0]
```

#### control structures

Supported control structures:

- `WHILE <rel_expression> ... END WHILE / WEND`
- `DO ... LOOP`
- `DO ... LOOP WHILE / UNTIL <rel_expression>`
- `FOR <id> = <expression> TO <expression> ... NEXT <id> / END FOR`

Please notice that `GOTO` is not supported.

#### data types

Supported data types:

- `INTEGER` (mapped to Python type `int`)
- `DECIMAL` (mapped to Python type `float`)
- `STRING` (mapped to Python type `str`)

Please notice that pybasic does not actually store data types, which means the type of variables can be modified at runtime.

Use `AS` operator to do type conversions. For example:

```basic
2.5 AS INTEGER ' 2
2.5 AS STRING ' "2.5"
```

#### functions

Some inner functions are provided, like `SQR()` and etc.

Single-line functions can be defined with `DEFUN`. For example:

```basic
DEFUN F(x) = x * x
PRINT F(5) ' 25
```

Multi-line functions can be defined with `SUB ... END SUB` or `FUNCTION ... END FUNCTION`.

Functions can be called directly by using the function name as an order. For example:

```basic
DEFUN PRTYES(s) = PRINT("yes! " + s)
SUB PRTYEAH(s)
PRINT "yeah! " + s
END SUB
PRTYES "pybasic" ' "yes! pybasic"
PRTYEAH "pybasic" ' "yeah! pybasic"
```

#### I/O

Unlike most BASIC dialects, pybasic provides `PRINT()` and `INPUT()` functions instead of statements. For example:

```basic
A = INPUT() AS INTEGER ' input 17
PRINT "My age is " + A ' "My age is 17"

```

#### modules

Use ```USE``` to import a Python module or another pybasic program. Pybasic will try finding a file ended with ```.bas``` or ```.py``` following the module's name in the current working directory. If no such file is finded, an error will be raised. For example:

```basic
' HELLO.bas
FUNCTION PRTHELLO(A)
PRINT "Hello, " + A
END FUNCTION
```

```python
# MORNING.py
from pybasic import global_table

@global_table.reflect('PRTMORN')
def print_morning(a):
print('Good morning, %s' % a)
```

```basic
' MAIN.bas
USE HELLO
USE MORNING

PRTHELLO "Jack" ' Hello, Jack
PRTMORN "Mary" ' Good morning, Mary
```

Any code in the module will be executed. If the module is python-based, it will be executed at runtime; if it is pybasic-based, it will be compiled into the main program before being executed.

Metadata

Release files for ply-pybasic 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ply-pybasic 1.0
File Size Uploaded
ply-pybasic-1.0.tar.gz 14.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ply-pybasic 1.0
File Interpreter ABI Platform
ply_pybasic-1.0-py3.7.egg Legacy Egg format - - Details

Total release size: 54.2 kB

Release files / ply-pybasic-1.0.tar.gz

Download URL ply-pybasic-1.0.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
da682d9d1fa70bf8cf752e1df169efcda55a162929e998027a860386e5de47da
BLAKE2b-256 checksum
How to use checksums
ebe5409a36b9bac8b87a96181a78719667293dc6cbbaf5fbb92ea7b5f17dc30f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.15

Release files / ply_pybasic-1.0-py3.7.egg

Download URL ply_pybasic-1.0-py3.7.egg
Size 39.8 kB
Tags Egg
SHA-256 checksum
How to use checksums
f25f882be5edfd194c28c15d209b754afa62188ceb8a73b3bbdc2222a40011db
BLAKE2b-256 checksum
How to use checksums
a7fa6f215b1d6825db2b5650b79c074f3f6d3a505b6e3a46bc8375be340bddab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.15

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page