Skip to content

Commit

Permalink
making universal NAME function
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbuick committed Dec 21, 2024
1 parent 2c89f46 commit 86d8317
Show file tree
Hide file tree
Showing 29 changed files with 82 additions and 66 deletions.
7 changes: 4 additions & 3 deletions zss/device.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MESSAGE } from './chip'
import { hub } from './hub'
import { createsid } from './mapping/guid'
import { NAME } from './words/types'

export function createmessage(
target: string,
Expand Down Expand Up @@ -33,8 +34,8 @@ export function createdevice(
onMessage: MESSAGE_FUNC,
) {
const id = createsid()
const iname = name.toLowerCase()
const itopics = topics.map((tag) => tag.toLowerCase())
const iname = NAME(name)
const itopics = topics.map(NAME)

const device: DEVICE = {
id() {
Expand All @@ -54,7 +55,7 @@ export function createdevice(
},
handle(message) {
const { target, path } = parsetarget(message.target)
const itarget = target.toLowerCase()
const itarget = NAME(target)

// we match by topics
if (itopics.findIndex((tag) => tag === 'all' || tag === itarget) !== -1) {
Expand Down
3 changes: 2 additions & 1 deletion zss/device/synth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
SYNTH_NOTE_ON,
} from 'zss/mapping/play'
import { isarray, isnumber, ispresent, isstring } from 'zss/mapping/types'
import { NAME } from 'zss/words/types'

import { api_error, tape_info } from './api'

Expand Down Expand Up @@ -585,7 +586,7 @@ function validatesynthtype(
maybepartials: string | number | number[],
) {
if (isstring(value)) {
const maybetype = value.toLowerCase()
const maybetype = NAME(value)
let type = maybetype

// validate partials
Expand Down
4 changes: 2 additions & 2 deletions zss/firmware/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { synth_play, synth_voice, synth_voicefx } from 'zss/device/api'
import { createfirmware } from 'zss/firmware'
import { isnumber, isstring } from 'zss/mapping/types'
import { ARG_TYPE, readargs } from 'zss/words/reader'
import { WORD } from 'zss/words/types'
import { NAME, WORD } from 'zss/words/types'

const isfx = ['echo', 'reverb', 'chorus', 'phaser', 'distortion', 'vibrato']

Expand All @@ -20,7 +20,7 @@ function handlesynthvoice(idx: number, words: WORD[]) {
const [voiceorfx] = readargs(words, 0, [ARG_TYPE.NUMBER_OR_STRING])
if (isnumber(voiceorfx)) {
synth_voice('audio', idx, 'volume', voiceorfx)
} else if (isfx.includes(voiceorfx.toLowerCase())) {
} else if (isfx.includes(NAME(voiceorfx))) {
const [maybeconfig, maybevalue] = readargs(words, 1, [
ARG_TYPE.NUMBER_OR_STRING,
ARG_TYPE.MAYBE_NUMBER_OR_STRING,
Expand Down
6 changes: 3 additions & 3 deletions zss/firmware/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { CODE_PAGE, CODE_PAGE_TYPE } from 'zss/memory/types'
import { ARG_TYPE, READ_CONTEXT, readargs } from 'zss/words/reader'
import { stattypestring } from 'zss/words/stats'
import { metakey } from 'zss/words/system'
import { STAT_TYPE } from 'zss/words/types'
import { NAME, STAT_TYPE } from 'zss/words/types'
import {
bg,
fg,
Expand Down Expand Up @@ -179,7 +179,7 @@ export const CLI_FIRMWARE = createfirmware({
const [maybetype, ...args] = words.map(maptostring)
const maybename = args.join(' ')
// attempt to check first word as codepage type to create
switch (maybetype.toLowerCase()) {
switch (NAME(maybetype)) {
case stattypestring(STAT_TYPE.LOADER):
codepage = memoryensuresoftwarecodepage(
MEMORY_LABEL.CONTENT,
Expand All @@ -190,7 +190,7 @@ export const CLI_FIRMWARE = createfirmware({
default:
codepage = memoryensuresoftwarecodepage(
MEMORY_LABEL.CONTENT,
`${maybetype} ${maybename}`,
[maybetype, ...args].join(' '),
CODE_PAGE_TYPE.OBJECT,
)
break
Expand Down
6 changes: 3 additions & 3 deletions zss/firmware/gadget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { bookelementdisplayread } from 'zss/memory/book'
import { BOARD_ELEMENT } from 'zss/memory/types'
import { ARG_TYPE, READ_CONTEXT, readargs } from 'zss/words/reader'
import { statformat } from 'zss/words/stats'
import { COLOR, STAT_TYPE } from 'zss/words/types'
import { COLOR, NAME, STAT_TYPE } from 'zss/words/types'

export const GADGET_FIRMWARE = createfirmware({
get() {
Expand Down Expand Up @@ -75,7 +75,7 @@ export const GADGET_FIRMWARE = createfirmware({
}

// the intent here is to gather a list of target chip ids
const ltarget = target.toLowerCase()
const ltarget = NAME(target)
switch (ltarget) {
case 'all':
for (const id of Object.keys(READ_CONTEXT.board?.objects ?? {})) {
Expand Down Expand Up @@ -140,7 +140,7 @@ export const GADGET_FIRMWARE = createfirmware({
// ---
.command('gadget', (_, words) => {
const [edge] = readargs(words, 0, [ARG_TYPE.STRING])
const edgeConst = PANEL_TYPE_MAP[edge.toLowerCase()]
const edgeConst = PANEL_TYPE_MAP[NAME(edge)]
if (edgeConst === PANEL_TYPE.SCROLL) {
const [, name, size] = readargs(words, 0, [
ARG_TYPE.STRING,
Expand Down
3 changes: 2 additions & 1 deletion zss/firmware/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
memoryreadbinaryfile,
} from 'zss/memory'
import { ARG_TYPE, readargs } from 'zss/words/reader'
import { NAME } from 'zss/words/types'

import { binaryloader } from './loader/binaryloader'

Expand All @@ -18,7 +19,7 @@ export const LOADER_FIRMWARE = createfirmware({

const binaryfile = memoryreadbinaryfile(chip.id())
if (ispresent(binaryfile)) {
switch (name.toLowerCase()) {
switch (NAME(name)) {
case 'filename':
// name of binary file
return [ispresent(binaryfile.filename), binaryfile.filename]
Expand Down
9 changes: 5 additions & 4 deletions zss/firmware/loader/binaryloader.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { FIRMWARE_COMMAND } from 'zss/firmware'
import { isnumber, ispresent, isstring, MAYBE_NUMBER } from 'zss/mapping/types'
import { isnumber, ispresent, isstring, MAYBE } from 'zss/mapping/types'
import { memoryreadbinaryfile } from 'zss/memory'
import { BINARY_READER } from 'zss/memory/types'
import { ARG_TYPE, readargs } from 'zss/words/reader'
import { NAME } from 'zss/words/types'

function readbin(binaryfile: BINARY_READER, kind: string): MAYBE_NUMBER {
function readbin(binaryfile: BINARY_READER, kind: string): MAYBE<number> {
if (!ispresent(binaryfile)) {
return undefined
}

const lkind = kind.toLowerCase()
const lkind = NAME(kind)
const le = lkind.endsWith('le')
switch (lkind) {
case 'float32':
Expand Down Expand Up @@ -83,7 +84,7 @@ export const binaryloader: FIRMWARE_COMMAND = (chip, words) => {
}
const [kind] = readargs(words, 0, [ARG_TYPE.STRING])

const lkind = kind.toLowerCase()
const lkind = NAME(kind)
switch (lkind) {
case 'seek': {
const [cursor] = readargs(words, 1, [ARG_TYPE.NUMBER])
Expand Down
5 changes: 3 additions & 2 deletions zss/firmware/loader/parsefile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
codepagereadtypetostring,
createcodepage,
} from 'zss/memory/codepage'
import { NAME } from 'zss/words/types'

export function mimetypeofbytesread(filename: string, filebytes: Uint8Array) {
const bytes = [...filebytes.slice(0, 4)]
Expand Down Expand Up @@ -106,8 +107,8 @@ export async function parsebinaryfile(
try {
tape_info('parsebinaryfile', file.name)
const arraybuffer = await file.arrayBuffer()
const ext = file.name.split('.').slice(-1)[0]
onbuffer((ext ?? '').toLowerCase(), new Uint8Array(arraybuffer))
const ext = file.name.split('.').slice(-1)[0] ?? ''
onbuffer(NAME(ext), new Uint8Array(arraybuffer))
} catch (err: any) {
api_error('memory', 'crash', err.message)
}
Expand Down
3 changes: 2 additions & 1 deletion zss/gadget/components/panel/number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useState } from 'react'
import { modemwritevaluenumber, useWaitForValueNumber } from 'zss/device/modem'
import { paneladdress } from 'zss/gadget/data/types'
import { tokenizeandwritetextformat } from 'zss/words/textformat'
import { NAME } from 'zss/words/types'

import { useBlink } from '../hooks'
import {
Expand Down Expand Up @@ -132,7 +133,7 @@ export function PanelItemNumber({
CANCEL_BUTTON={ok}
OK_BUTTON={ok}
keydown={(event) => {
switch (event.key.toLowerCase()) {
switch (NAME(event.key)) {
case 'delete':
if (strvalue.length > 0) {
setstrValue((state) => strsplice(state, cursor, 1))
Expand Down
3 changes: 2 additions & 1 deletion zss/gadget/components/panel/panelitem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useGadgetClientPlayer } from 'zss/gadget/data/state'
import { PANEL_ITEM } from 'zss/gadget/data/types'
import { isarray } from 'zss/mapping/types'
import { writetextreset } from 'zss/words/textformat'
import { NAME } from 'zss/words/types'

import { PanelItemContent } from './content'
import { PanelItemHotkey } from './hotkey'
Expand Down Expand Up @@ -45,7 +46,7 @@ export function PanelItem({ item, active }: PanelItemProps) {
context,
}

switch (input.toLowerCase()) {
switch (NAME(input)) {
case 'hk':
case 'hotkey':
return <PanelItemHotkey {...props} />
Expand Down
3 changes: 2 additions & 1 deletion zss/gadget/components/panel/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
applycolortoindexes,
applystrtoindex,
} from 'zss/words/textformat'
import { NAME } from 'zss/words/types'

import { useBlink } from '../hooks'
import { UserFocus, UserInput, UserInputMods } from '../userinput'
Expand Down Expand Up @@ -126,7 +127,7 @@ export function PanelItemText({
}

const { key } = event
const lkey = key.toLowerCase()
const lkey = NAME(key)
const mods: UserInputMods = {
alt: event.altKey,
ctrl: ismac ? event.metaKey : event.ctrlKey,
Expand Down
4 changes: 2 additions & 2 deletions zss/gadget/components/tape/elements/editorinput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useTapeEditor } from 'zss/gadget/data/state'
import { clamp } from 'zss/mapping/number'
import { MAYBE, ispresent } from 'zss/mapping/types'
import { applystrtoindex, textformatreadedges } from 'zss/words/textformat'
import { PT } from 'zss/words/types'
import { NAME, PT } from 'zss/words/types'

import { useBlink, useWriteText } from '../../hooks'
import { Scrollable } from '../../scrollable'
Expand Down Expand Up @@ -165,7 +165,7 @@ export function EditorInput({
}

const { key } = event
const lkey = key.toLowerCase()
const lkey = NAME(key)
const mods = modsfromevent(event)

switch (lkey) {
Expand Down
3 changes: 2 additions & 1 deletion zss/gadget/components/tape/elements/terminalinput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
textformatreadedges,
tokenizeandwritetextformat,
} from 'zss/words/textformat'
import { NAME } from 'zss/words/types'

import { useBlink, useWriteText } from '../../hooks'
import { setuplogitem } from '../common'
Expand Down Expand Up @@ -208,7 +209,7 @@ export function TerminalInput({
<UserInput
keydown={(event) => {
const { key } = event
const lkey = key.toLowerCase()
const lkey = NAME(key)
const mods = modsfromevent(event)
switch (lkey) {
case 'arrowleft':
Expand Down
3 changes: 2 additions & 1 deletion zss/gadget/components/tape/elements/terminalitem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
tokenize,
tokenizeandwritetextformat,
} from 'zss/words/textformat'
import { NAME } from 'zss/words/types'

import {
TerminalItemInputProps,
Expand Down Expand Up @@ -58,7 +59,7 @@ export function TerminalItem({ blink, active, text, y }: TerminalItemProps) {
}

// render hyperlink
switch (input.toLowerCase()) {
switch (NAME(input)) {
case 'hk':
case 'hotkey':
return null
Expand Down
5 changes: 3 additions & 2 deletions zss/gadget/components/userinput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { vm_cli } from 'zss/device/api'
import { getgadgetclientplayer } from 'zss/gadget/data/state'
import { INPUT } from 'zss/gadget/data/types'
import { ismac } from 'zss/words/system'
import { NAME } from 'zss/words/types'

// user input

Expand Down Expand Up @@ -92,7 +93,7 @@ function invoke(input: INPUT, mods: UserInputMods) {
document.addEventListener(
'keydown',
(event) => {
const key = event.key.toLowerCase()
const key = NAME(event.key)
const mods = modsfromevent(event)

// allowed shortcuts, all others we attempt to block
Expand Down Expand Up @@ -195,7 +196,7 @@ document.addEventListener(
document.addEventListener(
'keyup',
(event) => {
const key = event.key.toLowerCase()
const key = NAME(event.key)
const mods = modsfromevent(event)

if (mods.alt) {
Expand Down
4 changes: 2 additions & 2 deletions zss/gadget/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'zss/device/modem'
import { createsid } from 'zss/mapping/guid'
import { ispresent, isnumber, isstring, MAYBE } from 'zss/mapping/types'
import { WORD } from 'zss/words/types'
import { NAME, WORD } from 'zss/words/types'

import {
GADGET_STATE,
Expand Down Expand Up @@ -253,7 +253,7 @@ export function gadgethyperlink(
}

// package into a panel item
const linput = input.toLowerCase()
const linput = NAME(input)

const hyperlink: WORD[] = [
chip.id(),
Expand Down
5 changes: 3 additions & 2 deletions zss/lang/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CodeWithSourceMap, SourceNode } from 'source-map'
import { deepcopy, ispresent, MAYBE } from 'zss/mapping/types'
import { tokenize, MaybeFlag } from 'zss/words/textformat'
import { NAME } from 'zss/words/types'

import { COMPARE, CodeNode, LITERAL, NODE, OPERATOR } from './visitor'

Expand Down Expand Up @@ -281,15 +282,15 @@ function transformNode(ast: CodeNode): SourceNode {
}
return write(ast, ` // skipped ${ast.value}\n`)
case NODE.LABEL: {
const llabel = ast.name.toLowerCase()
const llabel = NAME(ast.name)
const ltype = ast.active ? 'label' : 'comment'
if (!context.labels[llabel]) {
context.labels[llabel] = []
}
const lindex = (ast.active ? 1 : -1) * ast.lineindex
context.labels[llabel].push(lindex)
// document label
return write(ast, ` // ${lindex} ${ast.name} ${ltype}\n`)
return write(ast, ` // ${lindex} '${llabel}' ${ltype}\n`)
}
case NODE.HYPERLINK:
return write(ast, [
Expand Down
5 changes: 2 additions & 3 deletions zss/memory/atomics.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { MAYBE, ispresent } from 'zss/mapping/types'
import { COLLISION } from 'zss/words/types'
import { ispt } from 'zss/words/dir'
import {
readstrkindbg,
readstrkindcolor,
readstrkindname,
STR_KIND,
} from 'zss/words/kind'
import { PT } from 'zss/words/types'
import { COLLISION, NAME, PT } from 'zss/words/types'

import { boardelementbg, boardelementcolor, boardelementname } from './board'
import { BOARD, BOARD_ELEMENT, BOARD_HEIGHT, BOARD_WIDTH } from './types'
Expand Down Expand Up @@ -95,7 +94,7 @@ export function listelementsbyattr(
return maybebyid
}
// check by name
const maybebyname = listnamedelements(board, idnameorpt.toLowerCase())
const maybebyname = listnamedelements(board, NAME(idnameorpt))
if (maybebyname.length) {
return maybebyname
}
Expand Down
4 changes: 2 additions & 2 deletions zss/memory/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ptapplydir,
STR_DIR,
} from 'zss/words/dir'
import { COLOR, DIR, PT } from 'zss/words/types'
import { COLOR, DIR, NAME, PT } from 'zss/words/types'

import { listnamedelements, picknearestpt } from './atomics'
import { exportboardelement, importboardelement } from './boardelement'
Expand Down Expand Up @@ -113,7 +113,7 @@ export function boardelementread(
}

export function boardelementname(element: MAYBE<BOARD_ELEMENT>) {
return (element?.name ?? element?.kinddata?.name ?? 'object').toLowerCase()
return NAME(element?.name ?? element?.kinddata?.name ?? 'object')
}

export function boardelementcolor(element: MAYBE<BOARD_ELEMENT>) {
Expand Down
Loading

0 comments on commit 86d8317

Please sign in to comment.