Root/
Source at commit 0acff965b0d5c474e331657a810577985d8af010 created 7 years 3 months ago. By Werner Almesberger, new option -l (lower case L) to list package names | |
---|---|
1 | /* |
2 | * expr.h - Expressions and values |
3 | * |
4 | * Written 2009, 2012 by Werner Almesberger |
5 | * Copyright 2009, 2012 by Werner Almesberger |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | */ |
12 | |
13 | |
14 | #ifndef EXPR_H |
15 | #define EXPR_H |
16 | |
17 | #include <math.h> |
18 | |
19 | |
20 | #define UNDEF HUGE_VAL |
21 | |
22 | |
23 | struct frame; |
24 | struct expr; |
25 | struct value; |
26 | |
27 | enum num_type { |
28 | nt_none, |
29 | nt_mm, |
30 | nt_mil, |
31 | }; |
32 | |
33 | struct num { |
34 | enum num_type type; |
35 | int exponent; |
36 | double n; |
37 | }; |
38 | |
39 | typedef struct num (*op_type)(const struct expr *self, |
40 | const struct frame *frame); |
41 | |
42 | struct expr { |
43 | op_type op; |
44 | union { |
45 | struct num num; |
46 | const char *var; |
47 | char *str; |
48 | struct { |
49 | struct expr *a; |
50 | struct expr *b; |
51 | } op; |
52 | } u; |
53 | int lineno; |
54 | }; |
55 | |
56 | |
57 | extern struct num undef; |
58 | |
59 | |
60 | #define is_undef(num) ((num).type == nt_none) |
61 | #define is_dimensionless(num) (!(num).exponent) |
62 | |
63 | |
64 | static inline int is_distance(struct num num) |
65 | { |
66 | return (num.type == nt_mm || num.type == nt_mil) && num.exponent == 1; |
67 | } |
68 | |
69 | |
70 | void fail_expr(const struct expr *expr); |
71 | |
72 | const char *str_unit(struct num n); |
73 | |
74 | |
75 | static inline struct num make_num(double n) |
76 | { |
77 | struct num res; |
78 | |
79 | res.type = nt_mm; |
80 | res.exponent = 0; |
81 | res.n = n; |
82 | return res; |
83 | } |
84 | |
85 | |
86 | static inline struct num make_mm(double mm) |
87 | { |
88 | struct num res; |
89 | |
90 | res.type = nt_mm; |
91 | res.exponent = 1; |
92 | res.n = mm; |
93 | return res; |
94 | } |
95 | |
96 | |
97 | static inline struct num make_mil(double mil) |
98 | { |
99 | struct num res; |
100 | |
101 | res.type = nt_mil; |
102 | res.exponent = 1; |
103 | res.n = mil; |
104 | return res; |
105 | } |
106 | |
107 | |
108 | int to_unit(struct num *n); |
109 | |
110 | struct num op_num(const struct expr *self, const struct frame *frame); |
111 | struct num op_var(const struct expr *self, const struct frame *frame); |
112 | struct num op_string(const struct expr *self, const struct frame *frame); |
113 | |
114 | struct num op_sin(const struct expr *self, const struct frame *frame); |
115 | struct num op_cos(const struct expr *self, const struct frame *frame); |
116 | struct num op_sqrt(const struct expr *self, const struct frame *frame); |
117 | |
118 | struct num op_minus(const struct expr *self, const struct frame *frame); |
119 | struct num op_floor(const struct expr *self, const struct frame *frame); |
120 | |
121 | struct num op_add(const struct expr *self, const struct frame *frame); |
122 | struct num op_sub(const struct expr *self, const struct frame *frame); |
123 | struct num op_mult(const struct expr *self, const struct frame *frame); |
124 | struct num op_div(const struct expr *self, const struct frame *frame); |
125 | |
126 | struct expr *new_op(op_type op); |
127 | struct expr *binary_op(op_type op, struct expr *a, struct expr *b); |
128 | |
129 | int var_eq(const struct frame *frame, const char *name, |
130 | const struct expr *expr); |
131 | |
132 | struct num eval_var(const struct frame *frame, const char *name); |
133 | |
134 | /* |
135 | * eval_str returns NULL if the result isn't a string. Evaluation may then |
136 | * be attempted with eval_num, and the result can be converted accordingly. |
137 | */ |
138 | const char *eval_str(const struct expr *expr, const struct frame *frame); |
139 | |
140 | struct num eval_num(const struct expr *expr, const struct frame *frame); |
141 | |
142 | /* if frame == NULL, we only check the syntax without expanding */ |
143 | char *expand(const char *name, const struct frame *frame); |
144 | |
145 | struct expr *new_num(struct num num); |
146 | struct expr *parse_expr(const char *s); |
147 | void free_expr(struct expr *expr); |
148 | |
149 | int parse_var(const char *s, const char **id, struct value **values, |
150 | int max_values); |
151 | int parse_values(const char *s, struct value **values); |
152 | void free_values(struct value *values, int keep_expr); |
153 | |
154 | #endif /* !EXPR_H */ |
155 |
Branches:
master