diff options
author | Pedro Souza <pedro@masba.net> | 2024-04-03 06:34:52 -0300 |
---|---|---|
committer | Pedro Souza <pedro@masba.net> | 2024-04-03 06:34:52 -0300 |
commit | 131dfc5e9f810b1c1e1fb1d75e781842ec601a65 (patch) | |
tree | 492de7cb339e79815c0f555b599f23e00398fd3d | |
parent | 4adade92f2b4f59ff09bbbd056f6ea2e366730ed (diff) |
test exe
-rw-r--r-- | main.c | 46 | ||||
-rw-r--r-- | tree.c | 14 | ||||
-rw-r--r-- | tree.h | 2 |
3 files changed, 56 insertions, 6 deletions
@@ -1,11 +1,55 @@ #include <stdio.h> #include "tree.h" +#include "bu-parser.h" + +struct lex_str_data { + const char *str; + size_t slen; + size_t needle; +}; + +struct chartoken lex_str(void *v) { + struct lex_str_data *d = v; + if (d->needle >= d->slen) { + return (struct chartoken){.id = TOKEN_EOS, .c = '\0' }; + } + struct chartoken token; + do { + token.c = d->str[d->needle++]; + enum token_id id = TOKEN_INVALID; + if (token.c >= 'a' && token.c <= 'z') { token.id = TOKEN_VARIABLE; } + else { + switch (token.c) { + case '!': token.id = TOKEN_UNARY_OPERATOR; break; + case '*': + case '+': token.id = TOKEN_BINARY_OPERATOR; break; + default: break; + } + } + } while (token.c == ' '); + return token; +} int main(){ char str[100]; printf("type in your tree(no spaces please): "); scanf("%[^\n]", str); - printTree(parse(str),0); + char str2[100]; + strncpy(str2, str, 100); + printTree(parse(str2),0); + + struct lex_str_data lxd; + lxd.str = str; + lxd.slen = strlen(str); + lxd.needle = 0; + struct expr_parser ep = expr_parser_init(lex_str, &lxd); + enum parser_state s = expr_parser_run(&ep); + if (s == PARSER_ACCEPT) { + printTree(ep.stack[0].expr, 0); + } else { + expr_parser_debug_print(stderr, &ep); + } + expr_parser_finish(&ep); return 0; } @@ -41,20 +41,24 @@ node * parse(char *str) { //expects a string with no whitespace return this; } -void printTree(node *root, int level) { +void fprintTree(FILE *stream, node *root, int level) { if (root == NULL) return; if (level) { for (int i = 1; i < level; i++) { - printf("│ "); + fprintf(stream, "│ "); } - printf("├─ "); + fprintf(stream, "├─ "); } - printf("%c\n", root->el); + fprintf(stream, "%c\n", root->el); for (int i = 0; i < 2; i++) { if (root->child[i] != NULL) { - printTree(root->child[i], level + 1); + fprintTree(stream, root->child[i], level + 1); } } } + +void printTree(node *root, int level) { + fprintTree(stdout, root, level); +} @@ -1,5 +1,6 @@ #ifndef TREE_H_HG #define TREE_H_HG +#include <stdio.h> enum nodeType { BIOP, @@ -20,6 +21,7 @@ typedef struct node { } node; node * parse(char *str); +void fprintTree(FILE *stream, node *root, int level); void printTree(node *root, int level); #endif |