Skip to main content

No project description provided

Project description

# 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.

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

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

Source Distribution

ply-pybasic-1.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

ply_pybasic-1.0-py3.7.egg (39.8 kB view details)

Uploaded Egg

File details

Details for the file ply-pybasic-1.0.tar.gz.

File metadata

  • Download URL: ply-pybasic-1.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for ply-pybasic-1.0.tar.gz
Algorithm Hash digest
SHA256 da682d9d1fa70bf8cf752e1df169efcda55a162929e998027a860386e5de47da
MD5 d8008ea0fa663b638f949378e24bea69
BLAKE2b-256 ebe5409a36b9bac8b87a96181a78719667293dc6cbbaf5fbb92ea7b5f17dc30f

See more details on using hashes here.

File details

Details for the file ply_pybasic-1.0-py3.7.egg.

File metadata

  • Download URL: ply_pybasic-1.0-py3.7.egg
  • Upload date:
  • Size: 39.8 kB
  • Tags: Egg
  • Uploaded using 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

File hashes

Hashes for ply_pybasic-1.0-py3.7.egg
Algorithm Hash digest
SHA256 f25f882be5edfd194c28c15d209b754afa62188ceb8a73b3bbdc2222a40011db
MD5 bdf35a9781f99a291c92cc2a7afa19fb
BLAKE2b-256 a7fa6f215b1d6825db2b5650b79c074f3f6d3a505b6e3a46bc8375be340bddab

See more details on using hashes here.

Supported by

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