Trying to make maro for INCR and DECR

General FreeBASIC programming questions.
lassar
Posts: 302
Joined: Jan 17, 2006 1:35
Contact:

Trying to make maro for INCR and DECR

Postby lassar » Apr 03, 2013 14:12

I am trying to make a macro for INCR, and DECR.

INCR I%

DECR I%

It is so to make translating PowerBasic code easier.

I have tried:

#macro INCR(a)
a = a + 1
#endmacro

#macro DECR(a)
a = a - 1
#endmacro

What am I doing wrong?
fxm
Posts: 9989
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Trying to make maro for INCR and DECR

Postby fxm » Apr 03, 2013 14:16

It works!

Code: Select all

#macro INCR(a)
a = a + 1
#endmacro

#macro DECR(a)
a = a - 1
#endmacro


Dim As Integer I = 3
Print I
INCR(I)
Print I
DECR(I)
Print I
Sleep
You cannot call it with:
INCR I
DECR I
TJF
Posts: 3604
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Trying to make maro for INCR and DECR

Postby TJF » Apr 03, 2013 18:34

There's no reason for a multi line MACRO. Better go single line here:

Code: Select all

#DEFINE INCR(_V_) _V_ += 1
#DEFINE DECR(_V_) _V_ -= 1

VAR a = 1

INCR(a)
?a

DECR(a)
?a

But as fxm mentioned: No way to skip the parenthesis when using a DEFINE. It works without parenthesis when you make SUBs. But this is slower, due to the stack actions when calling the SUB:

Code: Select all

SUB INCR(BYREF V AS INTEGER)
  V += 1
END SUB

SUB DECR(BYREF V AS INTEGER)
  V -= 1
END SUB

VAR a = 1

INCR a
?a

DECR a
?a
Stonemonkey
Posts: 649
Joined: Jun 09, 2005 0:08

Re: Trying to make maro for INCR and DECR

Postby Stonemonkey » Apr 03, 2013 20:36

is there any way to inline subs/functions in fb?
dodicat
Posts: 6724
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Trying to make maro for INCR and DECR

Postby dodicat » Apr 03, 2013 22:27

You can keep 'em in a line if you're careful with the dosbox,and don't overshoot.

Code: Select all

Sub inc(s As String)
    Dim As Integer counts
    Var ls=Len(s)
    Do
        If  s[ls-counts-1]=57 Then
            counts=counts+1
            If counts=ls Then s="1"+String(ls,"0"):Exit Do
        Else
            s=Left(s,ls-counts-1)+Str(s[ls-counts-1]-47)+String(counts,"0")
            Exit Do
        End If
    Loop
End Sub

Sub dec(s As String)
    Dim As Integer counts
    Var ls=Len(s)
    Do
        If  s[ls-counts-1]=48 Then
            counts=counts+1
        Else
            s=Ltrim(Left(s,ls-counts-1)+Str(s[ls-counts-1]-49)+String(counts,"9"),"0")
            Exit Do
        End If
    Loop
End Sub

'===========================================
enum :up:down:end enum
dim as integer direction=up

var n= "99999999999999999999999999999999999999999999999999999999999999999999999999950"
 print
do
    print n
if direction=up then inc(n) else dec(n)

if n="100000000000000000000000000000000000000000000000000000000000000000000000000050" then direction=down

loop until n="99999999999999999999999999999999999999999999999999999999999999999999999999949"
sleep
counting_pine
Site Admin
Posts: 6229
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Trying to make maro for INCR and DECR

Postby counting_pine » Apr 04, 2013 14:56

Stonemonkey wrote:is there any way to inline subs/functions in fb?

FreeBASIC doesn't support inline procedures. Although with the GCC emitter, GCC may well be able to inline simple routines like that.
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: Trying to make maro for INCR and DECR

Postby petan » Apr 10, 2013 11:51

Why so hard.
PowerBasic syntax INCR A%
FreeBasic syntax a+=1

After founding FB syntax, my old way was (FbEdit used):
-find every incr
-paste +=1 after variableName
-delete % sign
-delete INCR

Result >> clean FB syntax.
Of course, with massive using of block comments via "/' and '/" and immediate compiling to get things Okay.

Pete
Last edited by petan on Apr 11, 2013 14:47, edited 1 time in total.
Richard
Posts: 3038
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Trying to make maro for INCR and DECR

Postby Richard » Apr 10, 2013 12:35

Anything can be done with an editor. But if you edit the code you can no longer run the modified source on the original system.
#Macro and #Define require brackets about the parameters, while Sub and Function have execution time overheads.
Unfortunately, there seems to be no way in FB to execute the source " Inc var " inline without editing or overheads.
The fastest way to execute the original unedited source is with a Sub.

