From 24170851bfa05665cc9bf386735dc53968efd466 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Tue, 7 Mar 2023 23:13:23 +0100 Subject: [PATCH] move all code relative to readline into separate file (2) --- element.c | 88 +++++++++++++++++++++++++++++++ element.h | 16 +++++- parser.c | 149 ----------------------------------------------------- parser.h | 6 --- readline.c | 64 ++++++++++++++++++++++- readline.h | 5 ++ 6 files changed, 170 insertions(+), 158 deletions(-) diff --git a/element.c b/element.c index 19c30df..e285504 100644 --- a/element.c +++ b/element.c @@ -6,6 +6,94 @@ /* global variables */ +#define MAX_ARGS 100 + +keyword_t operators[NB_OPERATORS] = { + { "+\t", Add, 2, 1, 1}, + { "-\t", Sub, 2, 1, 1}, + { "*", Mul, 2, 1, 2}, + { "/", Div, 2, 1, 2}, + { "%", Mod, 2, 1, 3}, + { "^", Pow, 2, 1, 4}, + { "==", Equal, 2, 2, -1}, + { "!=", Diff, 2, 2, -1}, + { ">=", Ge, 2, 2, -1}, + { "<=", Le, 2, 2, -1}, + { ">", Gt, 2, 1, -1}, + { "<", Lt, 2, 1, -1}, + { "&", And, 2, 1, -2}, + { "|", Or, 2, 1, -2} +}; + +keyword_t functions[NB_FUNCTIONS] = { + { "sqrt", Sqr, 1, 4, 5}, + { "pow", Pow, 2, 3, 5}, + { "cos", Cos, 1, 3, 5}, + { "sin", Sin, 1, 3, 5}, + { "tan", Tan, 1, 3, 5}, + { "acos", Acos, 1, 4, 5}, + { "asin", Asin, 1, 4, 5}, + { "atan", Atan, 1, 4, 5}, + { "ln", Ln, 1, 2, 5}, + { "log", Log, 1, 3, 5}, + { "exp", Exp, 1, 3, 5}, + { "erfc", Erfc, 1, 4, 5}, + { "erf", Erf, 1, 3, 5}, + { "abs", Abs, 1, 3, 5}, + { "floor", Floor, 1, 5, 5}, + { "ceil", Ceil, 1, 4, 5}, + { "sto", Store, 2, 3, 5}, + { "rcl", Recall, 1, 3, 5}, + { "inc", Inc, 1, 3, 5}, + { "dec", Dec, 1, 3, 5}, + { "disp", Disp, 0, 4, 9}, + { "mem", Memory, 1, 3, 5}, + { "clr", Clear, 0, 3, 9}, + { "quit", Quit, 0, 4, 9}, + { "help", Help, 0, 4, 9}, + { "hist", History, 0, 4, 9}, + { "!", Not, 1, 1, 6}, + { "cond", Cond, 3, 4, 5}, + { "while", While, 2, 5, 5}, + { "print", Print, 1, 5, 5}, + { "prog", Prog, 2, 4, 9}, + { "arg", Arg, 1, 3, 5}, + { "call", Call, MAX_ARGS, 4, 5}, + { "ls", List, 0, 2, 9}, + { "edit", Edit, 1, 4, 9}, + { "del", Del, 1, 3, 9}, + { "get", Get, 1, 3, 5}, + { "len", Length, 0, 3, 5}, + { "pop", Pop, 0, 3, 5}, + { "push", Push, 1, 4, 5}, + { "put", Put, 2, 3, 5}, + { "set", Set, MAX_ARGS, 3, 5}, + { "show", Show, 0, 4, 5}, + { "max", Max, 2, 3, 5}, + { "mean", Mean, 2, 4, 5}, + { "med", Median, 0, 3, 5}, + { "min", Min, 2, 3, 5}, + { "ord", Order, 0, 3, 5}, + { "prod", Prod, 0, 4, 5}, + { "sum", Sum, 0, 3, 5}, + { "var", Variance, 2, 3, 5}, + { "format", Precision, 1, 6, 9}, + { "base", Base, 2, 4, 9}, + { "deg", Deg, 0, 3, 9}, + { "grad", Grad, 0, 4, 9}, + { "rad", Rad, 0, 3, 9} +}; + +keyword_t constants[NB_CONSTANTS] = { + { "ans", Ans, 0, 3, 5}, + { "e", E, 0, 1, 5}, + { "pi", Pi, 0, 2, 5} +}; + +char *symbols[NB_SYMBOLS] = { + "(", ")", "{", "}" +}; + /* allocate new element */ element_t *newelement (func_t function, int nbops, int prio) diff --git a/element.h b/element.h index 5467978..03b8128 100644 --- a/element.h +++ b/element.h @@ -1,8 +1,6 @@ #ifndef __ELEMENT_H__ #define __ELEMENT_H__ -/* global variables */ - /* function type */ typedef enum { @@ -36,6 +34,20 @@ typedef struct _keyword_t { float prio; } keyword_t; +/* global variables */ + +#define NB_OPERATORS 14 +extern keyword_t operators[]; + +#define NB_FUNCTIONS 56 +extern keyword_t functions[]; + +#define NB_CONSTANTS 3 +extern keyword_t constants[]; + +#define NB_SYMBOLS 4 +extern char *symbols[]; + /* calculus element type */ typedef struct _element_t { diff --git a/parser.c b/parser.c index 4d9b01a..44152a9 100644 --- a/parser.c +++ b/parser.c @@ -46,100 +46,6 @@ int codecmp (char *ref, char *str) return 0; } -/* functions */ - -#define MAX_ARGS 100 - -#define NB_OPERATORS 14 -keyword_t operators[NB_OPERATORS] = { - { "+\t", Add, 2, 1, 1}, - { "-\t", Sub, 2, 1, 1}, - { "*", Mul, 2, 1, 2}, - { "/", Div, 2, 1, 2}, - { "%", Mod, 2, 1, 3}, - { "^", Pow, 2, 1, 4}, - { "==", Equal, 2, 2, -1}, - { "!=", Diff, 2, 2, -1}, - { ">=", Ge, 2, 2, -1}, - { "<=", Le, 2, 2, -1}, - { ">", Gt, 2, 1, -1}, - { "<", Lt, 2, 1, -1}, - { "&", And, 2, 1, -2}, - { "|", Or, 2, 1, -2} -}; - -#define NB_FUNCTIONS 56 -keyword_t functions[NB_FUNCTIONS] = { - { "sqrt", Sqr, 1, 4, 5}, - { "pow", Pow, 2, 3, 5}, - { "cos", Cos, 1, 3, 5}, - { "sin", Sin, 1, 3, 5}, - { "tan", Tan, 1, 3, 5}, - { "acos", Acos, 1, 4, 5}, - { "asin", Asin, 1, 4, 5}, - { "atan", Atan, 1, 4, 5}, - { "ln", Ln, 1, 2, 5}, - { "log", Log, 1, 3, 5}, - { "exp", Exp, 1, 3, 5}, - { "erfc", Erfc, 1, 4, 5}, - { "erf", Erf, 1, 3, 5}, - { "abs", Abs, 1, 3, 5}, - { "floor", Floor, 1, 5, 5}, - { "ceil", Ceil, 1, 4, 5}, - { "sto", Store, 2, 3, 5}, - { "rcl", Recall, 1, 3, 5}, - { "inc", Inc, 1, 3, 5}, - { "dec", Dec, 1, 3, 5}, - { "disp", Disp, 0, 4, 9}, - { "mem", Memory, 1, 3, 5}, - { "clr", Clear, 0, 3, 9}, - { "quit", Quit, 0, 4, 9}, - { "help", Help, 0, 4, 9}, - { "hist", History, 0, 4, 9}, - { "!", Not, 1, 1, 6}, - { "cond", Cond, 3, 4, 5}, - { "while", While, 2, 5, 5}, - { "print", Print, 1, 5, 5}, - { "prog", Prog, 2, 4, 9}, - { "arg", Arg, 1, 3, 5}, - { "call", Call, MAX_ARGS, 4, 5}, - { "ls", List, 0, 2, 9}, - { "edit", Edit, 1, 4, 9}, - { "del", Del, 1, 3, 9}, - { "get", Get, 1, 3, 5}, - { "len", Length, 0, 3, 5}, - { "pop", Pop, 0, 3, 5}, - { "push", Push, 1, 4, 5}, - { "put", Put, 2, 3, 5}, - { "set", Set, MAX_ARGS, 3, 5}, - { "show", Show, 0, 4, 5}, - { "max", Max, 2, 3, 5}, - { "mean", Mean, 2, 4, 5}, - { "med", Median, 0, 3, 5}, - { "min", Min, 2, 3, 5}, - { "ord", Order, 0, 3, 5}, - { "prod", Prod, 0, 4, 5}, - { "sum", Sum, 0, 3, 5}, - { "var", Variance, 2, 3, 5}, - { "format", Precision, 1, 6, 9}, - { "base", Base, 2, 4, 9}, - { "deg", Deg, 0, 3, 9}, - { "grad", Grad, 0, 4, 9}, - { "rad", Rad, 0, 3, 9} -}; - -#define NB_CONSTANTS 3 -keyword_t constants[NB_CONSTANTS] = { - { "ans", Ans, 0, 3, 5}, - { "e", E, 0, 1, 5}, - { "pi", Pi, 0, 2, 5} -}; - -#define NB_SYMBOLS 4 -char *symbols[NB_SYMBOLS] = { - "(", ")", "{", "}" -}; - /* subparser function */ element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio) @@ -968,59 +874,4 @@ double evaluate_element (element_t *root, char mask) return 0; } -char **generate_completion_list () -{ - int i, j, l = 0; - char **list = (char **) callocordie (NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1, sizeof (char *)); - - for (i = 0; i < NB_OPERATORS; i++) { - list[l] = strdup ((operators + i)->keyword); - for (j = 0; j < (int)strlen (list[l]); j++) { - if (list[i][j] == '\t') { - list[i][j] = '\0'; - } - } - if (list[l] != NULL) { - l++; - } - } - - for (i = 0; i < NB_FUNCTIONS; i++) { - list[l] = strdup ((functions + i)->keyword); - if (list[l] != NULL) { - l++; - } - } - - for (i = 0; i < NB_CONSTANTS; i++) { - list[l] = strdup ((constants + i)->keyword); - if (list[l] != NULL) { - l++; - } - } - - for (i = 0; i < NB_SYMBOLS; i++) { - list[l] = strdup (symbols[i]); - if (list[l] != NULL) { - l++; - } - } - - return (list); -} - -void free_completion_list (char **list) -{ - int i; - - if (list) { - for (i = 0; i < NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1; i++) { - if (list[i] != NULL) { - free (list[i]); - } - } - free (list); - } -} - /* vim: set ts=4 sw=4 et: */ diff --git a/parser.h b/parser.h index 9ce4fd0..bf28113 100644 --- a/parser.h +++ b/parser.h @@ -7,12 +7,6 @@ extern double answer; -/* completion functions */ - -char **generate_completion_list (); - -void free_completion_list (char **list); - /* parser function */ void delelement (element_t *root); diff --git a/readline.c b/readline.c index 4b234f4..280e5f0 100644 --- a/readline.c +++ b/readline.c @@ -5,8 +5,9 @@ #include #include +#include "alloc.h" #include "debug.h" -#include "parser.h" +#include "element.h" #include "readline.h" @@ -138,3 +139,64 @@ void clean_read_line (char *buffer) free (buffer); } } + +/* generate completion list */ + +char **generate_completion_list () +{ + int i, j, l = 0; + char **list = (char **) callocordie (NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1, sizeof (char *)); + + for (i = 0; i < NB_OPERATORS; i++) { + list[l] = strdup ((operators + i)->keyword); + for (j = 0; j < (int)strlen (list[l]); j++) { + if (list[i][j] == '\t') { + list[i][j] = '\0'; + } + } + if (list[l] != NULL) { + l++; + } + } + + for (i = 0; i < NB_FUNCTIONS; i++) { + list[l] = strdup ((functions + i)->keyword); + if (list[l] != NULL) { + l++; + } + } + + for (i = 0; i < NB_CONSTANTS; i++) { + list[l] = strdup ((constants + i)->keyword); + if (list[l] != NULL) { + l++; + } + } + + for (i = 0; i < NB_SYMBOLS; i++) { + list[l] = strdup (symbols[i]); + if (list[l] != NULL) { + l++; + } + } + + return (list); +} + +/* free completion list */ + +void free_completion_list (char **list) +{ + int i; + + if (list) { + for (i = 0; i < NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1; i++) { + if (list[i] != NULL) { + free (list[i]); + } + } + free (list); + } +} + +/* vim: set ts=4 sw=4 et: */ diff --git a/readline.h b/readline.h index dcd143c..69fc54e 100644 --- a/readline.h +++ b/readline.h @@ -6,4 +6,9 @@ int read_line (char **buffer, char *prompt); void manage_history (char *buffer); void clean_read_line (char *buffer); +char **generate_completion_list (); +void free_completion_list (char **list); + #endif /* __READLINE_H__ */ + +/* vim: set ts=4 sw=4 et: */ -- 2.30.2