Example #1
0
/* machine printing, (write) */
void sexp_pprint(secd_t* secd, cell_t *port, const cell_t *cell) {
    switch (cell_type(cell)) {
      case CELL_UNDEF:  secd_pprintf(secd, port, "#?"); break;
      case CELL_INT:    secd_pprintf(secd, port, "%d", cell->as.num); break;
      case CELL_CHAR:
        if (isprint(cell->as.num))
            secd_pprintf(secd, port, "#\\%c", (char)cell->as.num);
        else
            secd_pprintf(secd, port, "#\\x%x", numval(cell));
        break;
      case CELL_OP:     sexp_print_opcode(secd, port, cell->as.op); break;
      case CELL_FUNC:   secd_pprintf(secd, port, "##func*%p", cell->as.ptr); break;
      case CELL_FRAME:  secd_pprintf(secd, port,
                                "##frame@%ld ", cell_index(secd, cell)); break;
      case CELL_KONT:   secd_pprintf(secd, port,
                                "##kont@%ld ", cell_index(secd, cell)); break;
      case CELL_CONS:   sexp_print_list(secd, port, cell); break;
      case CELL_ARRAY:  sexp_print_array(secd, port, cell); break;
      case CELL_STR:    secd_pprintf(secd, port, "\"%s\"", strval(cell) + cell->as.str.offset); break;
      case CELL_SYM:    secd_pprintf(secd, port, "%s", symname(cell)); break;
      case CELL_BYTES:  sexp_print_bytes(secd, port, strval(cell), mem_size(cell)); break;
      case CELL_ERROR:  secd_pprintf(secd, port, "#!\"%s\"", errmsg(cell)); break;
      case CELL_PORT:   sexp_pprint_port(secd, port, cell); break;
      case CELL_REF:    sexp_pprint(secd, port, cell->as.ref);  break;
      default: errorf("sexp_print: unknown cell type %d", (int)cell_type(cell));
    }
}
Example #2
0
void dbg_print_cell(secd_t *secd, const cell_t *c) {
    if (is_nil(c)) {
         secd_printf(secd, "NIL\n");
         return;
    }
    char buf[128];
    if (c->nref > DONT_FREE_THIS - 100000) strncpy(buf, "-", 64);
    else snprintf(buf, 128, "%ld", (long)c->nref);
    printf("[%ld]^%s: ", cell_index(secd, c), buf);

    switch (cell_type(c)) {
      case CELL_CONS:
        printf("CONS([%ld], [%ld])\n",
               cell_index(secd, get_car(c)), cell_index(secd, get_cdr(c)));
        break;
      case CELL_FRAME:
        printf("FRAME(syms: [%ld], vals: [%ld])\n",
               cell_index(secd, get_car(c)), cell_index(secd, get_cdr(c)));
        break;
      case CELL_INT:  printf("%d\n", c->as.num); break;
      case CELL_CHAR:
        if (isprint(c->as.num)) printf("#\\%c\n", (char)c->as.num);
        else printf("#x%x\n", c->as.num);
        break;
      case CELL_OP:
        sexp_print_opcode(secd, secd->output_port, c->as.op);
        printf("\n");
        break;
      case CELL_FUNC: printf("*%p()\n", c->as.ptr); break;
      case CELL_KONT: printf("KONT[%ld, %ld, %ld]\n",
                             cell_index(secd, c->as.kont.stack),
                             cell_index(secd, c->as.kont.env),
                             cell_index(secd, c->as.kont.ctrl)); break;
      case CELL_ARRAY: printf("ARR[%ld]\n",
                               cell_index(secd, arr_val(c, 0))); break;
      case CELL_STR: printf("STR[%ld\n",
                             cell_index(secd, (cell_t*)strval(c))); break;
      case CELL_SYM: printf("SYM[%08x]='%s'\n",
                             symhash(c), symname(c)); break;
      case CELL_BYTES: printf("BVECT[%ld]\n",
                               cell_index(secd, (cell_t*)strval(c))); break;
      case CELL_REF: printf("REF[%ld]\n",
                             cell_index(secd, c->as.ref)); break;
      case CELL_ERROR: printf("ERR[%s]\n", errmsg(c)); break;
      case CELL_ARRMETA: printf("META[%ld, %ld]\n",
                                 cell_index(secd, mcons_prev((cell_t*)c)),
                                 cell_index(secd, mcons_next((cell_t*)c))); break;
      case CELL_UNDEF: printf("#?\n"); break;
      case CELL_FREE: printf("FREE\n"); break;
      default: printf("unknown type: %d\n", cell_type(c));
    }
}
Example #3
0
File: parser.c Project: Saruta/a2c
struct expr *prefix(char *expect)
{
  struct expr *res = NULL;
  int toktype = tok->type;
  switch (toktype)
  {
    case IDENTIFIER:
      return identexpr(strdup(tok->val));
    case LPAREN:
      res = expression(0);
      eat(RPAREN);
      return res;
    case PLUS: case MINUS: case NOT:
      return unopexpr(toktype, expression(lbp(toktype) - 1));
    case REAL:
      return expr_from_val(realval(atof(tok->val)));
    case INT:
      return expr_from_val(intval(atoi(tok->val)));
    case CHAR:
      return expr_from_val(charval(tok->val[1]));
    case STRING:
      return expr_from_val(strval(strdup(tok->val)));
    case TRUE:
      return expr_from_val(boolval(1));
    case FALSE:
      return expr_from_val(boolval(0));
    case NULLKW:
      return nullexpr();
    default:
      syntaxerror("expected %s, not %s", expect, tok->val);
      return NULL;
  }
}
Example #4
0
void display_high_scores(Font *font) {
	load_high_scores();

	char str[256];
	for (u8 i = 0; i < MAX_HIGH_SCORES; i++) {
		Canvas *which_screen;
		u8 y_offset;

		if (i < 5) {
			which_screen = &screen_top;
			y_offset = 75;
		} else {
			which_screen = &screen_bottom;
			y_offset = 30;
		}

		strcpy(str, strval(i + 1));
		strcat(str, ". ");

		if (i < num_high_scores) {
			strcat(str, high_scores[i].name);
		}

		font->print_string(str, 25, y_offset + (i % 5) * 20, which_screen, RGB(31, 15, 0));

		if (i < num_high_scores) {
			font->print_number(high_scores[i].score, 200, y_offset + (i % 5) * 20, which_screen, RGB(31, 15, 0));
		}
	}
}
// parse: <CG_OPTIONS  POST_COMPILER="path+exe-name" POST_CALL="[%targetgenpath %postopt]|[<text>]" CUSTOM_DECLARATION="{AF69A710-4874-11D5-8546-009027A31158}" CUSTOM_DECL_CONFIG="4cOsai\\AutoDecl.ini" /> 
static void ReadCgOptions(CGT_CDiagMsg*    pdm,
                          IXMLDOMNodeList* pNList,
                          CG_OPTIONS*      pCgOptions)
{
    FC_CString              strval(MAX_PATH);
    IXMLDOMNode*            pNode;

    pNList->get_item(0, &pNode);

    if(CGT_XMLGetStrAttrTrim(pdm, pNode, L"MEMINFO", &strval, true, true))
        pCgOptions->bMemInfo = !_tcsicmp(strval, "1");
    
    CGT_XMLGetStrAttrTrim(pdm, pNode, L"POST_COMPILER", &pCgOptions->szPostCompileExe, true, true);

    // if POST_COMPILER is given POST_CALL is not optional
    CGT_XMLGetStrAttrTrim(pdm, 
                         pNode,
                         L"POST_CALL",
                         &pCgOptions->szPostCall,
                         true,
                         pCgOptions->szPostCompileExe.isEmpty());

    CGT_XMLGetCLSIDAttr(pdm, pNode, L"CUSTOM_DECLARATION", &pCgOptions->clsidAutoDecl, true);

    // if CUSTOM_DECLARATION is given CUSTOM_DECL_CONFIG is not optional
    CGT_XMLGetStrAttrTrim(pdm, 
                         pNode,
                         L"CUSTOM_DECL_CONFIG",
                         &pCgOptions->szAutoDeclIniFile,
                         true,
                         pCgOptions->clsidAutoDecl.Data1 == 0);

    FC_RELEASE_PTR(pNode);
}
Example #6
0
char *test_sort_key( struct job *job )
{
	char *cmpstr = 0;
	/* first key is DONE_TIME - done jobs come last */
	cmpstr = intval(DONE_TIME,&job->info,cmpstr);
	/* next key is REMOVE_TIME - removed jobs come before last */
	cmpstr = intval(REMOVE_TIME,&job->info,cmpstr);
	/* next key is ERROR - error jobs jobs come before removed */
	cmpstr = strzval(ERROR,&job->info,cmpstr);
	/* next key is HOLD - before the error jobs  */
	cmpstr = intval(HOLD_CLASS,&job->info,cmpstr);
	cmpstr = intval(HOLD_TIME,&job->info,cmpstr);
	/* next key is MOVE - before the held jobs  */
	cmpstr = strnzval(MOVE,&job->info,cmpstr);
	/* now by priority */
	if( Ignore_requested_user_priority_DYN == 0 ){
		cmpstr = strval(PRIORITY,&job->info,cmpstr,Reverse_priority_order_DYN);
	}
	/* now we do TOPQ */
	cmpstr = revintval(PRIORITY_TIME,&job->info,cmpstr);
	/* now we do FirstIn, FirstOut */
	cmpstr = intval(JOB_TIME,&job->info,cmpstr);
	cmpstr = intval(JOB_TIME_USEC,&job->info,cmpstr);
	/* now we do by job number if two at same time (very unlikely) */
	cmpstr = intval(NUMBER,&job->info,cmpstr);

	DEBUG4("test_sort_key: cmpstr '%s'", cmpstr );

	return(cmpstr);
}
Example #7
0
static Variant HHVM_METHOD(Collator, getSortKey, const String& val) {
  FETCH_COL(data, this_, false);
  UErrorCode error = U_ZERO_ERROR;
  icu::UnicodeString strval(u16(val, error));
  if (U_FAILURE(error)) {
    return false;
  }

  int sortkey_len = ucol_getSortKey(data->collator(),
                                    strval.getBuffer(), strval.length(),
                                    nullptr,
                                    0);
  if (sortkey_len <= 0) {
    return false;
  }

  String ret(sortkey_len + 1, ReserveString);
  sortkey_len = ucol_getSortKey(data->collator(),
                                strval.getBuffer(), strval.length(),
                                (uint8_t*) ret.get()->mutableData(),
                                ret.capacity() + 1);
  if (sortkey_len <= 0) {
    return false;
  }

  ret.setSize(sortkey_len);
  return ret;
}
Example #8
0
/* human-readable, (display) */
void sexp_display(secd_t *secd, cell_t *port, cell_t *cell) {
    switch (cell_type(cell)) {
      case CELL_STR:
        secd_pprintf(secd, port, "%s", strval(cell));
        break;
      default: sexp_pprint(secd, port, cell);
    }
}
Example #9
0
string Key::to_string() const {
  switch (type()) {
    case int_key_e:
      return dl_pstr ("%d", intval());
    case string_key_e:
      return dl_pstr ("%s", strval().c_str());
    case any_key_e:
      return "Any";
    default:
      dl_unreachable("...");
  }
  return "fail";
}
Example #10
0
void save_high_scores() {
	FILE *hiscores_file = fopen(options.full_path("hiscores"), "w");

	for (u8 i = 0; i < num_high_scores; i++) {
		// Write name
		fputs(high_scores[i].name, hiscores_file);
		fputc('\n', hiscores_file);

		// Write score
		fputs(strval(high_scores[i].score), hiscores_file);
		fputc('\n', hiscores_file);
	}

	fclose(hiscores_file);
}
Example #11
0
File: parser.c Project: Saruta/a2c
struct val *parse_val(void)
{
  next();
  switch (tok->type)
  {
    case INT: return intval(atoi(tok->val));
    case REAL: return realval(atof(tok->val));
    case STRING: return strval(tok->val);
    case TRUE: return boolval(true);
    case FALSE: return boolval(false);
    case CHAR: return charval(tok->val[1]);
    default:
               syntaxerror("expected a value, not %s", tok->val);
               return intval(0);
  }
}
Example #12
0
void configuration::load_config_file()
{
	std::shared_ptr<FILE>       pFile(fopen(PATH.c_str(), "r"), fclose);
	if (!pFile)
	{
		exit(EXIT_SUCCESS);
	}

	char*    line = NULL;
	size_t    len = 0;
	ssize_t   read_num = 0;
	std::vector<std::string>  str;

	while ((read_num = getline(&line, &len, pFile.get())) != -1) 
	{
		std::string  strval(line);
		str.push_back(strval);
		free(line);
        line = NULL;
	}

	analyse(str);
}
LevelSelector::LevelSelector(Font *the_font, Font *the_small_font, Color background) {
	font = the_font;
	small_font = the_small_font;
	background_color = background;

	level.init(&screen_top);

	num_buttons = level.get_num_levels();
	selected_button = 0;

	u8 current_button_x = LEVEL_SELECTOR_BUTTON_SPACING;
	u8 current_button_y = LEVEL_SELECTOR_BUTTON_SPACING;

	for (u8 current_button = 0; current_button < num_buttons; current_button++) {
		buttons[current_button] = new Button;
		buttons[current_button]->init(&screen_bottom, small_font, strval(level.get_level(current_button).id));
		buttons[current_button]->set_colors(BUTTON_COLORS, BUTTON_PRESSED_COLORS);
		buttons[current_button]->set_position(current_button_x, current_button_y);
		buttons[current_button]->set_width(LEVEL_SELECTOR_BUTTON_WIDTHS);
		buttons[current_button]->set_height(LEVEL_SELECTOR_BUTTON_HEIGHTS);

		if ((current_button + 1) % LEVEL_SELECTOR_BUTTON_COLUMNS == 0) {
			current_button_x = LEVEL_SELECTOR_BUTTON_SPACING;
			current_button_y += LEVEL_SELECTOR_BUTTON_HEIGHTS + LEVEL_SELECTOR_BUTTON_SPACING;
		} else {
			current_button_x += LEVEL_SELECTOR_BUTTON_WIDTHS + LEVEL_SELECTOR_BUTTON_SPACING;
		}
	}

	button_ok.init(&screen_bottom, font, "OK");
	button_ok.set_colors(BUTTON_COLORS, BUTTON_PRESSED_COLORS);
	button_ok.set_x(32);
	button_ok.set_y(BUTTON_OK_Y);
	button_ok.set_width(MENU_BUTTON_WIDTHS);
	button_ok.set_height(MENU_BUTTON_HEIGHTS);
}
Example #14
0
 void saveSettings( std::string file )
 {
     scoped_lock< mutex > slock( settings_mutex );
     
     std::fstream settings_file( file.c_str(), std::fstream::out | std::fstream::trunc );
     
     if( !settings_file.is_open() )
     {
         exception e;
         ff::write( *e,
                    "saveSettings(): Could not open file \"",
                    file,
                    "\"" );
         throw e;
     }
     
     ff::write( bqt_out,
                "Saving settings to \"",
                file,
                "\"\n" );
     
     std::map< std::string, std::pair< double     , bool > >::iterator num_iter = settings_num.begin();
     std::map< std::string, std::pair< std::string, bool > >::iterator str_iter = settings_str.begin();
     std::map< std::string, std::pair< bool       , bool > >::iterator bln_iter = settings_bln.begin();
     
     for( int i = settings_num.size() + settings_str.size() + settings_bln.size();
          i > 0;
          --i )
     {                                                                       // We ensure we never have duplicate keys across the three containers, so
                                                                             // checking each key individually (instead of in a tree) should be safe.
         if( num_iter != settings_num.end()
             && num_iter -> first < str_iter -> first
             && num_iter -> first < bln_iter -> first )
         {
             if( num_iter -> second.second )
             {
                 settings_file << num_iter -> first
                               << ' '
                               << num_iter -> second.first
                               << '\n';
             }
             
             ++num_iter;
             
             continue;
         }
         
         if( str_iter != settings_str.end()
             && str_iter -> first < num_iter -> first
             && str_iter -> first < bln_iter -> first )
         {
             if( str_iter -> second.second )
             {
                 std::string& strval( str_iter -> second.first );
                 
                 settings_file << " \"";
                 
                 for( int i = 0; i < strval.size(); ++i )
                 {
                     if( strval[ i ] == '\\' )
                         settings_file << "\\\\";
                     else
                         settings_file << strval[ i ];
                 }
                 
                 settings_file << "\"\n";
             }
             
             ++str_iter;
             
             continue;
         }
         
         if( bln_iter != settings_bln.end()
             && bln_iter -> first < str_iter -> first
             && bln_iter -> first < num_iter -> first )
         {
             if( bln_iter -> second.second )
             {
                 settings_file << bln_iter -> first
                               << ' '
                               << bln_iter -> second.first
                               << '\n';
             }
             
             ++bln_iter;
             
             continue;
         }
         
         exception e;
         ff::write( *e,
                    "saveSettings(): Failed to save a setting to file \"",
                    file,
                    "\"" );
         throw e;                                                            // Skipped if one of the above conditions passes
     }
 }
Example #15
0
void print_help_page(FontSet *fonts, u8 page, Canvas *buffer_top, Canvas *buffer_bottom) {
	Font *font_large = fonts->large_font;
	Font *font_small = fonts->small_font;

	buffer_top->clear(MENU_BACKGROUND_COLOR);
	buffer_bottom->clear(MENU_BACKGROUND_COLOR);

	buffer_top->rect(0, 0, SCREEN_WIDTH - 1, 23, RGB(8, 14, 20), RECT_FILLED);
	buffer_top->rect(0, 24, SCREEN_WIDTH - 1, 24, RGB(0, 0, 0), RECT_FILLED);

	char page_num[16] = "Page ";

	strcat(page_num, strval(page + 1));
	strcat(page_num, "/");
	strcat(page_num, strval(NUM_PAGES));

	font_large->print_string_right(page_num, 5, 5, buffer_top, RGB(31, 15, 0));

	Tile tile_mouse(options.full_path("tiles/mouse_big.tile"));
	tile_mouse.draw(buffer_top, 5, 6);

	Clock example_clock(buffer_top, CLOCK_X, 30);

	Tile tile_movable_block(options.full_path("tiles/movable_block.tile"));
	Tile tile_stationary_block(options.full_path("tiles/stationary_block.tile"));
	Tile tile_sink_hole(options.full_path("tiles/sinkhole.tile"));
	Tile tile_yarn(options.full_path("tiles/yarn.tile"));
	Tile tile_trap(options.full_path("tiles/trap.tile"));

	switch (page) {
		case 0:
			font_large->print_string("Intro", 22, 5, buffer_top, RGB(31, 15, 0));

			font_small->print_string("In Rodent's Revenge, you are a mouse who", 5, 30, buffer_top);
			font_small->print_string("gets his revenge on the cats by trapping", 5, 40, buffer_top);
			font_small->print_string("them. This is done by pushing around", 5, 50, buffer_top);
			font_small->print_string("blocks while avoiding dangers such as", 5, 60, buffer_top);
			font_small->print_string("mouse traps, sink holes, balls of yarn,", 5, 70, buffer_top);
			font_small->print_string("and the cats themselves who constantly", 5, 80, buffer_top);
			font_small->print_string("chase you.", 5, 90, buffer_top);

			font_small->print_string("There are 50 levels. You may choose", 5, 110, buffer_top);
			font_small->print_string("which level to start on. The higher", 5, 120, buffer_top);
			font_small->print_string("levels are more difficult, but give you", 5, 130, buffer_top);
			font_small->print_string("more points.", 5, 140, buffer_top);

			font_small->print_string("There are 5 speed settings. The game", 5, 160, buffer_top);
			font_small->print_string("speed affects the speed of the clock,", 5, 170, buffer_top);
			font_small->print_string("the cats, and the yarn.", 5, 180, buffer_top);
			break;

		case 1:
			font_large->print_string("Controls", 22, 5, buffer_top, RGB(31, 15, 0));

			font_small->print_string("Move the mouse up, down, left, and", 5, 30, buffer_top);
			font_small->print_string("right with the directional pad.", 5, 40, buffer_top);

			font_small->print_string("Pause/unpause the game with the", 5, 60, buffer_top);
			font_small->print_string("Start button.", 5, 70, buffer_top);
			break;

		case 2:
			font_large->print_string("Scoring", 22, 5, buffer_top, RGB(31, 15, 0));

			font_small->print_string("You get 100 points for eating a piece", 5, 30, buffer_top);
			font_small->print_string("of cheese.", 5, 40, buffer_top);

			font_small->print_string("You get points for every time the", 5, 60, buffer_top);
			font_small->print_string("second hand on the clock makes a", 5, 70, buffer_top);
			font_small->print_string("complete revolution. The amount of", 5, 80, buffer_top);
			font_small->print_string("points you get depends on what level you", 5, 90, buffer_top);
			font_small->print_string("are on. The higher levels give you more", 5, 100, buffer_top);
			font_small->print_string("points, while the lower levels give you", 5, 110, buffer_top);
			font_small->print_string("less points.", 5, 120, buffer_top);
			break;

		case 3:
			font_large->print_string("The Clock", 22, 5, buffer_top, RGB(31, 15, 0));

			example_clock.draw();

			font_small->print_string("The clock has a second hand, a minute", 5, 80, buffer_top);
			font_small->print_string("hand, and a blue line. The speed of the", 5, 90, buffer_top);
			font_small->print_string("clock depends on the game speed setting.", 5, 100, buffer_top);
			font_small->print_string("When the minute hand reaches the blue", 5, 110, buffer_top);
			font_small->print_string("line, new cats are spawned. If all cats", 5, 120, buffer_top);
			font_small->print_string("are trapped before the minute hand", 5, 130, buffer_top);
			font_small->print_string("reaches the blue line, the clock", 5, 140, buffer_top);
			font_small->print_string("fast-forwards up to the blue line. In", 5, 150, buffer_top);
			font_small->print_string("either case, the blue line then moves a", 5, 160, buffer_top);
			font_small->print_string("few minutes ahead. Once the minute hand", 5, 170, buffer_top);
			font_small->print_string("makes a full revolution around the clock,", 5, 180, buffer_top);

			font_small->print_string("the minute hand and blue line stop, and", 5, 5, buffer_bottom);
			font_small->print_string("your points start decreasing rapidly. If", 5, 15, buffer_bottom);
			font_small->print_string("all cats are trapped at this point, you", 5, 25, buffer_bottom);
			font_small->print_string("will move onto the next level before", 5, 35, buffer_bottom);
			font_small->print_string("your points decrease. Otherwise you must", 5, 45, buffer_bottom);
			font_small->print_string("trap all the remaining cats to move onto", 5, 55, buffer_bottom);
			font_small->print_string("the next level.", 5, 65, buffer_bottom);
			break;

		case 4:
			font_large->print_string("Tiles", 22, 5, buffer_top, RGB(31, 15, 0));

			tile_movable_block.draw(buffer_top, 5, 33);
			font_large->print_string("Movable Block", 17, 30, buffer_top);
			font_small->print_string("These blocks can be pushed by the mouse.", 5, 45, buffer_top);
			font_small->print_string("A whole row of any number of these blocks", 5, 55, buffer_top);
			font_small->print_string("can be pushed all at once as long as", 5, 65, buffer_top);
			font_small->print_string("there is a free tile for the last block", 5, 75, buffer_top);
			font_small->print_string("to move onto.", 5, 85, buffer_top);

			tile_stationary_block.draw(buffer_top, 5, 108);
			font_large->print_string("Stationary Block", 17, 105, buffer_top);
			font_small->print_string("These blocks cannot be moved or", 5, 120, buffer_top);
			font_small->print_string("destroyed.", 5, 130, buffer_top);

			tile_sink_hole.draw(buffer_top, 5, 153);
			font_large->print_string("Sink Hole", 17, 150, buffer_top);
			font_small->print_string("If you step onto one of these, you will", 5, 165, buffer_top);
			font_small->print_string("remain stuck in it for a few seconds. If", 5, 175, buffer_top);
			font_small->print_string("a movable block is pushed onto a sink", 5, 5, buffer_bottom);
			font_small->print_string("hole, the movable block disappears.", 5, 15, buffer_bottom);

			tile_yarn.draw(buffer_bottom, 5, 38);
			font_large->print_string("Ball of Yarn", 17, 35, buffer_bottom);
			font_small->print_string("These appear at the border of the level", 5, 50, buffer_bottom);
			font_small->print_string("randomly. They stay in one place for a", 5, 60, buffer_bottom);
			font_small->print_string("while and then suddenly move in one of", 5, 70, buffer_bottom);
			font_small->print_string("eight directions. If one touches you,", 5, 80, buffer_bottom);
			font_small->print_string("you die.", 5, 90, buffer_bottom);

			tile_trap.draw(buffer_bottom, 5, 113);
			font_large->print_string("Mouse Trap", 17, 110, buffer_bottom);
			font_small->print_string("If you step on one of these, you die", 5, 125, buffer_bottom);
			font_small->print_string("instantly.", 5, 135, buffer_bottom);
			break;	
	}
}
Example #16
0
/*
 *	recursive expression parser
 *
 *	Input: pointer to argument rest string
 *
 *	Output: computed value
 */
int eval(char *s)
{
	register char *p;
	register int val;
	char word[MAXLINE];
	struct sym *sp;

	val = 0;
	while (*s) {
		p = word;
		if (*s == '(') {
			s++;
			while (*s != ')') {
				if (*s == '\0') {
					asmerr(E_MISPAR);
					goto eval_break;
				}
				*p++ = *s++;
			}
			*p = '\0';
			s++;
			val = eval(word);
			continue;
		}
		if (*s == STRSEP) {
			s++;
			while (*s != STRSEP) {
				if (*s == '\n' || *s == '\0') {
					asmerr(E_MISHYP);
					goto hyp_error;
				}
				*p++ = *s++;
			}
			s++;
hyp_error:
			*p = '\0';
			val = strval(word);
			continue;
		}
		if (isari(*s))
			*p++ = *s++;
		else
			while (!isspace(*s) && !isari(*s) && (*s != '\0'))
				*p++ = *s++;
		*p = '\0';
		switch (get_type(word)) {
		case OPESYM:			/* symbol */
			if (strcmp(word, "$") == 0) {
				val = pc;
				break;
			}
			if (strlen(word) > SYMSIZE)
				word[SYMSIZE] = '\0';
			if ((sp = get_sym(word)) != NULL)
				val = sp->sym_val;
			else
				asmerr(E_UNDSYM);
			break;
		case OPEDEC:			/* decimal number */
			val = atoi(word);
			break;
		case OPEHEX:			/* hexadecimal number */
			val = axtoi(word);
			break;
		case OPEBIN:			/* binary number */
			val = abtoi(word);
			break;
		case OPEOCT:			/* octal number */
			val = aotoi(word);
			break;
		case OPESUB:			/* arithmetical - */
			val -= eval(s);
			goto eval_break;
		case OPEADD:			/* arithmetical + */
			val += eval(s);
			goto eval_break;
		case OPEMUL:			/* arithmetical * */
			val *= eval(s);
			goto eval_break;
		case OPEDIV:			/* arithmetical / */
			val /= eval(s);
			goto eval_break;
		case OPEMOD:			/* arithmetical modulo */
			val %= eval(s);
			goto eval_break;
		case OPESHL:			/* logical shift left */
			val <<= eval(s);
			goto eval_break;
		case OPESHR:			/* logical shift right */
			val >>= eval(s);
			goto eval_break;
		case OPELOR:			/* logical OR */
			val |= eval(s);
			goto eval_break;
		case OPELAN:			/* logical AND */
			val &= eval(s);
			goto eval_break;
		case OPEXOR:			/* logical XOR */
			val ^= eval(s);
			goto eval_break;
		case OPECOM:			/* logical complement */
			val = ~(eval(s));
			goto eval_break;
		}
	}
	eval_break:
	return(val);
}
Example #17
0
ERROR recursive_interpret(FUNCTION_PTR function, STACK_PTR stack)
{
	
// 	fprintf(stderr,"Zavolana funkce: %d\n",function);
	LIST_NODE_PTR instr_node = function->instructions.begin;
	ERROR err = E_OK;
	
	// ----- Priprava promenych ------
	INSTRUCTION_PTR instruction = NULL;
	int str_from,str_to,tmp_count;
	STRING tmp_string;
	
	STACK_PTR tmp_stack;
	tmp_stack = gcMalloc(sizeof(struct STACK));
	stackInit(tmp_stack);
	
	// NULL SYMBOL
	ITEMPTR null_item;
	null_item = gcMalloc(sizeof(struct ITEM));
	null_item->type = TYPE_NULL;
	
	SYMBOL* null_symbol;
	null_symbol = gcMalloc(sizeof(struct SYMBOL));
	null_symbol->type = TYPE_NULL;
	null_symbol->items = null_item;
	
	// TMP SYMBOL
	ITEMPTR tmp_item;
	
	SYMBOL* tmp_symbol;
	
    SYMBOL* op1 = NULL;
    SYMBOL* op2 = NULL;
    SYMBOL* op3 = NULL;
	
	FUNCTION_PTR tmp_function = NULL;
	
	while(instr_node != NULL && err == E_OK)
	{
		instruction = instr_node->value;
		
		tmp_item = gcMalloc(sizeof(struct ITEM));
		tmp_symbol = gcMalloc(sizeof(struct SYMBOL));
		tmp_symbol->items = tmp_item;
	
		op1 = (SYMBOL*) instruction->operand1;
		op2 = (SYMBOL*) instruction->operand2;
		op3 = (SYMBOL*) instruction->destionation;
		
		fprintf(stderr,"while interpret : %d (%s)\n",instruction->type, debugInstruction(instruction->type));
		
		switch(instruction->type)
		{
			case INSTRUCTION_NOP:
			break;
			
			case INSTRUCTION_MOV:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				
				data_copy(op2, op1);
				op1->filgy = true;
			break;
			
			case INSTRUCTION_LOAD_NULL:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'O')) != E_OK)
					return err;
				if ((err = op_check(op3,'O')) != E_OK)
					return err;
				
				data_copy(null_symbol,op1);
				data_copy(null_symbol,op2);
				data_copy(null_symbol,op3);
				op1->filgy = true;
				op2->filgy = true;
				op3->filgy = true;
			break;
			
			case INSTRUCTION_ADDITION:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				if ((err = op_check(op3,'I')) != E_OK)
					return err;
				
				if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->type = TYPE_DIGIT_INT;
					tmp_symbol->items->value.valInt = op2->items->value.valInt + op3->items->value.valInt;
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = op2->items->value.valInt + op3->items->value.valDouble;
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = op2->items->value.valDouble + op3->items->value.valInt;
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = op2->items->value.valDouble + op3->items->value.valDouble;
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_STRING)
				{
					tmp_symbol->items->type = TYPE_STRING;
					
					strConcatenate(&tmp_symbol->items->value.valString, &op2->items->value.valString);
					strConcatenate(&tmp_symbol->items->value.valString, &op3->items->value.valString);
					
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_DIGIT_INT)
				{
					STRING new_string;
					char* new_char_string = gcMalloc(sizeof(char*));;
					sprintf(new_char_string, "%d", op3->items->value.valInt);
					strInitRaw(&new_string, new_char_string);
					
					tmp_symbol->items->type = TYPE_STRING;
					
					strConcatenate(&tmp_symbol->items->value.valString, &op2->items->value.valString);
					strConcatenate(&tmp_symbol->items->value.valString, &op3->items->value.valString);
					
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					STRING new_string;
					char* new_char_string = gcMalloc(sizeof(char*));
					sprintf(new_char_string, "%lf", op3->items->value.valDouble);
					strInitRaw(&new_string, new_char_string);
					
					tmp_symbol->items->type = TYPE_STRING;
					
					strConcatenate(&tmp_symbol->items->value.valString, &op2->items->value.valString);
					strConcatenate(&tmp_symbol->items->value.valString, &op3->items->value.valString);
					
					data_copy(tmp_symbol,op1); // from, to
				}
				else
					return E_SEMANTIC_TYPE_MISMATCH;
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_SUBSTRACTION:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				if ((err = op_check(op3,'I')) != E_OK)
					return err;
				
				if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->type = TYPE_DIGIT_INT;
					tmp_symbol->items->value.valInt = op2->items->value.valInt - op3->items->value.valInt;
					
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = op2->items->value.valInt - op3->items->value.valDouble;
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = op2->items->value.valDouble - op3->items->value.valInt;
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = op2->items->value.valDouble - op3->items->value.valDouble;
					data_copy(tmp_symbol,op1); // from, to
				}
				else
					return E_SEMANTIC_TYPE_MISMATCH;
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_MULTIPLICATION:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				if ((err = op_check(op3,'I')) != E_OK)
					return err;
				
				if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->type = TYPE_DIGIT_INT;
					tmp_symbol->items->value.valInt = op2->items->value.valInt * op3->items->value.valInt;
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = ((double)op2->items->value.valInt) * op3->items->value.valDouble;
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = op2->items->value.valDouble * ((double)op3->items->value.valInt);
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = op2->items->value.valDouble * op3->items->value.valDouble;
					data_copy(tmp_symbol,op1); // from, to
				}
				else
					return E_SEMANTIC_TYPE_MISMATCH;
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_DIVISION:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				if ((err = op_check(op3,'I')) != E_OK)
					return err;
				
				if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_INT)
				{
					if(op3->items->value.valInt == 0)
						return E_SEMANTIC_ZERO_DIV;
					tmp_symbol->items->type = TYPE_DIGIT_INT;
					tmp_symbol->items->value.valInt = op2->items->value.valInt / op3->items->value.valInt;
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					if(op3->items->value.valDouble == 0.0)
						return E_SEMANTIC_ZERO_DIV;
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = op2->items->value.valInt / op3->items->value.valDouble;
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_INT)
				{
					if(op3->items->value.valInt == 0)
						return E_SEMANTIC_ZERO_DIV;
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = op2->items->value.valDouble / op3->items->value.valInt;
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					if(op3->items->value.valDouble == 0.0)
						return E_SEMANTIC_ZERO_DIV;
					tmp_symbol->items->type = TYPE_DIGIT_DOUBLE;
					tmp_symbol->items->value.valDouble = op2->items->value.valDouble / op3->items->value.valDouble;
					data_copy(tmp_symbol,op1); // from, to
				}
				else
					return E_SEMANTIC_TYPE_MISMATCH;
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_LESS:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				if ((err = op_check(op3,'I')) != E_OK)
					return err;
				
				tmp_symbol->items->type = TYPE_BOOL;
				if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valInt < op3->items->value.valInt;
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valDouble < op3->items->value.valDouble;
				}
				else if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_STRING)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valString.used < op3->items->value.valString.used;
				}
				else
					return E_SEMANTIC_TYPE_MISMATCH;
				data_copy(tmp_symbol,op1);
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_GREATER:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				if ((err = op_check(op3,'I')) != E_OK)
					return err;
				
				tmp_symbol->items->type = TYPE_BOOL;
				if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valInt > op3->items->value.valInt;
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valDouble > op3->items->value.valDouble;
				}
				else if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_STRING)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valString.used > op3->items->value.valString.used;
				}
				else
					return E_SEMANTIC_TYPE_MISMATCH;
				data_copy(tmp_symbol,op1);
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_LESS_EQUAL:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				if ((err = op_check(op3,'I')) != E_OK)
					return err;
				
				tmp_symbol->items->type = TYPE_BOOL;
				if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valInt <= op3->items->value.valInt;
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valDouble <= op3->items->value.valDouble;
				}
				else if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_STRING)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valString.used <= op3->items->value.valString.used;
				}
				else
					return E_SEMANTIC_TYPE_MISMATCH;
				data_copy(tmp_symbol,op1);
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_GREATER_EQUAL:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				if ((err = op_check(op3,'I')) != E_OK)
					return err;
				
				tmp_symbol->items->type = TYPE_BOOL;
				if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valInt >= op3->items->value.valInt;
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valDouble >= op3->items->value.valDouble;
				}
				else if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_STRING)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valString.used >= op3->items->value.valString.used;
				}
				else
					return E_SEMANTIC_TYPE_MISMATCH;
				data_copy(tmp_symbol,op1);
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_EQUAL:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				if ((err = op_check(op3,'I')) != E_OK)
					return err;
				
				tmp_symbol->items->type = TYPE_BOOL;
				if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valInt == op3->items->value.valInt;
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valDouble == op3->items->value.valDouble;
				}
				else if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_STRING)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valString.used == op3->items->value.valString.used;
				}
				else
					tmp_symbol->items->value.valBool = false;
				data_copy(tmp_symbol,op1);
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_NOT_EQUAL:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				if ((err = op_check(op3,'I')) != E_OK)
					return err;
				
				tmp_symbol->items->type = TYPE_BOOL;				
				if(op2->items->type == TYPE_DIGIT_INT && op3->items->type == TYPE_DIGIT_INT)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valInt != op3->items->value.valInt;
				}
				else if(op2->items->type == TYPE_DIGIT_DOUBLE && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valDouble != op3->items->value.valDouble;
				}
				else if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_STRING)
				{
					tmp_symbol->items->value.valBool = op2->items->value.valString.used != op3->items->value.valString.used;
					if(!tmp_symbol->items->value.valBool)
					{
						tmp_symbol->items->value.valBool = !strCompare(op2->items->value.valString, op3->items->value.valString.data);
					}
				}
				else
					tmp_symbol->items->value.valBool = false;
				data_copy(tmp_symbol,op1);
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_PUSH:
// 				printf("ahoj %d\n",op1->filgy);
				if ((err = op_check(op1,'I')) != E_OK)
					return err;
				
				stackPush(stack,op1);
			break;
			
			case INSTRUCTION_POP:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				
				tmp_symbol = stackPop(stack);
				
				if(tmp_symbol == NULL){
					tmp_symbol = gcMalloc(sizeof(SYMBOL));
					tmp_symbol->items = gcMalloc(sizeof(ITEM));
					
					tmp_symbol->items->type = TYPE_NULL;
					tmp_symbol->filgy = true;
					
				}
				
				data_copy(tmp_symbol,op1);
				op1->filgy = true;
			break;
			
			case INSTRUCTION_CALL:
				if(op1 == NULL)
					return E_COMPILATOR;
				
				tmp_function = function;
				function = (FUNCTION_PTR)op1;
				
				err=recursive_interpret(function,stack);
				if (err != E_OK)
					return err;
				
				function = tmp_function;
			break;
			
			case INSTRUCTION_RETURN:
				return err;
			break;
			
			case INSTRUCTION_JUMP:
				if(op3 == NULL)
					return E_COMPILATOR;
				
				instr_node = function->instructions.begin;
				while(instr_node != (LIST_NODE_PTR)op3)
				{
					if(instr_node->next == NULL)
					{
						fprintf(stderr,"NENALEZENO!!!\n");
						fprintf(stderr,"NENALEZENO!!!\n");
						fprintf(stderr,"NENALEZENO!!!\n");
						break;
					}
					instr_node = instr_node->next;
				}
				continue;
			break;
			
			case INSTRUCTION_IF_JUMP:	
