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

📄 tclib.c

📁 用C语言编的一个小解释器
💻 C
字号:
/*****   Internal Library Functions *****/

/* Add more of your own, here. */

#include "conio.h"    /* if your compiler does not support this
                         header file, remove it. */
#include "stdio.h"
#include "stdlib.h"

extern char *prog;   /* points to current location in program */
extern char token[80];   /* holds string representation of token */
extern char token_type;  /* contains type of token */
extern char tok;         /* holds the internal representation of token */

enum tok_types {DELIMITER, IDENTIFIER, NUMBER, COMMAND, STRING, QUOTE,
                VARIABLE, BLOCK, FUNCTION};

/* These are the constants used to call sntx_err() when
   a syntax error occurs. And more if you like.
   NOTE: SYNTAX is a generic error message used when
   nothing else seems appropriate.
*/
enum error_msg
	{SYNTAX, UNBAL_PARENS, NO_EXP, EQUALS_EXPECTED,
	 NOT_VAR, PARAM_ERR, SEMI_EXPECTED,
	 UNBAL_BRACES, FUNC_UNDEF, TYPE_EXPECTED,
	 NEST_FUNC, RET_NOCALL, PAREN_EXPECTED,
	 WHILE_EXPECTED, QUOTE_EXPECTED, NOT_STRING,
	 TOO_MANY_LVARS};

int get_token(void);
void sntx_err(int error), eval_exp(int *result);
void putback(void);
int call_getche(),call_putch();
int call_puts(),print(),getnum();

/* Get a character from console. (Use getchar() if
   your compiler does not support getche().) */
int call_getche()
{
	char ch;
	
	ch=getche();
	while(*prog!=')') prog++;
	prog++;   /* advance to end of line */
	return ch;
}

/* Put a character to the display. (Use putchar()
   if your compiler does not support putch().) */
int call_putch()
{
	int value;
	
	eval_exp(&value);
	printf("%c", value);
	return value;
}

/* Call puts(). */
int call_puts(void)
{
	get_token();
	if(*token!='(') sntx_err(PAREN_EXPECTED);
	get_token();
	if(token_type!=QUOTE) sntx_err(QUOTE_EXPECTED);
	puts(token);
	get_token();
	if(*token!=')') sntx_err(PAREN_EXPECTED);
	
	get_token();
	if(*token!=';') sntx_err(SEMI_EXPECTED);
	putback();
	return 0;
}

/* A built-in console output function. */
int print(void)
{
	int i;
	
	get_token();
	if(*token!='(') sntx_err(PAREN_EXPECTED);
	
	get_token();
	if(token_type==QUOTE) {  /* output a string */
		printf("%s",token);
	}
	else {   /* output a number */
		putback();
		eval_exp(&i);
		printf("%d",i);
	}
	
	get_token();
	
	if(*token!=')') sntx_err(PAREN_EXPECTED);
	
	get_token();
	if(*token!=';') sntx_err(SEMI_EXPECTED);
	putback();
	return 0;
}

/* Read an integer from the keyboard. */
int getnum(void)
{
	char s[80];
	
	gets(s);
	while(*prog!=')') prog++;
	prog++;   /* advance to end of line */
	return atoi(s);
}

⌨️ 快捷键说明

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