Додат булеански тип
This commit is contained in:
		
							parent
							
								
									a257cd21cd
								
							
						
					
					
						commit
						5b5f792f5d
					
				
					 4 changed files with 31 additions and 10 deletions
				
			
		
							
								
								
									
										9
									
								
								print.c
									
										
									
									
									
								
							
							
						
						
									
										9
									
								
								print.c
									
										
									
									
									
								
							| 
						 | 
					@ -54,16 +54,15 @@ void printValue(object input)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		printf("<процедура:%s>", PROC_TYPE(input) == builtinProc ?
 | 
							printf("<процедура:%s>", PROC_TYPE(input) == builtinProc ?
 | 
				
			||||||
				"уграђена" : "сложена");
 | 
									"уграђена" : "сложена");
 | 
				
			||||||
		if (PROC_TYPE(input) == compoundProc)
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			printValue(PROC_COMP_ARGS(input));
 | 
					 | 
				
			||||||
			printValue(PROC_COMP_BODY(input));
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	else if (TYPE(input) == symbolObject)
 | 
						else if (TYPE(input) == symbolObject)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		printf("%s", SYM(input));
 | 
							printf("%s", SYM(input));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						else if (TYPE(input) == boolObject)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							printf("#%s", BOOL(input) ? "истинито" : "лажно");
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	else if (TYPE(input) == consObject)
 | 
						else if (TYPE(input) == consObject)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		printf("(");
 | 
							printf("(");
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										25
									
								
								read.c
									
										
									
									
									
								
							
							
						
						
									
										25
									
								
								read.c
									
										
									
									
									
								
							| 
						 | 
					@ -13,6 +13,7 @@ typedef enum
 | 
				
			||||||
	numberFracToken,
 | 
						numberFracToken,
 | 
				
			||||||
	numberRealToken,
 | 
						numberRealToken,
 | 
				
			||||||
	symbolToken,
 | 
						symbolToken,
 | 
				
			||||||
 | 
						boolToken,
 | 
				
			||||||
	quoteToken,
 | 
						quoteToken,
 | 
				
			||||||
	lParenthesisToken,
 | 
						lParenthesisToken,
 | 
				
			||||||
	rParenthesisToken
 | 
						rParenthesisToken
 | 
				
			||||||
| 
						 | 
					@ -42,7 +43,8 @@ object read(char *prompt)
 | 
				
			||||||
		char *input = readline();
 | 
							char *input = readline();
 | 
				
			||||||
		if (input == NULL) /* унесен је EOF */
 | 
							if (input == NULL) /* унесен је EOF */
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			printf("\nКрај улазног стрима.\n");
 | 
								printf("\nКрај улазног стрима.\nВоЗдра и дођите нам\
 | 
				
			||||||
 | 
					 опет!\n");
 | 
				
			||||||
			exit(0);
 | 
								exit(0);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		append(&tokenQueue, lexLine(input));
 | 
							append(&tokenQueue, lexLine(input));
 | 
				
			||||||
| 
						 | 
					@ -151,11 +153,12 @@ void deleteTokenList(token *root)
 | 
				
			||||||
token *lexLine(char *input)
 | 
					token *lexLine(char *input)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	regex_t regSpace, regTokenGeneral, regNumberFrac, regNumberReal,
 | 
						regex_t regSpace, regTokenGeneral, regNumberFrac, regNumberReal,
 | 
				
			||||||
		regLParenthesis, regRParenthesis, regSymbol, regQuote;
 | 
							regLParenthesis, regRParenthesis, regSymbol, regQuote, regDot,
 | 
				
			||||||
 | 
							regBool;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	regcomp(®Space, "^[[:space:]]*", REG_EXTENDED);
 | 
						regcomp(®Space, "^[[:space:]]*", REG_EXTENDED);
 | 
				
			||||||
	regcomp(®TokenGeneral,"^(\\(|\\)|'|[-,.+/*_\\\\=<>!&?[:alnum:]]+)",
 | 
						regcomp(®TokenGeneral,"^(\\(|\\)|'|[-,.+/*_\\\\=<>!&?[:alnum:]]+|\
 | 
				
			||||||
			REG_EXTENDED);
 | 
					\\|[[:print:]]+\\||\"[[:print:]]+\")", REG_EXTENDED);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const int nmatches = 1;
 | 
						const int nmatches = 1;
 | 
				
			||||||
	regmatch_t a[nmatches];
 | 
						regmatch_t a[nmatches];
 | 
				
			||||||
| 
						 | 
					@ -201,10 +204,13 @@ token *lexLine(char *input)
 | 
				
			||||||
			REG_EXTENDED);
 | 
								REG_EXTENDED);
 | 
				
			||||||
	regcomp(®NumberReal, "^[-+]?[[:digit:]]*,[[:digit:]]+$",
 | 
						regcomp(®NumberReal, "^[-+]?[[:digit:]]*,[[:digit:]]+$",
 | 
				
			||||||
			REG_EXTENDED);
 | 
								REG_EXTENDED);
 | 
				
			||||||
	regcomp(®Symbol, "^[-+/*_\\\\=<>!&?[:alnum:]]+$", REG_EXTENDED);
 | 
						regcomp(®Symbol, "^([-+/*_\\\\=<>!&?[:alnum:]]+|\
 | 
				
			||||||
 | 
					\\|[[:print:]]+\\|)$", REG_EXTENDED);
 | 
				
			||||||
	regcomp(®Quote, "^'$", REG_EXTENDED);
 | 
						regcomp(®Quote, "^'$", REG_EXTENDED);
 | 
				
			||||||
	regcomp(®LParenthesis, "^\\($", REG_EXTENDED);
 | 
						regcomp(®LParenthesis, "^\\($", REG_EXTENDED);
 | 
				
			||||||
	regcomp(®RParenthesis, "^\\)$", REG_EXTENDED);
 | 
						regcomp(®RParenthesis, "^\\)$", REG_EXTENDED);
 | 
				
			||||||
 | 
						regcomp(®Dot, "^\\.$", REG_EXTENDED);
 | 
				
			||||||
 | 
						regcomp(®Bool, "^(истинито|лажно)$", REG_EXTENDED);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	new = &root;
 | 
						new = &root;
 | 
				
			||||||
	while ((*new) != NULL)
 | 
						while ((*new) != NULL)
 | 
				
			||||||
