Implement analyse function

This commit is contained in:
Yannick Reiß 2023-09-21 15:09:27 +02:00
parent 584545d9c3
commit 96a5f3cda6
No known key found for this signature in database
GPG Key ID: 5A3AF456F0A0338C
2 changed files with 54 additions and 0 deletions

View File

@ -1,6 +1,16 @@
#ifndef ANALYZER_H
#define ANALYZER_H
#include <stdio.h>
#include <stdlib.h>
/**
* @name analyze
* @return int
* @brief Analyze the tokens syntax. Return -1 on error and 1 on warning, 0 else.
*
* @param char* tokens: analyze_tokens
*/
int analyze (char* tokens);
#endif//ANALYZER_H

View File

@ -1 +1,45 @@
#include "../include/analyzer.h"
/**
* @name analyze
* @return int
* @brief Check for errors and warnings.
*
* @param char* token
*/
int analyze (char* token) {
int rv = 0;
int position = 0;
for (char* cursor = token; *cursor; cursor++) {
/* Analyze form */
if (*cursor == '[') {
rv += 1;
} else if (*cursor == ']') {
rv -= 1;
}
/* Check value for errors. */
if (rv < 0) {
(void)printf("Error on token %d\n", position);
perror("ERROR: Closing bracket without opening bracket!");
rv = EXIT_FAILURE;
break;
}
/* check for nested loops */
if (rv > 1) {
(void)printf("Warning on token %d\n", position);
(void)printf("WARNING: Nested loops are not supported on all versions of the target device!\n");
}
}
if (rv > 0) {
(void)printf("There are %d loops without end!\n", rv);
perror("ERROR: Loop not closed!");
rv = EXIT_FAILURE;
}
return rv;
}