-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlibast.h
156 lines (131 loc) · 6.46 KB
/
libast.h
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
148
149
150
151
152
153
154
155
156
/*******************************************************************************
* libast.h: this file is part of the libast library.
* libast: C library for evaluating expressions with the abstract syntax tree.
* Github repository:
https://github.com/cheng-zhao/libast
* Copyright (c) 2020 Cheng Zhao <zhaocheng03@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/
#ifndef _LIBAST_H_
#define _LIBAST_H_
#include <stdio.h>
#include <stdbool.h>
/*============================================================================*\
Indicators of variables
\*============================================================================*/
#define AST_VAR_FLAG '$'
#define AST_VAR_START '{'
#define AST_VAR_END '}'
/*============================================================================*\
Definitions of data structures
\*============================================================================*/
/* Enumeration of supported data types. */
typedef enum {
AST_DTYPE_BOOL = 1,
AST_DTYPE_INT = 2,
AST_DTYPE_LONG = 4,
AST_DTYPE_FLOAT = 8,
AST_DTYPE_DOUBLE = 16,
AST_DTYPE_STRING = 32
} ast_dtype_t;
/* The interface of the abstract syntax tree. */
typedef struct {
ast_dtype_t dtype; /* Data type for the expression. */
long nvar; /* Number of unique variables. */
void *var; /* The list of unique variables. */
long *vidx; /* Unique indices of variables. */
char *exp; /* A copy of the expression string. */
void *ast; /* The root node of the AST. */
void *error; /* Data structure for error handling. */
} ast_t;
/*============================================================================*\
Definitions of functions
\*============================================================================*/
/******************************************************************************
Function `ast_init`:
Initialise the interface of the abstract syntax tree.
Return:
The pointer to the interface on success; NULL on error.
******************************************************************************/
ast_t *ast_init(void);
/******************************************************************************
Function `ast_build`:
Build the abstract syntax tree given the expression and data type.
Arguments:
* `ast`: interface of the abstract syntax tree;
* `str`: null terminated string for the expression;
* `dtype`: data type for the abstract syntax tree;
* `eval`: true for pre-evaluating values.
Return:
Zero on success; non-zero on error.
******************************************************************************/
int ast_build(ast_t *ast, const char *str, const ast_dtype_t dtype,
const bool eval);
/******************************************************************************
Function `ast_set_var`:
Set the value of a variable in the variable array
Arguments:
* `ast`: interface of the abstract syntax tree;
* `idx`: index of the variable (starting from 1);
* `value`: pointer to a variable holding the value to be set;
* `size`: length of the string type variable;
* `dtype`: data type of the value.
Return:
Zero on success; non-zero on error.
******************************************************************************/
int ast_set_var(ast_t *ast, const long idx, const void *value,
const size_t size, const ast_dtype_t dtype);
/******************************************************************************
Function `ast_eval`:
Evaluate the expression given the abstract syntax tree and the variable array.
Arguments:
* `ast`: interface of the abstract syntax tree;
* `value`: address of the variable holding the evaluated value.
Return:
Zero on success; non-zero on error.
******************************************************************************/
int ast_eval(ast_t *ast, void *value);
/******************************************************************************
Function `ast_eval_num`:
Evaluate the numerical expression given the variable array with the same
data type.
Arguments:
* `ast`: interface of the abstract syntax tree;
* `value`: address of the variable holding the evaluated value;
* `var`: pointer to the variable array;
* `size`: number of elements in the variable array.
Return:
Zero on success; non-zero on error.
******************************************************************************/
int ast_eval_num(ast_t *ast, void *value, const void *var, const long size);
/******************************************************************************
Function `ast_perror`:
Print the error message if there is an error.
Arguments:
* `ast`: interface of the abstract syntax tree;
* `fp`: output file stream;
* `msg`: string to be printed before the error message.
******************************************************************************/
void ast_perror(const ast_t *ast, FILE *fp, const char *msg);
/******************************************************************************
Function `ast_destroy`:
Release memory allocated for the abstract syntax tree.
Arguments:
* `ast`: interface of the abstract syntax tree.
******************************************************************************/
void ast_destroy(ast_t *ast);
#endif