Skip to content

Commit

Permalink
codepage type centric firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbuick committed May 20, 2024
1 parent 7ac954f commit 60422a1
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 278 deletions.
164 changes: 96 additions & 68 deletions zss/firmware/all.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,73 @@
import { maptostring } from 'zss/chip'
import { api_error, register_read, register_write } from 'zss/device/api'
import { createfirmware } from 'zss/firmware'
import { gadgethyperlink, gadgettext } from 'zss/gadget/data/api'
import { isnumber } from 'zss/mapping/types'
import { memoryreadcontext } from 'zss/memory'
import { memoryreadchip, memoryreadcontext, memorysetbook } from 'zss/memory'
import { createbook } from 'zss/memory/book'

import { ARG_TYPE, readargs } from './wordtypes'

export const ALL_FIRMWARE = createfirmware({
get(chip, name) {
get() {
return [false, undefined]
},
set(chip, name, value) {
set() {
return [false, undefined]
},
shouldtick(chip) {
//
},
tick(chip) {
//
},
tock(chip) {
//
},
shouldtick() {},
tick() {},
tock() {},
})
.command('clear', (chip, words) => {
const name = maptostring(words[0])
chip.set(name, undefined)
return 0
})
.command('end', (chip) => {
// future, this will also afford giving a return value #end <value>
chip.endofprogram()
return 0
})
.command('give', (chip, words) => {
const [name, maybevalue, ii] = readargs(memoryreadcontext(chip, words), 0, [
ARG_TYPE.STRING,
ARG_TYPE.MAYBE_NUMBER,
])

const maybecurrent = chip.get(name)
const current = isnumber(maybecurrent) ? maybecurrent : 0
const value = maybevalue ?? 1

// giving a non-numerical value
if (!isnumber(value)) {
// todo: raise warning ?
return 0
}
// memory / app state
.command('book', (chip, words) => {
const [maybetarget, maybeaction] = readargs(
memoryreadcontext(chip, words),
0,
[ARG_TYPE.STRING, ARG_TYPE.STRING],
)

// returns true when setting an unset flag
const result = maybecurrent === undefined ? 1 : 0
if (result && ii < words.length) {
chip.command(...words.slice(ii))
const ltarget = maybetarget.toLowerCase()
const laction = maybeaction.toLowerCase()
switch (laction) {
case 'create':
memorysetbook(createbook(ltarget, []))
break
default:
// TODO raise error of unknown action
break
}

// update flag
chip.set(name, current + value)
return result
})
.command('idle', (chip) => {
chip.yield()
return 0
})
// .command('if' // stub-only, this is a lang feature
.command('lock', (chip) => {
chip.lock(chip.id())
.command('register', (chip, words) => {
const memory = memoryreadchip(chip.id())
const maybeplayer = memory.object?.stats?.player ?? ''
const [action, name] = readargs(memoryreadcontext(chip, words), 0, [
ARG_TYPE.STRING,
ARG_TYPE.STRING,
])
switch (action.toLowerCase()) {
case 'read':
register_read(chip.senderid(), name, maybeplayer)
break
case 'write':
register_write(chip.senderid(), name, chip.get(name), maybeplayer)
break
default:
api_error(
chip.senderid(),
'register',
`unknown #regsiter [action] ${action}`,
maybeplayer,
)
break
}
return 0
})
// .command('restart' // this is handled by a built-in 0 label
.command('restore', (chip, words) => {
chip.restore(maptostring(words[0]))
// flags
.command('clear', (chip, words) => {
const name = maptostring(words[0])
chip.set(name, undefined)
return 0
})
.command('set', (chip, words) => {
Expand Down Expand Up @@ -111,25 +108,56 @@ export const ALL_FIRMWARE = createfirmware({
chip.set(name, newvalue)
return 0
})
.command('unlock', (chip) => {
chip.unlock()
.command('give', (chip, words) => {
const [name, maybevalue, ii] = readargs(memoryreadcontext(chip, words), 0, [
ARG_TYPE.STRING,
ARG_TYPE.MAYBE_NUMBER,
])

const maybecurrent = chip.get(name)
const current = isnumber(maybecurrent) ? maybecurrent : 0
const value = maybevalue ?? 1

// giving a non-numerical value
if (!isnumber(value)) {
// todo: raise warning ?
return 0
}

// returns true when setting an unset flag
const result = maybecurrent === undefined ? 1 : 0
if (result && ii < words.length) {
chip.command(...words.slice(ii))
}

// update flag
chip.set(name, current + value)
return result
})
// lifecycle
.command('idle', (chip) => {
chip.yield()
return 0
})
.command('zap', (chip, words) => {
chip.zap(maptostring(words[0]))
.command('end', (chip) => {
// future, this will also afford giving a return value #end <value>
chip.endofprogram()
return 0
})
// gadget output & ui
.command('text', (chip, words) => {
const text = words.map(maptostring).join('')
gadgettext(chip, text)
// message mangement
.command('lock', (chip) => {
chip.lock(chip.id())
return 0
})
.command('restore', (chip, words) => {
chip.restore(maptostring(words[0]))
return 0
})
.command('hyperlink', (chip, args) => {
// package into a panel item
const [labelword, inputword, ...words] = args
const label = maptostring(labelword)
const input = maptostring(inputword)
gadgethyperlink(chip, label, input, words)
.command('unlock', (chip) => {
chip.unlock()
return 0
})
.command('zap', (chip, words) => {
chip.zap(maptostring(words[0]))
return 0
})
21 changes: 21 additions & 0 deletions zss/firmware/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createfirmware } from 'zss/firmware'

export const CLI_FIRMWARE = createfirmware({
get(chip, name) {
return [false, undefined]
},
set(chip, name, value) {
return [false, undefined]
},
shouldtick(chip) {
//
},
tick(chip) {
//
},
tock(chip) {
//
},
}).command('stub', (chip, words) => {
return 0
})
114 changes: 114 additions & 0 deletions zss/firmware/gadget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { maptostring } from 'zss/chip'
import { createfirmware } from 'zss/firmware'
import {
gadgetcheckscroll,
gadgetcheckset,
gadgethyperlink,
gadgetpanel,
gadgettext,
} from 'zss/gadget/data/api'
import { PANEL_TYPE, PANEL_TYPE_MAP } from 'zss/gadget/data/types'
import { ispresent } from 'zss/mapping/types'
import {
memorycreateeditframe,
memorycreateviewframe,
memoryreadchip,
memoryreadcontext,
memoryresetframes,
} from 'zss/memory'

import { ARG_TYPE, readargs } from './wordtypes'

export const GADGET_FIRMWARE = createfirmware({
get() {
return [false, undefined]
},
set(chip, name, value) {
// how about we split this out into gadget firmware
// we monitor changes on shared values here
gadgetcheckset(chip, name, value)
// return has unhandled
return [false, undefined]
},
shouldtick() {},
tick(chip) {
const memory = memoryreadchip(chip.id())

let withname = 'scroll'
if (ispresent(memory.object?.name)) {
withname = memory.object.name
}

gadgetpanel(chip, 'scroll', PANEL_TYPE.SCROLL, undefined, withname)
},
tock(chip) {
gadgetcheckscroll(chip)
},
})
// gadget output & ui
.command('gadget', (chip, words) => {
const context = memoryreadcontext(chip, words)

const [edge] = readargs(context, 0, [ARG_TYPE.STRING])
const edgeConst = PANEL_TYPE_MAP[edge.toLowerCase()]
if (edgeConst === PANEL_TYPE.SCROLL) {
const [, name, size] = readargs(context, 0, [
ARG_TYPE.STRING,
ARG_TYPE.MAYBE_STRING,
ARG_TYPE.MAYBE_NUMBER,
])
gadgetpanel(chip, edge, edgeConst, size, name)
} else {
const [, size, name] = readargs(context, 0, [
ARG_TYPE.STRING,
ARG_TYPE.MAYBE_NUMBER,
ARG_TYPE.MAYBE_STRING,
])
gadgetpanel(chip, edge, edgeConst, size, name)
}

return 0
})
.command('frame', (chip, words) => {
const memory = memoryreadchip(chip.id())
const [maybetarget, maybetype, maybeboard] = readargs(
memoryreadcontext(chip, words),
0,
[ARG_TYPE.STRING, ARG_TYPE.MAYBE_STRING, ARG_TYPE.MAYBE_STRING],
)

const board = memory.board?.id ?? ''

const ltarget = maybetarget.toLowerCase()
if (ltarget === 'reset') {
memoryresetframes(board)
} else if (ispresent(maybetype) && ispresent(maybeboard)) {
const ltype = maybetype.toLowerCase()
switch (ltype) {
case 'edit':
memorycreateeditframe(board, ltarget, maybeboard)
break
case 'view':
memorycreateviewframe(board, ltarget, maybeboard)
break
default:
// TODO raise error of unknown action
break
}
}

return 0
})
.command('text', (chip, words) => {
const text = words.map(maptostring).join('')
gadgettext(chip, text)
return 0
})
.command('hyperlink', (chip, args) => {
// package into a panel item
const [labelword, inputword, ...words] = args
const label = maptostring(labelword)
const input = maptostring(inputword)
gadgethyperlink(chip, label, input, words)
return 0
})
3 changes: 3 additions & 0 deletions zss/firmware/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { FIRMWARE } from 'zss/firmware'
import { CODE_PAGE_TYPE } from 'zss/memory/codepage'

import { ALL_FIRMWARE } from './all'
import { CLI_FIRMWARE } from './cli'
import { ZSS_FIRMWARE } from './zss'
import { ZZT_FIRMWARE } from './zzt'

const firmwares: Record<string, FIRMWARE> = {
all: ALL_FIRMWARE,
cli: CLI_FIRMWARE,
zzt: ZZT_FIRMWARE,
zss: ZSS_FIRMWARE,
}

export const CODE_PAGE_FIRMWARE = {
[CODE_PAGE_TYPE.ERROR]: [],
[CODE_PAGE_TYPE.CLI]: ['all', 'cli'],
[CODE_PAGE_TYPE.FUNC]: ['all'],
[CODE_PAGE_TYPE.BOARD]: ['all'],
[CODE_PAGE_TYPE.OBJECT]: ['all', 'zss', 'zzt'],
Expand Down
Loading

0 comments on commit 60422a1

Please sign in to comment.