File: interface.texi

package info (click to toggle)
oleo 1.6-16
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,640 kB
  • ctags: 3,139
  • sloc: ansic: 39,221; yacc: 1,737; sh: 362; makefile: 88
file content (144 lines) | stat: -rw-r--r-- 2,995 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
@c \input texinfo    @c -*- texinfo -*-
@c @defcodeindex cm
@cindex Extending 

@node Extending Oleo, Reporting Bugs, Functions, Top
@chapter Extending the oleo system



@menu
* New @@ functions::            Adding new @@ Functions
@end menu

@node New @@ functions,  , Extending Oleo, Extending Oleo
@section Adding new @@ Functions

It is easy to add new @@ functions.  Suppose that I want to add 
a greatest common divisor function.  Here is a definition:
@example
int gcd(i,k)
    int i,k;
@{     
  int c;
  if ( i < 0 )  i = -i;
  if ( k < 0 )  k = -k;
  while ( k != 0 ) @{
    c = k;
    k = i % k;
    i = c;
  @}
  return  i;
@}
@end example



In oleo, I would like to enter @code{@@gcd(6,20)}, and have the result
@code{2} displayed.  The following files need to be changed or created:

@table @file
@item my-new-functions.c

We will create a new class of functions called @var{int_funs}.  For
simplicity, there is only one function, @code{gcd}, in this class, but
there could be many more. In addition to the code for @var{gcd}, 
we need an interface between oleo and @var{gcd}.  
@example
static void
do_gcd (p)
     struct value *p;
@{
  int arg0 = p[0].Int;
  int arg1 = p[1].Int;

  p->Int = gcd (arg0,arg1);
  p->type = TYP_INT;
@}
@end example

Next, we need to give information about the members of this new class of
functions.  The "II" refers to the two integer arguments.  For more
examples, see @file{busi.c}.
@example
struct function int_funs[] =
@{
  @{C_FN2, X_A2, "II", do_gcd, "gcd"@},
  @{0, 0, "", 0, 0@},
@};
@end example

Finally, we need some standard headers and defines.  These are probably
not all necessary.

@example
#include "funcdef.h"
#include "sysdef.h"
#include "global.h"
#include "cell.h"
#include "eval.h"
#include "errors.h"

struct value
  @{
    int type;
    union vals x;
  @};

#define Float	x.c_d
#define String	x.c_s
#define Int	x.c_l
#define Value	x.c_i
#define Rng	x.c_r
@end example


@item forminfo.c

It's polite to add information about new functions.  This information
can be added in an appropriate place in @file{forminfo.c}:
@example
"gcd",
"find gcd                       gcd(a,b)",
""
"The greatest common divisor of A and B.",
0,
@end example

@item byte-compile.c

The byte compiler needs to be told about the new class of functions.  
We declare the new class of functions:
@example
extern struct function int_funs[];
@end example
and instruct the compiler to read them by adding the class to
the appropriate structure.  There are a few unfortunate hacks to clean up.
@example
int n_usr_funs = 5;
static struct function *__usr_funs[] =
@{
  date_funs,
  busi_funs,
  string_funs,
  cells_funs,
  int_funs,
@};
static int __usr_n_funs[] =	
@{				
  32, 18, 11, 10,1		
@};				

@end example

@item Makefile

The file @file{my-new-functions.o} needs to be added to the list of
object files.  One possibility is this:
@example
objs = my-new-functions.o \
 parse.tab.o byte-compile.o eval.o ref.o \
 ...
@end example
@end table