Ejemplo n.º 1
0
/** execute_FOR  Executes for statement.
 *          initialize   condition     increment
 *      for(<statement>; <expression>; <expression>)
 *              <statement>;
 *
 * @param p_function_id
 */
void cx_executor::execute_FOR(cx_symtab_node * p_function_id) {

    bool condition = false;

    get_token(); // for
    // get the location of where to go to if <expr> is false.
    int break_point = get_location_marker();
    get_token();
    int statement_location = get_location_marker();
    get_token();
    int condition_marker = get_location_marker();
    get_token();
    int increment_marker = get_location_marker();


    get_token();

    // (
    get_token();

    if (token != tc_semicolon) {
        // declaration would go here //
        execute_assignment(p_node);
    }

    do {
        get_token(); //  ;
        if (token != tc_semicolon) {

            // expr 2
            execute_expression();
            get_token(); //  ;
        } else get_token();

        condition = top()->basic_types.bool__;
        pop();
        if (condition) {
            go_to(statement_location);
            get_token();
            execute_statement(p_function_id);
            if (break_loop) go_to(break_point);
        } else {
            go_to(break_point);
            get_token();
            break;
        }

        go_to(increment_marker);
        get_token();
        // expr 3
        execute_expression();

        go_to(condition_marker);
    } while (current_location() == condition_marker);

    break_loop = false;
}
Ejemplo n.º 2
0
static GLuint
execute_expressions (slang_string *output, grammar eid, const byte *expr, GLint results[2],
                     slang_info_log *elog)
{
   GLint success;
   byte *code;
   GLuint size, count = 0;

   success = grammar_fast_check (eid, expr, &code, &size, 64);
   if (success) {
      GLuint i = 0;

      while (code[i++] == EXP_EXPRESSION) {
         assert (count < 2);

         if (!execute_expression (output, code, &i, &results[count], elog)) {
            count = 0;
            break;
         }
         count++;
      }
      grammar_alloc_free (code);
   }
   else {
      slang_info_log_error (elog, "syntax error in preprocessor expression.");\
   }
   return count;
}
Ejemplo n.º 3
0
static void
parse_configuration_in(int check, FILE *fp, mipv6_conf_item_t *items)
{
	int wptr = 0, c, line = 1, col = 1, currarg = 0, comment = 0, a0ln, i;
	char wbuf[MAX_WORD_LENGTH], *params[MAX_PARAMS];

	while (1) {
		c = fgetc(fp);
		if (c < 0)
			break;

		if (check && !(isprint (c) || c == '\n'))
			exit_with_error_f(line, "Invalid character in column %i.",
					  col);
		col++;

		if (comment) {
			if (c == '\n') {
				comment = 0;
				line++;
			}
			continue;
		}

		if (isspace(c) || c == ';') {
			if (wptr > 0) {
				wbuf[wptr] = 0;
				wptr = 0;
				debug_assert(currarg < MAX_PARAMS,
					     "Too many parameters.");
				if (currarg == 0)
					a0ln = line;
				params[currarg++] = strdup(wbuf);
			}

			if (c == '\n') {
				line++;
				col = 1;
			}

			if (c == ';') {
				debug_assert(currarg > 0, "Empty expression.");
				execute_expression(a0ln, check, params,
						   currarg, items);
				for (i = 0; i < currarg; i++)
					free(params[i]);
				currarg = 0;
			}
		} else if (c == '#') {
			comment = 1;
		} else {
			wbuf[wptr++] = c;
		}

	}

	if (check && currarg > 0)
		exit_with_error_f(a0ln, "Unfinished expression");
}
Ejemplo n.º 4
0
/** execute_IF   Executes if statements.
 *
 *      if(<boolean expression>)
 *              <statement>;
 *      else if (<boolean expression>)
 *              <statement>;
 *      else
 *              <statement>;
 *
 * @param p_function_id : routine ID this statement is apart of.
 */
void cx_executor::execute_IF(cx_symtab_node * p_function_id) {
    //  if
    get_token();

    // get the location of where to go to if <expr> is false.
    int at_false = get_location_marker();
    get_token();

    // (
    //get_token();

    execute_expression();
    bool condition = top()->basic_types.bool__;

    // )
    //get_token();

    if (condition) {

        // True: { or single statement
        execute_statement(p_function_id);
        while (token == tc_semicolon)get_token();

        // If there is an ELSE part, jump around it.
        if (token == tc_ELSE) {
            get_token();
            go_to(get_location_marker());
            get_token(); // token following the IF statement
        }
    } else {

        // False: Go to the false location.
        go_to(at_false);
        get_token();

        if (token == tc_ELSE) {

            // ELSE <stmt-2>
            get_token();
            get_location_marker(); // skip over location marker
            // { or single statement
            get_token();
            execute_statement(p_function_id);

            while (token == tc_semicolon)get_token();
        }
    }
}
Ejemplo n.º 5
0
/** execute_actual_parameters	Execute the actual parameters of
 *				a declared subroutine call.
 *
 * @param p_function_id : ptr to the subroutine name's symtab node
 */
void cx_executor::execute_actual_parameters (cx_symtab_node *p_function_id) {
    cx_symtab_node *p_formal_id; // ptr to formal parm's symtab node

    // Loop to execute each actual parameter.
    for (p_formal_id = p_function_id->defn.routine.locals.p_parms_ids;
            p_formal_id;
            p_formal_id = p_formal_id->next__) {

        cx_type *p_formal_type = p_formal_id->p_type;
        get_token();

        /* Reference parameter: execute_variable will leave the actual
         * parameter's address on top of the stack. */
        if (p_formal_id->defn.how == dc_reference) {

            const int size = p_node->p_type->size;
            p_formal_type->form = p_node->p_type->form;
            execute_variable(p_node, true);

            if (p_formal_type->form == fc_array) {

                p_formal_type->size = size;
                p_formal_type->array.element_count = size;
                p_formal_type->array.max_index = size;

                p_formal_id->runstack_item = top();

            } else {
                p_formal_id->runstack_item = top();
            }

            get_token();
        }// value parameter
        else {
            cx_type *p_actual_type = execute_expression();

            if ((p_formal_type == p_float_type) &&
                    (p_actual_type->base_type() == p_integer_type)) {

                // real formal := integer actual:
                // convert integer value to real.
                float ff = top()->basic_types.int__;
                pop();

                push(ff);
                p_formal_id->runstack_item = top();
            } else if (!p_formal_type->is_scalar_type()) {

                /* Formal parameter is an array or a record:
                 * Make a copy of the actual parameter's value. */

                const int size = p_actual_type->size;
                void *p_target_address = nullptr;
                const int num_of_elements = size / p_actual_type->base_type()->size;

                p_target_address = realloc(p_target_address, size);
                //memset(p_target_address, 0, size);

                if (p_target_address == nullptr) {
                    perror("realloc");
                    exit(0);
                }

                void *p_source = top()->basic_types.addr__;
                memcpy(p_target_address, p_source, size);

                pop();
                push(p_target_address);

                p_formal_type->array.element_count = num_of_elements;
                p_formal_type->array.max_index = num_of_elements;
                p_formal_type->size = size;
                p_formal_type->form = fc_array;
                p_formal_id->runstack_item = top();
            } else {

                // Range check an integer or enumeration
                // formal parameter.
                range_check(p_formal_type, top()->basic_types.int__);
                p_formal_id->runstack_item = top();
            }
        }
    }

    if (token == tc_left_paren) get_token();
}