Author Topic: ScriptBasic O2h Embedding  (Read 26918 times)

0 Members and 2 Guests are viewing this topic.

Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #75 on: May 28, 2011, 01:28:27 AM »

John,

Good to know you cracked it. I would not have much of a clue.

The difficulty is that the macros are a new language to learn. Furthermore macros are not syntax checked to the same degree as the base language. I think it would be easier keeping with the original C and using comments instead of macros. There is also the option of using wrapper functions for the more complex operations.

I found the best way to use macros in Oxygen is to use them very locally. So you can immediately see what they mean as well as how they are used. (Unlike C, Oxygen macros may have local scope. They can 'disappear' when no longer needed and not fill up the global namespace.

Charles

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #76 on: May 28, 2011, 09:08:48 AM »
Quote
The difficulty is that the macros are a new language to learn.

For me at this point, it's easier to understand macros then the function table pointer access method to the SB API. What got me going in the right direction with assigning the argument was the MySQL extension module (FetchHash) that fills an associative array with the result set given only a variable name as an argument. This example will be helpful once again when I get into the more complex GSL functions that work with packed arrays.

Update: Armando chimed in and took the macros to a new level.

Quote from: AIR
Nice work, John.  I just popped in to see what you've been up to.  

I'm curious if the following would work using some of the higher-level macros.

For example, originally you had:
Code: [Select]
besFUNCTION(_hypot3)
  VARIABLE Argument;

  Argument = besARGUMENT(1);
  besDEREFERENCE(Argument);
  double x = DOUBLEVALUE(Argument);
  Argument = besARGUMENT(2);
  besDEREFERENCE(Argument);
  double y = DOUBLEVALUE(Argument);
  Argument = besARGUMENT(3);
  besDEREFERENCE(Argument);
  double z = DOUBLEVALUE(Argument);

  besRETURN_DOUBLE(gsl_hypot3(x, y, z));
besEND

I'm wondering if this could be re-coded as:
Code: [Select]
besFUNCTION(_hypot3)
  double x,y,z;
  
  besARGUMENTS("rrr")
    &x,&y,&z
  besARGEND  

  besRETURN_DOUBLE(gsl_hypot3(x, y, z));
besEND

"rrr" above specifies that the 3 arguments should be Double values (not sure why it isn't "d" for clarity, but that's how it's set up internally. I guess "r" stands for "real" which I suppose is analogous to a Double?).

A.
« Last Edit: May 28, 2011, 12:19:37 PM by JRS »

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #77 on: May 28, 2011, 06:25:11 PM »
Here is the source for the GSL extension module supporting the Elementary Functions.

Code: [Select]
/*
   GNU Scientific Library
   Based on GSL 1.15
   Interface By: John Spikowski
   Refinements By: Armando I. Rivera (AIR)
   Version 0.01
*/

#include <stdio.h>
#include "../../basext.h"
#include <gsl/gsl_math.h>
      
besVERSION_NEGOTIATE
    return (int)INTERFACE_VERSION;
besEND

besSUB_START

besEND

besSUB_FINISH

besEND

/* Elementary Functions */

besFUNCTION(_log1p)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_log1p(x));
besEND


besFUNCTION(_expm1)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_expm1(x));
besEND


besFUNCTION(_hypot)
  double x,y;

  besARGUMENTS("rr")
    &x,&y
  besARGEND  

  besRETURN_DOUBLE(gsl_hypot(x, y));
besEND


besFUNCTION(_hypot3)
  double x,y,z;
  
  besARGUMENTS("rrr")
    &x,&y,&z
  besARGEND  

  besRETURN_DOUBLE(gsl_hypot3(x, y, z));
besEND


besFUNCTION(_acosh)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_acosh(x));
besEND


besFUNCTION(_asinh)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_asinh(x));
besEND


besFUNCTION(_atanh)
  double x;

  besARGUMENTS("r")
    &x
  besARGEND  

  besRETURN_DOUBLE(gsl_atanh(x));
besEND


besFUNCTION(_ldexp)
  double x;
  int y;

  besARGUMENTS("ri")
    &x,&y
  besARGEND  

  besRETURN_DOUBLE(gsl_ldexp(x, y));
besEND


besFUNCTION(_frexp)
  double f;
  LEFTVALUE e;

  besARGUMENTS("r")
    &f
  besARGEND
  besLEFTVALUE(besARGUMENT(2),e);
  
  besRETURN_DOUBLE(gsl_frexp(f, *e));
besEND

Calling GSL SB extension module functions:

log1p()

Code: [Select]
DECLARE SUB log1p ALIAS "_log1p" LIB "gsl"

PRINT FORMAT("%.32g", log1p(34.0)),"\n"

3.5553480614894135136694330867613

frexp()

Code: [Select]
DECLARE SUB frexp ALIAS "_frexp" LIB "gsl"

x = 16.4
e = 0
fraction = frexp(x, e)

PRINT FORMAT("%g",fraction),"\n"
PRINT e,"\n"

0.5125
5
« Last Edit: May 29, 2011, 08:28:14 PM by JRS »