Поправљени багови у лексеру

This commit is contained in:
kappa 2019-01-07 01:04:40 +01:00
parent 1bdbcdb020
commit d2f18f2339
5 changed files with 38 additions and 16 deletions

View file

@ -1,7 +1,7 @@
# cirilisp - компајлер за ћирилични дијалекат лиспа # cirilisp - компајлер за ћирилични дијалекат лиспа
# ћирилисп верзија # ћирилисп верзија
VERSION = 0.0 VERSION = 0.1
# локација за инсталацију # локација за инсталацију
PREFIX = /usr/local PREFIX = /usr/local
@ -13,7 +13,7 @@ LDFLAGS = -lm -lc
CC = cc CC = cc
SRC = cirilisp.c util.c lexer.c SRC = cirilisp.c readline.c lexer.c
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)
all: cirilisp all: cirilisp
@ -21,7 +21,7 @@ all: cirilisp
.c.o: .c.o:
$(CC) -c $(CFLAGS) $< $(CC) -c $(CFLAGS) $<
$(OBJ): util.h lexer.h $(OBJ): readline.h lexer.h
cirilisp: $(OBJ) cirilisp: $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS) $(CC) -o $@ $(OBJ) $(LDFLAGS)
@ -31,7 +31,7 @@ clean:
dist: clean dist: clean
mkdir -p cirilisp-$(VERSION) mkdir -p cirilisp-$(VERSION)
cp -r Makefile util.h $(SRC) cirilisp-$(VERSION) cp -r Makefile readline.h $(SRC) cirilisp-$(VERSION)
tar -cf cirilisp-$(VERSION).tar cirilisp-$(VERSION) tar -cf cirilisp-$(VERSION).tar cirilisp-$(VERSION)
gzip cirilisp-$(VERSION).tar gzip cirilisp-$(VERSION).tar
rm -rf cirilisp-$(VERSION) rm -rf cirilisp-$(VERSION)

View file

@ -32,7 +32,7 @@ int main(int argc, char **argv)
int i = 0; int i = 0;
while (current != NULL) while (current != NULL)
{ {
printf("Токен бр. %d: \"%s\", тип:%s\n", i, current->lexeme, current->type == number ? "number" : (current->type == symbol ? "symbol" : "parenthesis")); printf("Токен бр. %d: \"%s\", тип:%s\n", i, current->lexeme, current->type == numberToken ? "number" : (current->type == symbolToken ? "symbol" : "parenthesis"));
current = current->next; current = current->next;
++i; ++i;
} }

14
lexer.c
View file

@ -19,7 +19,7 @@ token *lexLine(char *input)
while (i < n) while (i < n)
{ {
*new = lex1Token(input, &i); *new = lex1Token(input, &i);
if ((*new)->type == undefined) if ((*new)->type == undefinedToken)
{ {
/* уколико се у реду нађе токен који је лексички погрешан, штампа се место тог /* уколико се у реду нађе токен који је лексички погрешан, штампа се место тог
токена у реду и брише се цела листа, функција враћа NULL*/ токена у реду и брише се цела листа, функција враћа NULL*/
@ -59,21 +59,21 @@ token *lex1Token(char *input, int *i)
*i += a[0].rm_eo; *i += a[0].rm_eo;
/* помера индекс да би се игнорисали почетни "вајт-спејс" карактери */ /* помера индекс да би се игнорисали почетни "вајт-спејс" карактери */
if (!regexec(&regSymbol, input + *i, nmatches, a, 0)) if (!regexec(&regNumber, input + *i, nmatches, a, 0))
{ {
result->type = symbol; result->type = numberToken;
} }
else if (!regexec(&regNumber, input + *i, nmatches, a, 0)) else if (!regexec(&regSymbol, input + *i, nmatches, a, 0))
{ {
result->type = number; result->type = symbolToken;
} }
else if (!regexec(&regParenthesis, input + *i, nmatches, a, 0)) else if (!regexec(&regParenthesis, input + *i, nmatches, a, 0))
{ {
result->type = parenthesis; result->type = parenthesisToken;
} }
else else
{ {
result->type = undefined; result->type = undefinedToken;
result->lexeme = NULL; result->lexeme = NULL;
goto skipStringCopy; goto skipStringCopy;
} }

View file

@ -2,10 +2,10 @@
typedef enum typedef enum
{ {
undefined, undefinedToken,
number, numberToken,
symbol, symbolToken,
parenthesis parenthesisToken
} tokenType; } tokenType;
typedef struct _Token typedef struct _Token

22
util.h Normal file
View file

@ -0,0 +1,22 @@
#pragma once
typedef enum
{
nilObject,
consObject,
numberObject,
symbolObject
} dataType;
typedef struct _Object
{
dataType type;
void *address;
} object;
typedef struct _Cons
{
object car;
object cdr;
} cons;