/*
READ THIS FILE AND CHANGE THE SOURCE WHEREVER YOU SEE COMMENTS STARTING
WITH THE WORD *TODO*
WHEN YOU ARE FINISHED YOU CAN
FILE : interface.c
HEADER : interface.h
BAS : gmp2.bas
AUTHOR : *TODO*
DATE:
CONTENT:
This is the interface.c file for the ScriptBasic module gmp2
----------------------------------------------------------------------------
Remove the two characters // from following line(s) if this module is supposed to
be compiled under specific OS's. If there is a need for some library to successfully
compile the module under Windows specify the names of the libraries on the line
as it is listed for the linker application. This is usually something like
WINDOWS: libname1.lib libname2.lib ... libnameX.lib
LINUX/MACOS: -lm -ldl
If there are no libraries, but still the module is to be compiled under Windows
do remove the // characters so that the program setup.pl will know that the
module is meant for Windows.
//NTLIBS:
UXLIBS: -lgmp
DWLIBS: -lgmp -macosx_version_min 10.12
*/
/*
*TODO*
INCLUDE HERE THE SYSTEM HEADER FILES THAT ARE NEEDED TO COMPILE THIS MODULE
*/
#ifdef WIN32
/*
*TODO*
INCLUDE HERE THE WIN32 SPECIFIC HEADER FILES THAT ARE NEEDED TO COMPILE THIS MODULE
*/
#else
/*
*TODO*
INCLUDE HERE THE UNIX SPECIFIC HEADER FILES THAT ARE NEEDED TO COMPILE THIS MODULE
*/
#endif
/*
*TODO*
INCLUDE HERE THE LOCAL HEADER FILES THAT ARE NEEDED TO COMPILE THIS MODULE
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../basext.h"
#include <gmp.h>
/*
*TODO*
INSERT THE BASIC CODE THAT WILL GET INTO THE FILE gmp2.BAS
AFTER THE LINE 'TO_BAS:' AND BEFORE THE LINE END OF THE COMMENT
NOTE THAT SUB AND COMMAND DECLARATIONS ARE CREATED AUTOMATICALLY
FROM THE FUNCTION DEFINTIONS WHEN THE MODULE IS CONFIGURED BEFORE
COMPILATION
TO_BAS:
*/
/*
*TODO*
DECLARE HERE THE MODULE OBJECT TYPE. THIS STRUCTURE SHOULD HOLD THE
DATA AVAILABLE FOR EACH INTERPRETER THREAD. USE THIS STRUCTURE TO
STORE GLOBAL VALUES INSTEAD OF USING GLOBAL VARIABLES.
*/
typedef struct _ModuleObject {
char a; /* You may delete this. It is here to make the initial interface.c compilable. */
}ModuleObject,*pModuleObject;
mpz_t g_Op1, g_Op2, g_Res;
/*
*TODO*
ALTER THE VERSION NEGOTIATION CODE IF YOU NEED
*/
besVERSION_NEGOTIATE
return (int)INTERFACE_VERSION;
besEND
/*
*TODO*
ALTER THE ERROR MESSAGE FUNCTION
*/
besSUB_ERRMSG
switch( iError ){
case 0x00080000: return "ERROR HAS HAPPENED";
}
return "Unknown gmp2 module error.";
besEND
/*
*TODO*
ALTER THE MODULE INITIALIZATION CODE
*/
besSUB_START
pModuleObject p;
besMODULEPOINTER = besALLOC(sizeof(ModuleObject));
if( besMODULEPOINTER == NULL )return 0;
mpz_init(g_Op1);
mpz_init(g_Op2);
mpz_init(g_Res);
/*
*TODO*
INSERT HERE ANY CODE THAT YOU NEED TO INITIALIZE THE MODULE FOR THE
ACTUAL INTERPRETER THREAD
*/
besEND
/*
*TODO*
ALTER THE MODULE FINISH CODE IF NEEDED
*/
besSUB_FINISH
pModuleObject p;
/*
YOU CERTAINLY NEED THIS POINTER TO FREE ALL RESOURCES THAT YOU ALLOCATED
YOU NEED NOT CALL besFREE TO FREE THE MEMORY ALLOCATED USING besALLOC
CLOSE ANY FILE THAT REMAINED OPEN, RELEASE DATABASE HANDLES AND ALL
OTHER RESOURCES INCLUDING MEMORY *NOT* ALLOCATED CALLING besALLOC
*/
p = (pModuleObject)besMODULEPOINTER;
if( p == NULL )return 0;
return 0;
besEND
/*
*TODO*
WRITE YOUR MODULE INTERFACE FUNCTIONS FOLLOWING THIS SKELETON
NOTE THAT THIS IS A SAMPLE FUNCTION, YOU CAN ALSO DELETE
LINES FROM IT IF NEEDED
*/
/**
=section mpz
=H mpz
Returns new mpz_t object
*/
besFUNCTION(mpz)
pModuleObject p;
mpz_t r;
p = (pModuleObject)besMODULEPOINTER;
// besARGUMENTS("i[s]")
// &r
// besARGEND
besRETURN_POINTER(r);
besEND
/**
=section init
=H mpz
Initializes mpz_t object
*/
besFUNCTION(init)
pModuleObject p;
mpz_t r;
char * res;
p = (pModuleObject)besMODULEPOINTER;
// besARGUMENTS("p")
// &r
// besARGEND
mpz_init(r);
res = mpz_get_str(NULL,10,r);
besSET_RETURN_STRING(res);
besRETURN_POINTER(r);
besEND
/**
=section init_set
=H mpz
Initializes mpz_t object
*/
besFUNCTION(init_set)
pModuleObject p;
char *s, *res;
int i;
p = (pModuleObject)besMODULEPOINTER;
besARGUMENTS("z")
&s
besARGEND
// mpz_set_str(g_Op1, s, 10);
mpz_init_set_str(g_Res,s,10);
res = mpz_get_str(NULL,10,g_Res);
besSET_RETURN_STRING(res);
besEND
/**
=section mul
=H mpz
Multiplies mpz_t objects
*/
besFUNCTION(mul)
pModuleObject p;
char *s, *t, *res;
int i;
p = (pModuleObject)besMODULEPOINTER;
besARGUMENTS("zz")
&s,&t
besARGEND
mpz_set_str(g_Op1, s, 10);
mpz_set_str(g_Op2, t, 10);
mpz_mul(g_Res, g_Op1, g_Op2);
res = mpz_get_str(NULL,10,g_Res);
besSET_RETURN_STRING(res);
besEND
/**
=section mul_si
=H mpz
Multiplies mpz_t objects
*/
besFUNCTION(mul_si)
pModuleObject p;
char *s,*res;
long i;
p = (pModuleObject)besMODULEPOINTER;
besARGUMENTS("zi")
&s,&i
besARGEND
mpz_set_str(g_Op1, s, 10);
mpz_mul_si(g_Res, g_Op1, i);
res = mpz_get_str(NULL,10,g_Res);
besSET_RETURN_STRING(res);
besEND
/**
=section add
=H add
Adds mpz_t objects
*/
besFUNCTION(add)
pModuleObject p;
char *s, *t, *res;
int i;
p = (pModuleObject)besMODULEPOINTER;
besARGUMENTS("zz")
&s,&t
besARGEND
mpz_set_str(g_Op1, s, 10);
mpz_set_str(g_Op2, t, 10);
mpz_add(g_Res, g_Op1, g_Op2);
res = mpz_get_str(NULL,10,g_Res);
besSET_RETURN_STRING(res);
besEND
/**
=section test
=H add
Adds mpz_t objects
*/
besFUNCTION(test)
pModuleObject p;
mpz_t r, s, t;
int i;
p = (pModuleObject)besMODULEPOINTER;
// besARGUMENTS("ppp")
// &r,&s,&t
// besARGEND
// mpz_add(r, s, t);
i=12345;
besRETURN_LONG(i);
besEND
/**
=section get_string
=H add
Adds mpz_t objects
*/
besFUNCTION(get_string)
pModuleObject p;
mpz_t r, s, t;
int i;
char * retstring;
p = (pModuleObject)besMODULEPOINTER;
besARGUMENTS("p")
&r
besARGEND
retstring = mpz_get_str(0,10,r);
besSET_RETURN_STRING(retstring);
besEND
/*
*TODO*
INSERT HERE THE NAME OF THE FUNCTION AND THE FUNCTION INTO THE
TABLE. THIS TABLE IS USED TO FIND THE FUNCTIONS WHEN THE MODULE
INTERFACE FILE IS COMPILED.
*/
SLFST GMP2_SLFST[] ={
{ "versmodu" , versmodu },
{ "bootmodu" , bootmodu },
{ "finimodu" , finimodu },
{ "emsgmodu" , emsgmodu },
{ "mpz" , mpz },
{ "init" , init },
{ "init_set" , init_set },
{ "mul" , mul },
{ "mul_si" , mul_si },
{ "add" , add },
{ "get_string" , get_string },
{ NULL , NULL }
};