Skip to content

Add OOP#61

Merged
cyteon merged 7 commits intomainfrom
oop
Mar 18, 2026
Merged

Add OOP#61
cyteon merged 7 commits intomainfrom
oop

Conversation

@cyteon
Copy link
Owner

@cyteon cyteon commented Mar 18, 2026

No description provided.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds initial object-oriented programming (OOP) support to the language by introducing class declarations, runtime representations for classes/instances, and VM dispatch for instance property/method access.

Changes:

  • Add class syntax to the lexer/parser/AST and compile class bodies into method chunks.
  • Introduce new VM Value variants (Class, Instance, InstanceFn) and implement method/property access + calling semantics.
  • Add an oop.modu example demonstrating constructors (init), properties, and methods.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
lang/src/vm/vm.rs Implements class construction, instance property access, instance method binding/calling, and a new “write-back self” mechanism.
lang/src/vm/value.rs Adds Class/Instance/InstanceFn runtime value variants plus equality/type/display support.
lang/src/parser.rs Adds class statement parsing and adjusts call expression span handling.
lang/src/lexer.rs Introduces class keyword token.
lang/src/compiler/scope.rs Changes Variable::Global to carry the global name.
lang/src/compiler/compiler.rs Compiles class declarations into Value::Class constants and method function chunks; updates global-variable pattern matches.
lang/src/ast.rs Adds Expr::Class AST node.
lang/examples/oop.modu Provides a usage example for the new OOP feature.
Comments suppressed due to low confidence (1)

lang/src/compiler/compiler.rs:200

  • target_local/target_global for CallMethod are only populated when the method receiver is a plain identifier. With the new instance write-back semantics, mutations inside methods (e.g. self.x = ...) won’t propagate for receivers like get_obj().method() or a.b.method(), because there’s no target to update. If you want mutations to persist for more receiver forms, consider extending the compiler to pass enough information to write back through property/index chains (or adjust the object model to use reference semantics).
                if let Expr::PropertyAccess { object, property: _ } = &callee.node {
                    let (target_local, target_global) = match &object.node {
                        Expr::Identifier(name) => {
                            match self.scope.resolve(name) {
                                Variable::Local(index) => (Some(index), None),
                                Variable::Global(_) => (None, Some(name.to_string())),
                            }
                        }

                        _ => (None, None),
                    };

                    self.emit(Instruction::CallMethod { argc, target_local, target_global, }, span);
                } else {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cyteon cyteon merged commit 2b98a07 into main Mar 18, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants