Wetpaint Lang

C++ | Cmake
Completed

Introduction

Wetpaint is a hobby programming language designed for educational purposes and to experiment with language design and interpreter implementation. It is a dynamically typed and interpreted language written in C++.
Wetpaint Programming Langauge Example

Features

  • Variables: Supports variable declarations and assignments with various types using the let and const keywords.
  • Arithmetic Operations: Supports arithmetic operations such as addition, subtraction, multiplication, division, and modulus.
  • Strings: Handles string literals and concatenation.
  • Booleans: Supports boolean literals and operations.
  • Objects: Supports object literals and property access.
  • User-Defined Functions: Allows creation of functions using the fn keyword.
  • Member Expressions: Allows accessing properties and methods on objects using dot notation.
  • Conditional Logic: Supports if, elif, and else statements for branching.
  • Looping Constructs: Includes for and while loops for iterative control flow.
  • Scope and Environment: Manages variable scope using an environment stack to support block-scoped variables.

Code Structure

The Wetpaint interpreter is organized into several key components, each responsible for a specific aspect of the language's functionality. This modular approach ensures clarity, maintainability, and scalability of the codebase. Below is a breakdown of the main components and their roles:

Tokens and Lexer

  • Tokens: Tokens are the smallest units of meaning in the source code, representing keywords, identifiers, literals, operators, and other symbols.
  • Lexer: The lexer is responsible for scanning the source code and converting it into a sequence of tokens. It handles lexical analysis by recognizing patterns and generating appropriate tokens.

Abstract Syntax Tree (AST)

  • ASTNode: The base class for all nodes in the abstract syntax tree. It encapsulates a value and provides methods to access and manipulate this value. ASTNodes can store various types of values using std::any.
  • Expression Nodes: Nodes that represent various expressions in the language, such as arithmetic expressions, boolean expressions, and literal values.
  • Statement Nodes: Nodes that represent different types of statements, such as variable declarations, assignments, function declarations, conditional statements, and loops.
  • Runtime Values Nodes that hold evaluated values and provides methods to interact with these values during interpretation.

Parser

  • Parser: The parser takes the sequence of tokens generated by the lexer and constructs an abstract syntax tree. It handles syntactic analysis by ensuring that the source code adheres to the language's grammar and structure.
  • Parse Functions: Functions that parse specific types of expressions and statements, building the corresponding AST nodes.

Runtime

  • Interpreter: The interpreter traverses the abstract syntax tree and executes the program. It evaluates expressions, executes statements, and manages the runtime environment.
  • Environment: The environment manages the scope and storage of variables. It keeps track of variable declarations, assignments, and their values.

Building the project

git clone https://github.com/myka0/wetpaint.git
cd wetpaint
cmake -S . -B build
cmake --build build
The executable will be paint in the build directory.

Running the Interpreter

To run the Wetpaint interpreter, execute the paint binary with your Wetpaint program as an argument:
./build/paint path/to/your/code.wp

Example Programs

Included are some example programs that can be run to demonstrate the capabilities of Wetpaint. The fizzbuzz.wp program solves the classic FizzBuzz problem which asks to iterate 1 to 100 and print:
  • "Fizz" for numbers divisible by 3,
  • "Buzz" for numbers divisible by 5,
  • "FizzBuzz" for numbers divisible by both 3 and 5
  • The number itself for all other cases.
You can run it using:
./build/paint fizzbuzz.wp
Introduction
Features
Code Structure
Tokens and Lexer
Abstract Syntax Tree
Parser
Runtime
Building the project
Running the Interpreter
Example Programs