juegos

Juegos de terminal de Pancho: Catan (TUI, GUI, web, servidor, PicoCalc), ajedrez, calculadora y minijuegos
git clone https://git.lu3dhn.xyz/juegos.git
Log | Files | Refs

eval.h (2413B)


      1 /* eval.h - compilar la expresion a RPN y evaluarla. */
      2 #ifndef EVAL_H
      3 #define EVAL_H
      4 #include "expr.h"
      5 #include "num.h"
      6 #include "matrix.h"
      7 
      8 enum CalcErr { CE_OK, CE_SYNTAX, CE_MATH, CE_STACK, CE_ARG, CE_NOVAR };
      9 
     10 enum Op {
     11     OP_NUM, OP_VAR, OP_ANS, OP_PREANS, OP_PI, OP_E, OP_RAN,
     12     OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_NEG, OP_POW, OP_SQRT, OP_ROOT, OP_LOGB, OP_ABS,
     13     OP_MIXED, OP_FACT, OP_PCT, OP_INV, OP_NPR, OP_NCR, OP_FN, OP_EQ,
     14     OP_AND, OP_OR, OP_XOR, OP_XNOR,
     15     OP_SKIP, OP_INTEG, OP_DERIV, OP_SUM, OP_PROD,    /* analisis: el cuerpo va en un tramo salteado */
     16     OP_IMAG, OP_POLAR,                               /* complejos: i y r∠θ */
     17     OP_MAT, OP_VCT                                   /* matrices: arg 0-3 = A-D, 4 = Ans */
     18 };
     19 
     20 typedef struct { uint8_t op, arg, pos, nargs, len; } Code;   /* len: largo del cuerpo (analisis) */
     21 
     22 #define PROG_MAX 160
     23 #define PROG_CONSTS 32
     24 typedef struct {
     25     Code c[PROG_MAX];
     26     int n;
     27     Num k[PROG_CONSTS];
     28     int nk;
     29     bool has_eq;            /* tiene "=" (para SOLVE) */
     30 } Prog;
     31 
     32 typedef struct {
     33     int ang;                /* ANG_DEG/RAD/GRA */
     34     Num *vars;              /* NVARS */
     35     Num ans, preans;
     36     uint32_t (*rand)(void);
     37     bool prog;              /* modo programador: enteros, letras A-F son digitos */
     38     int base, bits;         /* base de entrada (2/8/10/16), 32 o 64 */
     39     bool rvalid;            /* regresion de Estadistica para x̂ y ŷ */
     40     int rtype;
     41     Num ra, rb, rc;
     42     bool cplx;              /* modo Complejo */
     43     Num ans_im;
     44     Num *vars_im;
     45     const MVal *mats, *vcts;  /* 5 de cada uno: A-D y Ans (modo Matrices) */
     46     bool ans_mat;             /* el ultimo resultado fue matriz o vector */
     47 } EvalCtx;
     48 
     49 /* devuelve CE_* y en *pos el token donde esta el error */
     50 int compile(const Expr *e, const EvalCtx *cx, Prog *p, int *pos);
     51 int eval(const Prog *p, const EvalCtx *cx, Num *out, int *pos);
     52 /* compila y evalua; si hay "=" evalua izquierda - derecha */
     53 int calc_eval(const Expr *e, const EvalCtx *cx, Num *out, int *pos);
     54 /* modo Complejo: re + im·i */
     55 int calc_eval_c(const Expr *e, const EvalCtx *cx, Num *re, Num *im, int *pos);
     56 /* modo Matrices: el resultado puede ser escalar, matriz o vector */
     57 int calc_eval_m(const Expr *e, const EvalCtx *cx, MVal *out, int *pos);
     58 /* argumento de re + im·i en la unidad de angulo (exacto si se puede) */
     59 Num cplx_arg(Num re, Num im, int ang);
     60 
     61 #endif