From 5b5f792f5d2166b4e83bfa006a4192f7f3eb885f Mon Sep 17 00:00:00 2001 From: kappa Date: Tue, 29 Jan 2019 23:54:32 +0100 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B4=D0=B0=D1=82=20=D0=B1=D1=83?= =?UTF-8?q?=D0=BB=D0=B5=D0=B0=D0=BD=D1=81=D0=BA=D0=B8=20=D1=82=D0=B8=D0=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- print.c | 9 ++++----- read.c | 25 ++++++++++++++++++++----- util.c | 4 ++++ util.h | 3 +++ 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/print.c b/print.c index 582d145..8facbfb 100644 --- a/print.c +++ b/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("("); diff --git a/read.c b/read.c index f1a4eec..cb61ead 100644 --- a/read.c +++ b/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; diff --git a/util.c b/util.c index 4598e6c..073648f 100644 --- a/util.c +++ b/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)); diff --git a/util.h b/util.h index 9bd2b3a..4a2711c 100644 --- a/util.h +++ b/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; };