1
0

math.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import * as Commands from './../../ast/commands';
  2. import { Types } from './../../typeSystem/types';
  3. import { toReal } from "./../../typeSystem/parsers";
  4. import { Decimal } from 'decimal.js';
  5. import { MultiType } from '../../typeSystem/multiType';
  6. import { ArrayType } from '../../typeSystem/array_type';
  7. import { Modes } from '../modes';
  8. import { StoreValue } from '../store/value/store_value';
  9. import { ProcessorErrorFactory } from '../error/processorErrorFactory';
  10. /**
  11. * sin
  12. * cos
  13. * tan
  14. * sqrt
  15. * pow
  16. * log
  17. * abs
  18. * negate
  19. * invert
  20. * max
  21. * min
  22. */
  23. function convertToRadians (degrees) {
  24. return degrees.times(Decimal.acos(-1)).div(180);
  25. }
  26. export function createSinFun () {
  27. const sinFun = (sto, _) => {
  28. const x = sto.applyStore('x');
  29. const angle = x.get().mod(360);
  30. let result = null;
  31. if(angle.eq(90)) {
  32. result = new Decimal(1);
  33. } else if (angle.eq(180)) {
  34. result = new Decimal(0);
  35. } else if (angle.eq(270)) {
  36. result = new Decimal(-1);
  37. } else {
  38. result = Decimal.sin(convertToRadians(angle));
  39. }
  40. const temp = new StoreValue(Types.REAL, result);
  41. sto.insertStore("$", temp);
  42. sto.mode = Modes.RETURN;
  43. return Promise.resolve(sto);
  44. };
  45. const block = new Commands.CommandBlock([], [new Commands.SysCall(sinFun)]);
  46. const func = new Commands.Function('$sin', Types.REAL,
  47. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  48. block);
  49. return func;
  50. }
  51. export function createCosFun () {
  52. const cosFun = (sto, _) => {
  53. const x = sto.applyStore('x');
  54. const angle = x.get().mod(360);
  55. let result = null;
  56. if(angle.eq(90)) {
  57. result = new Decimal(0);
  58. } else if (angle.eq(180)) {
  59. result = new Decimal(-1);
  60. } else if (angle.eq(270)) {
  61. result = new Decimal(0)
  62. }
  63. result = Decimal.cos(convertToRadians(angle));
  64. const temp = new StoreValue(Types.REAL, result);
  65. sto.insertStore("$", temp);
  66. sto.mode = Modes.RETURN;
  67. return Promise.resolve(sto);
  68. };
  69. const block = new Commands.CommandBlock([], [new Commands.SysCall(cosFun)]);
  70. const func = new Commands.Function('$cos', Types.REAL,
  71. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  72. block);
  73. return func;
  74. }
  75. export function createTanFun () {
  76. const tanFun = function (sto, _) {
  77. const x = sto.applyStore('x');
  78. const angle = x.get().mod(360);
  79. if(angle.eq(90) || angle.eq(270)) {
  80. return Promise.reject(ProcessorErrorFactory.undefined_tanget_value(x.get().toNumber(), this.function_call_stack.pop()));
  81. }
  82. const result = Decimal.tan(convertToRadians(angle));
  83. const temp = new StoreValue(Types.REAL, result);
  84. sto.insertStore("$", temp);
  85. sto.mode = Modes.RETURN;
  86. return Promise.resolve(sto);
  87. };
  88. const block = new Commands.CommandBlock([], [new Commands.SysCall(tanFun)]);
  89. const func = new Commands.Function('$tan', Types.REAL,
  90. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  91. block);
  92. return func;
  93. }
  94. export function createSqrtFun () {
  95. const sqrtFun = function (sto, _) {
  96. const x = sto.applyStore('x');
  97. if(x.get().isNeg()) {
  98. return Promise.reject(ProcessorErrorFactory.negative_sqrt_value(this.function_call_stack.pop()));
  99. }
  100. const result = x.get().sqrt();
  101. const temp = new StoreValue(Types.REAL, result);
  102. sto.insertStore("$", temp);
  103. sto.mode = Modes.RETURN;
  104. return Promise.resolve(sto);
  105. };
  106. const block = new Commands.CommandBlock([], [new Commands.SysCall(sqrtFun)]);
  107. const func = new Commands.Function('$sqrt', Types.REAL,
  108. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  109. block);
  110. return func;
  111. }
  112. export function createPowFun () {
  113. const powFun = (sto, _) => {
  114. const x = sto.applyStore('x');
  115. const y = sto.applyStore('y');
  116. const result = x.get().pow(y.get());
  117. const temp = new StoreValue(Types.REAL, result);
  118. sto.insertStore("$", temp);
  119. sto.mode = Modes.RETURN;
  120. return Promise.resolve(sto);
  121. };
  122. const block = new Commands.CommandBlock([], [new Commands.SysCall(powFun)]);
  123. const func = new Commands.Function('$pow', Types.REAL,
  124. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false),
  125. new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'y', false)],
  126. block);
  127. return func;
  128. }
  129. export function createLogFun () {
  130. const logFun = function (sto, _) {
  131. const x = sto.applyStore('x');
  132. if (x.get().isNegative()) {
  133. return Promise.reject(ProcessorErrorFactory.negative_log_value(this.function_call_stack.pop()));
  134. }
  135. const result = Decimal.log10(x.get());
  136. const temp = new StoreValue(Types.REAL, result);
  137. sto.insertStore("$", temp);
  138. sto.mode = Modes.RETURN;
  139. return Promise.resolve(sto);
  140. };
  141. const block = new Commands.CommandBlock([], [new Commands.SysCall(logFun)]);
  142. const func = new Commands.Function('$log', Types.REAL,
  143. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  144. block);
  145. return func;
  146. }
  147. export function createAbsFun () {
  148. const absFun = (sto, _) => {
  149. const x = sto.applyStore('x');
  150. const result = x.get().abs();
  151. const temp = new StoreValue(x.type, result);
  152. sto.insertStore("$", temp);
  153. sto.mode = Modes.RETURN;
  154. return Promise.resolve(sto);
  155. };
  156. const block = new Commands.CommandBlock([], [new Commands.SysCall(absFun)]);
  157. const func = new Commands.Function('$abs', new MultiType([Types.INTEGER, Types.REAL]),
  158. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  159. block);
  160. return func;
  161. }
  162. export function createNegateFun () {
  163. const negateFun = (sto, _) => {
  164. const x = sto.applyStore('x');
  165. const result = x.get().negated();
  166. const temp = new StoreValue(x.type, result);
  167. sto.insertStore("$", temp);
  168. sto.mode = Modes.RETURN;
  169. return Promise.resolve(sto);
  170. };
  171. const block = new Commands.CommandBlock([], [new Commands.SysCall(negateFun)]);
  172. const func = new Commands.Function('$negate', new MultiType([Types.INTEGER, Types.REAL]),
  173. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  174. block);
  175. return func;
  176. }
  177. export function createInvertFun () {
  178. const invertFun = (sto, _) => {
  179. const x = sto.applyStore('x');
  180. const result = toReal(1).dividedBy(x.get());
  181. const temp = new StoreValue(Types.REAL, result);
  182. sto.insertStore("$", temp);
  183. sto.mode = Modes.RETURN;
  184. return Promise.resolve(sto);
  185. };
  186. const block = new Commands.CommandBlock([], [new Commands.SysCall(invertFun)]);
  187. const func = new Commands.Function('$invert', Types.REAL,
  188. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  189. block);
  190. return func;
  191. }
  192. export function createMaxFun () {
  193. const maxFun = (sto, _) => {
  194. const x = sto.applyStore('x');
  195. const numbers = x.get().map(sto_addrs => sto_addrs.get());
  196. const result = Decimal.max(...numbers);
  197. const temp = new StoreValue(x.type.innerType, result);
  198. sto.insertStore("$", temp);
  199. sto.mode = Modes.RETURN;
  200. return Promise.resolve(sto);
  201. };
  202. const paramType = new ArrayType(new MultiType([Types.INTEGER, Types.REAL]), 1);
  203. const block = new Commands.CommandBlock([], [new Commands.SysCall(maxFun)]);
  204. const func = new Commands.Function('$max', new MultiType([Types.INTEGER, Types.REAL]),
  205. [new Commands.FormalParameter(paramType, 'x', false)],
  206. block);
  207. return func;
  208. }
  209. export function createMinFun () {
  210. const minFun = (sto, _) => {
  211. const x = sto.applyStore('x');
  212. const numbers = x.get().map(sto_addrs => sto_addrs.get());
  213. const result = Decimal.min(...numbers);
  214. const temp = new StoreValue(x.type.innerType, result);
  215. sto.insertStore("$", temp);
  216. sto.mode = Modes.RETURN;
  217. return Promise.resolve(sto);
  218. };
  219. const paramType = new ArrayType(new MultiType([Types.INTEGER, Types.REAL]), 1);
  220. const block = new Commands.CommandBlock([], [new Commands.SysCall(minFun)]);
  221. const func = new Commands.Function('$min', new MultiType([Types.INTEGER, Types.REAL]),
  222. [new Commands.FormalParameter(paramType, 'x', false)],
  223. block);
  224. return func;
  225. }
  226. export function createRandFun () {
  227. const randFun = (sto, _) => {
  228. const val = Math.random();
  229. const temp = new StoreValue(Types.REAL, new Decimal(val));
  230. sto.insertStore("$", temp);
  231. sto.mode = Modes.RETURN;
  232. return Promise.resolve(sto);
  233. };
  234. const block = new Commands.CommandBlock([], [new Commands.SysCall(randFun)]);
  235. const func = new Commands.Function('$rand', Types.REAL, [], block);
  236. return func;
  237. }