summaryrefslogtreecommitdiff
path: root/src/services/quick-answers/math.h
blob: 77c587a9fd2670274505c9657f6a4d1701400992 (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
#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_value    ( FoobarMathValue             value );
FoobarMathExpression* foobar_math_expression_function ( FoobarMathFunction          function,
                                                        FoobarMathExpression*       input );
FoobarMathExpression* foobar_math_expression_constant ( FoobarMathConstant          constant );
FoobarMathExpression* foobar_math_expression_operation( FoobarMathOperation         operation,
                                                        FoobarMathExpression*       lhs,
                                                        FoobarMathExpression*       rhs );
void                  foobar_math_expression_print    ( FoobarMathExpression const* expr,
                                                        gint                        indentation );
void                  foobar_math_expression_free     ( FoobarMathExpression*       expression );
gboolean              foobar_math_value_from_string   ( gchar const*                input,
                                                        gsize                       input_length,
                                                        FoobarMathValue*            out_value );
void                  foobar_math_value_free          ( FoobarMathValue             value );
gchar*                foobar_math_value_format        ( FoobarMathValue             value );
gboolean              foobar_math_evaluate            ( FoobarMathExpression const* expression,
                                                        FoobarMathValue*            out_value );

G_END_DECLS