This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Robkrafczyk's profile picture

Lua help needed!

Started by Robkrafczyk, 27 November 2014 - 01:19 AM
Robkrafczyk #1
Posted 27 November 2014 - 02:19 AM
Hello CC community!

I am working on making a simple interpreter language supposed to be like a C based language.
I need a syntax that reads a line from a written text file and if it says "print (your text here)" then it will print
the text that the user wrote in the file. So far I have been getting this error and i'm not sure why:

A#:10: attempt to concentrate string and nil


cmd = read()
if cmd=="compile" then
name = read()
local file = fs.open(name, "r")
if file then
  local text = file.readLine()
  if text=="#include <kernel>" then
  local text = file.readLine()
  if text==("print"..text) then
  print(text)
  end
  end
end
end



I hope I can find some help, thankyou for reading!

best,
Rob
valithor #2
Posted 27 November 2014 - 03:26 AM
-snip

This really isnt a answer to your problem, but when you do the if statement for text == ("print"..text) that would never return true. You are litterally checking to see if a variable equals the same variable with a extra word. So assuming that text equals "hi" you are seeing if "hi" == "print hi"

It might help if you include the file, which you are attempting to read. I tested the code which you provided, and it only errored when the only line in the file that it was reading from was the #include <kernel>
Edited on 27 November 2014 - 02:32 AM
Bomb Bloke #3
Posted 27 November 2014 - 03:29 AM
It's saying that on the tenth line of your script, you're attempting to combine a string together with nothing.

Looking at the code you've posted, there's a bit of a mis-match in you're only attempting to concatenate on the ninth line. The string is obviously "print", so "text" must be nil.

This suggests that your file only contains one single line. After you've read that one line, your second attempt to read a line isn't going to "get" anything out of it.

You could prevent the error by first checking whether "text" exists (in much the same way as you checked to see if you got a valid file handle), but then you run into problems in interpreting what "text" contains - I can tell you it'll never contain "print"..text, because if you take any word (or sequence of words) and stick "print" in front of them, the result will never be the same as the original.

Really you'd want to check whether the text starts off with something along the lines of "print(". The problem with that is that there's a whole lot of different ways that line can be written - what if there's some spaces padding it out? What if it's sharing a line with an if/while/for/whatever statement? Extracting the true meaning of the line isn't exactly simple.

And that's not even scratching the surface of what writing an interpreter entails - so I suggest you choose a different project, as this one will likely lead to frustration when you start to realise exactly what you're diving into.

That said, if you really want to keep on with it, then I suggest reading about patterns. You'll need a deep understanding of them.