Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // BetterParsing
- //
- // Created by Victor Martins on 31/05/19.
- // Copyright © 2019 Victor Martins. All rights reserved.
- //
- #include<stdio.h>
- #include<stdlib.h>
- #include <string.h>
- #define msg_invalidComm "Comando inválido"
- #define show(s, idx) printf("%s %d\n", s, idx)
- static void error (const char *msg, int line) {
- fprintf(stderr, "%s na linha %d\n", msg, line);
- exit(EXIT_FAILURE);
- }
- int main (void) {
- int line = 1;
- int currentChar;
- /// Flag usada para checar a presença de um comentário
- char commentFlag; // TODO: Considerar remover
- FILE *inputFile;
- if ((inputFile = fopen ("fact", "r")) == NULL) {
- perror ("Não foi possível abrir o arquivo!");
- exit(1);
- }
- while ((currentChar = fgetc(inputFile)) != EOF) {
- switch (currentChar) {
- case 'r': { /* retorno */
- /// Tipo do valor a ser retornado
- char varType;
- /// Índice da variável ou valor da constante a ser retornada
- int idxOrConst;
- // Lê o final da instrução procurando por 1 valor
- if (fscanf(inputFile, "et %c%d", &varType, &idxOrConst) != 2)
- error(msg_invalidComm, line);
- switch (varType) {
- case '$': show("Constante", idxOrConst); break;
- case 'v': show("Variável", idxOrConst); break;
- default: error(msg_invalidComm, line);
- }
- printf("%d | ret %c%d\n", line, varType, idxOrConst);
- break;
- }
- case 'v': { /* Atribuição ou Op. Aritmética */
- /// Índice da variável que vai receber um novo valor
- int destIdx;
- /// Tipo da expressão (atribuição ou operação aritmética)
- char asignmOrEq;
- // Lê mais uma parte da instrução procurando um índice e
- // um operador ('<' ou '=').
- if (fscanf(inputFile, "%d %c", &destIdx, &asignmOrEq) != 2)
- error(msg_invalidComm, line);
- if (asignmOrEq == '<') { /* Atribuição */
- /// Tipo do valor a ser atribuído
- char srcType;
- /// Índice ou a constante a ser atribuída
- int idxOrConst;
- // Lê o restante do comando procurando por 1 operando
- if (fscanf(inputFile, " %c%d", &srcType, &idxOrConst) != 2)
- error(msg_invalidComm, line);
- switch (srcType) {
- case 'v': show("Variável", idxOrConst); break;
- case 'p': show("Parâmetro", idxOrConst); break;
- case '$': show("Constante", idxOrConst); break;
- default: error(msg_invalidComm, line);
- }
- printf("%d | v%d < %c%d\n", line, destIdx, srcType, idxOrConst);
- }
- else if (asignmOrEq == '=') { /* Operação Aritmética */
- /// Operador aritmético
- char op;
- /// Tipo dos valores a serem manipulados
- char fstType, secType;
- /// Índices ou constantes a serem manipulados
- int fstIdxOrConst, secIdxOrConst;
- // Lê o restante do comando procurando por dois valores e um
- // operando entre eles.
- if (fscanf(inputFile, " %c%d %c %c%d", &fstType, &fstIdxOrConst, &op, &secType, &secIdxOrConst) != 5)
- error(msg_invalidComm, line);
- printf("%d | v%d = %c%d %c %c%d\n",
- line, destIdx, fstType, fstIdxOrConst, op, secType, secIdxOrConst);
- } else {
- error(msg_invalidComm, line);
- }
- break;
- }
- case 'i': { /* desvio condicional */
- /// Índice da variável a ser checada
- int varIndex;
- /// Número da linha para a qual a execução será desviada caso
- /// a condicional seja verdadeira.
- int targetLine;
- // Lê o restante do comando procurando uma variável e qual
- // linha ir caso necessário
- if (fscanf(inputFile, "flez v%d %d", &varIndex, &targetLine) != 2)
- error(msg_invalidComm, line);
- printf("%d | iflez v%d %d\n", line, varIndex, targetLine);
- break;
- }
- default: error("Comando desconhecido", line);
- }
- line ++;
- commentFlag = '\0';
- fscanf(inputFile, " /%1[/]%*[^\n] ", &commentFlag);
- // printf("scanf %d\n", fscanf(inputFile, " /%1[/]%*[^\n] ", &commentFlag));
- if (commentFlag != '\0') {
- printf("<Comentário ignorado>\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement