LINE FNS (v3)

Revision 3 of this benchmark created on


Setup

const instructions = new Map([
  ['movr', 10],
  ['movv', 11],
  ['add', 20],
  ['sub', 21],
  ['push', 30],
  ['pop', 31],
  ['jp', 40],
  ['jl', 41],
  ['call', 42],
  ['ret', 50],
  ['print', 60],
  ['halt', 255],
])

const instructionsObj = {
  'movr': 10,
  'movv': 11,
  'add': 20,
  'sub': 21,
  'push': 30,
  'pop': 31,
  'jp': 40,
  'jl': 41,
  'call': 42,
  'ret': 50,
  'print': 60,
  'halt': 255,
}


///////////// FNS
const parseLine_better = (line) => {
  const tokens = line.split(/\s+/g)

  const parsed_tokens = []

  for(let token in tokens) {
    const clean_token = token.replace(",", "").trim().toLowerCase()

    if (clean_token.length > 0) {
      const instruction = instructions.get(clean_token)
      let t = clean_token

      if (instruction) t = instruction
      else if (clean_token.includes("R")) t = clean_token.slice(1)
      else parsed_tokens.push(parseInt(t))
    }
  }

  return parsed_tokens
}

const parseLine2 = (line) => {
  return line.split(/\s+/g)
    .map(token => token.replace(",", "").trim().toLowerCase())
    .filter(token => token.length > 0)
    .map(token => {
      const instruction = instructions.get(token)

      if (instruction) return parseInt(instruction)
      if (token.includes("R")) return parseInt(token.slice(1))
      return parseInt(token)
    })
}

const parseLine = (line) => {
  return line.split(/\s+/g)
    .map(token => token.replace(",", "").trim().toLowerCase())
    .filter(token => token.length > 0)
    .map(token => {
      const instruction = instructions.get(token)

      if (instruction) return instruction
      if (token.includes("R")) return token.slice(1)
      return token
    })
    .map(token => parseInt(token))
}

const parseLineOgObj = (line) => {
  return line.split(/\s+/g)
    .map(token => token.replace(",", "").trim().toLowerCase())
    .filter(token => token.length > 0)
    .map(token => {
      const instruction = instructionsObj[token]

      if (instruction) return instruction
      if (token.includes("R")) return token.slice(1)
      return token
    })
    .map(token => parseInt(token))
}


const parseLine_better_obj = (line) => {
  const tokens = line.split(/\s+/g)

  const parsed_tokens = []

  for(let token in tokens) {
    const clean_token = token.replace(",", "").trim().toLowerCase()

    if (clean_token.length > 0) {
      const instruction = instructionsObj[clean_token]
      let t = clean_token

      if (instruction) t = instruction
      else if (clean_token.includes("R")) t = clean_token.slice(1)
      else parsed_tokens.push(parseInt(t))
    }
  }

  return parsed_tokens
}

const asm = `MOVV   R0,   10
CALL  6
HALT  
PUSH   R0
MOVV R0, 0
MOVV R1, 1
MOVV R3, 1
PRINT R1
MOVR R2, R0
ADD R2, R1
PRINT R2
MOVR R0, R1
MOVR R1, R2
MOVV R2, 1
ADD R3, R2
POP R2
PUSH R2
JL R3, R2, 19
POP R0
RET`

Test runner

Ready to run.

Testing in
TestOps/sec
BETTER LINE
parseLine_better(asm)
ready
LINE 2
parseLine2(asm)
ready
LINE OG
parseLine(asm)
ready
BETTER LINE WITH OBJ
parseLine_better_obj(asm)
ready
LINE OG with OBJ
parseLineOgObj(asm)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.