Advertisement
Mr-A

AExpression.cpp

Oct 24th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.83 KB | None | 0 0
  1. #include "AExpression.h"
  2. #include "AScope.h"
  3. #include <sstream>
  4. #include <algorithm>
  5.  
  6. template <typename numeraltype> numeraltype ConvertToNumeral( std::string number )
  7. {
  8.     numeraltype value;
  9.     std::stringstream stream(number);
  10.     stream >> value;
  11.     return value;
  12. }
  13.  
  14. template <typename numeraltype> std::string ConvertToString(numeraltype value)
  15. {
  16.     std::ostringstream result;
  17.     result << value;
  18.     return result.str();
  19.  
  20. }
  21.  
  22. template <typename numeraltype> bool ConvertToBool(numeraltype value)
  23. {
  24.     if (value == numeraltype(0))
  25.         return false;
  26.     return true;
  27. }
  28.  
  29. template <typename numeraltype> void AExpression<numeraltype>::reset()
  30. {
  31.     _is_conditional = false;
  32.     _is_constant = false;
  33.     _is_function = false;
  34.     _is_variable = false;
  35.     _operator_stack.clear();
  36.     _value_stack.clear();
  37. }
  38.  
  39. template <typename numeraltype> AExpression<numeraltype>::AExpression( std::string expression_string )
  40. {
  41.     init( expression_string );
  42. }
  43.  
  44.  
  45. template <typename numeraltype> AExpression<numeraltype>::AExpression( std::string expression_string, AScope<numeraltype> * ascope )
  46. {
  47.     _ascope = ascope;
  48.     expression_string = processScopeTokens(expression_string);
  49.     init( expression_string );
  50. }
  51.  
  52. template <typename numeraltype> void AExpression<numeraltype>::setScope(AScope<numeraltype> * ascope)
  53. {
  54.     _ascope = ascope;
  55. }
  56.  
  57. template <typename numeraltype> std::string AExpression<numeraltype>::processScopeTokens(std::string expression_string)
  58. {
  59.     int token_pos;
  60.     for (int i = 0; i < _ascope->_variable_names.size(); i++)
  61.     {
  62.         std::string token = _ascope->_variable_names[i];
  63.         token_pos = expression_string.find(token);
  64.         while (token_pos != std::string::npos)
  65.         {
  66.             expression_string = expression_string.replace(token_pos, token.length(), "`v"+ConvertToString(i)+"`" );
  67.             token_pos = expression_string.find(token);
  68.         }
  69.     }
  70.  
  71.     for (int i = 0; i < _ascope->_function0_names.size(); i++)
  72.     {
  73.         std::string token = _ascope->_function0_names[i];
  74.         token_pos = expression_string.find(token);
  75.         while (token_pos != std::string::npos)
  76.         {
  77.             expression_string = expression_string.replace(token_pos, token.length(), "`f0"+ConvertToString(i) );
  78.             token_pos = expression_string.find(token);
  79.         }
  80.     }
  81.  
  82.     for (int i = 0; i < _ascope->_function1_names.size(); i++)
  83.     {
  84.         std::string token = _ascope->_function1_names[i];
  85.         token_pos = expression_string.find(token);
  86.         while (token_pos != std::string::npos)
  87.         {
  88.             expression_string = expression_string.replace(token_pos, token.length(), "`f1"+ConvertToString(i) );
  89.             token_pos = expression_string.find(token);
  90.         }
  91.     }
  92.  
  93.     for (int i = 0; i < _ascope->_function2_names.size(); i++)
  94.     {
  95.         std::string token = _ascope->_function2_names[i];
  96.         token_pos = expression_string.find(token);
  97.         while (token_pos != std::string::npos)
  98.         {
  99.             expression_string = expression_string.replace(token_pos, token.length(), "`f2"+ConvertToString(i) );
  100.             token_pos = expression_string.find(token);
  101.         }
  102.     }
  103.  
  104.     for (int i = 0; i < _ascope->_function3_names.size(); i++)
  105.     {
  106.         std::string token = _ascope->_function3_names[i];
  107.         token_pos = expression_string.find(token);
  108.         while (token_pos != std::string::npos)
  109.         {
  110.             expression_string = expression_string.replace(token_pos, token.length(), "`f3"+ConvertToString(i) );
  111.             token_pos = expression_string.find(token);
  112.         }
  113.     }
  114.  
  115.     for (int i = 0; i < _ascope->_function4_names.size(); i++)
  116.     {
  117.         std::string token = _ascope->_function4_names[i];
  118.         token_pos = expression_string.find(token);
  119.         while (token_pos != std::string::npos)
  120.         {
  121.             expression_string = expression_string.replace(token_pos, token.length(), "`f4"+ConvertToString(i) );
  122.             token_pos = expression_string.find(token);
  123.         }
  124.     }
  125.  
  126.     for (int i = 0; i < _ascope->_function5_names.size(); i++)
  127.     {
  128.         std::string token = _ascope->_function5_names[i];
  129.         token_pos = expression_string.find(token);
  130.         while (token_pos != std::string::npos)
  131.         {
  132.             expression_string = expression_string.replace(token_pos, token.length(), "`f5"+ConvertToString(i) );
  133.             token_pos = expression_string.find(token);
  134.         }
  135.     }
  136.  
  137.     return expression_string;
  138.  
  139. }
  140.  
  141. template <typename numeraltype> void AExpression<numeraltype>::pushOperator(AOperator oper)
  142. {
  143.     _operator_stack.push_back(oper);
  144. }
  145.  
  146. template <typename numeraltype> void AExpression<numeraltype>::pushOperand(AExpression<numeraltype> value)
  147. {
  148.     _value_stack.push_back(value);
  149. }
  150.  
  151. template <typename numeraltype> bool AExpression<numeraltype>::isConstant()
  152. {
  153.     if (_is_function || _is_variable || _is_conditional)
  154.     {
  155.         return false;
  156.     }
  157.     for (auto & e : _value_stack)
  158.     {
  159.         if (!e.isConstant())
  160.             return false;
  161.     }
  162.     return true;
  163. }
  164.  
  165. template <typename numeraltype> void AExpression<numeraltype>::init( std::string expression_string )
  166. {
  167.     while (expression_string.find("IF")!=std::string::npos)
  168.         expression_string = expression_string.replace(expression_string.find("IF"), 2, "~");
  169.     while (expression_string.find("THEN")!=std::string::npos)
  170.         expression_string = expression_string.replace(expression_string.find("THEN"), 4, "?");
  171.     while (expression_string.find("ELSE")!=std::string::npos)
  172.         expression_string = expression_string.replace(expression_string.find("ELSE"), 4, ":");
  173.     _expression_string = expression_string;
  174.     _expression_string = expression_string;
  175.     _is_constant = false;
  176.     int parantheses_normalizer = 0;
  177.     int func_bracket_normalizer = 0;
  178.     int if_else_normalizer = 0;
  179.     int if_endif_normalizer = 0;
  180.     int if_then_normalizer = 0;
  181.     bool is_sub_expression = false;
  182.     std::string last_token_value;
  183.  
  184.     std::string token;
  185.     std::string a_operator;
  186.     std::string sub_expression;
  187.     AOperator last_operator = A_NO_OPERATOR;
  188.     std::string character;
  189.     TokenType last_token = OPERATOR;
  190.     bool var_or_func_coming = false;
  191.     bool is_var = false, is_func = false;
  192.     int func_param_count;
  193.     int func_ptr_index;
  194.  
  195.     for (int char_position = 0; char_position < expression_string.length() ; char_position++)
  196.     {
  197.         character = expression_string[char_position];
  198.         if (character == " " || character == "\n" || character == "\t") continue;
  199.         if (last_token == OPERATOR)
  200.         {
  201.             if (var_or_func_coming)
  202.             {
  203.                 if (character == "v")
  204.                 {
  205.                     is_var = true;
  206.                 }
  207.                 if (character == "f")
  208.                 {
  209.                     is_func = true;
  210.                     func_param_count = ConvertToNumeral<int>(expression_string.substr(++char_position, 1));
  211.                     while (expression_string.substr(++char_position, 1) != _ascope->_parameter_list_open)
  212.                     {
  213.                         token.append(expression_string.substr(char_position, 1));
  214.                     }
  215.                     char_position--;
  216.                     func_ptr_index = ConvertToNumeral<int>(token);
  217.                     token = "";
  218.  
  219.                 }
  220.                 var_or_func_coming = false;
  221.                 continue;
  222.             }
  223.             if (character == "`" && !is_sub_expression)
  224.             {
  225.                 if (is_var)
  226.                 {
  227.                     is_var = false;
  228.                     AExpression<numeraltype> var_expression;
  229.                     var_expression.setScope(_ascope);
  230.                     var_expression.setAsVar(ConvertToNumeral<int>(token));
  231.                     pushOperand(var_expression);
  232.                     last_token_value = "";
  233.                     token = "";
  234.                     last_token = OPERAND;
  235.                     continue;
  236.                 }
  237.                 var_or_func_coming = true;
  238.                 continue;
  239.             }
  240.  
  241.             if (is_var)
  242.             {
  243.                 token.append(character);
  244.                 continue;
  245.             }
  246.  
  247.             if (character == "~" && !is_sub_expression)
  248.             {
  249.                 if_endif_normalizer ++;
  250.                 if_then_normalizer ++;
  251.                 if_else_normalizer ++;
  252.                 token = "";
  253.                 AExpression<numeraltype> cond_expr;
  254.                 cond_expr.setScope(_ascope);
  255.                 cond_expr.setAsCond();
  256.                 bool got_condition = false, got_then = false, got_else = false;
  257.                 while (if_endif_normalizer != 0  && char_position+1 != expression_string.length())
  258.                 {
  259.                     char_position ++;
  260.                     character = expression_string[char_position];
  261.                     if (character == "~")
  262.                     {
  263.                         if_endif_normalizer++;
  264.                         if_then_normalizer++;
  265.                         if_else_normalizer++;
  266.                     }
  267.                     if (character == "?")
  268.                         if_then_normalizer--;
  269.                     if (character == ":")
  270.                         if_else_normalizer--;
  271.                     if (character == ";")
  272.                         if_endif_normalizer--;
  273.  
  274.                     if (if_endif_normalizer == 0 && !got_condition)
  275.                     {
  276.                         AExpression<numeraltype> then_expr;
  277.                         then_expr.setScope(_ascope);
  278.                         then_expr.init(token);
  279.                         cond_expr.pushOperand(then_expr);
  280.                         token = "";
  281.                         got_condition = true;
  282.                         continue;
  283.                     }
  284.  
  285.                     if (if_else_normalizer == 0 && !got_else)
  286.                     {
  287.                         AExpression<numeraltype> else_expr;
  288.                         else_expr.setScope(_ascope);
  289.                         else_expr.init(token);
  290.                         cond_expr.pushOperand(else_expr);
  291.                         token = "";
  292.                         got_else = true;
  293.                         continue;
  294.                     }
  295.  
  296.                     if (if_then_normalizer == 0 && !got_then)
  297.                     {
  298.                         AExpression<numeraltype> if_expr;
  299.                         if_expr.setScope(_ascope);
  300.                         if_expr.init(token);
  301.                         cond_expr.pushOperand(if_expr);
  302.                         token = "";
  303.                         got_then = true;
  304.                         continue;
  305.                     }
  306.  
  307.                     token.append(character);
  308.                 }
  309.                 pushOperand(cond_expr);
  310.                 if_endif_normalizer=0;
  311.                 if_then_normalizer=0;
  312.                 if_else_normalizer=0;
  313.                 last_token_value = "";
  314.                 continue;
  315.             }
  316.  
  317.             if (character == "(" || (is_func && character == _ascope->_parameter_list_open))
  318.             {
  319.                 if (is_func)
  320.                     func_bracket_normalizer++;
  321.                 else
  322.                     parantheses_normalizer++;
  323.                 if (!is_sub_expression)
  324.                 {
  325.                     is_sub_expression = true;
  326.                     continue;
  327.                 }
  328.             }
  329.  
  330.             if (is_func)
  331.             {
  332.                 if (character == _ascope->_parameter_list_close)
  333.                 {
  334.                     func_bracket_normalizer--;
  335.                     if (is_sub_expression && func_bracket_normalizer == 0)
  336.                     {
  337.                         is_sub_expression = false;
  338.                         AExpression func_call, param;
  339.                         func_call.setAsFunc(func_ptr_index, func_param_count);
  340.                         func_call.setScope(_ascope);
  341.                         for (int i = 0; i < func_param_count; i++)
  342.                         {
  343.                             param.setScope(_ascope);
  344.                             param.init(_ascope->getFuncParameter(token, i));
  345.                             func_call.pushOperand(param);
  346.                         }
  347.                         pushOperand(func_call);
  348.                         last_token_value = "";
  349.                         token = "";
  350.                         last_token = OPERAND;
  351.                         continue;
  352.                     }
  353.  
  354.                 }
  355.             }
  356.             else
  357.             {
  358.                 if (character == ")")
  359.                 {
  360.                     parantheses_normalizer--;
  361.                     if (is_sub_expression && parantheses_normalizer == 0)
  362.                     {
  363.                         is_sub_expression = false;
  364.                         AExpression<numeraltype> sub_expression;
  365.                         sub_expression.setScope(_ascope);
  366.                         sub_expression.init(token);
  367.                         _value_stack.push_back(sub_expression);
  368.                         last_token_value = token;
  369.                         token = "";
  370.                         last_token = OPERAND;
  371.                         continue;
  372.                     }
  373.                 }
  374.             }
  375.  
  376.             if ((is_sub_expression || isValidOperandCharacter(character[0]) || (isMinus(character[0]) && token == "")) )
  377.             {
  378.                 token.append(character);
  379.                 last_token_value = token;
  380.             }
  381.             else
  382.             {
  383.                 AExpression<numeraltype> the_operand(token);
  384.                 _value_stack.push_back(the_operand);
  385.                 last_token_value = token;
  386.                 char_position--;
  387.                 last_token = OPERAND;
  388.                 token = "";
  389.             }
  390.         }
  391.         else if (last_token == OPERAND)
  392.         {
  393.             if (token == "")
  394.             {
  395.                 token.append(character);
  396.                 continue;
  397.             }
  398.             else if (token == "+")
  399.             {
  400.                 _operator_stack.push_back(A_ADD);
  401.                 last_operator = A_ADD;
  402.                 char_position--;
  403.             }
  404.             else if (token == "-")
  405.             {
  406.                 _operator_stack.push_back(A_SUB);
  407.                 last_operator = A_SUB;
  408.                 char_position--;
  409.             }
  410.             else if (token == "/")
  411.             {
  412.                 _operator_stack.push_back(A_DIV);
  413.                 char_position--;
  414.                 last_operator = A_DIV;
  415.             }
  416.             else if (token == "*")
  417.             {
  418.                 _operator_stack.push_back(A_MUL);
  419.                 char_position--;
  420.                 last_operator = A_MUL;
  421.             }
  422.             else if (token == "%")
  423.             {
  424.                 _operator_stack.push_back(A_MOD);
  425.                 char_position--;
  426.                 last_operator = A_MOD;
  427.             }
  428.             else if (token == ">")
  429.             {
  430.                 if (character == "=")
  431.                 {
  432.                     _operator_stack.push_back(A_GEQ);
  433.                     last_operator = A_GEQ;
  434.                 }
  435.                 else
  436.                 {
  437.                     _operator_stack.push_back(A_GRT);
  438.                     last_operator = A_GRT;
  439.                     char_position--;
  440.                 }
  441.             }
  442.             else if (token == "<")
  443.             {
  444.                 if (character == "=")
  445.                 {
  446.                     _operator_stack.push_back(A_SEQ);
  447.                     last_operator = A_SEQ;
  448.                 }
  449.                 else
  450.                 {
  451.                     _operator_stack.push_back(A_SML);
  452.                     last_operator = A_SML;
  453.                     char_position--;
  454.                 }
  455.             }
  456.             else if (token == "!")
  457.             {
  458.                 if (character == "=")
  459.                 {
  460.                     _operator_stack.push_back(A_NEQ);
  461.                     last_operator = A_NEQ;
  462.                 }
  463.                 else
  464.                 {
  465.                     _operator_stack.push_back(A_NOT);
  466.                     last_operator = A_NOT;
  467.                     char_position--;
  468.                 }
  469.             }
  470.             else if (token == "=")
  471.             {
  472.                 if (character == "=")
  473.                 {
  474.                     _operator_stack.push_back(A_EQS);
  475.                     last_operator = A_EQS;
  476.                 }
  477.             }
  478.             token = "";
  479.             last_token = OPERATOR;
  480.         }
  481.     }
  482.  
  483.     _is_constant = _operator_stack.size() == 0 && isConstant();
  484.     if (_is_constant)
  485.         _constant_value = ConvertToNumeral<numeraltype>(last_token_value);
  486.     if (_operator_stack.size() > 0 && last_token_value != "")
  487.     {
  488.         AExpression<numeraltype> the_operand(last_token_value);
  489.         _value_stack.push_back(the_operand);
  490.     }
  491.  
  492.  
  493.     this->handlePrecedenceOfOperators();
  494. }
  495.  
  496. template <typename numeraltype> AExpression<numeraltype>::~AExpression()
  497. {
  498.     //dtor
  499. }
  500.  
  501. template <typename numeraltype> AExpression<numeraltype>::AExpression()
  502. {
  503.     //dtor
  504. }
  505.  
  506. template <typename numeraltype> void AExpression<numeraltype>::setAsCond()
  507. {
  508.     _is_conditional = true;
  509. }
  510.  
  511. template <typename numeraltype> void AExpression<numeraltype>::setAsVar(int var_ptr_index)
  512. {
  513.     _is_variable = true;
  514.     _scope_ptr_index = var_ptr_index;
  515. }
  516.  
  517. template <typename numeraltype> void AExpression<numeraltype>::setAsFunc(int func_ptr_index, unsigned int param_count)
  518. {
  519.     _is_function = true;
  520.     _scope_ptr_index = func_ptr_index;
  521.     _func_param_count = param_count;
  522. }
  523.  
  524. template <typename numeraltype> void AExpression<numeraltype>::handlePrecedenceOfOperators()
  525. {
  526.     int highest_precedence = 0;
  527.     int group_index;
  528.     AExpression<numeraltype> sub;
  529.     int operand_index = 0;
  530.     for (int operator_index = 0; operator_index < _operator_stack.size(); operator_index++)
  531.     {
  532.         group_index = getGroupIndex(_operator_stack[operator_index]);
  533.  
  534.         if (group_index != -1 && group_index > highest_precedence)
  535.         {
  536.             highest_precedence = group_index;
  537.             operand_index ++;
  538.         }
  539.         else if (group_index != -1 && group_index < highest_precedence)
  540.         {
  541.             sub.pushOperator(_operator_stack[operator_index]);
  542.             switch(group_index)
  543.             {
  544.             case 0:
  545.                 _operator_stack.erase(_operator_stack.begin()+operator_index, _operator_stack.begin()+operator_index+1);
  546.                 break;
  547.  
  548.             default:
  549.                 _operator_stack.erase(_operator_stack.begin()+operator_index, _operator_stack.begin()+operator_index+1);
  550.                 sub.pushOperand(_value_stack[operand_index]);
  551.                 sub.pushOperand(_value_stack[operand_index+1]);
  552.                 /*int temp_operator_index = operator_index;
  553.                 while ( temp_operator_index+1 < _operator_stack.size() )
  554.                 {
  555.                     a
  556.                 }*/
  557.                 _value_stack.erase(_value_stack.begin()+operand_index+1, _value_stack.begin()+operand_index+2);
  558.                 _value_stack[operand_index] = sub;
  559.                 sub.reset();
  560.                 operator_index --;
  561.  
  562.             }
  563.  
  564.         }
  565.         else if (group_index != -1)
  566.         {
  567.             switch(group_index)
  568.             {
  569.             case 0:
  570.                 _operator_stack.erase(_operator_stack.begin()+operator_index, _operator_stack.begin()+operator_index+1);
  571.                 break;
  572.  
  573.             default:
  574.                 operand_index += 2;
  575.  
  576.             }
  577. //            break;
  578.         }
  579.  
  580.     }
  581. }
  582.  
  583. template <typename numeraltype> int AExpression<numeraltype>::getGroupIndex(AOperator oper)
  584. {
  585.     std::vector<AOperator> g0 = {A_NOT};
  586.     std::vector<AOperator> g1 = {A_DIV, A_MUL, A_MOD};
  587.     std::vector<AOperator> g2 = {A_ADD, A_SUB};
  588.     std::vector<AOperator> g3 = {A_GRT, A_SML, A_GEQ, A_SEQ};
  589.     std::vector<AOperator> g4 = {A_EQS, A_NEQ};
  590.     std::vector<AOperator> g5 = {A_AND};
  591.     std::vector<AOperator> g6 = {A_OR};
  592.     std::vector<AOperator> g7 = {A_IF, A_THN, A_ELS, A_EDF};
  593.     std::vector< std::vector<AOperator>* > groups = {&g0, &g1, &g2, &g3, &g4, &g5, &g6, &g7};
  594.     for (int i = 0; i < groups.size(); i++)
  595.     {
  596.         std::vector<AOperator> group = *groups[i];
  597.         if ( any_of(group.begin(), group.end(), [oper](int a)
  598.     {
  599.         return oper == a;
  600.     }) )
  601.         return i;
  602.     }
  603.     return -1;
  604. }
  605.  
  606. template <typename numeraltype> bool AExpression<numeraltype>::isValidOperandCharacter( char c )
  607. {
  608.     return isdigit( (int)c) || c == '.';
  609. }
  610.  
  611. template <typename numeraltype> bool AExpression<numeraltype>::isMinus(char c)
  612. {
  613.     return c == '-';
  614. }
  615.  
  616. template <typename numeraltype> int wordInStringVector(std::vector<std::string> string_vector, std::string word, bool * fullword)
  617. {
  618.     if (string_vector.size() == 0)
  619.     {
  620.         *fullword = false;
  621.         return 0;
  622.     }
  623.     *fullword = true;
  624.     int common_chr_count = 0;
  625.     for ( auto & w : string_vector )
  626.     {
  627.         for (int i = 0; i < w.length(); i++)
  628.         {
  629.             if (i >= word.length())
  630.             {
  631.                 *fullword = false;
  632.                 break;
  633.             }
  634.  
  635.             if (w[i] == word[i])
  636.                 common_chr_count ++;
  637.             else
  638.             {
  639.                 *fullword = false;
  640.                 break;
  641.             }
  642.         }
  643.     }
  644.     return 0;
  645. }
  646.  
  647. template <typename numeraltype> inline void AExpression<numeraltype>::evalOperations()
  648. {
  649.     _final_value = _value_stack[0].evaluate();
  650.     int currend_operand = 1;
  651.     for (int operator_index = 0; operator_index < _operator_stack.size(); operator_index ++)
  652.     {
  653.         switch (_operator_stack[operator_index])
  654.         {
  655.         case A_ADD:
  656.             _final_value += _value_stack[currend_operand++].evaluate();
  657.             break;
  658.         case A_SUB:
  659.             _final_value -= _value_stack[currend_operand++].evaluate();
  660.             break;
  661.         case A_DIV:
  662.             _final_value /= _value_stack[currend_operand++].evaluate();
  663.             break;
  664.         case A_MUL:
  665.             _final_value *= _value_stack[currend_operand++].evaluate();
  666.             break;
  667.         case A_MOD:
  668.             numeraltype n;
  669.             n = _value_stack[currend_operand++].evaluate();
  670.             _final_value = _final_value - n * numeraltype(floor(_final_value/n));
  671.             break;
  672.         case A_SML:
  673.             if (_final_value < _value_stack[currend_operand++].evaluate())
  674.                 _final_value = numeraltype(1);
  675.             else
  676.                 _final_value = numeraltype(0);
  677.             break;
  678.         case A_GRT:
  679.             if (_final_value > _value_stack[currend_operand++].evaluate())
  680.                 _final_value = numeraltype(1);
  681.             else
  682.                 _final_value = numeraltype(0);
  683.             break;
  684.         case A_SEQ:
  685.             if (_final_value <= _value_stack[currend_operand++].evaluate())
  686.                 _final_value = numeraltype(1);
  687.             else
  688.                 _final_value = numeraltype(0);
  689.             break;
  690.         case A_GEQ:
  691.             if (_final_value >= _value_stack[currend_operand++].evaluate())
  692.                 _final_value = numeraltype(1);
  693.             else
  694.                 _final_value = numeraltype(0);
  695.             break;
  696.         case A_EQS:
  697.             if (_final_value == _value_stack[currend_operand++].evaluate())
  698.                 _final_value = numeraltype(1);
  699.             else
  700.                 _final_value = numeraltype(0);
  701.             break;
  702.         case A_NEQ:
  703.             if (_final_value != _value_stack[currend_operand++].evaluate())
  704.                 _final_value = numeraltype(1);
  705.             else
  706.                 _final_value = numeraltype(0);
  707.             break;
  708.         }
  709.     }
  710. }
  711. template <typename numeraltype> inline void AExpression<numeraltype>::evalVarsFuncsConds(AScope<numeraltype> * scope)
  712. {
  713.     if (_is_variable)
  714.     {
  715.         _final_value = _ascope->getVariableValue( _scope_ptr_index );
  716.     }
  717.     else if (_is_conditional)
  718.     {
  719.         if (ConvertToBool<numeraltype>(_value_stack[0].evaluate()))
  720.         {
  721.             _final_value = _value_stack[1].evaluate();
  722.         }
  723.         else
  724.         {
  725.             _final_value = _value_stack[2].evaluate();
  726.         }
  727.     }
  728.     else if (_is_function)
  729.     {
  730.         switch (_func_param_count)
  731.         {
  732.         case 0:
  733.             _final_value = _ascope->getFunction0Value(_scope_ptr_index);
  734.             break;
  735.         case 1:
  736.             _final_value = _ascope->getFunction1Value(_scope_ptr_index,
  737.                                               _value_stack[0].evaluate()
  738.                                              );
  739.             break;
  740.         case 2:
  741.             _final_value = _ascope->getFunction2Value(_scope_ptr_index,
  742.                                               _value_stack[0].evaluate(),
  743.                                               _value_stack[1].evaluate()
  744.                                              );
  745.             break;
  746.         case 3:
  747.             _final_value = _ascope->getFunction3Value(_scope_ptr_index,
  748.                                               _value_stack[0].evaluate(),
  749.                                               _value_stack[1].evaluate(),
  750.                                               _value_stack[2].evaluate()
  751.                                              );
  752.             break;
  753.         case 4:
  754.             _final_value = _ascope->getFunction4Value(_scope_ptr_index,
  755.                                               _value_stack[0].evaluate(),
  756.                                               _value_stack[1].evaluate(),
  757.                                               _value_stack[2].evaluate(),
  758.                                               _value_stack[3].evaluate()
  759.                                              );
  760.             break;
  761.         case 5:
  762.             _final_value = _ascope->getFunction5Value(_scope_ptr_index,
  763.                                               _value_stack[0].evaluate(),
  764.                                               _value_stack[1].evaluate(),
  765.                                               _value_stack[2].evaluate(),
  766.                                               _value_stack[3].evaluate(),
  767.                                               _value_stack[4].evaluate()
  768.                                              );
  769.             break;
  770.         default:
  771.             ;
  772.         }
  773.     }
  774. }
  775.  
  776. template <typename numeraltype> numeraltype AExpression<numeraltype>::evaluate()
  777. {
  778.     if (_is_constant)
  779.     {
  780.         _final_value = _constant_value;
  781.         return _constant_value;
  782.     }
  783.     else if (_is_variable || _is_conditional || _is_function)
  784.     {
  785.         evalVarsFuncsConds(_ascope);
  786.     }
  787.     else
  788.     {
  789.         evalOperations();
  790.     }
  791.     return _final_value;
  792. }
  793.  
  794.  
  795.  
  796. template <typename numeraltype> numeraltype AExpression<numeraltype>::getFinalValue()
  797. {
  798.     return _final_value;
  799. }
  800.  
  801. template class AExpression<float>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement