Skip to content

Commit

Permalink
added list concat methods + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hugodecasta committed Feb 21, 2020
1 parent 260581e commit 6da3535
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 34 deletions.
86 changes: 55 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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]]])
Expand All @@ -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
Expand All @@ -116,12 +147,15 @@ let methods = {
return function(inner_args) {
let old_base = JSON.stringify(base)
for(let id=0;id<params.length;++id) {
base[params[id]] = inner_args[id]
handle(['define',params[id],inner_args[id]])
}
let ret = handle(prog)
base = JSON.parse(old_base)
return ret
}
},
'exit':function(args) {
process.exit(0)
}
}

Expand All @@ -144,33 +178,23 @@ function handle(statement) {

function compile(str) {

function create_eval(str) {

let replacer = str.replace(/\'\(/g,'(list ')

replacer = replacer
.replace(/\(([^()]*|\(([^()]*|\([^()]*\))*\))*\)/g,function(found) {
let repl = create_eval(found.slice(1,found.length-1))
return '['+repl+']'
})

return replacer
}

let evaluer = create_eval(str)
.replace(/(\w+|[-!$%^&*()_+|~=`{}:";'<>?,.\/])/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
module.exports = exports.compile = compile
19 changes: 16 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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])
})

0 comments on commit 6da3535

Please sign in to comment.