aboutsummaryrefslogtreecommitdiff
path: root/src/Infobox/Calculator.c
blob: 0c08a9ee4a058f34d9b7327272fb147f98a201f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "Calculator.h"
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char logic_log[4096];

typedef struct {
  const char *buffer;
  int pos;
} Parser;

static double parse_expression(Parser *p);

static void skip_ws(Parser *p) {
  while (p->buffer[p->pos] == ' ')
    p->pos++;
}

static double parse_factor(Parser *p) {
  skip_ws(p);
  if (p->buffer[p->pos] == '-') {
    p->pos++;
    return -parse_factor(p);
  }
  if (p->buffer[p->pos] == '(') {
    p->pos++;
    double res = parse_expression(p);
    if (p->buffer[p->pos] == ')')
      p->pos++;
    return res;
  }
  char *endptr;
  double val = strtod(&p->buffer[p->pos], &endptr);
  p->pos = (int)(endptr - p->buffer);
  return val;
}

static double parse_exponent(Parser *p) {
  double left = parse_factor(p);
  skip_ws(p);
  if (p->buffer[p->pos] == '^') {
    p->pos++;
    double right = parse_exponent(p);
    double old = left;
    left = pow(old, right);

    char step[256];
    snprintf(step, sizeof(step), "<div>%g ^ %g = <b>%g</b></div>", old, right,
             left);
    strncat(logic_log, step, sizeof(logic_log) - strlen(logic_log) - 1);
  }
  return left;
}

static double parse_term(Parser *p) {
  double left = parse_exponent(p);
  while (1) {
    skip_ws(p);
    char op = p->buffer[p->pos];
    if (op == '*' || op == '/') {
      p->pos++;
      double right = parse_exponent(p);
      double old = left;
      left = (op == '*') ? left * right : left / right;

      char step[256];

      snprintf(step, sizeof(step), "<div>%g %c %g = <b>%g</b></div>", old, op,
               right, left);
      strncat(logic_log, step, sizeof(logic_log) - strlen(logic_log) - 1);
    } else
      break;
  }
  return left;
}

static double parse_expression(Parser *p) {
  double left = parse_term(p);
  while (1) {
    skip_ws(p);
    char op = p->buffer[p->pos];
    if (op == '+' || op == '-') {
      p->pos++;
      double right = parse_term(p);
      double old = left;
      left = (op == '+') ? left + right : left - right;

      char step[256];

      snprintf(step, sizeof(step), "<div>%g %c %g = <b>%g</b></div>", old, op,
               right, left);
      strncat(logic_log, step, sizeof(logic_log) - strlen(logic_log) - 1);
    } else
      break;
  }
  return left;
}

double evaluate(const char *expr) {
  logic_log[0] = '\0';
  if (!expr || strlen(expr) == 0)
    return 0.0;
  Parser p = {expr, 0};
  return parse_expression(&p);
}

InfoBox fetch_calc_data(char *math_input) {
  InfoBox info = {NULL, NULL, NULL, NULL};
  if (!math_input)
    return info;

  double result = evaluate(math_input);

  char html_output[5120];
  snprintf(html_output, sizeof(html_output),
           "<div class='calc-container' style='line-height: 1.6;'>"
           "%s"
           "<div style='margin-top: 8px; border-top: 1px solid #eee; "
           "padding-top: 8px; font-size: 1.2em;'>"
           "<b>%g</b>"
           "</div>"
           "</div>",
           strlen(logic_log) > 0 ? logic_log : "<div>Constant value</div>",
           result);

  info.title = strdup("Calculation");
  info.extract = strdup(html_output);
  info.thumbnail_url = strdup("/static/calculation.svg");
  info.url = strdup("#");

  return info;
}