@graffiticode/basis 1.6.2 → 1.6.4

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.
@@ -0,0 +1,16 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "WebFetch(domain:www.npmjs.com)",
5
+ "WebFetch(domain:raw.githubusercontent.com)",
6
+ "Bash(gh api:*)",
7
+ "WebFetch(domain:api.github.com)",
8
+ "Bash(npm ls:*)",
9
+ "Bash(npx spec-md:*)",
10
+ "Bash(node:*)",
11
+ "Bash(npx:*)",
12
+ "Bash(npm run:*)",
13
+ "Bash(npm install:*)"
14
+ ]
15
+ }
16
+ }
package/CLAUDE.md ADDED
@@ -0,0 +1,55 @@
1
+ # CLAUDE.md
2
+
3
+ This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+ ## Commands
6
+
7
+ - `npm test` - Run Jest test suite
8
+ - `npm run build-spec` - Generate HTML spec from spec/spec.md
9
+ - `npm run watch-spec` - Watch spec.md and rebuild on changes
10
+
11
+ ## Architecture
12
+
13
+ This is the **basis library for Graffiticode languages** - a compiler framework for building domain-specific languages.
14
+
15
+ ### Compilation Pipeline (3-Stage Visitor Pattern)
16
+
17
+ All stages are in `src/compiler.js`:
18
+
19
+ 1. **Checker** (lines 91-516): Validates AST nodes, collects errors
20
+ 2. **Transformer** (lines 557-1391): Evaluates the program, manages environments/scope
21
+ 3. **Renderer** (lines 1393-1403): Formats output (minimal pass-through)
22
+ 4. **Compiler** (lines 1405-1450): Orchestrates Checker → Transformer → Renderer
23
+
24
+ ### Key Files
25
+
26
+ - `src/compiler.js` - Core compiler with Visitor base class and all stages
27
+ - `src/lexicon.js` - Builtin function definitions (28 functions with types/descriptions)
28
+ - `src/share.js` - Shared utilities, error codes, assertion helpers
29
+ - `index.js` - Exports: Checker, Transformer, Renderer, Compiler, lexicon
30
+ - `spec/spec.md` - Language specification (source of truth for syntax/semantics)
31
+
32
+ ### Implementation Details
33
+
34
+ - **AST representation**: Node pool as array of `{tag, elts}` objects with numeric IDs
35
+ - **Async traversal**: Uses `setTimeout(0)` to prevent stack overflow during deep recursion
36
+ - **Arithmetic**: Uses Decimal.js for precise numeric operations
37
+ - **Scope**: Stack-based environment management (`enterEnv`, `exitEnv`, `findWord`, `addWord`)
38
+
39
+ ## Language Features
40
+
41
+ Graffiticode is a functional language with:
42
+ - Prefix notation for function application (`add 1 2`). Functions have fixed arity, so applications can be parsed unambiguously without grouping syntax (e.g., `add 1 mul 2 3` parses as `add(1, mul(2, 3))`)
43
+ - First-class lambdas (`<x: add x 1>`)
44
+ - Pattern matching (`case x of ... end`)
45
+ - Records and lists as first-class data
46
+ - Let bindings (`let name = value..`)
47
+ - Programs terminated with `..`
48
+
49
+ ## Spec Synchronization
50
+
51
+ The language spec (`spec/spec.md`) should stay synchronized with `src/lexicon.js`. When adding/modifying builtin functions:
52
+ 1. Update the function in `src/compiler.js` (Transformer handlers)
53
+ 2. Add/update entry in `src/lexicon.js` with type signature and description
54
+ 3. Document in `spec/spec.md` Built-in Functions table and detailed section
55
+ 4. Run `npm run build-spec` to regenerate spec/spec.html
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@graffiticode/basis",
3
3
  "type": "module",
4
- "version": "1.6.2",
4
+ "version": "1.6.4",
5
5
  "description": "The basis library for creating Graffiticode languages",
6
6
  "main": "index.js",
7
7
  "scripts": {
8
8
  "test": "jest",
9
- "build-spec": "npx spec-md ./spec/spec.md > ./spec/spec.html",
10
- "watch-spec": "npx nodemon --exec 'npx spec-md > ./spec/spec.html' ./spec/spec.md",
9
+ "build-spec": "node spec/build.js",
10
+ "watch-spec": "npx nodemon --exec 'node spec/build.js' ./spec/spec.md",
11
11
  "publish-spec": "npm run build-spec && for dir in ../l0*/packages/api/public; do cp ./spec/spec.html \"$dir/graffiticode-language-spec.html\"; echo \"Copied to $dir/graffiticode-language-spec.html\"; done"
12
12
  },
13
13
  "engines": {
@@ -28,7 +28,8 @@
28
28
  "hashids": "^2.2.8",
29
29
  "https": "^1.0.0",
30
30
  "jest": "^27.0.1",
31
- "react": "^17.0.2"
31
+ "react": "^17.0.2",
32
+ "spec-md": "^3.1.0"
32
33
  },
33
34
  "dependencies": {
34
35
  "decimal.js": "^10.5.0"
package/spec/build.js ADDED
@@ -0,0 +1,54 @@
1
+ const specMarkdown = require('spec-md');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+
5
+ const source = path.join(__dirname, 'spec.md');
6
+ const dest = path.join(__dirname, 'spec.html');
7
+
8
+ const copyButtonHead = `
9
+ <style>
10
+ pre {
11
+ position: relative;
12
+ }
13
+ .copy-button {
14
+ position: absolute;
15
+ top: 6px;
16
+ right: 6px;
17
+ padding: 3px 8px;
18
+ border: 1px solid var(--color-pre-border);
19
+ border-radius: 4px;
20
+ background: var(--color-background);
21
+ color: var(--color-grey);
22
+ font-family: var(--font-family);
23
+ font-size: 12px;
24
+ cursor: pointer;
25
+ opacity: 0;
26
+ transition: opacity 0.15s;
27
+ }
28
+ pre:hover .copy-button {
29
+ opacity: 1;
30
+ }
31
+ .copy-button:hover {
32
+ background: var(--color-pre-border);
33
+ }
34
+ </style>
35
+ <script>
36
+ document.addEventListener("DOMContentLoaded", function () {
37
+ document.querySelectorAll("pre > code").forEach(function (code) {
38
+ var pre = code.parentElement;
39
+ var btn = document.createElement("button");
40
+ btn.className = "copy-button";
41
+ btn.textContent = "Copy";
42
+ btn.addEventListener("click", function () {
43
+ navigator.clipboard.writeText(code.textContent).then(function () {
44
+ btn.textContent = "Copied!";
45
+ setTimeout(function () { btn.textContent = "Copy"; }, 1500);
46
+ });
47
+ });
48
+ pre.appendChild(btn);
49
+ });
50
+ });
51
+ </script>`;
52
+
53
+ const html = specMarkdown.html(source, { head: copyButtonHead });
54
+ fs.writeFileSync(dest, html);
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "@graffiticode/basis-spec",
3
+ "version": "0.1.2",
4
+ "description": "Graffiticode Core Language Specification",
5
+ "private": true
6
+ }