greybel-interpreter 2.2.0 → 2.2.2

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,7 @@ 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 { CustomString } from '../types/string';
6
7
  import { CPSVisit, Operation } from './operation';
7
8
  export interface ProcessorHandlerFunction {
8
9
  (op: string, a: CustomValue, b: CustomValue): CustomValue;
@@ -12,8 +13,9 @@ export interface ProcessorHandler {
12
13
  }
13
14
  export declare const GenericProcessorHandler: ProcessorHandler;
14
15
  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;
16
+ export declare const minusString: (a: CustomString, b: CustomValue) => CustomValue;
17
+ export declare const multiplyString: (a: CustomString, b: CustomValue) => CustomValue;
18
+ export declare const divideString: (a: CustomString, b: CustomValue) => CustomValue;
17
19
  export declare const StringProcessorHandler: ProcessorHandler;
18
20
  export declare const multiplyList: (a: CustomList, b: CustomValue) => CustomValue;
19
21
  export declare const divideList: (a: CustomList, b: CustomValue) => CustomValue;
@@ -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,15 +54,42 @@ 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),
66
- [greyscript_core_1.Operator.LessThan]: (a, b) => new boolean_1.CustomBoolean(a.toString().length < b.toString().length),
67
- [greyscript_core_1.Operator.GreaterThan]: (a, b) => new boolean_1.CustomBoolean(a.toString().length > b.toString().length),
68
- [greyscript_core_1.Operator.GreaterThanOrEqual]: (a, b) => new boolean_1.CustomBoolean(a.toString().length >= b.toString().length),
87
+ [greyscript_core_1.Operator.Slash]: (a, b) => (0, exports.divideString)(a, b),
88
+ [greyscript_core_1.Operator.LessThan]: (a, b) => new boolean_1.CustomBoolean(a.toString() < b.toString()),
89
+ [greyscript_core_1.Operator.GreaterThan]: (a, b) => new boolean_1.CustomBoolean(a.toString() > b.toString()),
90
+ [greyscript_core_1.Operator.GreaterThanOrEqual]: (a, b) => new boolean_1.CustomBoolean(a.toString() >= b.toString()),
69
91
  [greyscript_core_1.Operator.Equal]: (a, b) => new boolean_1.CustomBoolean(a.toString() === b.toString()),
70
- [greyscript_core_1.Operator.LessThanOrEqual]: (a, b) => new boolean_1.CustomBoolean(a.toString().length <= b.toString().length),
92
+ [greyscript_core_1.Operator.LessThanOrEqual]: (a, b) => new boolean_1.CustomBoolean(a.toString() <= b.toString()),
71
93
  [greyscript_core_1.Operator.NotEqual]: (a, b) => new boolean_1.CustomBoolean(a.toString() !== b.toString())
72
94
  };
73
95
  const multiplyList = (a, b) => {
@@ -103,36 +125,12 @@ exports.ListProcessorHandler = {
103
125
  }
104
126
  return left;
105
127
  },
106
- [greyscript_core_1.Operator.LessThan]: (left, right) => {
107
- if (right instanceof list_1.CustomList) {
108
- return new boolean_1.CustomBoolean(left.value.length < right.value.length);
109
- }
110
- return default_1.DefaultType.Void;
111
- },
112
- [greyscript_core_1.Operator.GreaterThan]: (left, right) => {
113
- if (right instanceof list_1.CustomList) {
114
- return new boolean_1.CustomBoolean(left.value.length > right.value.length);
115
- }
116
- return default_1.DefaultType.Void;
117
- },
118
- [greyscript_core_1.Operator.GreaterThanOrEqual]: (left, right) => {
119
- if (right instanceof list_1.CustomList) {
120
- return new boolean_1.CustomBoolean(left.value.length >= right.value.length);
121
- }
122
- return default_1.DefaultType.Void;
123
- },
124
128
  [greyscript_core_1.Operator.Equal]: (left, right) => {
125
129
  if (right instanceof list_1.CustomList) {
126
130
  return new boolean_1.CustomBoolean((0, deep_equal_1.deepEqual)(left, right));
127
131
  }
128
132
  return default_1.DefaultType.Void;
129
133
  },
