Macro example Translation

Started by Frank Brübach, February 25, 2025, 03:28:22 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

How I can translate this macro example to o2

' freebasic code to o2
'
uses console

'Macro FIRST (_x_,_op_)
Macro FIRST int*(_x_,_op_)
  For index as Integer = LBound(_x_) to UBound(_x_) - 1
    Print _x_(index) _op_ ", "
  Next index
  Print _x_(Ubound(_x_))
Macro End

'Macro SECOND (_x_)
Macro SECOND int*(_x_)
  Print "Dump: " & #_x_
  FIRST(_x_, &)  ''FIRST(_x_, +)  ''corrected line
macro End

Dim as String test1(0 to ... ) => {"First", "Second", "Third" }
Dim as Integer test2(0 to ...) => { 1, 2 ,3 }

SECOND(test1)
SECOND(test2)
wait

' result output:
'
' dump: test1
' first, second, third
' dump: test2
' 1, 2, 3

Charles Pegge

in OxygenBasic it is End Macro, lexically consistent with End Function etc.

' freebasic code to o2
'
uses console

Macro FIRST (x,   index)
  int index
  For index = LBound(x) to UBound(x) - 1
    Print x(index) ", "
  Next 'index
  Print x(Ubound(x)) CR
end macro

def quot "%1"
Macro SECOND (x)
  Print "Dump: " quot(x)  CR
  FIRST(x)
end macro

Dim as String test1 = {"First", "Second", "Third" }
Dim as Integer test2 = { 1,2 ,3 }

SECOND(test1)
SECOND(test2)
wait

' result output:
'
' dump: test1
' first, second, third
' dump: test2
' 1, 2, 3

Zlatko Vid

it is not macro end than end macro

i get this with iteration:

Zlatko Vid

Hi Charles
what is quot modulo or integer 1 ?

Charles Pegge

#4
That little definition quot:

def quot "%1"

puts quote marks around its parameter
so
print quot test1
becomes:
print "test1"

Another related def:

def view print "%1"  tab %1 cr
can be used to display a variable and its value

Thus
int i=5
view i
displays:
i        5

Zlatko Vid

Thanks Charles  :)

I really don't know that and i never see that before
it is useful 

Frank Brübach


thanks charles for your translation..
this oxygen example shows how flexible and powerful macros are working


'
function add1(int *v)
v+=1
end function

macro madd1(v)
v+=1
end macro

macro Apply(v,f,w,n)
int n = countof v
typeof v *w = @v
while n--
  f w
  @w+=sizeof v
wend
end macro

macro smallList(v,f,c,n,w,s)
int c
int n=countof v
typeof v *w = @v
string s
while n--
  c++
  s += c  chr(9)  w  chr(13,10)
  @w+=sizeof v
wend
f s
s=""
end macro

float v={10.5,20,30,40,50,60}
apply     v, madd1
smallList v, print

second example

' 2)  related def..

def pr? print %1

pr? `Hello dear world`
pr? ( `Hello` ` ` `blue Sky!` )
pr? ( 6*7 )