math.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import * as Commands from './../../ast/commands';
  2. import { Types } from './../../typeSystem/types';
  3. import * as Parsers 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 = async (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 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 = async (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 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 = async function (sto, _) {
  77. const x = sto.applyStore('x');
  78. const angle = x.get().mod(360);
  79. if(angle.eq(90) || angle.eq(270)) {
  80. throw ProcessorErrorFactory.undefined_tanget_value(x.get().toNumber(),
  81. this.function_call_stack.pop());
  82. }
  83. const result = Decimal.tan(convertToRadians(angle));
  84. const temp = new StoreValue(Types.REAL, result);
  85. sto.insertStore("$", temp);
  86. sto.mode = Modes.RETURN;
  87. return sto;
  88. };
  89. const block = new Commands.CommandBlock([], [new Commands.SysCall(tanFun)]);
  90. const func = new Commands.Function('$tan', Types.REAL,
  91. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  92. block);
  93. return func;
  94. }
  95. export function createSqrtFun () {
  96. const sqrtFun = async function (sto, _) {
  97. const x = sto.applyStore('x');
  98. if(x.get().isNeg()) {
  99. return Promise.reject(ProcessorErrorFactory.negative_sqrt_value(this.function_call_stack.pop()));
  100. }
  101. const result = x.get().sqrt();
  102. const temp = new StoreValue(Types.REAL, result);
  103. sto.insertStore("$", temp);
  104. sto.mode = Modes.RETURN;
  105. return sto;
  106. };
  107. const block = new Commands.CommandBlock([], [new Commands.SysCall(sqrtFun)]);
  108. const func = new Commands.Function('$sqrt', Types.REAL,
  109. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  110. block);
  111. return func;
  112. }
  113. export function createPowFun () {
  114. const powFun = async (sto, _) => {
  115. const x = sto.applyStore('x');
  116. const y = sto.applyStore('y');
  117. const result = x.get().pow(y.get());
  118. const temp = new StoreValue(Types.REAL, result);
  119. sto.insertStore("$", temp);
  120. sto.mode = Modes.RETURN;
  121. return sto;
  122. };
  123. const block = new Commands.CommandBlock([], [new Commands.SysCall(powFun)]);
  124. const func = new Commands.Function('$pow', Types.REAL,
  125. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false),
  126. new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'y', false)],
  127. block);
  128. return func;
  129. }
  130. export function createLogFun () {
  131. const logFun = async function (sto, _) {
  132. const x = sto.applyStore('x');
  133. if (x.get().isNegative()) {
  134. return Promise.reject(ProcessorErrorFactory.negative_log_value(this.function_call_stack.pop()));
  135. }
  136. const result = Decimal.log10(x.get());
  137. const temp = new StoreValue(Types.REAL, result);
  138. sto.insertStore("$", temp);
  139. sto.mode = Modes.RETURN;
  140. return sto;
  141. };
  142. const block = new Commands.CommandBlock([], [new Commands.SysCall(logFun)]);
  143. const func = new Commands.Function('$log', Types.REAL,
  144. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  145. block);
  146. return func;
  147. }
  148. export function createAbsFun () {
  149. const absFun = async (sto, _) => {
  150. const x = sto.applyStore('x');
  151. const result = x.get().abs();
  152. const temp = new StoreValue(x.type, result);
  153. sto.insertStore("$", temp);
  154. sto.mode = Modes.RETURN;
  155. return sto;
  156. };
  157. const block = new Commands.CommandBlock([], [new Commands.SysCall(absFun)]);
  158. const func = new Commands.Function('$abs', new MultiType([Types.INTEGER, Types.REAL]),
  159. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  160. block);
  161. return func;
  162. }
  163. export function createNegateFun () {
  164. const negateFun = async (sto, _) => {
  165. const x = sto.applyStore('x');
  166. const result = x.get().negated();
  167. const temp = new StoreValue(x.type, result);
  168. sto.insertStore("$", temp);
  169. sto.mode = Modes.RETURN;
  170. return sto;
  171. };
  172. const block = new Commands.CommandBlock([], [new Commands.SysCall(negateFun)]);
  173. const func = new Commands.Function('$negate', new MultiType([Types.INTEGER, Types.REAL]),
  174. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  175. block);
  176. return func;
  177. }
  178. export function createInvertFun () {
  179. const invertFun = (sto, _) => {
  180. const x = sto.applyStore('x');
  181. const result = Parsers.toReal(1).dividedBy(x.get());
  182. const temp = new StoreValue(Types.REAL, result);
  183. sto.insertStore("$", temp);
  184. sto.mode = Modes.RETURN;
  185. return sto;
  186. };
  187. const block = new Commands.CommandBlock([], [new Commands.SysCall(invertFun)]);
  188. const func = new Commands.Function('$invert', Types.REAL,
  189. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.REAL]), 'x', false)],
  190. block);
  191. return func;
  192. }
  193. export function createMaxFun () {
  194. const maxFun = async (sto, _) => {
  195. const x = sto.applyStore('x');
  196. const numbers = x.get().map(sto_addrs => sto_addrs.get());
  197. const result = Decimal.max(...numbers);
  198. const temp = new StoreValue(x.type.innerType, result);
  199. sto.insertStore("$", temp);
  200. sto.mode = Modes.RETURN;
  201. return sto;
  202. };
  203. const paramType = new ArrayType(new MultiType([Types.INTEGER, Types.REAL]), 1);
  204. const block = new Commands.CommandBlock([], [new Commands.SysCall(maxFun)]);
  205. const func = new Commands.Function('$max', new MultiType([Types.INTEGER, Types.REAL]),
  206. [new Commands.FormalParameter(paramType, 'x', false)],
  207. block);
  208. return func;
  209. }
  210. export function createMinFun () {
  211. const minFun = async (sto, _) => {
  212. const x = sto.applyStore('x');
  213. const numbers = x.get().map(sto_addrs => sto_addrs.get());
  214. const result = Decimal.min(...numbers);
  215. const temp = new StoreValue(x.type.innerType, result);
  216. sto.insertStore("$", temp);
  217. sto.mode = Modes.RETURN;
  218. return sto;
  219. };
  220. const paramType = new ArrayType(new MultiType([Types.INTEGER, Types.REAL]), 1);
  221. const block = new Commands.CommandBlock([], [new Commands.SysCall(minFun)]);
  222. const func = new Commands.Function('$min', new MultiType([Types.INTEGER, Types.REAL]),
  223. [new Commands.FormalParameter(paramType, 'x', false)],
  224. block);
  225. return func;
  226. }
  227. export function createRandFun () {
  228. const randFun = (sto, _) => {
  229. const val = Math.random();
  230. const temp = new StoreValue(Types.REAL, new Decimal(val));
  231. sto.insertStore("$", temp);
  232. sto.mode = Modes.RETURN;
  233. return Promise.resolve(sto);
  234. };
  235. const block = new Commands.CommandBlock([], [new Commands.SysCall(randFun)]);
  236. const func = new Commands.Function('$rand', Types.REAL, [], block);
  237. return func;
  238. }