Code: Select all

Sub Inc(Byref z As Integer)
    z += 1
End Sub

Dim As Integer a
Print a
inc a
Print a
Sleep

Editing “ Inc var ” to become “ Inc(var) ” would permit the fastest solution, #Define Inc( z ) z += 1
TJF
Posts: 3604
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Trying to make maro for INCR and DECR

Postby TJF » Apr 10, 2013 13:37

Yes Richard, you got it. (That's what I wrote seven days ago : )
Richard
Posts: 3038
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Trying to make maro for INCR and DECR

Postby Richard » Apr 10, 2013 15:19

TJF did write it earlier, but Petan did not read it later, so I summarised the thread.
dodicat
Posts: 6724
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Trying to make maro for INCR and DECR

Postby dodicat » Apr 10, 2013 21:45

petan

You could just search and replace
INCR A%
with
a=a+1 'INCR A%
same for all the other INCR something%

That way you still retain the original code with the non fb bits commented.


I never use /' and '/, for commenting out biggish blocks.
I just put non running code or anything else inside dummy macros

#macro dummy1()
blah
blah
#endmacro


So long as you don't call the macro all is tucked away.
I find it easier that way.
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: Trying to make maro for INCR and DECR

Postby petan » Apr 11, 2013 14:42

Good tip, dodicat, massive commenting via dummy macro.Thx, bookmarked.
But actually I cannot compare this two ways for speed & easybility of such job, coz all my old PB code is recoded.

All upwritten ways of translating INCR DECR depends on :
- coder's needs of clean syntax
- how big source is
- how much of commands and different variable names will be replaced ?
IMO, for a few tens of replacing here is using of editor quite accurate.
For more then tens (hundreds, thousands), and different variable names, using of subroutine inc(variablename) is speedy.And for clean syntax - source code replacing/tuning via small FB utility in cycle with help of StrReplace function.Founded on german FB site.Very usefull for replacing strings in files etc.
All jobs on copy of original & backuped PB sourcecode ;)

Pete
counting_pine
Site Admin
Posts: 6229
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Trying to make maro for INCR and DECR

Postby counting_pine » Apr 11, 2013 15:24

I think the official way of using the preprocessor to remove code would be #if 0 ... #endif.

The most compatible way is probably to overload the incr/decr functions for every type you might use:

Code: Select all

'sub incr overload (byref a as short): a += 1: end sub
'sub incr overload (byref a as integer): a += 1: end sub
'sub incr overload (byref a as long): a += 1: end sub
'...

#define make_incr(typ) sub incr overload(byref n as typ): n += 1: end sub
make_incr(byte): make_incr(ubyte)
make_incr(ushort): make_incr(short)
make_incr(long): make_incr(ulong)
make_incr(integer): make_incr(uinteger)
make_incr(longint): make_incr(ulongint)
make_incr(single): make_incr(double)

#define make_decr(typ) sub decr overload(byref n as typ): n -= 1: end sub
make_decr(byte): make_decr(ubyte)
make_decr(short): make_decr(ushort)
make_decr(long): make_decr(ulong)
make_decr(integer): make_decr(uinteger)
make_decr(longint): make_decr(ulongint)
make_decr(single): make_decr(double)

dim as integer n = 0
print n ' 0
incr n
print n ' 1
decr n
print n ' 0
The only slight risk is that if you try to increment a variable of a type that you haven't made the overload for, it might get passed by value to a different overload, and so not be incremented. The above code should be thorough enough though.
Cherry
Posts: 351
Joined: Oct 23, 2007 12:06
Location: Austria
Contact:

Re: Trying to make maro for INCR and DECR

Postby Cherry » Apr 12, 2013 15:22

You could use Notepad++ and a simple regular expression to replace all INCR/DECR calls with the correct FB syntax.

Open your file in Notepad++, press Ctrl+R and configure it like this:

Image

The left configuration will replace INCR (press Find and then Replace Rest), then do the same with the right configuration which will replace DECR.
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35

Re: Trying to make maro for INCR and DECR

Postby Theunis Jansen » Apr 17, 2013 8:00

To increase or decrease a variable and keep it within certain bounds I have used the following code for years now. It works in #lang "fblite" but I have not tested it in #Lang "FB"

A = A + (A > 1) 'Decr A until it reaches 1 or any Mimimum

A = A - (A < MAX) 'Incr A until it reaches a certain Maximum

Being an oldtime QB man I may be missing the point but perhaps you can use it similar to that as suggest by Counting_pine by placing them in a SUB or a Function.

Return to “General”

Who is online

Users browsing this forum: No registered users and 13 guests