Author Topic: When t use delimiter  (Read 7161 times)

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
When t use delimiter
« on: September 26, 2017, 07:06:46 AM »
I have a quick question on when you need to use a delimiter in TBasic

I understand that if I want to shorten a section of program such as
IF A>B  THEN
    THEN C=D*5
ELSE
    C=D/5
ENDIF

Can be shortened to IS A>B: C=D*5:ELSE:C=D/5:ENDIF

However if you only have one IF statement and it is used to either SET or Clear I/O is the delimiter needed.
IF SUMCV >= 27
  SETIO SUCT27
ELSE CLRIO SUCT27
  ENDIF

IF SUMCV >= 27 SETIO SUCT27 ELSE CLRIO SUCT27 ENDIF ' Is a delimiter needed in this case

Lorne



garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:When t use delimiter
« Reply #1 on: September 26, 2017, 01:26:57 PM »
Lorne,

TBASIC is a TRI's programming language has syntax that is close to many other BASIC languages.

Your question about the use of delimiters is a very valid one to ask.  First I must remind you that ":" is only one of many delimiters used by TBASIC.  The other delimiters are ASCII spaces, ASCII tabs and ASCII newline characters.

The purpose of a delimiter is to allow the TBASIC compiler to be able to break the program you have written down into bits of stuff that can be understood.

Your first program statement:
IF A>B  THEN
    THEN C=D*5
ELSE
    C=D/5
ENDIF

Is not written correctly and cannot be compiled.  You are still holding on to the use of the "THEN" that I have corresponded with you about earlier. There is no reason to use "THEN" in TBASIC .  The use of "THEN" is 100% optional, however the use of THEN twice in row is 100% wrong.

First get rid of both "THEN" keywords. Your program snippet will compile:

IF A>B
    C=D*5
ELSE
    C=D/5
ENDIF

I have marked the delimiters in red for your programming snippet.  __ is where you used an ASCII space(s) as a delimiter and <CR> is where you you used an ASCII newline as a delimiter.

IF__A>B<CR>
__C=D*5<CR>
ELSE<CR>
__C=D/5<CR>
ENDIF<CR>

You will notice that you have several delimiters in a row.  You can make this program "smaller" by eliminating the extra delimiters. This is about as small as you can make your programming snippet:

IF_A>B_C=D*5_ELSE_C=D/5_ENDIF<CR>

OR (without the delimiter highlighting):

IF A>B C=D*5 ELSE C=D/5 ENDIF

Your shortened snippet will not compile as you used "IS" rather than "IF":

IS A>B: C=D*5:ELSE:C=D/5:ENDIF

OK, what about the ":" delimiter?  In many BASIC dialects the ":" was used to allow one to place multiple statements on a single line of code.  In TBASIC you can get this done with the use of the ASCII space.  I am not certain when or if the ":" delimiter is ever required by TBASIC. However, If I wanted to put the following code on a single line, I might use the ":" to remind me of the statement boundaries:

IF A>B
    C=D*5
    SetIO BIT0
ELSE
    C=D/5
    ClrIO BIT0
ENDIF

Could be written as:

IF A>B  C=D*5 : SetIO BIT0 ELSE C=D/5 : ClrIO BIT0 ENDIF

I added the " : " between the statements to make it a bit easier to read.  Please note that  I am using " : " as the delimiter rather than ":" as  some versions of the i-TriLOGI do not consistently recognize ":" as a delimiter.  TRI is aware of this and may be fixed this problem in future releases.

Your last question about:

IF SUMCV >= 27 SETIO SUCT27 ELSE CLRIO SUCT27

You have supplied all of the delimiters required by TBASIC. This code will compile and it will execute as you expect.

The key things to think about is this, "If you write code with litter or no structure, will you be able to understand or maintain this code in the future?"

Best regards,

Gary D*ckinson
« Last Edit: September 26, 2017, 04:46:06 PM by garysdickinson »

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re:When t use delimiter
« Reply #2 on: September 26, 2017, 01:47:12 PM »
Gary
As usual your reply is very well explanatory
The first example I used actually came right out of the TL6 reverence manual chapter 11
IF A>B  THEN
    THEN C=D*5
ELSE
    C=D/5
ENDIF
I guess they really should update the manual so that people that actually read it don't get confused.  

Oh and sorry the "IS" instead of the "IF" was just a typo error  

Again thank you very much for the very informative explanation.
I did not have any issues with my actual programming it was just that today was one of those days I had decided to read through one of the manuals and came across chapter 11 that only gave a brief description of how to use a delimiter and I was hoping someone on tech support could give a better explanation of which you did.

P.S Also Gary in one of your previous replies to me you suggested that I should indent my code code for better readability and you are absolutely correct. When I looked at your example you can clearly see directly below the last IF you have an ENDIF then directly below the previous IF you have an ENDIF then directly below the For you have the next then the final ENDIF you can tell at a glance that you have inserted the correct number of ENDIF statements as well as the NEXT

Lorne

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:When t use delimiter
« Reply #3 on: September 27, 2017, 10:10:42 PM »
Lorne,

I thought a little more about the use of delimiters in TBASIC, and how to write code that is 100% unreadable but actually compiles and works.

The 1st half of the following code example is how I usually write PLC code.  The 2nd half is the same code but I have intentionally pushed the boundaries of common sense with how I have use delimiters to make the code unreadable.

' Custom function to print 0..99 on serial device #2
'
' This is the text that will be sent to the serial device:
'
'   00 01 02 03 04 05 06 07 08 09
'   10 11 12 13 14 15 16 17 18 19
'   20 21 22 23 24 25 26 27 28 29
'   30 31 32 33 34 35 36 37 38 39
'   40 41 42 43 44 45 46 47 48 49
'   50 51 52 53 54 55 56 57 58 59
'   60 61 62 63 64 65 66 67 68 69
'   70 71 72 73 74 75 76 77 78 79
'   80 81 82 83 84 85 86 87 88 89
'   90 91 92 93 94 95 96 97 98 99
'
print #2 "First Version"      ' Debug Message
for i = 0 to 9               ' outer loop for the 1s digits
   for j = 0 to 9            '   inner loop for the 10s digits
      print   #2 i;j;" ";      '   print each number with 2 digits and a space
   next
   print #2               '   newline at the end of each group of 10 numbers
next

' this code will produce the same results
'
print   #2
"Second" ;
print   #2
" Version"
for  i = 0
to  9  for
j = 0 to 9
print   #2
i;j ;" " ;
next print
#2    next
[/font][/color]

When run under simulations this is what is output to the simulated serial port:

First Version
00 01 02 03 04 05 06 07 08 09
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
Second Version
00 01 02 03 04 05 06 07 08 09
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
[/font][/color]

TBASIC unlike very early dialects of BASIC can have statements and expressions split over multiple program lines.  You can have multiple statements on a single line as long as you have some delimiter between them.

Arithmetic, bitwise and relational expressions are handled a little differently by the TBASIC expression parser and in most cases do not require white space delimiters.  You can write code like this and it works:
A=45/C*42+2MOD4  ' no spaces needed!!!

Wasn't this fun?

Gary D*ckinson
« Last Edit: September 27, 2017, 10:13:26 PM by garysdickinson »