just-bash 1.1.0 → 1.2.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/Bash.d.ts +18 -0
- package/dist/ai/index.d.ts +6 -1
- package/dist/bin/chunks/awk2-4DKPX43H.js +20 -0
- package/dist/bin/chunks/awk2-6XCE67AX.js +20 -0
- package/dist/bin/chunks/awk2-DKAZ7LPG.js +20 -0
- package/dist/bin/chunks/awk2-GAJ72X3Z.js +20 -0
- package/dist/bin/chunks/awk2-HJAPWU2N.js +18 -0
- package/dist/bin/chunks/awk2-LVXC5OAC.js +20 -0
- package/dist/bin/chunks/awk2-T6HR4EYO.js +20 -0
- package/dist/bin/chunks/awk2-UYU2UYZ2.js +20 -0
- package/dist/bin/chunks/awk2-W64AZN7P.js +20 -0
- package/dist/bin/chunks/chunk-NWWB2XRE.js +6 -0
- package/dist/bin/chunks/find-7BUNQZX4.js +11 -0
- package/dist/bin/chunks/find-YATLECXJ.js +11 -0
- package/dist/bin/chunks/grep-PHBFGJCV.js +15 -0
- package/dist/bin/chunks/sed-B6FXUN7R.js +80 -0
- package/dist/bin/chunks/sed-LTCVBSRV.js +80 -0
- package/dist/bin/chunks/sed-RHLDIXPL.js +76 -0
- package/dist/bin/chunks/sed-SYSATJXR.js +75 -0
- package/dist/bin/just-bash.js +108 -108
- package/dist/bin/shell/chunks/awk2-4DKPX43H.js +20 -0
- package/dist/bin/shell/chunks/awk2-6XCE67AX.js +20 -0
- package/dist/bin/shell/chunks/awk2-DKAZ7LPG.js +20 -0
- package/dist/bin/shell/chunks/awk2-GAJ72X3Z.js +20 -0
- package/dist/bin/shell/chunks/awk2-HJAPWU2N.js +18 -0
- package/dist/bin/shell/chunks/awk2-LVXC5OAC.js +20 -0
- package/dist/bin/shell/chunks/awk2-T6HR4EYO.js +20 -0
- package/dist/bin/shell/chunks/awk2-UYU2UYZ2.js +20 -0
- package/dist/bin/shell/chunks/awk2-W64AZN7P.js +20 -0
- package/dist/bin/shell/chunks/chunk-NWWB2XRE.js +6 -0
- package/dist/bin/shell/chunks/find-7BUNQZX4.js +11 -0
- package/dist/bin/shell/chunks/find-YATLECXJ.js +11 -0
- package/dist/bin/shell/chunks/grep-PHBFGJCV.js +15 -0
- package/dist/bin/shell/chunks/sed-B6FXUN7R.js +80 -0
- package/dist/bin/shell/chunks/sed-LTCVBSRV.js +80 -0
- package/dist/bin/shell/chunks/sed-RHLDIXPL.js +76 -0
- package/dist/bin/shell/chunks/sed-SYSATJXR.js +75 -0
- package/dist/bin/shell/shell.js +116 -120
- package/dist/bundle/ai/index.js +410 -373
- package/dist/bundle/chunks/awk2-365HINHI.js +19 -0
- package/dist/bundle/chunks/awk2-4YCQ6UCB.js +19 -0
- package/dist/bundle/chunks/awk2-7YCFY7D2.js +19 -0
- package/dist/bundle/chunks/awk2-7YL3P4KU.js +19 -0
- package/dist/bundle/chunks/awk2-ETXZHQQ4.js +19 -0
- package/dist/bundle/chunks/awk2-FMEL7ESV.js +19 -0
- package/dist/bundle/chunks/awk2-LE4QUFGK.js +19 -0
- package/dist/bundle/chunks/awk2-MGHA6JPJ.js +17 -0
- package/dist/bundle/chunks/awk2-XEAK5PBN.js +19 -0
- package/dist/bundle/chunks/chunk-CG2HXOFG.js +5 -0
- package/dist/bundle/chunks/find-KP4M5DWC.js +10 -0
- package/dist/bundle/chunks/find-WM6ZETA7.js +10 -0
- package/dist/bundle/chunks/grep-CZTYKNEE.js +14 -0
- package/dist/bundle/chunks/sed-2WWCANZT.js +79 -0
- package/dist/bundle/chunks/sed-5LZHAGFR.js +74 -0
- package/dist/bundle/chunks/sed-IGVS3UIY.js +79 -0
- package/dist/bundle/chunks/sed-V7XSBPTD.js +75 -0
- package/dist/bundle/index.js +111 -111
- package/dist/commands/awk/ast.d.ts +201 -0
- package/dist/commands/awk/awk2.d.ts +7 -0
- package/dist/commands/awk/builtins.d.ts +17 -0
- package/dist/commands/awk/interpreter/context.d.ts +53 -0
- package/dist/commands/awk/interpreter/expressions.d.ts +17 -0
- package/dist/commands/awk/interpreter/fields.d.ts +25 -0
- package/dist/commands/awk/interpreter/helpers.d.ts +29 -0
- package/dist/commands/awk/interpreter/index.d.ts +8 -0
- package/dist/commands/awk/interpreter/interpreter.d.ts +50 -0
- package/dist/commands/awk/interpreter/statements.d.ts +11 -0
- package/dist/commands/awk/interpreter/types.d.ts +13 -0
- package/dist/commands/awk/interpreter/variables.d.ts +35 -0
- package/dist/commands/awk/interpreter2.d.ts +90 -0
- package/dist/commands/awk/lexer.d.ts +100 -0
- package/dist/commands/awk/parser2.d.ts +73 -0
- package/dist/commands/find/matcher.d.ts +6 -2
- package/dist/commands/find/types.d.ts +13 -0
- package/dist/commands/sed/executor.d.ts +1 -1
- package/dist/commands/sed/lexer.d.ts +74 -0
- package/dist/commands/sed/parser.d.ts +4 -0
- package/dist/commands/sed/types.d.ts +66 -2
- package/dist/index.d.ts +1 -1
- package/dist/interpreter/errors.d.ts +8 -0
- package/dist/interpreter/types.d.ts +2 -0
- package/package.json +2 -1
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Abstract Syntax Tree Types
|
|
3
|
+
*
|
|
4
|
+
* Follows the same discriminated union pattern as the main bash AST.
|
|
5
|
+
*/
|
|
6
|
+
export type AwkExpr = AwkNumberLiteral | AwkStringLiteral | AwkRegexLiteral | AwkFieldRef | AwkVariable | AwkArrayAccess | AwkBinaryOp | AwkUnaryOp | AwkTernaryOp | AwkFunctionCall | AwkAssignment | AwkPreIncrement | AwkPreDecrement | AwkPostIncrement | AwkPostDecrement | AwkGetline | AwkInExpr | AwkTuple;
|
|
7
|
+
export interface AwkNumberLiteral {
|
|
8
|
+
type: "number";
|
|
9
|
+
value: number;
|
|
10
|
+
}
|
|
11
|
+
export interface AwkStringLiteral {
|
|
12
|
+
type: "string";
|
|
13
|
+
value: string;
|
|
14
|
+
}
|
|
15
|
+
export interface AwkRegexLiteral {
|
|
16
|
+
type: "regex";
|
|
17
|
+
pattern: string;
|
|
18
|
+
}
|
|
19
|
+
export interface AwkFieldRef {
|
|
20
|
+
type: "field";
|
|
21
|
+
index: AwkExpr;
|
|
22
|
+
}
|
|
23
|
+
export interface AwkVariable {
|
|
24
|
+
type: "variable";
|
|
25
|
+
name: string;
|
|
26
|
+
}
|
|
27
|
+
export interface AwkArrayAccess {
|
|
28
|
+
type: "array_access";
|
|
29
|
+
array: string;
|
|
30
|
+
key: AwkExpr;
|
|
31
|
+
}
|
|
32
|
+
export interface AwkBinaryOp {
|
|
33
|
+
type: "binary";
|
|
34
|
+
operator: "+" | "-" | "*" | "/" | "%" | "^" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "~" | "!~" | "&&" | "||" | " ";
|
|
35
|
+
left: AwkExpr;
|
|
36
|
+
right: AwkExpr;
|
|
37
|
+
}
|
|
38
|
+
export interface AwkUnaryOp {
|
|
39
|
+
type: "unary";
|
|
40
|
+
operator: "!" | "-" | "+";
|
|
41
|
+
operand: AwkExpr;
|
|
42
|
+
}
|
|
43
|
+
export interface AwkPreIncrement {
|
|
44
|
+
type: "pre_increment";
|
|
45
|
+
operand: AwkVariable | AwkArrayAccess | AwkFieldRef;
|
|
46
|
+
}
|
|
47
|
+
export interface AwkPreDecrement {
|
|
48
|
+
type: "pre_decrement";
|
|
49
|
+
operand: AwkVariable | AwkArrayAccess | AwkFieldRef;
|
|
50
|
+
}
|
|
51
|
+
export interface AwkPostIncrement {
|
|
52
|
+
type: "post_increment";
|
|
53
|
+
operand: AwkVariable | AwkArrayAccess | AwkFieldRef;
|
|
54
|
+
}
|
|
55
|
+
export interface AwkPostDecrement {
|
|
56
|
+
type: "post_decrement";
|
|
57
|
+
operand: AwkVariable | AwkArrayAccess | AwkFieldRef;
|
|
58
|
+
}
|
|
59
|
+
export interface AwkTernaryOp {
|
|
60
|
+
type: "ternary";
|
|
61
|
+
condition: AwkExpr;
|
|
62
|
+
consequent: AwkExpr;
|
|
63
|
+
alternate: AwkExpr;
|
|
64
|
+
}
|
|
65
|
+
export interface AwkFunctionCall {
|
|
66
|
+
type: "call";
|
|
67
|
+
name: string;
|
|
68
|
+
args: AwkExpr[];
|
|
69
|
+
}
|
|
70
|
+
export interface AwkAssignment {
|
|
71
|
+
type: "assignment";
|
|
72
|
+
operator: "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "^=";
|
|
73
|
+
target: AwkFieldRef | AwkVariable | AwkArrayAccess;
|
|
74
|
+
value: AwkExpr;
|
|
75
|
+
}
|
|
76
|
+
export interface AwkInExpr {
|
|
77
|
+
type: "in";
|
|
78
|
+
key: AwkExpr;
|
|
79
|
+
array: string;
|
|
80
|
+
}
|
|
81
|
+
export interface AwkGetline {
|
|
82
|
+
type: "getline";
|
|
83
|
+
variable?: string;
|
|
84
|
+
file?: AwkExpr;
|
|
85
|
+
}
|
|
86
|
+
export interface AwkTuple {
|
|
87
|
+
type: "tuple";
|
|
88
|
+
elements: AwkExpr[];
|
|
89
|
+
}
|
|
90
|
+
export type AwkStmt = AwkExpressionStmt | AwkPrintStmt | AwkPrintfStmt | AwkIfStmt | AwkWhileStmt | AwkDoWhileStmt | AwkForStmt | AwkForInStmt | AwkBreakStmt | AwkContinueStmt | AwkNextStmt | AwkNextFileStmt | AwkExitStmt | AwkReturnStmt | AwkDeleteStmt | AwkBlock;
|
|
91
|
+
export interface AwkExpressionStmt {
|
|
92
|
+
type: "expr_stmt";
|
|
93
|
+
expression: AwkExpr;
|
|
94
|
+
}
|
|
95
|
+
export interface AwkPrintStmt {
|
|
96
|
+
type: "print";
|
|
97
|
+
args: AwkExpr[];
|
|
98
|
+
output?: {
|
|
99
|
+
redirect: ">" | ">>";
|
|
100
|
+
file: AwkExpr;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
export interface AwkPrintfStmt {
|
|
104
|
+
type: "printf";
|
|
105
|
+
format: AwkExpr;
|
|
106
|
+
args: AwkExpr[];
|
|
107
|
+
output?: {
|
|
108
|
+
redirect: ">" | ">>";
|
|
109
|
+
file: AwkExpr;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
export interface AwkIfStmt {
|
|
113
|
+
type: "if";
|
|
114
|
+
condition: AwkExpr;
|
|
115
|
+
consequent: AwkStmt;
|
|
116
|
+
alternate?: AwkStmt;
|
|
117
|
+
}
|
|
118
|
+
export interface AwkWhileStmt {
|
|
119
|
+
type: "while";
|
|
120
|
+
condition: AwkExpr;
|
|
121
|
+
body: AwkStmt;
|
|
122
|
+
}
|
|
123
|
+
export interface AwkDoWhileStmt {
|
|
124
|
+
type: "do_while";
|
|
125
|
+
body: AwkStmt;
|
|
126
|
+
condition: AwkExpr;
|
|
127
|
+
}
|
|
128
|
+
export interface AwkForStmt {
|
|
129
|
+
type: "for";
|
|
130
|
+
init?: AwkExpr;
|
|
131
|
+
condition?: AwkExpr;
|
|
132
|
+
update?: AwkExpr;
|
|
133
|
+
body: AwkStmt;
|
|
134
|
+
}
|
|
135
|
+
export interface AwkForInStmt {
|
|
136
|
+
type: "for_in";
|
|
137
|
+
variable: string;
|
|
138
|
+
array: string;
|
|
139
|
+
body: AwkStmt;
|
|
140
|
+
}
|
|
141
|
+
export interface AwkBlock {
|
|
142
|
+
type: "block";
|
|
143
|
+
statements: AwkStmt[];
|
|
144
|
+
}
|
|
145
|
+
export interface AwkBreakStmt {
|
|
146
|
+
type: "break";
|
|
147
|
+
}
|
|
148
|
+
export interface AwkContinueStmt {
|
|
149
|
+
type: "continue";
|
|
150
|
+
}
|
|
151
|
+
export interface AwkNextStmt {
|
|
152
|
+
type: "next";
|
|
153
|
+
}
|
|
154
|
+
export interface AwkNextFileStmt {
|
|
155
|
+
type: "nextfile";
|
|
156
|
+
}
|
|
157
|
+
export interface AwkExitStmt {
|
|
158
|
+
type: "exit";
|
|
159
|
+
code?: AwkExpr;
|
|
160
|
+
}
|
|
161
|
+
export interface AwkReturnStmt {
|
|
162
|
+
type: "return";
|
|
163
|
+
value?: AwkExpr;
|
|
164
|
+
}
|
|
165
|
+
export interface AwkDeleteStmt {
|
|
166
|
+
type: "delete";
|
|
167
|
+
target: AwkArrayAccess | AwkVariable;
|
|
168
|
+
}
|
|
169
|
+
export type AwkPattern = AwkBeginPattern | AwkEndPattern | AwkExprPattern | AwkRegexPattern | AwkRangePattern;
|
|
170
|
+
export interface AwkBeginPattern {
|
|
171
|
+
type: "begin";
|
|
172
|
+
}
|
|
173
|
+
export interface AwkEndPattern {
|
|
174
|
+
type: "end";
|
|
175
|
+
}
|
|
176
|
+
export interface AwkExprPattern {
|
|
177
|
+
type: "expr_pattern";
|
|
178
|
+
expression: AwkExpr;
|
|
179
|
+
}
|
|
180
|
+
export interface AwkRegexPattern {
|
|
181
|
+
type: "regex_pattern";
|
|
182
|
+
pattern: string;
|
|
183
|
+
}
|
|
184
|
+
export interface AwkRangePattern {
|
|
185
|
+
type: "range";
|
|
186
|
+
start: AwkPattern;
|
|
187
|
+
end: AwkPattern;
|
|
188
|
+
}
|
|
189
|
+
export interface AwkRule {
|
|
190
|
+
pattern?: AwkPattern;
|
|
191
|
+
action: AwkBlock;
|
|
192
|
+
}
|
|
193
|
+
export interface AwkFunctionDef {
|
|
194
|
+
name: string;
|
|
195
|
+
params: string[];
|
|
196
|
+
body: AwkBlock;
|
|
197
|
+
}
|
|
198
|
+
export interface AwkProgram {
|
|
199
|
+
functions: AwkFunctionDef[];
|
|
200
|
+
rules: AwkRule[];
|
|
201
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Built-in Functions
|
|
3
|
+
*
|
|
4
|
+
* Implementation of AWK built-in functions for the AST-based interpreter.
|
|
5
|
+
*/
|
|
6
|
+
import type { AwkExpr } from "./ast.js";
|
|
7
|
+
import type { AwkRuntimeContext } from "./interpreter/context.js";
|
|
8
|
+
import type { AwkValue } from "./interpreter/types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Interface for evaluating expressions (passed from interpreter)
|
|
11
|
+
*/
|
|
12
|
+
export interface AwkEvaluator {
|
|
13
|
+
evalExpr: (expr: AwkExpr) => Promise<AwkValue>;
|
|
14
|
+
}
|
|
15
|
+
export type AwkBuiltinFn = (args: AwkExpr[], ctx: AwkRuntimeContext, evaluator: AwkEvaluator) => AwkValue | Promise<AwkValue>;
|
|
16
|
+
export declare function formatPrintf(format: string, values: AwkValue[]): string;
|
|
17
|
+
export declare const awkBuiltins: Record<string, AwkBuiltinFn>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Runtime Context
|
|
3
|
+
*
|
|
4
|
+
* Holds all state for AWK program execution.
|
|
5
|
+
*/
|
|
6
|
+
import type { AwkFunctionDef } from "../ast.js";
|
|
7
|
+
import type { AwkFileSystem, AwkValue } from "./types.js";
|
|
8
|
+
export interface AwkRuntimeContext {
|
|
9
|
+
FS: string;
|
|
10
|
+
OFS: string;
|
|
11
|
+
ORS: string;
|
|
12
|
+
NR: number;
|
|
13
|
+
NF: number;
|
|
14
|
+
FNR: number;
|
|
15
|
+
FILENAME: string;
|
|
16
|
+
RSTART: number;
|
|
17
|
+
RLENGTH: number;
|
|
18
|
+
SUBSEP: string;
|
|
19
|
+
fields: string[];
|
|
20
|
+
line: string;
|
|
21
|
+
vars: Record<string, AwkValue>;
|
|
22
|
+
arrays: Record<string, Record<string, AwkValue>>;
|
|
23
|
+
ARGC: number;
|
|
24
|
+
ARGV: Record<string, string>;
|
|
25
|
+
functions: Map<string, AwkFunctionDef>;
|
|
26
|
+
lines?: string[];
|
|
27
|
+
lineIndex?: number;
|
|
28
|
+
fieldSep: RegExp;
|
|
29
|
+
maxIterations: number;
|
|
30
|
+
maxRecursionDepth: number;
|
|
31
|
+
currentRecursionDepth: number;
|
|
32
|
+
exitCode: number;
|
|
33
|
+
shouldExit: boolean;
|
|
34
|
+
shouldNext: boolean;
|
|
35
|
+
shouldNextFile: boolean;
|
|
36
|
+
loopBreak: boolean;
|
|
37
|
+
loopContinue: boolean;
|
|
38
|
+
returnValue?: AwkValue;
|
|
39
|
+
hasReturn: boolean;
|
|
40
|
+
output: string;
|
|
41
|
+
fs?: AwkFileSystem;
|
|
42
|
+
cwd?: string;
|
|
43
|
+
openedFiles: Set<string>;
|
|
44
|
+
random?: () => number;
|
|
45
|
+
}
|
|
46
|
+
export interface CreateContextOptions {
|
|
47
|
+
fieldSep?: RegExp;
|
|
48
|
+
maxIterations?: number;
|
|
49
|
+
maxRecursionDepth?: number;
|
|
50
|
+
fs?: AwkFileSystem;
|
|
51
|
+
cwd?: string;
|
|
52
|
+
}
|
|
53
|
+
export declare function createRuntimeContext(options?: CreateContextOptions): AwkRuntimeContext;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Expression Evaluation
|
|
3
|
+
*
|
|
4
|
+
* Async expression evaluator supporting file I/O operations.
|
|
5
|
+
*/
|
|
6
|
+
import type { AwkExpr } from "../ast.js";
|
|
7
|
+
import type { AwkRuntimeContext } from "./context.js";
|
|
8
|
+
import type { AwkValue } from "./types.js";
|
|
9
|
+
export type BlockExecutor = (ctx: AwkRuntimeContext, statements: import("../ast.js").AwkStmt[]) => Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Set the block executor function (called from statements.ts to avoid circular deps)
|
|
12
|
+
*/
|
|
13
|
+
export declare function setBlockExecutor(fn: BlockExecutor): void;
|
|
14
|
+
/**
|
|
15
|
+
* Evaluate an AWK expression asynchronously.
|
|
16
|
+
*/
|
|
17
|
+
export declare function evalExpr(ctx: AwkRuntimeContext, expr: AwkExpr): Promise<AwkValue>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Field Operations
|
|
3
|
+
*
|
|
4
|
+
* Handles $0, $1, $2, etc. field access and modification.
|
|
5
|
+
*/
|
|
6
|
+
import type { AwkRuntimeContext } from "./context.js";
|
|
7
|
+
import type { AwkValue } from "./types.js";
|
|
8
|
+
/**
|
|
9
|
+
* Get a field value by index.
|
|
10
|
+
* $0 is the whole line, $1 is first field, etc.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getField(ctx: AwkRuntimeContext, index: number): AwkValue;
|
|
13
|
+
/**
|
|
14
|
+
* Set a field value by index.
|
|
15
|
+
* Setting $0 re-splits the line. Setting other fields rebuilds $0.
|
|
16
|
+
*/
|
|
17
|
+
export declare function setField(ctx: AwkRuntimeContext, index: number, value: AwkValue): void;
|
|
18
|
+
/**
|
|
19
|
+
* Update context with a new line (used when processing input).
|
|
20
|
+
*/
|
|
21
|
+
export declare function setCurrentLine(ctx: AwkRuntimeContext, line: string): void;
|
|
22
|
+
/**
|
|
23
|
+
* Update field separator and recompile regex.
|
|
24
|
+
*/
|
|
25
|
+
export declare function setFieldSeparator(ctx: AwkRuntimeContext, fs: string): void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Type Conversion Helpers
|
|
3
|
+
*
|
|
4
|
+
* Pure functions for type conversion and truthiness checking.
|
|
5
|
+
*/
|
|
6
|
+
import type { AwkValue } from "./types.js";
|
|
7
|
+
/**
|
|
8
|
+
* Check if a value is truthy in AWK.
|
|
9
|
+
* Numbers are truthy if non-zero, strings if non-empty.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isTruthy(val: AwkValue): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Convert an AWK value to a number.
|
|
14
|
+
* Strings are parsed as floats, empty/non-numeric strings become 0.
|
|
15
|
+
*/
|
|
16
|
+
export declare function toNumber(val: AwkValue): number;
|
|
17
|
+
/**
|
|
18
|
+
* Convert an AWK value to a string.
|
|
19
|
+
* Numbers are formatted without trailing zeros.
|
|
20
|
+
*/
|
|
21
|
+
export declare function toAwkString(val: AwkValue): string;
|
|
22
|
+
/**
|
|
23
|
+
* Check if a value looks like a number for comparison purposes.
|
|
24
|
+
*/
|
|
25
|
+
export declare function looksLikeNumber(val: AwkValue): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Test if a string matches a regex pattern.
|
|
28
|
+
*/
|
|
29
|
+
export declare function matchRegex(pattern: string, text: string): boolean;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Interpreter Module
|
|
3
|
+
*
|
|
4
|
+
* Re-exports the public API for the AWK interpreter.
|
|
5
|
+
*/
|
|
6
|
+
export { type AwkRuntimeContext, type CreateContextOptions, createRuntimeContext, } from "./context.js";
|
|
7
|
+
export { AwkInterpreter } from "./interpreter.js";
|
|
8
|
+
export type { AwkFileSystem, AwkValue } from "./types.js";
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Interpreter
|
|
3
|
+
*
|
|
4
|
+
* Main interpreter class that orchestrates AWK program execution.
|
|
5
|
+
*/
|
|
6
|
+
import type { AwkProgram } from "../ast.js";
|
|
7
|
+
import type { AwkRuntimeContext } from "./context.js";
|
|
8
|
+
export declare class AwkInterpreter {
|
|
9
|
+
private ctx;
|
|
10
|
+
private program;
|
|
11
|
+
private rangeStates;
|
|
12
|
+
constructor(ctx: AwkRuntimeContext);
|
|
13
|
+
/**
|
|
14
|
+
* Initialize the interpreter with a program.
|
|
15
|
+
* Must be called before executeBegin/executeLine/executeEnd.
|
|
16
|
+
*/
|
|
17
|
+
execute(program: AwkProgram): void;
|
|
18
|
+
/**
|
|
19
|
+
* Execute all BEGIN blocks.
|
|
20
|
+
*/
|
|
21
|
+
executeBegin(): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Execute rules for a single input line.
|
|
24
|
+
*/
|
|
25
|
+
executeLine(line: string): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Execute all END blocks.
|
|
28
|
+
*/
|
|
29
|
+
executeEnd(): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Get the accumulated output.
|
|
32
|
+
*/
|
|
33
|
+
getOutput(): string;
|
|
34
|
+
/**
|
|
35
|
+
* Get the exit code.
|
|
36
|
+
*/
|
|
37
|
+
getExitCode(): number;
|
|
38
|
+
/**
|
|
39
|
+
* Get the runtime context (for access to control flow flags, etc.)
|
|
40
|
+
*/
|
|
41
|
+
getContext(): AwkRuntimeContext;
|
|
42
|
+
/**
|
|
43
|
+
* Check if a rule matches the current line.
|
|
44
|
+
*/
|
|
45
|
+
private matchesRule;
|
|
46
|
+
/**
|
|
47
|
+
* Check if a pattern matches.
|
|
48
|
+
*/
|
|
49
|
+
private matchPattern;
|
|
50
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Statement Execution
|
|
3
|
+
*
|
|
4
|
+
* Async statement executor supporting file I/O operations.
|
|
5
|
+
*/
|
|
6
|
+
import type { AwkStmt } from "../ast.js";
|
|
7
|
+
import type { AwkRuntimeContext } from "./context.js";
|
|
8
|
+
/**
|
|
9
|
+
* Execute a block of statements.
|
|
10
|
+
*/
|
|
11
|
+
export declare function executeBlock(ctx: AwkRuntimeContext, statements: AwkStmt[]): Promise<void>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Interpreter Types
|
|
3
|
+
*/
|
|
4
|
+
export type AwkValue = string | number;
|
|
5
|
+
/**
|
|
6
|
+
* File system interface for AWK file operations
|
|
7
|
+
*/
|
|
8
|
+
export interface AwkFileSystem {
|
|
9
|
+
readFile: (path: string) => Promise<string>;
|
|
10
|
+
writeFile: (path: string, content: string) => Promise<void>;
|
|
11
|
+
appendFile: (path: string, content: string) => Promise<void>;
|
|
12
|
+
resolvePath: (cwd: string, path: string) => string;
|
|
13
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Variable and Array Operations
|
|
3
|
+
*
|
|
4
|
+
* Handles user variables, built-in variables, and arrays.
|
|
5
|
+
*/
|
|
6
|
+
import type { AwkRuntimeContext } from "./context.js";
|
|
7
|
+
import type { AwkValue } from "./types.js";
|
|
8
|
+
/**
|
|
9
|
+
* Get a variable value. Handles built-in variables.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getVariable(ctx: AwkRuntimeContext, name: string): AwkValue;
|
|
12
|
+
/**
|
|
13
|
+
* Set a variable value. Handles built-in variables with special behavior.
|
|
14
|
+
*/
|
|
15
|
+
export declare function setVariable(ctx: AwkRuntimeContext, name: string, value: AwkValue): void;
|
|
16
|
+
/**
|
|
17
|
+
* Get an array element value.
|
|
18
|
+
*/
|
|
19
|
+
export declare function getArrayElement(ctx: AwkRuntimeContext, array: string, key: string): AwkValue;
|
|
20
|
+
/**
|
|
21
|
+
* Set an array element value.
|
|
22
|
+
*/
|
|
23
|
+
export declare function setArrayElement(ctx: AwkRuntimeContext, array: string, key: string, value: AwkValue): void;
|
|
24
|
+
/**
|
|
25
|
+
* Check if an array element exists.
|
|
26
|
+
*/
|
|
27
|
+
export declare function hasArrayElement(ctx: AwkRuntimeContext, array: string, key: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Delete an array element.
|
|
30
|
+
*/
|
|
31
|
+
export declare function deleteArrayElement(ctx: AwkRuntimeContext, array: string, key: string): void;
|
|
32
|
+
/**
|
|
33
|
+
* Delete an entire array.
|
|
34
|
+
*/
|
|
35
|
+
export declare function deleteArray(ctx: AwkRuntimeContext, array: string): void;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Interpreter
|
|
3
|
+
*
|
|
4
|
+
* Tree-walking interpreter that executes an AWK AST.
|
|
5
|
+
*/
|
|
6
|
+
import type { AwkExpr, AwkFunctionDef, AwkProgram } from "./ast.js";
|
|
7
|
+
export type AwkValue = string | number;
|
|
8
|
+
export interface AwkRuntimeContext {
|
|
9
|
+
FS: string;
|
|
10
|
+
OFS: string;
|
|
11
|
+
ORS: string;
|
|
12
|
+
NR: number;
|
|
13
|
+
NF: number;
|
|
14
|
+
FNR: number;
|
|
15
|
+
FILENAME: string;
|
|
16
|
+
RSTART: number;
|
|
17
|
+
RLENGTH: number;
|
|
18
|
+
SUBSEP: string;
|
|
19
|
+
fields: string[];
|
|
20
|
+
line: string;
|
|
21
|
+
vars: Record<string, AwkValue>;
|
|
22
|
+
arrays: Record<string, Record<string, AwkValue>>;
|
|
23
|
+
functions: Map<string, AwkFunctionDef>;
|
|
24
|
+
lines?: string[];
|
|
25
|
+
lineIndex?: number;
|
|
26
|
+
fieldSep: RegExp;
|
|
27
|
+
maxIterations: number;
|
|
28
|
+
maxRecursionDepth: number;
|
|
29
|
+
currentRecursionDepth: number;
|
|
30
|
+
exitCode: number;
|
|
31
|
+
shouldExit: boolean;
|
|
32
|
+
shouldNext: boolean;
|
|
33
|
+
loopBreak: boolean;
|
|
34
|
+
loopContinue: boolean;
|
|
35
|
+
returnValue?: AwkValue;
|
|
36
|
+
hasReturn: boolean;
|
|
37
|
+
output: string;
|
|
38
|
+
random?: () => number;
|
|
39
|
+
}
|
|
40
|
+
export declare function createRuntimeContext(fieldSep?: RegExp, maxIterations?: number): AwkRuntimeContext;
|
|
41
|
+
export declare class AwkInterpreter {
|
|
42
|
+
private ctx;
|
|
43
|
+
private program;
|
|
44
|
+
private rangeStates;
|
|
45
|
+
constructor(ctx: AwkRuntimeContext);
|
|
46
|
+
execute(program: AwkProgram): string;
|
|
47
|
+
executeBegin(): void;
|
|
48
|
+
executeLine(line: string): void;
|
|
49
|
+
executeEnd(): void;
|
|
50
|
+
getOutput(): string;
|
|
51
|
+
getExitCode(): number;
|
|
52
|
+
private splitFields;
|
|
53
|
+
private matchesRule;
|
|
54
|
+
private matchPattern;
|
|
55
|
+
private matchRegex;
|
|
56
|
+
private executeBlock;
|
|
57
|
+
private executeStmt;
|
|
58
|
+
private executePrint;
|
|
59
|
+
private executePrintf;
|
|
60
|
+
private executeIf;
|
|
61
|
+
private executeWhile;
|
|
62
|
+
private executeDoWhile;
|
|
63
|
+
private executeFor;
|
|
64
|
+
private executeForIn;
|
|
65
|
+
private executeDelete;
|
|
66
|
+
evalExpr(expr: AwkExpr): AwkValue;
|
|
67
|
+
private getField;
|
|
68
|
+
private setField;
|
|
69
|
+
private getVariable;
|
|
70
|
+
private setVariable;
|
|
71
|
+
private getArrayElement;
|
|
72
|
+
private setArrayElement;
|
|
73
|
+
private evalBinaryOp;
|
|
74
|
+
private isComparisonOp;
|
|
75
|
+
private evalComparison;
|
|
76
|
+
private looksLikeNumber;
|
|
77
|
+
private evalUnaryOp;
|
|
78
|
+
private evalFunctionCall;
|
|
79
|
+
private callUserFunction;
|
|
80
|
+
private evalAssignment;
|
|
81
|
+
private evalPreIncrement;
|
|
82
|
+
private evalPreDecrement;
|
|
83
|
+
private evalPostIncrement;
|
|
84
|
+
private evalPostDecrement;
|
|
85
|
+
private evalInExpr;
|
|
86
|
+
private evalGetline;
|
|
87
|
+
isTruthy(val: AwkValue): boolean;
|
|
88
|
+
toNumber(val: AwkValue): number;
|
|
89
|
+
toString(val: AwkValue): string;
|
|
90
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWK Lexer
|
|
3
|
+
*
|
|
4
|
+
* Tokenizes AWK source code into a stream of tokens.
|
|
5
|
+
*/
|
|
6
|
+
export declare enum TokenType {
|
|
7
|
+
NUMBER = "NUMBER",
|
|
8
|
+
STRING = "STRING",
|
|
9
|
+
REGEX = "REGEX",
|
|
10
|
+
IDENT = "IDENT",
|
|
11
|
+
BEGIN = "BEGIN",
|
|
12
|
+
END = "END",
|
|
13
|
+
IF = "IF",
|
|
14
|
+
ELSE = "ELSE",
|
|
15
|
+
WHILE = "WHILE",
|
|
16
|
+
DO = "DO",
|
|
17
|
+
FOR = "FOR",
|
|
18
|
+
IN = "IN",
|
|
19
|
+
BREAK = "BREAK",
|
|
20
|
+
CONTINUE = "CONTINUE",
|
|
21
|
+
NEXT = "NEXT",
|
|
22
|
+
NEXTFILE = "NEXTFILE",
|
|
23
|
+
EXIT = "EXIT",
|
|
24
|
+
RETURN = "RETURN",
|
|
25
|
+
DELETE = "DELETE",
|
|
26
|
+
FUNCTION = "FUNCTION",
|
|
27
|
+
PRINT = "PRINT",
|
|
28
|
+
PRINTF = "PRINTF",
|
|
29
|
+
GETLINE = "GETLINE",
|
|
30
|
+
PLUS = "PLUS",
|
|
31
|
+
MINUS = "MINUS",
|
|
32
|
+
STAR = "STAR",
|
|
33
|
+
SLASH = "SLASH",
|
|
34
|
+
PERCENT = "PERCENT",
|
|
35
|
+
CARET = "CARET",
|
|
36
|
+
EQ = "EQ",
|
|
37
|
+
NE = "NE",
|
|
38
|
+
LT = "LT",
|
|
39
|
+
GT = "GT",
|
|
40
|
+
LE = "LE",
|
|
41
|
+
GE = "GE",
|
|
42
|
+
MATCH = "MATCH",
|
|
43
|
+
NOT_MATCH = "NOT_MATCH",
|
|
44
|
+
AND = "AND",
|
|
45
|
+
OR = "OR",
|
|
46
|
+
NOT = "NOT",
|
|
47
|
+
ASSIGN = "ASSIGN",
|
|
48
|
+
PLUS_ASSIGN = "PLUS_ASSIGN",
|
|
49
|
+
MINUS_ASSIGN = "MINUS_ASSIGN",
|
|
50
|
+
STAR_ASSIGN = "STAR_ASSIGN",
|
|
51
|
+
SLASH_ASSIGN = "SLASH_ASSIGN",
|
|
52
|
+
PERCENT_ASSIGN = "PERCENT_ASSIGN",
|
|
53
|
+
CARET_ASSIGN = "CARET_ASSIGN",
|
|
54
|
+
INCREMENT = "INCREMENT",
|
|
55
|
+
DECREMENT = "DECREMENT",
|
|
56
|
+
QUESTION = "QUESTION",
|
|
57
|
+
COLON = "COLON",
|
|
58
|
+
COMMA = "COMMA",
|
|
59
|
+
SEMICOLON = "SEMICOLON",
|
|
60
|
+
NEWLINE = "NEWLINE",
|
|
61
|
+
LPAREN = "LPAREN",
|
|
62
|
+
RPAREN = "RPAREN",
|
|
63
|
+
LBRACE = "LBRACE",
|
|
64
|
+
RBRACE = "RBRACE",
|
|
65
|
+
LBRACKET = "LBRACKET",
|
|
66
|
+
RBRACKET = "RBRACKET",
|
|
67
|
+
DOLLAR = "DOLLAR",
|
|
68
|
+
APPEND = "APPEND",
|
|
69
|
+
PIPE = "PIPE",
|
|
70
|
+
EOF = "EOF"
|
|
71
|
+
}
|
|
72
|
+
export interface Token {
|
|
73
|
+
type: TokenType;
|
|
74
|
+
value: string | number;
|
|
75
|
+
line: number;
|
|
76
|
+
column: number;
|
|
77
|
+
}
|
|
78
|
+
export declare class AwkLexer {
|
|
79
|
+
private input;
|
|
80
|
+
private pos;
|
|
81
|
+
private line;
|
|
82
|
+
private column;
|
|
83
|
+
private lastTokenType;
|
|
84
|
+
constructor(input: string);
|
|
85
|
+
tokenize(): Token[];
|
|
86
|
+
private makeToken;
|
|
87
|
+
private peek;
|
|
88
|
+
private advance;
|
|
89
|
+
private skipWhitespace;
|
|
90
|
+
private nextToken;
|
|
91
|
+
private canBeRegex;
|
|
92
|
+
private readString;
|
|
93
|
+
private readRegex;
|
|
94
|
+
private readNumber;
|
|
95
|
+
private readIdentifier;
|
|
96
|
+
private readOperator;
|
|
97
|
+
private isDigit;
|
|
98
|
+
private isAlpha;
|
|
99
|
+
private isAlphaNumeric;
|
|
100
|
+
}
|