0
\$\begingroup\$

I am trying to implement the Dis language as a webpage in order to reproduce Dis programs for code golfing easily on browser.

Since a famous online environment tio.run is not accepting new languages, I decided to implement it by myself.

I am publishing just a JavaScript, but am not sure what the appropriate style of accepting inputs from browser (I am thinking a sequence of octets as a hexadecimal or UTF-8 or xargs -n 1 printf-strinf for I/O).

Here's some of my dis.js:

class DisVm{
  // ...
  
  /*
   * run(). For up to given steps.
   * Automatically halts when isHalt().
   * @steps: integer.
   * Destructive!
   * Returns this.
  **/
  run(steps=MAX_STEPS){
    if(!Number.isInteger(steps)){
      throw new TypeError(
        '@steps not an integer');
    }
    if(steps<MIN_STEPS||steps>MAX_STEPS){
      throw new RangeError(
        '@steps out of range: must be in '+
        `[${MIN_STEPS}, ${MAX_STEPS}]`);
    }
    
    // Now I can run.
    const vm=this;
    let mem=vm.memory;
    let a=vm.registers.A;
    let c=vm.registers.C;
    let d=vm.registers.D;
    let input=(
      Array.isArray(vm.input)?vm.input:[]);
    let output=(
      Array.isArray(vm.output)?vm.output:[]);
    const getVm=()=>{
      this.memory=mem;
      this.register={A:a,C:c,D:d};
      return this;
    }
    for(;steps>0;steps-=1){
      // TODO. randomized; necessary?
      if(
        Number.isInt(mem[c])&&
        0<=mem[c]&&mem[c]<=this.maxValue
      )
        ;
      else{
        mem[c]=(
          Math.floor(
            Math.random()*this.mod));
      }
      // end TODO.
      const cmd=mem[c];
      if(cmd==33)
        return getVm();
      if(cmd==42)
        d=mem[d];
      if(cmd==62){
        const v=mem[d]||0;
        a=mem[d]=(
          Math.floor(v/3)+
          v%3*(this.mod/3));
      }
      if(cmd==94)
        c=mem[d];
      if(cmd==123)
        if(a==this.maxValue)
          return getVm();
        else
          output.push(a);
      if(cmd==124){
        let b=mem[d]||0;
        let r=0;
        for(let i=0;i<this.trits;i++){
          r=r*3+(a%3-b%3+3)%3
          a=Math.floor(a/3);
          b=Math.floor(a/3);
        }
        a=mem[d]=r;
      }
      if(cmd==125){
        if(!input.hasOwnProperty(0))
          a=this.maxValue;
        else{
          let v=input.shift();
          if(!Number.isInteger(v))
            v=0;
          a=(
            v<0||v>this.maxValue?0:v);
        }
      }
      c=(c+1)%this.mod;
      d=(d+1)%this.mod;
    }
    
    return getVm();
  }
}

What I've just get it while making this post

  • I am missing to clarify some constants such as MAX_STEPS are defined within the class.

What to implement

  • Implement it securely.
    • This is why run() accepts steps to run.

What I am not sure

  • I am not sure if it's appropriate if run() and some methods that I documented destructive! should return this or not, or the class should be immutable at all. I think immutable objects are slow.
  • How strict should the code be for types of arguments?
  • Also what I should ask here.
\$\endgroup\$
2
  • \$\begingroup\$ Is the code working the way you expect it too? \$\endgroup\$
    – pacmaninbw
    Nov 10 at 13:40
  • \$\begingroup\$ This is probably not a good fit. Can you provide code (with a reduced dis.js if needed) that calls this VM and perhaps performs printing 'Hello World' or something? In this state, it is super hard to review and while interesting I would probably vote to close. \$\endgroup\$
    – konijn
    Nov 10 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.