forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathequations.h
151 lines (127 loc) · 4.37 KB
/
equations.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
#ifndef EQUATIONS_H
#define EQUATIONS_H
// ****************************************************************************
// equations.h DB48X project
// ****************************************************************************
//
// File Description:
//
// Representation of equations from the equations library
// This is defined by the file `config/equations.csv'
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2024 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
#include "complex.h"
#include "constants.h"
#include <string.h>
GCP(equation);
struct equation : constant
// ----------------------------------------------------------------------------
// An equation stored in `config/equations.csv` file
// ----------------------------------------------------------------------------
{
equation(id type, uint index): constant(type, index) {}
static equation_p make(uint index)
{
return rt.make<equation>(ID_equation, index);
}
static equation_p make(id type, uint index)
{
return rt.make<equation>(type, index);
}
static equation_p lookup(utf8 name, size_t len, bool error)
{
return equation_p(do_lookup(equations, name, len, error));
}
static equation_p lookup(cstring name, bool error = true)
{
return lookup(utf8(name), strlen(name), error);
}
uint index() const
{
byte_p p = payload();
return leb128<uint>(p);
}
utf8 name(size_t *size = nullptr) const
{
return do_name(equations, size);
}
object_p value() const
{
if (object_p obj = do_value(equations))
if (id ty = obj->type())
if (ty == ID_equation || ty == ID_expression ||
ty == ID_list || ty == ID_array)
return obj;
return nullptr;
}
static const config equations;
OBJECT_DECL(equation);
PARSE_DECL(equation);
EVAL_DECL(equation);
RENDER_DECL(equation);
GRAPH_DECL(equation);
HELP_DECL(equation);
};
struct equation_menu : constant_menu
// ----------------------------------------------------------------------------
// A equation menu is like a standard menu, but with equations
// ----------------------------------------------------------------------------
{
equation_menu(id type) : constant_menu(type) { }
static utf8 name(id type, size_t &len);
MENU_DECL(equation_menu);
HELP_DECL(equation_menu);
};
#define ID(i)
#define EQUATION_MENU(EquationMenu) struct EquationMenu : equation_menu {};
#include "ids.tbl"
COMMAND_DECLARE_INSERT_HELP(EquationName,-1);
COMMAND_DECLARE_INSERT_HELP(EquationValue,-1);
COMMAND_DECLARE_INSERT_HELP(EquationSolver,-1);
COMMAND_DECLARE(LibEq, 1);
GCP(assignment);
struct assignment : complex
// ----------------------------------------------------------------------------
// An assignment is an operation in the form `A=B` that performs a Store
// ----------------------------------------------------------------------------
{
assignment(id type, algebraic_r name, algebraic_r value):
complex(type, name, value) {}
static assignment_p make(algebraic_g name, algebraic_g value,
id ty = ID_assignment)
{
if (!name|| !value)
return nullptr;
while (assignment_p asn = value->as<assignment>())
value = asn->value();
return rt.make<assignment>(ty, name, value);
}
algebraic_p name() const { return x(); }
algebraic_p value() const { return y(); }
public:
OBJECT_DECL(assignment);
EVAL_DECL(assignment);
PARSE_DECL(assignment);
RENDER_DECL(assignment);
GRAPH_DECL(assignment);
HELP_DECL(assignment);
};
#endif // EQUATIONS_H