Skip to content

Commit

Permalink
[ ditch those ugly argument count mismatch errors ]
Browse files Browse the repository at this point in the history
Maybe I'll add them back in if I miss them, but they really don't seem necessary.  High level goal is to stick fairly close with javascript semantics, and javascript doesn't enforce argument counts.

Also, it's really nice to remove so many lines without *really* losing any important functionality or expressiveness.
  • Loading branch information
Jacob Rothstein committed Nov 7, 2010
1 parent ff58c4c commit 627f09a
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 121 deletions.
6 changes: 0 additions & 6 deletions lib/cli.js
Expand Up @@ -5,18 +5,12 @@ var sibilant = require("sibilant"),
script = (process.binding("evals"))["Script"],
context = script.createContext();
var createContext = (function() {
if (arguments.length > 0)
throw new Error("argument count mismatch: expected no arguments");

context.initializedQ = true;;
(module)["filename"] = (process.cwd() + "/exec");;
(context)["module"] = module;;
(context)["require"] = require;;
return (function() {
for (var key in global) (function() {
if (arguments.length > 0)
throw new Error("argument count mismatch: expected no arguments");

return (context)[key] = (global)[key];;
})();
})();;
Expand Down
3 changes: 0 additions & 3 deletions lib/functional.js
Expand Up @@ -74,9 +74,6 @@ var reject = (function(items, fn) {
// items:required fn:required
var args = [ items, fn ];;
return select(items, (function() {
if (arguments.length > 0)
throw new Error("argument count mismatch: expected no arguments");

return (!fn.apply(undefined, args));
}));
});
Expand Down
3 changes: 0 additions & 3 deletions lib/options.js
Expand Up @@ -57,9 +57,6 @@ var extractOptions = (function(config, args) {
});
;
var resetLabel = (function() {
if (arguments.length > 0)
throw new Error("argument count mismatch: expected no arguments");

return currentLabel = defaultLabel;;
});
;
Expand Down
52 changes: 3 additions & 49 deletions lib/sibilant.js
Expand Up @@ -19,18 +19,12 @@ var tokenize = sibilant.tokenize = (function(string) {
});
;
var increaseNesting = (function() {
if (arguments.length > 0)
throw new Error("argument count mismatch: expected no arguments");

var newArr = [ ];;
acceptToken(newArr);
return parseStack.unshift(newArr);
});
;
var decreaseNesting = (function() {
if (arguments.length > 0)
throw new Error("argument count mismatch: expected no arguments");

specials.shift();
parseStack.shift();
return (function() {
Expand Down Expand Up @@ -179,17 +173,13 @@ macros.progn = (function(body) {

macros.call = (function(fnName, args) {
// fnName:required args:rest
if (arguments.length < 1)
throw new Error("argument count mismatch: expected no fewer than 1 arguments");
var args = Array.prototype.slice.call(arguments, 1);

return (translate(fnName) + "(" + (map(args, translate)).join(", ") + ")");
});

macros.defun = (function(fnName, argsAndBody) {
// fnName:required argsAndBody:rest
if (arguments.length < 1)
throw new Error("argument count mismatch: expected no fewer than 1 arguments");
var argsAndBody = Array.prototype.slice.call(arguments, 1);

var fnNameTr = translate(fnName),
Expand All @@ -205,8 +195,6 @@ macros.defun = (function(fnName, argsAndBody) {

macros.defmacro = (function(name, argsAndBody) {
// name:required argsAndBody:rest
if (arguments.length < 1)
throw new Error("argument count mismatch: expected no fewer than 1 arguments");
var argsAndBody = Array.prototype.slice.call(arguments, 1);

var js = macros.lambda.apply(undefined, argsAndBody),
Expand Down Expand Up @@ -282,36 +270,13 @@ var buildArgsString = (function(args, rest) {
};
})();
}));
var argumentCountMismatch = (function(msg) {
// msg:rest
var msg = Array.prototype.slice.call(arguments, 0);

return indent(("throw new Error(\"argument " + "count mismatch: " + (msg).join(" ") + "\");"));
});
;
(function() {
return (function() {
if (typeof(rest) !== 'undefined') {
(function() {
if (((args.length - optionalCount) > 0)) {
return argsString = (argsString + "if (arguments.length < " + (args.length - optionalCount) + ")" + argumentCountMismatch("expected no fewer than", (args.length - optionalCount), "arguments"));;
};
})();
return argsString = (argsString + "var " + translate((rest)[1]) + " = Array.prototype.slice.call(arguments, " + args.length + ");\n");;;
return (argsString + "var " + translate((rest)[1]) + " = Array.prototype.slice.call(arguments, " + args.length + ");\n");
} else {
return (function() {
if (((args).length === 0)) {
return argsString = (argsString + "if (arguments.length > 0)" + argumentCountMismatch("expected no arguments"));;
} else {
return (function() {
if ((0 > optionalCount)) {
return argsString = (argsString + "if (argument.length < " + (args.length - optionalCount) + " || arguments.length > " + args.length + ")" + argumentCountMismatch("expected between", (args.length - optionalCount), "and", args.length, "arguments"));;
};
})();
};
})();
return argsString;
};
})();
return argsString;
});

var buildCommentString = (function(args) {
Expand All @@ -331,8 +296,6 @@ var buildCommentString = (function(args) {
// brain 'splode
macros.lambda = (function(arglist, body) {
// arglist:required body:rest
if (arguments.length < 1)
throw new Error("argument count mismatch: expected no fewer than 1 arguments");
var body = Array.prototype.slice.call(arguments, 1);

var args = transformArgs(arglist),
Expand Down Expand Up @@ -500,26 +463,17 @@ macros.include = (function(file) {
});

sibilant.packageInfo = (function() {
if (arguments.length > 0)
throw new Error("argument count mismatch: expected no arguments");

var fs = require("fs");;
return JSON.parse(fs.readFileSync((__dirname + "/../package.json")));
});

sibilant.versionString = (function() {
if (arguments.length > 0)
throw new Error("argument count mismatch: expected no arguments");

var package = sibilant.packageInfo(),
path = require("path");;
return (package.name + " version " + package.version + "\n(at " + path.join(__dirname, "..") + ")");
});

sibilant.version = (function() {
if (arguments.length > 0)
throw new Error("argument count mismatch: expected no arguments");

return (sibilant.packageInfo())["version"];
});

Expand Down
46 changes: 5 additions & 41 deletions src/sibilant.lisp
Expand Up @@ -186,48 +186,12 @@
";"))))
(incr optional-count)))

(defun argument-count-mismatch (&rest msg)
(indent (concat
"throw new Error(\"argument "
"count mismatch: " (join " " msg) "\");")))

(if (defined? rest)
(progn
(when (> (- args.length optional-count) 0)
(setf args-string
(concat args-string
"if (arguments.length < "
(- args.length optional-count)
")" (argument-count-mismatch
"expected no fewer than"
(- args.length optional-count)
"arguments"))))
(setf args-string
(concat args-string
"var " (translate (second rest))
" = Array.prototype.slice.call(arguments, "
args.length ");\n")))

(if (empty? args)
(setf args-string
(concat args-string
"if (arguments.length > 0)"
(argument-count-mismatch
"expected no arguments")))
(when (> 0 optional-count)
(setf args-string
(concat args-string
"if (argument.length < "
(- args.length optional-count)
" || arguments.length > "
args.length
")" (argument-count-mismatch
"expected between"
(- args.length optional-count)
'and args.length 'arguments))))))

args-string)

(concat args-string
"var " (translate (second rest))
" = Array.prototype.slice.call(arguments, "
args.length ");\n")
args-string))

(defun build-comment-string (args)
(if (empty? args) ""
Expand Down
19 changes: 0 additions & 19 deletions test/test.lisp
Expand Up @@ -116,10 +116,6 @@

(assert-translation "(comment (lambda () hello))"
(concat "// (function() {\n"
"// if (arguments.length > 0)\n"
"// throw new Error(\"argument count mismatch: "
"expected no arguments\");\n"
"// \n"
"// return hello;\n"
"// })"))

Expand All @@ -128,9 +124,6 @@
(assert-translation "(new (prototype a b c))" "(new prototype(a, b, c))")

(assert-translation "(thunk a b c)" "(function() {
if (arguments.length > 0)
throw new Error(\"argument count mismatch: expected no arguments\");
a;
b;
return c;
Expand All @@ -154,9 +147,6 @@
(assert-translation "(each-key key hash a b c)"
"(function() {
for (var key in hash) (function() {
if (arguments.length > 0)
throw new Error(\"argument count mismatch: expected no arguments\");
a;
b;
return c;
Expand All @@ -174,9 +164,6 @@

(assert-translation "(scoped a b c)"
"(function() {
if (arguments.length > 0)
throw new Error(\"argument count mismatch: expected no arguments\");
a;
b;
return c;
Expand Down Expand Up @@ -285,18 +272,12 @@ after-include-2();")

(assert-translation "(thunk (setf b c d e))"
"(function() {
if (arguments.length > 0)
throw new Error(\"argument count mismatch: expected no arguments\");
b = c;
return d = e;;
})")

(assert-translation "(thunk (set b c d e f))"
"(function() {
if (arguments.length > 0)
throw new Error(\"argument count mismatch: expected no arguments\");
(b)[c] = d;
return (b)[e] = f;;
})")
Expand Down

0 comments on commit 627f09a

Please sign in to comment.