2019-01-08 22:19:29 +01:00
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <regex.h>
|
|
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "read.h"
|
|
|
|
|
#include "print.h"
|
|
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
undefinedToken,
|
|
|
|
|
numberToken,
|
|
|
|
|
symbolToken,
|
2019-01-20 14:12:47 +01:00
|
|
|
|
quoteToken,
|
2019-01-08 22:19:29 +01:00
|
|
|
|
lParenthesisToken,
|
|
|
|
|
rParenthesisToken
|
|
|
|
|
} tokenType;
|
|
|
|
|
|
|
|
|
|
typedef struct _Token
|
|
|
|
|
{
|
|
|
|
|
tokenType type;
|
|
|
|
|
char *lexeme;
|
|
|
|
|
struct _Token *next;
|
|
|
|
|
} token;
|
|
|
|
|
|
2019-01-14 03:16:25 +01:00
|
|
|
|
int completeExpression(token **tokenQueue);
|
2019-01-08 22:19:29 +01:00
|
|
|
|
char *readline();
|
|
|
|
|
void append(token **head, token *appendix);
|
|
|
|
|
token *lexLine(char *input);
|
2019-01-14 03:16:25 +01:00
|
|
|
|
object parseExpression(token **inputList);
|
2019-01-08 22:19:29 +01:00
|
|
|
|
|
2019-01-14 03:16:25 +01:00
|
|
|
|
token *tokenQueue = NULL;
|
2019-01-08 22:19:29 +01:00
|
|
|
|
|
|
|
|
|
object read(char *prompt)
|
|
|
|
|
{
|
|
|
|
|
printf("%s", prompt);
|
|
|
|
|
|
2019-01-14 03:16:25 +01:00
|
|
|
|
while (!completeExpression(&tokenQueue))
|
2019-01-08 22:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
char *input = readline();
|
2019-01-14 03:16:25 +01:00
|
|
|
|
if (input == NULL) /* унесен је EOF */
|
2019-01-08 22:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
printf("\nКрај улазног стрима.\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
2019-01-14 03:16:25 +01:00
|
|
|
|
append(&tokenQueue, lexLine(input));
|
2019-01-08 22:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 03:16:25 +01:00
|
|
|
|
return parseExpression(&tokenQueue);
|
2019-01-08 22:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 03:16:25 +01:00
|
|
|
|
int completeExpression(token **tokenQueue)
|
2019-01-08 22:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
int result = 0, indentLevel = 0;
|
2019-01-14 03:16:25 +01:00
|
|
|
|
token *current = *tokenQueue;
|
2019-01-08 22:19:29 +01:00
|
|
|
|
|
|
|
|
|
while (current != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (current->type == lParenthesisToken)
|
|
|
|
|
{
|
|
|
|
|
++indentLevel;
|
|
|
|
|
}
|
|
|
|
|
else if (current->type == rParenthesisToken)
|
|
|
|
|
{
|
|
|
|
|
if (indentLevel == 0)
|
|
|
|
|
{
|
2019-01-14 03:16:25 +01:00
|
|
|
|
token **deleteParen = tokenQueue;
|
2019-01-08 22:19:29 +01:00
|
|
|
|
while (*deleteParen != current)
|
|
|
|
|
{
|
|
|
|
|
deleteParen = &(*deleteParen)->next;
|
|
|
|
|
}
|
|
|
|
|
*deleteParen = current->next;
|
|
|
|
|
free(current);
|
|
|
|
|
current = *deleteParen;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (indentLevel == 1)
|
|
|
|
|
{
|
|
|
|
|
result = 1;
|
|
|
|
|
}
|
|
|
|
|
--indentLevel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-01-20 14:12:47 +01:00
|
|
|
|
if (indentLevel == 0 && current->type != quoteToken)
|
2019-01-08 22:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
result = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
current = current->next;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ssize_t bytesRead;
|
|
|
|
|
size_t nbytes = 2048;
|
|
|
|
|
char *buffer = NULL;
|
|
|
|
|
|
|
|
|
|
char *readline()
|
|
|
|
|
{
|
|
|
|
|
if (buffer == NULL)
|
|
|
|
|
{
|
|
|
|
|
buffer = (char *) malloc(nbytes + 1);
|
|
|
|
|
}
|
|
|
|
|
bytesRead = getline(&buffer, &nbytes, stdin);
|
|
|
|
|
if (bytesRead == -1)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 00:33:54 +01:00
|
|
|
|
buffer[strlen(buffer)-1] = '\0';
|
2019-01-14 03:16:25 +01:00
|
|
|
|
/* Уклања завршни њу-лајн или ЕОФ у стрингу
|
|
|
|
|
* и копира стринг на ново место */
|
2019-01-08 22:19:29 +01:00
|
|
|
|
|
2019-01-15 00:33:54 +01:00
|
|
|
|
return buffer;
|
2019-01-08 22:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void append(token **head, token *tail)
|
|
|
|
|
{
|
|
|
|
|
token **current = head;
|
|
|
|
|
|
|
|
|
|
while (*current != NULL)
|
|
|
|
|
{
|
|
|
|
|
current = &(*current)->next;
|
|
|
|
|
}
|
|
|
|
|
*current = tail;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-20 01:24:29 +01:00
|
|
|
|
regex_t regNumber, regSymbol, regLParenthesis, regRParenthesis, regSpace,
|
|
|
|
|
regQuote;
|
2019-01-08 22:19:29 +01:00
|
|
|
|
|
|
|
|
|
token *lex1Token(char *input, int *i)
|
|
|
|
|
{
|
|
|
|
|
token *result = malloc(sizeof(token));
|
|
|
|
|
result->next = NULL;
|
|
|
|
|
|
|
|
|
|
regcomp(®Space, "^[[:space:]]*", REG_EXTENDED);
|
|
|
|
|
|
|
|
|
|
regcomp(®Number, "^[-+]?[[:digit:]]+", REG_EXTENDED);
|
2019-01-20 14:12:47 +01:00
|
|
|
|
regcomp(®Symbol, "^[-+/*_\\\\=<>!&?[:alnum:]]+", REG_EXTENDED);
|
2019-01-20 01:24:29 +01:00
|
|
|
|
regcomp(®Quote, "^'", REG_EXTENDED);
|
2019-01-08 22:19:29 +01:00
|
|
|
|
regcomp(®LParenthesis, "^\\(", REG_EXTENDED);
|
|
|
|
|
regcomp(®RParenthesis, "^\\)", REG_EXTENDED);
|
|
|
|
|
|
|
|
|
|
const int nmatches = 1;
|
|
|
|
|
regmatch_t a[nmatches];
|
|
|
|
|
|
|
|
|
|
regexec(®Space, input + *i, nmatches, a, 0);
|
|
|
|
|
*i += a[0].rm_eo;
|
|
|
|
|
/* помера индекс да би се игнорисали почетни "вајт-спејс" карактери */
|
|
|
|
|
|
2019-01-20 01:24:29 +01:00
|
|
|
|
if (!regexec(®Symbol, input + *i, nmatches, a, 0))
|
2019-01-08 22:19:29 +01:00
|
|
|
|
{
|
2019-01-20 01:24:29 +01:00
|
|
|
|
int tmp = a[0].rm_eo;
|
|
|
|
|
if (!regexec(®Number, input + *i, nmatches, a, 0) &&
|
|
|
|
|
tmp == a[0].rm_eo)
|
|
|
|
|
/* симбол може садржати цифре на било којој позицији али не може сам бити број
|
|
|
|
|
* не постоји погодан начина да се ово путем regex-a запише стога овај if
|
|
|
|
|
* исказ */
|
|
|
|
|
{
|
|
|
|
|
result->type = numberToken;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
regexec(®Symbol, input + *i, nmatches, a, 0);
|
|
|
|
|
/* претходни regexec позив је променио вредност a[0].rm_eo, овиме се она враћа
|
|
|
|
|
* на дужину нађеног симбола */
|
|
|
|
|
result->type = symbolToken;
|
|
|
|
|
}
|
2019-01-08 22:19:29 +01:00
|
|
|
|
}
|
2019-01-20 14:12:47 +01:00
|
|
|
|
else if (!regexec(®Quote, input + *i, nmatches, a, 0))
|
|
|
|
|
{
|
|
|
|
|
result->type = quoteToken;
|
|
|
|
|
}
|
2019-01-08 22:19:29 +01:00
|
|
|
|
else if (!regexec(®LParenthesis, input + *i, nmatches, a, 0))
|
|
|
|
|
{
|
|
|
|
|
result->type = lParenthesisToken;
|
|
|
|
|
}
|
|
|
|
|
else if (!regexec(®RParenthesis, input + *i, nmatches, a, 0))
|
|
|
|
|
{
|
|
|
|
|
result->type = rParenthesisToken;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result->type = undefinedToken;
|
|
|
|
|
result->lexeme = NULL;
|
|
|
|
|
goto skipStringCopy;
|
|
|
|
|
}
|
|
|
|
|
result->lexeme = malloc((a[0].rm_eo + 1) * sizeof(char));
|
|
|
|
|
strncpy(result->lexeme, input + *i, a[0].rm_eo);
|
|
|
|
|
result->lexeme[a[0].rm_eo] = '\0';
|
|
|
|
|
*i += a[0].rm_eo;
|
|
|
|
|
|
|
|
|
|
regexec(®Space, input + *i, nmatches, a, 0);
|
|
|
|
|
*i += a[0].rm_eo;
|
|
|
|
|
/* игнорисање крајњих вајт-спејс карактера */
|
|
|
|
|
|
|
|
|
|
skipStringCopy:
|
|
|
|
|
regfree(®Space);
|
|
|
|
|
regfree(®Number);
|
|
|
|
|
regfree(®Symbol);
|
|
|
|
|
regfree(®LParenthesis);
|
|
|
|
|
regfree(®RParenthesis);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token *lexLine(char *input)
|
|
|
|
|
{
|
|
|
|
|
int i = 0, n;
|
|
|
|
|
n = strlen(input);
|
|
|
|
|
token *root = NULL, **new;
|
|
|
|
|
new = &root;
|
|
|
|
|
while (i < n)
|
|
|
|
|
{
|
|
|
|
|
*new = lex1Token(input, &i);
|
|
|
|
|
if ((*new)->type == undefinedToken)
|
|
|
|
|
{
|
|
|
|
|
/* уколико се у реду нађе токен који је лексички погрешан, штампа се место тог
|
2019-01-14 03:16:25 +01:00
|
|
|
|
* токена у реду и бришу се сви токени нађени у реду, функција враћа NULL*/
|
2019-01-08 22:19:29 +01:00
|
|
|
|
fprintf(stderr, "Невалидан токен на месту %d\n", i);
|
|
|
|
|
new = &root;
|
|
|
|
|
while (*new != NULL)
|
|
|
|
|
{
|
|
|
|
|
free(root->lexeme);
|
|
|
|
|
new = &((*new)->next);
|
|
|
|
|
free(root);
|
|
|
|
|
root = *new;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
new = &((*new)->next);
|
|
|
|
|
}
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 03:16:25 +01:00
|
|
|
|
object parseExpression(token **inputList)
|
2019-01-08 22:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
object result;
|
|
|
|
|
|
|
|
|
|
token input = **inputList;
|
|
|
|
|
free(*inputList);
|
|
|
|
|
*inputList = input.next;
|
2019-01-14 03:16:25 +01:00
|
|
|
|
/* скида први преостали токен са листе унесених, да би се могао
|
|
|
|
|
* прерадити */
|
2019-01-08 22:19:29 +01:00
|
|
|
|
|
|
|
|
|
if (input.type == numberToken)
|
|
|
|
|
{
|
2019-01-14 03:16:25 +01:00
|
|
|
|
TYPE(result) = numberObject;
|
|
|
|
|
NUM(result) = atoll(input.lexeme);
|
2019-01-08 22:19:29 +01:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else if (input.type == symbolToken)
|
|
|
|
|
{
|
2019-01-14 03:16:25 +01:00
|
|
|
|
TYPE(result) = symbolObject;
|
|
|
|
|
SYM(result) = malloc((strlen(input.lexeme) + 1)
|
|
|
|
|
* sizeof(char));
|
|
|
|
|
/* Алокација стринга */
|
|
|
|
|
strcpy(SYM(result), input.lexeme);
|
2019-01-08 22:19:29 +01:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-01-20 14:12:47 +01:00
|
|
|
|
else if (input.type == quoteToken)
|
|
|
|
|
{
|
|
|
|
|
if ((*inputList)->type != rParenthesisToken)
|
|
|
|
|
{
|
|
|
|
|
TYPE(result) = consObject;
|
|
|
|
|
CONS(result) = malloc(sizeof(cons));
|
|
|
|
|
TYPE(CAR(result)) = symbolObject;
|
|
|
|
|
SYM(CAR(result)) = malloc((strlen("навод") + 1)
|
|
|
|
|
* sizeof(char));
|
|
|
|
|
strcpy(SYM(CAR(result)), "навод");
|
|
|
|
|
|
|
|
|
|
TYPE(CDR(result)) = consObject;
|
|
|
|
|
CONS(CDR(result)) = malloc(sizeof(cons));
|
|
|
|
|
CAR(CDR(result)) = parseExpression(inputList);
|
|
|
|
|
|
|
|
|
|
TYPE(CDR(CDR(result))) = nilObject;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
TYPE(result) = errorObject;
|
|
|
|
|
ERR(result) = syntaxError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* уколико "'" оператер директно претходи затвореној загради није могуће
|
|
|
|
|
* правилно га претворити у навод оператер стога се он изоставља из било којег
|
|
|
|
|
* израза */
|
|
|
|
|
}
|
2019-01-08 22:19:29 +01:00
|
|
|
|
else if (input.type == lParenthesisToken)
|
|
|
|
|
{
|
|
|
|
|
object *listCurrent = &result;
|
|
|
|
|
|
|
|
|
|
while ((*inputList)->type != rParenthesisToken)
|
|
|
|
|
{
|
2019-01-14 03:16:25 +01:00
|
|
|
|
TYPE(*listCurrent) = consObject;
|
|
|
|
|
CONS(*listCurrent) = malloc(sizeof(cons));
|
|
|
|
|
/* Алокација конс ћелије */
|
2019-01-08 22:19:29 +01:00
|
|
|
|
|
2019-01-14 03:16:25 +01:00
|
|
|
|
CAR(*listCurrent) = parseExpression(inputList);
|
2019-01-08 22:19:29 +01:00
|
|
|
|
|
2019-01-14 03:16:25 +01:00
|
|
|
|
listCurrent = &CDR(*listCurrent);
|
2019-01-08 22:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 03:16:25 +01:00
|
|
|
|
TYPE(*listCurrent) = nilObject;
|
2019-01-08 22:19:29 +01:00
|
|
|
|
|
|
|
|
|
input = **inputList;
|
|
|
|
|
free(*inputList);
|
|
|
|
|
*inputList = input.next;
|
|
|
|
|
|
2019-01-20 14:12:47 +01:00
|
|
|
|
int noErrors = 1;
|
|
|
|
|
listCurrent = &result;
|
|
|
|
|
|
|
|
|
|
while (TYPE(*listCurrent) != nilObject)
|
|
|
|
|
{
|
|
|
|
|
if (TYPE(CAR(*listCurrent)) == errorObject)
|
|
|
|
|
{
|
|
|
|
|
noErrors = 0;
|
|
|
|
|
}
|
|
|
|
|
listCurrent = &CDR(*listCurrent);
|
|
|
|
|
}
|
|
|
|
|
if (!noErrors)
|
|
|
|
|
{
|
|
|
|
|
deleteObject(result);
|
|
|
|
|
TYPE(result) = errorObject;
|
|
|
|
|
ERR(result) = syntaxError;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (input.type == rParenthesisToken)
|
|
|
|
|
{
|
|
|
|
|
TYPE(result) = errorObject;
|
|
|
|
|
ERR(result) = syntaxError;
|
|
|
|
|
}
|
|
|
|
|
free(input.lexeme);
|
2019-01-08 22:19:29 +01:00
|
|
|
|
return result;
|
|
|
|
|
}
|