| 
						 | 
					@ -218,6 +224,10 @@ token *lexLine(char *input)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			(*new)->type = numberRealToken;
 | 
								(*new)->type = numberRealToken;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							else if (!regexec(®Bool, (*new)->lexeme, nmatches, a, 0))
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								(*new)->type = boolToken;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		else if (!regexec(®Symbol, (*new)->lexeme, nmatches, a, 0))
 | 
							else if (!regexec(®Symbol, (*new)->lexeme, nmatches, a, 0))
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			(*new)->type = symbolToken;
 | 
								(*new)->type = symbolToken;
 | 
				
			||||||
| 
						 | 
					@ -272,6 +282,11 @@ object parseExpression(token **inputList)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		result = shortenFractionNum(result);
 | 
							result = shortenFractionNum(result);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						else if (input.type == boolToken)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							TYPE(result) = boolObject;
 | 
				
			||||||
 | 
							BOOL(result) = !strcmp("истинито", input.lexeme) ? 1 : 0;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	else if (input.type == numberRealToken)
 | 
						else if (input.type == numberRealToken)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		TYPE(result) = numberObject;
 | 
							TYPE(result) = numberObject;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										4
									
								
								util.c
									
										
									
									
									
								
							
							
						
						
									
										4
									
								
								util.c
									
										
									
									
									
								
							| 
						 | 
					@ -99,6 +99,10 @@ object copyObject(object input)
 | 
				
			||||||
		CAR(result) = copyObject(CAR(input));
 | 
							CAR(result) = copyObject(CAR(input));
 | 
				
			||||||
		CDR(result) = copyObject(CDR(input));
 | 
							CDR(result) = copyObject(CDR(input));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						else if (TYPE(input) == boolObject)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							BOOL(result) = BOOL(input);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	else if (TYPE(input) == procedureObject)
 | 
						else if (TYPE(input) == procedureObject)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		PROC(result) = malloc(sizeof(procedure));
 | 
							PROC(result) = malloc(sizeof(procedure));
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										3
									
								
								util.h
									
										
									
									
									
								
							
							
						
						
									
										3
									
								
								util.h
									
										
									
									
									
								
							| 
						 | 
					@ -6,6 +6,7 @@
 | 
				
			||||||
#define CAR(x) (((x).value.consCell)->car)
 | 
					#define CAR(x) (((x).value.consCell)->car)
 | 
				
			||||||
#define CDR(x) (((x).value.consCell)->cdr)
 | 
					#define CDR(x) (((x).value.consCell)->cdr)
 | 
				
			||||||
#define SYM(x) ((x).value.symbol)
 | 
					#define SYM(x) ((x).value.symbol)
 | 
				
			||||||
 | 
					#define BOOL(x) ((x).value.boolean)
 | 
				
			||||||
#define ERR(x) ((x).value.err)
 | 
					#define ERR(x) ((x).value.err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define PROC(x) ((x).value.proc)
 | 
					#define PROC(x) ((x).value.proc)
 | 
				
			||||||
| 
						 | 
					@ -27,6 +28,7 @@ typedef enum
 | 
				
			||||||
	numberObject,
 | 
						numberObject,
 | 
				
			||||||
	symbolObject,
 | 
						symbolObject,
 | 
				
			||||||
	procedureObject,
 | 
						procedureObject,
 | 
				
			||||||
 | 
						boolObject,
 | 
				
			||||||
	errorObject
 | 
						errorObject
 | 
				
			||||||
} dataType;
 | 
					} dataType;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -83,6 +85,7 @@ struct object
 | 
				
			||||||
		cons *consCell;
 | 
							cons *consCell;
 | 
				
			||||||
		number num;
 | 
							number num;
 | 
				
			||||||
		procedure *proc;
 | 
							procedure *proc;
 | 
				
			||||||
 | 
							int boolean;
 | 
				
			||||||
	} value;
 | 
						} value;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue