summaryrefslogtreecommitdiff
path: root/src/services/quick-answers/math.h
blob: 6bc07fe6601395866cf3198b7da4ef68b29d5974 (plain)
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
145
146
147
#pragma once

#include <gio/gio.h>
#include <gmp.h>

G_BEGIN_DECLS

typedef enum
{
	FOOBAR_MATH_VALUE_INT,
	FOOBAR_MATH_VALUE_FLOAT,
} FoobarMathValueType;

typedef struct _FoobarMathValue FoobarMathValue;

struct _FoobarMathValue
{
	FoobarMathValueType type;

	union
	{
		struct
		{
			mpz_t v;
			gsize decimal_places;
		} int_value;

		struct
		{
			long double v;
		} float_value;
	};
};

typedef enum
{
	FOOBAR_MATH_TOKEN_IDENTIFIER,
	FOOBAR_MATH_TOKEN_NUMBER,
	FOOBAR_MATH_TOKEN_PAREN_OPEN,
	FOOBAR_MATH_TOKEN_PAREN_CLOSE,
	FOOBAR_MATH_TOKEN_PLUS,
	FOOBAR_MATH_TOKEN_MINUS,
	FOOBAR_MATH_TOKEN_MUL,
	FOOBAR_MATH_TOKEN_DIV,
	FOOBAR_MATH_TOKEN_POW,
} FoobarMathTokenType;

typedef struct _FoobarMathToken FoobarMathToken;

struct _FoobarMathToken
{
	FoobarMathTokenType type;
	gchar const*        data;
	gsize               length;
};

typedef enum
{
	FOOBAR_MATH_EXPRESSION_VALUE,
	FOOBAR_MATH_EXPRESSION_FUNCTION,
	FOOBAR_MATH_EXPRESSION_CONSTANT,
	FOOBAR_MATH_EXPRESSION_OPERATION,
} FoobarMathExpressionType;

typedef enum
{
	FOOBAR_MATH_FUNCTION_NEGATE,
	FOOBAR_MATH_FUNCTION_SIN,
	FOOBAR_MATH_FUNCTION_COS,
	FOOBAR_MATH_FUNCTION_SEC,
	FOOBAR_MATH_FUNCTION_CSC,
	FOOBAR_MATH_FUNCTION_TAN,
	FOOBAR_MATH_FUNCTION_COT,
	FOOBAR_MATH_FUNCTION_ARCSIN,
	FOOBAR_MATH_FUNCTION_ARCCOS,
	FOOBAR_MATH_FUNCTION_ARCSEC,
	FOOBAR_MATH_FUNCTION_ARCCSC,
	FOOBAR_MATH_FUNCTION_ARCTAN,
	FOOBAR_MATH_FUNCTION_ARCCOT,
	FOOBAR_MATH_FUNCTION_EXP,
	FOOBAR_MATH_FUNCTION_SQRT,
} FoobarMathFunction;

typedef enum
{
	FOOBAR_MATH_CONSTANT_PI,
	FOOBAR_MATH_CONSTANT_E,
} FoobarMathConstant;

typedef enum
{
	FOOBAR_MATH_OPERATION_ADD,
	FOOBAR_MATH_OPERATION_SUB,
	FOOBAR_MATH_OPERATION_MUL,
	FOOBAR_MATH_OPERATION_DIV,
	FOOBAR_MATH_OPERATION_POW,
} FoobarMathOperation;

typedef struct _FoobarMathExpression FoobarMathExpression;

FoobarMathToken*      foobar_math_lex                     ( gchar const*                input,
                                                            gsize                       input_length,
                                                            gsize*                      out_count );
FoobarMathExpression* foobar_math_parse                   ( FoobarMathToken const*      tokens,
                                                            gsize                       tokens_count );
FoobarMathExpression* foobar_math_expression_new_value    ( FoobarMathValue             value );
FoobarMathExpression* foobar_math_expression_new_function ( FoobarMathFunction          function,
                                                            FoobarMathExpression*       input );
FoobarMathExpression* foobar_math_expression_new_constant ( FoobarMathConstant          constant );
FoobarMathExpression* foobar_math_expression_new_operation( FoobarMathOperation         operation,
                                                            FoobarMathExpression*       lhs,
                                                            FoobarMathExpression*       rhs );
void                  foobar_math_expression_print        ( FoobarMathExpression const* expr,
                                                            gint                        indentation );
void                  foobar_math_expression_free         ( FoobarMathExpression*       expression );
void                  foobar_math_value_new_int           ( FoobarMathValue*            out_value );
gboolean              foobar_math_value_from_string       ( gchar const*                input,
                                                            gsize                       input_length,
                                                            FoobarMathValue*            out_value );
void                  foobar_math_value_from_float        ( long double                 value,
                                                            FoobarMathValue*            out_value );
void                  foobar_math_value_copy              ( FoobarMathValue             value,
                                                            FoobarMathValue*            out_value );
void                  foobar_math_value_free              ( FoobarMathValue             value );
void                  foobar_math_value_negate            ( FoobarMathValue*            value );
gboolean              foobar_math_value_add               ( FoobarMathValue             lhs,
                                                            FoobarMathValue             rhs,
                                                            FoobarMathValue*            out_value );
gboolean              foobar_math_value_sub               ( FoobarMathValue             lhs,
                                                            FoobarMathValue             rhs,
                                                            FoobarMathValue*            out_value );
gboolean              foobar_math_value_mul               ( FoobarMathValue             lhs,
                                                            FoobarMathValue             rhs,
                                                            FoobarMathValue*            out_value );
gboolean              foobar_math_value_div               ( FoobarMathValue             lhs,
                                                            FoobarMathValue             rhs,
                                                            FoobarMathValue*            out_value );
gboolean              foobar_math_value_pow               ( FoobarMathValue             lhs,
                                                            FoobarMathValue             rhs,
                                                            FoobarMathValue*            out_value );
gboolean              foobar_math_value_is_simple_int     ( FoobarMathValue             value );
gchar*                foobar_math_value_to_string         ( FoobarMathValue             value );
long double           foobar_math_value_to_float          ( FoobarMathValue             value );
gboolean              foobar_math_evaluate                ( FoobarMathExpression const* expr,
                                                            FoobarMathValue*            out_value );

G_END_DECLS