From ecca7745eb825e4b379f32f6630729ac423f5590 Mon Sep 17 00:00:00 2001 From: Tim Disney Date: Mon, 29 May 2017 22:09:46 -0700 Subject: [PATCH] Fix exporting/importing custom operators --- src/enforester.js | 6 +++++- src/module-visitor.js | 14 ++++++++------ src/sweet-spec-utils.js | 5 ++++- test/unit/test-modules.js | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/enforester.js b/src/enforester.js index d0520a0d..02ea1eca 100644 --- a/src/enforester.js +++ b/src/enforester.js @@ -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({ diff --git a/src/module-visitor.js b/src/module-visitor.js index 7013de7a..cf264447 100644 --- a/src/module-visitor.js +++ b/src/module-visitor.js @@ -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, @@ -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, + }), ); }); }); diff --git a/src/sweet-spec-utils.js b/src/sweet-spec-utils.js index 5cbc3681..b53ae221 100644 --- a/src/sweet-spec-utils.js +++ b/src/sweet-spec-utils.js @@ -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') ); }; diff --git a/test/unit/test-modules.js b/test/unit/test-modules.js index 68853454..aa63ef10 100644 --- a/test/unit/test-modules.js +++ b/test/unit/test-modules.js @@ -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,