⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basic.c

📁 basic.c */ /**//* Project:NeuroBasic, basic package*//**/ /* Survey:This is a simple Basic b-code
💻 C
📖 第 1 页 / 共 2 页
字号:
	break;      case C_ERR_ARRAY_INDEX:	bc_print_error(3, msg[1].i, msg[2].i, msg[3].i);	break;      case C_ERR_MUSIC_MEM:	bc_print_error(1, 0, 0, msg[3].i);	break;      case C_SYSTEM:	p = bc_str_get(msg[1].i, msg[3].i, &term_signal);	msg[0].i = term_signal ? 0 : system(p);	Wr_to_music(msg, sizeof(msg));	break;    } /* end of switch (msg[0].i) */  } while (msg[0].i != C_END);    if (!new_line) printf("\n");  signal(SIGINT, SIG_DFL);} /* end of b_server() */static void do_call_b_interpreter(MINT b_code_length, MINT entry_point)/*===================================================================*/{  MCALL(b_interpreter(b_code_length, entry_point));  longjmp(env_host, 1);} /* end of do_call_b_interpreter() */static void call_b_interpreter(MINT b_code_length, MINT entry_point)/*================================================================*/{  fill_t	stack;		/* remaining stack for lifetime.. */  				/* ..of MUSIC task                */  stack.ft_a = 1;		/* to suppress compiler warnings  */  stack.ft_b = stack.ft_a;	/*	"		"	  */    if (setjmp(env_host) == 0)    do_call_b_interpreter(b_code_length, entry_point);} /* end of call_b_interpreter() */static void get_line(int make_nr, int line, char *p)/*================================================*//* store program line */{  size_t	slen;  if (*skipws(cmdbuf) == 0) return;   /* empty line */  if (!make_nr)  {    l = strtol(cmdbuf, &p, 0);    p++;    if (l < 0 || l >= PRG_MAX)    {      printf(INDENT);      printf("ERROR: Invalid line number %ld \n", l);      printf(INDENT);      printf("       (Range: 0 <= program line < %d) \n", PRG_MAX);      return;    }  }  else  {    p = cmdbuf;    l = line;  }  if (bsrc[l] != NULL) free(bsrc[l]);  slen = strlen(p);  if (slen > 0)  {    bsrc[l] = malloc(slen + 1);    strcpy(bsrc[l], p);  }  else  {    bsrc[l] = NULL;  }} /* end of get_line() */static void run_program(pr_t cmode)/*===============================*/{  MINT		prg_length;  /*===== compile program =====*/  bc_reset();  if (cmode == PR_SINGLELINE)  {				/* compile single-line command */    if (bc_compile_line(cmdbuf)) cmode = PR_NORMAL;  }  else  {				/* clear all global variables */    bc_clrvars();    MCALL(clear_mvars());    WAIT_FCT();  }  if (cmode == PR_NORMAL) bc_compile_program(bsrc);  if (bc_get_error()) return;  /*===== execute b-code =====*/  prg_length = bc_get_codelength();  call_b_interpreter(prg_length, 0);  Wr_to_music(bc_get_codeadr(), (MINT)(prg_length * sizeof(b_code_t)));  b_server();  WAIT_FCT();} /* end of run_program() */static void clean_cmd(char *cmd)/******************************/{  char	*pch;    pch = strchr(cmd, '\n');  if (pch != NULL)  {    *pch = 0;    pch = cmd;    while (*pch != 0 && strchr(WHITE_SPACE, *pch) != NULL) pch++;    strcpy(cmd, pch);  }  else  {    printf(INDENT);    printf("ERROR: Command too long. \n");    printf(INDENT);    printf("       (Maximum: %d characters.) \n", CMD_MAX);    fgets(cmdbuf, CMD_MAX, stdin);  /* skip rest of line */    *cmd = 0;				/* empty comand */   }} /* end of clean_cmd() */static char *getarg(char *cmd, int argnum)/****************************************//* Returns a pointer to the "argnum"th argument in "cmd"	*//* (starting at zero). Returns NULL if the argument doesn't	*//* exist.							*//* Warning: terminates the argument with 0 up to the "argnum"th	*//*          argument.						*/{  char	*ret;    ret = NULL;  argnum++;  while (*cmd != 0 && argnum > 0)  {    while (*cmd != 0 && strchr(WHITE_SPACE, *cmd) != NULL) cmd++;    if (*cmd != 0)      {      argnum--;      ret = cmd;      }    while (*cmd != 0 && strchr(WHITE_SPACE, *cmd) == NULL) cmd++;    if (*cmd != 0) *cmd++ = 0;  }  return ret;} /* end of getarg() */static int cmdcmp(const char *cmd, const char *templ)/***************************************************//* command compare *//* If the first argument of "cmd" matches (case insensitive)	*//* "templ", the number of arguments in "cmd" (including the	*//* first) is returned. Otherwise the function returns 0.	*//* The arguments must be separated with some sort of white	*//* space.							*/{  int	nargs = 1;    while (*templ != 0 && tolower(*cmd) == tolower(*templ))    {    cmd++;    templ++;    }  if (*templ != 0 || strchr(WHITE_SPACE, *cmd++) == NULL)    return 0;  while (*cmd != 0)  {    while (*cmd != 0 && strchr(WHITE_SPACE, *cmd) != NULL) cmd++;    if (*cmd != 0) nargs++;    while (*cmd != 0 && strchr(WHITE_SPACE, *cmd) == NULL) cmd++;  }  return nargs;} /* end of cmdcmp() */static void clr_prg(int first_time)/*===============================*/{  for (i = 0; i < PRG_MAX; i++)		/* clear program */  {    if (first_time || bsrc[i] != NULL) free(bsrc[i]);    bsrc[i] = NULL;  }  lpos = 0;				/* reset listing position */} /* end of clr_prg() */static void load_file(char *filename)/*=================================*/{  char		*pch;  int		make_nr, line;    fpt = fopen(filename, "r");  if (fpt == NULL)  {    printf(INDENT);    printf("Cannot open file \"%s\".\n", filename);  }  else  {    clr_prg(FALSE);    make_nr = TRUE;     /* default: no line numbers in file */    line = LINE_BEGIN;    while (fgets(cmdbuf,CMD_MAX, fpt))    {      pch = (skipws(cmdbuf));      if (isdigit(*pch))      {	make_nr = FALSE;        line = 0;	break;      }      else if (isalpha(*pch))      {	break;      }    }    rewind(fpt);    while (fgets(cmdbuf, CMD_MAX, fpt))    {      pch = strstr(cmdbuf, "\n");      if (pch != NULL) *pch = 0;	       /* remove '\n' */      if (isalnum(*skipws(cmdbuf)))      {	get_line(make_nr, line, pch);	if (make_nr) line += LINE_INCREMENT;      }      if (strlen(cmdbuf) >= CMD_MAX-1)      {	cmdbuf[30] = 0;        /* to shorten cmdbuf in error message */	printf(INDENT);	printf("ERROR: Program line \"%s ...\" is too long. \n",	       cmdbuf);	printf(INDENT);	printf("       (Maximum: %d characters.) \n", CMD_MAX);      }    }    fclose(fpt);  }} /* end of load_file() */int basic(int nfiles, char *file_list[])/**************************************/{  char	*pch;  MINT	i;  clr_prg(TRUE);  bc_clrvars();  MCALL(clear_mvars());  WAIT_FCT();  out_fpt = stdout;  load_function_table();  printf("\n");  prg_version();  if (nfiles > 0)  {    for (i = 0; i < nfiles; i++)    {      printf(INDENT);      printf("Executing Basic file \"%s\" ...\n", file_list[i]);      load_file(file_list[i]);      run_program(PR_NORMAL);    }    return EXIT_SUCCESS;  }    while (printf("%s", PROMPT),         fflush(stdout),         fgets(cmdbuf, CMD_MAX, stdin))  {    clean_cmd(cmdbuf);    if (strlen(cmdbuf) == 0)      continue;				/* emty statement */    if (cmdcmp(cmdbuf, "run") == 1)    {						/* run */      run_program(PR_NORMAL);    }    else if (i = cmdcmp(cmdbuf, "list"), i > 0 && i <= 2)    {						/* list */      if (i == 2) lpos = strtol(getarg(cmdbuf, 1), NULL, 0);      if (lpos < 0) lpos = 0;      for (i = 0; i < LIST_MAX && lpos < PRG_MAX; lpos++)      {        if (bsrc[lpos] != NULL)        {          printf(INDENT);          printf("%-4ld %s\n",lpos,bsrc[lpos]);          i++;        }      }      if (i == 0)      {        printf(INDENT);        printf("Exceeded end of program.\n");      }    }    else if (cmdcmp(cmdbuf, "new") == 1)    {						/* new */      clr_prg(FALSE);    }    else if (cmdcmp(cmdbuf, "clear") == 1)	/* clear */    {      bc_clrvars();      MCALL(clear_mvars());      WAIT_FCT();    }    else if (cmdcmp(cmdbuf, "open") == 2             || cmdcmp(cmdbuf, "load") == 2)    {						/* open, load */      load_file(getarg(cmdbuf, 1));    }    else if (cmdcmp(cmdbuf, "save") == 2 ||	     cmdcmp(cmdbuf, "saven") == 2)    {						/* save */      pch = getarg(cmdbuf, 1);      fpt = fopen(pch, "w");      if (fpt == NULL)      {        printf(INDENT);        printf("Cannot open file \"%s\".\n", pch);      }      else      {	pch = (skipws(cmdbuf)+4);        for (i = 1; i < PRG_MAX; i++)        {          if (bsrc[i] != NULL )	  {	    if (*pch == 'n')	      fprintf(fpt, "%-4ld %s\n", i, bsrc[i]);	    else	      fprintf(fpt, "%s\n", bsrc[i]);	  }        }        fclose(fpt);      }    }    else if (cmdcmp(cmdbuf, "version") == 1)    {						/* version */      prg_version();    }    else if (cmdcmp(cmdbuf, "bye") == 1             || cmdcmp(cmdbuf, "exit") == 1             || cmdcmp(cmdbuf, "quit") == 1             || cmdcmp(cmdbuf, "q") == 1)    {				/* quit */      bc_reset();      bc_clrvars();      MCALL(clear_mvars());      WAIT_FCT();      release_function_table();      clr_prg(FALSE);		/* free program lines and variable */      return EXIT_SUCCESS;	/* regular program termination */    }    else if (cmdbuf[0] == '!')    {				/* execute shell command */      system(cmdbuf+1);    }    else if (!isdigit(cmdbuf[0]))    {				/* carry out singleline command */      run_program(PR_SINGLELINE);    }    else    {				/* get program-line */      get_line(FALSE, 0, cmdbuf);    }  }  bc_reset();  bc_clrvars();  MCALL(clear_mvars());  WAIT_FCT();  release_function_table();  clr_prg(FALSE);		/* free program lines and variable */  return EXIT_FAILURE;		/* input stream ended */} /* end of basic() */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -