Lambda Calculus
Check-in [31d1efcbb4]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:testing parser, code generator, AST, and eval (testLazp.js)
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:31d1efcbb4282a4dd3adc8a4734f53b781b55f0f
User & Date: bill 2012-03-22 03:31:04
Context
2012-03-22
03:40
put wimpy testing framework into testing.js check-in: 1072b4863a user: bill tags: trunk
03:31
testing parser, code generator, AST, and eval (testLazp.js) check-in: 31d1efcbb4 user: bill tags: trunk
2012-03-20
23:25
renamed hl.js to lazp.js check-in: 20050d99dd user: bill tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to TODO.

1
2


3
4
5
6
TODO
* allow args in definitions: func arg1 arg2 = body => func = \arg1 arg2 . body


* debug VM

DONE
* print VM contexts


>
>




1
2
3
4
5
6
7
8
TODO
* allow args in definitions: func arg1 arg2 = body => func = \arg1 arg2 . body
* allow implicit recursion (puts rec in front of definition for you)
* a-b recursion?
* debug VM

DONE
* print VM contexts

Changes to lazp.js.

29
30
31
32
33
34
35




36
37
38
39
40
41
42
...
154
155
156
157
158
159
160

161
162
163
164
165
166
167
168
...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
...
196
197
198
199
200
201
202
203




























































204



205
206
207
208
209
210
211
...
217
218
219
220
221
222
223


224
225
226
227
228
229
230
231
232
233



234
235









236
237
238
239
240
241
242


243
244
245
246
247
248
249
...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
...
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
...
413
414
415
416
417
418
419







420
421
422
423
424
425
426
...
639
640
641
642
643
644
645

646
647

648
649
650

651
652
653
654
655
656
657
658
659
660
...
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
...
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
...
755
756
757
758
759
760
761

762
763
764
765
*/

