Add proof-of-concept for handling module types in Source Typed #1467
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.
Part of #1399
This PR adds a basic implementation of module type handling in Source Typed, using the
rune
module as an example. Source Typed is now able to properly handle types for runes, as well as throw errors when attempting to import a name that does not exist in the module.Summary of implementation:
runeTypes.ts
). The map also contains an extraprelude
attribute, which contains the types shared by variables and functions in the module (in this case,Rune
andAnimatedRune
). The type information in the map are in the form of Source Typed code strings (constant/function/type alias declarations) which can be evaluated by the Source Typed type checker. To hide the actual implementation of the module, the code in the map are placeholder code that only serves to save the correct types in the type environment when the code is evaluated. The types for the variables/functions are retrieved from the rune module documentation.rune
module, the Source Typed type checker will fetch the prelude code + code for the names imported for that module, and evaluate the code as a program string. Following evaluation, the correct types for the module names will be saved in the type environment for use in the rest of the program.Sample screenshots:
To test this PR, link local js-slang to local frontend and write runes module code in any of the Source Typed chapters. Some unit tests have also been written.
To expand this proof-of-concept to an implementation that is extensible to all modules, the following needs to be done:
runeTypes.ts
) should be shifted to the modules repo. All future module type maps should be written in the modules repo instead and made available for fetching (e.g. as JSON files).TODO: Add logic for fetching module type declarations map from modules repo
code should be replaced with logic that fetches the module maps from the module repo as and when needed.When writing module type maps, note the following:
prelude
attribute that contains code for the types shared by variables and functions in the module. If there are no shared types, an empty string should be provided.NameNotFoundInModule
error will be thrown for names that are not in the module map.Limitations in the current approach:
'Rune'
, type errors will not be thrown if attempting to assign typestring
to the rune. This is an unfortunate result of hiding the module implementation using Source primitive values. One possible solution would be to add special placeholder type for modules, however this will likely require more extensive changes to Source Typed, and is currently out of scope for this PR.