File: test_math.c

package info (click to toggle)
fungw 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,268 kB
  • sloc: ansic: 24,257; makefile: 500; sh: 32; awk: 9; perl: 8; tcl: 7; javascript: 7; ruby: 7; python: 6
file content (63 lines) | stat: -rw-r--r-- 2,022 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <libfungw/fungw.h>

#include "eng_math.h"

/* Import fawk init; this relies on two things:
   - fawk is always available in fungw, no external dep (well, the user can
     disable it in ./configure that'd lead to link error here)
   - fawk is in the big static lib (.a) of all languages and if we call its
     init, all relevant objects will get linked
*/
extern int pplg_init_fungw_fawk(void);

/* Handle runtime fungw errors - inform the user */
void async_error(fgw_obj_t *obj, const char *msg)
{
	printf("Async error: %s: %s\n", obj->name, msg);
}

/* Script calls C function: wrapper for atoi() */
fgw_error_t fgwc_atoi(fgw_arg_t *res, int argc, fgw_arg_t *argv)
{
	FGW_DECL_CTX;                        /* setup for the FGW macro shorthands, initializes fgw_ctx_t *ctx */
	FGW_ARGC_REQ_MATCH(1);               /* require exactly one argument */
	FGW_ARG_CONV(&argv[1], FGW_STR);     /* make sure argv[1] is a string */

	/* call the function, prepare the result in res */
	res->val.nat_int = atoi(argv[1].val.str);
	res->type = FGW_INT;
	return FGW_SUCCESS;
}

int main(int argc, char *argv[])
{
	fgw_ctx_t ctx;
	fgw_obj_t *mobj, *sobj;

	/* Initialize and register the script language engine fawk */
	pplg_init_fungw_fawk();

	/* Initialize and register the math engine */
	eng_math_init();

	/* initialize a context */
	fgw_init(&ctx, "my_context");
	ctx.async_error = async_error;

	/* create an object called trigonometry, an instance of the math engine, with
	   no instance argument (our local math engine is not configurable) */
	mobj = fgw_obj_new(&ctx, "trigonometry", "math", NULL, NULL);
	assert(mobj != NULL);

	/* create objects for the script: loads script and runs its main() */
	sobj = fgw_obj_new(&ctx, "test_script", "fawk", "test_math.fawk", NULL);
	assert(sobj != NULL);

	fgw_uninit(&ctx);   /* uninit the context; new contexts could still be created */
	fgw_atexit();       /* free all memory used by the lib */

	return 0;
}