Conversation
There was a problem hiding this comment.
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
classsyntax to the lexer/parser/AST and compile class bodies into method chunks. - Introduce new VM
Valuevariants (Class,Instance,InstanceFn) and implement method/property access + calling semantics. - Add an
oop.moduexample 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_globalforCallMethodare 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 likeget_obj().method()ora.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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.