add suport for {} objects

This commit is contained in:
Quentin Leonetti 2019-07-20 19:46:52 +02:00
parent 3d5a17431e
commit 2574ad297b
3 changed files with 40 additions and 6 deletions

View File

@ -233,6 +233,14 @@ function Library (ronin) {
}, h) }, h)
} }
this.keys = (item) => { // Returns a list of the object's keys
return Object.keys(item)
}
this.values = (item) => { // Returns a list of the object's values
return Object.values(item)
}
// Frame // Frame
this.frame = () => { // Returns a rect of the frame. this.frame = () => { // Returns a rect of the frame.

View File

@ -60,6 +60,12 @@ function Lisp (input, lib) {
return interpret(input[2], new Context(lambdaScope, context)) return interpret(input[2], new Context(lambdaScope, context))
} }
}, },
if: async function (input, context) {
if (await interpret(input[1], context)) {
return interpret(input[2], context)
}
return input[3] ? interpret(input[3], context) : []
},
__fn: function (input, context) { __fn: function (input, context) {
return async function () { return async function () {
const lambdaArguments = arguments const lambdaArguments = arguments
@ -70,11 +76,12 @@ function Lisp (input, lib) {
return interpret(input.slice(1), new Context(lambdaScope, context)) return interpret(input.slice(1), new Context(lambdaScope, context))
} }
}, },
if: async function (input, context) { __obj: async function (input, context) {
if (await interpret(input[1], context)) { const obj = {}
return interpret(input[2], context) for (let i = 1 ; i<input.length ; i+=2) {
obj[await interpret(input[i] ,context)] = await interpret(input[i+1], context)
} }
return input[3] ? interpret(input[3], context) : [] return obj
} }
} }
@ -124,10 +131,14 @@ function Lisp (input, lib) {
input.unshift('__fn') input.unshift('__fn')
list.push(parenthesize(input, [])) list.push(parenthesize(input, []))
return parenthesize(input, list) return parenthesize(input, list)
} else if (token === '{') {
input.unshift('__obj')
list.push(parenthesize(input, []))
return parenthesize(input, list)
} else if (token === '(') { } else if (token === '(') {
list.push(parenthesize(input, [])) list.push(parenthesize(input, []))
return parenthesize(input, list) return parenthesize(input, list)
} else if (token === ')') { } else if (token === ')' || token === '}') {
return list return list
} else { } else {
return parenthesize(input, list.concat(categorize(token))) return parenthesize(input, list.concat(categorize(token)))
@ -138,7 +149,11 @@ function Lisp (input, lib) {
const i = input.replace(/^\;.*\n?/gm, '').split('"') const i = input.replace(/^\;.*\n?/gm, '').split('"')
return i.map(function (x, i) { return i.map(function (x, i) {
return i % 2 === 0 ? return i % 2 === 0 ?
x.replace(/\(/g, ' ( ').replace(/\)/g, ' ) ').replace(/' \( /g, ' \'( ') x.replace(/\(/g, ' ( ')
.replace(/\)/g, ' ) ')
.replace(/' \( /g, ' \'( ') // '()
.replace(/\{/g, ' { ') // {}
.replace(/\}/g, ' } ') // {}
: x.replace(/ /g, '!whitespace!') : x.replace(/ /g, '!whitespace!')
}) })
.join('"').trim().split(/\s+/) .join('"').trim().split(/\s+/)

11
examples/objects.lisp Normal file
View File

@ -0,0 +1,11 @@
; objects
(def ob {"a" 1 "b" 2})
(echo (of ob "a"))
(echo (keys ob))
(echo (values ob))
(set ob "a" 4)
(echo (of ob "a"))