littlewing 0.3.0 → 0.3.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/README.md +34 -40
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ A minimal, high-performance arithmetic expression language with a complete lexer
|
|
|
8
8
|
- 📦 **Tiny Bundle** - 3.61 KB gzipped, zero dependencies
|
|
9
9
|
- 🌐 **Browser Ready** - 100% ESM, no Node.js APIs
|
|
10
10
|
- 🔒 **Type-Safe** - Strict TypeScript with full type coverage
|
|
11
|
-
- ✅ **Thoroughly Tested** -
|
|
11
|
+
- ✅ **Thoroughly Tested** - 106 tests, 97.66% coverage
|
|
12
12
|
- 📐 **Math Expressions** - Numbers, dates, operators, functions, variables
|
|
13
13
|
- 🎯 **Clean API** - Intuitive dual API (class-based + functional)
|
|
14
14
|
- 📝 **Well Documented** - Complete JSDoc and examples
|
|
@@ -183,6 +183,24 @@ const ast = parseSource("2 + 3 * 4");
|
|
|
183
183
|
// Returns: BinaryOp(+, NumberLiteral(2), BinaryOp(*, ...))
|
|
184
184
|
```
|
|
185
185
|
|
|
186
|
+
#### `generate(node: ASTNode): string`
|
|
187
|
+
|
|
188
|
+
Convert an AST node back to source code. Intelligently adds parentheses only when necessary to preserve semantics.
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
import { generate, ast } from "littlewing";
|
|
192
|
+
|
|
193
|
+
// From AST builders
|
|
194
|
+
const expr = ast.multiply(ast.add(ast.number(2), ast.number(3)), ast.number(4));
|
|
195
|
+
generate(expr); // → "(2 + 3) * 4"
|
|
196
|
+
|
|
197
|
+
// Round-trip: parse → generate → parse
|
|
198
|
+
const code = "2 + 3 * 4";
|
|
199
|
+
const tree = parseSource(code);
|
|
200
|
+
const regenerated = generate(tree); // → "2 + 3 * 4"
|
|
201
|
+
parseSource(regenerated); // Same AST structure
|
|
202
|
+
```
|
|
203
|
+
|
|
186
204
|
### Classes
|
|
187
205
|
|
|
188
206
|
#### `Lexer`
|
|
@@ -219,6 +237,17 @@ const executor = new Executor(context);
|
|
|
219
237
|
const result = executor.execute(ast);
|
|
220
238
|
```
|
|
221
239
|
|
|
240
|
+
#### `CodeGenerator`
|
|
241
|
+
|
|
242
|
+
Convert AST nodes back to source code. Handles operator precedence and associativity automatically.
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
import { CodeGenerator } from "littlewing";
|
|
246
|
+
|
|
247
|
+
const generator = new CodeGenerator();
|
|
248
|
+
const code = generator.generate(ast);
|
|
249
|
+
```
|
|
250
|
+
|
|
222
251
|
### AST Builders
|
|
223
252
|
|
|
224
253
|
The `ast` namespace provides convenient functions for building AST nodes:
|
|
@@ -256,37 +285,6 @@ import { defaultContext } from "littlewing";
|
|
|
256
285
|
(milliseconds, seconds, minutes, hours, days);
|
|
257
286
|
```
|
|
258
287
|
|
|
259
|
-
## Type Definitions
|
|
260
|
-
|
|
261
|
-
### ExecutionContext
|
|
262
|
-
|
|
263
|
-
```typescript
|
|
264
|
-
interface ExecutionContext {
|
|
265
|
-
functions?: Record<string, (...args: any[]) => number | Date>;
|
|
266
|
-
variables?: Record<string, number | Date>;
|
|
267
|
-
}
|
|
268
|
-
```
|
|
269
|
-
|
|
270
|
-
### RuntimeValue
|
|
271
|
-
|
|
272
|
-
```typescript
|
|
273
|
-
type RuntimeValue = number | Date | unknown;
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
### ASTNode
|
|
277
|
-
|
|
278
|
-
```typescript
|
|
279
|
-
type ASTNode =
|
|
280
|
-
| Program
|
|
281
|
-
| NumberLiteral
|
|
282
|
-
| StringLiteral
|
|
283
|
-
| Identifier
|
|
284
|
-
| BinaryOp
|
|
285
|
-
| UnaryOp
|
|
286
|
-
| FunctionCall
|
|
287
|
-
| Assignment;
|
|
288
|
-
```
|
|
289
|
-
|
|
290
288
|
## Examples
|
|
291
289
|
|
|
292
290
|
### Calculator
|
|
@@ -362,14 +360,14 @@ execute("kilometers(5)", context); // → 8.0467
|
|
|
362
360
|
|
|
363
361
|
### Bundle Size
|
|
364
362
|
|
|
365
|
-
- Raw:
|
|
366
|
-
- Gzipped: **
|
|
363
|
+
- Raw: 21.06 KB
|
|
364
|
+
- Gzipped: **4.26 KB**
|
|
367
365
|
- Zero dependencies
|
|
368
366
|
|
|
369
367
|
### Test Coverage
|
|
370
368
|
|
|
371
|
-
-
|
|
372
|
-
-
|
|
369
|
+
- 106 comprehensive tests
|
|
370
|
+
- 95.99% code coverage
|
|
373
371
|
- All edge cases handled
|
|
374
372
|
|
|
375
373
|
## Type Safety
|
|
@@ -408,7 +406,3 @@ MIT
|
|
|
408
406
|
## Contributing
|
|
409
407
|
|
|
410
408
|
See [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
|
|
411
|
-
|
|
412
|
-
---
|
|
413
|
-
|
|
414
|
-
Made with ❤️ by the littlewing team
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "littlewing",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.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",
|