Exemplo n.º 1
0
bool debug_ask(Machine *pmach){
	char input[NMAX];
	while(1){
		printf("DEBUG? ");
		gets(input);
		if(strcmp(input,"h")==0){
			printf("h\thelp\n");
			printf("c\tcontinue(exit interactive debug mode)\n");
			printf("s\tstep by step(next instruction)\n");
			printf("RET\tstep by step(next instruction)\n");
			printf("r\tprint registers\n");
			printf("d\tprint data memory\n");
			printf("t\tprint text(program) memory\n");
			printf("p\tprint text(program) memory\n");
			printf("m\tprint registers and data memory\n");	
		}
		if(strcmp(input,"c")==0)
			return false;
		if(strcmp(input,"r")==0)
			print_cpu(pmach);
		if(strcmp(input,"RET")==0 || strcmp(input,"s")==0 || strcmp(input,"\0")==0)
			return true;
		if(strcmp(input,"d")==0)
			print_data(pmach);
		if(strcmp(input,"t")==0)
			print_program(pmach);
		if(strcmp(input,"p")==0)
			print_program(pmach);
		if(strcmp(input,"m")==0){
			print_cpu(pmach);
			print_data(pmach);
		}
	}
		return true;
}
Exemplo n.º 2
0
void ExecutionManager::start_execution()
{
	ROS_INFO("executing the program %s", print_program().c_str());
	
	bool full_program_iteration = false;

	// TO-DO can be more efficient if current_operation_index is only reset upon clearing programs (currently
	// at stopping programs) so we can start this loop from current_operation_index instead of the beginning of
	// of the program and skipping over the programs that are done.
	for(int i = 0; i < program.size(); i++)
	{
		if(program[i] -> is_done())
			continue;
		// TO-DO remove if the loop starts at current_operation_index

		current_operation_index = i;
		
		program[i] -> start();
		full_program_iteration = program[i] -> execute();

		// NOTE: should not move to the next operation if the most recent operation failed
		if(!full_program_iteration)
			break;
	}


	if(full_program_iteration)
		for(int i = 0; i < program.size(); i++)
			program[i] -> reset();
	
}
Exemplo n.º 3
0
int main(int argc, char* argv[]) {
  FILE *fp;
  if (argc > 1) {
    // Open program
    if (! (fp = fopen(argv[1], "r"))) {
      fprintf(stderr, "Error, can't open %s\n", argv[1]);
      exit(1);
    }
    // AST!
    Program* prog = ast_from_program(fp);
    // Typing!
    FILE* typefp;
    if (! (typefp = fopen(PL_PATH, "w"))) {
        fprintf(stderr, "Error, can't write prolog typing file %s\n", PL_PATH);
	exit(EXIT_FAILURE);
    }
    fprintf(stdout, "Printing to type file..\n");
    print_program(typefp, prog);
    fprintf(stdout, "Done\n");
    fclose(typefp);
    fprintf(stdout, "Evaluating..\n");
    eval_program(prog);
  }
  else {
    fprintf(stderr, "Error, no file specified\n");
    exit(1);
  }
  return 0;
}
Exemplo n.º 4
0
int print_program(tree p){
	if (p->tp.op != CONS){
		print_external_decralation(p);
	} else {
		print_program(p->tp.a[0]);
		print_external_decralation(p->tp.a[1]);
	}
	return 0;
}
Exemplo n.º 5
0
/*!
 * Cette fonction gère le dialogue pour l'option \c -d (debug). Dans ce mode,
 * elle est invoquée après l'exécution de chaque instruction.  Elle affiche le
 * menu de mise au point et on exécute le choix de l'utilisateur. Si cette
 * fonction retourne faux, on abandonne le mode de mise au point interactive
 * pour les instructions suivantes et jusqu'à la fin du programme.
 * 
 * \param mach la machine/programme en cours de simulation
 * \return vrai si l'on doit continuer en mode debug, faux sinon
 */
