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

📄 lclib.c

📁 自己做的一个C语言的解释器 给大家做个参考
💻 C
字号:
/********************************************************
* Porject Name:
*	Little C Interprete
* Version & Revision:
*	1.0.1
* Creation Date:
*	2005-02-02
* Author:
*	Herbet Schildt & Yock Yu
* Description:
*	Internal Library Functions
*	You can add more your own, here.
********************************************************/

#include "conio.h"/* if you compile 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, KEYWORD, TEMP, STRING, BLOCK};

/*
	These are the constants used to all sntx_err()
	when a syntax error occurs. Add more if you like.
	NOTE:SYNTAX is generic error message used when
	noting 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,
				NO_TEMP, TOO_MANY_LVARS, DIV_BY_ZERO};

extern int get_token(void);
extern void sntx_err(int error);
extern void eval_exp(int *result);
extern void putback(void);

/*
	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++; /* advaced to end of line */
	return ch;
}

/* Put a character to the display. */
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 != STRING)
		sntx_err(QUOTE_EXPECTED);

	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 == STRING){ /* output a string */
		printf("%s", token);
	}
	else{ /* out put 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++;
	return atoi(s);
}

⌨️ 快捷键说明

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