Додат булеански тип

This commit is contained in:
kappa 2019-01-29 23:54:32 +01:00
parent a257cd21cd
commit 5b5f792f5d
4 changed files with 31 additions and 10 deletions

View file

@ -54,16 +54,15 @@ void printValue(object input)
{
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)
{
printf("%s", SYM(input));
}
else if (TYPE(input) == boolObject)
{
printf("#%s", BOOL(input) ? "истинито" : "лажно");
}
else if (TYPE(input) == consObject)
{
printf("(");

25
read.c
View file

@ -13,6 +13,7 @@ typedef enum
numberFracToken,
numberRealToken,
symbolToken,
boolToken,
quoteToken,
lParenthesisToken,
rParenthesisToken
@ -42,7 +43,8 @@ object read(char *prompt)
char *input = readline();
if (input == NULL) /* унесен је EOF */
{
printf("\nКрај улазног стрима.\n");
printf("\nКрај улазног стрима.\nВоЗдра и дођите нам\
опет!\n");
exit(0);
}
append(&tokenQueue, lexLine(input));
@ -151,11 +153,12 @@ void deleteTokenList(token *root)
token *lexLine(char *input)
{
regex_t regSpace, regTokenGeneral, regNumberFrac, regNumberReal,
regLParenthesis, regRParenthesis, regSymbol, regQuote;
regLParenthesis, regRParenthesis, regSymbol, regQuote, regDot,
regBool;
regcomp(&regSpace, "^[[:space:]]*", REG_EXTENDED);
regcomp(&regTokenGeneral,"^(\\(|\\)|'|[-,.+/*_\\\\=<>!&?[:alnum:]]+)",
REG_EXTENDED);
regcomp(&regTokenGeneral,"^(\\(|\\)|'|[-,.+/*_\\\\=<>!&?[:alnum:]]+|\
\\|[[:print:]]+\\||\"[[:print:]]+\")", REG_EXTENDED);
const int nmatches = 1;
regmatch_t a[nmatches];
@ -201,10 +204,13 @@ token *lexLine(char *input)
REG_EXTENDED);
regcomp(&regNumberReal, "^[-+]?[[:digit:]]*,[[:digit:]]+$",
REG_EXTENDED);
regcomp(&regSymbol, "^[-+/*_\\\\=<>!&?[:alnum:]]+$", REG_EXTENDED);
regcomp(&regSymbol, "^([-+/*_\\\\=<>!&?[:alnum:]]+|\
\\|[[:print:]]+\\|)$", REG_EXTENDED);
regcomp(&regQuote, "^'$", REG_EXTENDED);
regcomp(&regLParenthesis, "^\\($", REG_EXTENDED);
regcomp(&regRParenthesis, "^\\)$", REG_EXTENDED);
regcomp(&regDot, "^\\.$", REG_EXTENDED);
regcomp(&regBool, "^(истинито|лажно)$", REG_EXTENDED);
new = &root;
while ((*new) != NULL)
@ -218,6 +224,10 @@ token *lexLine(char *input)
{
(*new)->type = numberRealToken;
}
else if (!regexec(&regBool, (*new)->lexeme, nmatches, a, 0))
{
(*new)->type = boolToken;
}
else if (!regexec(&regSymbol, (*new)->lexeme, nmatches, a, 0))
{
(*new)->type = symbolToken;
@ -272,6 +282,11 @@ object parseExpression(token **inputList)
result = shortenFractionNum(result);
}
else if (input.type == boolToken)
{
TYPE(result) = boolObject;
BOOL(result) = !strcmp("истинито", input.lexeme) ? 1 : 0;
}
else if (input.type == numberRealToken)
{
TYPE(result) = numberObject;

4
util.c
View file

@ -99,6 +99,10 @@ object copyObject(object input)
CAR(result) = copyObject(CAR(input));
CDR(result) = copyObject(CDR(input));
}
else if (TYPE(input) == boolObject)
{
BOOL(result) = BOOL(input);
}
else if (TYPE(input) == procedureObject)
{
PROC(result) = malloc(sizeof(procedure));

3
util.h
View file

@ -6,6 +6,7 @@
#define CAR(x) (((x).value.consCell)->car)
#define CDR(x) (((x).value.consCell)->cdr)
#define SYM(x) ((x).value.symbol)
#define BOOL(x) ((x).value.boolean)
#define ERR(x) ((x).value.err)
#define PROC(x) ((x).value.proc)
@ -27,6 +28,7 @@ typedef enum
numberObject,
symbolObject,
procedureObject,
boolObject,
errorObject
} dataType;
@ -83,6 +85,7 @@ struct object
cons *consCell;
number num;
procedure *proc;
int boolean;
} value;
};