PDA

View Full Version : Multiple Statements On 1 Line



peralta_mike
21-04-2011, 19:56
How do I put multiple statements on 1 line.

i.e. in perl or C I would do following:
x=3 ; y=5 ;

Is there a way in tb in which I can do this?

ErosOlmi
21-04-2011, 20:55
You can use :

Charles Pegge
21-04-2011, 20:58
In most Basics we use a colon to separate statements, and a line break also separates statements, unlike C.



for i=1 to 10
x=i+4 : y=i*2 : z=i*3
next


Charles

John Spikowski
21-04-2011, 21:07
What is more efficient for an interpreter to parse? (and tokenize in some Basic languages)



for i=1 to 10
x=i+4 : y=i*2 : z=ii*3
next
OR



for i=1 to 10
x=i+4
y=i*2
z=ii*3
next


I think the z=ii*3 is a typo. ii ?

ScriptBasic requires one statement per line but does support _ line continuation along with multi-line strings, print statements and comment lines.

peralta_mike
21-04-2011, 22:14
You can use :


Excellent : will work for me.

btw is there line continuation syntax for tb as well?

ErosOlmi
21-04-2011, 22:36
Yes sure, underscore sign _ preceeded by (at least) one space

Also implicit line continuation (from version 1.8.6 (http://www.thinbasic.com/community/showthread.php?10672-thinBasic-Beta-1.8.6.0)) when in presence of a delimiter ending the line

ErosOlmi
21-04-2011, 22:52
For example, the following is a valid thinBasic code


Dim i, x, y, z As Long

For i=1 To 10
x=i+4 y=i*2 z=i*3
Next

MsgBox 0,
i & $CRLF &
x & $CRLF &
y & $CRLF &
z & $CRLF &
""





Note that

for simple expressions also : is not needed for separating statements because thinBasic automatically detect end of statement
implicit line termination in string concatenation because lines ends with a delimiter. thinBasic automatically detects it and go on parsing to the next line
Ciao

Eros

John Spikowski
22-04-2011, 02:04
Are you saying that the following is legal in ThinBasic?



A$ ="This
is
a
multi-line
string"

ErosOlmi
22-04-2011, 07:58
No, I was not saying that but also that is true.
the following is a perfect valid code in thinBasic from version 1.8.6



Dim MyString As String

MyString ="This
is
a
multi-Line
String"

MsgBox 0, MyString

'---Or
MsgBox 0, "This
is
again a
multi-Line
String"

'---or
Dim MyString2 As String = "This
is
another
multi-Line
String"

MsgBox 0, MyString2



So mainly plenty of options.

John Spikowski
23-04-2011, 07:13
Very Nice!

The only improvement might be to allow embedded quotes by using a triple double quote to start and end your strings" (Python, ScriptBasic, Vala, ...)

A$ = """This
is
"quoted"
text"""

Petr Schreiber
23-04-2011, 12:18
Hi John,

you can do the following currently already (with same result):


Dim myString As String = """This
is
""quoted""
Text"""

MsgBox 0, myString


to produce:


"This
is
"quoted"
Text"



Petr

peralta_mike
25-04-2011, 18:40
I am having a problem with a return statement on the same line as previous program statement. For example the following script does not work:



' tryreturn.bas April 25, 2011
uses "console","os","file","dt"
dim utmp as string
setutxtmp("c:\thinbasic\utx2.tmp")
utmp=getutxtmp()
console_write "UTXTMP=" & utmp & $CRLF
console_waitkey
'==================== end of thinbasic script =================
function setutxtmp(text2 as string) as string
static utxtmp as string = "utx.tmp"
if(len(text2)>0) then : utxtmp=text2 : end if : return utxtmp
end function
function getutxtmp() as string
dim utxtmp as string : utxtmp=setutxtmp("") : return utxtmp
end function
'==============================================================

HOWEVER IF I MOVE THE FIRST RETURN UTXTMP (IN RED) TO THE NEXT LINE IT WORKS.
IS THIS A BUG?


' tryreturn.bas April 25, 2011
uses "console","os","file","dt"
dim utmp as string
setutxtmp("c:\thinbasic\utx2.tmp")
utmp=getutxtmp()
console_write "UTXTMP=" & utmp & $CRLF
console_waitkey
'==================== end of thinbasic script =================
function setutxtmp(text2 as string) as string
static utxtmp as string = "utx.tmp"
if(len(text2)>0) then : utxtmp=text2 : end if
return utxtmp
end function
function getutxtmp() as string
dim utxtmp as string : utxtmp=setutxtmp("") : return utxtmp
end function
'==============================================================


NOTE: In these functions I am trying to use static variables in functions to avoid global variables. It is also to make my thinbasic user library as close to my perl and
c language libraries for my test software applications.

Also if I cannot trust the use of : to separate program statements this can be
a problem for the users of my application(s) as users will also have to write thinbasic scripts patterned after the examples I will be providing.

If we can fix this bug it would help a great deal. Also it would make thinbasic more robust in the use of : to separate program lines.


:confused:

peralta_mike
26-04-2011, 01:46
Can someone confirm this bug?

Since I am using MS-Wordpad as my text editor
perhaps the carriage return or line feed characters
are causing problems.

Thanks.

zak
26-04-2011, 05:57
hi john,
i think there is no bug , it returns what is in utxtmp.
may be it is when you copy code from word to thinbasic ide.
also when the readers of your code tried to copy it to thinbasic ide there is all sorts of errors in copying so if you have used the code tag "#" when posting code it will be formatted with color in the forum page, and can be copied correctly to the ide.
regarding the Multiple Statements On 1 Line: the following returns ss correctly


Uses "console"
Dim st As String
st = test
PrintL st
WaitKey
Function test() As String
Dim ss As String : ss="thin"
If(Len(ss)>0) Then : ss=ss+"basic" : End If: Return ss
End Function

John Spikowski
26-04-2011, 06:33
Zak,


hi john,

I think your reply was meant for peralta_mike rather than me.

John

zak
26-04-2011, 07:35
sorry John yes it is to peralta_mike

ErosOlmi
26-04-2011, 09:47
Dear Mike,

I think you are right regarding credibility of : as statement termination because there are some cases (for example END IF) in which parser checks for <EOL> token and doing that bypass whatever in between.

I will check what I can do to improve code parsing for those situations. In the meantime leave "Return" and "End If" statements on its own line.

That said, I personally would avoid writing code all in one line as much as possible. This mainly for code readability and code maintenance.
There is a great visual difference between

function setutxtmp(text2 as string) as string
static utxtmp as string = "utx.tmp"
if(len(text2)>0) then : utxtmp=text2 : end if : return utxtmp
end function

and


function setutxtmp(text2 as string) as string
static utxtmp as string = "utx.tmp"
if(len(text2)>0) then
utxtmp=text2
end if
return utxtmp
end function


In any case, I've opened a bug report:
http://www.thinbasic.com/community/project.php?issueid=274

Eros

peralta_mike
26-04-2011, 16:03
Thanks again Eros.