(function(){
    var LC=require('./lc.js')
    var VM=require('./lcvm.js')

    exports.hereDoc = hereDoc





    function hereDoc(f) {
	return f.toString().
	    replace(/^[^\/]+\/\*!?/, '').
	    replace(/\*\/[^\/]+$/, '');
    }

................................................................................
	    res.push('name ', getNameNm(ast), ' (')
	    astPrint(getNameAst(ast), res)
	    res.push(')')
	    break
	case _refId:
	    res.push('ref ')
	    var val = getRefVar(ast)

	    res.push(val.lambda ? "WHA?" : val)
	    break
	case _litId:
	    res.push('lit ')
	    var val = getLitVal(ast)

	    res.push(val.lambda ? "{" + val.lambda.toString() + "}" : val)
	    break
................................................................................
	    break
	case _primId:
	    res.push('prim ')
	    astPrint(getPrimArg(ast), res)
	    astPrint(getPrimRest(ast), res)
	    break
	default:
	    res.push("WHA???")
	    break
	}
	return isFirst && res.join('')
    }

    /**
       A context is program scope
................................................................................
     */
    function createContext() {
	var ctx = (function(){
	    var tokens = {}
	    var groupOpens = {'(': ')'}
	    var groupCloses = {')': 1}
	    var C





























































	    function id(func, id) {



		func.context = C
		func.id = id
		return func
	    }

	    function nameAst(nm, ast) {
		if (!ast.name) {
................................................................................
		if (!ast.id) {
		    C.astsById.push(ast)
		    ast.id = C.astsById.length
		}
		return ast
	    }



	    return C = {
		tokens: tokens,
		groupOpens: groupOpens,
		groupCloses: groupCloses,
		astsById: [],
		astsByName: {},
		addAst: addAst,
		id: id,
		nameAst: nameAst,
		eval: function(str){return eval(str)},



		subcontext: function(){return function(str){return eval(str)}},
	    }









	})()

	ctx.funcId = 0
	return ctx
    }

    var CTX = createContext()



    function Memo() {
	this.count = 0
    }
    Memo.prototype = {
	add: function() {return "memo" + this.count++},
	toString: function() {
................................................................................
	var isFirst = !ctx

	res = res || []
	ctx = ctx || CTX.subcontext()
	ctx.curLit = 0
	if (isFirst) ctx.gen = 0
	CTX.addAst(ast)
	switch (ast.lambda && ast.lambda.id) {
	case _nameId:
	    var nm = getNameNm(ast)
	    var a = getNameAst(ast)

	    CTX.nameAst(nm, a)
	    gen(a, res, ctx, vars, memo)
	    break
	case _refId:
	    var val = getRefVar(ast)

	    if (val.lambda) throw new Error("attempt to use lambda as a variable")
	    if (!vars.contains(val)) throw new Error("unbound variable -- use lit instead")
	    res.push(nameSub(val))
	    if (deref) res.push("()")
	    break
	case _litId:
	    var val = getLitVal(ast)
	    var lit

................................................................................
		    gen(rest, res, ctx, vars, memo, true, true, false)
		}
		res.push(")")
		memoEnd(res, memo, deref)
	    }
	    break
	default:
	    res.push("'WHA???'")
	    break
	}
	ast.src = res.join('')
	return isFirst && [ast.src, ctx]
    }

    function run(cmp, arg) {
................................................................................
    function wlit(value) {return lit(laz(value))}
    function wref(variable) {return ref(laz(variable))}
    function wlambda(v, func) {return lambda(laz(v))(laz(func))}
    function wapply(func, arg) {return apply(laz(func))(laz(arg))}
    function wprim(arg, rest) {return prim(laz(arg))(laz(rest))}
    function wname(nm, ast) {return name(laz(nm))(laz(ast))}
    // base functions







    function lit(_x) {
	return id(function(_f) {
	    return _f()(_x)
	}, _litId)
    }
    function ref(_x) {
	return id(function(_f) {
................................................................................
	return expr
    }
    
    function tparseVariable(tok, vars, oldVars) {
	if (vars[tok] || CTX.astsByName[tok]) {
	    vars[tok] = tok
	    cur = wref(tok)

	} else {
	    cur = wlit(tok)

	}
	return cur
    }

    function tparseLambda(toks, vars) {
	var name, old, body
	
	if (toks.length < 3 || toks[toks.length - 1] == '.') {
	    throw new Error('imcomplete lambda definition: ' + toks.reverse().join(' '))
	}
	old = vars[name]
	if (toks[toks.length - 2] == '.') {
	    name = toks.pop()
	    vars[name] = name
................................................................................
    console.log("t2 lambda: " + LC.L._t2().lambda)
    console.log("t2 id: " + LC.L._t2().lambda.id)
    console.log("t2 type: " + astType(LC.L._t2()))
    console.log("t2 var: " + LC.L._t2()(first))
    console.log("t2 body: " + LC.L._t2()(second).lambda)
    console.log("t2: " + astPrint(LC.L._t2()))
    console.log("t3: " + astPrint(LC.L._t3()))
*/
    console.log("t4: " + astPrint(LC.L._t4()))
/*
    console.log("t5: " + astPrint(LC.L._t5()))
    console.log("t6: " + astPrint(LC.L._t6()))
    console.log("gen t1: " + dgen(LC.L._t1())[0])
    console.log("run t1: " + run(dgen(LC.L._t1()), null))
    console.log("gen t2: " + dgen(LC.L._t2())[0])
    console.log("run t2: " + run(dgen(LC.L._t2()), null))
    console.log("gen t3: " + dgen(LC.L._t3())[0])
    console.log("run t3 'a' 'b': " + run(dgen(LC.L._t3()), null))
    console.log("t3 'a' 'b': " + run(dgen(LC.L._t3()), laz("a")))
*/
    console.log("gen t4: " + dgen(LC.L._t4())[0])
    console.log("run t4 'a': " + run(dgen(LC.L._t4()), laz('a')))
/*
    console.log("gen t5: " + dgen(LC.L._t5())[0])
    console.log("run t5 'a' 'b': " + run(dgen(LC.L._t5()), 'a')('b'))
    console.log("gen t6: " + dgen(LC.L._t6())[0])
    console.log("run t6 'a' 'b': " + run(dgen(LC.L._t6()), 'a')('b'))
    console.log("_refId: " + _refId)
    console.log("_litId: " + _litId)
    console.log("_lambdaId: " + _lambdaId)
................................................................................
    console.log("tlambda: " + LC.L.__lambda())
    console.log("tapply: " + LC.L.__apply())
    console.log("tprim: " + LC.L.__apply())
    console.log("tname: " + LC.L.__name())
    console.log("REF ID: " + (ref("duh")()).id)
    console.log("REF TEST: " + astPrint(ref("duh")()))
    console.log("REF GEN: " + dgen(ref("duh")())[0])
*/

    var t4 = wlambda('x', wref('x'))
    console.log("T4 AST: " + astPrint(t4))
    console.log("T4 gen: " + dgen(t4)[0])
    console.log("T4 src: " + t4)

    console.log("T7: " + astPrint(LC.L._t7()))
................................................................................
    console.log("T7 3: " + eval(dgen(t7)[0])(laz(function(x){log("x: " + x()); return String(x())+"1"}))(laz(function(y){log("y: " + y()); return y()+"2"}))(laz("FLOOP ")))

    console.log("T77: " + LC.L._t77)

    console.log("LIT: " + dgen(wlit('x'), true)[0])

    console.log("\\x.x x x: " + astPrint(parse("\\x.x x y")))


// TODO: compile AST funcs directly and include the JS source here
// for self hosting
})()







>
>
>
>







 







>
|







 







|







 








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>







 







>
>
|








|
>
>
>


>
>
>
>
>
>
>
>
>







>
>







 







|











|







 







|







 







>
>
>
>
>
>
>







 







>


>



>


|







 







<

<









<


<







 







<







 







>




29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
...
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
...
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
...
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
...
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
...
772
773
774
775
776
777
778

779

780
781
782
783
784
785
786
787
788

789
790

791
792
793
794
795
796
797
...
821
822
823
824
825
826
827

828
829
830
831
832
833
834
...
844
845
846
847
848
849
850
851
852
853
854
855
*/

(function(){
    var LC=require('./lc.js')
    var VM=require('./lcvm.js')

    exports.hereDoc = hereDoc
    exports.parse = parse
    exports.astPrint = astPrint
    exports.gen = dgen
    exports.laz = laz

    function hereDoc(f) {
	return f.toString().
	    replace(/^[^\/]+\/\*!?/, '').
	    replace(/\*\/[^\/]+$/, '');
    }

................................................................................
	    res.push('name ', getNameNm(ast), ' (')
	    astPrint(getNameAst(ast), res)
	    res.push(')')
	    break
	case _refId:
	    res.push('ref ')
	    var val = getRefVar(ast)
	    if (val.lambda) throw new Error("Attempt to use lambda in ref, instead of string or number: " + val)
	    res.push(val)
	    break
	case _litId:
	    res.push('lit ')
	    var val = getLitVal(ast)

	    res.push(val.lambda ? "{" + val.lambda.toString() + "}" : val)
	    break
................................................................................
	    break
	case _primId:
	    res.push('prim ')
	    astPrint(getPrimArg(ast), res)
	    astPrint(getPrimRest(ast), res)
	    break
	default:
	    throw new Error("Unknown type of object in AST: " + ast)
	    break
	}
	return isFirst && res.join('')
    }

    /**
       A context is program scope
................................................................................
     */
    function createContext() {
	var ctx = (function(){
	    var tokens = {}
	    var groupOpens = {'(': ')'}
	    var groupCloses = {')': 1}
	    var C

	    // standard functions
	    function _eval() {
		return function(ast) {
//		    console.log("EVAL func: " + ast)
//		    console.log("EVAL ast: " + astPrint(ast()))
//		    console.log("EVAL gen: " + dgen(ast())[0])
		    return eval(dgen(ast())[0])
		}
	    }
	    function __lit() {
		return function(_x) {
		    return id(function(_f) {
			return _f()(_x)
		    }, _litId)
		}
	    }
	    function __ref() {
		return function(_x) {
		    return id(function(_f) {
			return _f()(_x)
		    }, _refId)
		}
	    }
	    function __lambda() {
		return function(_v) {
		    return id(function(_f) {
			return id(function(_g) {
			    return _g()(_v)(_f)
			}, _lambdaId)
		    }, -1)
		}
	    }
	    function __apply() {
		return function(_func) {
		    return id(function(_arg) {
			return id(function(_f) {
			    return _f()(_func)(_arg)
			}, _applyId)
		    }, -2)
		}
	    }
	    function __prim() {
		return function(_func) {
		    return id(function(_arg) {
			return id(function(_f) {
			    return _f()(_func)(_arg)
			}, _primId)
		    }, -3)
		}
	    }
	    function __name() {
		return function(_nm) {
		    return id(function(_ast) {
			return id(function(_f) {
			    return _f()(_nm)(_ast)
			}, _nameId)
		    }, -4)
		}
	    }

	    function id(func, id) {
		if (!id) {
		    C.astsById.push(func.name)
		}
		func.context = C
		func.id = id
		return func
	    }

	    function nameAst(nm, ast) {
		if (!ast.name) {
................................................................................
		if (!ast.id) {
		    C.astsById.push(ast)
		    ast.id = C.astsById.length
		}
		return ast
	    }

	    var t = this

	    C = {
		tokens: tokens,
		groupOpens: groupOpens,
		groupCloses: groupCloses,
		astsById: [],
		astsByName: {},
		addAst: addAst,
		id: id,
		nameAst: nameAst,
		eval: function(str){
//		    console.log("CTX.eval: " + str)
		    return eval(str)
		},
		subcontext: function(){return function(str){return eval(str)}},
	    }
	    C.astsByName.eval = id(_eval())
	    C.astsByName._lit = id(__lit())
	    C.astsByName._ref = id(__ref())
	    C.astsByName._lambda = id(__lambda())
	    C.astsByName._apply = id(__apply())
	    C.astsByName._prim = id(__prim())
	    C.astsByName._name = id(__name())

	    return C
	})()

	ctx.funcId = 0
	return ctx
    }

    var CTX = createContext()
    exports.eval = CTX.eval
    exports.tst = CTX.test

    function Memo() {
	this.count = 0
    }
    Memo.prototype = {
	add: function() {return "memo" + this.count++},
	toString: function() {
................................................................................
	var isFirst = !ctx

	res = res || []
	ctx = ctx || CTX.subcontext()
	ctx.curLit = 0
	if (isFirst) ctx.gen = 0
	CTX.addAst(ast)
	switch (ast.id || (ast.lambda && ast.lambda.id)) {
	case _nameId:
	    var nm = getNameNm(ast)
	    var a = getNameAst(ast)

	    CTX.nameAst(nm, a)
	    gen(a, res, ctx, vars, memo)
	    break
	case _refId:
	    var val = getRefVar(ast)

	    if (val.lambda) throw new Error("attempt to use lambda as a variable")
	    if (!vars.contains(val) && !CTX.astsByName[val]) throw new Error("unbound variable, '" + val + "' -- use lit instead")
	    res.push(nameSub(val))
	    if (deref) res.push("()")
	    break
	case _litId:
	    var val = getLitVal(ast)
	    var lit

................................................................................
		    gen(rest, res, ctx, vars, memo, true, true, false)
		}
		res.push(")")
		memoEnd(res, memo, deref)
	    }
	    break
	default:
	    throw new Error("Unknown object type in gen: " + ast)
	    break
	}
	ast.src = res.join('')
	return isFirst && [ast.src, ctx]
    }

    function run(cmp, arg) {
................................................................................
    function wlit(value) {return lit(laz(value))}
    function wref(variable) {return ref(laz(variable))}
    function wlambda(v, func) {return lambda(laz(v))(laz(func))}
    function wapply(func, arg) {return apply(laz(func))(laz(arg))}
    function wprim(arg, rest) {return prim(laz(arg))(laz(rest))}
    function wname(nm, ast) {return name(laz(nm))(laz(ast))}
    // base functions
    var lit = CTX.astsByName._lit
    var ref = CTX.astsByName._ref
    var lambda = CTX.astsByName._lambda
    var apply = CTX.astsByName._apply
    var prim = CTX.astsByName._prim
    var name = CTX.astsByName._name

    function lit(_x) {
	return id(function(_f) {
	    return _f()(_x)
	}, _litId)
    }
    function ref(_x) {
	return id(function(_f) {
................................................................................
	return expr
    }
    
    function tparseVariable(tok, vars, oldVars) {
	if (vars[tok] || CTX.astsByName[tok]) {
	    vars[tok] = tok
	    cur = wref(tok)
//console.log("var: " + tok)
	} else {
	    cur = wlit(tok)
//console.log("lit: " + tok)
	}
	return cur
    }

    function tparseLambda(toks, vars) {
	var name, old, body

	if (toks.length < 3 || toks[toks.length - 1] == '.') {
	    throw new Error('imcomplete lambda definition: ' + toks.reverse().join(' '))
	}
	old = vars[name]
	if (toks[toks.length - 2] == '.') {
	    name = toks.pop()
	    vars[name] = name
................................................................................
    console.log("t2 lambda: " + LC.L._t2().lambda)
    console.log("t2 id: " + LC.L._t2().lambda.id)
    console.log("t2 type: " + astType(LC.L._t2()))
    console.log("t2 var: " + LC.L._t2()(first))
    console.log("t2 body: " + LC.L._t2()(second).lambda)
    console.log("t2: " + astPrint(LC.L._t2()))
    console.log("t3: " + astPrint(LC.L._t3()))

    console.log("t4: " + astPrint(LC.L._t4()))

    console.log("t5: " + astPrint(LC.L._t5()))
    console.log("t6: " + astPrint(LC.L._t6()))
    console.log("gen t1: " + dgen(LC.L._t1())[0])
    console.log("run t1: " + run(dgen(LC.L._t1()), null))
    console.log("gen t2: " + dgen(LC.L._t2())[0])
    console.log("run t2: " + run(dgen(LC.L._t2()), null))
    console.log("gen t3: " + dgen(LC.L._t3())[0])
    console.log("run t3 'a' 'b': " + run(dgen(LC.L._t3()), null))
    console.log("t3 'a' 'b': " + run(dgen(LC.L._t3()), laz("a")))

    console.log("gen t4: " + dgen(LC.L._t4())[0])
    console.log("run t4 'a': " + run(dgen(LC.L._t4()), laz('a')))

    console.log("gen t5: " + dgen(LC.L._t5())[0])
    console.log("run t5 'a' 'b': " + run(dgen(LC.L._t5()), 'a')('b'))
    console.log("gen t6: " + dgen(LC.L._t6())[0])
    console.log("run t6 'a' 'b': " + run(dgen(LC.L._t6()), 'a')('b'))
    console.log("_refId: " + _refId)
    console.log("_litId: " + _litId)
    console.log("_lambdaId: " + _lambdaId)
................................................................................
    console.log("tlambda: " + LC.L.__lambda())
    console.log("tapply: " + LC.L.__apply())
    console.log("tprim: " + LC.L.__apply())
    console.log("tname: " + LC.L.__name())
    console.log("REF ID: " + (ref("duh")()).id)
    console.log("REF TEST: " + astPrint(ref("duh")()))
    console.log("REF GEN: " + dgen(ref("duh")())[0])


    var t4 = wlambda('x', wref('x'))
    console.log("T4 AST: " + astPrint(t4))
    console.log("T4 gen: " + dgen(t4)[0])
    console.log("T4 src: " + t4)

    console.log("T7: " + astPrint(LC.L._t7()))
................................................................................
    console.log("T7 3: " + eval(dgen(t7)[0])(laz(function(x){log("x: " + x()); return String(x())+"1"}))(laz(function(y){log("y: " + y()); return y()+"2"}))(laz("FLOOP ")))

    console.log("T77: " + LC.L._t77)

    console.log("LIT: " + dgen(wlit('x'), true)[0])

    console.log("\\x.x x x: " + astPrint(parse("\\x.x x y")))
*/

// TODO: compile AST funcs directly and include the JS source here
// for self hosting
})()

Changes to testLazp.js.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61










(function(){
var LC=require('./lc.js')
var LAZP=require('./lazp.js')

    function run() {
	console.log('hello')
	LC.loadDefs(DEFS)
    }

    var DEFS = LAZP.hereDoc(function() {/*
true = \x y . x
false = \x y . y

# Y combinator
Y = \g  .  (\x  .  g (x x)) \x  .  g (x x)

rec = \f . f (Y f)

# rec = \f . f f

# lists
# using false as "nil" in lists, so you use a list like this:
# DUMMY can be anything, but it needs to be there
# here's how you use a list:
# aList (\h t DUMMY . {list-case}) {empty-case}
# If the list is not empty, h and t are the head and tail of the list and it returns list-case.  DUMMY is not used, but needs to be there
# If the list is empty, it returns empty-case
# these defs are required by the pretty-printer: cons, nil, head, tail
cons = \a b f . f a b
nil = \x y . y
head = \l . l \h t . h
tail = \l . l \h t . t
null = \l . l (\h t D . false) true
last = rec \last l  . l (\h t D . null t h (last t)) nil
append = rec \append l1 l2 . l1 (\h t D . cons h (append t l2)) l2
reverse = \l . (rec \rev l res . l (\h t D . rev t (cons h res)) res) l nil
# list constructor: list 1 , 2 , 3 end
# first attempt, using recursive 'list' definition
#
#list = (rec \list rest item if-continue . if-continue (list (cons item rest)) (reverse (cons item rest))) nil
#, = true
#end = false
#
# second attempt, using "post-processing" with reverse
#[ =(]= \item f  .  f (cons item nil)
#, =.= \l item f . f (cons item l)
#] =)= reverse
#
# current constructor
# no post processing
# also allows [1, 2, 3 | REST ] (like Prolog)

[ =(]= \item c . c \rest . cons item rest
, =.= \f item c . c \rest . f (cons item rest)
] =)= \f . f nil
| =.= \f rest g . f rest
ident = \x . x
*/})

 run()
})()










|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Copyright (C) 2012, Bill Burdick, Tiny Concepts: http://tinyconcepts.com/fs.pl/lambda.fsl

(licensed with ZLIB license)

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.

3. This notice may not be removed or altered from any source distribution.
*/

/*
Tests for Lazp
*/

(function(){
    var U = require('util')
    var LZ=require('./lazp.js')

    function assertEq(actual, expected, desc) {
	if (expected != actual) throw new Error((desc ? "[" + desc + "] " : "") +  "Expected <" + expected + "> but got <" + actual + ">")
    }
    function assertEval(actual, expected, desc) {
	assertEq(LZ.eval(LZ.gen(LZ.parse(actual))[0]), expected, desc || actual)
    }
    function assertParse(actual, expected, desc) {
	assertEq(LZ.astPrint(LZ.parse(actual)), expected, desc || actual)
    }

    
    function runTests() {
	for (var i = 0; i < arguments.length ;i++) {
	    try {
		arguments[i]()
		U.print('.')
	    } catch (err) {
		U.print("\nFailure, ", arguments[i].name, ": ", err.stack)
	    }
	}
	U.print("\nDone")
    }

    function test1() {
	assertParse("\\x.x x y", "lambda x . apply (apply (ref x) (ref x)) (lit y)", "\\x.x x y")
    }
    function test2() {
	assertEval("(\\x . x) hello", 'hello')
    }
    function test3() {
	assertEval("eval (_apply (_lambda x (_ref x)) (_lit hello))", 'hello')
    }

    runTests(
	test1,
	test2,
	test3
    )
})()