bool debug_ask(Machine *pmach){
	printf("DEBUG? ");
	char commande = '\n';
	bool premier = true;
	char rep;
	while( (rep=getchar()) != '\n' && rep != EOF){
		if(premier) commande = rep;
		premier = false;
	};
	switch(commande){
	  case 'h':
		printf("Available commands :\n\th\thelp\n\tc\tcontinue (exit interactive debug mode)\n\ts\tstep by step (next instruction)\n\tRET\tstep by step (next instruction)\n\tr\tprint registers\n\td\tprint data memory\n\tt\tprint text (program) memory\n\tp\tprint text (program) memory\n\tm\tprint registers and data memory\n");
          break;
	  case 'c':
		return false;
	  break;
          case 's':
		return true;
          break;
	  case 'r':
		print_cpu(pmach);
          break;
          case 'd':
		print_data(pmach);
          break;
          case 't':
		print_program(pmach);
          break;
	  case 'p':
		print_program(pmach);
          break;
	  case 'm':
		print_cpu(pmach);
		print_data(pmach);
          break;
	  case '\n':
		return true;
	  break;
	  default:
          break;
	}
	return debug_ask(pmach);
}
Exemplo n.º 6
0
int aspl_programme (struct get_chr *get, struct pred_list **programme)
{
struct clause_list *clause;
char name[IDENT_SIZE];
int n_args;
/*	while (aspl_clause (get)); */
	*programme = NULL;
	for (;;)
	{
		if (get->c == EOF)
			return 1;
		if (!aspl_clause (get, name, &n_args, &clause))
			return 0;
#ifdef TRACE
		print_program (*programme);
#endif
		*programme = aj_clause (*programme, name, n_args, clause);
#ifdef TRACE
		print_program (*programme);
#endif
	}
}
Exemplo n.º 7
0
int main() {
  Word mem[128];
  int ret;

  /* not bothering to check the return value of these calls */
  init_word(&mem[0], INSTR, ADD,  R1,  R2,  R3,  EMP, EMP, EMP);
  init_word(&mem[1], INSTR, SUB,  R2,  R3,  R4,  EMP, EMP, EMP);
  init_word(&mem[2], INSTR, MUL,  R3,  R4,  R5,  EMP, EMP, EMP);
  init_word(&mem[3], INSTR, DIV,  R5,  R4,  R3,  EMP, EMP, EMP);
  init_word(&mem[4], INSTR, MOD,  R4,  R3,  R2,  EMP, EMP, EMP);
  init_word(&mem[5], INSTR, NOT,  R3,  R2,  EMP, EMP, EMP, EMP);
  init_word(&mem[6], INSTR, NEG,  R2,  R1,  EMP, EMP, EMP, EMP);
  init_word(&mem[7], INSTR, INC,  R1,  R0,  EMP, EMP, EMP, EMP);

  /* put specific values in the locations that the program will be
     shifted to, so we can tell later whether they've been changed */
  init_word(&mem[100], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 111);
  init_word(&mem[101], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 222);
  init_word(&mem[102], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 333);
  init_word(&mem[103], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 444);
  init_word(&mem[104], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 555);
  init_word(&mem[105], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 666);
  init_word(&mem[106], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 777);
  init_word(&mem[107], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 888);

  ret= shift_program(mem, 0, 8, 100);

  printf("Contents of original memory region are:\n");
  ret= ret && print_program(mem, 0, 8);
  printf("Original program should have been printed.\n\n");
  printf("Contents of new memory region are:\n");
  ret= ret && print_program(mem, 100, 8);
  if (ret != 1)
    printf("At least one function call didn't return 1 as expected.\n");
  else printf("Shifted program should have been printed.\n");

  return 0;
}
Exemplo n.º 8
0
main (int argc, char *argv[])
{
struct get_fnct get;
struct param_get_file p_get_file;
int status;
struct pred_list *programme;
struct put_fnct put;
struct param_put_file p_put_file;

	if (argc != 3)
	{
		printf ("usage: %s source.pro output.c\n", argv[0]);
		exit (-1);
	}
	get.f = f_get_file;
	get.p = &p_get_file;

	p_get_file.fd = fopen (argv[1], "r");
	if (p_get_file.fd == NULL)
	{
		perror (argv[1]);
		return errno;
	}

	status = aspl (&get, &programme);

	if (status)
	{
		printf ("\nProgramme correct\n");
		print_program (programme);

		put.f = f_put_file;
		put.p = &p_put_file;
		p_put_file.fd = fopen (argv[2], "w");
		if (p_put_file.fd == NULL)
		{
			perror (argv[2]);
			return errno;
		}
		gen_code (programme, &put);
	}
	else
		printf ("<- Erreur syntaxique\n");
}
Exemplo n.º 9
0
int main() {
  Word mem[128];
  int ret;

  /* not bothering to check the return value of these calls */
  init_word(&mem[100], INSTR, ADD,  R1,  R2,  R3,  EMP, EMP, EMP);
  init_word(&mem[101], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 175);
  init_word(&mem[102], INSTR, MUL,  R3,  R4,  R5,  EMP, EMP, EMP);
  init_word(&mem[103], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 751);
  init_word(&mem[104], INSTR, MOD,  R4,  R3,  R2,  EMP, EMP, EMP);
  init_word(&mem[105], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 571);
  init_word(&mem[106], INSTR, NEG,  R2,  R1,  EMP, EMP, EMP, EMP);
  init_word(&mem[107], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 715);
  init_word(&mem[108], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 517);
  init_word(&mem[109], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 157);

  /* put specific values in the locations that the program will be
     shifted to, so we can tell later whether they've been changed */
  init_word(&mem[0], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 111);
  init_word(&mem[1], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 222);
  init_word(&mem[2], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 333);
  init_word(&mem[3], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 444);
  init_word(&mem[4], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 555);
  init_word(&mem[5], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 666);
  init_word(&mem[6], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 777);
  init_word(&mem[7], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 888);
  init_word(&mem[8], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, 999);
  init_word(&mem[9], DATA,  EMP,  EMP, EMP, EMP, EMP, EMP, -11);

  ret= shift_program(mem, 100, 10, 0);

  printf("Program is:\n");
  ret= ret && print_program(mem, 0, 10);
  if (ret != 1)
    printf("Either shift_program() or print_program() didn't return 1.\n");
  else printf("Shifted program should have been printed.\n");

  return 0;
}
Exemplo n.º 10
0
int print_external_decralation(tree p){
	if (p->tk.op == IDENTIFIER){
		printf(" %s:%d", p->tk.name, p->tk.lev);
	}else if(p->c.op == CONSTANT){
		printf(" %d", p->c.v);
	}else{
		printf(" (");
		switch(p->tp.op){
			case PSTFIX:
				printf("FCALL");
				print_program(p->tp.a[0]);
				if (p->tp.a[1] != NULL) print_program(p->tp.a[1]);
				break;
			case FUNDEF:
				printf("\nint");
				print_program(p->tp.a[0]);
				printf(" (");
				if (p->tp.a[1] != NULL) print_program(p->tp.a[1]);
				printf(") \n");
				print_program(p->tp.a[2]);
				break;
			case INT:
				print_parm(p->tp.a[0]);
				break;
			case RETURN:
				printf("RETURN");
				if (p->tp.a[0] != NULL) print_program(p->tp.a[0]);
				break;
			case IF:
				printf("IF");
				print_program(p->tp.a[0]);
				printf(" ");
				print_program(p->tp.a[1]);
				printf("\n");
				if (p->tp.a[2] != NULL){
					printf("(ELSE");
					print_program(p->tp.a[2]);
					printf(")");
				}
				break;
			case WHILE:
				printf("WHILE");
				print_program(p->tp.a[0]);
				printf("\n");
				print_program(p->tp.a[1]);
				break;
			case ASS:
				printf("=");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case OR:
				printf("||");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case AND:
				printf("&&");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case EQU:
				printf("==");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case NEQU:
				printf("!=");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case LT:
				printf("<");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case MT:
				printf(">");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case ELT:
				printf("=<");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case EMT:
				printf("=>");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case PLUS:
				printf("+");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case MINUS:
				printf("-");
				if(p->tp.a[0] != NULL ) print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case MUL:
				printf("*");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case DIV:
				printf("/");
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			case CMPD:
				if(p->tp.a[0] != NULL) 
					print_program(p->tp.a[0]);
				printf("\n");
				if(p->tp.a[1] != NULL) 
					print_program(p->tp.a[1]);
				break;
			case CONS:
				print_program(p->tp.a[0]);
				print_program(p->tp.a[1]);
				break;
			}		
		printf(")");
	}
	return 0;
}
Exemplo n.º 11
0
int main( int argc, char** argv )
{
  GetOptContext ctx;
  init_getopt_context( &ctx );
  char c;

  while( (c = getopt( argc, argv, "?i:o:a:de:x:lrh:v", &ctx )) != -1 )
  {
    switch( c )
    {
    case 'i':
      g_inputFileName = ctx.optarg;
      break;
    case 'o':
      g_outputFileName = ctx.optarg;
      break;
    case 'e':
      if( strcmp( ctx.optarg, "big" ) == 0 )
        g_swapEndian = true;
      else if( strcmp( ctx.optarg, "little" ) == 0 )
        g_swapEndian = false;
      else
      {
        fprintf( stderr, "error: unknown argument for option -e: %s\n",
          ctx.optarg );
        return -1;
      }
      break;
    case 'a':
      g_asmFileName = ctx.optarg;
      break;
    case 'l':
      g_printIncludes = true;
      break;
    case 'v':
      /* verbose, print extra funny stuff! */
      break;
    case 'h':
      g_outputHeaderName = ctx.optarg;
      break;
    case ':':
      print_usage();
      return -1;
      break;
    case '?':
      print_usage();
      return 0;
      break;
    }
  }

  if(ctx.optind != argc)
  {
    fprintf( stdout, "%s: unexpected argument '%s'\n", argv[0], argv[ctx.optind] );
    print_usage();
    return -1;
  }

  int returnCode = 0;

  if( !g_inputFileName )
  {
    returnCode = -1;
    fprintf( stderr, "error: No input file given.\n" );
  }

  // Convert all \ to / in the input file name, in order to
  // aid the include path translation
  for( char* p = g_inputFileName; p && *p; ++p )
  {
    if( *p == '\\' )
      *p = '/';
  }

  if( returnCode == 0 )
  {
    Allocator a;
    a.m_Alloc = &allocate_memory;
    a.m_Free = &free_memory;
    BehaviorTreeContext btc = create_bt_context( a );

    ParserContextFunctions pcf;
    pcf.m_Read = &read_file;
    pcf.m_Error = &parser_error;
    pcf.m_Warning = &parser_warning;
    pcf.m_Translate = &parser_translate_include;

    ParsingInfo pi;
    pi.m_Name = g_inputFileName;
    pi.m_File = fopen( pi.m_Name, "r" );
    if( !pi.m_File )
    {
      fprintf( stderr, "%s(0): error: unable to open input file \"%s\" for reading.\n",
        g_inputFileName, pi.m_Name );
      returnCode = -1;
    }

    if( returnCode == 0 )
    {
      ParserContext pc = create_parser_context( btc );
      set_extra( pc, &pi );
      set_current( pc, pi.m_Name );
      returnCode = parse( pc, &pcf );
      destroy( pc );
    }

    if( pi.m_File )
      fclose( pi.m_File );

    Include* include = get_first_include( btc );
    while( returnCode == 0 && include )
    {
      pi.m_Name = include->m_Name;
      pi.m_File = fopen( pi.m_Name, "r" );
      if( !pi.m_File )
      {
        fprintf( stderr, "%s(%d): error: unable to open include file \"%s\" for reading.\n",
          include->m_Parent, include->m_LineNo, pi.m_Name );
        returnCode = -1;
        break;
      }

      ParserContext pc = create_parser_context( btc );
      set_extra( pc, &pi );
      set_current( pc, pi.m_Name );
      returnCode = parse( pc, &pcf );
      destroy( pc );

      if( pi.m_File )
        fclose( pi.m_File );

      if( returnCode != 0 )
        break;

      include = include->m_Next;
    }

    include = get_first_include( btc );
    while( returnCode == 0 && include && g_printIncludes )
    {
      printf( "%s\n", include->m_Name );
      include = include->m_Next;
    }

    if( g_outputHeaderName && returnCode == 0 )
    {
      FILE* header = fopen( g_outputHeaderName, "w" );
      if( !header )
      {
        fprintf( stderr, "%s(0): error: Unable to open output file %s for writing.\n",
          g_inputFileName, g_outputHeaderName );
        returnCode = -1;
      }
      else
      {
        returnCode = print_header( header, g_inputFileName, btc );
        if( returnCode != 0 )
          fprintf( stderr, "%s(0): error: unspecified error when writing header %s.\n",
            g_inputFileName, g_outputHeaderName );
        fclose( header );
      }
    }

    if( g_outputFileName && returnCode == 0 )
    {
      Program p;

      unsigned int debug_hash = hashlittle( "debug_info" );
      Parameter* debug_param = find_by_hash( get_options( btc ), debug_hash );
      if( debug_param )
        p.m_I.SetGenerateDebugInfo( as_integer( *debug_param ) );

      returnCode = setup( btc, &p );
      if( returnCode == 0 )
      {
        returnCode = generate( &p );
        if( returnCode != 0 )
          fprintf( stderr, "%s(0): error: Internal compiler error in generate.\n", g_inputFileName );
      }
      else
      {
        fprintf( stderr, "%s(0): error: Internal compiler error in setup.\n", g_inputFileName );
      }

      teardown( &p );

      if( returnCode == 0 )
      {
        g_outputFile = fopen( g_outputFileName, "wb" );
        if( !g_outputFile )
        {
          fprintf( stderr, "%s(0): error: Unable to open output file %s for writing.\n",
            g_inputFileName, g_outputFileName );
          returnCode = -2;
        }

        if( returnCode == 0 )
          returnCode = save_program( g_outputFile, g_swapEndian, &p );
        if( returnCode != 0 )
        {
          fprintf( stderr, "%s(0): error: Failed to write output file %s.\n",
            g_inputFileName, g_outputFileName );
          returnCode = -5;
        }
      }

      if( !g_asmFileName )
      {
        unsigned int hash = hashlittle( "force_asm" );
        Parameter* force_asm = find_by_hash( get_options( btc ), hash );
        if( force_asm && as_bool( *force_asm ) )
        {
          unsigned int len = strlen( g_outputFileName );
          g_asmFileNameMemory = (char*)malloc( len + 5 );
          memcpy( g_asmFileNameMemory, g_outputFileName, len );
          g_asmFileNameMemory[len + 0] = '.';
          g_asmFileNameMemory[len + 1] = 'a';
          g_asmFileNameMemory[len + 2] = 's';
          g_asmFileNameMemory[len + 3] = 'm';
          g_asmFileNameMemory[len + 4] = 0;
          g_asmFileName = g_asmFileNameMemory;
        }
      }

      if( returnCode == 0 && g_asmFileName )
      {
        FILE* asmFile = fopen( g_asmFileName, "w" );
        if( !asmFile )
        {
          fprintf( stderr, "%s(0): error: Unable to open assembly file %s for writing.\n",
            g_inputFileName, g_asmFileName );
          returnCode = -1;
        }
        else
        {
          print_program( asmFile, &p );
          fclose( asmFile );
        }
      }
    }
    destroy( btc );
  }

  if( g_asmFileNameMemory )
    free( g_asmFileNameMemory );

  if( g_outputFile )
    fclose( g_outputFile );

  return returnCode;
}
Exemplo n.º 12
0
Arquivo: main.c Projeto: zayac/eq
int
main (int argc, char *argv[])
{
  int c, ret = 0;
  char *subopts;
  char *value;
  char *src_name = NULL;
  extern char *optarg;
  extern int optind;

  struct eq_lexer *lex = (struct eq_lexer *) malloc (sizeof (struct eq_lexer));
  struct eq_parser *parser = (struct eq_parser *) malloc (sizeof (struct eq_parser));

  init_global ();
  init_global_tree ();
  init_options ();

  progname = strrchr (argv[0], '/');
  if (NULL == progname)
    progname = argv[0];
  else
    progname++;

  while (-1 != (c = getopt (argc, argv, "B:P:V")))
    switch (c)
      {
      case 'P':
	subopts = optarg;
	while (*subopts != '\0')
	  switch (getsubopt (&subopts, p_opts, &value))
	    {
	    case OPT_PRINT_PROGRAM:
	      options.print_program = true;
	      break;
	    case OPT_PRINT_MATCHES:
	      options.print_matches = true;
	      break;
	    case OPT_PRINT_TYPES:
	      options.print_types = true;
	      break;
	    default:
	      fprintf (stderr, "unknown -P suboption `%s'\n", value);
	      goto cleanup;
	      break;
	    }
	break;
      case 'B':
	subopts = optarg;
	while (*subopts != '\0')
	  switch (getsubopt (&subopts, b_opts, &value))
	    {
	    case OPT_BREAK_PARSER:
	      options.break_option = break_parser;
	      break;
	    case OPT_BREAK_TYPECHECK:
	      options.break_option = break_typecheck;
	      break;
	    case OPT_BREAK_CONTROLFLOW:
	      options.break_option = break_controlflow;
	    case OPT_BREAK_DATAFLOW:
	      options.break_option = break_dataflow;
	      break;
	    default:
	      fprintf (stderr, "unknown -B suboption `%s'\n", value);
	      goto cleanup;
	      break;
	    }
	break;
      case 'V':
	version ();
	goto cleanup;
      default:
	usage ();
	goto cleanup;
      }

  if (options.print_types
      && !(options.print_program || options.print_matches))
    fprintf (stderr, "warning: 'types' flag is useless without either "
	     "'program' flag or 'matches' flag\n");

  argv += optind;

  /* FIXME: What if we have multiple files?  */
  if (NULL == *argv)
    {
      fprintf (stderr, "%s:error: filename argument required\n", progname);
      usage ();
      ret = -1;
      goto cleanup;
    }

  /* Initialize the lexer.  */
  if (!eq_lexer_init (lex, *argv))
    {
      fprintf (stderr, "%s cannot create a lexer for file `%s'\n", progname,
	       *argv);
      ret = -2;
      goto cleanup;
    }
  else
    {
      /* Discard extension from file to compile.  */
      char* start = strrchr (*argv, '/');
      char* ext = strrchr (*argv, '.');
      int size = 0;
      
      if (start == NULL)
	start = *argv;
      else
	start += 1;
      if (ext == NULL)
	size = strlen(start);
      else
	size = ext - start;
      src_name = strndup (start, size);
    }


  /* Initialize the parser.  */
  eq_parser_init (parser, lex);

  if ((ret += eq_parse (parser)) == 0 && options.break_option != break_parser)
    ret += typecheck ();

  /* printing debug routine.  */
  if (options.print_program)
    {
      xfile *  xf = xfile_init_file_stdout ();

      printf ("\n######### Output ########\n");

      print_program (xf);

      xfile_finalize (xf);
    }

  if (options.print_matches)
    {
      printf ("\n####### Transforms ########\n");
      print_matches ();
    }
 
  if (options.break_option != break_typecheck
   && options.break_option != break_parser && !ret)
    controlflow ();

  if (options.break_option != break_controlflow
   && options.break_option != break_typecheck
   && options.break_option != break_parser && !ret)
    dataflow ();

  if (options.break_option != break_dataflow
   && options.break_option != break_controlflow
   && options.break_option != break_typecheck
   && options.break_option != break_parser && !ret)
    codegen (src_name);
  printf ("note: finished compiling.\n");

  free (src_name);
cleanup:
  eq_parser_finalize (parser);
  finalize_global_tree ();
  finalize_global ();

  /* That should be called at the very end.  */
  free_atomic_trees ();

  if (parser)
    free (parser);
  if (lex)
    free (lex);

  return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Exemplo n.º 13
0
int main( int argc, char *argv[])
{
	//direct linked numbers - these change during runtime
	double minutes, seconds;
	
	int   stack_size = 0;
	char *string = NULL;
	pstack_t *ex_stack;
	
	FILE *file = fopen( READ_FILE, "r");
	
	
	if( !file ) {
		printf( "No file!\n");
		return 1;
	}
	
	// associate direct links
	runtime_vars[0].stuff.ptr = (void*)&seconds;
	runtime_vars[1].stuff.ptr = (void*)&minutes;
	
	if( read_file( file, &stack_size) == -1 )
		return 1;
	
	fclose( file);
	
	while( 1 ) {
		var_t  *var;
		time_t tim;
		struct tm *br_tim;
		
		
		ex_stack = stack_init( stack_size);
		
		printf( "Variable: ");
		string = get_string();
		if( (var = get_var( string, varlist)) != NULL ) {
			if( var->stuff.prog == NULL ) {
				printf( "'%s' is uninitialized.\n", string);
				continue;
			}
			
			tim = time( NULL);
			br_tim = gmtime( &tim);
			minutes = br_tim->tm_min;
			seconds = br_tim->tm_sec;
			
			printf( "   Address: %p\n", var->stuff.prog);
			print_program( var->stuff.prog);
			
			if( var->type == DATA_NUMBER )
				printf( "Result: %f\n\n", *execute_number( var->stuff.prog, ex_stack));
			else if( var->type == DATA_STRING )
				printf( "Result: %s\n\n", execute_string( var->stuff.prog, ex_stack));
			else if( var->type == DATA_ALIGN ) {
				int align = *execute_align( var->stuff.prog, ex_stack);
				
				if     ( align == ALIGN_LEFT   ) printf( "Result: 'left'\n\n");
				else if( align == ALIGN_RIGHT  ) printf( "Result: 'right'\n\n");
				else if( align == ALIGN_CENTER ) printf( "Result: 'center'\n\n");
			}
				
		}
		stack_free( ex_stack);
	}
	
	return 0;
};
Exemplo n.º 14
0
Arquivo: driver.c Projeto: 1m69/xapian
extern int main(int argc, char * argv[]) {

    NEW(options, o);
    if (argc == 1) print_arglist();
    read_options(o, argc, argv);
    {
        symbol * filename = add_s_to_b(0, argv[1]);
        char * file;
        symbol * u = get_input(filename, &file);
        if (u == 0) {
            fprintf(stderr, "Can't open input %s\n", argv[1]);
            exit(1);
        }
        {
            struct tokeniser * t = create_tokeniser(u, file);
            struct analyser * a = create_analyser(t);
            t->widechars = o->widechars;
            t->includes = o->includes;
            a->utf8 = t->utf8 = o->utf8;
            read_program(a);
            if (t->error_count > 0) exit(1);
            if (o->syntax_tree) print_program(a);
            close_tokeniser(t);
            if (!o->syntax_tree) {
                struct generator * g;

                const char * s = o->output_file;
                if (!s) {
                    fprintf(stderr, "Please include the -o option\n");
                    print_arglist();
                    exit(1);
                }
                g = create_generator(a, o);
                if (o->make_lang == LANG_C || o->make_lang == LANG_CPLUSPLUS) {
                    symbol * b = add_s_to_b(0, s);
                    b = add_s_to_b(b, ".h");
                    o->output_h = get_output(b);
                    b[SIZE(b) - 1] = 'c';
                    if (o->make_lang == LANG_CPLUSPLUS) {
                        b = add_s_to_b(b, "c");
                    }
                    o->output_src = get_output(b);
                    lose_b(b);

                    generate_program_c(g);
                    fclose(o->output_src);
                    fclose(o->output_h);
                }
#ifndef DISABLE_JAVA
                if (o->make_lang == LANG_JAVA) {
                    symbol * b = add_s_to_b(0, s);
                    b = add_s_to_b(b, ".java");
                    o->output_src = get_output(b);
                    lose_b(b);
                    generate_program_java(g);
                    fclose(o->output_src);
                }
#endif
#ifndef DISABLE_PYTHON
                if (o->make_lang == LANG_PYTHON) {
                    symbol * b = add_s_to_b(0, s);
                    b = add_s_to_b(b, ".py");
                    o->output_src = get_output(b);
                    lose_b(b);
                    generate_program_python(g);
                    fclose(o->output_src);
                }
#endif
#ifndef DISABLE_JSX
                if (o->make_lang == LANG_JSX) {
                    symbol * b = add_s_to_b(0, s);
                    b = add_s_to_b(b, ".jsx");
                    o->output_src = get_output(b);
                    lose_b(b);
                    generate_program_jsx(g);
                    fclose(o->output_src);
                }
#endif
                close_generator(g);
            }
            close_analyser(a);
        }
        lose_b(u);
        lose_b(filename);
    }
    {   struct include * p = o->includes;
        while (p) {
            struct include * q = p->next;
            lose_b(p->b); FREE(p); p = q;
        }
    }
    FREE(o);
    if (space_count) fprintf(stderr, "%d blocks unfreed\n", space_count);
    return 0;
}
Exemplo n.º 15
0
/**
*** Main()
**/
int main(int argc, char* argv[])
{
  int res, verbose = 0, do_read = 0, do_write=1, do_interactive = 0, vid = FAN_VID, pid = FAN_PID, report_size=FAN_SIZE;
  int serial = 0;
  char *name;
  unsigned char buf[256], program[2 * FAN_STEPS];
  #define MAX_STR 255
  wchar_t wstr[MAX_STR];
  hid_device *handle;
  int i;

  #ifdef WIN32
  UNREFERENCED_PARAMETER(argc);
  UNREFERENCED_PARAMETER(argv);
  #endif


  // parse the command-line
  for(int i=1; i<argc; i++)
  {
    if ( !strncmp(argv[i], "--vid=", 6) ) sscanf(argv[i]+6, "%X",  &vid);
    if ( !strncmp(argv[i], "--pid=", 6) ) sscanf(argv[i]+6, "%X",  &pid);
    if ( !strcmp(argv[i], "--verbose") ) verbose = 1;
    if ( !strcmp(argv[i], "--read") ) do_read = 1; 
    if ( !strcmp(argv[i], "--help") ) 
    {
      printf("fanbot: control fanbot via USB HID communication\n" 
       "kekbot.org - rev 1.0 - " __DATE__ " " __TIME__ "\n"
       "USE: \n"
       "  fanbot [options] [data]\n"
       "  --vid=0xABCD      VID to use (default=0x%4.4X)\n"
       "  --pid=0xABCD      PID to use (default=0x%4.4X)\n"
       "  --verbose         Show more information\n",
       FAN_VID, FAN_PID, FAN_SIZE
      );
      exit(1);
    }
  }
  
  // Wait for connection
  while( 1 )
  {
    if ( verbose ) printf("Wait for connection...\n");
    handle = hid_open(vid, pid, NULL);
    if ( handle ) break; // we have a connection
    Sleep(500);    
    // usleep(1000*100);
  }
  hid_flush(handle);
  

  // Read serial# and name
  buf[1] = 0;
  res = hid_read(handle, buf, sizeof(buf));
  memcpy(&serial, &buf[8], sizeof(serial) );
  name = (char*)&buf[12];
  printf("CONNECT %X %s\n", serial, name);
  
  memset(buf, 0, sizeof(buf));
  
  read_program(handle, program);
  print_program(program);
  
  if ( verbose ) 
  {
    // Read the Manufacturer String
    wstr[0] = 0x0000;
    res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
    if (res < 0)
      printf("Unable to read manufacturer string\n");
    else
      printf("Manufacturer String: %ls\n", wstr);
    // Read the Product String
    wstr[0] = 0x0000;
    res = hid_get_product_string(handle, wstr, MAX_STR);
    if (res < 0)
      printf("Unable to read product string\n");
    else
      printf("Product String: %ls\n", wstr);

    // Read the Serial Number String
    wstr[0] = 0x0000;
    res = hid_get_serial_number_string(handle, wstr, MAX_STR);
    if (res < 0)
      printf("Unable to read serial number string\n");
    else
      printf("Serial Number String: (%d) %ls", wstr[0], wstr);
    printf("\n");

    // Read Indexed String 1
    wstr[0] = 0x0000;
    res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
    if (res < 0)
      printf("Unable to read indexed string 1\n");
    else
      printf("Indexed String 1: %ls\n", wstr);
  }
  
#define BEGIN() pch = strtok (str,",. ")
#define NEXT() pch = strtok (NULL,",. ")
#define COMMAND(s) (!strncmp(pch, s, sizeof(s)-1))

  while ( 1 )
  {
    char str[MAX_STR], *pch;
    
    fgets(str , MAX_STR-1, stdin);
    if ( strlen(str) < 1 ) continue;
    BEGIN();
    
    if ( COMMAND("SET") )
    {
      int leds=0, s1=0, s2=0;
      if ( (NEXT()) != NULL ) sscanf(pch, "%d", &leds);
      if ( (NEXT()) != NULL ) sscanf(pch, "%d", &s1);
      if ( (NEXT()) != NULL ) sscanf(pch, "%d", &s2);    
      buf[1] = CMD_SET;
      buf[2] = leds;
      buf[3] = s1;
      buf[4] = s2;
      res = hid_write(handle, buf, report_size);
    }
    else if ( COMMAND("PLAY") )
    {
      int n = 1;
      buf[1] = CMD_PLAY;
      if ( (NEXT()) != NULL ) sscanf(pch, "%d", &n);
      buf[2] = n;
      res = hid_write(handle, buf, report_size);
    }
    else if ( COMMAND("PROGRAM") )
    {
      int i = 2, val = 0;
      memset(buf,0,sizeof(buf));
      buf[1] = CMD_PROGRAM;
      while ( (NEXT()) != NULL )
      {
        sscanf(pch, "%d", &val);
        buf[i++] = val;     
     }
     res = hid_write(handle, buf, report_size);
    }
    else if ( COMMAND("NAME") )
    {
      buf[1] = CMD_NAME;
      memset(&buf[2], 0, 32);
      for(i=0; i<32 && str[i+5] != 0 && str[i+5] != '\n' && str[i+5] != '\r'; i++)
        buf[i+2] = (unsigned char) str[i+5];
      res = hid_write(handle, buf, report_size);
    }
    else if ( COMMAND("STOP") )
    {
      buf[1] = CMD_STOP;
      res = hid_write(handle, buf, report_size);
    }
    else if ( COMMAND("READ") )  // read memory content
    {
      res = read_program(handle, program);
      if ( res < 0 )
        break;
      print_program(program);
    }
    else if ( COMMAND("QUIT") )
    {
      break; 
    }
    else
      buf[1] = CMD_NONE;
  
    if (res < 0) 
    {
      printf("ERROR: Unable to write(), '%ls'\n", hid_error(handle));
      exit(1);
    }
    
  }

  printf("DISCONNECT\n");  
  
  hid_close(handle); 
  hid_exit();/* Free static HIDAPI objects. */
  
  return res;
}
Exemplo n.º 16
0
/*!
 * Options de la ligne de commande :
 *
 * <dl>
 *   <dt>-d</dt><dd>mode pas à pas (mise au point)</dd>
 *
 *   <dt>-f</dt><dd>le programme est dans un fichier binaire ; le nom de ce
 *   fichier doit être fourni également en paramètre de la ligne de
 *   commande ; sans cette option, on exécute un programme de test prédéfini.</dd>
 *
 * </dl>
 */
int main(int argc, char *argv[])
{
    bool debug = false;
    bool binfile = false;
    bool no_exec = false;
    char *programfile = NULL;

    if (argc > 1) 
    {
        for (int iarg = 1; iarg < argc; ++iarg)
        {
            if (argv[iarg][0] == '-')
                switch (argv[iarg][1])
                {
                case 'd':
                    debug = true;
                    break;
                case 'b': 
                    binfile = true;
                    break;
                 case 'l': 
                    no_exec = true;
                    break;
                  case 'h':
                    usage();
                    exit(EXIT_SUCCESS);
                default:
                    fprintf(stderr, "Unknown option: %s\n", argv[iarg]);
                    usage();
                    exit(EXIT_FAILURE);
                }
            else if (binfile)
                programfile = argv[iarg];
            else 
                fprintf(stderr, "Trailing options ignored...\n");
        }
    }

    Machine mach;

    if (!binfile) 
        load_program(&mach, textsize, text, datasize, data, dataend);
    else 
        read_program(&mach, programfile);   

    printf("\n*** Sauvegarde des programmes et données initiales en format binaire ***\n\n");
    dump_memory(&mach);

    printf("\n*** Machine state before execution ***\n");
    print_program(&mach);
    print_data(&mach);
    print_cpu(&mach);

    if (no_exec) 
        return 0;

    printf("\n*** Execution trace ***\n\n");
    simul(&mach, debug);

    printf("\n*** Machine state after execution ***\n");
    print_cpu(&mach);
    print_data(&mach);
    if(allocated){
		free(mach._text);
		free(mach._data);
	}

    return 0; 
}
Exemplo n.º 17
0
int main(int argc, char *argv[])
{
  FILE *fp, *fp2;
  char buf[100000][100];	/*命令 読み込み用バッファ */
  char fpt[FPT_MAX_COUNT][20] = { 0 };
  char ofname[1000] = "result";
  int i = 0, j, k;
  //メモリ上の浮動小数点テーブルの位置
  int fpmemoffset = 0;
  int result;
  bool doprintregs = false, doprintmem = false;
  program *answer;
  program2 *answer2;
  double t2, t1;
  int ll = 0;

  while ((result = getopt(argc, argv, "rmo:")) != -1) {
    switch (result) {

      /* 値をとらないオプション */
    case 'r':
      /* getoptの返り値は見付けたオプションである. */
      doprintregs = true;
      break;
    case 'm':
      doprintmem = true;
      break;

    case 'o':
      /* 値を取る引数の場合は外部変数optargにその値を格納する. */
      strcpy(ofname, optarg);
      break;
    }
  }
  argc -= optind;
  argv += optind;


  /*assembler-file open and read */
  fp = fopen(argv[0], "r");
  if (fp == NULL) {
    printf("%sが開けませんaa\n", argv[0]);
    return 1;
  }

  if (argc > 1) {
    fp2 = fopen(argv[1], "r");
    if (fp2 == NULL) {
      printf("%sが開けません\n", argv[1]);
      return 1;
    }
    for (i = 0; i < FPT_MAX_COUNT; ++i) {
      if (fgets(fpt[i], 20, fp2) == NULL) {	/* 1行読み込み */
	break;		/* 末尾まで完了したか、エラー発生で終了 */
      }
    }

    for (j = 0; j < i; ++j) {
      //エラーが起こっても関係ない
      //printf("%s\n", fpt[j]);
      memory[j + fpmemoffset].i = (int) strtoll(fpt[j], NULL, 16);
    }
    //print_memory();
  }

  i = 0;
  while (1) {
    if (fgets(buf[i], 81, fp) == NULL) {	/* 1行読み込み */
      break;		/* 末尾まで完了したか、エラー発生で終了 */
    }
    i++;
  }

  out_fp = fopen(ofname, "w");

  answer = parse_all(buf, i);
  answer2 = parse_all2(parse_all(buf, i));
  
  print_program(answer);

  t1 = gettimeofday_sec();
  do_assemble2(answer, answer2);
  t2 = gettimeofday_sec();
  
  printf("%lf\n", t2 - t1);
  printf("%lld\n", cnt);

  if (doprintregs)
    print_register();
  if (doprintmem)
    print_memory();

  fclose(fp);
  fclose(fp2);
  fclose(out_fp);
    
  return 0;
}