void do_print (stack *stack) { if (peek_stack (stack, 0) != NULL){ print_bigint (peek_stack (stack, 0), stdout); } else { fprintf(stderr, "mydc: stack empty\n"); } }
void do_print_all (stack *stack) { int size = size_stack (stack); for (int index = 0; index < size; ++index) { if (peek_stack(stack, index) != NULL){ print_bigint (peek_stack (stack, index), stdout); } } }
void __parser(lp_token** input, int count) { success=false; int top_state=0; int input_index=0; int accept_state=16; int pop_count=0; int action=0; //initialize the stack at state 0 pcstack* parse_stack=new_pcstack(); push_stack(parse_stack,(void*)new_token(0,0)); while(true) { top_state=((lp_token*)peek_stack(parse_stack))->lex_id; if(input_index==count)action=parse_table[top_state][12]; else if(input[input_index]->lex_id>=12)return; else action=parse_table[top_state][input[input_index]->lex_id]; if(action==accept_state)//accept { action=-((lp_token*)peek_stack(parse_stack))->lex_id; __prh(&action,parse_stack); //printf("accept\n"); success=true; return; } if(action>0)//shift { //printf("shift\n"); push_stack(parse_stack,(void*)new_token(action,input[input_index]->lex_val)); ++input_index; } else if(action<0)//reduce { pop_count=__prh(&action,parse_stack); if(pop_count==-1)break;//catch errors here while(pop_count>0) { pop_stack(parse_stack); --pop_count; } push_stack(parse_stack,(void*)new_token(parse_table[((lp_token*)peek_stack(parse_stack))->lex_id][action],0)); //printf("reduce\n"); } else//error { //printf("error\n"); return; } } }
void do_print_all (stack *stack) { DEBUGS ('m', show_stack (stack)); int size = size_stack (stack); for (int index = 0; index < size; ++index) { print_bigint (peek_stack (stack, index)); } }
int main() { stack_sq S; int a[] = {3, 8, 5, 17, 9, 30, 15, 22}; int i; init_stack(&S, 5); for (i = 0; i < 8; i++) { push(&S, a[i]); } printf("%d ", pop(&S)); printf("%d \n", pop(&S)); push(&S, 68); printf("%d ", peek_stack(&S)); printf("%d \n", pop(&S)); while(!empty_stack(&S)) { printf("%d ", pop(&S)); } printf("\n"); clear_stack(&S); return 0; }
void multiply_top_stack(matrix * m){ matrix *copy = new matrix(); matrix *ptr = peek_stack(); copy_matrix(ptr,copy); matrix_multiply(copy,m,ptr); delete(copy); }
void do_print (stack *stack) { DEBUGS ('m', show_stack (stack)); if (empty_stack(stack)) { fprintf(stderr, "mydc: stack empty\n"); } else { print_bigint (peek_stack (stack, 0), stdout); } }
static void eval_assign_expression(SIMCAR_Interpreter *inter, SIMCAR_LocalEnvironment *env, Expression *left, Expression *expression) { SIMCAR_Value *src; SIMCAR_Value *dest; eval_expression(inter, env, expression); src = peek_stack(inter, 0); dest = get_lvalue(inter, env, left); *dest = *src; }
void do_print (stack *stack) { DEBUGS ('m', show_stack (stack)); print_bigint (peek_stack (stack, 0)); }
void normalization(point3d * eye, point3d * center, point3d * up, double near, double far, double left, double right, double bottom, double top){ matrix * t; matrix * r = new matrix(); matrix * shear = new matrix(); matrix * scaleXY = new matrix(); matrix * scaleZ = new matrix(); t = translate(-eye->ele[0], -eye->ele[1], -eye->ele[2]); double Wmagnitude = sqrt (pow((eye->ele[0] - center->ele[0]),2) + pow((eye->ele[1] - center->ele[1]),2) + pow((eye->ele[2] - center->ele[2]),2) ); point3d * w = new point3d; w->ele[3] = 1; w->ele[2] = (eye->ele[2] - center->ele[2]) / Wmagnitude; w->ele[1] = (eye->ele[1] - center->ele[1]) / Wmagnitude; w->ele[0] = (eye->ele[0] - center->ele[0]) / Wmagnitude; point3d * u = new point3d; crossProdcut(up, w, u); double Umagnitude = sqrt (pow(u->ele[0],2) + pow(u->ele[1],2) + pow(u->ele[2],2) ); u->ele[3] = 1; u->ele[2] = u->ele[2] / Umagnitude; u->ele[1] = u->ele[1] / Umagnitude; u->ele[0] = u->ele[0] / Umagnitude; point3d * v = new point3d; crossProdcut(w, u, v); double Vmagnitude = sqrt (pow(v->ele[0],2) + pow(v->ele[1],2) + pow(v->ele[2],2) ); v->ele[3] = 1; v->ele[2] = v->ele[2] / Vmagnitude; v->ele[1] = v->ele[1] / Vmagnitude; v->ele[0] = v->ele[0] / Vmagnitude; for(int i=0; i<4; i++){ for(int j=0; j<4; j++){ r->ele[i][j] = 0; } } r->ele[3][3] = 1; r->ele[0][0] = u->ele[0]; r->ele[0][1] = u->ele[1]; r->ele[0][2] = u->ele[2]; r->ele[1][0] = v->ele[0]; r->ele[1][1] = v->ele[1]; r->ele[1][2] = v->ele[2]; r->ele[2][0] = w->ele[0]; r->ele[2][1] = w->ele[1]; r->ele[2][2] = w->ele[2]; for(int i=0; i<4; i++){ for(int j=0; j<4; j++){ shear->ele[i][j] = 0; } } shear->ele[0][0] = 1; shear->ele[1][1] = 1; shear->ele[2][2] = 1; shear->ele[3][3] = 1; shear->ele[0][2] = (left + right) / (2 * near); shear->ele[1][2] = (top + bottom) / (2 * near); scaleXY = scale(((2 * near) / (right - left)), (2 * near) / (top - bottom), 1); scaleZ = scale(1 / far, 1 / far, 1 / far); matrix * temp = new matrix(); matrix_multiply (t, r, temp); matrix_multiply (temp, shear,normalized_matrix); matrix_multiply (normalized_matrix, scaleXY, temp); matrix_multiply (temp, scaleZ, normalized_matrix); delete(temp); delete(t); delete(r); delete(shear); delete(scaleXY); delete(scaleZ); // need check!!! matrix * topstack = peek_stack(); copy_matrix(normalized_matrix, topstack); // push into the stack?? delete normalized_matrix? }
static void eval_method_call_expression(SIMCAR_Interpreter *inter, SIMCAR_LocalEnvironment *env, Expression *expr) { SIMCAR_Value *left; SIMCAR_Value result; SIMCAR_Boolean error_flag = SIMCAR_FALSE; eval_expression(inter, env, expr->u.method_call_expression.expression); left = peek_stack(inter, 0); if (left->type == SIMCAR_ARRAY_VALUE) { if (!strcmp(expr->u.method_call_expression.identifier, "add")) { SIMCAR_Value *add; check_method_argument_count(expr->line_number, expr->u.method_call_expression .argument, 1); eval_expression(inter, env, expr->u.method_call_expression.argument ->expression); add = peek_stack(inter, 0); crb_array_add(inter, left->u.object, *add); pop_value(inter); result.type = SIMCAR_NULL_VALUE; } else if (!strcmp(expr->u.method_call_expression.identifier, "size")) { check_method_argument_count(expr->line_number, expr->u.method_call_expression .argument, 0); result.type = SIMCAR_INT_VALUE; result.u.int_value = left->u.object->u.array.size; } else if (!strcmp(expr->u.method_call_expression.identifier, "resize")) { SIMCAR_Value new_size; check_method_argument_count(expr->line_number, expr->u.method_call_expression .argument, 1); eval_expression(inter, env, expr->u.method_call_expression.argument ->expression); new_size = pop_value(inter); if (new_size.type != SIMCAR_INT_VALUE) { crb_runtime_error(expr->line_number, ARRAY_RESIZE_ARGUMENT_ERR, MESSAGE_ARGUMENT_END); } crb_array_resize(inter, left->u.object, new_size.u.int_value); result.type = SIMCAR_NULL_VALUE; } else { error_flag = SIMCAR_TRUE; } } else if (left->type == SIMCAR_STRING_VALUE) { if (!strcmp(expr->u.method_call_expression.identifier, "length")) { check_method_argument_count(expr->line_number, expr->u.method_call_expression .argument, 0); result.type = SIMCAR_INT_VALUE; result.u.int_value = strlen(left->u.object->u.string.string); } else { error_flag = SIMCAR_TRUE; } } else { error_flag = SIMCAR_TRUE; } if (error_flag) { crb_runtime_error(expr->line_number, NO_SUCH_METHOD_ERR, STRING_MESSAGE_ARGUMENT, "method_name", expr->u.method_call_expression.identifier, MESSAGE_ARGUMENT_END); } pop_value(inter); push_value(inter, &result); }
static void eval_binary_expression(SIMCAR_Interpreter *inter, SIMCAR_LocalEnvironment *env, ExpressionType operato, Expression *left, Expression *right) { SIMCAR_Value *left_val; SIMCAR_Value *right_val; SIMCAR_Value result; eval_expression(inter, env, left); eval_expression(inter, env, right); left_val = peek_stack(inter, 1); right_val = peek_stack(inter, 0); if (left_val->type == SIMCAR_INT_VALUE && right_val->type == SIMCAR_INT_VALUE) { eval_binary_int(inter, operato, left_val->u.int_value, right_val->u.int_value, &result, left->line_number); } else if (left_val->type == SIMCAR_DOUBLE_VALUE && right_val->type == SIMCAR_DOUBLE_VALUE) { eval_binary_double(inter, operato, left_val->u.double_value, right_val->u.double_value, &result, left->line_number); } else if (left_val->type == SIMCAR_INT_VALUE && right_val->type == SIMCAR_DOUBLE_VALUE) { eval_binary_double(inter, operato, (double)left_val->u.int_value, right_val->u.double_value, &result, left->line_number); } else if (left_val->type == SIMCAR_DOUBLE_VALUE && right_val->type == SIMCAR_INT_VALUE) { eval_binary_double(inter, operato, left_val->u.double_value, (double)right_val->u.int_value, &result, left->line_number); } else if (left_val->type == SIMCAR_BOOLEAN_VALUE && right_val->type == SIMCAR_BOOLEAN_VALUE) { result.type = SIMCAR_BOOLEAN_VALUE; result.u.boolean_value = eval_binary_boolean(inter, operato, left_val->u.boolean_value, right_val->u.boolean_value, left->line_number); } else if (left_val->type == SIMCAR_STRING_VALUE && operato == ADD_EXPRESSION) { chain_string(inter, left_val, right_val, &result); } else if (left_val->type == SIMCAR_STRING_VALUE && right_val->type == SIMCAR_STRING_VALUE) { result.type = SIMCAR_BOOLEAN_VALUE; result.u.boolean_value = eval_compare_string(operato, left_val, right_val, left->line_number); } else if (left_val->type == SIMCAR_NULL_VALUE || right_val->type == SIMCAR_NULL_VALUE) { result.type = SIMCAR_BOOLEAN_VALUE; result.u.boolean_value = eval_binary_null(inter, operato, left_val, right_val, left->line_number); } else { char *op_str = crb_get_operator_string(operato); crb_runtime_error(left->line_number, BAD_OPERAND_TYPE_ERR, STRING_MESSAGE_ARGUMENT, "operator", op_str, MESSAGE_ARGUMENT_END); } pop_value(inter); pop_value(inter); push_value(inter, &result); }