-
Notifications
You must be signed in to change notification settings - Fork 71
[WIP] Add cast function #1402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[WIP] Add cast function #1402
Conversation
There was a problem hiding this 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 introduces a __cast__() function to support type casting with (type) value syntax in MethodScript. The implementation focuses on non-conversion casts and serves as a foundation for design discussions.
Key changes:
- Adds
__cast__()internal function for type casting operations - Implements compiler rewriting logic to convert
(type) valuesyntax to__cast__(value, type)calls - Updates parenthesis handling to distinguish between casts and regular parenthetical expressions
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
1a571de to
ab92ab8
Compare
Add `__cast__(val, type)` function and syntax. Syntax is `(type) val`.
- Mark `__cast__()` for constant and cached returns. - Remove nested casts where the second executed cast is removed if the first executed cast passing ensures that the second executed cast will pass.
Optimize `assign()` to `__unsafe_assign__ ()` when it is known that the `assign()` typecheck will always pass.
- Do not redefine variables in variable list when not necessary. - Unwrap IVariable values only once. - Directly create new IVariable with correct values when necessary.
ab92ab8 to
ddf74cd
Compare
Fixes compile error in the following example code: ``` @A = (1 + 2) msg(123) ```
0f7800b to
e889605
Compare
Allows for not recreating a new `IVariable` for every assign operation.
Add
__cast__()function with(type) valuesyntax.The main purpose of this PR is to share code and serve as a basis for MethodScript design discussions.
In the current implementation,
__cast__()only performs regular non-conversion casts (i.e. no cross-casts / data conversions). An idea is to use(type) valuesyntax for the non-conversion casts andvalue as typesyntax for cross-casts (including non-conversion casts).TODOs:
Remove (common) double runtime checks. This is mainly about optimizingassign(type, __cast__(val, type)). toassign(type, val). An interesting alternative is to remove the type check fromassign()and instead wrap its value in a__cast__(), which allows for removal of the entire runtime type check if this has been validated in compile-time already. That implementation is not trivial though.EDIT: Implemented by rewriting
assign()to__unsafe_assign__()when its typecheck is known to pass during optimization.Remove nested__cast__()functions that cast to the same type. Also remove nested casts where there is a stronger check followed by a weaker check (the weaker check will always pass if the stronger check will pass).Add compile warning on casting to the exact type that the value already has.Add compile error on casts that can never pass.__cast__()if the value is instanceof the type being cast to (always passing casts). This will be easy for code that optimizes to constants and very hard to impossible for code that is variable (and might get its type from a different file).EDIT: This is implemented for static values (that are optimized in compile time). It likely won't be implemented for dynamic values based on their type information.
assign()optimization that no longer clones ivariables causes pass-by-value variables above a closure to be changed by modifications within the closure. Either a newIVariableinstance needs to be created or environment cloning needs to create this copy to avoid this issue.