Додат булеански тип
This commit is contained in:
parent
a257cd21cd
commit
5b5f792f5d
9
print.c
9
print.c
|
@ -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
25
read.c
|
@ -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(®Space, "^[[:space:]]*", REG_EXTENDED);
|
||||
regcomp(®TokenGeneral,"^(\\(|\\)|'|[-,.+/*_\\\\=<>!&?[:alnum:]]+)",
|
||||
REG_EXTENDED);
|
||||
regcomp(®TokenGeneral,"^(\\(|\\)|'|[-,.+/*_\\\\=<>!&?[:alnum:]]+|\
|
||||
\\|[[:print:]]+\\||\"[[:print:]]+\")", REG_EXTENDED);
|
||||
|
||||
const int nmatches = 1;
|
||||
regmatch_t a[nmatches];
|
||||
|
@ -201,10 +204,13 @@ token *lexLine(char *input)
|
|||
REG_EXTENDED);
|
||||
regcomp(®NumberReal, "^[-+]?[[:digit:]]*,[[:digit:]]+$",
|
||||
REG_EXTENDED);
|
||||
regcomp(®Symbol, "^[-+/*_\\\\=<>!&?[:alnum:]]+$", REG_EXTENDED);
|
||||
regcomp(®Symbol, "^([-+/*_\\\\=<>!&?[:alnum:]]+|\
|
||||
\\|[[:print:]]+\\|)$", REG_EXTENDED);
|
||||
regcomp(®Quote, "^'$", REG_EXTENDED);
|
||||
regcomp(®LParenthesis, "^\\($", REG_EXTENDED);
|
||||
regcomp(®RParenthesis, "^\\)$", REG_EXTENDED);
|
||||
regcomp(®Dot, "^\\.$", REG_EXTENDED);
|
||||
regcomp(®Bool, "^(истинито|лажно)$", REG_EXTENDED);
|
||||
|
||||
new = &root;
|
||||
while ((*new) != NULL)
|
||||
|
@ -218,6 +224,10 @@ token *lexLine(char *input)
|
|||
{
|
||||
(*new)->type = numberRealToken;
|
||||
}
|
||||
else if (!regexec(®Bool, (*new)->lexeme, nmatches, a, 0))
|
||||
{
|
||||
(*new)->type = boolToken;
|
||||
}
|
||||
else if (!regexec(®Symbol, (*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
4
util.c
|
@ -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
3
util.h
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue