From 6da3535bc0c432f14b116bf9a4fe338267200cee Mon Sep 17 00:00:00 2001 From: hugodecasta Date: Fri, 21 Feb 2020 15:06:28 +0100 Subject: [PATCH] added list concat methods + tests --- index.js | 86 ++++++++++++++++++++++++++++++++++++-------------------- test.js | 19 +++++++++++-- 2 files changed, 71 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index 87481ce..a90efa2 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,11 @@ // ----------------------------------- -let base = {} +function get_list(statement) { + let list = handle(statement) + return list.filter(item=>item!='list') +} +let base = {} let methods = { 'define':function(args) { let name = args[0] @@ -16,6 +20,9 @@ let methods = { 'eval':function(args) { let value = args[0] while(typeof value == 'object' || typeof value == 'string') { + if(typeof value == 'object') { + value = value.filter(item=>item != 'list') + } value = handle(value) } return value @@ -87,16 +94,20 @@ let methods = { return false }, // ----------------------------------------------------- - 'list':function(args) { - return args + 'null?':function(args) { + let list = get_list(args[0]) + return list.length == 0 + }, + 'abslist':function(args) { + return ['list'].concat(args) }, 'car':function(args) { - let list = handle(args[0]) + let list = get_list(args[0]) return list[0] }, 'cdr':function(args) { - let list = handle(args[0]) - return list.slice(1) + let list = get_list(args[0]) + return ['list'].concat(list.slice(1)) }, 'cadr':function(args) { return handle(['car',['cdr',args[0]]]) @@ -105,6 +116,26 @@ let methods = { return handle(['cdr',['cdr',args[0]]]) }, // ----------------------------------------------------- + 'append':function(args) { + let final_list = ['list'] + for(let arg of args) { + let sub_list = get_list(arg) + for(let elm of sub_list) { + final_list.push(elm) + } + } + return final_list + }, + 'cons':function(args) { + let car = handle(args[0]) + let cdr = get_list(args[1]) + return ['list',car].concat(cdr) + }, + 'list':function(args) { + let list = args.map(item=>handle(item)) + return handle(['abslist'].concat(list)) + }, + // ----------------------------------------------------- 'clear':function() { base = {} return true @@ -116,12 +147,15 @@ let methods = { return function(inner_args) { let old_base = JSON.stringify(base) for(let id=0;id?,.\/])/g,function(test) { - let val = parseInt(test) - if(isNaN(val)) { - val = '"'+test+'"' + let replacer = str + .replace(/\'\(\)/g,'(abslist)') + .replace(/\'\(/g,'(abslist ') + .replace(/\(/g,'[') + .replace(/\)/g,']') + .replace(/\s/g,',') + .replace(/(\w+\?|\w+|[-!$%^&*()_+|~=`{}:";'<>?.\/])/g,function(match) { + let ival = parseInt(match) + if(isNaN(ival)) { + return '"'+match+'"' } - return val + return ival }) - .replace(/\s/g,',') + let obj_command = JSON.parse(replacer) - return handle(JSON.parse(evaluer)) + return handle(obj_command) } -module.exports = exports.compile = compile - -let scheme = compile \ No newline at end of file +module.exports = exports.compile = compile \ No newline at end of file diff --git a/test.js b/test.js index 8a206c0..e5b1694 100644 --- a/test.js +++ b/test.js @@ -27,7 +27,7 @@ test('list car', t => { }) test('list cdr', t => { - t.deepEqual(scheme("(cdr '(a b c d))"), ['b','c','d']) + t.deepEqual(scheme("(cdr '(a b c d))"), ['list','b','c','d']) }) test('list cadr', t => { @@ -36,15 +36,28 @@ test('list cadr', t => { test('list define', t => { scheme('(clear)') - t.deepEqual(scheme("(define tot '(+ a b))"), ['+','a','b']) + t.deepEqual(scheme("(define tot '(+ a b))"), ['list','+','a','b']) }) test('list read', t => { - t.deepEqual(scheme("tot"), ['+','a','b']) + t.deepEqual(scheme("tot"), ['list','+','a','b']) }) test('list eval', t => { t.is(scheme("(define a 5)"), 5) t.is(scheme("(define b 4)"), 4) t.is(scheme("(eval tot)"),9) +}) + +test('lambda recurs', t => { + scheme('(define rev (lambda (l) (if (null? l) l (append (rev (cdr l)) (list (car l))))))') + t.deepEqual(scheme("(rev '(a b c d))"), ['list','d','c','b','a']) +}) + +test('cons', t => { + scheme('(clear)') + scheme("(define l1 '(a b c d))") + scheme("(define l2 '(1 2 3 4))") + t.deepEqual(scheme("(cons (car l1) (cdr l2))"), ['list','a',2,3,4]) + t.deepEqual(scheme("(cons '(g h) (cdr l2))"), ['list',['list','g','h'],2,3,4]) }) \ No newline at end of file