Skip to content
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

Modules: Proposal: import syntax for Source §1-§4 #399

Closed
martin-henz opened this issue Feb 20, 2020 · 6 comments
Closed

Modules: Proposal: import syntax for Source §1-§4 #399

martin-henz opened this issue Feb 20, 2020 · 6 comments
Labels
important Fixing this is important, but not mission-critical Proposal Proposing a feature, please discuss

Comments

@martin-henz
Copy link
Member

martin-henz commented Feb 20, 2020

I'm proposing to allow the following syntax in Source:

program         ::= importdirective... statement...
importdirective ::= import { importnames } from string;
importnames     ::= importname (, importname)...
importname      ::= name | name as name

This means that we will explicitly import modules (libraries) from now on, in order to use them:

import { heart as lung, show as view, stack, sail } from 'lib/runes';

view(stack(lung, sail));

This will:

  • demystify module loading for students: Source would be in line with other programming languages where imported functionality is explicitly named in programs
  • simplify the Source Academy user interface: no more need for a "Library" menu
  • simplify js-slang tools: imported names are all explicitly mentioned in the program

For Source 2021, the path will be resolved to:
https://github.com/source-academy/modules/tree/master/
This means that from 'lib/runes' refers to
https://github.com/source-academy/modules/tree/master/lib/runes.js (the default file extension will be .js)

The Source Academy frontend will fetch the module from the backend, which of course will cache it. Our modules can still be written in JavaScript and loaded in the frontend in whatever way works (e.g. eval).

In the future, we can allow for a more flexible scheme where users can import Source programs using the same syntax.

@martin-henz martin-henz added important Fixing this is important, but not mission-critical Proposal Proposing a feature, please discuss labels Feb 20, 2020
@martin-henz martin-henz changed the title Proposal: include import syntax in Source §1-§4 Proposal: import syntax for Source §1-§4 Feb 20, 2020
@openorclose
Copy link
Contributor

Yes, I think that's a good suggestion.

Ideally we would also have some sort of autoimport feature built into Ace (since it's really troublesome to have to keep adding library functions to the import statment in the midst of solving a mission).

Otherwise I can foresee this "header file" that includes all library functions being prepended to Source programs which would defeat the purpose of this.

Haven't gave it too much thought on the implementation side.

@martin-henz
Copy link
Member Author

For solving missions, our template would already provide a sufficient import directive, and the students would be advised not to change it.

@btzy
Copy link
Contributor

btzy commented May 31, 2020

program ::= importdirective... statement

Shouldn't a program be allowed to contain more than one statement?

@martin-henz
Copy link
Member Author

martin-henz commented May 31, 2020

You are right. It should be statement...

(corrected above)

@btzy
Copy link
Contributor

btzy commented May 31, 2020

I would recommend allowing aliased imports for ease of implementation. I think it is impossible (without hacks) to differentiate between import {foo} from "file"; and import {foo as foo} from "file"; in an ESTree.

@martin-henz
Copy link
Member Author

I would recommend allowing aliased imports for ease of implementation. I think it is impossible (without hacks) to differentiate between import {foo} from "file"; and import {foo as foo} from "file"; in an ESTree.

I agree. I've changed the proposal above accordingly.

@martin-henz martin-henz changed the title Proposal: import syntax for Source §1-§4 Modules: Proposal: import syntax for Source §1-§4 Oct 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
important Fixing this is important, but not mission-critical Proposal Proposing a feature, please discuss
Projects
None yet
Development

No branches or pull requests

3 participants