bonescript-compiler 0.5.0 → 0.5.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.
Files changed (2) hide show
  1. package/README.md +382 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,382 @@
1
+ # bonescript-compiler
2
+
3
+ [![npm](https://img.shields.io/npm/v/bonescript-compiler)](https://www.npmjs.com/package/bonescript-compiler)
4
+ [![license](https://img.shields.io/npm/l/bonescript-compiler)](https://github.com/Doorman11991/BoneScript/blob/main/compiler/LICENSE)
5
+ [![node](https://img.shields.io/node/v/bonescript-compiler)](https://www.npmjs.com/package/bonescript-compiler)
6
+
7
+ A declarative language that compiles system descriptions into complete, runnable Node.js backends. Write the bones, get the whole skeleton.
8
+
9
+ ```bone
10
+ system Shop {
11
+ entity Product {
12
+ owns: [name: string, price: uint, stock: uint]
13
+ constraints: [price > 0, stock >= 0]
14
+ states: available -> sold_out | archived
15
+ }
16
+
17
+ capability purchase(buyer: User, product: Product, qty: uint) {
18
+ requires: [product.stock >= qty, buyer.balance >= product.price * qty]
19
+ effects: [product.stock -= qty, buyer.balance -= product.price * qty]
20
+ emits: OrderPlaced
21
+ sync: transactional
22
+ }
23
+
24
+ event OrderPlaced {
25
+ payload: { order_id: uuid, buyer_id: uuid, total: uint }
26
+ delivery: exactly_once
27
+ ttl: 90d
28
+ }
29
+ }
30
+ ```
31
+
32
+ Run `bonec compile shop.bone` and get back a complete Express API with PostgreSQL, auth middleware, state machine enforcement, transactional SQL, durable events, health checks, migrations, WebSocket support, a Dockerfile, and a GitHub Actions CI pipeline. No LLMs. Deterministic — same input always produces identical output.
33
+
34
+ ---
35
+
36
+ ## Install
37
+
38
+ ```bash
39
+ npm install -g bonescript-compiler
40
+ ```
41
+
42
+ Or run without installing:
43
+
44
+ ```bash
45
+ npx bonescript-compiler compile shop.bone
46
+ ```
47
+
48
+ Requires Node.js 18 or later.
49
+
50
+ ---
51
+
52
+ ## Quick Start
53
+
54
+ ```bash
55
+ # 1. Scaffold a new project
56
+ bonec init my-app --domain saas_platform
57
+
58
+ # 2. Compile
59
+ bonec compile my-app/my-app.bone
60
+
61
+ # 3. Configure
62
+ cp my-app/output/.env.example my-app/output/.env
63
+ # Edit .env — set JWT_SECRET, DATABASE_URL, etc.
64
+
65
+ # 4. Run
66
+ cd my-app/output
67
+ npm install
68
+ docker compose up -d # starts Postgres + Redis
69
+ npm run migrate
70
+ npm run dev
71
+ # → http://localhost:3000
72
+ ```
73
+
74
+ ---
75
+
76
+ ## What Gets Generated
77
+
78
+ From a single `.bone` file, the compiler produces a complete Node.js project:
79
+
80
+ ```
81
+ output/
82
+ ├── src/
83
+ │ ├── index.ts Express server, all routes wired
84
+ │ ├── db.ts Postgres connection pool
85
+ │ ├── auth.ts JWT / OAuth2 / API key middleware (domain-selected)
86
+ │ ├── events.ts Durable event bus (transactional outbox)
87
+ │ ├── publishers.ts Typed event publisher functions
88
+ │ ├── health.ts /health/live, /health/ready, /health/metrics
89
+ │ ├── logger.ts Structured JSON logging
90
+ │ ├── metrics.ts Prometheus-style counters/histograms
91
+ │ ├── failure_rules.ts Rule-based remediation
92
+ │ ├── flows.ts Saga runtime with backward compensation
93
+ │ ├── websocket.ts WebSocket server (if channels declared)
94
+ │ ├── channel_filters.ts Channel filter predicates (if filters declared)
95
+ │ ├── algorithms.ts Algorithm implementations (only what's used)
96
+ │ ├── extensions.ts Extension point stubs (preserved on recompile)
97
+ │ ├── routes/ One file per entity — CRUD + capabilities
98
+ │ ├── state_machines/ One file per entity with states
99
+ │ ├── models/ TypeScript interfaces + Zod validators
100
+ │ └── derived/ Derived field helpers (if derived fields declared)
101
+ ├── migrations/ SQL schemas, indexes, triggers, FK constraints
102
+ │ ├── <entity>.sql
103
+ │ ├── event_outbox.sql
104
+ │ └── api_keys.sql (apikey auth domains only)
105
+ ├── openapi.json OpenAPI 3.0 schema
106
+ ├── Dockerfile
107
+ ├── docker-compose.yaml Postgres + Redis for local dev
108
+ ├── k8s/deployment.yaml Kubernetes deployment manifest
109
+ ├── .github/workflows/ CI/CD pipeline
110
+ └── src/tests.ts Generated regression tests
111
+ ```
112
+
113
+ ---
114
+
115
+ ## Language Features
116
+
117
+ ### Entities
118
+
119
+ Stateful data objects with fields, constraints, state machines, and relations.
120
+
121
+ ```bone
122
+ entity Order {
123
+ owns: [buyer_id: uuid, total: uint, status: string]
124
+ constraints: [total > 0, status in ["pending", "paid", "shipped"]]
125
+ states: pending -> paid -> shipped -> delivered | cancelled
126
+ relation buyer: belongs_to User
127
+ }
128
+ ```
129
+
130
+ Auto-generated fields on every entity: `id: uuid`, `created_at: timestamp`, `updated_at: timestamp`.
131
+
132
+ ### Capabilities
133
+
134
+ Named operations with preconditions, effects, and event emissions.
135
+
136
+ ```bone
137
+ capability ship_order(seller: Seller, order: Order) {
138
+ requires: [order.status == "paid", order.seller_id == seller.id]
139
+ effects: [order.status = "shipped"]
140
+ emits: OrderShipped
141
+ sync: transactional
142
+ timeout: 10s
143
+ retry: { max_attempts: 3, backoff: exponential, interval: 1s }
144
+ }
145
+ ```
146
+
147
+ `sync` modes: `transactional` (BEGIN/COMMIT), `eventual` (outbox), `realtime` (WebSocket broadcast), `batch` (queued).
148
+
149
+ ### Events
150
+
151
+ Immutable records with delivery guarantees.
152
+
153
+ ```bone
154
+ event OrderShipped {
155
+ payload: { order_id: uuid, shipped_at: timestamp }
156
+ delivery: exactly_once // transactional outbox + deduplication
157
+ ttl: 30d
158
+ }
159
+ ```
160
+
161
+ ### Channels
162
+
163
+ Real-time WebSocket communication with optional filter predicates.
164
+
165
+ ```bone
166
+ channel game_lobby {
167
+ transport: websocket
168
+ ordering: causal
169
+ participants: set<Player>
170
+ persistence: last_100
171
+ filter: event.room_id == participant.room_id
172
+ }
173
+ ```
174
+
175
+ ### Stores
176
+
177
+ Explicit persistence declarations that generate SQL migrations.
178
+
179
+ ```bone
180
+ store AuditLog {
181
+ engine: postgresql
182
+ schema: {
183
+ actor_id: uuid,
184
+ action: string,
185
+ occurred_at: timestamp
186
+ }
187
+ retention: 90d
188
+ }
189
+ ```
190
+
191
+ Supported engines: `postgresql`, `redis`.
192
+
193
+ ### Pipelines
194
+
195
+ Multi-step operations with automatic rollback.
196
+
197
+ ```bone
198
+ capability checkout(buyer: Buyer, cart: Cart) {
199
+ pipeline: {
200
+ validate_inventory(cart)
201
+ charge_payment(buyer, cart.total) as payment
202
+ create_order(buyer, cart, payment)
203
+ on_error: rollback
204
+ }
205
+ sync: transactional
206
+ }
207
+ ```
208
+
209
+ ### Flows (Sagas)
210
+
211
+ Multi-service orchestration with backward compensation.
212
+
213
+ ```bone
214
+ flow place_order {
215
+ step reserve: reserve_inventory(order)
216
+ compensate: release_inventory(order)
217
+ step charge: charge_buyer(order)
218
+ compensate: refund_buyer(order)
219
+ step notify: send_confirmation(order)
220
+ }
221
+ ```
222
+
223
+ ### Algorithms
224
+
225
+ Named implementations from a built-in catalog.
226
+
227
+ ```bone
228
+ capability find_route(start: string, end: string) {
229
+ algorithm: shortest_path using { graph: road_network, source: start, target: end }
230
+ returns: json
231
+ }
232
+ ```
233
+
234
+ Available: `shortest_path`, `topological_sort`, `binary_search`, `bipartite_matching`, `round_robin`, `weighted_average`, `percentile`, `rank_by`, `consistent_hash`.
235
+
236
+ ### Extension Points
237
+
238
+ Escape hatches for custom logic that survive recompilation.
239
+
240
+ ```bone
241
+ extension_point calculate_fee(order: Order) {
242
+ returns: uint
243
+ stable: true // compilation fails if not implemented
244
+ }
245
+ ```
246
+
247
+ ---
248
+
249
+ ## Auth Strategies
250
+
251
+ Selected automatically based on domain. Override with `auth:` on any entity.
252
+
253
+ | Strategy | Trigger | Generated code |
254
+ |----------|---------|----------------|
255
+ | `jwt` | default / `multiplayer_game` / `realtime_collaboration` | Bearer token middleware, `issueToken()` helper |
256
+ | `oauth2` | `saas_platform` / `marketplace` / `social_network` | Full Authorization Code + PKCE flow, `/auth/login`, `/auth/callback`, `/auth/refresh`, `/auth/logout` |
257
+ | `apikey` | `iot_system` | `X-API-Key` header, SHA-256 hashed keys, LRU cache, `/auth/keys` CRUD routes, `api_keys.sql` migration |
258
+
259
+ ---
260
+
261
+ ## Commands
262
+
263
+ | Command | Description |
264
+ |---------|-------------|
265
+ | `bonec compile <file>` | Full 7-stage compilation → runnable project |
266
+ | `bonec check <file>` | Validate without generating code |
267
+ | `bonec fmt <file>` | Format in place |
268
+ | `bonec watch <file>` | Recompile on save |
269
+ | `bonec init <name> --domain <d>` | Scaffold from a domain template |
270
+ | `bonec diff <old> <new>` | Show schema migration diff |
271
+ | `bonec test [output-dir]` | Run generated regression tests |
272
+ | `bonec debug <file>` | Generate source maps |
273
+ | `bonec ir <file>` | Print the IR as JSON |
274
+ | `bonec lex <file>` | Print the token stream |
275
+ | `bonec parse <file>` | Print the AST |
276
+ | `bonec verify-determinism <file>` | Confirm two compilations are identical |
277
+
278
+ ### Domain Templates
279
+
280
+ `bonec init my-app --domain <name>`
281
+
282
+ | Domain | Auth | Sync |
283
+ |--------|------|------|
284
+ | `multiplayer_game` | JWT | realtime |
285
+ | `saas_platform` | OAuth2 | eventual |
286
+ | `iot_system` | API key | eventual |
287
+ | `social_network` | OAuth2 | eventual |
288
+ | `marketplace` | OAuth2 | transactional |
289
+ | `realtime_collaboration` | JWT | realtime |
290
+
291
+ ---
292
+
293
+ ## Type Errors
294
+
295
+ The compiler reports structured errors with codes:
296
+
297
+ | Code | Meaning |
298
+ |------|---------|
299
+ | T001 | Undefined type reference |
300
+ | T003 | Type mismatch in assignment |
301
+ | T005 | Expression must type to bool |
302
+ | T006 | Undeclared parameter type |
303
+ | T008 | Invalid set/numeric operator |
304
+ | T009 | Duplicate field name |
305
+ | T010 | Undefined state in transition |
306
+ | T011 | Emitted event not declared |
307
+ | T012 | Flow has fewer than 2 steps |
308
+ | T013 | Entity name used as capability call |
309
+ | T014 | Unsupported store engine |
310
+ | T015 | Invalid policy value |
311
+
312
+ ---
313
+
314
+ ## Compilation Pipeline
315
+
316
+ Every stage is deterministic — same `.bone` file always produces bitwise-identical output.
317
+
318
+ ```
319
+ .bone source
320
+ ↓ Lex tokens
321
+ ↓ Parse AST (with error recovery)
322
+ ↓ Type Check validated AST + structured errors
323
+ ↓ Lower Architecture IR
324
+ ↓ Optimize dead module elimination, deduplication
325
+ ↓ Solve constraint propagation → concrete decisions
326
+ ↓ Emit TypeScript + SQL + YAML + JSON
327
+ ↓ Verify IR consistency + generated code checks
328
+ ```
329
+
330
+ ---
331
+
332
+ ## Programmatic API
333
+
334
+ ```typescript
335
+ import { compile } from "bonescript-compiler";
336
+
337
+ const { files, errors, warnings } = await compile(`
338
+ system Shop {
339
+ entity Product { owns: [name: string, price: uint] }
340
+ }
341
+ `);
342
+
343
+ for (const file of files) {
344
+ console.log(file.path, file.language);
345
+ }
346
+ ```
347
+
348
+ Individual pipeline stages are also exported:
349
+
350
+ ```typescript
351
+ import {
352
+ Lexer, Parser, TypeChecker,
353
+ Lowering, optimize, ConstraintSolver,
354
+ FullEmitter, Verifier, Formatter,
355
+ scaffold, ModuleLoader,
356
+ } from "bonescript-compiler";
357
+ ```
358
+
359
+ ---
360
+
361
+ ## VS Code Extension
362
+
363
+ Install from the repo:
364
+
365
+ ```bash
366
+ .\install-extension.ps1
367
+ ```
368
+
369
+ Features: real-time diagnostics, context-aware completions, hover docs, go-to-definition, document outline, signature help, quick fixes for all error codes, cross-file rename.
370
+
371
+ ---
372
+
373
+ ## Contributing
374
+
375
+ Issues and PRs welcome at [github.com/Doorman11991/BoneScript](https://github.com/Doorman11991/BoneScript).
376
+
377
+ ```bash
378
+ cd compiler
379
+ npm test # run all tests
380
+ npm run test:jest:unit # lexer, parser, typechecker
381
+ npm run test:jest:integration # emitter, integration
382
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bonescript-compiler",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "BoneScript compiler — compile .bone system descriptions into complete, runnable Node.js backends",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",