Pine throws syntax error on first line of for loop - syntax-error

I have a pine strategy:
//#version=5
strategy("Elie's strategy", overlay=true, margin_long=100, margin_short=100)
var float[] overSRLs = na
var IDCounter = 0
int[] removeFromOver = na
for int i = 0 to (array.size(overSRLs) - 1) // line 8
if low < array.get(overSRLs, i)
orderID = str.tostring(IDCounter)
exitID = str.tostring(IDCounter + 1)
IDCounter += 2
strategy.order(orderID, strategy.long, 1)
strategy.exit(exitID, from_entry=orderID, stop = (array.get(overSRLs, i) * (1 - ATR5[1])), limit = (array.get(overSRLs, i) * (1 + ATR5[1]))
array.push(removeFromOver, i)
I know it's not much of a strategy, but I cut out the irrelevant parts to make a smaller reproductible example. When just the code above is saved, it throws the following error:
line 8: Syntax error at input 'int'.
Now, even though line 8 is the init of the for, I think the problem is in the code block in the for, and the compiler/interpreter just has bad error handling. Is there something I'm missing here? Everything looks fine to me

I think the problem is in the code block in the for, and the compiler/interpreter just has bad error handling.
Yes, the issue is in the strategy.exit -- you need one extra closing bracket in the end there. Compiler does not always provide correct lines for errors in loops. Comment out the loop and move everything one tab to the left and you'll see that the new error is line 14: Syntax error at input '('., which indicates that there is an issue with the opening bracket (because it can't find its pair).
P.S. The script will not compile after that because of the undeclared ATR5 variable, but I assume it's due to the fact that you trimmed the example code for readability.

Related

Adding 1 to a byte in Structured Text

I'm trying to implement the logic below:
if bcc = STX or
bcc = CR, then bcc := +1 (increment
of 1).
bcc is a byte and i'm trying to increment it by 1 if this condition above is true.
my code is:
IF message_byte[11] = 16#0D OR message_byte[11] = 16#02 THEN
message_byte[11] := message_byte[11] + TO_BYTE(1);
END_IF
where message_byte is an array of bytes and I want to access a specific one. However, it gives me an error when saying it can't be added.
any help is appreciated
I believe the error is telling you that message_byte[11] (left operant of your addition) is not a type BYTE, so the compiler can’t figure out how to add a BYTE to it - as commented by dwpessoa. I also don’t know what a data type “BYTES” is - maybe that should just be BYTE.
When you get these types of errors, try breaking apart your task and just try the absolute basics - just try adding 1 to message_byte[11]. Then it will be easier to solve the error.

Segmentation fault in file I/O, cannot figure out

Basically, I have rewritten code that kept giving me a segmentation fault (core dump) error when running, and I decided to check each step to rule out issues.
My code works, until I try accessing/using the last line of the input files data. I do the same things to this line as to the line previous, but it's suggesting somethings wrong.
Here is my code for the file I/O and data handling:
The input file itself is simply:
20 20
10 10 u
5 5 d
In line 27, you dereference the uninitialised playerDirInput, which is undefined behaviour:
playerDir = (char)playerDirInput[0];
That's probably the cause of your crash. If that code block is meant to mirror the following one, it looks like you just haven't read the third item on that line, which is where playerDirInput is probably meant to come from. That would be something like:
fgets(line, 8, f);
playerRowChar = strtok(line, " ");
playerRow = atoi(playerRowChar);
playerColChar = strtok(NULL, " "); // <- fixed this, see below.
playerCol = atoi(playerColChar);
playerDirInput = strtok(NULL, " "); // <- add this.
playerDir = (char)playerDirInput[0];
However, I would suggest you instead opt for the simpler sscanf version, which would go something like (including a check to ensure you get the three items):
fgets(line, 8, f);
if (sscanf(line, "%d %d %c", &playerRow, &playerCol, &playerDir) != 3) {
handleErrorIntelligently();
}
I tend to prefer fgets followed by sscanf, rather than fscanf. The latter can fail in such a way that you're not sure where the input stream pointer ends up as. With fgets, you always know you've read a line (or can easily detect that you read a partial line and adjust for it).
Other potential problems you should look at:
On line 25, this strtok should be of the NULL type, not line. The latter will simply re-read the first item on that line wheras you want the next item.
You really should check functions that can return problematic values (such as NULL from strtok). Otherwise, using them can cause issues. That depends on the data you're reading, of course, so may not necessarily be a problem if you control that.

Why am I getting this Mathematica error - "Syntax: Incomplete expression more input is needed"

I'm running this on Wolfram cloud. Here's the code
ClearAll[s,l,L,L1,M1,Mt,m,M,vym,vxm,wp,J,Meff,theta,omega]
Rr = {{Cos[theta],-Sin[theta],0},{Sin[theta],Cos[theta],0},{0,0,1}};
Wm = {0,0,omega};
This is obviously part of a larger code, but just this much is enough to create the error. If I remove any line, the error disappears. I can't for the life of me figure out where a syntax error could be in here!

Self-Made BCD Class - Multiplying BCDs Error

I am making a BCD class as an exercise for school, and am encountering some issues. Below is my BCD class.
My problem is with the multiplyBCDs method.
It works fine with smaller numbers such as 4,329 * 4, however, with larger products, such as the product of 4,329 and 29,385, I receive a NullPointerException error at the first line of my addBCDs method:
int[] added = new int[other.numberOfDigits()];
I have tried retracing the problem and could not find the issue. Why am I receiving this error and how could I fix it?
Thanks for the help!
In the method:
public BCD multiplyBy(int num)
In the last else statement, the following condition is never met:
if (x == digits.length - 1 && carry != 0)
and so "ans" is never set and remains null.
int[] added = new int[other.numberOfDigits()];
The only way you can get an NPE on that line is if other is null.

Why can't you use a object property as an index in a for loop? (MATLAB)

Below is an example that doesn't work in Matlab because obj.yo is used as the for loop's index. You can just convert this to the equivalent while loop and it works fine, so why won't Matlab let this code run?
classdef iter_test
properties
yo = 1;
end
methods
function obj = iter_test
end
function run(obj)
for obj.yo = 1:10
disp('yo');
end
end
end
end
Foreword: You shouldn't expect too much from Matlab's oop capabilities. Even though things have gotten better with matlab > 2008a, compared to a real programming language, oop support in Matlab is very poor.
From my experience, Mathworks is trying to protect the user as much as possible from doing mistakes. This sometimes also means that they are restricting the possibilities.
Looking at your example I believe that exactly the same is happening.
Possible Answer: Since Matlab doesn't have any explicit typing (variables / parameters are getting typed on the fly), your code might run into problems. Imagine:
$ a = iter_test()
% a.yo is set to 1
% let's overwrite 'yo'
$ a.yo = struct('somefield', [], 'second_field', []);
% a.yo is now a struct
The following code will therefore fail:
$ for a.yo
disp('hey');
end
I bet that if matlab would support typing of parameters / variables, your code would work just fine. However, since you can assign a completely different data type to a parameter / variable after initialization, the compiler doesn't allow you to do what you want to do because you might run into trouble.
From help
"properties are like fields of a struct object."
Hence, you can use a property to read/write to it. But not use it as variable like you are trying to do. When you write
for obj.yo = 1:10
disp('yo');
end
then obj.yo is being used as a variable, not a field name.
compare to actual struct usage to make it more clear:
EDU>> s = struct('id',10)
for s.id=1:10
disp('hi')
end
s =
id: 10
??? for s.id=1:10
|
Error: Unexpected MATLAB operator.
However, one can 'set' the struct field to new value
EDU>> s.id=4
s =
id: 4
compare the above error to what you got:
??? Error using ==> iter_test
Error: File: iter_test.m Line: 9 Column: 20
Unexpected MATLAB operator.
Therefore, I do not think what you are trying to do is possible.
The error is
??? Error: File: iter_test.m Line: 9 Column: 20
Unexpected MATLAB operator.
Means that the MATLAB parser doesn't understand it. I'll leave it to you to decide whether it's a bug or deliberate. Raise it with TMW Technical Support.
EDIT: This also occurs for all other kinds of subscripting:
The following all fail to parse:
a = [0 1];
for a(1) = 1:10, end
a = {0 1};
for a{1} = 1:10, end
a = struct('a', 0, 'b', 0);
for a.a = 1:10, end
It's an issue with the MATLAB parser. Raise it with Mathworks.