BETTER LINE | 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 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
}
parseLine_better(`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`)
| ready |
LINE 2 | 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 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)
})
}
parseLine2(`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`)
| ready |
LINE OG | 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 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))
}
parseLine(`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`)
| ready |
BETTER LINE WITH OBJ | const instructions = {
'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 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[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
}
parseLine_better(`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`)
| ready |
LINE OG with OBJ | const instructions = {
'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 parseLine = (line) => {
return line.split(/\s+/g)
.map(token => token.replace(",", "").trim().toLowerCase())
.filter(token => token.length > 0)
.map(token => {
const instruction = instructions[token]
if (instruction) return instruction
if (token.includes("R")) return token.slice(1)
return token
})
.map(token => parseInt(token))
}
parseLine(`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`)
| ready |