diff options
Diffstat (limited to '21sh/ll-expr/src/eval')
| -rw-r--r-- | 21sh/ll-expr/src/eval/ast_print.c | 57 | ||||
| -rw-r--r-- | 21sh/ll-expr/src/eval/rpn_print.c | 52 | ||||
| -rw-r--r-- | 21sh/ll-expr/src/eval/token_printer.c | 34 |
3 files changed, 143 insertions, 0 deletions
diff --git a/21sh/ll-expr/src/eval/ast_print.c b/21sh/ll-expr/src/eval/ast_print.c new file mode 100644 index 0000000..9d7cbb8 --- /dev/null +++ b/21sh/ll-expr/src/eval/ast_print.c @@ -0,0 +1,57 @@ +#include <stdio.h> + +#include "lexer.h" +#include "parser.h" + +char tab[] = { [AST_PLUS] = '+', + [AST_MINUS] = '-', + [AST_MUL] = '*', + [AST_DIV] = '/' }; + +void print_ast(struct ast *ast) +{ + if (ast == NULL) + return; + + if (ast->type == AST_NUMBER) + printf("%zu", ast->value); + else if (ast->type == AST_NEG) + printf("-%zu", (ast->left)->value); + else + { + printf("("); + + print_ast(ast->left); + + printf("%c", tab[ast->type]); + + print_ast(ast->right); + + printf(")"); + } +} + +int main(int argc, char *argv[]) +{ + if (argc != 2) + return 1; + + struct lexer *lexer = lexer_new(argv[1]); + + struct ast *ast; + enum parser_status status = PARSER_OK; + ast = parse(&status, lexer); + if (status != PARSER_OK) + { + lexer_free(lexer); + return 1; + } + + print_ast(ast); + printf("\n"); + + ast_free(ast); + lexer_free(lexer); + + return 0; +} diff --git a/21sh/ll-expr/src/eval/rpn_print.c b/21sh/ll-expr/src/eval/rpn_print.c new file mode 100644 index 0000000..defb519 --- /dev/null +++ b/21sh/ll-expr/src/eval/rpn_print.c @@ -0,0 +1,52 @@ +#include <stdio.h> + +#include "lexer.h" +#include "parser.h" + +char tab[] = { [AST_PLUS] = '+', + [AST_MINUS] = '-', + [AST_MUL] = '*', + [AST_DIV] = '/' }; + +void print_ast(struct ast *ast) +{ + if (!ast) + return; + + if (ast->type == AST_NUMBER) + printf("%zu ", ast->value); + else if (ast->type == AST_NEG) + printf("-%zu ", (ast->left)->value); + else + { + print_ast(ast->left); + print_ast(ast->right); + + printf("%c ", tab[ast->type]); + } +} + +int main(int argc, char *argv[]) +{ + if (argc != 2) + return 1; + + struct lexer *lexer = lexer_new(argv[1]); + + struct ast *ast; + enum parser_status status; + ast = parse(&status, lexer); + if (status != PARSER_OK) + { + lexer_free(lexer); + return 1; + } + + print_ast(ast); + printf("\n"); + + ast_free(ast); + lexer_free(lexer); + + return 0; +} diff --git a/21sh/ll-expr/src/eval/token_printer.c b/21sh/ll-expr/src/eval/token_printer.c new file mode 100644 index 0000000..78096f4 --- /dev/null +++ b/21sh/ll-expr/src/eval/token_printer.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +#include "lexer.h" + +char tab[] = { + [TOKEN_PLUS] = '+', [TOKEN_MINUS] = '-', [TOKEN_MUL] = '*', + [TOKEN_DIV] = '/', [TOKEN_LEFT_PAR] = '(', [TOKEN_RIGHT_PAR] = ')' +}; + +int main(int argc, char *argv[]) +{ + if (argc != 2) + return 1; + + struct lexer *lexer = lexer_new(argv[1]); + struct token token = lexer_pop(lexer); + + while (token.type != TOKEN_EOF && token.type != TOKEN_ERROR) + { + if (token.type == TOKEN_NUMBER) + printf("%zu\n", token.value); + else + printf("%c\n", tab[token.type]); + + token = lexer_pop(lexer); + } + + if (token.type == TOKEN_EOF) + printf("EOF\n"); + + lexer_free(lexer); + + return 0; +} |
