littlewing 0.1.0 → 0.2.0
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 +12 -1
- package/dist/index.js +49 -20
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -107,6 +107,17 @@ interface Assignment {
|
|
|
107
107
|
value: ASTNode;
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
110
|
+
* Type guard functions for discriminated union narrowing
|
|
111
|
+
*/
|
|
112
|
+
declare function isNumberLiteral(node: ASTNode): node is NumberLiteral;
|
|
113
|
+
declare function isStringLiteral(node: ASTNode): node is StringLiteral;
|
|
114
|
+
declare function isIdentifier(node: ASTNode): node is Identifier;
|
|
115
|
+
declare function isBinaryOp(node: ASTNode): node is BinaryOp;
|
|
116
|
+
declare function isUnaryOp(node: ASTNode): node is UnaryOp;
|
|
117
|
+
declare function isFunctionCall(node: ASTNode): node is FunctionCall;
|
|
118
|
+
declare function isAssignment(node: ASTNode): node is Assignment;
|
|
119
|
+
declare function isProgram(node: ASTNode): node is Program;
|
|
120
|
+
/**
|
|
110
121
|
* Builder functions for creating AST nodes manually
|
|
111
122
|
*/
|
|
112
123
|
/**
|
|
@@ -357,4 +368,4 @@ declare class Parser {
|
|
|
357
368
|
* Parse source code string into AST
|
|
358
369
|
*/
|
|
359
370
|
declare function parseSource(source: string): ASTNode;
|
|
360
|
-
export { parseSource, execute, defaultContext, exports_ast as ast, TokenType, Token, RuntimeValue, Parser, Lexer, Executor, ExecutionContext, ASTNode };
|
|
371
|
+
export { parseSource, isUnaryOp, isStringLiteral, isProgram, isNumberLiteral, isIdentifier, isFunctionCall, isBinaryOp, isAssignment, execute, defaultContext, exports_ast as ast, TokenType, Token, RuntimeValue, Parser, Lexer, Executor, ExecutionContext, ASTNode };
|
package/dist/index.js
CHANGED
|
@@ -132,6 +132,30 @@ var TokenType;
|
|
|
132
132
|
TokenType2["COMMA"] = "COMMA";
|
|
133
133
|
TokenType2["EOF"] = "EOF";
|
|
134
134
|
})(TokenType ||= {});
|
|
135
|
+
function isNumberLiteral(node) {
|
|
136
|
+
return node.type === "NumberLiteral";
|
|
137
|
+
}
|
|
138
|
+
function isStringLiteral(node) {
|
|
139
|
+
return node.type === "StringLiteral";
|
|
140
|
+
}
|
|
141
|
+
function isIdentifier(node) {
|
|
142
|
+
return node.type === "Identifier";
|
|
143
|
+
}
|
|
144
|
+
function isBinaryOp(node) {
|
|
145
|
+
return node.type === "BinaryOp";
|
|
146
|
+
}
|
|
147
|
+
function isUnaryOp(node) {
|
|
148
|
+
return node.type === "UnaryOp";
|
|
149
|
+
}
|
|
150
|
+
function isFunctionCall(node) {
|
|
151
|
+
return node.type === "FunctionCall";
|
|
152
|
+
}
|
|
153
|
+
function isAssignment(node) {
|
|
154
|
+
return node.type === "Assignment";
|
|
155
|
+
}
|
|
156
|
+
function isProgram(node) {
|
|
157
|
+
return node.type === "Program";
|
|
158
|
+
}
|
|
135
159
|
|
|
136
160
|
// src/lexer.ts
|
|
137
161
|
class Lexer {
|
|
@@ -496,26 +520,23 @@ class Executor {
|
|
|
496
520
|
this.variables = new Map(Object.entries(context.variables || {}));
|
|
497
521
|
}
|
|
498
522
|
execute(node) {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
default:
|
|
517
|
-
throw new Error(`Unknown node type`);
|
|
518
|
-
}
|
|
523
|
+
if (isProgram(node))
|
|
524
|
+
return this.executeProgram(node);
|
|
525
|
+
if (isNumberLiteral(node))
|
|
526
|
+
return this.executeNumberLiteral(node);
|
|
527
|
+
if (isStringLiteral(node))
|
|
528
|
+
return this.executeStringLiteral(node);
|
|
529
|
+
if (isIdentifier(node))
|
|
530
|
+
return this.executeIdentifier(node);
|
|
531
|
+
if (isBinaryOp(node))
|
|
532
|
+
return this.executeBinaryOp(node);
|
|
533
|
+
if (isUnaryOp(node))
|
|
534
|
+
return this.executeUnaryOp(node);
|
|
535
|
+
if (isFunctionCall(node))
|
|
536
|
+
return this.executeFunctionCall(node);
|
|
537
|
+
if (isAssignment(node))
|
|
538
|
+
return this.executeAssignment(node);
|
|
539
|
+
throw new Error(`Unknown node type`);
|
|
519
540
|
}
|
|
520
541
|
executeProgram(node) {
|
|
521
542
|
let result;
|
|
@@ -663,6 +684,14 @@ function execute(source, context) {
|
|
|
663
684
|
}
|
|
664
685
|
export {
|
|
665
686
|
parseSource,
|
|
687
|
+
isUnaryOp,
|
|
688
|
+
isStringLiteral,
|
|
689
|
+
isProgram,
|
|
690
|
+
isNumberLiteral,
|
|
691
|
+
isIdentifier,
|
|
692
|
+
isFunctionCall,
|
|
693
|
+
isBinaryOp,
|
|
694
|
+
isAssignment,
|
|
666
695
|
execute,
|
|
667
696
|
defaultContext,
|
|
668
697
|
exports_ast as ast,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "littlewing",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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",
|