diff options
author | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2024-09-26 17:10:00 +0330 |
---|---|---|
committer | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2024-09-26 17:10:00 +0330 |
commit | 2ce72027e79a5aba03ecb4180039cd874b230c7a (patch) | |
tree | f9a99c4d241a69405a21c4212796276fda75edcb | |
parent | 3b5df1794867ccffc56ac5633af6c77a0b3952c8 (diff) |
some small improvement
added \h in string (which can use hex as character)
-rw-r--r-- | src/compiler/error_helper/error_helper.c | 9 | ||||
-rw-r--r-- | src/compiler/tree_parser/tree_parser.c | 50 | ||||
-rw-r--r-- | src/compiler/tree_parser/tree_parser.h | 2 |
3 files changed, 52 insertions, 9 deletions
diff --git a/src/compiler/error_helper/error_helper.c b/src/compiler/error_helper/error_helper.c index 42e3745..e307c91 100644 --- a/src/compiler/error_helper/error_helper.c +++ b/src/compiler/error_helper/error_helper.c @@ -16,12 +16,13 @@ void printError(char const *error, SourceCode *sourceCode, char const *begin, for (size_t i = 0; i < sourceCode->size; ++i) { const Code *source = sourceCode->codes[i]; - if (source->code <= begin && strlen(source->code)+source->code >= end) { + if (source->code <= begin && strlen(source->code) + source->code >= end) { code = source; } } if (code == NULL) { - fprintf(stderr, "Bad Error: the error is in no source code\n"); + fprintf(stderr, + "Compiler Internal Error: the error is in no source code\n"); return; } @@ -33,9 +34,7 @@ void printError(char const *error, SourceCode *sourceCode, char const *begin, const char c = *iter; switch (c) { case '\0': - fprintf(stderr, - "Compiler Internal Error: in printing errors at compiler %s:%d", - __FILE_NAME__, __LINE__); + fprintf(stderr, "Compiler Internal Error: in printing errors\n"); exit(1); case '\n': lineBegin = iter + 1; diff --git a/src/compiler/tree_parser/tree_parser.c b/src/compiler/tree_parser/tree_parser.c index fd1dabb..474b6ca 100644 --- a/src/compiler/tree_parser/tree_parser.c +++ b/src/compiler/tree_parser/tree_parser.c @@ -3,6 +3,7 @@ #include <compiler/error_helper/error_helper.h> #include <compiler/lexer/lexer.h> #include <compiler/parser/parser.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -115,6 +116,7 @@ void deleteParsedTree(ParsedTree *parsedTree) { } free(scope->lines); free(scope->variables); + free(scope); } for (size_t i = 0; i < metadata->params_size; ++i) { deleteParsedTree(metadata->params[i]->value); @@ -546,6 +548,13 @@ ParsedTree *treeParseFunction(const ParsedNode *node, SourceCode *code, metadata->params = a404m_malloc(0); metadata->params_size = 0; + metadata->scope = a404m_malloc(sizeof(*metadata->scope)); + + metadata->scope->variables = a404m_malloc(0); + metadata->scope->variables_size = 0; + metadata->scope->lines = a404m_malloc(0); + metadata->scope->lines_size = 0; + for (size_t i = 0; i < params->operands_size; ++i) { const ParsedNode *operand = params->operands[i]; TreeDefineVariableMetadata *define = @@ -574,7 +583,8 @@ ParsedTree *treeParseFunction(const ParsedNode *node, SourceCode *code, goto RETURN_ERROR; return NULL; } else { - metadata->scope = NULL; + free(metadata->scope->lines); + metadata->scope->lines_size = 0; } return tree; @@ -709,9 +719,21 @@ SizedString *nodeToString(ParsedNode const *node, SourceCode *code) { case 'v': c = '\v'; break; - /*case 'u':*/ // TODO: do it - /* c = '';*/ - /* break;*/ + case 'h': { + if (iter + 2 >= strEnd) { + printError("Expected 2 hex characters after '\\c'", code, + node->strBegin, node->strEnd); + goto RETURN_ERROR; + } + bool isRight; + c = hexToInt(iter + 1, iter + 3, &isRight); + if (!isRight) { + printError("Bad hex number", code, iter + 1, iter + 3); + goto RETURN_ERROR; + } + iter += 2; + break; + } default: printError("Bad escape code '\\%s'", code, node->strBegin, node->strEnd, *iter); @@ -737,6 +759,26 @@ RETURN_ERROR: return NULL; } +uint64_t hexToInt(char const *begin, char const *end, bool *isRight) { + uint64_t value = 0; + for (char const *iter = begin; iter < end; ++iter) { + value *= 16; + const char c = *iter; + if ('0' <= c && c <= '9') { + value += c - '0'; + } else if ('A' <= c && c <= 'F') { + value += c - 'A' + 10; + } else if ('a' <= c && c <= 'f') { + value += c - 'a' + 10; + } else { + *isRight = false; + return value; + } + } + *isRight = true; + return value; +} + void pushVariableToScope(TreeScopeMetadata *scope, TreeDefineVariableMetadata *variable) { size_t scope_variables_size = a404m_malloc_usable_size(scope->variables) / diff --git a/src/compiler/tree_parser/tree_parser.h b/src/compiler/tree_parser/tree_parser.h index 1eeb63d..8a9f7b5 100644 --- a/src/compiler/tree_parser/tree_parser.h +++ b/src/compiler/tree_parser/tree_parser.h @@ -1,6 +1,7 @@ #pragma once #include <compiler/parser/parser.h> +#include <stdint.h> typedef enum TreeToken { TREE_TOKEN_NONE = 0, @@ -113,6 +114,7 @@ extern TypeId getTreeExpressionType(ParsedTree *const tree); extern TypeId getType(const TreeDefineVariableMetadata *define); extern bool isType(ParsedTree *const tree); extern SizedString *nodeToString(ParsedNode const *node, SourceCode *code); +extern uint64_t hexToInt(char const*begin,char const*end,bool *isRight); extern void pushVariableToScope(TreeScopeMetadata *scope,TreeDefineVariableMetadata *variable); extern void pushLineToScope(TreeScopeMetadata *scope,ParsedTree *line); |