Skip to content
Snippets Groups Projects
Select Git revision
  • leo
  • dex
  • pendulum
  • master default protected
  • apfelstruder
  • littlerascal
6 results

typeset.js

Blame
  • typeset.js 19.46 KiB
    // typeset: functional types -> bytes for js -> embedded sys
    // bless up @ modern js https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
    
    let tsdebug = false
    
    // oy,
    
    const checkKey = (type, arr, start) => {
      if (arr[start] !== type.key) {
        console.log('erroneous bytes')
        console.log(arr)
        throw new Error(`mismatched key on phy read: for ${type.name}, find ${arr[start]} instead of ${type.key} ... at ${start} index`)
      }
    }
    
    const checkBoolean = (value) => {
      if (typeof value !== 'boolean')
        throw new Error('cannot cast non-boolean to bool at phy')
    }
    
    const checkNumber = (value) => {
      if (typeof value !== 'number')
        throw new Error(`cannot cast non-number into physical world "${value}", ${typeof value}`)
    }
    
    const checkString = (value) => {
      if (typeof value !== 'string')
        throw new Error(`cannot cast non-string to string at phy! "${value}", "${typeof value}"`)
    }
    
    const checkArray = (thing) => {
      if (!Array.isArray(thing))
        throw new Error('this thing is not an array!')
    }
    
    const checkUnsigned = (value, bits) => {
      if (value > Math.pow(2, bits))
        throw new Error('value out of byte bounds')
    }
    
    const checkSigned = (value, bits) => {
      let comparator = Math.pow(2, bits - 1)
      if (value > comparator || value < -comparator)
        throw new Error('value out of byte bounds')
    }
    
    const findPhy = (type) => {
      let phy = TSET.find((cand) => {
        return cand.name === type
      })
      if (phy === undefined)
        // try this... catch err elsewhere
        throw new Error(`could not find phy for datatype: ${type}`)
      return phy
    }
    
    const bounds = (value, min, max) => {
      return Math.max(min, Math.min(value, max))
    }
    
    const intBounds = (pfloat, min, max) => {
      return Math.round(bounds(pfloat, min, max))
    }
    
    // TYPES:
    /*
    name: string identifier, how we 'specify' what an input / output / state is
    key: byte-code for the network. if this, write, and read, don't exist, link can't have one
    write: serialize into bytes
    read: deserialize