novapoly-interpreter

Crates.ionovapoly-interpreter
lib.rsnovapoly-interpreter
version0.1.0
created_at2025-11-29 17:22:45.896008+00
updated_at2025-11-29 17:22:45.896008+00
descriptionA Python-like code prediction language interpreter written in Rust
homepagehttps://novapoly.org
repositoryhttps://github.com/NovaPoly/novapoly-interpreter
max_upload_size
id1957095
size66,524
Ben (brunjamin)

documentation

https://novapoly.org/docs

README

NovaPoly Interpreter

A Python-like language interpreter written in Rust. This library provides a complete interpreter for a Python-inspired language with support for variables, functions, control flow, data structures, and more.

Features

  • Lexer: Tokenizes source code with support for Python-style indentation
  • Parser: Builds an Abstract Syntax Tree (AST) from tokens
  • Interpreter: Executes the AST with a runtime environment
  • Data Types: Numbers, strings, booleans, lists, dictionaries, and None
  • Control Flow: if/else, while loops, for loops
  • Functions: User-defined functions with parameters
  • Operators: Arithmetic, comparison, logical, and assignment operators
  • Built-in Functions: print(), len(), range(), resolve_yes(), resolve_no()

Usage

Add this to your Cargo.toml:

[dependencies]
novapoly-interpreter = { path = "." }

Basic Example

use novapoly_interpreter::execute;

fn main() {
    let code = r#"
x = 10
y = 20
result = x + y
print(result)
"#;
    
    match execute(code) {
        Ok(value) => println!("Program executed successfully: {}", value),
        Err(e) => eprintln!("Error: {}", e),
    }
}

Advanced Example

use novapoly_interpreter::execute;

fn main() {
    let code = r#"
def factorial(n):
    if n <= 1:
        return 1
    else:
        return n * factorial(n - 1)

result = factorial(5)
print(result)

# Lists and loops
numbers = [1, 2, 3, 4, 5]
sum = 0
for num in numbers:
    sum = sum + num
print(sum)

# Dictionaries
person = {"name": "Alice", "age": 30}
print(person["name"])
"#;
    
    execute(code).unwrap();
}

Language Features

Variables

x = 10
name = "Hello"
is_active = True

Arithmetic Operations

a = 10 + 5
b = 10 - 5
c = 10 * 5
d = 10 / 5
e = 10 % 3
f = 2 ** 3  # Power

Control Flow

if x > 10:
    print("Large")
elif x > 5:
    print("Medium")
else:
    print("Small")

while x > 0:
    print(x)
    x = x - 1

for i in range(10):
    print(i)

Functions

def greet(name):
    return "Hello, " + name

message = greet("World")
print(message)

Lists

numbers = [1, 2, 3, 4, 5]
first = numbers[0]
numbers[0] = 10

Dictionaries

person = {"name": "Alice", "age": 30}
name = person["name"]
person["city"] = "New York"

String Operations

greeting = "Hello"
name = "World"
message = greeting + " " + name
repeated = "Hi" * 3  # "HiHiHi"

API

execute(code: &str) -> Result<Value, InterpreterError>

Execute Python-like code and return the result.

Interpreter

Create a new interpreter instance for more control:

use novapoly_interpreter::{Interpreter, parse};
use novapoly_interpreter::lexer::tokenize;

let code = "x = 10";
let tokens = tokenize(code)?;
let ast = parse(&tokens)?;
let mut interpreter = Interpreter::new();
let result = interpreter.eval_program(&ast)?;

Error Handling

The interpreter provides detailed error messages:

  • LexerError: Issues during tokenization
  • ParserError: Syntax errors
  • RuntimeError: Runtime execution errors
  • TypeError: Type mismatch errors

Limitations

  • No classes or objects (attribute access is not fully implemented)
  • No modules or imports
  • No exception handling (try/except)
  • No list/dict comprehensions
  • Limited built-in functions

License

MIT OR Apache-2.0

Commit count: 0

cargo fmt