Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some basic fixes that don’t change program output #33

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
28 changes: 21 additions & 7 deletions lib/ls.js
Expand Up @@ -24,7 +24,8 @@ var banner = "// Generated by LispyScript v" + this.version + "\n",
noReturn = /^var\b|^set\b|^throw\b/,
isHomoiconicExpr = /^#args-if\b|^#args-shift\b|^#args-second\b/,
noSemiColon = false;
indent = -4,
indentSize = 4;
indent = -indentSize,
keywords = {},
macros = {},
templates = {};
Expand Down Expand Up @@ -68,16 +69,21 @@ var parse = function(code, filename) {
while (pos < length) {
var c = code.charAt(pos);
pos++;

if (c == "\n") {
lineno++;
if (isComment) isComment = false;
}

if (isComment) continue;

if (isEscape) {
isEscape = false;
token += c;
continue;
}

// strings
if (c == '"') {
isString = !isString;
token += c;
Expand All @@ -101,6 +107,8 @@ var parse = function(code, filename) {
token += c;
continue;
}

// data types
if (c == '[') {
isJSArray++;
token += c;
Expand Down Expand Up @@ -131,10 +139,13 @@ var parse = function(code, filename) {
token += c;
continue;
}

if (c == ";") {
isComment = true;
continue;
}

// regexes
// regex in function position with first char " " is a prob. Use \s instead.
if (c === "/" && !(tree.length === 0 && token.length === 0 && isWhitespace.test(code.charAt(pos)))) {
isRegex = !isRegex;
Expand All @@ -146,6 +157,7 @@ var parse = function(code, filename) {
token += c;
continue;
}

if (c == "(") {
tree.push(parser());
continue;
Expand All @@ -155,10 +167,12 @@ var parse = function(code, filename) {
handleToken();
break;
}

if (isWhitespace.test(c)) {
handleToken();
continue;
}

token += c;
}
if (isString) handleError(3, tree._line, tree._filename);
Expand All @@ -175,7 +189,7 @@ var parse = function(code, filename) {
};

var handleExpressions = function(exprs) {
indent += 4;
indent += indentSize;
var ret = "",
l = exprs.length,
indentstr = " ".repeat(indent);
Expand All @@ -201,7 +215,7 @@ var handleExpressions = function(exprs) {
ret += indentstr + r + tmp + endline;
}
});
indent -= 4;
indent -= indentSize;
return ret;
};

Expand Down Expand Up @@ -367,14 +381,14 @@ keywords["try"] = function(arr) {

keywords["if"] = function(arr) {
if (arr.length < 3 || arr.length > 4) handleError(0, arr._line, arr._filename);
indent += 4;
indent += indentSize;
handleSubExpressions(arr);
var ret = templates["if"]({
condition: arr[1],
trueexpr: arr[2],
falseexpr: arr[3] || "undefined",
indent: " ".repeat(indent)});
indent -= 4;
indent -= indentSize;
return ret;
};

Expand Down Expand Up @@ -404,7 +418,7 @@ var includeFile = (function () {

keywords["include"] = function(arr) {
if (arr.length != 2) handleError(0, arr._line, arr._filename);
indent -= 4;
indent -= indentSize;
var filename = arr[1];
if (typeof filename === "string")
filename = filename.replace(/["']/g, "");
Expand All @@ -418,7 +432,7 @@ keywords["include"] = function(arr) {
}
}
var ret = includeFile(filename);
indent += 4;
indent += indentSize;
return ret;
};

Expand Down
2 changes: 1 addition & 1 deletion src/macros.ls
Expand Up @@ -2,7 +2,7 @@
;; default by the LispyScript compiler.


;;;;;;;;;;;;;;;;;;;; Conditinals ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;; Conditionals ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(macro undefined? (obj)
(= (typeof ~obj) "undefined"))
Expand Down