Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: smtlaissezfaire/loop
base: c3250c870f
...
head fork: smtlaissezfaire/loop
compare: 2d04df3bbd
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on Feb 09, 2013
@smtlaissezfaire improve syntax error messaging c8a5eca
Commits on Feb 10, 2013
@smtlaissezfaire * add (quote) to turn a symbol into a string (useful in macros
* fix a bug where multiple instances of a value inside a macro was only being replaced once
2d04df3
View
15 lib/loop/eval.js
@@ -83,7 +83,20 @@ var loopEval = function(syntaxTree) {
};
}
- throw new Error("unknown error - not sure what to do here. Bailing");
+ var errorMessage = "Parse error near: `" + firstToken.contents + "`";
+ if (firstToken.sourceInfo) {
+ errorMessage += [
+ ' ',
+ '-- line: ',
+ firstToken.sourceInfo.first_line,
+ " -- col: ",
+ firstToken.sourceInfo.first_column,
+ "-",
+ firstToken.sourceInfo.last_column
+ ].join('');
+ }
+
+ throw new Error(errorMessage);
case 'prop-access':
var key = contents.key;
var value = contents.value;
View
20 lib/loop/macro-compiler.js
@@ -361,7 +361,7 @@ extractFormals = function(template, ast) {
return locals;
};
-var replaceFormalsFromList = function(replacement, locals) {
+var replaceFormalsFromList = function(replacement, locals, shouldShift) {
replacement = helpers.deepCopy(replacement);
if (replacement.type === 'list') {
@@ -382,7 +382,17 @@ var replaceFormalsFromList = function(replacement, locals) {
var extraMacroPattern;
_.each(replacement.contents, function(el) {
- var newValue = replaceFormalsFromList(el, locals);
+ var newValue;
+
+ // in a macro pattern we'll have multiple values, so
+ // we'll want to shift an element off the variable list. Otherwise
+ // we don't want to touch the variables, though, as in the
+ // case of a repeated variable
+ if (replacement.hasMacroPattern) {
+ newValue = replaceFormalsFromList(el, locals, true);
+ } else {
+ newValue = replaceFormalsFromList(el, locals);
+ }
if (newValue instanceof Array) {
_.each(newValue, function(el) {
@@ -414,7 +424,11 @@ var replaceFormalsFromList = function(replacement, locals) {
if (replacement.hasMacroPattern) {
return locals[replacement.contents];
} else {
- return locals[replacement.contents].shift();
+ if (shouldShift) {
+ return locals[replacement.contents].shift();
+ } else {
+ return locals[replacement.contents][0];
+ }
}
} else { // we found no actual value for this macro
// if it is a macro pattern like ((a b) ...),
View
7 lib/loop/uglify.js
@@ -174,7 +174,12 @@ var progn = function(tree) {
args = args.contents;
if (f.type === 'id') {
- if (f.contents === 'lambda' || f.contents === 'function') {
+ if (f.contents === 'quote') {
+ if (args.length !== 1) {
+ throw new Error("quote must take one argument");
+ }
+ return ['string', car(args).contents];
+ } else if (f.contents === 'lambda' || f.contents === 'function') {
var formalArgs = _.map(car(args).contents, function(arg) {
return arg.contents;
});
View
41 spec/integration/macro-spec.js
@@ -493,7 +493,44 @@ vows.describe("integration specs (macros)").addBatch({
expected += "})(10)";
assert.equal(loop.compile(code), expected);
- }
- }
+ },
+ },
+ 'quote': {
+ 'it should be able to quote': function() {
+ var code = "(quote Foo)";
+ var expected = "\"Foo\"";
+ assert.equal(loop.compile(code), expected);
+ },
+ 'it should be able to quote inside a define-macro': function() {
+ var code = '';
+ code += '(define-macro';
+ code += ' (my-quote some-var)';
+ code += ' (quote some-var))';
+ code += '(my-quote Foo)';
+ var expected = '"Foo"';
+
+ assert.equal(loop.compile(code), expected);
+ },
+ },
+ 'it should allow substitution in the body of the macro more than once': function() {
+ var code = '';
+ code += '(define-macro ';
+ code += ' (my-square num)';
+ code += ' (mult num num))';
+ code += '(my-square 2)';
+
+ var expected = "mult(2,2)";
+ assert.equal(loop.compile(code), expected);
+ },
+ 'it should allow multiple substiution on multiple levels': function() {
+ var code = '';
+ code += '(define-macro ';
+ code += ' (foo num)';
+ code += ' (mult num (mult num 1)))';
+ code += '(foo 2)';
+
+ var expected = "mult(2,mult(2,1))";
+ assert.equal(loop.compile(code), expected);
+ }
}).export(module);

No commit comments for this range

Something went wrong with that request. Please try again.