littlewing 0.7.0 → 0.7.1

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.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,3 @@
1
- declare namespace exports_ast {
2
- export { unaryOp, subtract, program, number, notEquals, negate, multiply, modulo, logicalOr, logicalAnd, lessThan, lessEqual, identifier, greaterThan, greaterEqual, functionCall, exponentiate, equals, divide, conditional, binaryOp, assign, add };
3
- }
4
1
  /**
5
2
  * Runtime value type - only numbers
6
3
  */
@@ -134,6 +131,31 @@ declare function isAssignment(node: ASTNode): node is Assignment;
134
131
  declare function isProgram(node: ASTNode): node is Program;
135
132
  declare function isConditionalExpression(node: ASTNode): node is ConditionalExpression;
136
133
  /**
134
+ * Extracts input variables from an AST.
135
+ *
136
+ * Input variables are those whose values can be determined without knowing
137
+ * the values of other variables in the script. This includes:
138
+ * - Literals (10, -5, 3.14)
139
+ * - Unary minus of literals (-10)
140
+ * - Constant expressions (2 + 3, -5 * 2)
141
+ * - Function calls with constant arguments (MAX(10, 20), NOW())
142
+ *
143
+ * Computed variables (those that reference other variables) are excluded.
144
+ *
145
+ * @param ast - The AST to analyze (can be a single statement or Program node)
146
+ * @returns Array of input variable names
147
+ *
148
+ * @example
149
+ * ```typescript
150
+ * const ast = parseSource('price = 100; tax = price * 0.08')
151
+ * extractInputVariables(ast) // ['price']
152
+ * ```
153
+ */
154
+ declare function extractInputVariables(ast: ASTNode): string[];
155
+ declare namespace exports_ast {
156
+ export { unaryOp, subtract, program, number, notEquals, negate, multiply, modulo, logicalOr, logicalAnd, lessThan, lessEqual, identifier, greaterThan, greaterEqual, functionCall, exponentiate, equals, divide, conditional, binaryOp, assign, add };
157
+ }
158
+ /**
137
159
  * Builder functions for creating AST nodes manually
138
160
  */
139
161
  /**
@@ -682,4 +704,4 @@ declare class Parser {
682
704
  * @returns Parsed AST
683
705
  */
684
706
  declare function parseSource(source: string): ASTNode;
685
- export { parseSource, optimize, isUnaryOp, isProgram, isNumberLiteral, isIdentifier, isFunctionCall, isConditionalExpression, isBinaryOp, isAssignment, generate, execute, defaultContext, exports_date_utils as dateUtils, exports_ast as ast, UnaryOp, TokenType, Token, RuntimeValue, Program, Parser, Operator, NumberLiteral, Lexer, Identifier, FunctionCall, Executor, ExecutionContext, ConditionalExpression, CodeGenerator, BinaryOp, Assignment, ASTNode };
707
+ export { parseSource, optimize, isUnaryOp, isProgram, isNumberLiteral, isIdentifier, isFunctionCall, isConditionalExpression, isBinaryOp, isAssignment, generate, extractInputVariables, execute, defaultContext, exports_date_utils as dateUtils, exports_ast as ast, UnaryOp, TokenType, Token, RuntimeValue, Program, Parser, Operator, NumberLiteral, Lexer, Identifier, FunctionCall, Executor, ExecutionContext, ConditionalExpression, CodeGenerator, BinaryOp, Assignment, ASTNode };
package/dist/index.js CHANGED
@@ -10,6 +10,92 @@ var __export = (target, all) => {
10
10
  });
11
11
  };
12
12
 
