Skip to content

Reference

This page is the draft grammar, the built-in functions, the command-line tool, the error categories, and the shape of the reference interpreter.

Grammar

The grammar is Extended Backus–Naur Form. { x } means zero or more, [ x ] means optional, and | separates alternatives. Newlines inside () , [] , and {} are layout, not statement breaks.

program     = { newline } , { statement , newline } ;
statement   = let | const | print | if | while | for | function
            | return | break | continue | import
            | assignment | expression ;
let         = "let" , name , [ "=" , expression ] ;
const       = "const" , name , "=" , expression ;
print       = "print" , [ expression , { "," , expression } ] ;
if          = "if" , expression , newline , block ,
              { "elseif" , expression , newline , block } ,
              [ "else" , newline , block ] , "end" ;
while       = "while" , expression , newline , block , "end" ;
for         = "for" , name , "=" , expression , "to" , expression ,
              [ "step" , expression ] , newline , block , "end"
            | "for" , name , "in" , expression , newline , block , "end" ;
function    = "function" , name , "(" , [ name , { "," , name } ] , ")" ,
              newline , block , "end" ;
return      = "return" , [ expression ] ;
break       = "break" ;
continue    = "continue" ;
import      = "import" , string ;
assignment  = target , assign-op , expression ;
target      = name | expression , "[" , expression , "]" ;
assign-op   = "=" | "+=" | "-=" | "*=" | "/=" | "//=" | "%=" | "**=" ;
block       = { statement , newline } ;

expression  = or ;
or          = and , { "or" , and } ;
and         = not , { "and" , not } ;
not         = { "not" } , comparison ;
comparison  = term , [ compare-op , term ] ;
term        = factor , { ("+" | "-") , factor } ;
factor      = unary , { ("*" | "/" | "//" | "%") , unary } ;
unary       = { ("+" | "-") } , power ;
power       = postfix , [ "**" , unary ] ;
postfix     = primary , { call | index | member } ;
call        = "(" , [ expression , { "," , expression } ] , ")" ;
index       = "[" , expression , "]" ;
member      = "." , name ;
primary     = integer | float | string | "true" | "false" | "null"
            | name | list | dictionary | "(" , expression , ")" ;
list        = "[" , [ expression , { "," , expression } ] , "]" ;
dictionary  = "{" , [ pair , { "," , pair } ] , "}" ;
pair        = expression , ":" , expression ;

function , import , return , break , and continue are also limited by where they appear. A function or import at the top level of a file is valid. return must sit inside a function. break and continue must sit inside a loop. A comparison cannot be followed by another comparison.

Operator priority, from tightest to loosest:

Priority Operators
1 Parentheses, function calls, indexing
2 **
3 Unary + , unary -
4 * , / , // , %
5 + , -
6 == , != , < , > , <= , >=
7 not
8 and
9 or

Built-in Functions

Invalid argument counts are argument errors. Invalid types are type errors unless the table says the failure is a value error.

Type Conversion

Function Behaviour
int(value) Integer, truncated float, whole-number text, or boolean
float(value) Float, integer, numeric text, or boolean
str(value) Text, using the same spelling print uses
bool(value) False for null, zero, and empty text or collections
type(value) The Lupe type name of the value

bool is the explicit conversion. Conditions do not use it for you.

Mathematics

Function Behaviour
abs(value) Absolute value of a number
min(a, b) Smaller of two numbers, or of two strings
max(a, b) Larger of two numbers, or of two strings
round(value) Nearest integer, with halves away from zero
floor(value) Integer at or toward negative infinity
ceil(value) Integer at or toward positive infinity
sqrt(value) Square root of a non-negative number

Strings

Function Behaviour
upper(value) Upper case
lower(value) Lower case
trim(value) Remove whitespace from both ends
split(value, separator) Split on a non-empty separator
join(values, separator) Join a list of strings
replace(value, old, new) Replace every match

Collections

Function Behaviour
len(value) Length of a string, list, or dictionary
append(list, value) Add a value at the end. Returns null
pop(list) Remove and return the last element
keys(dictionary) List of keys, in insertion order
values(dictionary) List of values, in insertion order
contains(collection, value) List membership, dictionary key, or substring
remove(collection, key) Remove a list index or dictionary key

System

Function Behaviour
input(prompt) Write the prompt and read one line
exit(code) Stop the process with an integer status
args() Arguments after the file name, or after --

Command-line Interface

Command Result
lupe run file.lupe Execute a script
lupe file.lupe Execute a script, as a shebang does
lupe check file.lupe Lex and parse, without executing
lupe tokens file.lupe Print one token per line
lupe ast file.lupe Print the syntax tree
lupe repl Start an interactive session
lupe --version Print the package version
lupe --help Print command help
Code Meaning
0 Successful execution
1 Runtime failure
2 Invalid command-line arguments
3 Lexical or syntax failure
4 Source file or import failure

A syntax error inside an imported file is still code 3. A missing file, a circular import, or a module path that cannot be used is code 4. exit(code) uses the integer you pass.

A token line looks like 1:5 IDENTIFIER name . The tree is indented text. Parentheses do not appear in the tree; they only change how expressions nest.

Errors

Category When it is raised
Lexical error The text is not a valid token
Syntax error The tokens do not match the grammar
Name error A name or function is unknown
Type error An operation does not accept these types
Value error The type is right and the value is not
Arithmetic error The mathematics cannot be performed
Index error A list index is outside the list
Key error A dictionary key is missing
Argument error A call has the wrong number of arguments
Import error A module cannot be loaded
Runtime error Any other execution failure

The names printed by the interpreter are LexicalError , SyntaxError , NameError , TypeError , ValueError , ArithmeticError , IndexError , KeyError , ArgumentError , ImportError , and RuntimeError .

TypeError: Cannot add string and integer

  File: example.lupe
  Line: 1
  Column: 24

  let result = "Total: " + 42
                         ^

When the failure happens inside a function, a trace of Lupe calls follows the source line. There is no user-level exception handler in this draft. An unhandled error stops the script. In the REPL it is printed and the session continues.

Interpreter Architecture

The reference interpreter is a tree walk. It never passes Lupe source to Python's eval or exec .

Component Role
Source loading Read UTF-8 and normalise line endings
Lexer Turn characters into tokens and drop comments
Tokens Carry the kind, the spelling, and the source position
Parser Build a syntax tree with recursive descent
Syntax tree Statements and expressions the interpreter can walk
Interpreter Execute the tree
Values Python objects with Lupe type rules, including booleans
Environments Globals for a module, and a local environment per call
Built-ins The fixed function set, protected from redeclaration
Modules Load each resolved file once and keep its own globals
Errors Attach the category, the place, the line, and the call trace
Command line run , check , tokens , ast , and repl
REPL Keep one interpreter alive across submissions

The lexer exists so later stages never re-scan characters. The parser exists so execution is not a second, hidden parse of the text. The tree walk is the execution model a reader can follow statement by statement. Separate environments are how a function can see a global without being able to replace it. Modules are separate interpreters on one shared runtime, so print , args , and the call stack are common while globals are not.

Booleans are not Python bool used as integers. Every arithmetic and comparison path rejects a boolean before it can be treated as 0 or 1 . bool() is the only conversion that makes a boolean from another value.