nano

A minimal interpreted programming language implemented in Python.

Author: retoor retoor@molodetz.nl

Overview

nano is a dynamically typed scripting language with C-like syntax. The interpreter is implemented in approximately 500 lines of Python code. The language supports object-oriented programming with classes, constructors, destructors, and methods.

Usage

python nano.py <filename.nano>

Language Features

Data Types

Type Example
int 42
float 3.14
string "hello" or 'hello'
array {1, 2, 3}
null null
object new ClassName()

Operators

Arithmetic: +, -, *, /, %

Comparison: ==, !=, <, >, <=, >=

Logical: &&, ||

Assignment: =, +=, -=, *=, /=

Increment/Decrement: ++, --

Pointer: & (address-of), * (dereference)

Control Flow

if (condition) {
    // statements
} else {
    // statements
}

while (condition) {
    // statements
}

for (init; condition; update) {
    // statements
}

Classes

class ClassName {
    property = initialValue;

    ClassName(this, param1, param2) {
        this.property = param1;
    }

    ~ClassName(this) {
        // destructor
    }

    method(this, arg) {
        return this.property + arg;
    }
}

obj = new ClassName(value1, value2);
obj.method(arg);

Functions

Methods support default parameters, variable arguments, and keyword arguments:

method(this, required, optional = "default", *args, **kwargs) {
    // body
}

Built-in Functions

Function Description
print() Output values to console
len() Return length of string or array
str() Convert value to string
int() Convert value to integer
bool() Convert value to boolean (0 or 1)
typeof() Return type name as string

String Methods

Method Description
.substr(start, length) Extract substring
.split(delimiter) Split into array
.count(substr) Count occurrences
.indexOf(substr) Find first index
.toUpper() Convert to uppercase
.toLower() Convert to lowercase
.trim() Remove whitespace
.replace(old, new) Replace substring
.length String length property

Array Methods

Method Description
.push(value) Append element
.pop() Remove and return last element
.join(separator) Join elements into string
.indexOf(value) Find index of element
.slice(start, end) Extract subarray
.length Array length property

Comments

// single line comment

/* multi-line
   comment */

Examples

The examples/ directory contains demonstrations of language features:

File Description
arrays.nano Array operations and iteration
classes.nano Class definitions and object creation
control_flow.nano Conditionals and loops
fibonacci.nano Fibonacci sequence calculation
functions.nano Method definitions and recursion
json.nano JSON encoder/decoder implementation
objects.nano Object composition and state management
operators.nano Arithmetic and logical operators
pointers.nano Pointer operations and references
strings.nano String manipulation methods
sudoku.nano Sudoku solver using backtracking
types.nano Type system and coercion

Implementation

The interpreter consists of:

  • Tokenizer: Converts source code into tokens using regular expressions
  • Parser: Constructs syntax structures from token streams
  • Runtime: Executes parsed statements with scope management

Classes are stored as NanoClass objects containing properties, methods, constructor, and destructor definitions. Instances are NanoObject objects with their own property copies.

License

MIT - Like always.