A simple Python-like interpreter written in Zig.
int- 64-bit integersfloat- 64-bit floating point numbersstring- UTF-8 stringsbool-true/falsenone- null valuelist- dynamic arrays[1, 2, 3]dict- hash maps{"key": value}
if/elif/elseconditionalswhileloopsfor x in collectionloopsbreak/continuestatements
- Arithmetic:
+,-,*,/,%,**(power) - Comparison:
==,!=,<,>,<=,>= - Logical:
and,or,not - Assignment:
=,+=,-=,*=,/=,%= - Indexing:
list[0],dict["key"]
print(value)- output to consolelen(collection)- get lengthint(value),float(value),str(value),bool(value)- type conversionrange(start, end)- generate number sequenceappend(list, value)- add to listdelete(list, index),delete(dict, key)- remove itemskeys(dict),values(dict)- dict operationstype(value)- get type name
file_read(path),file_write(path, content),file_append(path, content)file_delete(path),file_exists(path)dir_list(path),dir_create(path),dir_exists(path)
- Working directory:
os_getcwd(),os_chdir(path) - File ops:
os_rename(),os_copy(),os_stat(),os_remove() - Directory ops:
os_mkdir(),os_rmdir(),os_walk() - Path ops:
os_path_join(),os_path_exists(),os_path_isdir(), etc. - Environment:
os_getenv(),os_setenv(),os_environ()
- JSON:
json_parse(),json_stringify() - CSV:
csv_parse(),csv_stringify() - YAML:
yaml_parse(),yaml_stringify()
http_get(url),http_post(url, body),http_request(url, options)
Requires Zig 0.16.0 or later.
zig buildzig build run -- examples/hello.zpyzig build run# Variables
name = "ZPy"
count = 10
# Power operator
print(2 ** 10) # 1024
# Lists
numbers = [1, 2, 3, 4, 5]
print("Numbers:", numbers)
# Conditionals
if count > 5:
print("Large")
else:
print("Small")
# Loops
for n in numbers:
print(n * n)
# While with break
i = 0
while true:
i = i + 1
if i > 3:
break
print(i)
# File operations
file_write("test.txt", "Hello, World!")
content = file_read("test.txt")
print(content)
# HTTP request
response = http_get("https://api.github.com")
print(response)ZPy uses a tree-walking interpreter architecture:
- Lexer - Tokenizes source code, handles Python-style indentation (INDENT/DEDENT tokens)
- Parser - Recursive descent parser producing an AST
- Interpreter - Directly evaluates the AST
MIT