June 26, 2024, 02:58:49 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Syntax error?

Started by Protected, July 19, 2006, 08:48:15 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Protected

I was making this function to return an item from a string that contains a list of items, when I ran into two weird errors.

sub strpart (string source, string sep, int partnum), string {
int cpart = 0, lpos = 0;
while (1) {
lpos = INSTR (source, sep);
if (cpart == partnum) {
if (lpos > 0) return LEFT$ (source, lpos - len (sep));
else return source;
} else if (lpos > 0) {
cpart += 1;
source = RIGHT$ (source, len (source) - lpos - len (sep) + 1);
} else return "";
}
}


The errors are:

QuoteFile: C:\Programas\Aurora Demo\examples\!test.src (7) syntax error - else
File: C:\Programas\Aurora Demo\examples\!test.src (15) local subroutines not supported - {
Error(s) in compiling "C:\Programas\Aurora Demo\examples\!test.src"

I can get rid of the first error by making this change:

Quotesub strpart (string source, string sep, int partnum), string {
   int cpart = 0, lpos = 0;
   while (1) {
      lpos = INSTR (source, sep);
      if (cpart == partnum) {
         if (lpos > 0) { return LEFT$ (source, lpos - len (sep)); }
         else return source;
      } else if (lpos > 0) {
         cpart += 1;
         source = RIGHT$ (source, len (source) - lpos - len (sep) + 1);
      } else return "";
   }
}

I can get rid of the second error by making this change:
Quote
sub strpart (string source, string sep, int partnum), string {
   int cpart = 0, lpos = 0;
   while (1) {
      lpos = INSTR (source, sep);
      if (cpart == partnum) {
         if (lpos > 0) { return LEFT$ (source, lpos - len (sep)); }
         else return source;
      } else { if (lpos > 0) {
         cpart += 1;
         source = RIGHT$ (source, len (source) - lpos - len (sep) + 1);
      } else return ""; }
   }
}

However, if I do this, the code looks weird :P I'm very used to my own way of writing in C-like code - it should be valid to write it like I was writing. Can this be a problem in the parser?

Ionic Wind Support Team

There are two types of IF statements in Aurora.

The single line IF can be separated on multiple source lines but requires just one semicolon.

if blah action else action;

or

if blah
   action
else
   action;

So just removing the spurious semicolon would have fixed your error. 
Ionic Wind Support Team

Protected

July 19, 2006, 10:17:43 PM #2 Last Edit: July 19, 2006, 10:20:16 PM by Protected
A.. no... semicolon... that's... wrong! Not-C! What if the single line command is an if with a block (which has no semicolon to begin with)?

EDIT: Removing the semicolon doesn't fix the "local subroutines not supported".

Ionic Wind Support Team

July 19, 2006, 11:18:49 PM #3 Last Edit: July 19, 2006, 11:24:56 PM by Paul Turley
Aurora is not C.  Never has been. It is a syntax of our own design. The usage of IF statements is explained in detail in the tutorial that I wrote.

Local subroutines not supported means you have a syntax error somewhere.  The semicolon was just what I noticed off hand.

After writing it out in a more readable fashion it was easy to spot.


sub strpart (string source, string sep, int partnum), string
{
int cpart = 0, lpos = 0;
while (1)
{
lpos = INSTR (source, sep);
if (cpart == partnum)
{
if (lpos > 0)
return LEFT$ (source, lpos - len (sep))  //removed semicolon
else
return source;
}
else if (lpos > 0)
{
cpart += 1;
source = RIGHT$ (source, len (source) - lpos - len (sep) + 1);
}
else //needed to be a block else.
{
return "";
}
}
}
Ionic Wind Support Team

Ionic Wind Support Team

And I fixed the parser for the second problem.  For now just use the block else when you run into problems.
Ionic Wind Support Team

Protected

Aurora it's not C but not having a semicolon in the instruction that comes after an if is not very intuitive  :-\ I went and read your tutorial yesterday right after you posted :P

What exactly was the problem you fixed?

When the if is a block if, does the else have to be a block else too? That's related to what I asked you before:

QuoteWhat if the single line command is an if with a block (which has no semicolon to begin with)?

Ionic Wind Support Team

Your question didn't make any sense.

if (a = 1) b = 2 else b = 3;

one semicolon. And it is intuitive since the parser just ignores whitespace you can format that statement in any way.

if (a = 1) b=2
else b = 3;

All that is different is a NL character, which is ignored by the parser.  So the statement is identical.

If you have brackets then it is not a single line if.  Semicolon's are required after each program statment in a block.
Ionic Wind Support Team

Protected

Quoteelse //needed to be a block else.
{
return "";
}

This is my question - why does it need to be a block else?

And the previous question was about two ifs nested inside each other, the inner one being the (single command) of the outer one, and having a block for its own instructions.

Protected

I don't want to start a new topic, since this is still the same program, so here goes another one :P

sub loadfile (string path), string {
string block, buffer = "";
fp = openfile (path, MODE_READ);
if (fp) do {
if (len (buffer) < 32702 && readstring (fp, block, 64) != 0)
buffer = buffer + block + chr$(10);
} until (eof (fp) || len (buffer) >= 32702);
closefile (fp);
return buffer;
}


This is practically a copy from the editor.src example, but it crashes the program - why? When I comment out the buffer = buffer + (...) line, it works fine. If I replace it with "print block;", any file I try loading with this is properly displayed in the console window.

Ionic Wind Support Team

That is what I fixed.  The parser was not removing the else token from the input stream in this case, so the next statement was the closing brace of the subroutine which was seen as the end of an else block.  YaCC isn't perfect.

Quote
And the previous question was about two ifs nested inside each other, the inner one being the (single command) of the outer one, and having a block for its own instructions.

Still don't know what your asking.  Nested if's work fine.  A block is not a single command.  A block, even if it is on its own line is still a block of instructions.  Whitespace is ignored

if (a=b)
{
    if(b=c) {z=1;q=4;} else {blue = red; }
}

Or are you trying to do:

if(a=b)
    if(b=c){z=1;q=4;} else {blue = red; }

Which also works and follow the same syntax rules. 

Quote
This is practically a copy from the editor.src example

Not it's not.

Your overwritng the string.  A standard string is 255 characters long.

string block, buffer = "";

Buffer is only 255 characters.

Ionic Wind Support Team

Ionic Wind Support Team

What you probably meant to do was

string block; buffer = "";

Which sets the global buffer to an empty string instead of creating a local variable named 'buffer'
Ionic Wind Support Team

Protected

July 20, 2006, 09:07:13 AM #11 Last Edit: July 20, 2006, 09:13:42 AM by Protected
Alright, I got it now.

Can a function return dstring?

Ionic Wind Support Team

A string is a string.  the only place a DSTRING keyword is used is to define the length of a string, it is not a type.

So you can return any length string.  Just make sure the receiving variable is large enough.

sub bigstring(),string
{
dstring blah[1000];
    blah = strspace(999);
return blah;
}

Works fine.

string a = bigstring();

would overwrite the 'a' variable as it is only 255 characters.

dstring a[1000];
a = bigstring();

woudl be correct.  Although it is more proper to dynamically allocate strings and use pointers, I don't use code like the above except in simple example programs ;)

Ionic Wind Support Team