You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
5.1 KiB

  1. #include <string>
  2. #include <iostream>
  3. #include <regex>
  4. #include <string>
  5. #include <iostream>
  6. #include <stdexcept>
  7. #include <sstream>
  8. #include <stdio.h> /* printf, NULL */
  9. #include <stdlib.h>
  10. #include <cassert>
  11. #include "fr.h"
  12. typedef void (*Func1)(PFrElement, PFrElement);
  13. typedef void (*Func2)(PFrElement, PFrElement, PFrElement);
  14. typedef void *FuncAny;
  15. typedef struct {
  16. FuncAny fn;
  17. int nOps;
  18. } FunctionSpec;
  19. std::map<std::string, FunctionSpec> functions;
  20. std::vector<FrElement> stack;
  21. void addFunction(std::string name, FuncAny f, int nOps) {
  22. FunctionSpec fs;
  23. fs.fn = f;
  24. fs.nOps = nOps;
  25. functions[name] = fs;
  26. }
  27. void fillMap() {
  28. addFunction("add", (FuncAny)Fr_add, 2);
  29. addFunction("sub", (FuncAny)Fr_sub, 2);
  30. addFunction("neg", (FuncAny)Fr_neg, 1);
  31. addFunction("mul", (FuncAny)Fr_mul, 2);
  32. addFunction("square", (FuncAny)Fr_square, 1);
  33. addFunction("idiv", (FuncAny)Fr_idiv, 2);
  34. addFunction("inv", (FuncAny)Fr_inv, 1);
  35. addFunction("div", (FuncAny)Fr_div, 2);
  36. addFunction("band", (FuncAny)Fr_band, 2);
  37. addFunction("bor", (FuncAny)Fr_bor, 2);
  38. addFunction("bxor", (FuncAny)Fr_bxor, 2);
  39. addFunction("bnot", (FuncAny)Fr_bnot, 1);
  40. addFunction("eq", (FuncAny)Fr_eq, 2);
  41. addFunction("neq", (FuncAny)Fr_neq, 2);
  42. addFunction("lt", (FuncAny)Fr_lt, 2);
  43. addFunction("gt", (FuncAny)Fr_gt, 2);
  44. addFunction("leq", (FuncAny)Fr_leq, 2);
  45. addFunction("geq", (FuncAny)Fr_geq, 2);
  46. addFunction("land", (FuncAny)Fr_land, 2);
  47. addFunction("lor", (FuncAny)Fr_lor, 2);
  48. addFunction("lnot", (FuncAny)Fr_lnot, 1);
  49. }
  50. u_int64_t readInt(std::string &s) {
  51. if (s.rfind("0x", 0) == 0) {
  52. return std::stoull(s.substr(2), 0, 16);
  53. } else {
  54. return std::stoull(s, 0, 10);
  55. }
  56. }
  57. void pushNumber(std::vector<std::string> &v) {
  58. u_int64_t a;
  59. if ((v.size()<1) || (v.size() > (Fr_N64+1))) {
  60. printf("Invalid Size: %d - %d \n", v.size(), Fr_N64);
  61. throw std::runtime_error("Invalid number of parameters for number");
  62. }
  63. FrElement e;
  64. a = readInt(v[0]);
  65. *(u_int64_t *)(&e) = a;
  66. for (int i=0; i<Fr_N64; i++) {
  67. if (i+1 < v.size()) {
  68. a = readInt(v[i+1]);
  69. } else {
  70. a = 0;
  71. }
  72. e.longVal[i] = a;
  73. }
  74. stack.push_back(e);
  75. }
  76. void callFunction(FunctionSpec fs) {
  77. if (stack.size() < fs.nOps) {
  78. throw new std::runtime_error("Not enough elements in stack");
  79. }
  80. if (fs.nOps == 1) {
  81. FrElement a = stack.back();
  82. stack.pop_back();
  83. FrElement c;
  84. (*(Func1)fs.fn)(&c, &a);
  85. stack.push_back(c);
  86. } else if (fs.nOps == 2) {
  87. FrElement b = stack.back();
  88. stack.pop_back();
  89. FrElement a = stack.back();
  90. stack.pop_back();
  91. FrElement c;
  92. (*(Func2)fs.fn)(&c, &a, &b);
  93. stack.push_back(c);
  94. } else {
  95. assert(false);
  96. }
  97. }
  98. void processLine(std::string &line) {
  99. std::regex re("(\\s*[,;]\\s*)|\\s+"); // whitespace
  100. std::sregex_token_iterator begin( line.begin(), line.end(), re ,-1);
  101. std::sregex_token_iterator end;
  102. std::vector<std::string> tokens;
  103. std::copy(begin, end, std::back_inserter(tokens));
  104. // Remove initial empty tokens
  105. while ((tokens.size() > 0)&&(tokens[0] == "")) {
  106. tokens.erase(tokens.begin());
  107. }
  108. // Empty lines are valid but are not processed
  109. if (tokens.size() == 0) return;
  110. auto search = functions.find(tokens[0]);
  111. if (search == functions.end()) {
  112. pushNumber(tokens);
  113. } else {
  114. if (tokens.size() != 1) {
  115. throw std::runtime_error("Functions does not accept parameters");
  116. }
  117. callFunction(search->second);
  118. }
  119. }
  120. int main(void)
  121. {
  122. fillMap();
  123. std::string line;
  124. int i=0;
  125. while (std::getline(std::cin, line)) {
  126. processLine(line);
  127. // if (i%1000 == 0) printf("%d\n", i);
  128. // printf("%d\n", i);
  129. i++;
  130. }
  131. // Print the elements in the stack
  132. //
  133. for (int i=0; i<stack.size(); i++) {
  134. char *s;
  135. s = Fr_element2str(&stack[i]);
  136. printf("%s\n", s);
  137. free(s);
  138. }
  139. return EXIT_SUCCESS;
  140. }
  141. /*
  142. #include <stdlib.h>
  143. #include <string.h>
  144. #include "fr.h"
  145. typedef void (*Func2)(PFrElement, PFrElement, PFrElement);
  146. typedef struct {
  147. const char *fnName;
  148. Func2 fn;
  149. } FN;
  150. #define NFN 2
  151. FN fns[NFN] = {
  152. {"add", Fr_add},
  153. {"mul", Fr_mul},
  154. };
  155. int main(int argc, char **argv) {
  156. if (argc <= 1) {
  157. fprintf( stderr, "invalid number of parameters");
  158. return 1;
  159. }
  160. for (int i=0; i< NFN;i++) {
  161. if (strcmp(argv[1], fns[i].fnName) == 0) {
  162. if (argc != 4) {
  163. fprintf( stderr, "invalid number of parameters");
  164. return 1;
  165. }
  166. FrElement a;
  167. FrElement b;
  168. Fr_str2element(&a, argv[2]);
  169. Fr_str2element(&b, argv[3]);
  170. FrElement c;
  171. fns[i].fn(&c, &a, &b);
  172. char *s;
  173. s = Fr_element2str(&c);
  174. printf("%s", s);
  175. free(s);
  176. return 0;
  177. }
  178. }
  179. fprintf( stderr, "invalid operation %s", argv[1]);
  180. return 1;
  181. }
  182. */