Misc improvements
This commit is contained in:
@@ -43,27 +43,16 @@ function Client () {
|
||||
this.acels.set('∷', 'Toggle Menubar', 'Tab', () => { this.acels.toggle() })
|
||||
this.acels.set('∷', 'Open Theme', 'CmdOrCtrl+Shift+O', () => { this.theme.open() })
|
||||
this.acels.set('∷', 'Reset Theme', 'CmdOrCtrl+Backspace', () => { this.theme.reset() })
|
||||
|
||||
this.acels.set('File', 'New', 'CmdOrCtrl+N', () => { this.source.new(); this.surface.clear(); this.commander.clear() })
|
||||
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) })
|
||||
|
||||
this.acels.set('Project', 'Run', 'CmdOrCtrl+R', () => { this.commander.run() })
|
||||
this.acels.set('Project', 'Re-Indent', 'CmdOrCtrl+Shift+I', () => { this.commander.lint() })
|
||||
this.acels.set('Project', 'Clean', 'Escape', () => { this.commander.cleanup() })
|
||||
|
||||
this.acels.route(this)
|
||||
}
|
||||
|
||||
|
||||
@@ -161,21 +161,21 @@ function Commander (client) {
|
||||
|
||||
// Display
|
||||
|
||||
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 {
|
||||
@@ -222,38 +222,11 @@ function Commander (client) {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
// add linebreak after paren at depth 0
|
||||
if (c === ')' && depth === 0) {
|
||||
val = insert(val, '\n', i + 1)
|
||||
}
|
||||
}
|
||||
// Space out comments
|
||||
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)
|
||||
}
|
||||
|
||||
// Splash
|
||||
@@ -261,9 +234,41 @@ function Commander (client) {
|
||||
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)`
|
||||
}
|
||||
|
||||
function lintLISP (str) {
|
||||
// cleanup
|
||||
let val = str.replace(/\n/g, '').replace(/ \)/g, ')').replace(/ +(?= )/g, '').replace(/\( \(/g, '((').replace(/\) \)/g, '))').trim()
|
||||
// begin
|
||||
let depth = 0
|
||||
for (let i = 0; i < val.length; i++) {
|
||||
const c = val.charAt(i)
|
||||
depth += c === '(' ? 1 : c === ')' ? -1 : 0
|
||||
// Pad comments
|
||||
if (c === ';') {
|
||||
const indent = '\n' + (' '.repeat(depth))
|
||||
val = [val.slice(0, i), `${indent}`, val.slice(i)].join('')
|
||||
i += indent.length
|
||||
}
|
||||
// Don't pad when closing on next char
|
||||
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
|
||||
}
|
||||
// Add linebreak after paren at depth 0
|
||||
if (c === ')' && depth === 0) {
|
||||
val = [val.slice(0, i), ')\n', val.slice(i + 1)].join('')
|
||||
}
|
||||
}
|
||||
// Space out comments
|
||||
val = val.split('\n').map((line) => { return line.substr(0, 2) === '; ' ? `\n${line}\n` : line }).join('\n')
|
||||
return val.trim()
|
||||
}
|
||||
|
||||
@@ -1,180 +0,0 @@
|
||||
'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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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}))`))
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ function Acels (client) {
|
||||
|
||||
this.order = []
|
||||
this.all = {}
|
||||
this.roles = {}
|
||||
this.pipe = null
|
||||
|
||||
this.install = (host = document.body) => {
|
||||
@@ -40,10 +39,6 @@ function Acels (client) {
|
||||
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]
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
/* global Image */
|
||||
/* global Image */
|
||||
|
||||
function Library (client) {
|
||||
|
||||
Reference in New Issue
Block a user