Created
September 27, 2015 16:54
-
-
Save whacked/8a48d619fb80bc3dc368 to your computer and use it in GitHub Desktop.
reader fails on `(return null)`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(var myState (object inArray false)) | |
(var myStack (array)) | |
(var rxOpenBracket (regex ^\\[)) | |
(var rxCloseBracket (regex \\]$)) | |
(var strTail | |
(function (s) | |
(return ((. s substring) 1)))) | |
(var strAllButLast | |
(function (s) | |
(return ((. s substring) 0 (- (. s length) 1))))) | |
(var transform | |
(function | |
(node) | |
(if (instanceof node Array) | |
;; is array | |
(block | |
(return ((. node map) (function (y) (return (transform y)))))) | |
;; non array | |
(block | |
(if (|| (== (typeof node) "string") | |
(== (typeof node) "number")) | |
(block | |
(if (. myState inArray) | |
(block | |
((. myStack push) node) | |
;; ======================================== | |
;; exception on (return null) | |
;; ======================================== | |
(return "") | |
) | |
(return node))) | |
(block | |
(var s (. node atom)) | |
(if ((. rxOpenBracket test) s) | |
(block | |
;; got open array | |
;; special case | |
(if (== s "[]") | |
(return (array (object atom "array")))) | |
(= (. myState inArray) true) | |
;; start making the array | |
((. myStack push) (object atom "array")) | |
(if (< 1 (. s length)) | |
(block | |
;; this will push from the inner recursion!!! | |
(transform (eval (strTail s))))) | |
;; ======================================== | |
;; exception on (return null) | |
;; ======================================== | |
(return ""))) | |
(if ((. rxCloseBracket test) s) | |
(block | |
(= (. myState inArray) false) | |
;; last atom is more than just a bracket | |
(if (< 1 (. s length)) | |
((. myStack push) (eval (strAllButLast s)))) | |
;; clone buffer + empty the global stack | |
(var rtn ((. myStack slice) 0)) | |
(= myStack (array)) | |
(return (transform rtn)) | |
) | |
) | |
(return (object atom (. node atom)))))) | |
))) | |
(= (. module exports) | |
(function | |
() | |
(var args ((. Array prototype slice call) arguments 0)) | |
(return ((. this multi apply) null ((. args map) transform))))) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export PATH := node_modules/.bin:$(PATH) | |
index2.js: index2.esl | |
eslc < $< > $@ | |
test2.js: test2.esl | |
eslc < $< > $@ | |
test2: index2.js test2.js | |
node test2.js | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(var runTest (function (name func) | |
((require "tape") name | |
(function (t) | |
(func t) | |
((. t end)))))) ; Automatically end tests | |
(var eslc | |
(function (code) | |
(var fullCode ; wrap in a call | |
(+ "(macro xfm (require \"./index2.js\")) (xfm " | |
code | |
")")) | |
(return ((require "eslisp") fullCode)))) | |
(macro test | |
(function (name body) | |
(return `(runTest ,name (function (t) ,@body))))) | |
(test "hello" | |
(((. t equals) (eslc "1") "1;"))) | |
(test "common vector" | |
(((. t equals) (eslc "[1 2 3]") | |
"'';\n'';\n[\n 1,\n 2,\n 3\n];"))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment