Skip to content

Commit

Permalink
new string type feature
Browse files Browse the repository at this point in the history
  • Loading branch information
hugodecasta committed Mar 21, 2020
1 parent cf4503d commit aefc6f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ function get_list(statement) {
}

function is_string(statement) {
if(typeof statement == 'object') {
return false
}
var count = (statement.match(/"/g) || []).length
return statement.charAt(0) == '"'
&& statement.charAt(statement.length-1) == '"'
Expand All @@ -62,6 +65,9 @@ let methods = {
if(typeof value == 'object') {
value = value.filter(item=>item != 'list')
}
if(is_string(value)) {
return value
}
value = handle(value)
}
return value
Expand Down Expand Up @@ -240,7 +246,7 @@ function handle(statement) {

if(typeof statement === 'string') {
if(is_string(statement)) {
return statement.replace(/"/g,'')
return statement
}
if(!(statement in base)) {
throw new ExistenceError(statement,'variable')
Expand All @@ -261,9 +267,15 @@ function handle(statement) {

function parse(str) {

let str_next_index = 0
let strs = {}

let replacer = str
.replace(/"(.*?)"/g,function(_,string){
return '___'+string.replace(/\s/g,'__')+'___'
let index = str_next_index
str_next_index++
strs[index] = string
return '#'+index+'#'
})
.replace(/(\#t)/g,'true')
.replace(/(\#f)/g,'false')
Expand All @@ -279,8 +291,8 @@ function parse(str) {
return '"'+match+'"'
}
})
.replace(/"___(.*?)___"/g,function(_,string) {
return '"\\"'+string.replace(/__/g,' ')+'\\""'
.replace(/#(.*?)#/g,function(_,index) {
return '"\\"'+strs[index]+'\\""'
})

try {
Expand Down
15 changes: 10 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,15 @@ test('or', t => {
})

test('string', t => {
t.is(scheme('"hello world"'),'hello world')
t.is(scheme('(define a "holo")'),'holo')
t.is(scheme('a'),'holo')
t.is(scheme('"hello world"'),'"hello world"')
t.is(scheme('(define a "holo")'),'"holo"')
t.is(scheme('a'),'"holo"')
scheme('(define a (lambda (a) (if (> a 5) "is bigger" "is less")))')
t.is(scheme('(a 10)'),'is bigger')
t.is(scheme('(a 2)'),'is less')
t.is(scheme('(a 10)'),'"is bigger"')
t.is(scheme('(a 2)'),'"is less"')
})

test('string in list', t => {
scheme("(define a '(define a \"hello world\"))")
t.deepEqual(scheme('a'), ['list','define','a','"hello world"'])
})

0 comments on commit aefc6f9

Please sign in to comment.