-
Notifications
You must be signed in to change notification settings - Fork 1
/
lure.h
100 lines (85 loc) · 2.87 KB
/
lure.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
//
// Created by Shijing Lu on 11/3/18.
// Copyright © 2018 OpenAB. All rights reserved.
//
#ifndef __LURE__H__
#define __LURE__H__
#include <stdbool.h>
/** Main AST Node Type */
typedef struct Node Node;
/** Type for a list of nodes */
typedef struct NodeList NodeList;
/** Hashmap Type */
typedef void* ContextPtr;
/* Create a new context object. */
ContextPtr NewContext(void);
/**
* Set String Context, e.g. USER_NAME: "Alice"
* NOTE: this setter does not free space, make sure ctx is empty before calling this.
* @param ctx context object to be set
* @param key case sensitive context key, e.g. USER_NAME.
* @param val value of the context
*/
void setStringContext(ContextPtr ctx, char *key, char *val);
/**
* Set Integer Context, e.g. CITY_ID: 1
* NOTE: this setter does not free space, make sure ctx is empty before calling this.
* @param ctx context object to be set
* @param key case sensitive context key, e.g. CITY_ID.
* @param val value of the context
*/
void setIntContext(ContextPtr ctx, char *key, int val);
/**
* Set Double Context, e.g. PVALUE: 0.5
* NOTE: this setter does not free space, make sure ctx is empty before calling this.
* @param ctx context object to be set
* @param key case sensitive context key, e.g. PVALUE
* @param val value of the context
*/
void setDoubleContext(ContextPtr ctx, char *key, double val);
/**
* Set boolean Context, e.g. SWITCH_ON: true
* NOTE: this setter does not free space, make sure ctx is empty before calling this.
* @param ctx context object to be set
* @param key case sensitive context key, e.g. SWITCH_ON.
* @param val value of the context
*/
void setBoolContext(ContextPtr ctx, char *key, bool val);
/**
* Set Custom Context, e.g. APP_VERSION: v3.2.1/semver
* NOTE: this setter does not free space, make sure ctx is empty before calling this.
* @param ctx context object to be set
* @param key case sensitive context key, e.g. APP_VERSION.
* @param val value of the context
* @param typeDesc a string to find the right extension, e.g "semver"
*/
void setCustomContext(ContextPtr ctx, char *key, char *val, char *typeDesc);
/**
* Print all entries in the context.
*/
void printContext(ContextPtr ctx);
/**
* Compile string into AST tree, assuming there is only one expr.
* Example:
* > Node *n = lure_compile("CITY_ID == 1");
* > Data *d = n->evaluate(n, context);
* > assert(d->toBoolean(d) == true);
* > d->clean(); free(d);
* > node_free(n);
* @param s string to parse
* @return AST root node
*/
Node *lure_compile(const char *s);
/**
* Eval with pre-compiled AST.
* @param node root of the compiled ast
* @param ctx map of context
*/
bool lure_compile_eval(Node *node, ContextPtr ctx);
/**
* Evaluate expression with ctx, return false in case of error.
* @param ctx a map of context objects
* @param s string to eval.
* */
bool lure_eval(ContextPtr ctx, const char *s);
#endif /* __LURE__H__ */