Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement basic reader macros to allow clojurscript like (.filter arr…

…ay pedicure) form #13
  • Loading branch information...
commit 7ea976bd7523bb29fb48d2606ab7605bd272207f 1 parent 16ed991
@Gozala authored
Showing with 14 additions and 3 deletions.
  1. +14 −3 lib/ls.js
View
17 lib/ls.js
@@ -30,6 +30,7 @@ templates["try"] = _.template("(function () {\n<%= indent %><%= indent %>try {\n
templates["if"] = _.template("<%= condition %> ?\n<%= indent %><%= trueexpr %> :\n<%= indent %><%= falseexpr %>");
templates["operator"] = _.template("(<%= loperand %> <%= operator %> <%= roperand %>)");
templates["str"] = _.template("[<%= elems %>].join('')");
+templates[".invoke"] = _.template("(<%= target %>).<%= method %>");
var parse = function(code, filename) {
code = "(" + code + ")";
@@ -231,9 +232,8 @@ var handleExpression = function(expr) {
if (_.isString(command)) {
if (keywords[command])
return keywords[command](expr);
- if (command.charAt(0) === ".") {
- return "(" + (_.isArray(expr[1]) ? handleExpression(expr[1]) : expr[1]) + ")" + command;
- }
+ if (!validName.test(command.charAt(0)))
+ return readers[command.charAt(0)](expr);
}
handleSubExpressions(expr);
var fName = expr[0];
@@ -247,6 +247,7 @@ var handleSubExpressions = function(expr) {
t[i] = _.isArray(value) ? handleExpression(value)
: handleSymbol(value);
});
+ return expr;
};
var macroExpand = function(tree) {
@@ -322,8 +323,18 @@ var handleError = function(no, line, filename) {
return errors[no] + ((line) ? "\nLine no " + line : "") + ((filename) ? "\nFile " + filename : "");
};
+var readers = {};
+readers["."] = function(expression) {
+ var invoker = expression[0];
+ var target = expression[1];
+ var params = expression.slice(2);
+ var owner = _.isArray(target) ? handleExpression(target)
+ : handleSymbol(target);
+ var method = handleExpression([ invoker.substr(1) ].concat(params));
+ return templates[".invoke"]({ target: owner, method: method });
+};
keywords["js"] = function(arr) {
return handleSubExpressions(arr.slice(1)).join(' ');
Please sign in to comment.
Something went wrong with that request. Please try again.