13
+ // src/types.ts
14
+ var TokenType;
15
+ ((TokenType2) => {
16
+ TokenType2["NUMBER"] = "NUMBER";
17
+ TokenType2["IDENTIFIER"] = "IDENTIFIER";
18
+ TokenType2["PLUS"] = "PLUS";
19
+ TokenType2["MINUS"] = "MINUS";
20
+ TokenType2["STAR"] = "STAR";
21
+ TokenType2["SLASH"] = "SLASH";
22
+ TokenType2["PERCENT"] = "PERCENT";
23
+ TokenType2["CARET"] = "CARET";
24
+ TokenType2["DOUBLE_EQUALS"] = "DOUBLE_EQUALS";
25
+ TokenType2["NOT_EQUALS"] = "NOT_EQUALS";
26
+ TokenType2["LESS_THAN"] = "LESS_THAN";
27
+ TokenType2["GREATER_THAN"] = "GREATER_THAN";
28
+ TokenType2["LESS_EQUAL"] = "LESS_EQUAL";
29
+ TokenType2["GREATER_EQUAL"] = "GREATER_EQUAL";
30
+ TokenType2["LOGICAL_AND"] = "LOGICAL_AND";
31
+ TokenType2["LOGICAL_OR"] = "LOGICAL_OR";
32
+ TokenType2["LPAREN"] = "LPAREN";
33
+ TokenType2["RPAREN"] = "RPAREN";
34
+ TokenType2["EQUALS"] = "EQUALS";
35
+ TokenType2["COMMA"] = "COMMA";
36
+ TokenType2["QUESTION"] = "QUESTION";
37
+ TokenType2["COLON"] = "COLON";
38
+ TokenType2["EOF"] = "EOF";
39
+ })(TokenType ||= {});
40
+ function isNumberLiteral(node) {
41
+ return node.type === "NumberLiteral";
42
+ }
43
+ function isIdentifier(node) {
44
+ return node.type === "Identifier";
45
+ }
46
+ function isBinaryOp(node) {
47
+ return node.type === "BinaryOp";
48
+ }
49
+ function isUnaryOp(node) {
50
+ return node.type === "UnaryOp";
51
+ }
52
+ function isFunctionCall(node) {
53
+ return node.type === "FunctionCall";
54
+ }
55
+ function isAssignment(node) {
56
+ return node.type === "Assignment";
57
+ }
58
+ function isProgram(node) {
59
+ return node.type === "Program";
60
+ }
61
+ function isConditionalExpression(node) {
62
+ return node.type === "ConditionalExpression";
63
+ }
64
+
65
+ // src/analyzer.ts
66
+ function extractInputVariables(ast) {
67
+ const inputVars = new Set;
68
+ const statements = isProgram(ast) ? ast.statements : [ast];
69
+ for (const statement of statements) {
70
+ if (isAssignment(statement)) {
71
+ if (!containsVariableReference(statement.value)) {
72
+ inputVars.add(statement.name);
73
+ }
74
+ }
75
+ }
76
+ return Array.from(inputVars);
77
+ }
78
+ function containsVariableReference(node) {
79
+ if (isIdentifier(node)) {
80
+ return true;
81
+ }
82
+ if (isNumberLiteral(node)) {
83
+ return false;
84
+ }
85
+ if (isBinaryOp(node)) {
86
+ return containsVariableReference(node.left) || containsVariableReference(node.right);
87
+ }
88
+ if (isUnaryOp(node)) {
89
+ return containsVariableReference(node.argument);
90
+ }
91
+ if (isFunctionCall(node)) {
92
+ return node.arguments.some((arg) => containsVariableReference(arg));
93
+ }
94
+ if (isConditionalExpression(node)) {
95
+ return containsVariableReference(node.condition) || containsVariableReference(node.consequent) || containsVariableReference(node.alternate);
96
+ }
97
+ return false;
98
+ }
13
99
  // src/ast.ts
14
100
  var exports_ast = {};
15
101
  __export(exports_ast, {
@@ -137,58 +223,6 @@ function logicalAnd(left, right) {
137
223
  function logicalOr(left, right) {
138
224
  return binaryOp(left, "||", right);
139
225
  }
140
- // src/types.ts
141
- var TokenType;
142
- ((TokenType2) => {
143
- TokenType2["NUMBER"] = "NUMBER";
144
- TokenType2["IDENTIFIER"] = "IDENTIFIER";
145
- TokenType2["PLUS"] = "PLUS";
146
- TokenType2["MINUS"] = "MINUS";
147
- TokenType2["STAR"] = "STAR";
148
- TokenType2["SLASH"] = "SLASH";
149
- TokenType2["PERCENT"] = "PERCENT";
150
- TokenType2["CARET"] = "CARET";
151
- TokenType2["DOUBLE_EQUALS"] = "DOUBLE_EQUALS";
152
- TokenType2["NOT_EQUALS"] = "NOT_EQUALS";
153
- TokenType2["LESS_THAN"] = "LESS_THAN";
154
- TokenType2["GREATER_THAN"] = "GREATER_THAN";
155
- TokenType2["LESS_EQUAL"] = "LESS_EQUAL";
156
- TokenType2["GREATER_EQUAL"] = "GREATER_EQUAL";
157
- TokenType2["LOGICAL_AND"] = "LOGICAL_AND";
158
- TokenType2["LOGICAL_OR"] = "LOGICAL_OR";
159
- TokenType2["LPAREN"] = "LPAREN";
160
- TokenType2["RPAREN"] = "RPAREN";
161
- TokenType2["EQUALS"] = "EQUALS";
162
- TokenType2["COMMA"] = "COMMA";
163
- TokenType2["QUESTION"] = "QUESTION";
164
- TokenType2["COLON"] = "COLON";
165
- TokenType2["EOF"] = "EOF";
166
- })(TokenType ||= {});
167
- function isNumberLiteral(node) {
168
- return node.type === "NumberLiteral";
169
- }
170
- function isIdentifier(node) {
171
- return node.type === "Identifier";
172
- }
173
- function isBinaryOp(node) {
174
- return node.type === "BinaryOp";
175
- }
176
- function isUnaryOp(node) {
177
- return node.type === "UnaryOp";
178
- }
179
- function isFunctionCall(node) {
180
- return node.type === "FunctionCall";
181
- }
182
- function isAssignment(node) {
183
- return node.type === "Assignment";
184
- }
185
- function isProgram(node) {
186
- return node.type === "Program";
187
- }
188
- function isConditionalExpression(node) {
189
- return node.type === "ConditionalExpression";
190
- }
191
-
192
226
  // src/utils.ts
193
227
  function evaluateBinaryOperation(operator, left, right) {
194
228
  switch (operator) {
@@ -1058,6 +1092,7 @@ export {
1058
1092
  isBinaryOp,
1059
1093
  isAssignment,
1060
1094
  generate,
1095
+ extractInputVariables,
1061
1096
  execute,
1062
1097
  defaultContext,
1063
1098
  exports_date_utils as dateUtils,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "littlewing",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "A minimal, high-performance arithmetic expression language with lexer, parser, and executor. Optimized for browsers with zero dependencies and type-safe execution.",
5
5
  "keywords": [
6
6
  "arithmetic",