arc-lang 0.6.2 → 0.6.3
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/ast.d.ts +30 -2
- package/dist/build.js +1 -1
- package/dist/index.js +104 -0
- package/dist/interpreter.js +129 -16
- package/dist/lexer.d.ts +41 -37
- package/dist/lexer.js +47 -39
- package/dist/modules.js +5 -1
- package/dist/parser.d.ts +2 -0
- package/dist/parser.js +72 -11
- package/dist/repl.js +63 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +4 -2
- package/stdlib/API_DESIGN.md +357 -0
- package/stdlib/ASYNC_DESIGN.md +815 -0
- package/stdlib/EXAMPLES.md +710 -0
- package/stdlib/MODULES.md +854 -0
- package/stdlib/README.md +64 -0
- package/stdlib/collections.arc +140 -0
- package/stdlib/crypto.arc +62 -0
- package/stdlib/csv.arc +40 -0
- package/stdlib/datetime.arc +75 -0
- package/stdlib/embed.arc +42 -0
- package/stdlib/env.arc +10 -0
- package/stdlib/error.arc +36 -0
- package/stdlib/html.arc +30 -0
- package/stdlib/http.arc +43 -0
- package/stdlib/io.arc +28 -0
- package/stdlib/json.arc +204 -0
- package/stdlib/llm.arc +193 -0
- package/stdlib/log.arc +20 -0
- package/stdlib/map.arc +72 -0
- package/stdlib/math.arc +133 -0
- package/stdlib/net.arc +17 -0
- package/stdlib/os.arc +81 -0
- package/stdlib/path.arc +11 -0
- package/stdlib/prompt.arc +49 -0
- package/stdlib/regex.arc +59 -0
- package/stdlib/result.arc +50 -0
- package/stdlib/store.arc +62 -0
- package/stdlib/strings.arc +44 -0
- package/stdlib/test.arc +61 -0
- package/stdlib/time.arc +19 -0
- package/stdlib/toml.arc +10 -0
- package/stdlib/yaml.arc +10 -0
package/dist/ast.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export interface Loc {
|
|
|
2
2
|
line: number;
|
|
3
3
|
col: number;
|
|
4
4
|
}
|
|
5
|
-
export type Expr = IntLiteral | FloatLiteral | BoolLiteral | NilLiteral | StringLiteral | StringInterp | Identifier | BinaryExpr | UnaryExpr | CallExpr | MemberExpr | IndexExpr | PipelineExpr | IfExpr | MatchExpr | LambdaExpr | ListLiteral | MapLiteral | ListComprehension | ToolCallExpr | RangeExpr | BlockExpr | AsyncExpr | AwaitExpr | FetchExpr | SpreadExpr | OptionalMemberExpr | TryExpr;
|
|
5
|
+
export type Expr = IntLiteral | FloatLiteral | BoolLiteral | NilLiteral | StringLiteral | StringInterp | Identifier | BinaryExpr | UnaryExpr | CallExpr | MemberExpr | IndexExpr | PipelineExpr | IfExpr | MatchExpr | LambdaExpr | ListLiteral | MapLiteral | ListComprehension | ToolCallExpr | RangeExpr | BlockExpr | AsyncExpr | AwaitExpr | FetchExpr | SpreadExpr | OptionalMemberExpr | TryExpr | TryCatchExpr;
|
|
6
6
|
export interface IntLiteral {
|
|
7
7
|
kind: "IntLiteral";
|
|
8
8
|
value: number;
|
|
@@ -170,6 +170,13 @@ export interface TryExpr {
|
|
|
170
170
|
expr: Expr;
|
|
171
171
|
loc: Loc;
|
|
172
172
|
}
|
|
173
|
+
export interface TryCatchExpr {
|
|
174
|
+
kind: "TryCatchExpr";
|
|
175
|
+
body: Expr;
|
|
176
|
+
catchVar: string;
|
|
177
|
+
catchBody: Expr;
|
|
178
|
+
loc: Loc;
|
|
179
|
+
}
|
|
173
180
|
export type Pattern = WildcardPattern | LiteralPattern | BindingPattern | ArrayPattern | OrPattern | ConstructorPattern;
|
|
174
181
|
export interface WildcardPattern {
|
|
175
182
|
kind: "WildcardPattern";
|
|
@@ -201,7 +208,7 @@ export interface ConstructorPattern {
|
|
|
201
208
|
args: Pattern[];
|
|
202
209
|
loc: Loc;
|
|
203
210
|
}
|
|
204
|
-
export type Stmt = LetStmt | FnStmt | ForStmt | DoStmt | ExprStmt | UseStmt | TypeStmt | AssignStmt | MemberAssignStmt | IndexAssignStmt | RetStmt;
|
|
211
|
+
export type Stmt = LetStmt | FnStmt | ForStmt | DoStmt | WhileStmt | ExprStmt | UseStmt | TypeStmt | AssignStmt | MemberAssignStmt | IndexAssignStmt | RetStmt | BreakStmt | ContinueStmt | TryCatchStmt;
|
|
205
212
|
export interface AssignStmt {
|
|
206
213
|
kind: "AssignStmt";
|
|
207
214
|
target: string;
|
|
@@ -270,6 +277,27 @@ export interface DoStmt {
|
|
|
270
277
|
isWhile: boolean;
|
|
271
278
|
loc: Loc;
|
|
272
279
|
}
|
|
280
|
+
export interface WhileStmt {
|
|
281
|
+
kind: "WhileStmt";
|
|
282
|
+
condition: Expr;
|
|
283
|
+
body: Expr;
|
|
284
|
+
loc: Loc;
|
|
285
|
+
}
|
|
286
|
+
export interface BreakStmt {
|
|
287
|
+
kind: "BreakStmt";
|
|
288
|
+
loc: Loc;
|
|
289
|
+
}
|
|
290
|
+
export interface ContinueStmt {
|
|
291
|
+
kind: "ContinueStmt";
|
|
292
|
+
loc: Loc;
|
|
293
|
+
}
|
|
294
|
+
export interface TryCatchStmt {
|
|
295
|
+
kind: "TryCatchStmt";
|
|
296
|
+
body: Expr;
|
|
297
|
+
catchVar: string;
|
|
298
|
+
catchBody: Expr;
|
|
299
|
+
loc: Loc;
|
|
300
|
+
}
|
|
273
301
|
export interface ExprStmt {
|
|
274
302
|
kind: "ExprStmt";
|
|
275
303
|
expr: Expr;
|
package/dist/build.js
CHANGED
|
@@ -127,7 +127,7 @@ export function newProject(name, parentDir) {
|
|
|
127
127
|
"dev-dependencies": {},
|
|
128
128
|
};
|
|
129
129
|
writeFileSync(resolve(projectDir, "arc.toml"), serializeArcToml(toml));
|
|
130
|
-
writeFileSync(resolve(projectDir, "src", "main.arc"),
|
|
130
|
+
writeFileSync(resolve(projectDir, "src", "main.arc"), `# Hello from ${name}!\nlet msg = "Hello from ${name}!"\nprint(msg)\n`);
|
|
131
131
|
writeFileSync(resolve(projectDir, "tests", "main.test.arc"), `fn test_main() {\n let x = 1 + 1\n print(x)\n}\n`);
|
|
132
132
|
writeFileSync(resolve(projectDir, "README.md"), `# ${name}\n\nAn Arc project.\n\n## Getting Started\n\n\`\`\`bash\narc build\narc run\n\`\`\`\n`);
|
|
133
133
|
console.log(`Created project '${name}'`);
|
package/dist/index.js
CHANGED
|
@@ -101,6 +101,108 @@ else if (command === "pkg") {
|
|
|
101
101
|
process.exit(1);
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
+
else if (command === "builtins") {
|
|
105
|
+
console.log("Arc Built-in Functions\n");
|
|
106
|
+
console.log("I/O:");
|
|
107
|
+
console.log(" print(...values) Print values to stdout\n");
|
|
108
|
+
console.log("Type Conversion:");
|
|
109
|
+
console.log(" int(v) Convert to integer (throws on bad input)");
|
|
110
|
+
console.log(" float(v) Convert to float (throws on bad input)");
|
|
111
|
+
console.log(" str(v) Convert to string");
|
|
112
|
+
console.log(" bool(v) Convert to boolean");
|
|
113
|
+
console.log(" type_of(v) Get type: \"int\" \"float\" \"string\" \"bool\" \"list\" \"map\" \"fn\" \"nil\"\n");
|
|
114
|
+
console.log("Strings:");
|
|
115
|
+
console.log(" len(s) Length (codepoints, not bytes)");
|
|
116
|
+
console.log(" trim(s) Strip whitespace");
|
|
117
|
+
console.log(" upper(s) / lower(s) Case conversion");
|
|
118
|
+
console.log(" split(s, sep) Split into list");
|
|
119
|
+
console.log(" join(list, sep) Join list into string");
|
|
120
|
+
console.log(" replace(s, old, new) Replace all occurrences");
|
|
121
|
+
console.log(" contains(s, sub) Check substring");
|
|
122
|
+
console.log(" starts(s, prefix) Starts with");
|
|
123
|
+
console.log(" ends(s, suffix) Ends with");
|
|
124
|
+
console.log(" repeat(s, n) Repeat string n times");
|
|
125
|
+
console.log(" chars(s) Split into character list");
|
|
126
|
+
console.log(" slice(s, start, end?) Substring");
|
|
127
|
+
console.log(" index_of(s, sub) Find index (nil if not found)");
|
|
128
|
+
console.log(" ord(s) Char to code point");
|
|
129
|
+
console.log(" chr(n) Code point to char");
|
|
130
|
+
console.log(" char_at(s, i) Character at index\n");
|
|
131
|
+
console.log("Lists:");
|
|
132
|
+
console.log(" len(list) Length");
|
|
133
|
+
console.log(" map(list, fn) Transform each element");
|
|
134
|
+
console.log(" filter(list, fn) Keep elements matching predicate");
|
|
135
|
+
console.log(" reduce(list, fn, init) Fold left");
|
|
136
|
+
console.log(" fold(list, init, fn) Fold (init-first arg order)");
|
|
137
|
+
console.log(" find(list, fn) First matching element");
|
|
138
|
+
console.log(" any(list, fn) Any element matches?");
|
|
139
|
+
console.log(" all(list, fn) All elements match?");
|
|
140
|
+
console.log(" sort(list) Sort (numbers or strings)");
|
|
141
|
+
console.log(" head(list) First element");
|
|
142
|
+
console.log(" tail(list) All but first");
|
|
143
|
+
console.log(" last(list) Last element");
|
|
144
|
+
console.log(" reverse(list) Reverse");
|
|
145
|
+
console.log(" take(list, n) First n elements");
|
|
146
|
+
console.log(" drop(list, n) Skip first n");
|
|
147
|
+
console.log(" flat(list) Flatten nested lists");
|
|
148
|
+
console.log(" zip(a, b) Zip two lists");
|
|
149
|
+
console.log(" enumerate(list) Add indices: [[0,a],[1,b],...]");
|
|
150
|
+
console.log(" push(list, item) Append (returns new list)");
|
|
151
|
+
console.log(" concat(a, b) Concatenate lists");
|
|
152
|
+
console.log(" sum(list) Sum numbers");
|
|
153
|
+
console.log(" range(a, b) Generate [a, a+1, ..., b-1]\n");
|
|
154
|
+
console.log("Maps:");
|
|
155
|
+
console.log(" keys(map) Get keys as list");
|
|
156
|
+
console.log(" values(map) Get values as list");
|
|
157
|
+
console.log(" entries(map) Key-value pairs\n");
|
|
158
|
+
console.log("Math:");
|
|
159
|
+
console.log(" abs(n) Absolute value");
|
|
160
|
+
console.log(" min(...) / max(...) Min/max (args or list)");
|
|
161
|
+
console.log(" round(n) Round to integer\n");
|
|
162
|
+
console.log("Other:");
|
|
163
|
+
console.log(" assert(cond, msg?) Assert truth (throws on false)");
|
|
164
|
+
console.log(" time_ms() Unix timestamp in milliseconds\n");
|
|
165
|
+
console.log("Operators: + - * / % ** ++ == != < > <= >= and or not |> .. ?.");
|
|
166
|
+
console.log("Strings: \"text {expr}\" (interpolation) \"ha\" * 3 (repetition)");
|
|
167
|
+
console.log("Comments: # or //");
|
|
168
|
+
console.log("Errors: try { ... } catch e { ... }\n");
|
|
169
|
+
console.log("Stdlib: arc builtins --modules (list all standard library modules)");
|
|
170
|
+
if (args.includes("--modules")) {
|
|
171
|
+
console.log("\nStandard Library Modules: (import with: use <module>)\n");
|
|
172
|
+
const mods = [
|
|
173
|
+
["math", "sqrt, pow, ceil, floor, clamp, PI, E, sin, cos, log, 25 functions"],
|
|
174
|
+
["strings", "pad_left, pad_right, capitalize, words"],
|
|
175
|
+
["collections", "group_by, chunk, flatten, zip_with, partition, sort_by, unique"],
|
|
176
|
+
["map", "merge, map_values, filter_map, from_pairs, pick, omit"],
|
|
177
|
+
["io", "read_file, write_file, read_lines, exists, append"],
|
|
178
|
+
["http", "get, post, put, delete — real HTTP requests"],
|
|
179
|
+
["json", "to_json, from_json, pretty, get_path"],
|
|
180
|
+
["csv", "parse_csv, to_csv, parse_csv_headers"],
|
|
181
|
+
["regex", "match, find, test, replace, split, capture"],
|
|
182
|
+
["datetime", "now, today, parse, format, add_days, diff_days"],
|
|
183
|
+
["os", "cwd, list_dir, mkdir, exec, platform, env, remove, copy"],
|
|
184
|
+
["env", "get, set, has, all — environment variables"],
|
|
185
|
+
["crypto", "sha256, sha512, hmac_sha256, uuid, random_bytes"],
|
|
186
|
+
["error", "try_catch, try_finally, throw, retry, assert"],
|
|
187
|
+
["result", "ok, err, is_ok, unwrap, map_result, try_fn"],
|
|
188
|
+
["net", "ws_connect, tcp_connect, dns_lookup, base64_encode"],
|
|
189
|
+
["yaml", "parse, stringify"],
|
|
190
|
+
["toml", "parse, stringify"],
|
|
191
|
+
["html", "parse, create_element, to_html"],
|
|
192
|
+
["path", "join, dirname, basename, extname"],
|
|
193
|
+
["log", "info, warn, error, debug"],
|
|
194
|
+
["store", "get, set, delete — persistent key-value storage"],
|
|
195
|
+
["test", "describe, it, expect_eq, run_tests"],
|
|
196
|
+
["prompt", "template, count_tokens, window"],
|
|
197
|
+
["embed", "similarity, cosine, search"],
|
|
198
|
+
["llm", "chat, complete — multi-provider LLM API"],
|
|
199
|
+
];
|
|
200
|
+
for (const [name, desc] of mods) {
|
|
201
|
+
console.log(` ${name.padEnd(14)} ${desc}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
process.exit(0);
|
|
205
|
+
}
|
|
104
206
|
else if (command === "help" || command === "--help" || command === "-h" || !command || !file) {
|
|
105
207
|
console.log(`Arc ${ARC_VERSION} — A programming language designed by AI agents, for AI agents.\n`);
|
|
106
208
|
console.log("Usage: arc <command> [options]\n");
|
|
@@ -117,6 +219,8 @@ else if (command === "help" || command === "--help" || command === "-h" || !comm
|
|
|
117
219
|
console.log(" build Build the current project");
|
|
118
220
|
console.log(" test Run project tests");
|
|
119
221
|
console.log(" new <name> Create a new project");
|
|
222
|
+
console.log(" builtins List all built-in functions");
|
|
223
|
+
console.log(" builtins --modules List all standard library modules");
|
|
120
224
|
console.log(" pkg <sub> Package manager (init|add|remove|list|install)");
|
|
121
225
|
console.log(" version Print version info");
|
|
122
226
|
console.log("\nOptions:");
|
package/dist/interpreter.js
CHANGED
|
@@ -182,7 +182,7 @@ function makePrelude(env) {
|
|
|
182
182
|
print: (...args) => { console.log(args.map(toStr).join(" ")); return null; },
|
|
183
183
|
len: (v) => {
|
|
184
184
|
if (typeof v === "string")
|
|
185
|
-
return v.length;
|
|
185
|
+
return [...v].length; // codepoint count, not UTF-16
|
|
186
186
|
if (Array.isArray(v))
|
|
187
187
|
return v.length;
|
|
188
188
|
if (v && typeof v === "object" && "__map" in v)
|
|
@@ -276,8 +276,28 @@ function makePrelude(env) {
|
|
|
276
276
|
},
|
|
277
277
|
starts: (s, pre) => typeof s === "string" ? s.startsWith(pre) : false,
|
|
278
278
|
ends: (s, suf) => typeof s === "string" ? s.endsWith(suf) : false,
|
|
279
|
-
int: (v) =>
|
|
280
|
-
|
|
279
|
+
int: (v) => {
|
|
280
|
+
if (typeof v === "number")
|
|
281
|
+
return Math.floor(v);
|
|
282
|
+
if (typeof v === "string") {
|
|
283
|
+
const n = parseInt(v);
|
|
284
|
+
if (isNaN(n))
|
|
285
|
+
throw new ArcRuntimeError(`ValueError: cannot convert '${v}' to int`, { code: ErrorCode.TYPE_MISMATCH });
|
|
286
|
+
return n;
|
|
287
|
+
}
|
|
288
|
+
return 0;
|
|
289
|
+
},
|
|
290
|
+
float: (v) => {
|
|
291
|
+
if (typeof v === "number")
|
|
292
|
+
return v;
|
|
293
|
+
if (typeof v === "string") {
|
|
294
|
+
const n = parseFloat(v);
|
|
295
|
+
if (isNaN(n))
|
|
296
|
+
throw new ArcRuntimeError(`ValueError: cannot convert '${v}' to float`, { code: ErrorCode.TYPE_MISMATCH });
|
|
297
|
+
return n;
|
|
298
|
+
}
|
|
299
|
+
return 0;
|
|
300
|
+
},
|
|
281
301
|
str: (v) => toStr(v),
|
|
282
302
|
bool: (v) => isTruthy(v),
|
|
283
303
|
min: (...args) => {
|
|
@@ -2361,6 +2381,11 @@ class ReturnSignal {
|
|
|
2361
2381
|
this.value = value;
|
|
2362
2382
|
}
|
|
2363
2383
|
}
|
|
2384
|
+
// Break/Continue signals for loops
|
|
2385
|
+
class BreakSignal {
|
|
2386
|
+
}
|
|
2387
|
+
class ContinueSignal {
|
|
2388
|
+
}
|
|
2364
2389
|
// Evaluate expression in tail position — returns TCOSignal for self-recursive tail calls
|
|
2365
2390
|
function evalExprTCO(expr, env, fnName) {
|
|
2366
2391
|
// Only handle tail-position expressions specially
|
|
@@ -2469,20 +2494,14 @@ function evalExpr(expr, env) {
|
|
|
2469
2494
|
if (typeof left !== "number" || typeof right !== "number")
|
|
2470
2495
|
throw new ArcRuntimeError(`TypeError: cannot divide non-numbers`, { code: ErrorCode.INVALID_OPERATOR, loc: expr.loc });
|
|
2471
2496
|
if (right === 0)
|
|
2472
|
-
|
|
2473
|
-
code: ErrorCode.DIVISION_BY_ZERO, loc: expr.loc,
|
|
2474
|
-
suggestion: "Check that the divisor is not zero before dividing.",
|
|
2475
|
-
});
|
|
2497
|
+
return left === 0 ? null : (left > 0 ? Infinity : -Infinity);
|
|
2476
2498
|
return left / right;
|
|
2477
2499
|
}
|
|
2478
2500
|
case "%": {
|
|
2479
2501
|
if (typeof left !== "number" || typeof right !== "number")
|
|
2480
2502
|
throw new ArcRuntimeError(`TypeError: cannot modulo non-numbers`, { code: ErrorCode.INVALID_OPERATOR, loc: expr.loc });
|
|
2481
2503
|
if (right === 0)
|
|
2482
|
-
|
|
2483
|
-
code: ErrorCode.DIVISION_BY_ZERO, loc: expr.loc,
|
|
2484
|
-
suggestion: "Check that the divisor is not zero before dividing.",
|
|
2485
|
-
});
|
|
2504
|
+
return null;
|
|
2486
2505
|
return left % right;
|
|
2487
2506
|
}
|
|
2488
2507
|
case "**": {
|
|
@@ -2576,8 +2595,24 @@ function evalExpr(expr, env) {
|
|
|
2576
2595
|
if (obj && typeof obj === "object" && "__map" in obj) {
|
|
2577
2596
|
return obj.entries.get(expr.property) ?? null;
|
|
2578
2597
|
}
|
|
2579
|
-
|
|
2580
|
-
|
|
2598
|
+
// Teaching error messages for common method-style access
|
|
2599
|
+
const prop = expr.property;
|
|
2600
|
+
const methodBuiltins = {
|
|
2601
|
+
length: "len(value)", repeat: "repeat(str, n)", trim: "trim(str)",
|
|
2602
|
+
split: "split(str, sep)", join: "join(list, sep)", replace: "replace(str, old, new)",
|
|
2603
|
+
includes: "contains(value, sub)", startsWith: "starts(str, prefix)", endsWith: "ends(str, suffix)",
|
|
2604
|
+
toUpperCase: "upper(str)", toLowerCase: "lower(str)", indexOf: "index_of(str, sub)",
|
|
2605
|
+
slice: "slice(value, start, end)", map: "map(list, fn)", filter: "filter(list, fn)",
|
|
2606
|
+
reduce: "reduce(list, fn, init)", find: "find(list, fn)", sort: "sort(list)",
|
|
2607
|
+
reverse: "reverse(list)", push: "push(list, item)", concat: "concat(a, b)",
|
|
2608
|
+
forEach: "for item in list { ... }", charAt: "char_at(str, i)",
|
|
2609
|
+
toString: "str(value)", match: "use regex; regex.find(str, pattern)",
|
|
2610
|
+
};
|
|
2611
|
+
const suggestion = methodBuiltins[prop]
|
|
2612
|
+
? `Arc uses free functions, not methods. Try: ${methodBuiltins[prop]}`
|
|
2613
|
+
: undefined;
|
|
2614
|
+
throw new ArcRuntimeError(`Cannot access property '${prop}' on ${toStr(obj)}`, {
|
|
2615
|
+
code: ErrorCode.PROPERTY_ACCESS, loc: expr.loc, suggestion,
|
|
2581
2616
|
});
|
|
2582
2617
|
}
|
|
2583
2618
|
case "OptionalMemberExpr": {
|
|
@@ -2606,6 +2641,23 @@ function evalExpr(expr, env) {
|
|
|
2606
2641
|
// Not a Result type — just return the value
|
|
2607
2642
|
return val;
|
|
2608
2643
|
}
|
|
2644
|
+
case "TryCatchExpr": {
|
|
2645
|
+
try {
|
|
2646
|
+
return evalExpr(expr.body, env);
|
|
2647
|
+
}
|
|
2648
|
+
catch (e) {
|
|
2649
|
+
if (e instanceof ReturnSignal)
|
|
2650
|
+
throw e;
|
|
2651
|
+
if (e instanceof BreakSignal)
|
|
2652
|
+
throw e;
|
|
2653
|
+
if (e instanceof ContinueSignal)
|
|
2654
|
+
throw e;
|
|
2655
|
+
const catchEnv = new Env(env);
|
|
2656
|
+
const errMsg = e instanceof Error ? e.message : String(e);
|
|
2657
|
+
catchEnv.set(expr.catchVar, errMsg);
|
|
2658
|
+
return evalExpr(expr.catchBody, catchEnv);
|
|
2659
|
+
}
|
|
2660
|
+
}
|
|
2609
2661
|
case "IndexExpr": {
|
|
2610
2662
|
const obj = evalExpr(expr.object, env);
|
|
2611
2663
|
const idx = evalExpr(expr.index, env);
|
|
@@ -2786,10 +2838,11 @@ function evalExpr(expr, env) {
|
|
|
2786
2838
|
return resolveAsync(val);
|
|
2787
2839
|
}
|
|
2788
2840
|
case "FetchExpr": {
|
|
2789
|
-
|
|
2841
|
+
const results = expr.targets.map(t => {
|
|
2790
2842
|
const val = evalExpr(t, env);
|
|
2791
2843
|
return resolveAsync(val);
|
|
2792
2844
|
});
|
|
2845
|
+
return results.length === 1 ? results[0] : results;
|
|
2793
2846
|
}
|
|
2794
2847
|
case "BlockExpr": {
|
|
2795
2848
|
const blockEnv = new Env(env);
|
|
@@ -2894,14 +2947,72 @@ function evalStmt(stmt, env) {
|
|
|
2894
2947
|
loopEnv.set(n, m.get(n) ?? null);
|
|
2895
2948
|
}
|
|
2896
2949
|
}
|
|
2897
|
-
|
|
2950
|
+
try {
|
|
2951
|
+
result = evalExpr(stmt.body, loopEnv);
|
|
2952
|
+
}
|
|
2953
|
+
catch (e) {
|
|
2954
|
+
if (e instanceof BreakSignal)
|
|
2955
|
+
break;
|
|
2956
|
+
if (e instanceof ContinueSignal)
|
|
2957
|
+
continue;
|
|
2958
|
+
throw e;
|
|
2959
|
+
}
|
|
2898
2960
|
}
|
|
2899
2961
|
return result;
|
|
2900
2962
|
}
|
|
2963
|
+
case "WhileStmt": {
|
|
2964
|
+
let result = null;
|
|
2965
|
+
while (isTruthy(evalExpr(stmt.condition, env))) {
|
|
2966
|
+
try {
|
|
2967
|
+
result = evalExpr(stmt.body, env);
|
|
2968
|
+
}
|
|
2969
|
+
catch (e) {
|
|
2970
|
+
if (e instanceof BreakSignal)
|
|
2971
|
+
break;
|
|
2972
|
+
if (e instanceof ContinueSignal)
|
|
2973
|
+
continue;
|
|
2974
|
+
throw e;
|
|
2975
|
+
}
|
|
2976
|
+
}
|
|
2977
|
+
return result;
|
|
2978
|
+
}
|
|
2979
|
+
case "TryCatchStmt": {
|
|
2980
|
+
try {
|
|
2981
|
+
return evalExpr(stmt.body, env);
|
|
2982
|
+
}
|
|
2983
|
+
catch (e) {
|
|
2984
|
+
if (e instanceof ReturnSignal)
|
|
2985
|
+
throw e; // don't catch return/break/continue
|
|
2986
|
+
if (e instanceof BreakSignal)
|
|
2987
|
+
throw e;
|
|
2988
|
+
if (e instanceof ContinueSignal)
|
|
2989
|
+
throw e;
|
|
2990
|
+
const catchEnv = new Env(env);
|
|
2991
|
+
const errMsg = e instanceof Error ? e.message : String(e);
|
|
2992
|
+
catchEnv.set(stmt.catchVar, errMsg);
|
|
2993
|
+
return evalExpr(stmt.catchBody, catchEnv);
|
|
2994
|
+
}
|
|
2995
|
+
}
|
|
2901
2996
|
case "DoStmt": {
|
|
2902
2997
|
let result = null;
|
|
2903
2998
|
do {
|
|
2904
|
-
|
|
2999
|
+
try {
|
|
3000
|
+
result = evalExpr(stmt.body, env);
|
|
3001
|
+
}
|
|
3002
|
+
catch (e) {
|
|
3003
|
+
if (e instanceof BreakSignal) {
|
|
3004
|
+
break;
|
|
3005
|
+
}
|
|
3006
|
+
if (e instanceof ContinueSignal) {
|
|
3007
|
+
const cond = evalExpr(stmt.condition, env);
|
|
3008
|
+
if (stmt.isWhile && !isTruthy(cond))
|
|
3009
|
+
break;
|
|
3010
|
+
if (!stmt.isWhile && isTruthy(cond))
|
|
3011
|
+
break;
|
|
3012
|
+
continue;
|
|
3013
|
+
}
|
|
3014
|
+
throw e;
|
|
3015
|
+
}
|
|
2905
3016
|
const cond = evalExpr(stmt.condition, env);
|
|
2906
3017
|
if (stmt.isWhile && !isTruthy(cond))
|
|
2907
3018
|
break;
|
|
@@ -2910,6 +3021,8 @@ function evalStmt(stmt, env) {
|
|
|
2910
3021
|
} while (true);
|
|
2911
3022
|
return result;
|
|
2912
3023
|
}
|
|
3024
|
+
case "BreakStmt": throw new BreakSignal();
|
|
3025
|
+
case "ContinueStmt": throw new ContinueSignal();
|
|
2913
3026
|
case "AssignStmt": {
|
|
2914
3027
|
const value = evalExpr(stmt.value, env);
|
|
2915
3028
|
env.assign(stmt.target, value);
|
package/dist/lexer.d.ts
CHANGED
|
@@ -34,43 +34,47 @@ export declare enum TokenType {
|
|
|
34
34
|
Where = 32,
|
|
35
35
|
Matching = 33,
|
|
36
36
|
Fetch = 34,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
37
|
+
Break = 35,
|
|
38
|
+
Continue = 36,
|
|
39
|
+
Try = 37,
|
|
40
|
+
Catch = 38,
|
|
41
|
+
Plus = 39,
|
|
42
|
+
Minus = 40,
|
|
43
|
+
Star = 41,
|
|
44
|
+
Slash = 42,
|
|
45
|
+
Percent = 43,
|
|
46
|
+
Power = 44,
|
|
47
|
+
Eq = 45,
|
|
48
|
+
Neq = 46,
|
|
49
|
+
Lt = 47,
|
|
50
|
+
Gt = 48,
|
|
51
|
+
Lte = 49,
|
|
52
|
+
Gte = 50,
|
|
53
|
+
Pipe = 51,
|
|
54
|
+
Bar = 52,
|
|
55
|
+
FatArrow = 53,
|
|
56
|
+
Arrow = 54,
|
|
57
|
+
Question = 55,
|
|
58
|
+
QuestionDot = 56,
|
|
59
|
+
Range = 57,
|
|
60
|
+
Concat = 58,
|
|
61
|
+
At = 59,
|
|
62
|
+
Hash = 60,
|
|
63
|
+
DotDotDot = 61,
|
|
64
|
+
Assign = 62,
|
|
65
|
+
LParen = 63,
|
|
66
|
+
RParen = 64,
|
|
67
|
+
LBrace = 65,
|
|
68
|
+
RBrace = 66,
|
|
69
|
+
LBracket = 67,
|
|
70
|
+
RBracket = 68,
|
|
71
|
+
Comma = 69,
|
|
72
|
+
Colon = 70,
|
|
73
|
+
Dot = 71,
|
|
74
|
+
Semicolon = 72,
|
|
75
|
+
Newline = 73,
|
|
76
|
+
Regex = 74,
|
|
77
|
+
EOF = 75
|
|
74
78
|
}
|
|
75
79
|
export interface Token {
|
|
76
80
|
type: TokenType;
|
package/dist/lexer.js
CHANGED
|
@@ -38,47 +38,51 @@ export var TokenType;
|
|
|
38
38
|
TokenType[TokenType["Where"] = 32] = "Where";
|
|
39
39
|
TokenType[TokenType["Matching"] = 33] = "Matching";
|
|
40
40
|
TokenType[TokenType["Fetch"] = 34] = "Fetch";
|
|
41
|
+
TokenType[TokenType["Break"] = 35] = "Break";
|
|
42
|
+
TokenType[TokenType["Continue"] = 36] = "Continue";
|
|
43
|
+
TokenType[TokenType["Try"] = 37] = "Try";
|
|
44
|
+
TokenType[TokenType["Catch"] = 38] = "Catch";
|
|
41
45
|
// Operators
|
|
42
|
-
TokenType[TokenType["Plus"] =
|
|
43
|
-
TokenType[TokenType["Minus"] =
|
|
44
|
-
TokenType[TokenType["Star"] =
|
|
45
|
-
TokenType[TokenType["Slash"] =
|
|
46
|
-
TokenType[TokenType["Percent"] =
|
|
47
|
-
TokenType[TokenType["Power"] =
|
|
48
|
-
TokenType[TokenType["Eq"] =
|
|
49
|
-
TokenType[TokenType["Neq"] =
|
|
50
|
-
TokenType[TokenType["Lt"] =
|
|
51
|
-
TokenType[TokenType["Gt"] =
|
|
52
|
-
TokenType[TokenType["Lte"] =
|
|
53
|
-
TokenType[TokenType["Gte"] =
|
|
54
|
-
TokenType[TokenType["Pipe"] =
|
|
55
|
-
TokenType[TokenType["Bar"] =
|
|
56
|
-
TokenType[TokenType["FatArrow"] =
|
|
57
|
-
TokenType[TokenType["Arrow"] =
|
|
58
|
-
TokenType[TokenType["Question"] =
|
|
59
|
-
TokenType[TokenType["QuestionDot"] =
|
|
60
|
-
TokenType[TokenType["Range"] =
|
|
61
|
-
TokenType[TokenType["Concat"] =
|
|
62
|
-
TokenType[TokenType["At"] =
|
|
63
|
-
TokenType[TokenType["Hash"] =
|
|
64
|
-
TokenType[TokenType["DotDotDot"] =
|
|
65
|
-
TokenType[TokenType["Assign"] =
|
|
46
|
+
TokenType[TokenType["Plus"] = 39] = "Plus";
|
|
47
|
+
TokenType[TokenType["Minus"] = 40] = "Minus";
|
|
48
|
+
TokenType[TokenType["Star"] = 41] = "Star";
|
|
49
|
+
TokenType[TokenType["Slash"] = 42] = "Slash";
|
|
50
|
+
TokenType[TokenType["Percent"] = 43] = "Percent";
|
|
51
|
+
TokenType[TokenType["Power"] = 44] = "Power";
|
|
52
|
+
TokenType[TokenType["Eq"] = 45] = "Eq";
|
|
53
|
+
TokenType[TokenType["Neq"] = 46] = "Neq";
|
|
54
|
+
TokenType[TokenType["Lt"] = 47] = "Lt";
|
|
55
|
+
TokenType[TokenType["Gt"] = 48] = "Gt";
|
|
56
|
+
TokenType[TokenType["Lte"] = 49] = "Lte";
|
|
57
|
+
TokenType[TokenType["Gte"] = 50] = "Gte";
|
|
58
|
+
TokenType[TokenType["Pipe"] = 51] = "Pipe";
|
|
59
|
+
TokenType[TokenType["Bar"] = 52] = "Bar";
|
|
60
|
+
TokenType[TokenType["FatArrow"] = 53] = "FatArrow";
|
|
61
|
+
TokenType[TokenType["Arrow"] = 54] = "Arrow";
|
|
62
|
+
TokenType[TokenType["Question"] = 55] = "Question";
|
|
63
|
+
TokenType[TokenType["QuestionDot"] = 56] = "QuestionDot";
|
|
64
|
+
TokenType[TokenType["Range"] = 57] = "Range";
|
|
65
|
+
TokenType[TokenType["Concat"] = 58] = "Concat";
|
|
66
|
+
TokenType[TokenType["At"] = 59] = "At";
|
|
67
|
+
TokenType[TokenType["Hash"] = 60] = "Hash";
|
|
68
|
+
TokenType[TokenType["DotDotDot"] = 61] = "DotDotDot";
|
|
69
|
+
TokenType[TokenType["Assign"] = 62] = "Assign";
|
|
66
70
|
// Delimiters
|
|
67
|
-
TokenType[TokenType["LParen"] =
|
|
68
|
-
TokenType[TokenType["RParen"] =
|
|
69
|
-
TokenType[TokenType["LBrace"] =
|
|
70
|
-
TokenType[TokenType["RBrace"] =
|
|
71
|
-
TokenType[TokenType["LBracket"] =
|
|
72
|
-
TokenType[TokenType["RBracket"] =
|
|
73
|
-
TokenType[TokenType["Comma"] =
|
|
74
|
-
TokenType[TokenType["Colon"] =
|
|
75
|
-
TokenType[TokenType["Dot"] =
|
|
76
|
-
TokenType[TokenType["Semicolon"] =
|
|
77
|
-
TokenType[TokenType["Newline"] =
|
|
71
|
+
TokenType[TokenType["LParen"] = 63] = "LParen";
|
|
72
|
+
TokenType[TokenType["RParen"] = 64] = "RParen";
|
|
73
|
+
TokenType[TokenType["LBrace"] = 65] = "LBrace";
|
|
74
|
+
TokenType[TokenType["RBrace"] = 66] = "RBrace";
|
|
75
|
+
TokenType[TokenType["LBracket"] = 67] = "LBracket";
|
|
76
|
+
TokenType[TokenType["RBracket"] = 68] = "RBracket";
|
|
77
|
+
TokenType[TokenType["Comma"] = 69] = "Comma";
|
|
78
|
+
TokenType[TokenType["Colon"] = 70] = "Colon";
|
|
79
|
+
TokenType[TokenType["Dot"] = 71] = "Dot";
|
|
80
|
+
TokenType[TokenType["Semicolon"] = 72] = "Semicolon";
|
|
81
|
+
TokenType[TokenType["Newline"] = 73] = "Newline";
|
|
78
82
|
// Regex
|
|
79
|
-
TokenType[TokenType["Regex"] =
|
|
83
|
+
TokenType[TokenType["Regex"] = 74] = "Regex";
|
|
80
84
|
// Special
|
|
81
|
-
TokenType[TokenType["EOF"] =
|
|
85
|
+
TokenType[TokenType["EOF"] = 75] = "EOF";
|
|
82
86
|
})(TokenType || (TokenType = {}));
|
|
83
87
|
const KEYWORDS = {
|
|
84
88
|
fn: TokenType.Fn, let: TokenType.Let, mut: TokenType.Mut, type: TokenType.Type,
|
|
@@ -89,6 +93,10 @@ const KEYWORDS = {
|
|
|
89
93
|
true: TokenType.True, false: TokenType.False, nil: TokenType.NilKw,
|
|
90
94
|
and: TokenType.And, or: TokenType.Or, not: TokenType.Not,
|
|
91
95
|
where: TokenType.Where, matching: TokenType.Matching, fetch: TokenType.Fetch,
|
|
96
|
+
break: TokenType.Break, continue: TokenType.Continue,
|
|
97
|
+
return: TokenType.Ret, // alias: Arc uses 'ret' but accept 'return' too
|
|
98
|
+
else: TokenType.El, // alias: Arc uses 'el' but accept 'else' too
|
|
99
|
+
try: TokenType.Try, catch: TokenType.Catch,
|
|
92
100
|
};
|
|
93
101
|
export function lex(source) {
|
|
94
102
|
const tokens = [];
|
|
@@ -124,8 +132,8 @@ export function lex(source) {
|
|
|
124
132
|
tokens.push(tok(TokenType.Newline, "\\n", sl, sc));
|
|
125
133
|
continue;
|
|
126
134
|
}
|
|
127
|
-
// Comments: # to end of line
|
|
128
|
-
if (ch === "#") {
|
|
135
|
+
// Comments: # or // to end of line
|
|
136
|
+
if (ch === "#" || (ch === "/" && peek(1) === "/")) {
|
|
129
137
|
while (i < source.length && peek() !== "\n")
|
|
130
138
|
advance();
|
|
131
139
|
continue;
|
package/dist/modules.js
CHANGED
|
@@ -35,10 +35,14 @@ export function resolveModule(path, basePath) {
|
|
|
35
35
|
const relPath = resolve(dirname(basePath), modulePath);
|
|
36
36
|
if (existsSync(relPath))
|
|
37
37
|
return relPath;
|
|
38
|
-
// 3. Check compiler's sibling stdlib/
|
|
38
|
+
// 3. Check compiler's sibling stdlib/ (monorepo layout)
|
|
39
39
|
const compilerStdlib = resolve(__dirname2, "..", "..", "stdlib", modulePath);
|
|
40
40
|
if (existsSync(compilerStdlib))
|
|
41
41
|
return compilerStdlib;
|
|
42
|
+
// 4. Check stdlib bundled inside the npm package (npm install -g layout)
|
|
43
|
+
const bundledStdlib = resolve(__dirname2, "..", "stdlib", modulePath);
|
|
44
|
+
if (existsSync(bundledStdlib))
|
|
45
|
+
return bundledStdlib;
|
|
42
46
|
throw new Error(`Module not found: ${path.join("/")} (searched from ${basePath})`);
|
|
43
47
|
}
|
|
44
48
|
/**
|