I am attempting to learn the Nim language by making a small and simplistic register machine. It is intended to interpret some basic instructions.
The entire parsing lexing process consists of splitting the inputted code by spaces (not whitespace in general), which newlines are counted as. Is this reasonable code style in Nim?
The code reads a file name containing the instructions from stdin and then executes those instructions on the seq.
I am a beginner at programming in general, not just Nim. Also, my very basic knowledge of Turing completeness suggests that it might be Turing complete. Is that so?
The instructions are as follows:
set+ ADDRESS: int, VALUE: int
, which sets the value of ADDRESS to the previous value of ADDRESS plus the value.set- ADDRESS: int, VALUE: int
, which sets the value of ADDRESS to the previous value of ADDRESS minus the value.mul ADDRESS1: int, ADDRESS2: int ADDRESS3: int
, which sets ADDRESS3's value to ADDRESS1's * ADDRESS2'sadd ADDRESS1: int, ADDRESS2: int ADDRESS3: int
, which sets ADDRESS3's value to ADDRESS1's + ADDRESS2'ssub ADDRESS1: int, ADDRESS2: int ADDRESS3: int
, which sets ADDRESS3's value to ADDRESS1's - ADDRESS2'sdiv ADDRESS1: int, ADDRESS2: int ADDRESS3: int
, which sets ADDRESS3's value to ADDRESS1's // ADDRESS2'sif+ ADDRESS1: int, ADDRESS2: int, JUMPLEN: int ,EQUALORUNEQUAL: ! or =
, which adds jumplen to code pointer if ADDRESS1 and ADDRESS2 are equal or unequal.if- ADDRESS1: int, ADDRESS2: int JUMPLEN: int, EQUALORUNEQUAL: ! or =
, which subtracts jumplen from code pointer if ADDRESS1 and ADDRESS2 are equal or unequal.inp ADDRESS: int
which takes one integer input and sets ADDRESS to it.echo ADDRESS: int
which echoes the value of ADDRESSswap ADDRESS1: int ADDRESS2: int
which swaps the values of ADDRESS1 and ADDRESS2
import strutils
var mem: seq[int]
for i in 0..30000:
mem.add(0)
var code = readFile(readLine(stdin))
var codeptr = 0
for i in 0..len(code) - 1:
if code[i] == '\n':
code[i] = ' '
var codes = code.split(" ")
var done: bool
var pyqt = false
while codeptr < len(codes):
done = false
pyqt = false
if codes[codeptr] == "set+" and done == false:
var IP = codes[codeptr + 1].parseInt()
mem[IP] = mem[IP] + codes[codeptr + 2].parseInt()
inc(codeptr, 2)
done = true
if codes[codeptr] == "set-" and done == false:
var IP1 = codes[codeptr + 1].parseInt()
mem[IP1] = mem[IP1] - codes[codeptr + 2].parseInt()
inc(codeptr, 2)
done = true
if codes[codeptr] == "add" and done == false:
var IP2 = codes[codeptr + 1].parseInt()
var IP3 = codes[codeptr + 2].parseInt()
var IP4 = codes[codeptr + 3].parseInt()
mem[IP4] = mem[IP2] + mem[IP3]
inc(codeptr, 2)
done = true
if codes[codeptr] == "sub" and done == false:
var IP5 = codes[codeptr + 1].parseInt()
var IP6 = codes[codeptr + 2].parseInt()
var IP7 = codes[codeptr + 3].parseInt()
mem[IP7] = mem[IP5] - mem[IP6]
inc(codeptr, 2)
done = true
if codes[codeptr] == "mul" and done == false:
var IP8 = codes[codeptr + 1].parseInt()
var IP9 = codes[codeptr + 2].parseInt()
var IP10 = codes[codeptr + 3].parseInt()
mem[IP10] = mem[IP8] * mem[IP9]
inc(codeptr, 2)
done = true
if codes[codeptr] == "div" and done == false:
var IP11 = codes[codeptr + 1].parseInt()
var IP12 = codes[codeptr + 2].parseInt()
var IP13 = codes[codeptr + 3].parseInt()
mem[IP13] = int(mem[IP11] / mem[IP12])
inc(codeptr, 2)
done = true
if codes[codeptr] == "if-" and done == false:
var IP14 = codes[codeptr + 1].parseInt()
var IP15 = codes[codeptr + 2].parseInt()
var IP16 = codes[codeptr + 3].parseInt()
var IP17 = codes[codeptr + 4]
if IP17 == "=":
if mem[IP14] == mem[IP15]:
codeptr = codeptr - IP16
pyqt = true
if IP17 == "!":
if mem[IP14] != mem[IP15]:
codeptr = codeptr - IP16
pyqt = true
done = true
if codes[codeptr] == "if+" and done == false:
var IP14 = codes[codeptr + 1].parseInt()
var IP15 = codes[codeptr + 2].parseInt()
var IP16 = codes[codeptr + 3].parseInt()
var IP17 = codes[codeptr + 4]
if IP17 == "=":
if mem[IP14] == mem[IP15]:
codeptr = codeptr + IP16
pyqt = true
if IP17 == "!":
if mem[IP14] != mem[IP15]:
codeptr = codeptr + IP16
pyqt = true
done = true
if codes[codeptr] == "inp" and done == false:
var IP18 = codes[codeptr + 1].parseInt()
mem[IP18] = readLine(stdin).parseInt()
inc(codeptr, 1)
done = true
if codes[codeptr] == "echo" and done == false:
var IP19 = codes[codeptr + 1].parseInt()
echo mem[IP19]
inc(codeptr, 1)
done = true
if codes[codeptr] == "swap" and done == false:
var IP20 = codes[codeptr + 1].parseInt()
var IP21 = codes[codeptr + 2].parseInt()
var temp = mem[IP21]
mem[IP21] = mem[IP20]
mem[IP20] = temp
inc(codeptr, 2)
done = true
if codes[codeptr] == "cp" and done == false:
var IP22 = codes[codeptr + 1].parseInt()
var IP23 = codes[codeptr + 2].parseInt()
mem[IP23] = mem[IP22]
inc(codeptr, 2)
done = true
if pyqt == false:
codeptr = codeptr + 1