From 2110e6589cd82f88186ccfcd5f1622d52f7e1d7b Mon Sep 17 00:00:00 2001 From: Bill Burdick Date: Sat, 31 Mar 2012 21:52:35 -0500 Subject: [PATCH] file loading in browser, handle compiled def references (Code.globals) --- browserRepl.cs | 12 +++-- browserRepl.js | 30 ++++++++++--- lazp.cs | 66 +++++++++++++++------------- lazp.js | 83 ++++++++++++++++++++--------------- repl.cs | 17 ++------ repl.js | 27 +++--------- replCore.cs | 22 +++++++++- replCore.js | 30 ++++++++++++- std.js | 116 ++++++++++++++++++++++++++++++++++--------------- std.laz | 2 +- testLazp.cs | 3 +- testLazp.js | 6 ++- 12 files changed, 261 insertions(+), 153 deletions(-) diff --git a/browserRepl.cs b/browserRepl.cs index c54b1509..a2e42e21 100644 --- a/browserRepl.cs +++ b/browserRepl.cs @@ -6,8 +6,10 @@ lastLine = null input = null +write = null init = (inputField, output, defs)-> + write = (line)-> defs.innerHTML += line ReplCore.setWriter (line)-> output.innerHTML += line ReplCore.setNext -> input.value = '' ReplCore.setHandler (ast, result, a, c, r)-> @@ -22,6 +24,7 @@ input.select() markupDef = (line)-> + if line.match /^\s*#/ then line if (match = line.match /^[^=]*(?=\s*=)/) then "#{match[0]}#{line.substring(match[0].length)}" else line @@ -32,10 +35,11 @@ reader = new FileReader() reader.onerror = (evt)-> alert('error' + evt) reader.onload = -> - LC.loadDefs(reader.result) - displayOutput() - displayResults(true) - result.innerHTML = '' + for line in reader.result.split('\n') + if line + write "#{markupDef line}\n" + ast = Lazp.compileLine line + if ast then [ast, result] = Lazp.evalLine line reader.readAsText(files[0]) fileElement.value = null input.select() diff --git a/browserRepl.js b/browserRepl.js index 2a74d229..f09d3c85 100644 --- a/browserRepl.js +++ b/browserRepl.js @@ -1,5 +1,5 @@ (function() { - var Pretty, handleFiles, init, input, lastLine, markupDef, markupLines, root; + var Pretty, handleFiles, init, input, lastLine, markupDef, markupLines, root, write; if ((typeof window !== "undefined" && window !== null) && (!(typeof global !== "undefined" && global !== null) || global === window)) { window.global = window; @@ -13,7 +13,12 @@ input = null; + write = null; + init = function init(inputField, output, defs) { + write = function write(line) { + return defs.innerHTML += line; + }; ReplCore.setWriter(function(line) { return output.innerHTML += line; }); @@ -40,6 +45,7 @@ markupDef = function markupDef(line) { var match; + if (line.match(/^\s*#/)) line; if ((match = line.match(/^[^=]*(?=\s*=)/))) { return "" + match[0] + "" + (line.substring(match[0].length)); } else { @@ -59,10 +65,24 @@ return alert('error' + evt); }; reader.onload = function onload() { - LC.loadDefs(reader.result); - displayOutput(); - displayResults(true); - return result.innerHTML = ''; + var ast, line, result, _i, _len, _ref, _ref2, _results; + _ref = reader.result.split('\n'); + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + line = _ref[_i]; + if (line) { + write("" + (markupDef(line)) + "\n"); + ast = Lazp.compileLine(line); + if (ast) { + _results.push((_ref2 = Lazp.evalLine(line), ast = _ref2[0], result = _ref2[1], _ref2)); + } else { + _results.push(void 0); + } + } else { + _results.push(void 0); + } + } + return _results; }; reader.readAsText(files[0]); fileElement.value = null; diff --git a/lazp.cs b/lazp.cs index 43180b11..692972a8 100644 --- a/lazp.cs +++ b/lazp.cs @@ -218,14 +218,16 @@ class CNil extends Cons cons = (a, b)-> new Cons(a, b) class Code - constructor: (@main, @subfuncs, @fcount, @mcount, @vars, @err)-> + constructor: (@main, @subfuncs, @fcount, @mcount, @vars, @err, @global)-> @main = @main ? '' @subfuncs = @subfuncs ? '' @fcount = @fcount ? 0 @mcount = @mcount ? 0 @vars = @vars ? Nil - copyWith: (main, subfuncs, fcount, mcount, vars, err)->new Code(main ? @main, subfuncs ? @subfuncs, fcount ? @fcount, mcount ? @mcount, vars ? @vars, err ? @err) + @global = @global ? Nil + copyWith: (main, subfuncs, fcount, mcount, vars, err, global)->new Code(main ? @main, subfuncs ? @subfuncs, fcount ? @fcount, mcount ? @mcount, vars ? @vars, err ? @err, global ? @global) addErr: (e)-> @copyWith(null, null, null, null, null, "#{@err}#{e}\n") + setGlobal: (v)-> @copyWith(null, null, null, null, null, null, v) addVar: (v)-> @copyWith(null, null, null, null, cons(v, @vars), null) setVars: (v)-> @copyWith(null, null, null, null, v, null) resetMemo: -> @copyWith(null, null, null, 0) @@ -238,10 +240,10 @@ class Code if !@mcount then @unreffedValue(deref) else @copyWith("(function(){var #{@memoNames()}; return #{@main}})", null, null, 0).reffedValue(deref) -dgen = (ast, lazy, name)-> +dgen = (ast, lazy, name, globals)-> ast.lits = [] res = [] - code = (gen ast, new Code(), ast.lits, (if name? then cons(name, Nil) else Nil), true).memo(!lazy) + code = (gen ast, new Code().setGlobal(cons(name, globals ? Nil)), ast.lits, Nil, true).memo(!lazy) if code.err? then ast.err = code.err else if code.subfuncs.length then ast.src = """ (function(){ @@ -250,6 +252,7 @@ class Code })() """ else ast.src = if name? then "define('#{name}', #{code.main})" else "(#{code.main})" + ast.globals = code.global ast wrap = (ast, src)-> @@ -264,7 +267,7 @@ when _refId if val.lambda then throw new Error("attempt to use lambda as a variable") code = code.copyWith(nameSub val).reffedValue(deref) if vars.find((v)-> v == val) then code.addVar(val) - else if global[nameSub(val)]? then code + else if global[nameSub(val)]? or code.global.find((v)-> v == val) then code else code.addErr "Referenced free variable: #{val}, use lit, instead of ref." when _litId val = getLitVal ast @@ -327,33 +330,31 @@ when _primId getNthBody = (ast, n)-> if n == 1 then ast else getNthBody(getLambdaBody(ast), n - 1) -compileLine = (line)-> +compileLine = (line, globals)-> if line.match commentPat then line = '' - if true - def = line.match linePat - expr = (if def then def[3] else line).trim() - if expr - nm = if def and def[1] then def[1].trim().split(/\s+/) else null - ast = null - if nm - astsByName[nm[0]] = 1 - if def then defineToken(nm[0], def[2]) - ast = parse(prefix(nm, 1, expr, [])) - bod = ast - if nm.length > 1 - bod = getNthBody(ast, nm.length) - addAst(ast) - if getAstType(bod) == _lambdaId - bod.type = nm[0] - ast.dataType = nm[0] - nameAst(nm[0], ast) - dgen(ast, false, nm[0]) - if nm.length == 1 then nameAst(nm[0], ast) - else - ast = parse(expr) - dgen(ast) - ast - else console.log("comment: #{line}") + def = line.match linePat + expr = (if def then def[3] else line).trim() + if expr + nm = if def and def[1] then def[1].trim().split(/\s+/) else null + ast = null + if nm + astsByName[nm[0]] = 1 + if def then defineToken(nm[0], def[2]) + ast = parse(prefix(nm, 1, expr, [])) + bod = ast + if nm.length > 1 + bod = getNthBody(ast, nm.length) + addAst(ast) + if getAstType(bod) == _lambdaId + bod.type = nm[0] + ast.dataType = nm[0] + nameAst(nm[0], ast) + dgen(ast, false, nm[0], globals) + if nm.length == 1 then nameAst(nm[0], ast) + else + ast = parse(expr) + dgen(ast, null, null, globals) + ast evalLine = (line)-> ast = compileLine line @@ -454,3 +455,6 @@ when _primId root.define = define root.getAstType = getAstType root.getType = getType +root.linePat = linePat +root.Nil = Nil +root.cons = cons \ No newline at end of file diff --git a/lazp.js b/lazp.js index 480c8aae..9b1c8a35 100644 --- a/lazp.js +++ b/lazp.js @@ -461,29 +461,35 @@ misrepresented as being the original software. Code = (function() { - function Code(main, subfuncs, fcount, mcount, vars, err) { - var _ref, _ref2, _ref3, _ref4, _ref5; + function Code(main, subfuncs, fcount, mcount, vars, err, global) { + var _ref, _ref2, _ref3, _ref4, _ref5, _ref6; this.main = main; this.subfuncs = subfuncs; this.fcount = fcount; this.mcount = mcount; this.vars = vars; this.err = err; + this.global = global; this.main = (_ref = this.main) != null ? _ref : ''; this.subfuncs = (_ref2 = this.subfuncs) != null ? _ref2 : ''; this.fcount = (_ref3 = this.fcount) != null ? _ref3 : 0; this.mcount = (_ref4 = this.mcount) != null ? _ref4 : 0; this.vars = (_ref5 = this.vars) != null ? _ref5 : Nil; + this.global = (_ref6 = this.global) != null ? _ref6 : Nil; } - Code.prototype.copyWith = function copyWith(main, subfuncs, fcount, mcount, vars, err) { - return new Code(main != null ? main : this.main, subfuncs != null ? subfuncs : this.subfuncs, fcount != null ? fcount : this.fcount, mcount != null ? mcount : this.mcount, vars != null ? vars : this.vars, err != null ? err : this.err); + Code.prototype.copyWith = function copyWith(main, subfuncs, fcount, mcount, vars, err, global) { + return new Code(main != null ? main : this.main, subfuncs != null ? subfuncs : this.subfuncs, fcount != null ? fcount : this.fcount, mcount != null ? mcount : this.mcount, vars != null ? vars : this.vars, err != null ? err : this.err, global != null ? global : this.global); }; Code.prototype.addErr = function addErr(e) { return this.copyWith(null, null, null, null, null, "" + this.err + e + "\n"); }; + Code.prototype.setGlobal = function setGlobal(v) { + return this.copyWith(null, null, null, null, null, null, v); + }; + Code.prototype.addVar = function addVar(v) { return this.copyWith(null, null, null, null, cons(v, this.vars), null); }; @@ -548,11 +554,11 @@ misrepresented as being the original software. })(); - dgen = function dgen(ast, lazy, name) { + dgen = function dgen(ast, lazy, name, globals) { var code, res; ast.lits = []; res = []; - code = (gen(ast, new Code(), ast.lits, (name != null ? cons(name, Nil) : Nil), true)).memo(!lazy); + code = (gen(ast, new Code().setGlobal(cons(name, globals != null ? globals : Nil)), ast.lits, Nil, true)).memo(!lazy); if (code.err != null) { ast.err = code.err; } else if (code.subfuncs.length) { @@ -560,6 +566,7 @@ misrepresented as being the original software. } else { ast.src = name != null ? "define('" + name + "', " + code.main + ")" : "(" + code.main + ")"; } + ast.globals = code.global; return ast; }; @@ -584,7 +591,9 @@ misrepresented as being the original software. return v === val; })) { return code.addVar(val); - } else if (global[nameSub(val)] != null) { + } else if ((global[nameSub(val)] != null) || code.global.find(function(v) { + return v === val; + })) { return code; } else { return code.addErr("Referenced free variable: " + val + ", use lit, instead of ref."); @@ -689,39 +698,35 @@ misrepresented as being the original software. } }; - compileLine = function compileLine(line) { + compileLine = function compileLine(line, globals) { var ast, bod, def, expr, nm; if (line.match(commentPat)) line = ''; - if (true) { - def = line.match(linePat); - expr = (def ? def[3] : line).trim(); - if (expr) { - nm = def && def[1] ? def[1].trim().split(/\s+/) : null; - ast = null; - if (nm) { - astsByName[nm[0]] = 1; - if (def) defineToken(nm[0], def[2]); - ast = parse(prefix(nm, 1, expr, [])); - bod = ast; - if (nm.length > 1) { - bod = getNthBody(ast, nm.length); - addAst(ast); - } - if (getAstType(bod) === _lambdaId) { - bod.type = nm[0]; - ast.dataType = nm[0]; - } - nameAst(nm[0], ast); - dgen(ast, false, nm[0]); - if (nm.length === 1) nameAst(nm[0], ast); - } else { - ast = parse(expr); - dgen(ast); + def = line.match(linePat); + expr = (def ? def[3] : line).trim(); + if (expr) { + nm = def && def[1] ? def[1].trim().split(/\s+/) : null; + ast = null; + if (nm) { + astsByName[nm[0]] = 1; + if (def) defineToken(nm[0], def[2]); + ast = parse(prefix(nm, 1, expr, [])); + bod = ast; + if (nm.length > 1) { + bod = getNthBody(ast, nm.length); + addAst(ast); } - return ast; + if (getAstType(bod) === _lambdaId) { + bod.type = nm[0]; + ast.dataType = nm[0]; + } + nameAst(nm[0], ast); + dgen(ast, false, nm[0], globals); + if (nm.length === 1) nameAst(nm[0], ast); + } else { + ast = parse(expr); + dgen(ast, null, null, globals); } - } else { - return console.log("comment: " + line); + return ast; } }; @@ -879,4 +884,10 @@ misrepresented as being the original software. root.getType = getType; + root.linePat = linePat; + + root.Nil = Nil; + + root.cons = cons; + }).call(this); diff --git a/repl.cs b/repl.cs index b3de485d..ba1bb57a 100644 --- a/repl.cs +++ b/repl.cs @@ -57,26 +57,15 @@ stream = FS.createReadStream(file) stream.on('data', (data)-> contents += data) stream.on('end', ()-> - generateCode(file, contents, !root.quiet) + out = Core.generateCode(file, contents, !root.quiet) + stream = FS.createWriteStream("#{Path.basename file, '.laz'}.js") + stream.end(out, 'utf8') Core.next()) stream.on('error', (ex)-> console.log("Exception reading file: ", ex.stack) Core.next()) -generateCode = (file, contents, loud)-> - if loud then console.log("Compiling #{file}:\n") - out = 'if (typeof require !== "undefined" && require !== null) {Lazp = require("./lazp")};\nsetId = Lazp.setId;\nsetType = Lazp.setType;\nsetDataType = Lazp.setDataType;\ndefine = Lazp.define;\n' - for line, i in contents.split('\n') - if line - ast = L.compileLine line - if ast - ast.src = "//AST: #{L.astPrint(ast)}\n#{ast.src}" - src = if ast.lazpName then ast.src else "console.log(String(#{ast.src}))" - out += "#{src};\n" - stream = FS.createWriteStream("#{Path.basename file, '.laz'}.js") - stream.end(out, 'utf8') - Core.setHelp help Core.setCompiler compile Core.setWriter (str)-> process.stdout.write(str) diff --git a/repl.js b/repl.js index e75a82f5..1d20eb21 100644 --- a/repl.js +++ b/repl.js @@ -1,5 +1,5 @@ (function() { - var Core, FS, L, Path, R, U, compile, generateCode, help, print, repl, root, vars, write, + var Core, FS, L, Path, R, U, compile, help, print, repl, root, vars, write, __slice = Array.prototype.slice; U = require('util'); @@ -78,7 +78,10 @@ return contents += data; }); stream.on('end', function() { - generateCode(file, contents, !root.quiet); + var out; + out = Core.generateCode(file, contents, !root.quiet); + stream = FS.createWriteStream("" + (Path.basename(file, '.laz')) + ".js"); + stream.end(out, 'utf8'); return Core.next(); }); return stream.on('error', function(ex) { @@ -88,26 +91,6 @@ } }; - generateCode = function generateCode(file, contents, loud) { - var ast, i, line, out, src, stream, _len, _ref; - if (loud) console.log("Compiling " + file + ":\n"); - out = 'if (typeof require !== "undefined" && require !== null) {Lazp = require("./lazp")};\nsetId = Lazp.setId;\nsetType = Lazp.setType;\nsetDataType = Lazp.setDataType;\ndefine = Lazp.define;\n'; - _ref = contents.split('\n'); - for (i = 0, _len = _ref.length; i < _len; i++) { - line = _ref[i]; - if (line) { - ast = L.compileLine(line); - if (ast) { - ast.src = "//AST: " + (L.astPrint(ast)) + "\n" + ast.src; - src = ast.lazpName ? ast.src : "console.log(String(" + ast.src + "))"; - out += "" + src + ";\n"; - } - } - } - stream = FS.createWriteStream("" + (Path.basename(file, '.laz')) + ".js"); - return stream.end(out, 'utf8'); - }; - Core.setHelp(help); Core.setCompiler(compile); diff --git a/replCore.cs b/replCore.cs index 8b46db4b..458abf0a 100644 --- a/replCore.cs +++ b/replCore.cs @@ -85,6 +85,25 @@ write(err.stack) nextFunc() +generateCode = (file, contents, loud)-> + if loud then console.log("Compiling #{file}:\n") + out = 'if (typeof require !== "undefined" && require !== null) {Lazp = require("./lazp")};\nsetId = Lazp.setId;\nsetType = Lazp.setType;\nsetDataType = Lazp.setDataType;\ndefine = Lazp.define;\n' + errs = '' + globals = Lazp.Nil + for line, i in contents.split('\n') + if line + ast = Lazp.compileLine line, globals + if ast + globals = ast.globals + if ast.err? then errs = "#{errs}#{ast.err}\n" + m = line.match(Lazp.linePat) + nm = m and m[1].trim().split(/\s+/)[0] + ast.src = "//#{if nm then nm + ' = ' else ''}#{Lazp.astPrint(ast)}\n#{ast.src}" + src = if ast.lazpName then ast.src else "console.log(String(#{ast.src}))" + out += "#{src};\n" + if errs then throw new Error("Errors compiling #{file}: #{errs}") + out + root.processLine = processLine root.setCompiler = setCompiler root.setHelp = setHelp @@ -93,4 +112,5 @@ root.setHandler = setHandler root.next = -> nextFunc() root.help = -> helpFunc() -root.getType = getType \ No newline at end of file +root.getType = getType +root.generateCode = generateCode diff --git a/replCore.js b/replCore.js index 68672d63..f5e6d2ea 100644 --- a/replCore.js +++ b/replCore.js @@ -1,5 +1,5 @@ (function() { - var Lazp, P, U, compileFunc, getType, handleVar, handlerFunc, helpFunc, nextFunc, print, processLine, root, setCompiler, setHandler, setHelp, setNext, setWriter, vars, write, writeFunc, + var Lazp, P, U, compileFunc, generateCode, getType, handleVar, handlerFunc, helpFunc, nextFunc, print, processLine, root, setCompiler, setHandler, setHelp, setNext, setWriter, vars, write, writeFunc, __slice = Array.prototype.slice; if ((typeof window !== "undefined" && window !== null) && (!(typeof global !== "undefined" && global !== null) || global === window)) { @@ -143,6 +143,32 @@ return nextFunc(); }; + generateCode = function generateCode(file, contents, loud) { + var ast, errs, globals, i, line, m, nm, out, src, _len, _ref; + if (loud) console.log("Compiling " + file + ":\n"); + out = 'if (typeof require !== "undefined" && require !== null) {Lazp = require("./lazp")};\nsetId = Lazp.setId;\nsetType = Lazp.setType;\nsetDataType = Lazp.setDataType;\ndefine = Lazp.define;\n'; + errs = ''; + globals = Lazp.Nil; + _ref = contents.split('\n'); + for (i = 0, _len = _ref.length; i < _len; i++) { + line = _ref[i]; + if (line) { + ast = Lazp.compileLine(line, globals); + if (ast) { + globals = ast.globals; + if (ast.err != null) errs = "" + errs + ast.err + "\n"; + m = line.match(Lazp.linePat); + nm = m && m[1].trim().split(/\s+/)[0]; + ast.src = "//" + (nm ? nm + ' = ' : '') + (Lazp.astPrint(ast)) + "\n" + ast.src; + src = ast.lazpName ? ast.src : "console.log(String(" + ast.src + "))"; + out += "" + src + ";\n"; + } + } + } + if (errs) throw new Error("Errors compiling " + file + ": " + errs); + return out; + }; + root.processLine = processLine; root.setCompiler = setCompiler; @@ -165,4 +191,6 @@ root.getType = getType; + root.generateCode = generateCode; + }).call(this); diff --git a/std.js b/std.js index 1cda10eb..dbff49b5 100644 --- a/std.js +++ b/std.js @@ -3,111 +3,155 @@ setId = Lazp.setId; setType = Lazp.setType; setDataType = Lazp.setDataType; define = Lazp.define; -//AST: lambda x . ref x +//id = lambda x . ref x (function(){ var subfunc0 = function(_x){return _x()} return define('id', subfunc0) })(); -//AST: lambda a . lambda b . ref a +//true = lambda a . lambda b . ref a (function(){ var subfunc0 = setType(function(_a){return function(_b){return _a()}}, 'true') return define('true', subfunc0) })(); -//AST: lambda a . lambda b . ref b +//false = lambda a . lambda b . ref b (function(){ var subfunc0 = function(_b){return _b()} var subfunc1 = setType(function(_a){return subfunc0}, 'false') return define('false', subfunc1) })(); -//AST: lambda a . lambda b . lambda f . apply (apply (ref f) (ref a)) (ref b) +//cons = lambda a . lambda b . lambda f . apply (apply (ref f) (ref a)) (ref b) (function(){ var subfunc0 = setDataType(function(_a){return function(_b){return setType(function(_f){return _f()(_a)(_b)}, 'cons')}}, 'cons') return define('cons', subfunc0) })(); -//AST: lambda a . lambda b . ref b +//nil = lambda a . lambda b . ref b (function(){ var subfunc0 = function(_b){return _b()} var subfunc1 = setType(function(_a){return subfunc0}, 'nil') return define('nil', subfunc1) })(); -//AST: apply (apply (apply (apply (apply (lit l1) (lit l2)) (lit .)) (lit l1)) (lambda h . lambda t . lambda D . apply (apply (ref cons) (ref h)) (apply (apply (ref append) (ref t)) (lit l2)))) (lit l2) -undefined; -//AST: lambda item . lambda c . apply (ref c) (lambda rest . apply (apply (ref cons) (ref item)) (ref rest)) -undefined; -//AST: lambda f . lambda item . lambda c . apply (ref c) (lambda rest . apply (ref f) (apply (apply (ref cons) (ref item)) (ref rest))) -undefined; -//AST: lambda f . apply (ref f) (ref nil) -undefined; -//AST: lambda f . lambda rest . lambda g . apply (ref f) (ref rest) +//append = lambda l1 . lambda l2 . apply (apply (ref l1) (lambda h . lambda t . lambda D . apply (apply (ref cons) (ref h)) (apply (apply (ref append) (ref t)) (ref l2)))) (ref l2) +(function(){ + var subfunc0 = function(_l1){return function(_l2){return _l1()((function(){return function(_h){return function(_t){return function(_D){return _cons()(_h)((function(){return _append()(_t)(_l2)}))}}}}))(_l2)}} + + return define('append', subfunc0) +})(); +//[ = lambda item . lambda c . apply (ref c) (lambda rest . apply (apply (ref cons) (ref item)) (ref rest)) +(function(){ + var subfunc0 = setType(function(_item){return function(_c){return _c()((function(){return function(_rest){return _cons()(_item)(_rest)}}))}}, '[') + + return define('[', subfunc0) +})(); +//, = lambda f . lambda item . lambda c . apply (ref c) (lambda rest . apply (ref f) (apply (apply (ref cons) (ref item)) (ref rest))) +(function(){ + var subfunc0 = setType(function(_f){return function(_item){return function(_c){return _c()((function(){return function(_rest){return _f()((function(){return _cons()(_item)(_rest)}))}}))}}}, ',') + + return define(',', subfunc0) +})(); +//] = lambda f . apply (ref f) (ref nil) +(function(){ + var subfunc0 = setType(function(_f){return _f()(_nil)}, ']') + + return define(']', subfunc0) +})(); +//| = lambda f . lambda rest . lambda g . apply (ref f) (ref rest) (function(){ var subfunc0 = setType(function(_f){return function(_rest){return function(_g){return _f()(_rest)}}}, '|') return define('|', subfunc0) })(); -//AST: lambda list . apply (ref append) (ref list) -undefined; -//AST: lambda da . lambda db . lambda list . apply (ref da) (apply (ref db) (ref list)) +//dl = lambda list . apply (ref append) (ref list) +(function(){ + var subfunc0 = function(_list){return _append()(_list)} + + return define('dl', subfunc0) +})(); +//dlAppend = lambda da . lambda db . lambda list . apply (ref da) (apply (ref db) (ref list)) (function(){ var subfunc0 = function(_da){return function(_db){return function(_list){return _da()((function(){return _db()(_list)}))}}} return define('dlAppend', subfunc0) })(); -//AST: lambda dl . apply (ref dl) (ref nil) -undefined; -//AST: lambda cmds . lambda f . apply (ref f) (ref cmds) +//dlList = lambda dl . apply (ref dl) (ref nil) +(function(){ + var subfunc0 = function(_dl){return _dl()(_nil)} + + return define('dlList', subfunc0) +})(); +//ioMonad = lambda cmds . lambda f . apply (ref f) (ref cmds) (function(){ var subfunc0 = setDataType(function(_cmds){return setType(function(_f){return _f()(_cmds)}, 'ioMonad')}, 'ioMonad') return define('ioMonad', subfunc0) })(); -//AST: lambda m . apply (ref m) (lambda cmds . ref cmds) +//getCmds = lambda m . apply (ref m) (lambda cmds . ref cmds) (function(){ var subfunc0 = function(_m){return _m()((function(){return function(_cmds){return _cmds()}}))} return define('getCmds', subfunc0) })(); -//AST: lambda m . apply (ref dlList) (apply (ref getCmds) (ref m)) -undefined; -//AST: lambda x . lambda f . apply (ref f) (ref x) +//getAllCmds = lambda m . apply (ref dlList) (apply (ref getCmds) (ref m)) +(function(){ + var subfunc0 = function(_m){return _dlList()((function(){return _getCmds()(_m)}))} + + return define('getAllCmds', subfunc0) +})(); +//returnCmd = lambda x . lambda f . apply (ref f) (ref x) (function(){ var subfunc0 = function(_x){return function(_f){return _f()(_x)}} return define('returnCmd', subfunc0) })(); -//AST: lambda x . apply (ref ioMonad) (apply (ref dl) (apply (apply (ref [) (apply (ref returnCmd) (ref x))) (ref ]))) -undefined; -//AST: lambda action . lambda f . apply (ref f) (ref action) +//return = lambda x . apply (ref ioMonad) (apply (ref dl) (apply (apply (ref [) (apply (ref returnCmd) (ref x))) (ref ]))) +(function(){ + var subfunc0 = function(_x){return _ioMonad()((function(){return _dl()((function(){return _$r()((function(){return _returnCmd()(_x)}))(_$s)}))}))} + + return define('return', subfunc0) +})(); +//bindCmd = lambda action . lambda f . apply (ref f) (ref action) (function(){ var subfunc0 = function(_action){return function(_f){return _f()(_action)}} return define('bindCmd', subfunc0) })(); -//AST: lambda cmd . apply (ref cmd) (lit ident) +//getBindAction = lambda cmd . apply (ref cmd) (lit ident) (function(){ var subfunc0 = function(_cmd){return _cmd()((function(){return "ident"}))} return define('getBindAction', subfunc0) })(); -//AST: lambda io . lambda f . apply (ref io) (lambda cmds . apply (ref ioMonad) (apply (apply (ref dlAppend) (ref cmds)) (apply (ref dl) (apply (apply (ref [) (apply (ref bindCmd) (ref f))) (ref ]))))) -undefined; -//AST: lambda thing . lambda f . apply (ref f) (ref thing) +//bind = lambda io . lambda f . apply (ref io) (lambda cmds . apply (ref ioMonad) (apply (apply (ref dlAppend) (ref cmds)) (apply (ref dl) (apply (apply (ref [) (apply (ref bindCmd) (ref f))) (ref ]))))) +(function(){ + var subfunc0 = function(_io){return function(_f){return _io()((function(){return function(_cmds){return _ioMonad()((function(){return _dlAppend()(_cmds)((function(){return _dl()((function(){return _$r()((function(){return _bindCmd()(_f)}))(_$s)}))}))}))}}))}} + + return define('bind', subfunc0) +})(); +//alertCmd = lambda thing . lambda f . apply (ref f) (ref thing) (function(){ var subfunc0 = function(_thing){return function(_f){return _f()(_thing)}} return define('alertCmd', subfunc0) })(); -//AST: lambda thing . apply (ref ioMonad) (apply (ref dl) (apply (apply (ref [) (apply (ref alertCmd) (ref thing))) (ref ]))) -undefined; -//AST: lambda pmt . lambda f . apply (ref f) (ref pmt) +//alert = lambda thing . apply (ref ioMonad) (apply (ref dl) (apply (apply (ref [) (apply (ref alertCmd) (ref thing))) (ref ]))) +(function(){ + var subfunc0 = function(_thing){return _ioMonad()((function(){return _dl()((function(){return _$r()((function(){return _alertCmd()(_thing)}))(_$s)}))}))} + + return define('alert', subfunc0) +})(); +//promptCmd = lambda pmt . lambda f . apply (ref f) (ref pmt) (function(){ var subfunc0 = function(_pmt){return function(_f){return _f()(_pmt)}} return define('promptCmd', subfunc0) })(); -//AST: lambda pmt . apply (ref ioMonad) (apply (ref dl) (apply (apply (ref [) (apply (ref promptCmd) (ref pmt))) (ref ]))) -undefined; +//prompt = lambda pmt . apply (ref ioMonad) (apply (ref dl) (apply (apply (ref [) (apply (ref promptCmd) (ref pmt))) (ref ]))) +(function(){ + var subfunc0 = function(_pmt){return _ioMonad()((function(){return _dl()((function(){return _$r()((function(){return _promptCmd()(_pmt)}))(_$s)}))}))} + + return define('prompt', subfunc0) +})(); diff --git a/std.laz b/std.laz index fd1813a3..a31285f4 100644 --- a/std.laz +++ b/std.laz @@ -35,7 +35,7 @@ false = \a b . b # lists cons a b = \f . f a b nil = \a b . b -append = l1 l2 . l1 (\h t D . cons h (append t l2)) l2 +append l1 l2 = l1 (\h t D . cons h (append t l2)) l2 # simple list constructor syntax [ =(]= \item c . c \rest . cons item rest diff --git a/testLazp.cs b/testLazp.cs index 24897396..3c840ba6 100644 --- a/testLazp.cs +++ b/testLazp.cs @@ -58,7 +58,8 @@ run 'test10', -> assertEval("last (cons a nil)", 'a') run 'test11', -> assertEval("last (cons a (cons b nil))", 'b') run 'test12', -> assertEval("(is (cons a b) cons) yes no", 'yes') -run 'test12', -> assertEval("(eval (lambda a (lambda b (ref a)))) yes no", 'yes') +run 'test13', -> assertEval("(eval (lambda a (lambda b (ref a)))) yes no", 'yes') +run 'test14', -> assertEval("(\\1 . 1) hello", 'hello') console.log '\nDone' if !T.stats.failures then console.log "Succeeded all #{T.stats.successes} tests." diff --git a/testLazp.js b/testLazp.js index 320c2d38..d60c01be 100644 --- a/testLazp.js +++ b/testLazp.js @@ -101,10 +101,14 @@ Tests for Lazp return assertEval("(is (cons a b) cons) yes no", 'yes'); }); - run('test12', function() { + run('test13', function() { return assertEval("(eval (lambda a (lambda b (ref a)))) yes no", 'yes'); }); + run('test14', function() { + return assertEval("(\\1 . 1) hello", 'hello'); + }); + console.log('\nDone'); if (!T.stats.failures) {