Skip to content

Commit

Permalink
debugging conditonals
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbuick committed Dec 22, 2024
1 parent 658b010 commit 25d1de9
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 106 deletions.
4 changes: 3 additions & 1 deletion zss/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ export function createchip(

// update chip state based on incoming message
chip.set('sender', sender)
chip.set('arg', arg)
if (ispresent(arg)) {
chip.set('arg', arg)
}

// this sets player focus
if (player) {
Expand Down
21 changes: 6 additions & 15 deletions zss/firmware/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ispresent, isstring, MAYBE } from 'zss/mapping/types'
import { MEMORY_LABEL, memorytickobject } from 'zss/memory'
import { listelementsbykind, listnamedelements } from 'zss/memory/atomics'
import { boardterrainsetfromkind } from 'zss/memory/board'
import { boardelementwritestats } from 'zss/memory/boardelement'
import {
bookboardelementreadname,
bookboardmoveobject,
Expand Down Expand Up @@ -98,12 +97,6 @@ function commandshoot(chip: CHIP, words: WORD[], arg?: WORD): 0 | 1 {
// read direction + what to shoot
const [dir, kind] = readargs(words, 0, [ARG_TYPE.DIR, ARG_TYPE.MAYBE_KIND])

// set walking direction
const step = {
x: dir.x - READ_CONTEXT.element.x,
y: dir.y - READ_CONTEXT.element.y,
}

// write new element
const bulletkind = kind ?? ['bullet']
const bullet = bookboardwritebulletobject(
Expand All @@ -118,17 +111,14 @@ function commandshoot(chip: CHIP, words: WORD[], arg?: WORD): 0 | 1 {

// success ! get it moving
if (ispresent(bullet)) {
// write arg if set
if (ispresent(arg)) {
bullet.arg = arg
}
// write arg
bullet.arg = arg

// ensure correct collection type
bullet.collision = COLLISION.ISBULLET
boardelementwritestats(bullet, {
stepx: step.x,
stepy: step.y,
})
// set walking direction
bullet.stepx = dir.x - READ_CONTEXT.element.x
bullet.stepy = dir.y - READ_CONTEXT.element.y

// object code
const kind = bookelementkindread(READ_CONTEXT.book, bullet)
Expand All @@ -137,6 +127,7 @@ function commandshoot(chip: CHIP, words: WORD[], arg?: WORD): 0 | 1 {
// bullets get one immediate tick
memorytickobject(READ_CONTEXT.book, READ_CONTEXT.board, bullet, code, 1)

// determine outcome
if (
bullet.x === READ_CONTEXT.element.x &&
bullet.y === READ_CONTEXT.element.y
Expand Down
25 changes: 9 additions & 16 deletions zss/firmware/element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { is } from '@react-three/fiber/dist/declarations/src/core/utils'
import { createfirmware } from 'zss/firmware'
import {
INPUT,
Expand All @@ -9,15 +8,13 @@ import {
import { isarray, isnumber, ispresent } from 'zss/mapping/types'
import { memoryreadflags } from 'zss/memory'
import { boardelementapplycolor } from 'zss/memory/board'
import { boardelementwritestats } from 'zss/memory/boardelement'
import {
bookboardwrite,
bookboardobjectsafedelete,
bookboardsetlookup,
bookboardobjectnamedlookupdelete,
} from 'zss/memory/book'
import { BOARD_ELEMENT } from 'zss/memory/types'
import { dirfrompts, ispt, ptapplydir } from 'zss/words/dir'
import { ARG_TYPE, READ_CONTEXT, readargs } from 'zss/words/reader'
import { PT } from 'zss/words/types'

Expand Down Expand Up @@ -163,10 +160,8 @@ export const ELEMENT_FIRMWARE = createfirmware({
},
) === false
) {
boardelementwritestats(READ_CONTEXT.element, {
stepx: 0,
stepy: 0,
})
READ_CONTEXT.element.stepx = 0
READ_CONTEXT.element.stepy = 0
}
// headless only gets a single tick to do its magic
if (READ_CONTEXT.element?.headless) {
Expand Down Expand Up @@ -251,6 +246,7 @@ export const ELEMENT_FIRMWARE = createfirmware({
})
.command('try', (chip, words) => {
const [, ii] = readargs(words, 0, [ARG_TYPE.DIR])

// try and move
const result = chip.command('go', ...words)
if (result && ii < words.length) {
Expand All @@ -260,20 +256,17 @@ export const ELEMENT_FIRMWARE = createfirmware({
return 0
})
.command('walk', (_, words) => {
// invalid data
if (!ispt(READ_CONTEXT.element)) {
if (!ispresent(READ_CONTEXT.element)) {
return 0
}

// read walk direction
const [maybedir] = readargs(words, 0, [ARG_TYPE.DIR])
const dir = dirfrompts(READ_CONTEXT.element, maybedir)
const step = ptapplydir({ x: 0, y: 0 }, dir)
const [dest] = readargs(words, 0, [ARG_TYPE.DIR])
const x = READ_CONTEXT.element.x ?? 0
const y = READ_CONTEXT.element.y ?? 0

// create delta from dir
boardelementwritestats(READ_CONTEXT.element, {
stepx: step.x,
stepy: step.y,
})
READ_CONTEXT.element.stepx = dest.x - x
READ_CONTEXT.element.stepy = dest.y - y
return 0
})
13 changes: 7 additions & 6 deletions zss/firmware/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { maptostring } from 'zss/chip'
import { vm_endgame } from 'zss/device/api'
import { createfirmware } from 'zss/firmware'
import { clamp } from 'zss/mapping/number'
import { ispresent } from 'zss/mapping/types'
import { memoryrun } from 'zss/memory'
import { boardelementwritestat } from 'zss/memory/boardelement'
import {
bookboardobjectnamedlookupdelete,
bookboardobjectsafedelete,
Expand Down Expand Up @@ -41,11 +41,12 @@ export const LIFECYCLE_FIRMWARE = createfirmware()
return 0
})
.command('cycle', (_, words) => {
// read cycle
const [cyclevalue] = readargs(words, 0, [ARG_TYPE.NUMBER])
// write cycle
const cycle = clamp(Math.round(cyclevalue), 1, 255)
boardelementwritestat(READ_CONTEXT.element, 'cycle', cycle)
if (ispresent(READ_CONTEXT.element)) {
// read cycle
const [cyclevalue] = readargs(words, 0, [ARG_TYPE.NUMBER])
// write cycle
READ_CONTEXT.element.cycle = clamp(Math.round(cyclevalue), 1, 255)
}
return 0
})
.command('die', (chip) => {
Expand Down
48 changes: 30 additions & 18 deletions zss/lang/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CodeWithSourceMap, SourceNode } from 'source-map'
import { deepcopy, ispresent, MAYBE } from 'zss/mapping/types'
import { ispresent, MAYBE } from 'zss/mapping/types'
import { tokenize, MaybeFlag } from 'zss/words/textformat'
import { NAME } from 'zss/words/types'

Expand Down Expand Up @@ -181,7 +181,7 @@ function writegoto(ast: CodeNode, line: number): SourceNode {
}

function readlookup(id: MAYBE<string>) {
return context.linelookup[id ?? ''] ?? 0
return context.linelookup[id ?? ''] ?? -1
}

function writelookup(lines: CodeNode[], type: NODE, value: string) {
Expand Down Expand Up @@ -321,35 +321,42 @@ function transformNode(ast: CodeNode): SourceNode {
])
// core / structure
case NODE.IF: {
const block = ast.block.type === NODE.IF_BLOCK ? ast.block : undefined
if (!ispresent(block)) {
return write(ast, '')
}
const block = ast.block?.type === NODE.IF_BLOCK ? ast.block : undefined
if (ispresent(block)) {
// check if conditional
writelookup([ast.check], NODE.IF_CHECK, block.skip)
const source = write(ast, transformNode(ast.check))

// check if conditional
writelookup([ast.check], NODE.IF_CHECK, block.skip)
const source = write(ast, transformNode(ast.check))
// if true logic
block.lines.forEach((item) => source.add(transformNode(item)))

// if true logic
block.lines.forEach((item) => source.add(transformNode(item)))
// start of (alt) logic
writelookup(block.altlines, NODE.ELSE_IF, block.done)
block.altlines.forEach((item) => source.add(transformNode(item)))

// start of (alt) logic
writelookup(block.altlines, NODE.ELSE_IF, block.done)
block.altlines.forEach((item) => source.add(transformNode(item)))
// all done
return source
}

// all done
return source
// check if conditional only inline nested commands only
return write(ast, transformNode(ast.check))
}
case NODE.IF_CHECK: {
const skip = readlookup(ast.skip)
const source = write(ast, [
if (skip === -1) {
return write(ast, [
` `,
writeApi(ast, ast.method, transformNodes(ast.words)),
`;\n`,
])
}
return write(ast, [
` if (!`,
writeApi(ast, ast.method, transformNodes(ast.words)),
`) { `,
writegoto(ast, skip),
` }\n`,
])
return source
}
case NODE.ELSE_IF:
case NODE.ELSE: {
Expand Down Expand Up @@ -461,6 +468,11 @@ function transformNode(ast: CodeNode): SourceNode {
}

function indexnode(ast: CodeNode) {
// bail on blank nodes
if (!ispresent(ast)) {
return
}

// inc line
if (ast.type === NODE.LINE) {
++context.lineindex
Expand Down
36 changes: 1 addition & 35 deletions zss/memory/boardelement.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { objectKeys } from 'ts-extras'
import { createsid } from 'zss/mapping/guid'
import { ispresent, MAYBE } from 'zss/mapping/types'
import { WORD } from 'zss/words/types'
import { MAYBE } from 'zss/mapping/types'

import {
FORMAT_OBJECT,
Expand Down Expand Up @@ -65,35 +63,3 @@ export function importboardelement(
): MAYBE<BOARD_ELEMENT> {
return unformatobject(boardelemententry, BOARD_ELEMENT_KEYS)
}

export function boardelementreadstat(
boardelement: MAYBE<BOARD_ELEMENT>,
key: keyof BOARD_ELEMENT,
defaultvalue: WORD,
): WORD {
if (!ispresent(boardelement)) {
return
}
const value = boardelement[key]
return value ?? defaultvalue
}

export function boardelementwritestat(
boardelement: MAYBE<BOARD_ELEMENT>,
key: keyof BOARD_ELEMENT,
value: WORD,
) {
if (!ispresent(boardelement)) {
return
}
boardelement[key] = value
}

export function boardelementwritestats(
boardelement: MAYBE<BOARD_ELEMENT>,
stats: BOARD_ELEMENT,
) {
objectKeys(stats).forEach((key) =>
boardelementwritestat(boardelement, key, stats[key]),
)
}
1 change: 0 additions & 1 deletion zss/memory/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
import {
codepagereaddata,
codepagereadname,
codepagereadstatdefaults,
codepagereadtype,
exportcodepage,
importcodepage,
Expand Down
12 changes: 2 additions & 10 deletions zss/memory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
boardobjectcreatefromkind,
boardobjectread,
} from './board'
import { boardelementreadstat } from './boardelement'
import {
bookboardobjectnamedlookupdelete,
bookboardtick,
Expand Down Expand Up @@ -406,15 +405,8 @@ export function memorytickobject(
READ_CONTEXT.isplayer = ispid(object.id ?? '')

// read cycle
const cycle = boardelementreadstat(
object,
'cycle',
boardelementreadstat(
bookelementkindread(book, object),
'cycle',
cycledefault,
),
)
const kinddata = bookelementkindread(book, object)
const cycle = object.cycle ?? kinddata?.cycle ?? cycledefault

// run chip code
const id = object.id ?? ''
Expand Down
6 changes: 3 additions & 3 deletions zss/words/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ export function readexpr(index: number, stringeval = true): [any, number] {

// check for flag
if (stringeval) {
const maybeflag = READ_CONTEXT.get?.(maybevalue)
if (ispresent(maybeflag)) {
return [maybeflag, index + 1]
const maybeflagfromvalue = READ_CONTEXT.get?.(maybevalue)
if (ispresent(maybeflagfromvalue)) {
return [maybeflagfromvalue, index + 1]
}
}

Expand Down
2 changes: 1 addition & 1 deletion zss/words/textformat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function writetextformat(tokens: IToken[], context: WRITE_TEXT_CONTEXT) {
const maybename = colorconsts[tokenname]
const maybecolor = colortofg(COLOR[maybename])
const maybebg = colortobg(COLOR[maybename])
if (maybename === 'CLEAR') {
if (maybename === 'ONCLEAR') {
// reset colors
context.active.color = context.reset.color
context.active.bg = context.reset.bg
Expand Down

0 comments on commit 25d1de9

Please sign in to comment.