/[svn]/linuxsampler/trunk/src/scriptvm/common.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/scriptvm/common.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3573 - (show annotations) (download)
Tue Aug 27 21:36:53 2019 UTC (4 years, 7 months ago) by schoenebeck
File size: 7558 byte(s)
NKSP: Introducing floating point support.

* NKSP language: Added support for NKSP real number literals and
  arithmetic operations on them (e.g. "(3.9 + 2.9) / 12.3 - 42.0").

* NKSP language: Added support for NKSP real number (floating point)
  script variables (declare ~foo := 3.4).

* NKSP language: Added support for NKSP real number (floating point)
  array script variables (declare ?foo[3] := ( 1.1, 2.7, 49.0 )).

* NKSP built-in script function "message()" accepts now real number
  argument as well.

* Added built-in NKSP script function "real_to_int()" and its short
  hand form "int()" for casting from real number to integer in NKSP
  scripts.

* Added built-in NKSP script function "int_to_real()" and its short
  hand form "real()" for casting from integer to real number in NKSP
  scripts.

* Bumped version (2.1.1.svn6).

1 /*
2 * Copyright (c) 2014-2019 Christian Schoenebeck
3 *
4 * http://www.linuxsampler.org
5 *
6 * This file is part of LinuxSampler and released under the same terms.
7 * See README file for details.
8 */
9
10 #include "common.h"
11 #include <iostream>
12 #include "editor/SourceToken.h"
13
14 namespace LinuxSampler {
15
16 ///////////////////////////////////////////////////////////////////////
17 // class 'VMUnit'
18
19 static vmfloat _unitFactor(MetricPrefix_t prefix) {
20 switch (prefix) {
21 case VM_NO_PREFIX: return 1.f;
22 case VM_KILO: return 1000.f;
23 case VM_HECTO: return 100.f;
24 case VM_DECA: return 10.f;
25 case VM_DECI: return 0.1f;
26 case VM_CENTI: return 0.01f;
27 case VM_MILLI: return 0.001f;
28 case VM_MICRO: return 0.000001f;
29 }
30 return 1.f;
31 }
32
33 vmfloat VMUnit::unitFactor(MetricPrefix_t prefix) {
34 return _unitFactor(prefix);
35 }
36
37 vmfloat VMUnit::unitFactor(MetricPrefix_t prefix1, MetricPrefix_t prefix2) {
38 return _unitFactor(prefix1) * _unitFactor(prefix2);
39 }
40
41 vmfloat VMUnit::unitFactor() const {
42 vmfloat f = 1.f;
43 for (vmuint i = 0; unitPrefix(i); ++i)
44 f *= _unitFactor(unitPrefix(i));
45 return f;
46 }
47
48 ///////////////////////////////////////////////////////////////////////
49 // class 'VMExpr'
50
51 VMIntExpr* VMExpr::asInt() const {
52 return const_cast<VMIntExpr*>( dynamic_cast<const VMIntExpr*>(this) );
53 }
54
55 VMRealExpr* VMExpr::asReal() const {
56 return const_cast<VMRealExpr*>( dynamic_cast<const VMRealExpr*>(this) );
57 }
58
59 VMScalarNumberExpr* VMExpr::asScalarNumberExpr() const {
60 return const_cast<VMScalarNumberExpr*>(
61 dynamic_cast<const VMScalarNumberExpr*>(this)
62 );
63 }
64
65 VMStringExpr* VMExpr::asString() const {
66 return const_cast<VMStringExpr*>( dynamic_cast<const VMStringExpr*>(this) );
67 }
68
69 VMIntArrayExpr* VMExpr::asIntArray() const {
70 return const_cast<VMIntArrayExpr*>( dynamic_cast<const VMIntArrayExpr*>(this) );
71 }
72
73 VMRealArrayExpr* VMExpr::asRealArray() const {
74 return const_cast<VMRealArrayExpr*>(
75 dynamic_cast<const VMRealArrayExpr*>(this)
76 );
77 }
78
79 bool VMExpr::isModifyable() const {
80 const VMVariable* var = dynamic_cast<const VMVariable*>(this);
81 return (!var) ? false : var->isAssignable();
82 }
83
84 bool VMFunction::acceptsArgUnitType(vmint iArg, StdUnit_t type) const {
85 return type == VM_NO_UNIT;
86 }
87
88 bool VMFunction::acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const {
89 return false;
90 }
91
92 bool VMFunction::acceptsArgFinal(vmint iArg) const {
93 return false;
94 }
95
96 void VMFunction::wrnMsg(const String& txt) {
97 std::cout << "[ScriptVM] " << txt << std::endl;
98 }
99
100 void VMFunction::errMsg(const String& txt) {
101 std::cerr << "[ScriptVM] " << txt << std::endl;
102 }
103
104 ///////////////////////////////////////////////////////////////////////
105 // class 'VMIntExpr'
106
107 vmint VMIntExpr::evalInt(MetricPrefix_t prefix) {
108 vmfloat f = (vmfloat) evalInt();
109 vmfloat factor = unitFactor() / _unitFactor(prefix);
110 return (vmint) f * factor;
111 }
112
113 vmint VMIntExpr::evalInt(MetricPrefix_t prefix1, MetricPrefix_t prefix2) {
114 vmfloat f = (vmfloat) evalInt();
115 vmfloat factor = unitFactor() /
116 ( _unitFactor(prefix1) * _unitFactor(prefix2) );
117 return (vmint) f * factor;
118 }
119
120 ///////////////////////////////////////////////////////////////////////
121 // class 'VMRealExpr'
122
123 vmfloat VMRealExpr::evalReal(MetricPrefix_t prefix) {
124 vmfloat f = evalReal();
125 vmfloat factor = unitFactor() / _unitFactor(prefix);
126 return f * factor;
127 }
128
129 vmfloat VMRealExpr::evalReal(MetricPrefix_t prefix1, MetricPrefix_t prefix2) {
130 vmfloat f = evalReal();
131 vmfloat factor = unitFactor() /
132 ( _unitFactor(prefix1) * _unitFactor(prefix2) );
133 return f * factor;
134 }
135
136 ///////////////////////////////////////////////////////////////////////
137 // class 'VMSourceToken'
138
139 VMSourceToken::VMSourceToken() : m_token(NULL) {
140 }
141
142 VMSourceToken::VMSourceToken(SourceToken* ct) : m_token(ct) {
143 }
144
145 VMSourceToken::VMSourceToken(const VMSourceToken& other) {
146 if (other.m_token) {
147 m_token = new SourceToken;
148 *m_token = *other.m_token;
149 } else m_token = NULL;
150 }
151
152 VMSourceToken::~VMSourceToken() {
153 if (m_token) {
154 delete m_token;
155 m_token = NULL;
156 }
157 }
158
159 VMSourceToken& VMSourceToken::operator=(const VMSourceToken& other) {
160 if (m_token) delete m_token;
161 if (other.m_token) {
162 m_token = new SourceToken;
163 *m_token = *other.m_token;
164 } else m_token = NULL;
165 return *this;
166 }
167
168 String VMSourceToken::text() const {
169 return (m_token) ? m_token->text() : "";
170 }
171
172 int VMSourceToken::firstLine() const {
173 return (m_token) ? m_token->firstLine() : 0;
174 }
175
176 int VMSourceToken::firstColumn() const {
177 return (m_token) ? m_token->firstColumn() : 0;
178 }
179
180 bool VMSourceToken::isEOF() const {
181 return (m_token) ? m_token->isEOF() : true;
182 }
183
184 bool VMSourceToken::isNewLine() const {
185 return (m_token) ? m_token->isNewLine() : false;
186 }
187
188 bool VMSourceToken::isKeyword() const {
189 return (m_token) ? m_token->isKeyword() : false;
190 }
191
192 bool VMSourceToken::isVariableName() const {
193 return (m_token) ? m_token->isVariableName() : false;
194 }
195
196 bool VMSourceToken::isIdentifier() const {
197 return (m_token) ? m_token->isIdentifier() : false;
198 }
199
200 bool VMSourceToken::isNumberLiteral() const {
201 return (m_token) ? m_token->isNumberLiteral() : false;
202 }
203
204 bool VMSourceToken::isStringLiteral() const {
205 return (m_token) ? m_token->isStringLiteral() : false;
206 }
207
208 bool VMSourceToken::isComment() const {
209 return (m_token) ? m_token->isComment() : false;
210 }
211
212 bool VMSourceToken::isPreprocessor() const {
213 return (m_token) ? m_token->isPreprocessor() : false;
214 }
215
216 bool VMSourceToken::isMetricPrefix() const {
217 return (m_token) ? m_token->isMetricPrefix() : false;
218 }
219
220 bool VMSourceToken::isStdUnit() const {
221 return (m_token) ? m_token->isStdUnit() : false;
222 }
223
224 bool VMSourceToken::isOther() const {
225 return (m_token) ? m_token->isOther() : true;
226 }
227
228 bool VMSourceToken::isIntegerVariable() const {
229 return (m_token) ? m_token->isIntegerVariable() : false;
230 }
231
232 bool VMSourceToken::isRealVariable() const {
233 return (m_token) ? m_token->isRealVariable() : false;
234 }
235
236 bool VMSourceToken::isStringVariable() const {
237 return (m_token) ? m_token->isStringVariable() : false;
238 }
239
240 bool VMSourceToken::isIntArrayVariable() const {
241 return (m_token) ? m_token->isIntegerArrayVariable() : false;
242 }
243
244 bool VMSourceToken::isRealArrayVariable() const {
245 return (m_token) ? m_token->isRealArrayVariable() : false;
246 }
247
248 bool VMSourceToken::isArrayVariable() const { // deprecated API: will be removed !
249 return isIntArrayVariable();
250 }
251
252 bool VMSourceToken::isEventHandlerName() const {
253 return (m_token) ? m_token->isEventHandlerName() : false;
254 }
255
256 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC