Invalid keyword at beginning of an expression(lolcode) - lolcode

I just started using LOLCODE. I tried making a program that tests if a input is a NUMBR but I get the error Invalid keyword at beginning of an expression on line 13 (IM OUTTA YR LOOP).
Hese is my code. Please help. Thanks.
I HAS A VARIABLE
I HAS A ITERATOR ITZ 0
I HAS A TRUTH
IM IN YR LOOP UPPIN YR ITERATOR WILE DIFFRINT TRUTH AN "WIN"
VISIBLE "GIMMEH NUMBR"
GIMMEH VARIABLE
VARIABLE IS NOW A NUMBR, O RLY?
YA RLY
I HAS A TRUTH ITZ "FAIL"
GTFO
NO WAI
VISIBLE "HOW U FAIL SIMPL INSTUCTINS"
IM OUTTA YR LOOP

You're missing OIC at the end of your if statement.
VARIABLE IS NOW A NUMBR, O RLY?
YA RLY
I HAS A TRUTH ITZ "FAIL"
GTFO
NO WAI
VISIBLE "HOW U FAIL SIMPL INSTUCTINS"
OIC
See : https://github.com/justinmeza/lolcode-spec/blob/master/v1.2/lolcode-spec-v1.2.md#if-then

Related

FCEUX stops responding when i run this code made by me

--llama a IUP
require("iuplua")
--Variables
on= 1
puntos1=memory.readbyte(0x0007DE) --Read the points value
puntos2=memory.readbyte(0x0007DF)
puntos3=memory.readbyte(0x0007E0)
puntos4=memory.readbyte(0x0007E1)
puntos5=memory.readbyte(0x0007E2)
p1=puntos1*100000 --Convert the raw values to it's game value
p2=puntos2*10000
p3=puntos3*1000
p4=puntos4*100
p5=puntos5*10
maxpuntaje=p1+p2+p3+p4+p5 --Calculate the final result
mundo=memory.readbyte(0x00075F) --Read the "world" value
nivel=memory.readbyte(0x000760) --Read the "level" value
estado=memory.readbyte(0x000770) --Read the mario "state" (00 not in game, 01 playing, 03 game over)
--Escribir las variables anteriores al morir
memory.writebyte(0x00075A,00) --Change the "lives" value to 1
while(on==1) do
estado=memory.readbyte(0x000770)
print(estado)
print(type(estado))
if(estado==03)then
print("Puntuacion maxima (sesion actual) = ",maxpuntaje)
print("Mundo y nivel Actual: ",mundo+1,"-",nivel+1)
end
end
(code edited)
This is the state of 0x000770 when playing
This is the state of 0x000770 when the Game Over scene jumps in
is there anything wrong? fceux just stop responding when i run this script, im new at stacks overflow and at programming so, every help will be welcome
It's rather simple to debug this but more information is required
Make sure (estado==03) yields true and also print following variables and add into your question
mundo=memory.readbyte(0x00075F)
nivel=memory.readbyte(0x000760)
estado=memory.readbyte(0x000770)
print(mundo, nivel, estado)
Check the types of these memory read vaiables i-g print(type(mundo))
Make Sure that the if condition checks against correct types
You realize that memonry is read once and while is always true
Try this:
while(on==1) do
estado=memory.readbyte(0x000770)
print(estado)
print(type(estado))
if(estado==03)then
print("Puntuacion maxima (sesion actual) = ",maxpuntaje)
print("Mundo y nivel Actual: ",mundo+1,"-",nivel+1)
end
end
im so sorry for wasting your time, but i found the error, it was actually quite simple, there was a missing FCEU.frameadvance at the end of the cycle.
i dont know why it caused the emulator to crash but after putting it in place it stopped crashing. thanks to wsha for helping.

Definition of f(x) in Lua documentation

While trying to completely understand the solution to Lua - generate sequence of numbers, the section 4.3.4 of Programming in Lua is unclear:
for i=1,f(x) do print(i) end
for i=10,1,-1 do print(i) end
The for loop has some subtleties that you should learn in order to
make good use of it. First, all three expressions are evaluated once,
before the loop starts. For instance, in the first example, f(x) is
called only once. Second, the control variable is a local variable
automatically declared by the for statement and is visible only inside
the loop. [...]
The first line of code doesn't work of course.
What is f(x) and where is it defined?
Unfortunately the documentation isn't available as a single page, making it a huge effort to search for the first occurrence. Searching for "lua f(x)" doesn't bear fruit either.
Explanation: now that I have received answers, I realize the problem was a misunderstanding. I incorrectly interpreted "f(x) is called only once" as "the line containing f(x) - for i=1,f(x) do print(i) end - will only return one value" and didn't pay enough attention to "all three expressions are evaluated once, before the loop starts".
This sentence clarifies it: expressions are evaluated once, before the loop starts.
Thus, f(x) is called only once is merely stating that the expressions will not be affected by potential changes in the loop.
For example, the following code (expressions are i=1 and x in the second line):
x=5
for i=1,x do
x = x - 1
print(i, x)
end
print(x)
will produce the following output:
1 4
2 3
3 2
4 1
5 0
0
and will not produce the following output:
1 4
2 3
3 2
2
f(x) is just a function which takes the argument x and returns a value that is used as the upper bound for the loop.
So for example, if the function f(x) calculates x² and you call it as f(3), it would return the value of 9. The resulting for loop would look like this:
for i=1, f(3) do print(i) end
which is exactly the same as
for i=1, 9 do print(i) end

