Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 239 lines (206 sloc) 7.457 kb
6e516fb Bill Burdick making REPL work in browsers
authored
1 if window? and (!global? or global == window)
2 window.global = window
3 window.ReplCore = root = {}
c5d3417 Bill Burdick spelled Leisure properly
authored
4 Leisure = window.Leisure
7b9d7bc Bill Burdick simple pretty printing, list constructors (need work)
authored
5 P = window.Pretty
17facf4 Bill Burdick fixed monads in browser REPL
authored
6 Prim = window.Prim
6e516fb Bill Burdick making REPL work in browsers
authored
7 else root = exports ? this
8
c5d3417 Bill Burdick spelled Leisure properly
authored
9 if !Leisure? and require?
10 Leisure = require('./leisure')
7b9d7bc Bill Burdick simple pretty printing, list constructors (need work)
authored
11 P = require('./pretty')
20fce92 Bill Burdick tweaked monads, added bind function
authored
12 Prim = require('./prim')
72bd694 Bill Burdick Browser version seems to work, now
authored
13 U = require('util')
6e516fb Bill Burdick making REPL work in browsers
authored
14
15 compileFunc = ->
16
17 setCompiler = (c)-> compileFunc = c
18
19 writeFunc = ->
20
21 setWriter = (w)-> writeFunc = w
22
23 nextFunc = ->
24
25 setNext = (n)-> nextFunc = n
26
c90d5ea Bill Burdick added :r command to REPL, removed global arg from define,
authored
27 resetFunc = null
28
29 setResetFunc = (func)-> resetFunc = func
30
c5d3417 Bill Burdick spelled Leisure properly
authored
31 getType = Leisure.getType
72bd694 Bill Burdick Browser version seems to work, now
authored
32
c8f4883 Bill Burdick output in doc
authored
33 handlerFunc = (ast, result, a, c, r, src, env)->
34 env = env ? Prim.defaultEnv
35 if a then env.write "PREPARED: #{Leisure.prepare(src)}"
092dfd0 Bill Burdick errors if you redefine a function, now
authored
36 if ast? and ast.err?
c8f4883 Bill Burdick output in doc
authored
37 env.write("ERROR: #{ast.err}\n")
092dfd0 Bill Burdick errors if you redefine a function, now
authored
38 nextFunc()
d18ae11 Bill Burdick better error handling
authored
39 else
40 if a
c8f4883 Bill Burdick output in doc
authored
41 env.write("PARSED: #{Leisure.astPrint(ast)}\n")
42 env.write("FORMATTED: #{P.print ast}\n")
c8d0629 Bill Burdick cleaned up notebook
authored
43 if c then env.write("GEN: #{ast.src}\n")
d18ae11 Bill Burdick better error handling
authored
44 if r
c8b1ac5 Bill Burdick added `xxx` to quote token functions, better error reporting, fixed REPL...
authored
45 if !result?
c8f4883 Bill Burdick output in doc
authored
46 env.write("(No Result)\n")
092dfd0 Bill Burdick errors if you redefine a function, now
authored
47 nextFunc()
d18ae11 Bill Burdick better error handling
authored
48 else
a031e97 Bill Burdick added $0 to hold the last result in the REPL, made compiler remove old *...
authored
49 global.$0 = result
c8f4883 Bill Burdick output in doc
authored
50 env.write("#{getType result}: #{P.print(result)}\n")
d18ae11 Bill Burdick better error handling
authored
51 processResult result
6e516fb Bill Burdick making REPL work in browsers
authored
52
53 setHandler = (f)-> handlerFunc = f
54
c8d0629 Bill Burdick cleaned up notebook
authored
55 helpFunc = (env)->
56 env.write("""
c5d3417 Bill Burdick spelled Leisure properly
authored
57 Type a Leisure expression or one of these commands and hit enter:
6e516fb Bill Burdick making REPL work in browsers
authored
58
59 :h -- display this help
c90d5ea Bill Burdick added :r command to REPL, removed global arg from define,
authored
60 :c filename -- compile file
c5d3417 Bill Burdick spelled Leisure properly
authored
61 :r -- reset the Leisure environment
6e516fb Bill Burdick making REPL work in browsers
authored
62 :v -- display variable values
63 :v var value -- set a variable
64 :q -- quit
c5d3417 Bill Burdick spelled Leisure properly
authored
65 ! code -- eval JavaScript code in the leisure environment
a31333f Bill Burdick sandbox work
authored
66 !! code -- eval JavaScript code in the host environment
6e516fb Bill Burdick making REPL work in browsers
authored
67
68 """)
69
70 setHelp = (h)-> helpFunc = h
71
72 vars = {
73 c: [false, 'show generated code'],
74 a: [false, 'show parsed AST'],
75 r: [true, 'show evaluation result'],
76 }
77
78 print = (args...)-> writeFunc(U.format.apply(null, args))
79
80 write = (args...)-> writeFunc args.join('')
81
c8f4883 Bill Burdick output in doc
authored
82 processResult = (result, env)->
83 if (getType result) == 'monad' then Prim.runMonad result, (env ? Prim.defaultEnv), -> nextFunc()
963f35b Bill Burdick starting on monads
authored
84 else nextFunc()
85
c8d0629 Bill Burdick cleaned up notebook
authored
86 handleVar = (name, value, env)->
6e516fb Bill Burdick making REPL work in browsers
authored
87 if !name
88 for prop in (k for k of vars).sort()
c8d0629 Bill Burdick cleaned up notebook
authored
89 env.write("#{prop} = #{vars[prop][0]} -- #{vars[prop][1]}\n")
90 else if !value and !vars[name] then env.write("No variable named #{name}\n")
91 else if !value then env.write("#{name} = #{vars[name]} -- #{vars[prop][1]}\n")
e434f17 Bill Burdick removed prim from ASTs, added pretty printing for ASTs
authored
92 else vars[name][0] = !(value[0] in ['f', 'F'])
6e516fb Bill Burdick making REPL work in browsers
authored
93
c5d3417 Bill Burdick spelled Leisure properly
authored
94 # rewrite in Leisure
c8f4883 Bill Burdick output in doc
authored
95 processLine = (line, env)->
1bdca54 Bill Burdick ripped sandbox out from cmdline REPL, for now
authored
96 env = env ? Prim.defaultEnv
6e516fb Bill Burdick making REPL work in browsers
authored
97 try
98 if line
f3cfe1d Bill Burdick ! works in browsers, now (but doesn't inspect, yet), lone exprs in files...
authored
99 if line[0] == '!'
a31333f Bill Burdick sandbox work
authored
100 if line[1] == '!'
101 result = eval(line.substr(2))
102 result = if U? then U.inspect(result) else result
1bdca54 Bill Burdick ripped sandbox out from cmdline REPL, for now
authored
103 env.write("#{result}\n")
a31333f Bill Burdick sandbox work
authored
104 else
c5d3417 Bill Burdick spelled Leisure properly
authored
105 result = Leisure.eval(line.substr(1))
a31333f Bill Burdick sandbox work
authored
106 result = if U? then U.inspect(result) else result
1bdca54 Bill Burdick ripped sandbox out from cmdline REPL, for now
authored
107 env.write("#{result}\n")
c8d0629 Bill Burdick cleaned up notebook
authored
108 else if (m = line.match(/^:v\s*(([^\s]*)\s*([^\s]*)\s*)$/)) then handleVar(m[2], m[3], env)
109 else if (m = line.match(/^:c\s*([^\s]*)$/)) then return compileFunc(m[1], env)
c90d5ea Bill Burdick added :r command to REPL, removed global arg from define,
authored
110 else if (m = line.match(/^:r/)) then resetFunc()
c8d0629 Bill Burdick cleaned up notebook
authored
111 else if (line.trim() == ':h') then helpFunc(env)
112 else if (line.trim() == ':q') then process.exit(0)
113 else
114 [a, c, r] = [vars.a[0], vars.c[0], vars.r[0]]
115 [l, err1] = Leisure.prepare(line)
116 [ast, err] = Leisure.compileNext(l, getGlobals(), false, false)
117 if err1? or err?
118 if ast? then ast.err = err1 ? err
119 else ast = {err: err1 ? err}
120 else [ast, result] = if r then Leisure.evalNext(l) else [ast, null]
121 return handlerFunc(ast, result, a, c, r, line, env)
6e516fb Bill Burdick making REPL work in browsers
authored
122 catch err
c8d0629 Bill Burdick cleaned up notebook
authored
123 env.write(err.stack)
6e516fb Bill Burdick making REPL work in browsers
authored
124 nextFunc()
125
eba0366 Bill Burdick fixed repl to import properly, again,
authored
126 escape = (str)-> str.replace(/\n/g, '\\n')
127
99f8d67 Bill Burdick integrated macro processing into REPL, changed monads to be do-friendly
authored
128 generateCode = (file, contents, loud, handle, nomacros)->
2110e65 Bill Burdick file loading in browser, handle compiled def references (Code.globals)
authored
129 if loud then console.log("Compiling #{file}:\n")
ffe42d4 Bill Burdick made 'require' monad (for both cmdline and browser, but not tested in cm...
authored
130 objName = if file? and file.match /\.lsr$/ then file.substring(0, file.length - 4) else file ? '_anonymous'
963f35b Bill Burdick starting on monads
authored
131 out = """
ffe42d4 Bill Burdick made 'require' monad (for both cmdline and browser, but not tested in cm...
authored
132 var #{objName} = (function(){
59a74da Bill Burdick trying to make sandbox cleaner -- browser still seems to retain defs aft...
authored
133 var root;
134
135 if ((typeof window !== 'undefined' && window !== null) && (!(typeof global !== 'undefined' && global !== null) || global === window)) {
989bc5d Bill Burdick changed *.laz to *.lsr
authored
136 #{if file? then file.replace(/\.lsr/, '') + ' = ' else ''}root = {};
59a74da Bill Burdick trying to make sandbox cleaner -- browser still seems to retain defs aft...
authored
137 global = window;
138 } else {
139 root = typeof exports !== 'undefined' && exports !== null ? exports : this;
c5d3417 Bill Burdick spelled Leisure properly
authored
140 Leisure = require('./leisure');
141 Leisure.req('./std');
963f35b Bill Burdick starting on monads
authored
142 require('./prim');
65508ce Bill Burdick seems to work in sandbox
authored
143 ReplCore = require('./replCore');
963f35b Bill Burdick starting on monads
authored
144 Repl = require('./repl');
145 }
59a74da Bill Burdick trying to make sandbox cleaner -- browser still seems to retain defs aft...
authored
146 root.defs = {};
147 root.tokenDefs = [];
5327c67 Bill Burdick macros seem to work, started work on do macro, improved pretty printing ...
authored
148 root.macros = {};
59a74da Bill Burdick trying to make sandbox cleaner -- browser still seems to retain defs aft...
authored
149
c5d3417 Bill Burdick spelled Leisure properly
authored
150 var setType = Leisure.setType;
151 var setDataType = Leisure.setDataType;
152 var define = Leisure.define;
153 var defineMacro = Leisure.defineMacro;
154 var defineToken = Leisure.defineToken;
59a74da Bill Burdick trying to make sandbox cleaner -- browser still seems to retain defs aft...
authored
155 var processResult = Repl.processResult;
963f35b Bill Burdick starting on monads
authored
156
157 """
fb57e61 Bill Burdick added -v option to runRepl, made runRepl with no -c still give prompt on...
authored
158 [globals, errs] = findDefs contents, nomacros, loud
159 names = globals
160 prev = Leisure.Nil
838ad3b Bill Burdick started on notebook. mark up functions in doc.
authored
161 if err then throw new Error(err)
65508ce Bill Burdick seems to work in sandbox
authored
162 defs = []
c5d3417 Bill Burdick spelled Leisure properly
authored
163 [rest, err] = Leisure.prepare contents
5e98bff Bill Burdick allow python-style indentation
authored
164 if err then throw new Error(err)
48ce68e Bill Burdick fixed compile error for files with only naked exprs
authored
165 varOut = ''
fb57e61 Bill Burdick added -v option to runRepl, made runRepl with no -c still give prompt on...
authored
166 for v, i in globals.reverse().toArray()
48ce68e Bill Burdick fixed compile error for files with only naked exprs
authored
167 if i > 0 then varOut += ","
168 varOut += " #{Leisure.nameSub v}"
169 if varOut then out += "\nvar#{varOut};\n"
c5d3417 Bill Burdick spelled Leisure properly
authored
170 globals = Leisure.append(globals, getGlobals())
5f736b7 Bill Burdick fixed empty code issue in generateCode
authored
171 while rest and rest.trim()
fb57e61 Bill Burdick added -v option to runRepl, made runRepl with no -c still give prompt on...
authored
172 if loud > 1 and prev != names and names != Leisure.Nil then console.log "Compiling function: #{names.head}"
d8acdfe Bill Burdick new code gen works
authored
173 oldRest = rest
c5d3417 Bill Burdick spelled Leisure properly
authored
174 [ast, err, rest] = Leisure.compileNext rest, globals, null, false, nomacros
fb57e61 Bill Burdick added -v option to runRepl, made runRepl with no -c still give prompt on...
authored
175 if ast.leisureName?
176 prev = ast.leisureName
177 names = names.tail
171d5e3 Bill Burdick newlines in front of lambda bodies work
authored
178 code = if rest then oldRest.substring(0, oldRest.length - rest.length) else ''
5e98bff Bill Burdick allow python-style indentation
authored
179 err = err ? ast?.err
180 if err
c5d3417 Bill Burdick spelled Leisure properly
authored
181 errs = "#{errs}#{if ast?.leisureName then "Error in #{ast.leisureName}" else ""}#{err}\n"
5e98bff Bill Burdick allow python-style indentation
authored
182 rest = ''
183 else if ast
d8acdfe Bill Burdick new code gen works
authored
184 globals = ast.globals
c5d3417 Bill Burdick spelled Leisure properly
authored
185 m = code.match(Leisure.linePat)
186 nm = ast.leisureName
59a74da Bill Burdick trying to make sandbox cleaner -- browser still seems to retain defs aft...
authored
187 ast.src = """
5327c67 Bill Burdick macros seem to work, started work on do macro, improved pretty printing ...
authored
188 //#{if nm? then nm + ' = ' else ''}#{escape(P.print(ast))}
c5d3417 Bill Burdick spelled Leisure properly
authored
189 #{if nm? then "root.defs.#{Leisure.nameSub(nm)} = #{Leisure.nameSub(nm)} = " else ""}#{ast.src}
59a74da Bill Burdick trying to make sandbox cleaner -- browser still seems to retain defs aft...
authored
190 """
c5d3417 Bill Burdick spelled Leisure properly
authored
191 src = if ast.leisureName
192 defs.push Leisure.nameSub(ast.leisureName)
65508ce Bill Burdick seems to work in sandbox
authored
193 ast.src
194 else "processResult(#{ast.src})"
d8acdfe Bill Burdick new code gen works
authored
195 out += "#{src};\n"
ac53a01 Bill Burdick load file in browser repl
authored
196 [a, c, r] = [vars.a[0], vars.c[0], vars.r[0]]
197 if handle then handlerFunc ast, null, a, c, r, code
65508ce Bill Burdick seems to work in sandbox
authored
198 out += """
59a74da Bill Burdick trying to make sandbox cleaner -- browser still seems to retain defs aft...
authored
199
56ed337 Bill Burdick fixed code generation
authored
200 if (typeof window !== 'undefined' && window !== null) {
c5d3417 Bill Burdick spelled Leisure properly
authored
201 Leisure.processTokenDefs(root.tokenDefs);
56ed337 Bill Burdick fixed code generation
authored
202 }
59a74da Bill Burdick trying to make sandbox cleaner -- browser still seems to retain defs aft...
authored
203 return root;
204 }).call(this)
65508ce Bill Burdick seems to work in sandbox
authored
205 """
d8acdfe Bill Burdick new code gen works
authored
206 if errs != '' then throw new Error("Errors compiling #{file}: #{errs}")
2110e65 Bill Burdick file loading in browser, handle compiled def references (Code.globals)
authored
207 out
208
c5d3417 Bill Burdick spelled Leisure properly
authored
209 getGlobals = -> Leisure.eval 'leisureGetFuncs()'
092dfd0 Bill Burdick errors if you redefine a function, now
authored
210
fb57e61 Bill Burdick added -v option to runRepl, made runRepl with no -c still give prompt on...
authored
211 findDefs = (contents, nomacros, loud)->
9145a66 Bill Burdick handle forward variable declarations
authored
212 errs = ''
c5d3417 Bill Burdick spelled Leisure properly
authored
213 globals = Leisure.Nil
9145a66 Bill Burdick handle forward variable declarations
authored
214 rest = contents
215 while rest
216 oldRest = rest
c5d3417 Bill Burdick spelled Leisure properly
authored
217 [ast, err, rest] = Leisure.compileNext rest, globals, true, null, nomacros
218 if err then errs = "#{errs}#{if ast.leisureName then "Error in #{ast.leisureName}" else ""}#{err}\n"
219 if ast?.leisureName
fb57e61 Bill Burdick added -v option to runRepl, made runRepl with no -c still give prompt on...
authored
220 if loud > 2 then console.log "Found function: #{ast.leisureName}"
c5d3417 Bill Burdick spelled Leisure properly
authored
221 if globals?.find((v)->v == ast.leisureName) then throw new Error("Attempt to redefine function: #{ast.leisureName}")
222 globals = Leisure.cons(ast.leisureName, globals)
fb57e61 Bill Burdick added -v option to runRepl, made runRepl with no -c still give prompt on...
authored
223 [globals.reverse(), errs]
4973dbf Bill Burdick adding support for forward refs
authored
224
225
6e516fb Bill Burdick making REPL work in browsers
authored
226 root.processLine = processLine
227 root.setCompiler = setCompiler
228 root.setHelp = setHelp
229 root.setWriter = setWriter
230 root.setNext = setNext
231 root.setHandler = setHandler
232 root.next = -> nextFunc()
233 root.help = -> helpFunc()
2110e65 Bill Burdick file loading in browser, handle compiled def references (Code.globals)
authored
234 root.getType = getType
235 root.generateCode = generateCode
3c0df11 Bill Burdick getting ready for separate environments
authored
236 root.processResult = processResult
c90d5ea Bill Burdick added :r command to REPL, removed global arg from define,
authored
237 root.setResetFunc = setResetFunc
838ad3b Bill Burdick started on notebook. mark up functions in doc.
authored
238 root.findDefs = findDefs
Something went wrong with that request. Please try again.