Skip to content

Repository files navigation

NovaScript

NovaScript is a modern scripting language focused on speed and simplicity.

Version: v0.0.2

This repository contains a small reference interpreter for NovaScript.

Features

  • Simple, readable syntax
  • Dynamic typing
  • Functions
  • Variables
  • Arrays
  • Conditionals
  • While loops
  • Built-in functions
  • REPL
  • Clean module layout
  • For-in loops
  • break and continue support
  • Script arguments via global args
  • Maps / dictionaries
  • First-class functions
  • Anonymous function expressions
  • Higher-order functions: map, filter, reduce, each

Example

let name = "Nova";

fn greet(who) {
    return "Hello, " + who + "!";
}

print(greet(name));

Running

From the project root:

python -m novascript examples/hello.nova

Or install in editable mode:

pip install -e .
nova examples/hello.nova

REPL

python -m novascript

Example:

nova> let x = 10;
nova> print(x * 2);
20
nova> exit

Tests

python -m unittest discover -s tests

Syntax Overview

Variables

let x = 10;
let message = "hello";

Functions

fn add(a, b) {
	return a + b;
}

print(add(2, 3));

Conditionals

if x > 5 {
	print("big");
} else {
	print("small");
}

Loops

let i = 0;

while i < 5 {
	print(i);
	i = i + 1;
}

Arrays

let items = [1, 2, 3];
items[0] = 10;

print(items);
print(len(items));

For Loops

for n in [1, 2, 3] {
	print(n);
}

for ch in "Nova" {
	print(ch);
}

for i in range(3) {
	print(i);
}

Break and Continue

let i = 0;

while i < 10 {
    i = i + 1;

    if i == 3 {
        continue;
    }

    if i == 7 {
        break;
    }

    print(i);
}

Assertions

assert(1 == 1);
assert(2 + 2 == 4, "math should work");

Maps

let config = {
	debug: true,
	name: "NovaScript",
	version: 3
};

print(config["name"]);

config["debug"] = false;

for key in config {
	print(key, "=", config[key]);
}

Collection Helpers

let fruits = ["apple", "pear"];

push(fruits, "plum");
print(fruits);

print(pop(fruits));
print(contains(fruits, "pear"));

let user = {
	name: "Nova",
	version: 3
};

print(keys(user));
print(values(user));
print(entries(user));

String Utilities

print(upper("nova"));
print(lower("NOVA"));
print(trim(" padded "));

print(split("one,two,three", ","));
print(join(["one", "two", "three"], " | "));

print(replace("NovaScript", "Script", "Lang"));

print(starts_with("NovaScript", "Nova"));
print(ends_with("NovaScript", "Script"));

Function Expressions

let double = fn(x) {
	return x * 2;
};

print(double(21));

Higher-Order Functions

let nums = [1,2,3,4];

let doubled = map(nums, fn(n) {
	return n * 2;
});

let evens = filter(nums, fn(n) {
	return n * 2;
});

let total = reduce(nums, 0, fn(acc, n) {
	return acc + n;
});

each(nums, fn(n) {
	print(n);
});

Debugging

Print tokens:

python -m novascript --tokens examples/hello.nova

Print the AST:

python -m novascript --ast examples/hello.nova

Print tokens and AST, then still run the program:

python -m novascript --tokens --ast --run examples/hello.nova

Check a file without running it:

python -m novascript --check examples/hello.nova

Script Arguments

Use -- to separate NovaScript CLI options from script arguments.

python -m novascript examples/args.nova -- alpha beta gamma

Inside NovaScript, arguments are available as an array called args:

print(args);

for arg in args {
	print(arg);
}

print("Argument count:", len(args));

Exit Codes

Stop execution normally:

exit();

Stop execution with a failure code:

exit(1);

From a shell:

python -m novascript examples/exit.nova -- --fail echo $?

Built-in Functions

  • print(...)
  • len(value)
  • str(value)
  • num(value)
  • type(value)
  • range(stop)
  • range(start, stop)
  • range(start, stop, step)
  • input(prompt)
  • clock()
  • version()
  • assert(condition)
  • assert(condition, message)
  • exit()
  • exit(code)
  • has(map, key)
  • keys(map)
  • values(map)
  • entries(map)
  • remove(map, key)
  • push(array, value)
  • pop(array)
  • contains(collection, value)
  • upper(string)
  • lower(string)
  • trim(string)
  • split(string, separator)
  • join(array, separator)
  • replace(string, old, new)
  • starts_with(string, prefix)
  • ends_with(string, suffix)
  • map(array, function)
  • filter(array, function)
  • reduce(array, initial, function)
  • each(array, function)

Roadmap

  • Bytecode compiler
  • Faster VM
  • Modules
  • Error messages with source spans
  • Standard library expansion
  • Package manager

pyproject.toml

[build-system]
requires = ["setuptools>=68"]
build-backend = "setuptools.build_meta"

[project]
name = "novascript"
version = "0.0.2"
description = "A modern scripting language focused on speed and simplicity."
readme = "README.md"
requires-python = ">=3.10"
license = { text = "MIT" }
authors = [
    { name = "NovaScript" }
]

[project.scripts]
nova = "novascript.cli:main"

[tool.setuptools.packages.find]
include = ["novascript*"]

About

A modern scripting language focused on speed and simplicity.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages