arc-lang 0.5.4 → 0.5.6

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 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;
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;
6
6
  export interface IntLiteral {
7
7
  kind: "IntLiteral";
8
8
  value: number;
@@ -103,12 +103,19 @@ export interface ListLiteral {
103
103
  elements: Expr[];
104
104
  loc: Loc;
105
105
  }
106
+ export interface SpreadExpr {
107
+ kind: "SpreadExpr";
108
+ expr: Expr;
109
+ loc: Loc;
110
+ }
111
+ export interface MapEntry {
112
+ key?: string | Expr;
113
+ value?: Expr;
114
+ spread?: Expr;
115
+ }
106
116
  export interface MapLiteral {
107
117
  kind: "MapLiteral";
108
- entries: {
109
- key: string | Expr;
110
- value: Expr;
111
- }[];
118
+ entries: MapEntry[];
112
119
  loc: Loc;
113
120
  }
114
121
  export interface ListComprehension {
@@ -152,7 +159,18 @@ export interface FetchExpr {
152
159
  targets: Expr[];
153
160
  loc: Loc;
154
161
  }
155
- export type Pattern = WildcardPattern | LiteralPattern | BindingPattern | ArrayPattern | OrPattern;
162
+ export interface OptionalMemberExpr {
163
+ kind: "OptionalMemberExpr";
164
+ object: Expr;
165
+ property: string;
166
+ loc: Loc;
167
+ }
168
+ export interface TryExpr {
169
+ kind: "TryExpr";
170
+ expr: Expr;
171
+ loc: Loc;
172
+ }
173
+ export type Pattern = WildcardPattern | LiteralPattern | BindingPattern | ArrayPattern | OrPattern | ConstructorPattern;
156
174
  export interface WildcardPattern {
157
175
  kind: "WildcardPattern";
158
176
  loc: Loc;
@@ -177,6 +195,12 @@ export interface OrPattern {
177
195
  patterns: Pattern[];
178
196
  loc: Loc;
179
197
  }
198
+ export interface ConstructorPattern {
199
+ kind: "ConstructorPattern";
200
+ name: string;
201
+ args: Pattern[];
202
+ loc: Loc;
203
+ }
180
204
  export type Stmt = LetStmt | FnStmt | ForStmt | DoStmt | ExprStmt | UseStmt | TypeStmt | AssignStmt | MemberAssignStmt | IndexAssignStmt | RetStmt;
181
205
  export interface AssignStmt {
182
206
  kind: "AssignStmt";
@@ -215,11 +239,18 @@ export interface DestructureTarget {
215
239
  kind: "DestructureTarget";
216
240
  type: "object" | "array";
217
241
  names: string[];
242
+ rest?: string;
243
+ }
244
+ export interface Param {
245
+ name: string;
246
+ default?: Expr;
247
+ rest?: boolean;
218
248
  }
219
249
  export interface FnStmt {
220
250
  kind: "FnStmt";
221
251
  name: string;
222
252
  params: string[];
253
+ richParams?: Param[];
223
254
  body: Expr;
224
255
  isAsync: boolean;
225
256
  pub: boolean;
@@ -227,7 +258,7 @@ export interface FnStmt {
227
258
  }
228
259
  export interface ForStmt {
229
260
  kind: "ForStmt";
230
- variable: string;
261
+ variable: string | DestructureTarget;
231
262
  iterable: Expr;
232
263
  body: Expr;
233
264
  loc: Loc;
@@ -11,6 +11,7 @@ export function generateJS(module) {
11
11
  lines.push(` push(arr, v) { arr.push(v); return arr; },`);
12
12
  lines.push(` __toStr(v) { if (v === null) return "nil"; if (typeof v === "boolean") return v ? "true" : "false"; if (typeof v === "number" || typeof v === "string") return String(v); if (Array.isArray(v)) return "[" + v.map(x => this.__toStr(x)).join(", ") + "]"; if (v && v.__map) { const entries = [...v.entries.entries()].map(([k, val]) => k + ": " + this.__toStr(val)); return "{" + entries.join(", ") + "}"; } return String(v); },`);
13
13
  lines.push(` print(v) { console.log(this.__toStr(v)); },`);
14
+ lines.push(` println(v) { console.log(this.__toStr(v)); },`);
14
15
  lines.push(` head(a) { return a[0]; },`);
15
16
  lines.push(` tail(a) { return a.slice(1); },`);
16
17
  lines.push(` map(a, f) { return a.map(f); },`);
@@ -223,7 +224,7 @@ function emitUnop(op, operand) {
223
224
  }
224
225
  function emitCall(fn, args) {
225
226
  const builtins = [
226
- "len", "str", "push", "print", "head", "tail", "map", "filter", "fold",
227
+ "len", "str", "push", "print", "println", "head", "tail", "map", "filter", "fold",
227
228
  "range", "keys", "values", "type", "abs", "max", "min", "floor", "ceil",
228
229
  "round", "sort", "reverse", "contains", "join", "split", "trim", "replace",
229
230
  "uppercase", "lowercase", "sum", "flat", "zip", "enumerate", "slice",
package/dist/formatter.js CHANGED
@@ -204,6 +204,8 @@ export function format(source, options) {
204
204
  if (expr.entries.length === 0)
205
205
  return "{}";
206
206
  const entries = expr.entries.map(e => {
207
+ if (e.spread)
208
+ return `...${formatExpr(e.spread, depth + 1)}`;
207
209
  const key = typeof e.key === "string" ? e.key : formatExpr(e.key, depth + 1);
208
210
  return `${key}: ${formatExpr(e.value, depth + 1)}`;
209
211
  });
@@ -235,6 +237,10 @@ export function format(source, options) {
235
237
  const targets = expr.targets.map(t => formatExpr(t, depth)).join(", ");
236
238
  return `fetch [${targets}]`;
237
239
  }
240
+ case "SpreadExpr": return `...${formatExpr(expr.expr, depth)}`;
241
+ case "OptionalMemberExpr": return `${formatExpr(expr.object, depth)}?.${expr.property}`;
242
+ case "TryExpr": return `${formatExpr(expr.expr, depth)}?`;
243
+ default: return `/* unknown */`;
238
244
  }
239
245
  }
240
246
  function formatBlockExpr(expr, depth) {
@@ -272,6 +278,8 @@ export function format(source, options) {
272
278
  case "BindingPattern": return pat.name;
273
279
  case "ArrayPattern": return `[${pat.elements.map(formatPattern).join(", ")}]`;
274
280
  case "OrPattern": return pat.patterns.map(formatPattern).join(" | ");
281
+ case "ConstructorPattern": return `${pat.name}(${pat.args.map(formatPattern).join(", ")})`;
282
+ default: return "_";
275
283
  }
276
284
  }
277
285
  function formatTypeExpr(t) {
package/dist/index.js CHANGED
@@ -15,6 +15,8 @@ import { format } from "./formatter.js";
15
15
  import { lint, formatDiagnostic } from "./linter.js";
16
16
  import { printVersion } from "./version.js";
17
17
  import { setPrettyErrors } from "./errors.js";
18
+ import { build, test as buildTest, newProject } from "./build.js";
19
+ import { pkgInit, pkgAdd, pkgRemove, pkgList, pkgInstall } from "./package-manager.js";
18
20
  const args = process.argv.slice(2);
19
21
  if (args.includes("--no-pretty-errors"))
20
22
  setPrettyErrors(false);
@@ -51,6 +53,54 @@ else if (command === "fuzz") {
51
53
  process.exit(1);
52
54
  });
53
55
  }
56
+ else if (command === "build") {
57
+ const targetArg = args.find(a => a.startsWith("--target="))?.split("=")[1];
58
+ build({ target: targetArg, dir: process.cwd() });
59
+ }
60
+ else if (command === "test") {
61
+ buildTest(process.cwd());
62
+ }
63
+ else if (command === "new") {
64
+ const name = args[1];
65
+ if (!name) {
66
+ console.error("Usage: arc new <project-name>");
67
+ process.exit(1);
68
+ }
69
+ newProject(name);
70
+ }
71
+ else if (command === "pkg") {
72
+ const sub = args[1];
73
+ if (sub === "init") {
74
+ pkgInit();
75
+ }
76
+ else if (sub === "add") {
77
+ const pkg = args[2];
78
+ if (!pkg) {
79
+ console.error("Usage: arc pkg add <package>");
80
+ process.exit(1);
81
+ }
82
+ const dev = args.includes("--dev");
83
+ pkgAdd(pkg, { dev });
84
+ }
85
+ else if (sub === "remove") {
86
+ const pkg = args[2];
87
+ if (!pkg) {
88
+ console.error("Usage: arc pkg remove <package>");
89
+ process.exit(1);
90
+ }
91
+ pkgRemove(pkg);
92
+ }
93
+ else if (sub === "list") {
94
+ pkgList();
95
+ }
96
+ else if (sub === "install") {
97
+ pkgInstall();
98
+ }
99
+ else {
100
+ console.error("Usage: arc pkg <init|add|remove|list|install>");
101
+ process.exit(1);
102
+ }
103
+ }
54
104
  else if (!command || !file) {
55
105
  console.log("Usage:");
56
106
  console.log(" npx tsx src/index.ts run <file.arc> - Execute an Arc file");
@@ -12,6 +12,7 @@ interface FnValue {
12
12
  __fn: true;
13
13
  name: string;
14
14
  params: string[];
15
+ richParams?: AST.Param[];
15
16
  body: AST.Expr;
16
17
  closure: Env;
17
18
  }