lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


--[==[

Hi!

As you can see below, I'm trying to use strsub(string, i) in a little program that is an attempt at building a very simple vm on top of Lua 5.3:.

This is the output that I get:

j10 m20
false is not equal to 'true.'
m20 false
push30 m30 executed.
x35 print.lua
j40 m50
true is equal to 'true.'
lua53: main.lua:34: attempt to call a nil value (local 'strsub')
stack traceback:
        main.lua:34: in main chunk
        [C]: in ?

Does anyone see what I'm doing wrong?

Thanks,

Clint
Lua newbie

]==]--
memory = {}

-- write a simple program to memory
memory["j10"] = "m20" -- Line 10 and 11: If m20 is true, jump to x100
memory["m11"] = "x100"
memory["m20"] = "false"
memory["push30"] = "m30 executed."
memory["x35"] = "print.lua"
memory["j40"] = "m50"
memory["j41"] = "x100"
memory["m50"] = "true"
memory["m55"] = "Why wasn't this line skipped?"
memory["x100"] = "exit.lua"

-- Instruction pointer
ip = 0

-- Execution loop
while ip < 200  do
-- check for the existence of the next instruction in the dictionary. If more than one type of instruction
-- with the same number exists in the dictionary, don't worry about it. Test for that at the assembler level.

 keyString = "j" .. ip
 if memory[keyString] ~= nil then
  print(keyString .. " " .. memory[keyString])
  memoryToTest = memory[keyString]
  if memory[memoryToTest] == "true" then
   print(memory[memoryToTest] .. " is equal to 'true.'")
   ip = ip + 1
   keyString = "m" .. ip
   substring1 = strsub(memory[keyString], 1)
   -- print(strsub("Hi" , 1))
   ip = tonumber(subString1)
   print("ip = " .. ip)
   -- get the number part of the memory indicator, and set ip to it.
  else
   print(memory[memoryToTest] .. " is not equal to 'true.'")
   ip = ip + 2  
  end
 end
 keyString = "m" .. ip
 if memory[keyString] ~= nil then
  print(keyString .. " " .. memory[keyString])
 end
 keyString = "r" .. ip
 if memory[keyString] ~= nil then
  print(keyString .. " " .. memory[keyString])
 end
 keyString = "w" .. ip
 if memory[keyString] ~= nil then
  print(keyString .. " " .. memory[keyString])
 end
 keyString = "pop" .. ip
 if memory[keyString] ~= nil then
  print(keyString .. " " .. memory[keyString])
 end
 keyString = "push" .. ip
 if memory[keyString] ~= nil then
  print(keyString .. " " .. memory[keyString])
 end
 keyString = "x" .. ip
 if memory[keyString] ~= nil then
  print(keyString .. " " .. memory[keyString])
 end

ip = ip + 1

end