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.

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