Redis Lua Doesn't Recognize Inf?

I'm writing a Lua script in Redis, and have one line that's breaking everything:
local to_remove = redis.call('ZRANGE', KEYS[1], -5, "+inf")
Returns:
redis.exceptions.ResponseError: Error running script (call to f_f1d95d2e103f00220a476f0ef2a2abc798682c55): ERR value is not an integer or out of range
This goes away completely if I replace "+inf" with any number. I've tried "inf" and "-inf" as well, and none of these work. Ideas?
Nevermind. I'm an idiot. Should have just used "-1" in place of "inf." ZRANGE just doesn't support inf, and -1 gives the same results I would have expected from inf. Hopefully this helps someone trying to do the same later on.

Lua - Couple Questions

Im a amatuer at coding. So, mind me if i face palmed some things.
Anyways, im making a alpha phase for a OS im making right? I'm making my installer. Two questions. Can i get a code off of pastebin then have my lua script download it? Two. I put the "print" part of the code in cmd. I get "Illegal characters". I dont know what went wrong. Here's my code.
--Variables
Yes = True
No = False
--Loading Screen
print ("1")
sleep(0.5)
print("2")
sleep(0.5)
print("Dowloading OS")
sleep(2)
print("Done!")
sleep(0.2)
print("Would you like to open the OS?")
end
I see a few issues with your code.
First of all, True and False are both meaningless names - which, unless you have assigned something to them earlier, are both equal to nil. Therefore, your Yes and No variables are both set to nil as well. This isn't because true and false don't exist in lua - they're just in lowercase: true and false. Creating Yes and No variables is redundant and hard to read - just use true and false directly.
Second of all, if you're using standard lua downloaded from their website, sleep is not a valid function (although it is in the Roblox version of Lua, or so I've heard). Like uppercase True and False, sleep is nil by default, so calling it won't work. Depending on what you're running this on, you'll want to use either os.execute("sleep " .. number_of_seconds) if you're on a mac, or os.execute("timeout /t " .. number_of_seconds) if you're on a PC. You might want to wrap these up into a function
function my_sleep_mac(number_of_seconds)
os.execute("sleep " .. number_of_seconds)
end
function my_sleep_PC(number_of_seconds)
os.execute("timeout /t " .. number_of_seconds)
end
As for the specific error you're experiencing, I think it's due to your end statement as the end of your program. end in lua doesn't do exactly what you think it does - it doesn't specify the end of the program. Lua can figure out where the program ends just by looking to see if there's any text left in the file. What it can't figure out without you saying it is where various sub-blocks of code end, IE the branches of if statements, functions, etc. For example, suppose you write the code
print("checking x...")
if x == 2 then
print("x is 2")
print("Isn't it awesome that x is 2?")
print("x was checked")
lua has no way of knowing whether or not that last statement, printing the x was checked, is supposed to only happen if x is 2 or always. Consequently, you need to explicitly say when various sections of code end, for which you use end. For a file, though, it's unnecessary and actually causes an error. Here's the if statement with an end introduced
print("checking x...")
if x == 2 then
print("x is 2")
print("isn't it awesome that x is 2?")
end
print("x was checked")
although lua doesn't care, it's a very good idea to indent these sections of code so that you can tell at a glance where it starts and ends:
print("checking x...")
if x == 2 then
print("x is 2")
print("isn't it awesome that x is 2?")
end
print("x was checked")
with regards to your "pastebin" problem, you're going to have to be more specific.
You can implement sleep in OS-independent (but CPU-intensive) way:
local function sleep(seconds)
local t0 = os.clock()
repeat
until os.clock() - t0 >= seconds
end

Prolog Code Example: Unification

From an old final for my class:
Here is some prolog code:
mystery(1, 1).
mystery(N, F) :-
N1 is N-1,
mystery(N1,F1),
F is F1*N.
Question 1: What value is unified with P in
mystery(3, P).
Question 2: If a semicolon is pressed after Prolog produces and answer for mystery, and interpreter will eventually report "ERROR: Out of local stack". Why dies this occur, and how could you modify mystery to avoid the error?
Question 1:
I get
P = 6 ?
Question 2:
If I press semi-colon to get all answers, I get an out of local stack error.
I'm not sure what this code is trying to accomplish or how to fix it so I don't go out of local stack. Any ideas?
The out of stack error is probably because, when you get down to mystery(1, F1) which resolves to mystery(1, 1) and ask for more solutions, you hit the next clause, which calls mystery(0, F1). At this point, it tries to find a solution, but the only clause that matches calls for mystery(-1, F1), and that calls for mystery(-2, F1) and so forth. This means that there's matches called for until either the first number wraps around to 1 or you get a stack overflow, since each pending match takes stack space.
#David Thornley already explained why searching for more answers blows up your stack. You could fix it by using a cut operator:
mystery(1, F) :- !, F is 1.
mystery(N, F) :-
N1 is N-1,
mystery(N1,F1),
F is F1*N.
The cut here makes it so that if the first argument is 1, the second rule may not be applied.

Resources