Misc improvements

This commit is contained in:
neauoire
2020-03-26 20:04:24 +09:00
parent ff51bd7b89
commit 3b07edefc4
15 changed files with 92 additions and 774 deletions

View File

@@ -14,7 +14,6 @@ function Acels (client) {
this.el.id = 'acels'
this.order = []
this.all = {}
this.roles = {}
this.pipe = null
this.install = (host = document.body) => {
window.addEventListener('keydown', this.onKeyDown, false)
@@ -44,9 +43,6 @@ function Acels (client) {
if (this.order.indexOf(cat) < 0) { this.order.push(cat) }
this.all[accelerator] = { cat, name, downfn, upfn, accelerator }
}
this.add = (cat, role) => {
this.all[':' + role] = { cat, name: role, role }
}
this.get = (accelerator) => {
return this.all[accelerator]
}
@@ -503,12 +499,6 @@ function Client () {
this.acels.set('File', 'Save', 'CmdOrCtrl+S', () => { this.source.write('ronin', 'lisp', this.commander._input.value, 'text/plain') })
this.acels.set('File', 'Export Image', 'CmdOrCtrl+E', () => { this.source.write('ronin', 'png', this.surface.el.toDataURL('image/png', 1.0), 'image/png') })
this.acels.set('File', 'Open', 'CmdOrCtrl+U', () => { this.source.open('lisp', this.whenOpen) })
this.acels.add('Edit', 'undo')
this.acels.add('Edit', 'redo')
this.acels.add('Edit', 'cut')
this.acels.add('Edit', 'copy')
this.acels.add('Edit', 'paste')
this.acels.add('Edit', 'selectAll')
this.acels.set('View', 'Toggle Guides', 'CmdOrCtrl+Shift+H', () => { this.surface.toggleGuides() })
this.acels.set('View', 'Toggle Commander', 'CmdOrCtrl+K', () => { this.commander.toggle() })
this.acels.set('View', 'Expand Commander', 'CmdOrCtrl+Shift+K', () => { this.commander.toggle(true) })
@@ -802,19 +792,19 @@ function Commander (client) {
if (word === 'x' || word === 'y' || word === 'xy' || word === 'wh' || word === 'a' || word === 'r') { return `${shape}` }
return ''
}
this.show = function (expand = false) {
if (this.isVisible === true) { return }
this.show = (expand = false) => {
if (this.isVisible === true && expand !== true) { return }
client.el.className = expand ? 'expand' : ''
this.isVisible = true
this._input.focus()
}
this.hide = function () {
this.hide = () => {
if (this.isVisible !== true) { return }
client.el.className = 'hidden'
this.isVisible = false
this._input.blur()
}
this.toggle = function (expand = false) {
this.toggle = (expand = false) => {
if (this.isVisible !== true) {
this.show(expand)
} else {
@@ -853,212 +843,41 @@ function Commander (client) {
return `(${(name + ' ' + fnParams).trim()})`
}
this.lint = function () {
let val = this._input.value.replace(/\n/g, '').replace(/ \)/g, ')').replace(/ +(?= )/g, '').replace(/\( \(/g, '((').replace(/\) \)/g, '))').trim()
let depth = 0
if (val.split('(').length !== val.split(')').length) {
client.log('Uneven number of parens.')
return
}
for (let i = 0; i < val.length; i++) {
const c = val.charAt(i)
if (c === '(') { depth++ } else if (c === ')') { depth-- }
if (c === ';') {
const indent = '\n' + (' '.repeat(depth))
val = insert(val, indent, i)
i += indent.length
}
if (c === '(') {
const indent = '\n' + (' '.repeat(depth - 1))
val = insert(val, indent, i)
i += indent.length
}
if (c === ')' && depth === 0) {
val = insert(val, '\n', i + 1)
}
}
val = val.split('\n').map((line) => { return line.substr(0, 2) === '; ' ? `\n${line}\n` : line }).join('\n')
this._input.value = val.trim()
function insert (str, add, i) {
return [str.slice(0, i), `${add}`, str.slice(i)].join('')
const value = this._input.value
if (value.split('(').length !== value.split(')').length) {
return client.log('Uneven number of parens.')
}
this._input.value = lintLISP(value)
}
this.splash = `; Ronin v2.50
(def logo-path "M60,60 L195,60 A45,45 0 0,1 240,105 A45,45 0 0,1 195,150 L60,150 M195,150 A45,45 0 0,1 240,195 L240,240 ")
;
(clear)
(clear)
(resize 600 600)
(stroke
(svg 140 140 logo-path) "black" 7)`
}
'use strict'
function Lisp (lib = {}) {
const TYPES = { identifier: 0, number: 1, string: 2, bool: 3, symbol: 4 }
const Context = function (scope, parent) {
this.scope = scope
this.parent = parent
this.get = function (identifier) {
if (identifier in this.scope) {
return this.scope[identifier]
} else if (this.parent !== undefined) {
return this.parent.get(identifier)
}
function lintLISP (str) {
let val = str.replace(/\n/g, '').replace(/ \)/g, ')').replace(/ +(?= )/g, '').replace(/\( \(/g, '((').replace(/\) \)/g, '))').trim()
let depth = 0
for (let i = 0; i < val.length; i++) {
const c = val.charAt(i)
depth += c === '(' ? 1 : c === ')' ? -1 : 0
if (c === ';') {
const indent = '\n' + (' '.repeat(depth))
val = [val.slice(0, i), `${indent}`, val.slice(i)].join('')
i += indent.length
}
if (c === '(' && val.charAt(i + 1) !== ')') {
const indent = '\n' + (' '.repeat(depth - 1))
val = [val.slice(0, i), `${indent}`, val.slice(i)].join('')
i += indent.length
}
if (c === ')' && depth === 0) {
val = [val.slice(0, i), ')\n', val.slice(i + 1)].join('')
}
}
const special = {
let: function (input, context) {
const letContext = input[1].reduce(function (acc, x) {
acc.scope[x[0].value] = interpret(x[1], context)
return acc
}, new Context({}, context))
return interpret(input[2], letContext)
},
def: function (input, context) {
const identifier = input[1].value
const value = input[2].type === TYPES.string && input[3] ? input[3] : input[2]
context.scope[identifier] = interpret(value, context)
return value
},
defn: function (input, context) {
const fnName = input[1].value
const fnParams = input[2].type === TYPES.string && input[3] ? input[3] : input[2]
const fnBody = input[2].type === TYPES.string && input[4] ? input[4] : input[3]
context.scope[fnName] = async function () {
const lambdaArguments = arguments
const lambdaScope = fnParams.reduce(function (acc, x, i) {
acc[x.value] = lambdaArguments[i]
return acc
}, {})
return interpret(fnBody, new Context(lambdaScope, context))
}
},
lambda: function (input, context) {
return async function () {
const lambdaArguments = arguments
const lambdaScope = input[1].reduce(function (acc, x, i) {
acc[x.value] = lambdaArguments[i]
return acc
}, {})
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) {
return async function () {
const lambdaArguments = arguments
const keys = [...new Set(input.slice(2).flat(100).filter(i =>
i.type === TYPES.identifier &&
i.value[0] === '%'
).map(x => x.value).sort())]
const lambdaScope = keys.reduce(function (acc, x, i) {
acc[x] = lambdaArguments[i]
return acc
}, {})
return interpret(input.slice(1), new Context(lambdaScope, context))
}
},
__obj: async function (input, context) {
const obj = {}
for (let i = 1; i < input.length; i += 2) {
obj[await interpret(input[i], context)] = await interpret(input[i + 1], context)
}
return obj
}
}
const interpretList = async function (input, context) {
if (input.length > 0 && input[0].value in special) {
return special[input[0].value](input, context)
}
const list = []
for (let i = 0; i < input.length; i++) {
if (input[i].type === TYPES.symbol) {
if (input[i].host) {
const host = await context.get(input[i].host)
if (host) {
list.push(host[input[i].value])
}
} else {
list.push(obj => obj[input[i].value])
}
} else {
list.push(await interpret(input[i], context))
}
}
return list[0] instanceof Function ? list[0].apply(undefined, list.slice(1)) : list
}
const interpret = async function (input, context) {
if (!input) { console.warn('Lisp', 'error', context.scope); return null }
if (context === undefined) {
return interpret(input, new Context(lib))
} else if (input instanceof Array) {
return interpretList(input, context)
} else if (input.type === TYPES.identifier) {
return context.get(input.value)
} else if (input.type === TYPES.number || input.type === TYPES.symbol || input.type === TYPES.string || input.type === TYPES.bool) {
return input.value
}
}
const categorize = function (input) {
if (!isNaN(parseFloat(input))) {
return { type: TYPES.number, value: parseFloat(input) }
} else if (input[0] === '"' && input.slice(-1) === '"') {
return { type: TYPES.string, value: input.slice(1, -1) }
} else if (input[0] === ':') {
return { type: TYPES.symbol, value: input.slice(1) }
} else if (input.indexOf(':') > 0) {
return { type: TYPES.symbol, host: input.split(':')[0], value: input.split(':')[1] }
} else if (input === 'true' || input === 'false') {
return { type: TYPES.bool, value: input === 'true' }
} else {
return { type: TYPES.identifier, value: input }
}
}
const parenthesize = function (input, list) {
if (list === undefined) { return parenthesize(input, []) }
const token = input.shift()
if (token === undefined) {
return list.pop()
} else if (token === '\'(') {
input.unshift('__fn')
list.push(parenthesize(input, []))
return parenthesize(input, list)
} else if (token === '{') {
input.unshift('__obj')
list.push(parenthesize(input, []))
return parenthesize(input, list)
} else if (token === '(') {
list.push(parenthesize(input, []))
return parenthesize(input, list)
} else if (token === ')' || token === '}') {
return list
} else {
return parenthesize(input, list.concat(categorize(token)))
}
}
const tokenize = function (input) {
const i = input.replace(/^;.*\n?/gm, '').replace(/λ /g, 'lambda ').split('"')
return i.map(function (x, i) {
return i % 2 === 0
? x.replace(/\(/g, ' ( ')
.replace(/\)/g, ' ) ')
.replace(/' \( /g, ' \'( ') // '()
.replace(/\{/g, ' { ') // {}
.replace(/\}/g, ' } ') // {}
: x.replace(/ /g, '!whitespace!')
})
.join('"').trim().split(/\s+/)
.map(function (x) { return x.replace(/!whitespace!/g, ' ') })
}
this.parse = function (input) {
return parenthesize(tokenize(input))
}
this.run = async function (input) {
return interpret(this.parse(`((def theme (get-theme))(def frame (get-frame))(${input}))`))
}
val = val.split('\n').map((line) => { return line.substr(0, 2) === '; ' ? `\n${line}\n` : line }).join('\n')
return val.trim()
}
'use strict'
function Library (client) {