Skip to content

Commit

Permalink
fix ellipsis tail from SRFI-46 #43
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Aug 22, 2020
1 parent b66acbb commit 844723a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![LIPS - Scheme Based Powerful Lisp Language](https://github.com/jcubic/lips/blob/devel/assets/lips.svg?raw=true)

[![npm](https://img.shields.io/badge/npm-1.0.0%E2%80%93beta.3-blue.svg)](https://www.npmjs.com/package/@jcubic/lips)
[![travis](https://travis-ci.org/jcubic/lips.svg?branch=devel&f2a5a5623bf3a3610ae946274aeaa01cfd91754e)](https://travis-ci.org/jcubic/lips)
[![travis](https://travis-ci.org/jcubic/lips.svg?branch=devel&b66acbb057dad33253820b9e649039581579dc31)](https://travis-ci.org/jcubic/lips)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&2c48907438a7265935a7b21e6931008d)](https://coveralls.io/github/jcubic/lips?branch=devel)
[![Join Gitter Chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jcubic/lips)

Expand Down
29 changes: 23 additions & 6 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: Sat, 22 Aug 2020 14:06:34 +0000
* build: Sat, 22 Aug 2020 16:08:48 +0000
*/
(function () {
'use strict';
Expand Down Expand Up @@ -3886,9 +3886,26 @@
}

if (pattern instanceof Pair && pattern.cdr instanceof Pair && LSymbol.is(pattern.cdr.car, ellipsis_symbol)) {
// pattern (... ???)
// pattern (... ???) - SRFI-46
if (pattern.cdr.cdr !== nil) {
throw new Error('syntax: invalid usage of ellipsis');
if (pattern.cdr.cdr instanceof Pair) {
// if we have (x ... a b) we need to remove two from the end
var list_len = pattern.cdr.cdr.length();
var code_len = code.length();
var list = code;

while (code_len - 1 > list_len) {
list = list.cdr;
code_len--;
}

var rest = list.cdr;
list.cdr = nil;

if (!traverse(pattern.cdr.cdr, rest, pattern_names, ellipsis)) {
return false;
}
}
}

if (pattern.car instanceof LSymbol) {
Expand Down Expand Up @@ -10271,10 +10288,10 @@

var banner = function () {
// Rollup tree-shaking is removing the variable if it's normal string because
// obviously 'Sat, 22 Aug 2020 14:06:34 +0000' == '{{' + 'DATE}}'; can be removed
// obviously 'Sat, 22 Aug 2020 16:08:48 +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('Sat, 22 Aug 2020 14:06:34 +0000').valueOf();
var date = LString('Sat, 22 Aug 2020 16:08:48 +0000').valueOf();

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

Expand Down Expand Up @@ -10311,7 +10328,7 @@
var lips = {
version: 'DEV',
banner: banner,
date: 'Sat, 22 Aug 2020 14:06:34 +0000',
date: 'Sat, 22 Aug 2020 16:08:48 +0000',
exec: exec,
parse: parse,
tokenize: tokenize,
Expand Down
4 changes: 2 additions & 2 deletions dist/lips.min.js

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions src/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -2330,9 +2330,23 @@
if (pattern instanceof Pair &&
pattern.cdr instanceof Pair &&
LSymbol.is(pattern.cdr.car, ellipsis_symbol)) {
// pattern (... ???)
// pattern (... ???) - SRFI-46
if (pattern.cdr.cdr !== nil) {
throw new Error('syntax: invalid usage of ellipsis');
if (pattern.cdr.cdr instanceof Pair) {
// if we have (x ... a b) we need to remove two from the end
const list_len = pattern.cdr.cdr.length();
let code_len = code.length();
let list = code;
while (code_len - 1 > list_len) {
list = list.cdr;
code_len--;
}
const rest = list.cdr;
list.cdr = nil;
if (!traverse(pattern.cdr.cdr, rest, pattern_names, ellipsis)) {
return false;
}
}
}
if (pattern.car instanceof LSymbol) {
let name = pattern.car.name;
Expand Down
11 changes: 9 additions & 2 deletions tests/syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@
(f :::)))
(t.is result '((1) 2 (3) (4)))))

(test_ "syntax-rules: elipsis in middle of pattern (srfi-46)"
(test "syntax-rules: tail of ellipsis (srfi-46)"
(lambda (t)

(define result (let-syntax
Expand All @@ -369,8 +369,15 @@
(list ?x (list ?y ...) ?z)))))
(foo 1 2 3 4 5)))

(t.is result '(1 (2 3 4) 5))))
(t.is result '(1 (2 3 4) 5))

(define result (let-syntax
((foo (syntax-rules ()
((foo ?a ?b ... ?c ?d)
(list ?a (list ?b ...) ?c ?d)))))
(foo 1 2 3 4 5)))

(t.is result '(1 (2 3) 4 5))))

(test "syntax-rules: rec macro (srfi-31)"
(lambda (t)
Expand Down

0 comments on commit 844723a

Please sign in to comment.