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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2596 by schoenebeck, Thu Jun 5 19:39:12 2014 UTC revision 3564 by schoenebeck, Sat Aug 24 09:18:57 2019 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014 Christian Schoenebeck   * Copyright (c) 2014-2019 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 8  Line 8 
8   */   */
9    
10  #include "common.h"  #include "common.h"
11    #include <iostream>
12    #include "editor/SourceToken.h"
13    
14  namespace LinuxSampler {  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 {      VMIntExpr* VMExpr::asInt() const {
52          return const_cast<VMIntExpr*>( dynamic_cast<const VMIntExpr*>(this) );          return const_cast<VMIntExpr*>( dynamic_cast<const VMIntExpr*>(this) );
53      }      }
# Line 19  namespace LinuxSampler { Line 56  namespace LinuxSampler {
56          return const_cast<VMStringExpr*>( dynamic_cast<const VMStringExpr*>(this) );          return const_cast<VMStringExpr*>( dynamic_cast<const VMStringExpr*>(this) );
57      }      }
58    
59        VMIntArrayExpr* VMExpr::asIntArray() const {
60            return const_cast<VMIntArrayExpr*>( dynamic_cast<const VMIntArrayExpr*>(this) );
61        }
62    
63        bool VMExpr::isModifyable() const {
64            const VMVariable* var = dynamic_cast<const VMVariable*>(this);
65            return (!var) ? false : var->isAssignable();
66        }
67    
68        bool VMFunction::acceptsArgUnitType(vmint iArg, StdUnit_t type) const {
69            return type == VM_NO_UNIT;
70        }
71    
72        bool VMFunction::acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const {
73            return false;
74        }
75    
76        bool VMFunction::acceptsArgFinal(vmint iArg) const {
77            return false;
78        }
79    
80        void VMFunction::wrnMsg(const String& txt) {
81            std::cout << "[ScriptVM] " << txt << std::endl;
82        }
83    
84        void VMFunction::errMsg(const String& txt) {
85            std::cerr << "[ScriptVM] " << txt << std::endl;
86        }
87    
88        ///////////////////////////////////////////////////////////////////////
89        // class 'VMIntExpr'
90    
91        vmint VMIntExpr::evalInt(MetricPrefix_t prefix) {
92            vmfloat f = (vmfloat) evalInt();
93            vmfloat factor = unitFactor() / _unitFactor(prefix);
94            return (vmint) f * factor;
95        }
96    
97        vmint VMIntExpr::evalInt(MetricPrefix_t prefix1, MetricPrefix_t prefix2) {
98            vmfloat f = (vmfloat) evalInt();
99            vmfloat factor = unitFactor() /
100                                ( _unitFactor(prefix1) * _unitFactor(prefix2) );
101            return (vmint) f * factor;
102        }
103        
104        ///////////////////////////////////////////////////////////////////////
105        // class 'VMSourceToken'
106    
107        VMSourceToken::VMSourceToken() : m_token(NULL) {
108        }
109    
110        VMSourceToken::VMSourceToken(SourceToken* ct) : m_token(ct) {
111        }
112    
113        VMSourceToken::VMSourceToken(const VMSourceToken& other) {
114            if (other.m_token) {
115                m_token = new SourceToken;
116                *m_token = *other.m_token;
117            } else m_token = NULL;
118        }
119    
120        VMSourceToken::~VMSourceToken() {
121            if (m_token) {
122                delete m_token;
123                m_token = NULL;
124            }
125        }
126    
127        VMSourceToken& VMSourceToken::operator=(const VMSourceToken& other) {
128            if (m_token) delete m_token;
129            if (other.m_token) {
130                m_token = new SourceToken;
131                *m_token = *other.m_token;
132            } else m_token = NULL;
133            return *this;
134        }
135    
136        String VMSourceToken::text() const {
137            return (m_token) ? m_token->text() : "";
138        }
139    
140        int VMSourceToken::firstLine() const {
141            return (m_token) ? m_token->firstLine() : 0;
142        }
143    
144        int VMSourceToken::firstColumn() const {
145            return (m_token) ? m_token->firstColumn() : 0;
146        }
147    
148        bool VMSourceToken::isEOF() const {
149            return (m_token) ? m_token->isEOF() : true;
150        }
151    
152        bool VMSourceToken::isNewLine() const {
153            return (m_token) ? m_token->isNewLine() : false;
154        }
155    
156        bool VMSourceToken::isKeyword() const {
157            return (m_token) ? m_token->isKeyword() : false;
158        }
159    
160        bool VMSourceToken::isVariableName() const {
161            return (m_token) ? m_token->isVariableName() : false;
162        }
163    
164        bool VMSourceToken::isIdentifier() const {
165            return (m_token) ? m_token->isIdentifier() : false;
166        }
167    
168        bool VMSourceToken::isNumberLiteral() const {
169            return (m_token) ? m_token->isNumberLiteral() : false;
170        }
171    
172        bool VMSourceToken::isStringLiteral() const {
173            return (m_token) ? m_token->isStringLiteral() : false;
174        }
175    
176        bool VMSourceToken::isComment() const {
177            return (m_token) ? m_token->isComment() : false;
178        }
179    
180        bool VMSourceToken::isPreprocessor() const {
181            return (m_token) ? m_token->isPreprocessor() : false;
182        }
183    
184        bool VMSourceToken::isMetricPrefix() const {
185            return (m_token) ? m_token->isMetricPrefix() : false;
186        }
187    
188        bool VMSourceToken::isStdUnit() const {
189            return (m_token) ? m_token->isStdUnit() : false;
190        }
191    
192        bool VMSourceToken::isOther() const {
193            return (m_token) ? m_token->isOther() : true;
194        }
195    
196        bool VMSourceToken::isIntegerVariable() const {
197            return (m_token) ? m_token->isIntegerVariable() : false;
198        }
199    
200        bool VMSourceToken::isStringVariable() const {
201            return (m_token) ? m_token->isStringVariable() : false;
202        }
203    
204        bool VMSourceToken::isArrayVariable() const {
205            return (m_token) ? m_token->isArrayVariable() : false;
206        }
207    
208        bool VMSourceToken::isEventHandlerName() const {
209            return (m_token) ? m_token->isEventHandlerName() : false;
210        }
211    
212  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.2596  
changed lines
  Added in v.3564

  ViewVC Help
Powered by ViewVC