Skip to content

Commit

Permalink
refactor binding in syntax-rules + unit tests #43
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Nov 8, 2020
1 parent 4b29de9 commit 181c89a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 33 deletions.
37 changes: 19 additions & 18 deletions dist/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Copyright (c) 2014-present, Facebook, Inc.
* released under MIT license
*
* build: Sun, 08 Nov 2020 11:23:14 +0000
* build: Sun, 08 Nov 2020 12:52:24 +0000
*/
(function () {
'use strict';
Expand Down Expand Up @@ -4241,7 +4241,8 @@
symbols: {},
// symbols ellipsis (x ...)
lists: []
}
},
symbols: {}
};
var expansion = scope.expansion,
define = scope.define; // pattern_names parameter is used to distinguish
Expand Down Expand Up @@ -4448,8 +4449,8 @@
bindings['...'].symbols[_name3].push(code);
}

if (!bindings[_name3]) {
bindings[_name3] = code;
if (!bindings.symbols[_name3]) {
bindings.symbols[_name3] = code;
}

return true;
Expand Down Expand Up @@ -4480,14 +4481,14 @@

var _name4 = pattern.cdr.valueOf();

if (!bindings[_name4]) {
bindings[_name4] = nil;
if (!(_name4 in bindings.symbols)) {
bindings.symbols[_name4] = nil;
}

_name4 = pattern.car.valueOf();

if (!bindings[_name4]) {
bindings[_name4] = code.car;
if (!(_name4 in bindings.symbols)) {
bindings.symbols[_name4] = code.car;
}

return true;
Expand Down Expand Up @@ -4622,8 +4623,10 @@
var gensyms = {};

function transform(symbol) {
if (!(symbol instanceof LSymbol || typeof symbol === 'string')) {
throw new Error('syntax: internal error, rename neeed to be symbol');
if (!(symbol instanceof LSymbol || ['string', 'symbol'].includes(_typeof_1(symbol)))) {
//typeof symbol === 'string')) {
var t = type(symbol);
throw new Error("syntax: internal error, need symbol got ".concat(t));
}

var name = symbol.valueOf();
Expand All @@ -4633,10 +4636,10 @@
} // symbols are gensyms from nested syntax-rules


var type = _typeof_1(name);
var n_type = _typeof_1(name);

if (['string', 'symbol'].includes(type) && name in bindings) {
return bindings[name];
if (['string', 'symbol'].includes(n_type) && name in bindings.symbols) {
return bindings.symbols[name];
}

if (symbols.includes(name)) {
Expand Down Expand Up @@ -4882,8 +4885,6 @@
// ellipsis decide it what should be the next value
// there are two cases ((a . b) ...) and (a ...)
new_bind[key] = value;
log('NEXT CALLED');
log(new_bind);
};

var car = transform_ellipsis_expr(new_expr, _bind, {
Expand Down Expand Up @@ -11060,10 +11061,10 @@

var banner = function () {
// Rollup tree-shaking is removing the variable if it's normal string because
// obviously 'Sun, 08 Nov 2020 11:23:14 +0000' == '{{' + 'DATE}}'; can be removed
// obviously 'Sun, 08 Nov 2020 12:52:24 +0000' == '{{' + 'DATE}}'; can be removed
// but disablig Tree-shaking is adding lot of not used code so we use this
// hack instead
var date = LString('Sun, 08 Nov 2020 11:23:14 +0000').valueOf();
var date = LString('Sun, 08 Nov 2020 12:52:24 +0000').valueOf();

var _date = date === '{{' + 'DATE}}' ? new Date() : new Date(date);

Expand Down Expand Up @@ -11100,7 +11101,7 @@
var lips = {
version: 'DEV',
banner: banner,
date: 'Sun, 08 Nov 2020 11:23:14 +0000',
date: 'Sun, 08 Nov 2020 12:52:24 +0000',
exec: exec,
parse: parse,
tokenize: tokenize,
Expand Down
36 changes: 21 additions & 15 deletions src/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -2412,7 +2412,8 @@
'...': {
symbols: { }, // symbols ellipsis (x ...)
lists: [ ]
}
},
symbols: { }
};
const { expansion, define } = scope;
// pattern_names parameter is used to distinguish
Expand Down Expand Up @@ -2589,8 +2590,8 @@
bindings['...'].symbols[name] = bindings['...'].symbols[name] || [];
bindings['...'].symbols[name].push(code);
}
if (!bindings[name]) {
bindings[name] = code;
if (!bindings.symbols[name]) {
bindings.symbols[name] = code;
}
return true;
}
Expand All @@ -2615,12 +2616,12 @@
}
log('>> 12 | 1');
let name = pattern.cdr.valueOf();
if (!bindings[name]) {
bindings[name] = nil;
if (!(name in bindings.symbols)) {
bindings.symbols[name] = nil;
}
name = pattern.car.valueOf();
if (!bindings[name]) {
bindings[name] = code.car;
if (!(name in bindings.symbols)) {
bindings.symbols[name] = code.car;
}
return true;
}
Expand Down Expand Up @@ -2743,18 +2744,25 @@
names,
ellipsis: ellipsis_symbol } = options;
var gensyms = {};
function valid_symbol(symbol) {
if (symbol instanceof LSymbol) {
return true;
}
return ['string', 'symbol'].includes(typeof symbol);
}
function transform(symbol) {
if (!(symbol instanceof LSymbol || typeof symbol === 'string')) {
throw new Error('syntax: internal error, rename neeed to be symbol');
if (!valid_symbol(symbol)) {
const t = type(symbol);
throw new Error(`syntax: internal error, need symbol got ${t}`);
}
const name = symbol.valueOf();
if (name === ellipsis_symbol) {
throw new Error('syntax: internal error, ellipis not transformed');
}
// symbols are gensyms from nested syntax-rules
var type = typeof name;
if (['string', 'symbol'].includes(type) && name in bindings) {
return bindings[name];
var n_type = typeof name;
if (['string', 'symbol'].includes(n_type) && name in bindings.symbols) {
return bindings.symbols[name];
}
if (symbols.includes(name)) {
return LSymbol(name);
Expand Down Expand Up @@ -2940,8 +2948,6 @@
// ellipsis decide it what should be the next value
// there are two cases ((a . b) ...) and (a ...)
new_bind[key] = value;
log('NEXT CALLED');
log(new_bind);
};
const car = transform_ellipsis_expr(
new_expr,
Expand Down Expand Up @@ -3044,7 +3050,7 @@
}
const value = scope.get(expr.car, { throwError: false });
var is_syntax = value instanceof Macro &&
value.__name__ == 'syntax-rules';
value.__name__ === 'syntax-rules';
const head = traverse(expr.car, { disabled });
let rest;
if (is_syntax) {
Expand Down
26 changes: 26 additions & 0 deletions tests/syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,29 @@
(baz bar)))))

(t.is (foo 1 "hello") '("hello"))))

(test "syntax: it should expand nested macro with ellipsis as identifier from parent"
(lambda (t)

(define-syntax foo (syntax-rules (ellipsis)
((_)
(let ()
(define-syntax foo
(syntax-rules ellipsis ()
((_ x ellipsis) (list x ellipsis))))
(foo 1 2 3)))))

(t.is (foo) '(1 2 3))

;; recursive case
(define-syntax foo (syntax-rules (ellipsis)
((_)
(let ()
(define-syntax foo
(syntax-rules ellipsis ()
((_) ())
((_ x) (list x))
((_ x ellipsis) (list (foo x) ellipsis))))
(foo 1 2 3)))))

(t.is (foo) '((1) (2) (3)))))

0 comments on commit 181c89a

Please sign in to comment.