⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expr.cxx

📁 ecos实时嵌入式操作系统
💻 CXX
📖 第 1 页 / 共 5 页
字号:
    CYG_PRECONDITION_CLASSC(expr);    CdlReference ref(reference);    expr->references.push_back(ref);    int result = (int) expr->references.size() - 1;    CYG_REPORT_RETVAL(result);    return result;}// A utility to add a subexpression, returning its index.static voidpush_subexpression(CdlExpression expr, const CdlSubexpression& subexpr){    CYG_REPORT_FUNCNAME("push_subexpression");    CYG_PRECONDITION_CLASSC(expr);    expr->sub_expressions.push_back(subexpr);    expr->first_subexpression = ((int) expr->sub_expressions.size()) - 1;    CYG_REPORT_RETURN();}// Another utility to hold of the most recent subexpressionstatic CdlSubexpression&current_subexpression(CdlExpression expr){    CYG_REPORT_FUNCNAME("current_subexpression");    CdlSubexpression& result = expr->sub_expressions[expr->first_subexpression];    CYG_REPORT_RETURN();    return result;}static voidparse_function(CdlExpression expr){    CYG_REPORT_FUNCNAME("parse_function");    CYG_REPORT_FUNCARG1XV(expr);    CYG_PRECONDITION_CLASSC(expr);    CdlSubexpression subexpr;    subexpr.op          = CdlExprOp_Function;    subexpr.func        = current_function_id;    int number_of_args  = CdlFunction::get_args_count(current_function_id);    CYG_ASSERTC((0 < number_of_args) && (number_of_args <= CdlFunction_MaxArgs));    std::string name    = current_special;    // check for the opening bracket: xyzzy(arg1, arg2)    next_token();    if (T_OpenBracket != current_token) {        throw CdlParseException(std::string("Expected opening bracket after function ") + name + "\n" + get_error_location());    }    next_token();    int i;    for (i = 0; i < number_of_args; i++) {        parse_expression(expr);        subexpr.args[i] = expr->first_subexpression;        if (i < (number_of_args - 1)) {            if (T_Comma != current_token) {                throw CdlParseException(std::string("Expected comma between arguments in function ") +                                        name + "\n" + get_error_location());            }            next_token();        }    }    if (T_Comma == current_token) {        throw CdlParseException(std::string("Too many arguments passed to function ") + name + "\n" + get_error_location());    }    if (T_CloseBracket != current_token) {        throw CdlParseException(std::string("Expected closing bracket after function ") + name + "\n" + get_error_location());    }    next_token();        // Allow the function implementation to check its arguments if it is so inclined.    CdlFunction::check(expr, subexpr);        push_subexpression(expr, subexpr);    CYG_REPORT_RETURN();}static voidparse_unary(CdlExpression expr){    CYG_REPORT_FUNCNAME("parse_operand");    CYG_REPORT_FUNCARG1XV(expr);    CYG_PRECONDITION_CLASSC(expr);    CdlSubexpression subexpr;    switch(current_token) {      case T_EOD :      {        // This warrants a special case        throw CdlParseException("End of expression reached when expecting an operand.\n" + get_error_location());      }      case T_Function :      {          parse_function(expr);          break;      }            case T_Reference :      {        subexpr.op              = CdlExprOp_Reference;        subexpr.reference_index = push_reference(expr, current_reference);        push_subexpression(expr, subexpr);        next_token();        break;      }            case T_String :      {        subexpr.op              = CdlExprOp_StringConstant;        subexpr.constants       = current_string;        push_subexpression(expr, subexpr);        next_token();        break;      }            case T_Integer :      {        subexpr.op               = CdlExprOp_IntegerConstant;        subexpr.constants.set_integer_value(current_int, current_format);        push_subexpression(expr, subexpr);        next_token();        break;      }            case T_Double :      {        subexpr.op              = CdlExprOp_DoubleConstant;        subexpr.constants.set_double_value(current_double, current_format);        push_subexpression(expr, subexpr);        next_token();        break;      }            case T_OpenBracket :      {        next_token();        parse_expression(expr);        if (T_CloseBracket != current_token) {            throw CdlParseException("Missing close bracket after subexpression.\n" + get_error_location());        }        next_token();        break;      }            case T_Minus :      {        next_token();        parse_unary(expr);        CdlSubexpression& last_sub      = current_subexpression(expr);        if (CdlExprOp_IntegerConstant == last_sub.op) {            // Do the negating inline, no need for another subexpression.            last_sub.constants = last_sub.constants.get_integer_value() * -1;        } else if (CdlExprOp_DoubleConstant == last_sub.op) {            last_sub.constants = last_sub.constants.get_double_value() * -1;        } else {            // We could detect certain cases such as string constants etc.            // For now don't bother.            subexpr.op          = CdlExprOp_Negate;            subexpr.lhs_index   = expr->first_subexpression;            push_subexpression(expr, subexpr);        }        break;      }            case T_Plus :      {        next_token();        parse_unary(expr);        CdlSubexpression& last_sub      = current_subexpression(expr);        if ((CdlExprOp_IntegerConstant == last_sub.op) || (CdlExprOp_DoubleConstant == last_sub.op)) {            // No need to do anything here.        } else {            subexpr.op          = CdlExprOp_Plus;            subexpr.lhs_index   = expr->first_subexpression;            push_subexpression(expr, subexpr);        }        break;      }      case T_Times :      {          next_token();          parse_unary(expr);          subexpr.op            = CdlExprOp_Indirect;          subexpr.lhs_index     = expr->first_subexpression;          push_subexpression(expr, subexpr);          break;      }            case T_Exclamation :      {          next_token();          parse_unary(expr);          subexpr.op            = CdlExprOp_LogicalNot;          subexpr.lhs_index     = expr->first_subexpression;          push_subexpression(expr, subexpr);          break;      }      case T_Tilde :      {          next_token();          parse_unary(expr);          subexpr.op            = CdlExprOp_BitNot;          subexpr.lhs_index     = expr->first_subexpression;          push_subexpression(expr, subexpr);          break;      }      case T_Questionmark:      {          // This is the `active' operator, it can only be applied directly to a reference.          next_token();          parse_unary(expr);          CdlSubexpression& last_sub = current_subexpression(expr);          if (CdlExprOp_Reference != last_sub.op) {              throw CdlParseException("The active operator ? can only be applied directly to a reference.\n" +                                      get_error_location());          }          // There is no point in creating a new subexpression object, just modify          // the existing one. This has the useful side effect of avoiding          // reference substitution in the eval code.          last_sub.op           = CdlExprOp_Active;          break;      }      default:      {        throw CdlParseException("Unexpected token `" + token_to_string() + "', expecting an operand.\n" +                                get_error_location());      }    }    CYG_REPORT_RETURN();}static voidparse_multiply(CdlExpression expr){    CYG_REPORT_FUNCNAME("parse_multiply");        parse_unary(expr);    while ((T_Times == current_token) || (T_Divide == current_token) || (T_Remainder == current_token)) {        CdlSubexpression subexpr;        subexpr.op      =            (T_Times  == current_token) ? CdlExprOp_Multiply :            (T_Divide == current_token) ? CdlExprOp_Divide : CdlExprOp_Remainder;        subexpr.lhs_index = expr->first_subexpression;                next_token();        parse_unary(expr);        subexpr.rhs_index = expr->first_subexpression;        push_subexpression(expr, subexpr);    }        CYG_REPORT_RETURN();}static voidparse_add(CdlExpression expr){    CYG_REPORT_FUNCNAME("parse_add");        parse_multiply(expr);    while ((T_Plus == current_token)  ||           (T_Minus == current_token) ||           (T_StringConcat == current_token)) {        CdlSubexpression subexpr;        subexpr.op = (T_Plus == current_token) ? CdlExprOp_Add :                     (T_Minus == current_token) ? CdlExprOp_Subtract :                     CdlExprOp_StringConcat;        subexpr.lhs_index = expr->first_subexpression;                next_token();        parse_multiply(expr);        subexpr.rhs_index = expr->first_subexpression;        push_subexpression(expr, subexpr);    }        CYG_REPORT_RETURN();}static voidparse_shift(CdlExpression expr){    CYG_REPORT_FUNCNAME("parse_shift");        parse_add(expr);    while ((T_LeftShift == current_token) || (T_RightShift == current_token)) {        CdlSubexpression subexpr;        subexpr.op = (T_LeftShift == current_token) ? CdlExprOp_LeftShift : CdlExprOp_RightShift;        subexpr.lhs_index = expr->first_subexpression;                next_token();        parse_add(expr);        subexpr.rhs_index = expr->first_subexpression;        push_subexpression(expr, subexpr);    }        CYG_REPORT_RETURN();}static voidparse_comparison(CdlExpression expr){    CYG_REPORT_FUNCNAME("parse_comparison");        parse_shift(expr);    while ((T_LessThan == current_token)    || (T_LessEqual    == current_token) ||           (T_GreaterThan == current_token) || (T_GreaterEqual == current_token))  {        CdlSubexpression subexpr;        subexpr.op =            (T_LessThan    == current_token) ? CdlExprOp_LessThan :             (T_LessEqual   == current_token) ? CdlExprOp_LessEqual :            (T_GreaterThan == current_token) ? CdlExprOp_GreaterThan : CdlExprOp_GreaterEqual;        subexpr.lhs_index = expr->first_subexpression;                next_token();        parse_shift(expr);        subexpr.rhs_index = expr->first_subexpression;        push_subexpression(expr, subexpr);    }        CYG_REPORT_RETURN();}static voidparse_equals(CdlExpression expr){    CYG_REPORT_FUNCNAME("parse_equals");        parse_comparison(expr);    while ((T_Equal == current_token) ||           (T_NotEqual == current_token)) {        CdlSubexpression subexpr;        subexpr.op = (T_Equal == current_token) ? CdlExprOp_Equal : CdlExprOp_NotEqual;        subexpr.lhs_index = expr->first_subexpression;                next_token();        parse_comparison(expr);        subexpr.rhs_index = expr->first_subexpression;        push_subexpression(expr, subexpr);    }        CYG_REPORT_RETURN();}static voidparse_bitand(CdlExpression expr){    CYG_REPORT_FUNCNAME("parse_bitand");        parse_equals(expr);    while (T_BitAnd == current_token) {        CdlSubexpression subexpr;        subexpr.op = CdlExprOp_BitAnd;        subexpr.lhs_index = expr->first_subexpression;                next_token();        parse_equals(expr);        subexpr.rhs_index = expr->first_subexpression;        push_subexpression(expr, subexpr);    }        CYG_REPORT_RETURN();}static voidparse_bitxor(CdlExpression expr){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -