xref: /dragonfly/usr.bin/dc/bcode.h (revision 36a3d1d6)
1 /*
2  * $OpenBSD: bcode.h,v 1.3 2003/12/01 09:13:24 otto Exp $
3  * $DragonFly: src/usr.bin/dc/bcode.h,v 1.1 2004/09/20 04:20:39 dillon Exp $
4  */
5 
6 /*
7  * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 #include <sys/types.h>
23 #include <openssl/bn.h>
24 
25 
26 struct number {
27 	BIGNUM	*number;
28 	u_int	scale;
29 };
30 
31 enum stacktype {
32 	BCODE_NONE,
33 	BCODE_NUMBER,
34 	BCODE_STRING
35 };
36 
37 enum bcode_compare {
38 	BCODE_EQUAL,
39 	BCODE_NOT_EQUAL,
40 	BCODE_LESS,
41 	BCODE_NOT_LESS,
42 	BCODE_GREATER,
43 	BCODE_NOT_GREATER
44 };
45 
46 struct array;
47 
48 struct value {
49 	union {
50 		struct number	*num;
51 		char		*string;
52 	} u;
53 	struct array	*array;
54 	enum stacktype	type;
55 };
56 
57 struct array {
58 	struct value	*data;
59 	size_t		size;
60 };
61 
62 struct stack {
63 	struct value	*stack;
64 	int		sp;
65 	int		size;
66 };
67 
68 struct source;
69 
70 struct vtable {
71 	int	(*readchar)(struct source *);
72 	int	(*unreadchar)(struct source *);
73 	char	*(*readline)(struct source *);
74 	void	(*free)(struct source *);
75 };
76 
77 struct source {
78 	struct vtable	*vtable;
79 	union {
80 			FILE *stream;
81 			struct {
82 				u_char *buf;
83 				size_t pos;
84 			} string;
85 	} u;
86 	int		lastchar;
87 };
88 
89 void			init_bmachine(bool);
90 void			reset_bmachine(struct source *);
91 void			scale_number(BIGNUM *, int);
92 void			normalize(struct number *, u_int);
93 void			eval(void);
94 void			pn(const char *, const struct number *);
95 void			pbn(const char *, const BIGNUM *);
96 void			negate(struct number *);
97 void			split_number(const struct number *, BIGNUM *, BIGNUM *);
98 void			bmul_number(struct number *, struct number *,
99 			    struct number *);
100 
101 extern BIGNUM		zero;
102