Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix exporting/importing custom operators #711

Merged
merged 1 commit into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/enforester.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,11 @@ export class Enforester {
enforestImportSpecifiers() {
let lookahead = this.peek();
let name;
if (this.isIdentifier(lookahead) || this.isKeyword(lookahead)) {
if (
this.isIdentifier(lookahead) ||
this.isKeyword(lookahead) ||
this.isPunctuator(lookahead)
) {
name = this.matchRawSyntax();
if (!this.isIdentifier(this.peek(), 'as')) {
return new T.ImportSpecifier({
Expand Down
14 changes: 8 additions & 6 deletions src/module-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ export default class {
return store;
}

registerSyntaxDeclaration(
term: T.VariableDeclaration,
phase: any,
store: any,
) {
registerSyntaxDeclaration(term: any, phase: any, store: any) {
term.declarators.forEach(decl => {
let val = evalCompiletimeValue(
decl.init,
Expand All @@ -160,9 +156,15 @@ export default class {
});
}
let resolvedName = stx.resolve(phase);
let compiletimeType = term.kind === 'operator' ? 'operator' : 'syntax';
store.set(
resolvedName,
new CompiletimeTransform({ type: 'syntax', f: val }),
new CompiletimeTransform({
type: compiletimeType,
prec: decl.prec == null ? void 0 : decl.prec.val(),
assoc: decl.assoc == null ? void 0 : decl.assoc.val(),
f: val,
}),
);
});
});
Expand Down
5 changes: 4 additions & 1 deletion src/sweet-spec-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export const isSyntaxDeclarationStatement = (term: any) => {
// syntaxrec m = ...
return (
isVariableDeclarationStatement(term) &&
isSyntaxVariableDeclartion(term.declaration)
term.declaration.type === 'VariableDeclaration' &&
(term.declaration.kind === 'syntax' ||
term.declaration.kind === 'syntaxrec' ||
term.declaration.kind === 'operator')
);
};

Expand Down
34 changes: 34 additions & 0 deletions test/unit/test-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,40 @@ output = m`,
1,
);

test(
'should export a simple operator',
evalWithStore,
{
'./m.js': `
'lang sweet.js';
export operator sub left 2 = (left, right) => {
return #\`\${left} - \${right}\`;
}`,

'main.js': `
import { sub } from "./m.js";
output = 2 sub 2`,
},
0,
);

test(
'should export a simple punctuator operator',
evalWithStore,
{
'./m.js': `
'lang sweet.js';
export operator - left 2 = (left, right) => {
return #\`\${left} + \${right}\`;
}`,

'main.js': `
import { - } from "./m.js";
output = 2 - 2`,
},
4,
);

test(
'importing for syntax with a single number exported',
evalWithStore,
Expand Down