Repository navigation
VirtLang v2.1.0
We are thrilled to announce the release of VirtLang v2.1.0!
This significant release introduces a foundational Object-Oriented Programming (OOP) system to VirtLang, enabling developers to create more structured, reusable, and encapsulated code. This version builds upon the robust features of v2.0.x while maintaining backward compatibility.
Major Features & Enhancements
Object-Oriented Programming (OOP) System
VirtLang now supports class-based object-oriented programming, empowering you to define custom data types with associated behaviors.
- Class Declarations: Define blueprints for objects using the
classkeyword.class Greeter { // ... members } - Constructors: Initialize new objects with the
constructormethod. Constructors are automatically called when a class is instantiated.class Point { public x public y public constructor(xVal, yVal) { x = xVal y = yVal } } - Instance Properties: Define data members (fields) within a class.
- Public Properties: Accessible from anywhere.
class User { public name } - Private Properties: Accessible only from within the class itself. (Note: Attempting to access a private property from outside the class will currently yield
nil.)class Account { private balance public constructor(initialDeposit) { balance = initialDeposit } }
- Public Properties: Accessible from anywhere.
- Instance Methods: Define functions (behaviors) that operate on an instance of the class.
- Public Methods: Callable from anywhere.
class Counter { public count = 0 public increment() { count = count + 1 } } - Private Methods: Callable only from other methods within the same class.
class Service { private secretKey() { return "supersecret" } public performAction() { let key = secretKey() // ... use key } }
- Public Methods: Callable from anywhere.
- Instantiation: Create new instances (objects) of a class using the class name followed by parentheses (which invoke the constructor).
let myPoint = Point(10, 20) let myCounter = Counter() - Member Access: Access properties and call methods on an instance using dot notation (
.) or computed member access ([]).print(myPoint.x) // Access public property myCounter.increment() // Call public method let propName = "x" print(myPoint[propName]) // Computed access to public property - Implicit
thisContext: Methods automatically operate within the context of the instance they are called on, allowing direct access to the instance's properties and other methods.
Core Interpreter Enhancements
To support the new OOP system, significant updates have been made across the interpreter:
- Lexer & Parser:
- Added new keywords:
class,public,private. - New AST nodes (
ClassNode,ClassMethodNode,ClassPropertyNode) and parsing logic to correctly build the program structure from class definitions.
- Added new keywords:
- Evaluator:
- New evaluation logic for class declarations, effectively creating callable "class values."
- Handling of class instantiation, including constructor invocation and environment setup for instances.
- Resolution of method calls and property access on class instances, respecting public/private visibility.
- Management of separate environments for each class instance to store its state.
- Runtime System:
- Introduced new
RuntimeValuetypes:shared.Classandshared.ClassInstance. - New internal value structures (
values.ClassValue,values.ClassInstanceValue) to represent classes and their instances at runtime.
- Introduced new
Testing
- A comprehensive new test suite (
TestClassesinevaluator_test.go) has been added to thoroughly validate all aspects of the new class system, including:- Basic instantiation and method calls.
- Public and private access control for properties and methods.
- Constructor behavior and argument passing.
- State mutation within instances.
- Methods calling other methods within the same class.
- Computed member access.
- Nested class instantiation and access.
- Recursive method calls.
Known Issues and Limitations
- No Inheritance or
super: The current OOP system does not support class inheritance or asuperkeyword to call parent methods. This is planned for a future update. - No Static Members/Methods: All properties and methods are instance-specific; there is no concept of static members belonging to the class itself.
- Privacy Returns
nil: Attempting to access a private member from outside its class currently results in anilvalue rather than a runtime error. - The
Asyncfield previously onFnDeclarationAST nodes has been removed as async functionality is not yet implemented.
Getting Started
Refer to the README.md and the generated DOCS.md for information on how to use the interpreter or contribute to the project. Examples of the new class syntax will be added to the documentation.
Acknowledgements
A massive thank you to @dev-kas for this very substantial and transformative update to VirtLang!
We are incredibly excited about VirtLang v2.1.0 and the new possibilities that object-oriented programming brings to the language. We look forward to your feedback and seeing what you build!