GenZ is a simple interpreted programming language designed for educational purposes. It features basic constructs for printing, variable assignment, and conditional statements.
TL;DR The language is called genZ, and draws its grammatical inspiration from Generation Z slang. It uses keywords like BET for printing, CAP for assignment and for conditionals SUS and NOSUS first prompt here. The purpose of GenZ is to provide a minimalistic language that can be used to demonstrate the fundamental concepts of language design, lexical analysis, parsing, and interpretation.
- Interpreted language (tree-walk interpreter)
- Statically typed (variables must be declared with a specific type)
- Strongly typed (no implicit type conversions)
- Defined Grammar (instructions on how to write statements for the programming language)
GenZ enforces a strong typing system to ensure type safety and prevent runtime errors caused by implicit type conversions. Below are the key aspects of the typing system:
-
Static Typing:
- Variables are assigned a type at the time of their first assignment.
- The type of a variable cannot change after it is assigned.
-
Strong Typing:
- Operations between mismatched types (e.g., adding a string to a number) are not allowed.
- Type mismatches result in a parse or runtime error.
-
Supported Types:
- NUMBER: Represents integer values.
- STRING: Represents text enclosed in double quotes (e.g.,
"Hello").
-
Type Enforcement:
- During assignment, the type of the value being assigned is checked against the variable's type.
- Arithmetic operations (
+,-,*,/) are only allowed betweenNUMBERtypes. - Concatenation or other operations on
STRINGtypes must be explicitly defined.
CAP x = 42
CAP y = "Hello"
BET x + 10
BET y + " World"
CAP x = "Oops"
- Lexer (Tokenizer): breaks code in tokens (meaningful pieces)
- Parser: analyze token structure and builds a syntax tree
- Interpreter/Compiler: Interpreter (executes code directly) or a compiler (translates it to bytecode or machine code).
The first step is scanning, also known as lexing, or lexical analysis. A scanner (or lexer) takes in the linear stream of characters and chunks them together into a series of something more akin to “words” (tokens). The next step is parsing. This is where our syntax gets a grammar—the ability to compose larger expressions and statements out of smaller parts.
The BET statement is used to print expressions to the console.
BET "Hello, World!"
The CAP statement is used to assign values to variables.
CAP x = 10
The SUS and NOSUS statements are used for conditional execution. These statements evaluate a condition and execute the corresponding block of code based on whether the condition is true or false.
SUS <condition> {
// Code to execute if the condition is true
} NOSUS {
// Code to execute if the condition is false
}
CAP x = 10
CAP y = 20
SUS x > y {
BET "x is greater than y"
} NOSUS {
BET "x is not greater than y"
}
In this example:
- If
x > yevaluates totrue, the message"x is greater than y"will be printed. - Otherwise, the message
"x is not greater than y"will be printed.
GenZ supports single-line comments. Any text following the # symbol on a line is treated as a comment and ignored during execution.
# This is a comment
CAP x = 42 # Assign 42 to x
BET x # Print the value of x
To run a .genz file, use the GenZ interpreter. Follow these steps:
-
Navigate to the project directory:
cd /Users/dej/Desktop/Git_projects/genz_lang -
Run the interpreter with the
.genzfile as an argument:./interpreter.py examples/var_instantiation.genz
To display the Abstract Syntax Tree (AST) before executing the code, use the --show-ast flag:
./interpreter.py --show-ast examples/var_instantiation.genzThis will output the AST to the console, followed by the execution output.
To ensure the correctness of the implementation, unit tests are provided. Follow these steps to run the tests:
-
Navigate to the project directory:
cd path/to/your/genz_lang -
Run the tests using the
run_tests.pyscript:python run_tests.py
This will execute all test files matching the pattern test_*.py in the project directory.