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

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

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

revision 2581 by schoenebeck, Fri May 30 12:48:05 2014 UTC revision 2888 by schoenebeck, Sun Apr 24 18:16:10 2016 UTC
# Line 11  Line 11 
11  #include <string.h>  #include <string.h>
12  #include "tree.h"  #include "tree.h"
13  #include "../common/global_private.h"  #include "../common/global_private.h"
14    #include <assert.h>
15    
16  namespace LinuxSampler {  namespace LinuxSampler {
17            
# Line 143  void Args::dump(int level) { Line 144  void Args::dump(int level) {
144      printf(")\n");      printf(")\n");
145  }  }
146    
147    bool Args::isPolyphonic() const {
148        for (int i = 0; i < args.size(); ++i)
149            if (args[i]->isPolyphonic())
150                return true;
151        return false;
152    }
153    
154  EventHandlers::EventHandlers() {  EventHandlers::EventHandlers() {
155      //printf("EventHandlers::Constructor 0x%lx\n", (long long)this);      //printf("EventHandlers::Constructor 0x%lx\n", (long long)this);
156  }  }
# Line 176  EventHandler* EventHandlers::eventHandle Line 184  EventHandler* EventHandlers::eventHandle
184      return const_cast<EventHandler*>(&*args.at(index));      return const_cast<EventHandler*>(&*args.at(index));
185  }  }
186    
187    bool EventHandlers::isPolyphonic() const {
188        for (int i = 0; i < args.size(); ++i)
189            if (args[i]->isPolyphonic())
190                return true;
191        return false;
192    }
193    
194  Assignment::Assignment(VariableRef variable, ExpressionRef value)  Assignment::Assignment(VariableRef variable, ExpressionRef value)
195     : variable(variable), value(value)     : variable(variable), value(value)
196  {  {
# Line 193  StmtFlags_t Assignment::exec() { Line 208  StmtFlags_t Assignment::exec() {
208      return STMT_SUCCESS;      return STMT_SUCCESS;
209  }  }
210    
211    EventHandler::EventHandler(StatementsRef statements) {
212        this->statements = statements;
213        usingPolyphonics = statements->isPolyphonic();
214    }
215    
216  void EventHandler::dump(int level) {  void EventHandler::dump(int level) {
217      printIndents(level);      printIndents(level);
218      printf("EventHandler {\n");      printf("EventHandler {\n");
# Line 216  Statement* Statements::statement(uint i) Line 236  Statement* Statements::statement(uint i)
236      return &*args.at(i);      return &*args.at(i);
237  }  }
238    
239    bool Statements::isPolyphonic() const {
240        for (int i = 0; i < args.size(); ++i)
241            if (args[i]->isPolyphonic())
242                return true;
243        return false;
244    }
245    
246  void FunctionCall::dump(int level) {  void FunctionCall::dump(int level) {
247      printIndents(level);      printIndents(level);
248      printf("FunctionCall '%s' args={\n", functionName.c_str());      printf("FunctionCall '%s' args={\n", functionName.c_str());
# Line 274  String FunctionCall::evalCastToStr() { Line 301  String FunctionCall::evalCastToStr() {
301  IntVariable::IntVariable(ParserContext* ctx)  IntVariable::IntVariable(ParserContext* ctx)
302      : Variable(ctx, ctx ? ctx->globalIntVarCount++ : 0, false), polyphonic(false)      : Variable(ctx, ctx ? ctx->globalIntVarCount++ : 0, false), polyphonic(false)
303  {  {
304        //printf("globalIntVar parserctx=0x%lx memPOS=%d\n", ctx, memPos);
305        assert(ctx);
306    }
307    
308    inline static int postfixInc(int& object, int incBy) {
309        const int i = object;
310        object += incBy;
311        return i;
312  }  }
313    
314  IntVariable::IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size)  IntVariable::IntVariable(ParserContext* ctx, bool polyphonic, bool bConst, int size)
315      : Variable(ctx, !ctx ? 0 : polyphonic ? ctx->polyphonicIntVarCount += size : ctx->globalIntVarCount += size, bConst),      : Variable(ctx, !ctx ? 0 : polyphonic ? postfixInc(ctx->polyphonicIntVarCount, size) : postfixInc(ctx->globalIntVarCount, size), bConst),
316        polyphonic(polyphonic)        polyphonic(polyphonic)
317  {  {
318        //printf("InvVar size=%d parserCtx=0x%lx\n", size, (uint64_t)ctx);
319        if (polyphonic) {
320            //printf("polyIntVar memPOS=%d\n", memPos);
321            assert(ctx);
322        }
323  }  }
324    
325  void IntVariable::assign(Expression* expr) {  void IntVariable::assign(Expression* expr) {
# Line 293  void IntVariable::assign(Expression* exp Line 333  void IntVariable::assign(Expression* exp
333    
334  int IntVariable::evalInt() {  int IntVariable::evalInt() {
335      //printf("IntVariable::eval pos=%d\n", memPos);      //printf("IntVariable::eval pos=%d\n", memPos);
336      if (polyphonic)      if (polyphonic) {
337            //printf("evalInt() poly memPos=%d execCtx=0x%lx\n", memPos, (uint64_t)context->execContext);
338          return context->execContext->polyphonicIntMemory[memPos];          return context->execContext->polyphonicIntMemory[memPos];
339        }
340      return (*context->globalIntMemory)[memPos];      return (*context->globalIntMemory)[memPos];
341  }  }
342    
343  void IntVariable::dump(int level) {  void IntVariable::dump(int level) {
344      printIndents(level);      printIndents(level);
345      printf("IntVariable memPos=%d\n", memPos);      //printf("IntVariable memPos=%d\n", memPos);
346  }  }
347    
348  //ConstIntVariable::ConstIntVariable(ParserContext* ctx, int value)  //ConstIntVariable::ConstIntVariable(ParserContext* ctx, int value)
# Line 329  void ConstIntVariable::dump(int level) { Line 371  void ConstIntVariable::dump(int level) {
371      printf("ConstIntVariable val=%d\n", value);      printf("ConstIntVariable val=%d\n", value);
372  }  }
373    
374    BuiltInIntVariable::BuiltInIntVariable(const String& name, VMIntRelPtr* ptr)
375        : IntVariable(NULL,false,false), name(name), ptr(ptr)
376    {
377    }
378    
379    void BuiltInIntVariable::assign(Expression* expr) {
380        IntExpr* valueExpr = dynamic_cast<IntExpr*>(expr);
381        if (!valueExpr) return;
382        ptr->assign(valueExpr->evalInt());
383    }
384    
385    int BuiltInIntVariable::evalInt() {
386        return ptr->evalInt();
387    }
388    
389    void BuiltInIntVariable::dump(int level) {
390        printIndents(level);
391        printf("Built-in IntVar '%s'\n", name.c_str());
392    }
393    
394  PolyphonicIntVariable::PolyphonicIntVariable(ParserContext* ctx)  PolyphonicIntVariable::PolyphonicIntVariable(ParserContext* ctx)
395      : IntVariable(ctx,true,false)      : IntVariable(ctx,true,false)
396  {  {
# Line 356  IntArrayVariable::IntArrayVariable(Parse Line 418  IntArrayVariable::IntArrayVariable(Parse
418      }      }
419  }  }
420    
421    IntArrayVariable::IntArrayVariable(ParserContext* ctx, bool bConst)
422        : Variable(ctx, 0, bConst)
423    {
424    }
425    
426  int IntArrayVariable::evalIntElement(uint i) {  int IntArrayVariable::evalIntElement(uint i) {
427      if (i >= values.size()) return 0;      if (i >= values.size()) return 0;
428      return values[i];      return values[i];
# Line 380  void IntArrayVariable::dump(int level) { Line 447  void IntArrayVariable::dump(int level) {
447      printf(")\n");      printf(")\n");
448  }  }
449    
450    BuiltInIntArrayVariable::BuiltInIntArrayVariable(const String& name, VMInt8Array* array)
451        : IntArrayVariable(NULL, false), name(name), array(array)
452    {
453    }
454    
455    int BuiltInIntArrayVariable::evalIntElement(uint i) {
456        return i >= array->size ? 0 : array->data[i];
457    }
458    
459    void BuiltInIntArrayVariable::assignIntElement(uint i, int value) {
460        if (i >= array->size) return;
461        array->data[i] = value;
462    }
463    
464    void BuiltInIntArrayVariable::dump(int level) {
465        printIndents(level);
466        printf("Built-In Int Array Variable '%s'\n", name.c_str());
467    }
468    
469  IntArrayElement::IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex)  IntArrayElement::IntArrayElement(IntArrayVariableRef array, IntExprRef arrayIndex)
470      : IntVariable(NULL, false, false, 0), array(array), index(arrayIndex)      : IntVariable(NULL, false, false, 0), array(array), index(arrayIndex)
471  {      {    
# Line 477  Statements* If::branch(uint i) const { Line 563  Statements* If::branch(uint i) const {
563      return NULL;      return NULL;
564  }  }
565    
566    bool If::isPolyphonic() const {
567        if (condition->isPolyphonic() || ifStatements->isPolyphonic())
568            return true;
569        return elseStatements ? elseStatements->isPolyphonic() : false;
570    }
571    
572  void SelectCase::dump(int level) {  void SelectCase::dump(int level) {
573      printIndents(level);      printIndents(level);
574      if (select)      if (select)
# Line 527  Statements* SelectCase::branch(uint i) c Line 619  Statements* SelectCase::branch(uint i) c
619      return NULL;      return NULL;
620  }  }
621    
622    bool SelectCase::isPolyphonic() const {
623        if (select->isPolyphonic()) return true;
624        for (int i = 0; i < branches.size(); ++i)
625            if (branches[i].statements->isPolyphonic())
626                return true;
627        return false;
628    }
629    
630  // void Case::addBranch(IntExprRef condition, StatementsRef statements) {  // void Case::addBranch(IntExprRef condition, StatementsRef statements) {
631  //     CaseBranchRef b = new CaseBranchRef;  //     CaseBranchRef b = new CaseBranchRef;
632  //     b->from = condition;  //     b->from = condition;
# Line 653  bool Relation::isConstExpr() const { Line 753  bool Relation::isConstExpr() const {
753    
754  int Or::evalInt() {  int Or::evalInt() {
755      IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs);      IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs);
756        if (pLHS->evalInt()) return 1;
757      IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);;      IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);;
758      return pLHS->evalInt() || pRHS->evalInt();      return (pRHS->evalInt()) ? 1 : 0;
759  }  }
760    
761  void Or::dump(int level) {  void Or::dump(int level) {
# Line 670  void Or::dump(int level) { Line 771  void Or::dump(int level) {
771    
772  int And::evalInt() {  int And::evalInt() {
773      IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs);      IntExpr* pLHS = dynamic_cast<IntExpr*>(&*lhs);
774      IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);;      if (!pLHS->evalInt()) return 0;
775      return pLHS->evalInt() && pRHS->evalInt();      IntExpr* pRHS = dynamic_cast<IntExpr*>(&*rhs);
776        return (pRHS->evalInt()) ? 1 : 0;
777  }  }
778    
779  void And::dump(int level) {  void And::dump(int level) {
# Line 718  StringVariableRef ParserContext::globalS Line 820  StringVariableRef ParserContext::globalS
820      return globalVar(name);      return globalVar(name);
821  }  }
822    
823  void ParserContext::addErr(int line, const char* txt) {  ParserContext::~ParserContext() {
824        destroyScanner();
825        if (globalIntMemory) {
826            delete globalIntMemory;
827            globalIntMemory = NULL;
828        }
829    }
830    
831    void ParserContext::addErr(int line, int column, const char* txt) {
832      ParserIssue e;      ParserIssue e;
833      e.type = PARSER_ERROR;      e.type = PARSER_ERROR;
834      e.txt = txt;      e.txt = txt;
835      e.line = line;      e.line = line;
836      errors.push_back(e);      e.column = column;
837      issues.push_back(e);      vErrors.push_back(e);
838        vIssues.push_back(e);
839  }  }
840    
841  void ParserContext::addWrn(int line, const char* txt) {  void ParserContext::addWrn(int line, int column, const char* txt) {
842      ParserIssue w;      ParserIssue w;
843      w.type = PARSER_WARNING;      w.type = PARSER_WARNING;
844      w.txt = txt;      w.txt = txt;
845      w.line = line;      w.line = line;
846      warnings.push_back(w);      w.column = column;
847      issues.push_back(w);      vWarnings.push_back(w);
848        vIssues.push_back(w);
849  }  }
850    
851  bool ParserContext::setPreprocessorCondition(const char* name) {  bool ParserContext::setPreprocessorCondition(const char* name) {
# Line 755  bool ParserContext::isPreprocessorCondit Line 867  bool ParserContext::isPreprocessorCondit
867      return userPreprocessorConditions.count(name);      return userPreprocessorConditions.count(name);
868  }  }
869    
870    std::vector<ParserIssue> ParserContext::issues() const {
871        return vIssues;
872    }
873    
874    std::vector<ParserIssue> ParserContext::errors() const {
875        return vErrors;
876    }
877    
878    std::vector<ParserIssue> ParserContext::warnings() const {
879        return vWarnings;
880    }
881    
882    VMEventHandler* ParserContext::eventHandler(uint index) {
883        if (!handlers) return NULL;
884        return handlers->eventHandler(index);
885    }
886    
887    VMEventHandler* ParserContext::eventHandlerByName(const String& name) {
888        if (!handlers) return NULL;
889        return handlers->eventHandlerByName(name);
890    }
891    
892    void ParserContext::registerBuiltInConstIntVariables(const std::map<String,int>& vars) {
893        for (std::map<String,int>::const_iterator it = vars.begin();
894             it != vars.end(); ++it)
895        {
896            ConstIntVariableRef ref = new ConstIntVariable(it->second);
897            vartable[it->first] = ref;
898        }
899    }
900    
901    void ParserContext::registerBuiltInIntVariables(const std::map<String,VMIntRelPtr*>& vars) {
902        for (std::map<String,VMIntRelPtr*>::const_iterator it = vars.begin();
903             it != vars.end(); ++it)
904        {
905            BuiltInIntVariableRef ref = new BuiltInIntVariable(it->first, it->second);
906            vartable[it->first] = ref;
907        }
908    }
909    
910    void ParserContext::registerBuiltInIntArrayVariables(const std::map<String,VMInt8Array*>& vars) {
911        for (std::map<String,VMInt8Array*>::const_iterator it = vars.begin();
912             it != vars.end(); ++it)
913        {
914            BuiltInIntArrayVariableRef ref = new BuiltInIntArrayVariable(it->first, it->second);
915            vartable[it->first] = ref;
916        }
917    }
918    
919  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.2581  
changed lines
  Added in v.2888

  ViewVC Help
Powered by ViewVC