3 releases

new 0.4.2 Nov 6, 2024
0.4.1 Nov 6, 2024
0.4.0 Nov 5, 2024

#32 in Programming languages

Download history 295/week @ 2024-11-01

295 downloads per month

MIT license

770KB
19K SLoC

p2sh

Rust

🦀 🦀 🦀 Interpreter for the p2sh programming language 🦀 🦀 🦀

Introduction

p2sh is a scripting language designed for packet processing. The Documentation can be found here. This project served as a means for me to learn Rust; therefore, some parts of the code might not adhere strictly to Rust idioms. A p2sh script can be edited using the kilo editor which is tailored to recognize its syntax.

Dependencies

The p2sh interpreter is compiled using a Rust compiler v1.65.0. Any version later than that should work. Please visit the Rust website for installation.

Build and test

Run tests

It has decent test coverage for all the modules, including tests for the scanner, parser, opcode definitions, compiler, virtual machine, and built-ins.

cargo test

Run examples

cargo run --release examples/formatting/formatted-output.p2
cargo run --release examples/algorithms/fibonacci-recursive.p2
cargo run --release examples/algorithms/fibonacci-iterative.p2

Run the REPL

Run an interactive REPL loop to execute program statements on the terminal one at a time and see the output immediately.

cargo run --release
>> 1 + 2
3

Create a release build

Build release with default options

cargo build --release

This will create a release binary in the './target/release' directory.

Additional build options

debug_print_code

The option helps building source with source code disassembly enabled.

Build with the option:

cargo build --release --features  'debug_print_code'

Run the REPL:

cargo run --release --features  'debug_print_code'
...

>> 1 + 2
--------- Instructions [len: 8   ] -------------------
0000 OpConstant 0
0003 OpConstant 1
0006 OpAdd
0007 OpPop
------------------------------------------------------
----------- Constants [len: 2   ] --------------------
[0] 1
[1] 2
------------------------------------------------------
3

Run an example:

cargo run --release --features  'debug_print_code' examples/algorithms/fibonacci-recursive.p2

debug_trace_execution

This option helps build the release for tracing program execution and examining the state of the stack.

cargo build --release --features  'debug_trace_execution'

Installation

The p2sh intepreter can be installed by copying the binary to a directory that is in your path such as /usr/local/bin. Once installed, it can be executed by running it like so

p2sh script.p2 <args>

shebang

A p2sh script may also be executed using a shebang. The first line of a p2sh script must be a line that looks like this:

#!/usr/bin/env p2sh

Then, the script may be run as:

./script.p2 <args>

OR

/path/to/script.p2 <args>

More Information

  • Examples may be found in the examples directory
  • Documentation can be found here

Dependencies

~3–10MB
~100K SLoC