Root/expr.h

Source at commit 5fdea0e99a2e8543a59d508cf28e1f2ff3607f18 created 4 years 7 months ago.
By Werner Almesberger, gui_canvas.c (key_press_event): make / rotate through packages as well
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
23struct frame;
24struct expr;
25struct value;
26
27enum num_type {
28    nt_none,
29    nt_mm,
30    nt_mil,
31};
32
33struct num {
34    enum num_type type;
35    int exponent;
36    double n;
37};
38
39typedef struct num (*op_type)(const struct expr *self,
40    const struct frame *frame);
41
42struct 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
57extern struct num undef;
58
59
60#define is_undef(num) ((num).type == nt_none)
61#define is_dimensionless(num) (!(num).exponent)
62
63
64static inline int is_distance(struct num num)
65{
66    return (num.type == nt_mm || num.type == nt_mil) && num.exponent == 1;
67}
68
69
70void fail_expr(const struct expr *expr);
71
72const char *str_unit(struct num n);
73
74
75static 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
86static 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
97static 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
108int to_unit(struct num *n);
109
110struct num op_num(const struct expr *self, const struct frame *frame);
111struct num op_var(const struct expr *self, const struct frame *frame);
112struct num op_string(const struct expr *self, const struct frame *frame);
113
114struct num op_sin(const struct expr *self, const struct frame *frame);
115struct num op_cos(const struct expr *self, const struct frame *frame);
116struct num op_sqrt(const struct expr *self, const struct frame *frame);
117
118struct num op_minus(const struct expr *self, const struct frame *frame);
119struct num op_floor(const struct expr *self, const struct frame *frame);
120
121struct num op_add(const struct expr *self, const struct frame *frame);
122struct num op_sub(const struct expr *self, const struct frame *frame);
123struct num op_mult(const struct expr *self, const struct frame *frame);
124struct num op_div(const struct expr *self, const struct frame *frame);
125
126struct expr *new_op(op_type op);
127struct expr *binary_op(op_type op, struct expr *a, struct expr *b);
128
129int var_eq(const struct frame *frame, const char *name,
130    const struct expr *expr);
131
132struct 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 */
138const char *eval_str(const struct expr *expr, const struct frame *frame);
139
140struct num eval_num(const struct expr *expr, const struct frame *frame);
141
142/* if frame == NULL, we only check the syntax without expanding */
143char *expand(const char *name, const struct frame *frame);
144
145struct expr *new_num(struct num num);
146struct expr *parse_expr(const char *s);
147void free_expr(struct expr *expr);
148
149int parse_var(const char *s, const char **id, struct value **values,
150    int max_values);
151int parse_values(const char *s, struct value **values);
152void free_values(struct value *values, int keep_expr);
153
154#endif /* !EXPR_H */
155

Archive Download this file

Branches:
master



interactive