greybel-interpreter 2.2.1 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,8 @@ import { OperationContext } from '../context';
3
3
  import { CustomValue } from '../types/base';
4
4
  import { CustomBoolean } from '../types/boolean';
5
5
  import { CustomList } from '../types/list';
6
+ import { CustomNumber } from '../types/number';
7
+ import { CustomString } from '../types/string';
6
8
  import { CPSVisit, Operation } from './operation';
7
9
  export interface ProcessorHandlerFunction {
8
10
  (op: string, a: CustomValue, b: CustomValue): CustomValue;
@@ -12,11 +14,12 @@ export interface ProcessorHandler {
12
14
  }
13
15
  export declare const GenericProcessorHandler: ProcessorHandler;
14
16
  export declare const NumberProcessorHandler: ProcessorHandler;
15
- export declare const multiplyString: (a: CustomValue, b: CustomValue) => CustomValue;
16
- export declare const minusString: (a: CustomValue, b: CustomValue) => CustomValue;
17
+ export declare const minusString: (a: CustomString, b: CustomValue) => CustomValue;
18
+ export declare const multiplyString: (a: CustomString, b: CustomValue) => CustomValue;
19
+ export declare const divideString: (a: CustomString, b: CustomValue) => CustomValue;
17
20
  export declare const StringProcessorHandler: ProcessorHandler;
18
- export declare const multiplyList: (a: CustomList, b: CustomValue) => CustomValue;
19
- export declare const divideList: (a: CustomList, b: CustomValue) => CustomValue;
21
+ export declare const multiplyList: (a: CustomList, b: CustomNumber) => CustomValue;
22
+ export declare const divideList: (a: CustomList, b: CustomNumber) => CustomValue;
20
23
  export declare const ListProcessorHandler: ProcessorHandler;
21
24
  export declare const MapProcessorHandler: ProcessorHandler;
22
25
  export declare const InterfaceProcessorHandler: ProcessorHandler;
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Evaluate = exports.handle = exports.handleFunction = exports.handleNil = exports.handleInterface = exports.handleMap = exports.handleList = exports.handleString = exports.handleNumber = exports.FunctionProcessorHandler = exports.NilProcessorHandler = exports.InterfaceProcessorHandler = exports.MapProcessorHandler = exports.ListProcessorHandler = exports.divideList = exports.multiplyList = exports.StringProcessorHandler = exports.minusString = exports.multiplyString = exports.NumberProcessorHandler = exports.GenericProcessorHandler = void 0;
12
+ exports.Evaluate = exports.handle = exports.handleFunction = exports.handleNil = exports.handleInterface = exports.handleMap = exports.handleList = exports.handleString = exports.handleNumber = exports.FunctionProcessorHandler = exports.NilProcessorHandler = exports.InterfaceProcessorHandler = exports.MapProcessorHandler = exports.ListProcessorHandler = exports.divideList = exports.multiplyList = exports.StringProcessorHandler = exports.divideString = exports.multiplyString = exports.minusString = exports.NumberProcessorHandler = exports.GenericProcessorHandler = void 0;
13
13
  const greyscript_core_1 = require("greyscript-core");
14
14
  const boolean_1 = require("../types/boolean");
15
15
  const default_1 = require("../types/default");
@@ -45,11 +45,6 @@ exports.NumberProcessorHandler = {
45
45
  [greyscript_core_1.Operator.LessThanOrEqual]: (a, b) => new boolean_1.CustomBoolean(a.toNumber() <= b.toNumber()),
46
46
  [greyscript_core_1.Operator.NotEqual]: (a, b) => new boolean_1.CustomBoolean(a.toNumber() !== b.toNumber())
47
47
  };
48
- const multiplyString = (a, b) => {
49
- const multiStr = new Array(b.toNumber()).fill(a.toString()).join('');
50
- return new string_1.CustomString(multiStr);
51
- };
52
- exports.multiplyString = multiplyString;
53
48
  const minusString = (a, b) => {
54
49
  const origin = a.toString();
55
50
  const toRemove = b.toString();
@@ -59,10 +54,37 @@ const minusString = (a, b) => {
59
54
  return new string_1.CustomString(origin);
60
55
  };
61
56
  exports.minusString = minusString;
57
+ const multiplyString = (a, b) => {
58
+ const factor = b.toNumber();
59
+ if (factor <= 0) {
60
+ return new list_1.CustomList();
61
+ }
62
+ let newString = '';
63
+ const max = Math.floor(a.value.length * factor);
64
+ for (let index = 0; index < max; index++) {
65
+ newString += a.value[index % a.value.length];
66
+ }
67
+ return new string_1.CustomString(newString);
68
+ };
69
+ exports.multiplyString = multiplyString;
70
+ const divideString = (a, b) => {
71
+ const factor = 1 / b.toNumber();
72
+ if (factor <= 0) {
73
+ return new list_1.CustomList();
74
+ }
75
+ let newString = '';
76
+ const max = Math.floor(a.value.length * factor);
77
+ for (let index = 0; index < max; index++) {
78
+ newString += a.value[index % a.value.length];
79
+ }
80
+ return new string_1.CustomString(newString);
81
+ };
82
+ exports.divideString = divideString;
62
83
  exports.StringProcessorHandler = {
63
84
  [greyscript_core_1.Operator.Plus]: (a, b) => new string_1.CustomString(a.toString() + b.toString()),
64
85
  [greyscript_core_1.Operator.Minus]: (a, b) => (0, exports.minusString)(a, b),
65
86
  [greyscript_core_1.Operator.Asterik]: (a, b) => (0, exports.multiplyString)(a, b),
87
+ [greyscript_core_1.Operator.Slash]: (a, b) => (0, exports.divideString)(a, b),
66
88
  [greyscript_core_1.Operator.LessThan]: (a, b) => new boolean_1.CustomBoolean(a.toString() < b.toString()),
67
89
  [greyscript_core_1.Operator.GreaterThan]: (a, b) => new boolean_1.CustomBoolean(a.toString() > b.toString()),
68
90
  [greyscript_core_1.Operator.GreaterThanOrEqual]: (a, b) => new boolean_1.CustomBoolean(a.toString() >= b.toString()),
@@ -101,7 +123,7 @@ exports.ListProcessorHandler = {
101
123
  if (right instanceof list_1.CustomList) {
102
124
  return left.fork().extend(right);
103
125
  }
104
- return left;
126
+ return default_1.DefaultType.Void;
105
127
  },
106
128
  [greyscript_core_1.Operator.Equal]: (left, right) => {
107
129
  if (right instanceof list_1.CustomList) {
@@ -115,15 +137,25 @@ exports.ListProcessorHandler = {
115
137
  }
116
138
  return default_1.DefaultType.Void;
117
139
  },
118
- [greyscript_core_1.Operator.Asterik]: (left, right) => (0, exports.multiplyList)(left, right),
119
- [greyscript_core_1.Operator.Slash]: (left, right) => (0, exports.divideList)(left, right)
140
+ [greyscript_core_1.Operator.Asterik]: (left, right) => {
141
+ if (right instanceof number_1.CustomNumber) {
142
+ return (0, exports.multiplyList)(left, right);
143
+ }
144
+ return default_1.DefaultType.Void;
145
+ },
146
+ [greyscript_core_1.Operator.Slash]: (left, right) => {
147
+ if (right instanceof number_1.CustomNumber) {
148
+ return (0, exports.divideList)(left, right);
149
+ }
150
+ return default_1.DefaultType.Void;
151
+ }
120
152
  };
121
153
  exports.MapProcessorHandler = {
122
154
  [greyscript_core_1.Operator.Plus]: (left, right) => {
123
155
  if (right instanceof map_1.CustomMap) {
124
156
  return left.fork().extend(right);
125
157
  }
126
- return left;
158
+ return default_1.DefaultType.Void;
127
159
  },
128
160
  [greyscript_core_1.Operator.Equal]: (left, right) => {
129
161
  if (right instanceof map_1.CustomMap) {
@@ -143,6 +175,12 @@ exports.InterfaceProcessorHandler = {
143
175
  [greyscript_core_1.Operator.NotEqual]: (left, right) => new boolean_1.CustomBoolean(left.value !== right.value)
144
176
  };
145
177
  exports.NilProcessorHandler = {
178
+ [greyscript_core_1.Operator.Plus]: () => default_1.DefaultType.Void,
179
+ [greyscript_core_1.Operator.Minus]: () => default_1.DefaultType.Void,
180
+ [greyscript_core_1.Operator.Slash]: () => default_1.DefaultType.Void,
181
+ [greyscript_core_1.Operator.Asterik]: () => default_1.DefaultType.Void,
182
+ [greyscript_core_1.Operator.Power]: () => default_1.DefaultType.Void,
183
+ [greyscript_core_1.Operator.PercentSign]: () => default_1.DefaultType.Void,
146
184
  [greyscript_core_1.Operator.Equal]: (_a, b) => new boolean_1.CustomBoolean(b instanceof nil_1.CustomNil),
147
185
  [greyscript_core_1.Operator.NotEqual]: (_a, b) => new boolean_1.CustomBoolean(!(b instanceof nil_1.CustomNil))
148
186
  };
@@ -151,9 +189,8 @@ exports.FunctionProcessorHandler = {
151
189
  [greyscript_core_1.Operator.NotEqual]: (a, b) => new boolean_1.CustomBoolean(a !== b)
152
190
  };
153
191
  const handleNumber = (op, a, b) => {
154
- if (b instanceof nil_1.CustomNil) {
192
+ if (b instanceof nil_1.CustomNil)
155
193
  b = new number_1.CustomNumber(0);
156
- }
157
194
  if (op in exports.NumberProcessorHandler && b instanceof number_1.CustomNumber) {
158
195
  return exports.NumberProcessorHandler[op](a, b);
159
196
  }
@@ -164,6 +201,10 @@ const handleNumber = (op, a, b) => {
164
201
  };
165
202
  exports.handleNumber = handleNumber;
166
203
  const handleString = (op, a, b) => {
204
+ if (a instanceof nil_1.CustomNil)
205
+ a = new string_1.CustomString('');
206
+ if (b instanceof nil_1.CustomNil)
207
+ b = new string_1.CustomString('');
167
208
  if (op in exports.StringProcessorHandler) {
168
209
  return exports.StringProcessorHandler[op](a, b);
169
210
  }
@@ -174,10 +215,8 @@ const handleString = (op, a, b) => {
174
215
  };
175
216
  exports.handleString = handleString;
176
217
  const handleList = (op, a, b) => {
177
- const left = a;
178
- const right = b;
179
218
  if (op in exports.ListProcessorHandler) {
180
- return exports.ListProcessorHandler[op](left, right);
219
+ return exports.ListProcessorHandler[op](a, b);
181
220
  }
182
221
  else if (op in exports.GenericProcessorHandler) {
183
222
  return exports.GenericProcessorHandler[op](a, b);
@@ -186,10 +225,8 @@ const handleList = (op, a, b) => {
186
225
  };
187
226
  exports.handleList = handleList;
188
227
  const handleMap = (op, a, b) => {
189
- const left = a;
190
- const right = b;
191
228
  if (op in exports.MapProcessorHandler) {
192
- return exports.MapProcessorHandler[op](left, right);
229
+ return exports.MapProcessorHandler[op](a, b);
193
230
  }
194
231
  else if (op in exports.GenericProcessorHandler) {
195
232
  return exports.GenericProcessorHandler[op](a, b);
@@ -198,10 +235,8 @@ const handleMap = (op, a, b) => {
198
235
  };
199
236
  exports.handleMap = handleMap;
200
237
  const handleInterface = (op, a, b) => {
201
- const left = a;
202
- const right = b;
203
238
  if (op in exports.InterfaceProcessorHandler) {
204
- return exports.InterfaceProcessorHandler[op](left, right);
239
+ return exports.InterfaceProcessorHandler[op](a, b);
205
240
  }
206
241
  else if (op in exports.GenericProcessorHandler) {
207
242
  return exports.GenericProcessorHandler[op](a, b);
@@ -245,7 +280,7 @@ const handle = (op, a, b) => {
245
280
  }
246
281
  if ((a instanceof string_1.CustomString || b instanceof string_1.CustomString) &&
247
282
  op === greyscript_core_1.Operator.Plus) {
248
- return exports.StringProcessorHandler[op](a, b);
283
+ return (0, exports.handleString)(op, a, b);
249
284
  }
250
285
  else if (a instanceof number_1.CustomNumber) {
251
286
  return (0, exports.handleNumber)(op, a, b);
package/dist/types/nil.js CHANGED
@@ -14,7 +14,7 @@ class CustomNil extends base_1.CustomValue {
14
14
  return this.toString();
15
15
  }
16
16
  toString() {
17
- return '';
17
+ return 'null';
18
18
  }
19
19
  fork() {
20
20
  return new CustomNil();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "2.2.1",
3
+ "version": "2.2.3",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",