onejs-sl 0.1.4 → 0.1.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.
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The prelude's source, and what an editor reads from it without parsing it.
3
+ *
4
+ * Kept out of `prelude.ts`, which parses it, so that `onejs-sl/tables` can offer
5
+ * the prelude's names and descriptions with no parser in the bundle. Each
6
+ * function's description is the comment above it, so there is one place to
7
+ * write it.
8
+ */
9
+
10
+ export const PRELUDE_SOURCE = `
11
+ // Rotate a point about the origin. Translate first if you want another centre.
12
+ float2 rotate(float2 p, float angle) {
13
+ float c = cos(angle);
14
+ float s = sin(angle);
15
+ return float2(p.x * c - p.y * s, p.x * s + p.y * c);
16
+ }
17
+
18
+ // Cartesian to polar: x is the angle in radians, y is the radius.
19
+ float2 polar(float2 p) {
20
+ return float2(atan2(p.y, p.x), length(p));
21
+ }
22
+
23
+ // Signed distance to a circle at the origin. Negative inside.
24
+ float circle(float2 p, float r) {
25
+ return length(p) - r;
26
+ }
27
+
28
+ // Signed distance to an axis aligned box of the given half extents.
29
+ float box(float2 p, float2 size) {
30
+ float2 d = abs(p) - size;
31
+ return length(max(d, 0)) + min(max(d.x, d.y), 0);
32
+ }
33
+
34
+ // Repeat a unit cell n times across the input, centred on zero in each cell.
35
+ float2 tile(float2 p, float n) {
36
+ return frac(p * n) - 0.5;
37
+ }
38
+
39
+ // Inigo Quilez's cosine palette. Four float3s in, a colour out, no texture.
40
+ float3 palette(float t, float3 a, float3 b, float3 c, float3 d) {
41
+ return a + b * cos(6.28318530718 * (c * t + d));
42
+ }
43
+ `
44
+
45
+ /** Each prelude function's name, in the order the source declares them. */
46
+ const DECLARED = /* @__PURE__ */ [...PRELUDE_SOURCE.matchAll(/^\/\/ (.+)\n\w+ (\w+)\(/gm)]
47
+
48
+ export const PRELUDE_NAMES: ReadonlySet<string> = /* @__PURE__ */ new Set(DECLARED.map((m) => m[2]!))
49
+
50
+ /** Each prelude function's one line description: the comment above it. */
51
+ export const PRELUDE_DOCS: Readonly<Record<string, string>> = /* @__PURE__ */ Object.fromEntries(
52
+ DECLARED.map((m) => [m[2]!, m[1]!]),
53
+ )
@@ -21,41 +21,9 @@
21
21
  import type { FuncDecl } from "./ast"
22
22
  import { check } from "./check"
23
23
  import { parseUnit } from "./parser"
24
+ import { PRELUDE_SOURCE } from "./prelude-source"
24
25
 
25
- export const PRELUDE_SOURCE = `
26
- // Rotate a point about the origin. Translate first if you want another centre.
27
- float2 rotate(float2 p, float angle) {
28
- float c = cos(angle);
29
- float s = sin(angle);
30
- return float2(p.x * c - p.y * s, p.x * s + p.y * c);
31
- }
32
-
33
- // Cartesian to polar: x is the angle in radians, y is the radius.
34
- float2 polar(float2 p) {
35
- return float2(atan2(p.y, p.x), length(p));
36
- }
37
-
38
- // Signed distance to a circle at the origin. Negative inside.
39
- float circle(float2 p, float r) {
40
- return length(p) - r;
41
- }
42
-
43
- // Signed distance to an axis aligned box of the given half extents.
44
- float box(float2 p, float2 size) {
45
- float2 d = abs(p) - size;
46
- return length(max(d, 0)) + min(max(d.x, d.y), 0);
47
- }
48
-
49
- // Repeat a unit cell n times across the input, centred on zero in each cell.
50
- float2 tile(float2 p, float n) {
51
- return frac(p * n) - 0.5;
52
- }
53
-
54
- // Inigo Quilez's cosine palette. Four float3s in, a colour out, no texture.
55
- float3 palette(float t, float3 a, float3 b, float3 c, float3 d) {
56
- return a + b * cos(6.28318530718 * (c * t + d));
57
- }
58
- `
26
+ export { PRELUDE_SOURCE }
59
27
 
60
28
  let cached: FuncDecl[] | null = null
61
29
 
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The words the parser reads as syntax, and the value types.
3
+ *
4
+ * One list, read by the parser (a name may not be one of these) and by
5
+ * `classify` (these highlight as keywords and types), so an editor that
6
+ * highlights with it agrees with the parser by construction rather than by a
7
+ * list it keeps in step by hand.
8
+ */
9
+
10
+ import { TYPE_WIDTH, type TypeName } from "./ast"
11
+
12
+ /**
13
+ * Declaration and statement words. `int` is here rather than among the types
14
+ * because it is only ever a for loop's counter, `for (int i = 0; i < 4; i++)`;
15
+ * a value cannot be declared as one.
16
+ */
17
+ export const SL_KEYWORDS = ["uniform", "texture2D", "const", "if", "else", "for", "return", "int"] as const
18
+
19
+ /** The types a value, a uniform or a function can have. */
20
+ export const SL_TYPES = Object.keys(TYPE_WIDTH) as readonly TypeName[]
21
+
22
+ export const KEYWORD_SET: ReadonlySet<string> = new Set(SL_KEYWORDS)
23
+ export const TYPE_SET: ReadonlySet<string> = new Set(SL_TYPES)
package/src/params.ts CHANGED
@@ -20,6 +20,7 @@ import { BUILTINS } from "./lang/builtins"
20
20
  import { LIB_FUNCTIONS, SDF_CALLS } from "./lib/table"
21
21
  import { LIB_PARAM_NAMES } from "./lib/names"
22
22
  import { SL_SDF_PARAMS, SL_SDF_SHAPES, type SlSdfKind } from "./shapes"
23
+ import type { InputName } from "./ir"
23
24
 
24
25
  /** One function of the helper library, as its source declares it. */
25
26
  export interface LibSignature {
@@ -111,3 +112,63 @@ function shapeNames(): Record<SlSdfKind, readonly string[]> {
111
112
  }
112
113
  return out
113
114
  }
115
+
116
+ /**
117
+ * One line on what each builtin does, for a completion's tooltip and the docs
118
+ * page: the other half of `BUILTIN_PARAMS`, and named in its words, so a line
119
+ * can say `s` and mean the third argument.
120
+ */
121
+ export const BUILTIN_DOCS: Readonly<Record<string, string>> = {
122
+ sin: "The sine of x, in radians.",
123
+ cos: "The cosine of x, in radians.",
124
+ tan: "The tangent of x, in radians.",
125
+ asin: "The angle whose sine is x, with x clamped to -1 to 1.",
126
+ acos: "The angle whose cosine is x, with x clamped to -1 to 1.",
127
+ exp: "e raised to the power x.",
128
+ log: "The natural logarithm of x.",
129
+ sqrt: "The square root of x.",
130
+ sign: "-1, 0 or 1, by the sign of x.",
131
+ ceil: "x rounded up to a whole number.",
132
+ round: "x rounded to the nearest whole number.",
133
+ toLinear: "A colour as written, in sRGB, converted to the space the target holds; alpha is left alone.",
134
+ abs: "x without its sign.",
135
+ floor: "x rounded down to a whole number.",
136
+ frac: "The part of x after the decimal point, 0 up to 1.",
137
+ saturate: "x clamped to 0 to 1.",
138
+ rcp: "1 / x.",
139
+ normalize: "x scaled to length 1.",
140
+ pow: "x raised to the power y.",
141
+ min: "The smaller of x and y, per component.",
142
+ max: "The larger of x and y, per component.",
143
+ clamp: "x held between min and max.",
144
+ atan2: "The angle of the point (x, y) from the positive x axis, in radians.",
145
+ length: "The length of the vector x.",
146
+ distance: "The distance between the points x and y.",
147
+ dot: "The dot product of x and y.",
148
+ cross: "The cross product of two float3s.",
149
+ reflect: "The direction i reflected off a surface with normal n.",
150
+ lerp: "Mixes x into y by s: x at 0, y at 1.",
151
+ step: "0 where x is below y, else 1.",
152
+ smoothstep: "0 below min, 1 above max, and a smooth curve between.",
153
+ remap: "v moved from the range fromMin to fromMax into the range toMin to toMax.",
154
+ hsv2rgb: "A colour from hue, saturation and value, each 0 to 1.",
155
+ luminance: "How bright a colour looks, as one number.",
156
+ noise: "Smooth value noise at p, about 0 to 1. Offset p for a different field.",
157
+ simplex: "Simplex noise at p, about 0 to 1, with no grid pattern.",
158
+ voronoi: "The distance from p to the nearest point of a jittered grid: cells.",
159
+ fbm: "Value noise layered in octaves, 1 to 4 (3 when left out): clouds and terrain.",
160
+ turbulence: "Simplex creases layered in octaves, 1 to 4: fire, smoke, marble.",
161
+ ridged: "Turbulence made bright at its creases: ridges, lightning, cracks.",
162
+ tex2D: "The colour of texture s at the uv t, with straight alpha.",
163
+ sdf: "The signed distance from p to a shape, negative inside: `sdf.circle(p, r)`.",
164
+ ramp: "t mapped through evenly spaced colour stops, from the first at 0 to the last at 1.",
165
+ }
166
+
167
+ /** One line on each input, for the same tooltips. */
168
+ export const INPUT_DOCS: Readonly<Record<InputName, string>> = {
169
+ uv: "Where this pixel is in the element, 0 to 1 on each axis, with y up.",
170
+ fragCoord: "Where this pixel is in the element, in pixels, with y up.",
171
+ resolution: "The element's size in pixels.",
172
+ time: "Seconds since the effect started.",
173
+ aspect: "The element's width divided by its height.",
174
+ }
package/src/tables.ts CHANGED
@@ -2,8 +2,10 @@
2
2
  * `onejs-sl/tables`: what completion and highlighting read.
3
3
  *
4
4
  * The same tables the checker and the emitters use, so an editor offers
5
- * exactly the builtins that compile, with the arities they take and the names
6
- * of their parameters (`params.ts`), and the helper library's signatures.
5
+ * exactly the builtins that compile, with the arities they take, the names of
6
+ * their parameters and a line on each (`params.ts`), the words the parser
7
+ * reads as syntax, and the helper library's signatures. `classify`, in the
8
+ * main entry, is the highlighter built on the same lists.
7
9
  */
8
10
  export { BUILTINS, NOT_YET } from "./lang/builtins"
9
11
  export type { Builtin } from "./lang/builtins"
@@ -13,6 +15,10 @@ export {
13
15
  export type { SLSurface } from "./ops"
14
16
  export { INPUTS } from "./ir"
15
17
  export { SL_SDF_SHAPES, SL_SDF_PARAMS } from "./shapes"
16
- export { BUILTIN_PARAMS, LIB_SIGNATURES, SL_SDF_PARAM_NAMES } from "./params"
18
+ export { BUILTIN_DOCS, BUILTIN_PARAMS, INPUT_DOCS, LIB_SIGNATURES, SL_SDF_PARAM_NAMES } from "./params"
19
+ export { PRELUDE_DOCS, PRELUDE_NAMES } from "./lang/prelude-source"
20
+ export { SL_KEYWORDS, SL_TYPES } from "./lang/words"
21
+ export { TYPE_WIDTH } from "./lang/ast"
22
+ export type { TypeName } from "./lang/ast"
17
23
  export type { LibSignature } from "./params"
18
24
  export type { SlSdfKind } from "./shapes"