Esempio n. 1
0
struct program
aggregate_ops(struct program *prog) {
  struct program aggregated_program;
  aggregated_program.ops = calloc(sizeof(struct brainfuck_op), INITIAL_PROGRAM_SIZE);
  aggregated_program.length = 0;
  aggregated_program.capacity = INITIAL_PROGRAM_SIZE;

  enum brainfuck_op_type last_seen_type = NOP;
  int count = 1;
  for (size_t i = 0; i < prog->length; i++) {
    if (prog->ops[i].type == last_seen_type) {
      // if the type we're looking at is the same as the last one,
      // increase the count and continue.
      if (last_seen_type == BRANCH_LEFT || last_seen_type == BRANCH_RIGHT) {
        // if the previous type was a branch and this one was as well, do not
        // combine them.
        struct brainfuck_op op;
        op.type = last_seen_type;
        op.op_value = 0;
        insert_op(&aggregated_program, op);
        continue;
      }

      OPS_AGGREGATED++;
      count++;
    } else {
      // if we're looking at a type that's not the same as the last type
      // that we observed, we need to "finish" off the last type.
      struct brainfuck_op op;
      if (last_seen_type != NOP) {
        op.type = last_seen_type;
        op.op_value = count;
        insert_op(&aggregated_program, op);
      }
      last_seen_type = prog->ops[i].type;
      count = 1;
    }
  }

  if (count != 1) {
    struct brainfuck_op op;
    op.type = last_seen_type;
    op.op_value = count;
    insert_op(&aggregated_program, op);
  }

  // stick a terminator onto the new program
  struct brainfuck_op exit_op;
  exit_op.type = EXIT;
  exit_op.op_value = 0;
  insert_op(&aggregated_program, exit_op);

  destroy_program(prog);
  return aggregated_program;
}
int main(void){
  program_fkvm_t program;
  unsigned long size;

  size = load_program("program_input",&program);
  if (size == 0){
    perror("Couldn't load program");
    return 1;
  }
  
  for (int i=0; i<size; i++){
    printf("%d -> %c\n",i,get_index(&program,i));
  }

  destroy_program(&program);
  
  return 0;
}
Esempio n. 3
0
/***************************entry point*******************************/
plc_t parse_ld_program(const char * name, 
                       const char lines[][MAXSTR], 
                       plc_t p) {
    int rv = PLC_OK;
    if(p == NULL){
    
        return NULL;
    }    
    unsigned int len = program_length(lines, MAXBUF); 
    ld_line_t * program = construct_program(lines, len);
    
    int node = 0; 
    while(rv >= PLC_OK 
       && node >= 0) {
        rv = horizontal_parse(len, program);
        if(rv >= PLC_OK){
        
            node = find_next_node(program, node, len);
        }
        if(node >= 0){
        
            rv = vertical_parse(node, len, program);
        }
    }
    if(rv < PLC_OK){  
    
        p->status = rv;
    } else {
    
        p = generate_code(len, name, program, p);
        
        char dump[MAXBUF];
        memset(dump, 0, MAXBUF);
        dump_rung(p->rungs[0], dump);
        plc_log(dump);
    }
    destroy_program(len, program);
    return p;
}
Esempio n. 4
0
void
destroy_OGL() {
	destroy_shader(fragShader);
	destroy_shader(vertexShader);
	destroy_program(program);
	clear_attribute(vertPosAttrib);
	clear_attribute(texCoordAttrib);
	clear_uniform(colorUni);
	clear_uniform(diffuseSamplerUni);
	
	destroy_texture(whiteTexture);

	free(whiteTexture);

	free(program);

	free(fragShader);
	free(vertexShader);

	free(vertPosAttrib);
	free(texCoordAttrib);
	free(colorUni);
	free(diffuseSamplerUni);
}
Esempio n. 5
0
void tvm_destroy(tvm_t* vm)
{
	if(vm && vm->pMemory)destroy_memory(vm->pMemory);
	if(vm && vm->pProgram)destroy_program(vm->pProgram);
	if(vm) free(vm);
}