1 | <html>
|
2 | <head>
|
3 | <meta charset="utf-8">
|
4 | <title>LispyScript</title>
|
5 | <script src="http://codemirror.net/lib/codemirror.js"></script>
|
6 | <script src="http://codemirror.net/mode/scheme/scheme.js"></script>
|
7 | <script src="http://codemirror.net/mode/javascript/javascript.js"></script>
|
8 | <link rel="stylesheet" href="http://codemirror.net/lib/codemirror.css">
|
9 | <link rel="stylesheet" href="http://codemirror.net/theme/ambiance.css">
|
10 | <link rel="stylesheet" href="http://codemirror.net/doc/docs.css">
|
11 |
|
12 | <script data-main=./lib/browser type=text/javascript src=http://requirejs.org/docs/release/2.0.2/minified/require.js></script>
|
13 | <script>
|
14 | require.config({
|
15 | paths: {
|
16 | "underscore": "https://raw.github.com/amdjs/underscore/9e944c2dd3e1c64227a260d1974c44a29c38e962/underscore-min"
|
17 | }
|
18 | })
|
19 | </script>
|
20 | <script type=application/lispyscript src=./src/macros.ls></script>
|
21 | <style>
|
22 | html, body {
|
23 | font-size: 12px;
|
24 | margin: 0;
|
25 | padding: 0;
|
26 | position: absolute;
|
27 | top: 0;
|
28 | left: 0;
|
29 | width: 100%;
|
30 | min-width: 100%;
|
31 | height: 100%;
|
32 | min-height: 100%;
|
33 | }
|
34 |
|
35 | .CodeMirror {
|
36 | float: left;
|
37 | width: 50%;
|
38 | height: 100%;
|
39 | }
|
40 |
|
41 | .CodeMirror .CodeMirror-scroll {
|
42 | height: 100%;
|
43 | }
|
44 | </style>
|
45 | </head>
|
46 | <body>
|
47 | <textarea id="input" name="input">
|
48 | ;; Hello World! in LispyScript.
|
49 | (console.log "Hello LispyScript!")
|
50 |
|
51 | ;; A more intricate Hello World!
|
52 | (if (undefined? window)
|
53 | (console.log "Hello LispyScript!")
|
54 | (alert "Hello LispyScript!"))
|
55 |
|
56 | ; Functions
|
57 | ;; An anonymous function in LispyScript
|
58 | (function (x) (* x x))
|
59 |
|
60 | ;; The first element in an expression can be an anonymous function.
|
61 | ((function (x) (* x x)) 2)
|
62 |
|
63 | ;; You can set a variable name to a function.
|
64 | (var square
|
65 | (function (x)
|
66 | (* x x)))
|
67 | (console.log (square 10))
|
68 |
|
69 | ; LispyScript is Javascript!
|
70 |
|
71 | (Array.prototype.forEach.call [1, 2, 3]
|
72 | (function (elem index list)
|
73 | (console.log elem)))
|
74 |
|
75 | ;; You can access object methods and properties using the "." notation.
|
76 |
|
77 | (console.log (.greet {greet: "hello"}))
|
78 |
|
79 | ;; You can also use the 'get' expression to access a property of an object.
|
80 |
|
81 | (console.log (get "greet" {greet: "hello"}))
|
82 | (console.log (get 1 [1, 2, 3]))
|
83 |
|
84 | ;; You can 'set' variables too.
|
85 |
|
86 | (set window.onload (function () (alert "Page Loaded")))
|
87 |
|
88 | ; Node
|
89 | ;; The node server example in LispyScript.
|
90 |
|
91 | (var http (require "http"))
|
92 | (var server
|
93 | (http.createServer
|
94 | (function (request response)
|
95 | (response.writeHead 200 {'Content-Type': 'text/plain'})
|
96 | (response.end "Hello World\n"))))
|
97 | (server.listen 1337 "127.0.0.1")
|
98 | (console.log "Server running at http://127.0.0.1:1337/")
|
99 |
|
100 | ; Macros
|
101 |
|
102 | ;; You can define a macro.
|
103 | (macro array? (obj)
|
104 | (= (toString.call ~obj) "[object Array]"))
|
105 |
|
106 | ;; Now let us create a Lisp like 'let' macro in LispyScript.
|
107 |
|
108 | (macro let (names vals rest...)
|
109 | ((function ~names ~rest...) ~@vals))
|
110 |
|
111 | (let (name email tel) ("John" "john@example.org" "555-555-5555")
|
112 | (console.log name)
|
113 | (console.log email)
|
114 | (console.log tel))
|
115 |
|
116 | ;; Conditions
|
117 |
|
118 | (if (= document.readyState "complete")
|
119 | (console.log "loaded") ;; true expression
|
120 | (console.log "loading")) ;; optional false expression
|
121 |
|
122 | ;; Do expression
|
123 |
|
124 | (if (= process.argv.length 2)
|
125 | (do
|
126 | (process.stdin.resume)
|
127 | (process.stdin.setEncoding "utf8")
|
128 | (compile process.stdin process.stdout (process.cwd))))
|
129 |
|
130 | ;; Each macro
|
131 |
|
132 | (each [1, 2, 3]
|
133 | (function (elem index list)
|
134 | (console.log elem)))
|
135 |
|
136 | ;; Exception handling
|
137 |
|
138 | (var fs (require 'fs'))
|
139 | (var outfile "text.txt")
|
140 | (try
|
141 | (fs.writeFileSync outfile "Hello World")
|
142 | (function (e)
|
143 | (console.log (+ "Cannot write file " outfile)
|
144 | (process.exit 1))))
|
145 |
|
146 | </textarea>
|
147 | <textarea id="output" name="output"></textarea>
|
148 | <script>
|
149 | function updatePreview(editor) {
|
150 | output.setValue(require('../lib/ls')._compile(editor.getValue()))
|
151 | }
|
152 | var input = CodeMirror.fromTextArea(document.getElementById("input"), {
|
153 | lineNumbers: true,
|
154 | mode: "scheme",
|
155 | theme: "ambiance",
|
156 | autofocus: true,
|
157 | onChange: updatePreview
|
158 | });
|
159 | var output = CodeMirror.fromTextArea(document.getElementById("output"), {
|
160 | lineNumbers: true,
|
161 | mode: "javascript",
|
162 | theme: "ambiance",
|
163 | readOnly: "nocursor"
|
164 | });
|
165 |
|
166 | setTimeout(updatePreview, 1000, input)
|
167 | </script>
|
168 | </body>
|
169 | </html>
|