two-bit/memory.ts

25 lines
675 B
TypeScript

import { Word, emptyWord } from "./word"
export interface Memory<Width extends number> {
value: () => Word<Width>
set: (word: Word<Width>) => void
}
export function memory<Width extends number>(initialValue: Word<Width>): Memory<Width>
export function memory<Width extends number>(width: Width): Memory<Width>
export function memory<Width extends number>(valueOrWidth: Width | Word<Width>): Memory<Width> {
let word = typeof valueOrWidth === 'number' ? emptyWord(valueOrWidth) : valueOrWidth
function set(newWord: Word<Width>) {
word = newWord
}
function value() {
return word
}
return {
value,
set
}
}