// 				if ((err = op_check(op2,'I')) != E_OK)
// 					return err;
				if(op3 == NULL)
					return E_COMPILATOR;
				
				if(op2->items->type == TYPE_BOOL)
				{
					if(!op2->items->value.valBool)
					{
						instr_node = function->instructions.begin;
						while(instr_node != (LIST_NODE_PTR)op3)
						{
							if(instr_node->next == NULL)
								break;
							instr_node = instr_node->next;
						}
						continue;						
					}
				}
// 				if(op2->items->type == TYPE_DIGIT_INT)
// 				{
// 					printf("AHOJ\n");
// 					if(!op2->items->value.valInt)
// 					{
// 						instr_node = function->instructions.begin;
// 						while(instr_node != (LIST_NODE_PTR)op3)
// 						{
// 							if(instr_node->next == NULL)
// 								break;
// 							instr_node = instr_node->next;
// 						}
// 						continue;						
// 					}
// 				}
			break;
			
			case INSTRUCTION_LABEL:
				
			break;
			
			case INSTRUCTION_BOOLVAL:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				
				tmp_symbol = stackPop(stack);
				tmp_symbol->items = boolval(*tmp_symbol->items);
				
				data_copy(tmp_symbol,op1);
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_DOUBLEVAL:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				
				tmp_symbol = stackPop(stack);
				tmp_symbol->items = doubleval(*tmp_symbol->items);
				if(tmp_symbol->items == NULL)
					return E_SEMANTIC_DOUBLEVAL;
				
				data_copy(tmp_symbol,op1);
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_INTVAL:	
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				
				op1 = stackPop(stack);
				if ((err = op_check(op1,'I')) != E_OK)
					return err;
				
				tmp_symbol->items = intval(*op1->items);
				data_copy(tmp_symbol,op1);
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_STRVAL:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				
				tmp_symbol = stackPop(stack);
				tmp_symbol->items = strval(*tmp_symbol->items);
				
				data_copy(tmp_symbol,op1);
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_GET_STRING:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				
				tmp_string = get_string();
				
				tmp_symbol->items->type = TYPE_STRING;
				strInit(&tmp_symbol->items->value.valString);
				strCopy(&tmp_string, &tmp_symbol->items->value.valString);
				
				data_copy(tmp_symbol, op1);
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_CONCATE:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				if ((err = op_check(op3,'I')) != E_OK)
					return err;
				
				if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_STRING)
				{
					tmp_symbol->items->type = TYPE_STRING;
					strInit(&tmp_symbol->items->value.valString);
					strConcatenate(&tmp_symbol->items->value.valString, &op2->items->value.valString);
					strConcatenate(&tmp_symbol->items->value.valString, &op3->items->value.valString);
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_DIGIT_INT)
				{
					STRING new_string;
					char* new_char_string = gcMalloc(sizeof(char*));
					sprintf(new_char_string, "%d", op3->items->value.valInt);
					strInitRaw(&new_string, new_char_string);
					
					tmp_symbol->items->type = TYPE_STRING;
					strConcatenate(&tmp_symbol->items->value.valString, &op2->items->value.valString);
					strConcatenate(&tmp_symbol->items->value.valString, &op3->items->value.valString);
					
					data_copy(tmp_symbol,op1); // from, to
				}
				else if(op2->items->type == TYPE_STRING && op3->items->type == TYPE_DIGIT_DOUBLE)
				{
					STRING new_string;
					char* new_char_string = gcMalloc(sizeof(char*));
					sprintf(new_char_string, "%lf", op3->items->value.valDouble);
					strInitRaw(&new_string, new_char_string);
					
					tmp_symbol->items->type = TYPE_STRING;
					
					strConcatenate(&tmp_symbol->items->value.valString, &op2->items->value.valString);
					strConcatenate(&tmp_symbol->items->value.valString, &op3->items->value.valString);
					
					data_copy(tmp_symbol,op1); // from, to
				}
				else
					return E_SEMANTIC_TYPE_MISMATCH;
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_PUT_STRING:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if (op2 == NULL)
					return E_COMPILATOR;
				
				// DODELAT VRACENI POCTU VYPSANYCH! DO OP1
				
				tmp_count = *((int*)op2);
				for(int i=0;i<tmp_count;i++)
				{
					tmp_symbol = stackPop(stack);
					if ((err = op_check(tmp_symbol,'I')) != E_OK)
						return err;
					stackPush(tmp_stack,tmp_symbol);
				}
				for(int i=0;i<tmp_count;i++)
				{
					tmp_symbol = stackPop(tmp_stack);
					if(tmp_symbol->items->type == 2)
						printf("%d",tmp_symbol->items->value.valInt);
					else if(tmp_symbol->items->type == 3)
						printf("%lf",tmp_symbol->items->value.valDouble);
					else if(tmp_symbol->items->type == 4)
						printf("%s",tmp_symbol->items->value.valString.data);
				}
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_STRLEN:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if (op2 == NULL)
					return E_COMPILATOR;
				
				tmp_count = *((int*)op2);
				if (tmp_count != 1)
					return E_COMPILATOR;
				
				op1 = stackPop(stack);
				if ((err = op_check(op1,'I')) != E_OK)
					return err;
				tmp_symbol->items->type = TYPE_DIGIT_INT;
				tmp_symbol->items->value.valInt = my_strlen(op1->items->value.valString);
				
				data_copy(tmp_symbol, op1);
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_GET_SUBSTRING:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				if (op2 == NULL)
					return E_COMPILATOR;
				tmp_count = *((int*)op2);
				if (tmp_count != 3)
					return E_COMPILATOR;
				
				// TO
				tmp_symbol = stackPop(stack);
				if ((err = op_check(tmp_symbol,'I')) != E_OK)
					return err;
				str_to = tmp_symbol->items->value.valInt;
				
				// FROM
				tmp_symbol = stackPop(stack);
				if ((err = op_check(tmp_symbol,'I')) != E_OK)
					return err;
				str_from = tmp_symbol->items->value.valInt;
				
				// STRING
				tmp_symbol = stackPop(stack);
				if ((err = op_check(tmp_symbol,'I')) != E_OK)
					return err;
				
				tmp_string = get_substring(tmp_symbol->items->value.valString,str_from,str_to,&err);
				if(err != E_OK)
					return err;
				
				tmp_symbol->items->type = TYPE_STRING;
				strCopy(&tmp_symbol->items->value.valString,&tmp_string);
				data_copy(tmp_symbol,op1);
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_FIND_STRING:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				
				op2 = stackPop(stack);
				if ((err = op_check(op2,'I')) != E_OK)
					return err;
				
				op3 = stackPop(stack);
				if ((err = op_check(op1,'I')) != E_OK)
					return err;
				
				tmp_symbol->items->type = TYPE_DIGIT_INT;
				tmp_symbol->items->value.valInt = find_string(op3->items->value.valString, op2->items->value.valString);
				data_copy(tmp_symbol,op1);
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			case INSTRUCTION_SORT_STRING:
				if ((err = op_check(op1,'O')) != E_OK)
					return err;
				
				tmp_symbol = stackPop(stack);
				if ((err = op_check(tmp_symbol,'I')) != E_OK)
					return err;
				
				sort_string(tmp_symbol->items->value.valString);
				data_copy(tmp_symbol,op1);
				op1->filgy = true;
				tmp_symbol = NULL;
			break;
			
			default:
				return E_OK;
			break;
			
		}
		instr_node = instr_node->next;
	}
	return err;
}
Example #18
0
char *
captoinfo(char *cap)
{
	char *info, *ip, *token, *val, *p, tok[3];
	const char *name;
	size_t len, lp, nl, vl, rl;
	int defs[__arraycount(def_infos)], fv;

	_DIAGASSERT(cap != NULL);

	len = strlen(cap) * 2;
	len += __arraycount(def_infos) * (5 + 4 + 3); /* reserve for defs */
	info = ip = malloc(len);
	if (info == NULL)
		return NULL;

	memset(defs, 0, sizeof(defs));
	lp = 0;
	tok[2] = '\0';
	for (token = _ti_get_token(&cap, ':');
	     token != NULL;
	     token = _ti_get_token(&cap, ':'))
	{
		if (token[0] == '\0')
			continue;
		name = token;
		val = p = NULL;
		fv = nl = 0;
		if (token[1] != '\0') {
			tok[0] = token[0];
			tok[1] = token[1];
			nl = 1;
			if (token[2] == '\0') {
				name = flagname(tok);
				val = NULL;
			} else if (token[2] == '#') {
				name = numname(tok);
				val = token + 2;
			} else if (token[2] == '=') {
				name = strname(tok);
				val = strval(token + 2);
				fv = 1;
			} else
				nl = 0;
		}
		/* If not matched we may need to convert padding still. */
		if (nl == 0) {
			p = strchr(name, '=');
			if (p != NULL) {
				val = strval(p);
				*p = '\0';
				fv = 1;
			}
		}

		/* See if this sets a default. */
		for (nl = 0; nl < __arraycount(def_infos); nl++) {
			if (strcmp(name, def_infos[nl].name) == 0) {
				defs[nl] = 1;
				break;
			}
		}

		nl = strlen(name);
		if (val == NULL)
			vl = 0;
		else
			vl = strlen(val);
		rl = nl + vl + 3; /* , \0 */

		if (lp + rl > len) {
			if (rl < 256)
				len += 256;
			else
				len += rl;
			p = realloc(info, len);
			if (p == NULL) {
				if (fv == 1)
					free(val);
				return NULL;
			}
			info = p;
		}

		if (ip != info) {
			*ip++ = ',';
			*ip++ = ' ';
		}

		strcpy(ip, name);
		ip += nl;
		if (val != NULL) {
			strcpy(ip, val);
			ip += vl;
			if (fv == 1)
				free(val);
		}
	}

	/* Add any defaults not set above. */
	for (nl = 0; nl < __arraycount(def_infos); nl++) {
		if (defs[nl] == 0) {
			*ip++ = ',';
			*ip++ = ' ';
			strcpy(ip, def_infos[nl].name);
			ip += strlen(def_infos[nl].name);
			*ip++ = '=';
			strcpy(ip, def_infos[nl].cap);
			ip += strlen(def_infos[nl].cap);
		}
	}

	*ip = '\0';
	return info;
}
Example #19
0
int
main (int argc, char **argv)
{
  int i = 1, rc;
  mu_url_t url = NULL;
  const char *arg;
  
  mu_set_program_name (argv[0]);
  
  if (argc > 1)
    {
      if (strcmp (argv[1], "help") == 0 ||
	  strcmp (argv[1], "--help") == 0 ||
	  strcmp (argv[1], "-h") == 0)
	usage (stdout, 0);

      if (strncmp (argv[1], "url=", 4) == 0)
	{
	  MU_ASSERT (mu_url_create (&url, argv[1] + 4));
	  i = 2;
	}
    }

  if (!url)
    {
      MU_ASSERT (mu_url_create_null (&url));
      i = 1;
    }
  
  for (; i < argc; i++)
    {
      if (strncmp (argv[i], "scheme=", 7) == 0)
	{
	  MU_ASSERT (mu_url_set_scheme (url, strval (argv[i] + 7)));
	}
      else if (strncmp (argv[i], "user="******"path=", 5) == 0)
	{
	  MU_ASSERT (mu_url_set_path (url, strval (argv[i] + 5)));
	}
      else if (strncmp (argv[i], "host=", 5) == 0)
	{
	  MU_ASSERT (mu_url_set_host (url, strval (argv[i] + 5)));
	}
      else if (strncmp (argv[i], "port=", 5) == 0)
	{
	  MU_ASSERT (mu_url_set_port (url, atoi (argv[i] + 5)));
	}
      else if (strncmp (argv[i], "service=", 8) == 0)
	{
	  MU_ASSERT (mu_url_set_service (url, strval (argv[i] + 8)));
	}
      else if (strncmp (argv[i], "auth=", 5) == 0)
	{
	  MU_ASSERT (mu_url_set_auth (url, strval (argv[i] + 5)));
	}
      else if (strncmp (argv[i], "pass="******"param=", 6) == 0)
	{
	  arg = strval (argv[i] + 6);
	  if (arg)
	    MU_ASSERT (mu_url_add_param (url, 1, (const char **)&arg));
	  else
	    MU_ASSERT (mu_url_clear_param (url));
	}
      else if (strncmp (argv[i], "query=", 6) == 0)
	{
	  arg = strval (argv[i] + 6);
	  if (arg)
	    MU_ASSERT (mu_url_add_query (url, 1, (const char **)&arg));
	  else
	    MU_ASSERT (mu_url_clear_query (url));
	}
      else
	{
	  mu_error ("unrecognized argument: %s", argv[i]);
	  return 1;
	}
    }
		       
  rc = mu_url_sget_name (url, &arg);
  if (rc)
    {
      mu_error ("%s", mu_strerror (rc));
      return 1;
    }
  

  printf ("%s\n", arg);
  return 0;
}