130
- [greyscript_core_1.Operator.LessThanOrEqual]: (left, right) => {
131
- if (right instanceof list_1.CustomList) {
132
- return new boolean_1.CustomBoolean(left.value.length <= right.value.length);
133
- }
134
- return default_1.DefaultType.Void;
135
- },
136
134
  [greyscript_core_1.Operator.NotEqual]: (left, right) => {
137
135
  if (right instanceof list_1.CustomList) {
138
136
  return new boolean_1.CustomBoolean(!(0, deep_equal_1.deepEqual)(left, right));
@@ -149,36 +147,12 @@ exports.MapProcessorHandler = {
149
147
  }
150
148
  return left;
151
149
  },
152
- [greyscript_core_1.Operator.LessThan]: (left, right) => {
153
- if (right instanceof map_1.CustomMap) {
154
- return new boolean_1.CustomBoolean(left.value.size < right.value.size);
155
- }
156
- return default_1.DefaultType.Void;
157
- },
158
- [greyscript_core_1.Operator.GreaterThan]: (left, right) => {
159
- if (right instanceof map_1.CustomMap) {
160
- return new boolean_1.CustomBoolean(left.value.size > right.value.size);
161
- }
162
- return default_1.DefaultType.Void;
163
- },
164
- [greyscript_core_1.Operator.GreaterThanOrEqual]: (left, right) => {
165
- if (right instanceof map_1.CustomMap) {
166
- return new boolean_1.CustomBoolean(left.value.size >= right.value.size);
167
- }
168
- return default_1.DefaultType.Void;
169
- },
170
150
  [greyscript_core_1.Operator.Equal]: (left, right) => {
171
151
  if (right instanceof map_1.CustomMap) {
172
152
  return new boolean_1.CustomBoolean((0, deep_equal_1.deepEqual)(left, right));
173
153
  }
174
154
  return default_1.DefaultType.Void;
175
155
  },
176
- [greyscript_core_1.Operator.LessThanOrEqual]: (left, right) => {
177
- if (right instanceof map_1.CustomMap) {
178
- return new boolean_1.CustomBoolean(left.value.size <= right.value.size);
179
- }
180
- return default_1.DefaultType.Void;
181
- },
182
156
  [greyscript_core_1.Operator.NotEqual]: (left, right) => {
183
157
  if (right instanceof map_1.CustomMap) {
184
158
  return new boolean_1.CustomBoolean(!(0, deep_equal_1.deepEqual)(left, right));
@@ -199,7 +173,10 @@ exports.FunctionProcessorHandler = {
199
173
  [greyscript_core_1.Operator.NotEqual]: (a, b) => new boolean_1.CustomBoolean(a !== b)
200
174
  };
201
175
  const handleNumber = (op, a, b) => {
202
- if (op in exports.NumberProcessorHandler) {
176
+ if (b instanceof nil_1.CustomNil) {
177
+ b = new number_1.CustomNumber(0);
178
+ }
179
+ if (op in exports.NumberProcessorHandler && b instanceof number_1.CustomNumber) {
203
180
  return exports.NumberProcessorHandler[op](a, b);
204
181
  }
205
182
  else if (op in exports.GenericProcessorHandler) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",
@@ -48,8 +48,8 @@
48
48
  "typescript": "^5.0.4"
49
49
  },
50
50
  "dependencies": {
51
- "greybel-core": "^0.9.5",
52
- "greyscript-core": "^0.9.7"
51
+ "greybel-core": "^0.9.6",
52
+ "greyscript-core": "^0.9.9"
53
53
  },
54
54
  "keywords": [
55
55
  "greyscript",