March 29, 2024, 03:01:11 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Info Only Update 03-01-2013

Started by LarryMc, March 01, 2013, 07:13:33 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

This is the Help file section on converting from CBasic/IBasic to IWBasic
As I'm getting into this it is taking more time then what I thought so here's a preview draft of what I've got done so far.


Why convert to IWBasic?

· To switch from an interpreted to compiled program.
· To use some of the advanced capabilities.
· To keep from re-writing the whole program.

Getting Started

The process is not as easy as it might seem. Some code translates easily â€" other code might be troublesome.
The important point is to follow a systematic approach in doing the conversion.
By using the following as a checklist the process will be greatly simplified.

NOTE: For the rest of this discussion CB will be used to denote CBasic and IBasic code.  IWB will be used to denote IWBasic code.

First, create a new IWB file. Then copy and paste the desired CB code into it.
Don't even try to compile the code at this point. It will fail.

Subroutines - Local

This section covers local, User coded subroutines / functions.  It excludes calls to subroutines in components and calls to external subroutines in DLLs. Those will be covered later.

--------------------------------------------------------------------------------
In CB, the RETURN statement  indicates the end of a subroutine
SUB mysub1
  IF x = 4 THEN RETURN
...
RETURN

In IWB, the END SUB or ENDSUB statement indicates the end of a subroutine.
SUB mysub1
  IF x = 4 THEN RETURN
...
RETURN
ENDSUB

Therefore, END SUB or ENDSUB has to be added after the last RETURN in each and every subroutine/function.

--------------------------------------------------------------------------------
In  CB, a name followed by a : can be used to define a subroutine.
mysub:
  ...
RETURN

In IWB, mysub: would indicate a LABEL. Therefore, it would need to be converted to:
SUB mysub
  ...
RETURN
ENDSUB


--------------------------------------------------------------------------------
In CB, a subroutine with no passed parameters or return value can be called via the GOSUB command
GOSUB mysub1

SUB mysub1
...
RETURN

In IWB, it can be called exactly the same way
GOSUB mysub1

SUB mysub1
...
RETURN
ENDSUB

Or, it can be called like the following:
mysub1()

SUB mysub1
...
RETURN
ENDSUB


--------------------------------------------------------------------------------
In CB, subroutines that have passed parameters are called functions, regardless of whether or not they return a value.
Functions have to be declared before they can be used.
DECLARE docircle(x:INT,y:INT,size:INT,colora:INT,colorb:INT)
…
docircle 100,100,25,color1,color2
…
SUB docircle(x,y,size,colora,colorb)
  CIRCLE win,x,y,size,colora,colorb
RETURN

In IWB the DECLARE and SUB statements are combined.  This represents how all User defined subroutines, that don't return a value, have to be defined.
...
docircle (100,100,25,color1,color2)
...

sub docircle(x:INT,y:INT,size:INT,colora:INT,colorb:INT)
  CIRCLE win,x,y,size,colora,colorb
RETURN
ENDSUB


--------------------------------------------------------------------------------
In CB, there is no requirement to pre-define the return value type.. The following are valid;
DECLARE mysub1(x: INT, y: INT)
DECLARE mysub2(x: STRING, y: STRING)
...
a=1
b=2
PRINT mysub1(a, b)

c = "My dog "
d = "has fleas"
PRINT mysub2(c, d)

SUB mysub1(x, y)
  z: INT
  z=x + y
RETURN z

SUB mysub2(x, y)
  z: STRING
  z=x + y
RETURN z

In IWB, the code will look like this:
a=1
b=2
PRINT mysub1(a, b)

c = "My dog "
d = "has fleas"
PRINT mysub2(c, d)

SUB mysub1(x: INT, y: INT), INT
  z: INT
  z=x + y
RETURN z
ENDSUB

SUB mysub2(x: STRING, y: STRING), STRING
  z: STRING
  z=x + y
RETURN z
ENDSUB


Subroutines - Components

In CB, component files allow the User to add common subroutines to a multiple applications. Component files are compiled and can only be used by CB itself. Therefore, to convert a CB program that uses components to IWB requires one of two things:
1. The User has the original CB source file hat was used to create the component, or
2. The User completely rewrite all the subroutines contained in the component.

The following assumes the User has the original source file:

In CB, the following is the source code for a valid component.
InitMySubs
  DECLARE mysub1(x: INT, y: INT)
  DECLARE mysub2(x: STRING, y: STRING)
RETURN

SUB mysub1(x, y)
  z: INT
  z=x + y
RETURN z

SUB mysub2(x, y)
  z: STRING
  z=x + y
RETURN z

and the following is an example of the calling program
GOSUB InitMySubs
...
a=1
b=2
PRINT mysub1(a, b)

c = "My dog "
d = "has fleas"
PRINT mysub2(c, d)

In IWB, the User has two options:
One is to simply add the subroutines to the same main file the User has been converting to.  In that case the code would look like this:
a=1
b=2
PRINT mysub1(a, b)

c = "My dog "
d = "has fleas"
PRINT mysub2(c, d)

SUB mysub1(x: INT, y: INT), INT
  z: INT
  z=x + y
RETURN z
ENDSUB

SUB mysub2(x: STRING, y: STRING), STRING
  z: STRING
  z=x + y
RETURN z
ENDSUB

But taking this option is not in the original spirit on the component code, which was to make the code easily reusable in multiple applications. This leads to the second option the user has.
The User creates a second source file to contain the converted component code which will now look like this:
GLOBAL SUB mysub1(x: INT, y: INT), INT
  z: INT
  z=x + y
RETURN z
ENDSUB

GLOBAL SUB mysub2(x: STRING, y: STRING), STRING
  z: STRING
  z=x + y
RETURN z
ENDSUB


Up until the introduction of a second IWB source the User was dealing with a single file application and could follow the procedures outlined in the How-To»Single File Applications section.  With the introduction of a second IWB source file the User will be required to create a project following the procedures outlined in the How-To»Projects section.

The calling IWB program(that the CB source file is being converted to) will appear like this
$MAIN
DECLARE EXTERN, mysub1(x: INT, y: INT), INT
DECLARE EXTERN,mysub2(x: STRING, y: STRING), STRING
a=1
b=2
PRINT mysub1(a, b)

c = "My dog "
d = "has fleas"
PRINT mysub2(c, d)

The above file and the converted component file are then both added to the project.

NOTE: If additional IWB source files are added to the project the DECLARE EXTERN statements will have to be added at the top of any file that calls a component subroutine.  Sometimes this is best done by using an $INCLUDE file.

Subroutines -  DLL Calls

--- WIP, more to be added ---

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Brian

Larry,

I was under the impression that, in IWB, if you did not need to return a value before
the ENDSUB, you could leave the RETURN out. My code always seems to work OK
that way. I'm sure you will advise me otherwise!

Brian

LarryMc

Quote from: Brian Pugh on March 02, 2013, 11:02:36 AM
Larry,

I was under the impression that, in IWB, if you did not need to return a value before
the ENDSUB, you could leave the RETURN out. My code always seems to work OK
that way. I'm sure you will advise me otherwise!

Brian
You're absolutely correct.  If RETURN is not there then the compiler automatically inserts it when it is compiling.

QuoteThis is the Help file section on converting from CBasic/IBasic to IWBasic
I guess the point is that this section is NOT to tell you how to write an IWB program.
It is to tell you what you HAVE to CHANGE in order to convert CB/IB code to IWB code that will compile.
And that is what I'm trying to demonstrate in the examples.

So, with that in mind, there is no requirement for the RETURN to be removed when there is no value to return.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library