difftran 0.0.0-stage → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Phu Minh (Catto)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,97 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ ## Difftran
2
+
3
+ Difftran is an esoteric programming language inspired by Fractran, utilizing multiplication and differentiation rather than multiplying with fractions, hence the name "Difftran".
4
+
5
+ ### Basics
6
+
7
+ State in Difftran is represented as a function containing powers of variables multiplied together, e.g `3 x^2 y^3 z^4`. In a Difftran program, you would define the state at the top of the program's code, for example:
8
+
9
+ ```
10
+ 3 x^2 y^3 z^4
11
+
12
+ ...
13
+ ...
14
+ ...
15
+ ```
16
+
17
+ In Difftran, there are 4 operators:
18
+
19
+ * `var`: Multiply the state by variable `var`. For example, if state is `x^3 y^2`, after op `x`, the state would be `x^4 y^2`.
20
+ * `dvar`: Take the partial derivative of the state function with respect to variable `var`. For example, if state is `x^3 y^2`, after op `dy`, the state would be `2 x^3 y`. Because of this, your variable's names should not start with `d`.
21
+ * `var^a`: Multiply the state by variable `var` `a` times. For example, if state is `x^3 y^2`, after op `x^3`, the state would be `x^6 y^2`.
22
+ * `dvar^a`: Take the partial derivative of the state function with respect to variable `var` `a` times. For example, if state is `x^3 y^2`, after op `dx^2`, the state would be `6 x y^2`.
23
+
24
+ A command in Difftran can contain multiple ops separated by space(s), and commands are separated by new lines(s). A Difftran program will loop through each command, run them, and if the new temporary state is not zero after executing, keep that state and loop back to the start, otherwise discard the temporary state and try to run the next command. If every command does not suffice the non-zero check, the program stops.
25
+
26
+ Here is a Difftran program to divide 17 by 5 with remainder:
27
+
28
+ ```
29
+ a^17 b^5
30
+
31
+ df f dt b
32
+ df q
33
+ da db t
34
+ db b dt r
35
+ dt t f
36
+ db
37
+ ```
38
+
39
+ The result would be:
40
+
41
+ ```
42
+ 286760781709867745280000000000000 q^3 r^2
43
+ ```
44
+
45
+ where 3 is the quotient and 2 is the remainder.
46
+
47
+ The coefficient of the state function does not really do anything for now, but you can use it as a multiplication calculator (lol). Let's say you want to multiply 2 by 3:
48
+
49
+ ```
50
+ 2 a^3 b
51
+
52
+ da db
53
+ ```
54
+
55
+ And you will have `6 a^2` at the end, and `6 = 2 * 3`.
56
+
57
+ ### Turing completeness
58
+
59
+ Fractran can be reimplemented in Difftran, and Fractran is turing complete, so Difftran is turing complete:
60
+
61
+ * Fractran state can be factored into powers of primes, with those primes being registers, and powers of those primes being values of those registers. Difftran is also similar: Variables are registers, and their powers are the registers' values.
62
+ * Fractran checks if multiplying with a fraction result in an integer (which effectively means checking if all registers are not less than 0), then update the state and loop back if the condition is satisfied, move to the next command if not. Difftran also has the same condition, just expressed in a different way: If the derivative is 0, it means some variable's power is lower than the number of times we differentiate it, i.e. that register would go below 0.
63
+
64
+ ### Interpreter
65
+
66
+ There is a simple interpreter written in JS in `interpreter.js`. A rough tutorial on how to use it:
67
+
68
+ ```js
69
+ const { Interpreter } = require("./interpreter");
70
+ // or const { Interpreter } = require("difftran"); if you installed through npm
71
+
72
+ // Interpreter is just a class with static methods. Here are some core ones:
73
+
74
+ // Converts code from text to a program object { state, commands }
75
+ const program = Interpreter.parse(code);
76
+
77
+ // state is an object of the form { coeff: bigint, vars: Map<string, bigint> }
78
+ // commands is an array of command, each command is an array of ops, and each op has the form of:
79
+ // { type: "mul" | "diff", varName: string, times: bigint }
80
+
81
+ // Runs a single command and returns the new state (null if the result is 0)
82
+ const stateAfterCommand = Interpreter.runCommand(state, command);
83
+
84
+ // Runs the whole program and returns the new state
85
+ Interpreter.run(program, log, steps);
86
+ // log is true, and steps is Infinity by default
87
+ ```
88
+
89
+ ### Example
90
+
91
+ There is an example on how to use the interpreter and a sample program in `example.js`.
92
+
93
+ ### Copyright and License
94
+
95
+ Copyright © 2026 Nguyen Phu Minh.
96
+
97
+ This project is licensed under the MIT License.
package/interpreter.js ADDED
@@ -0,0 +1,190 @@
1
+ class Interpreter {
2
+ // Variable name can be whatever, just not empty, not a plain number, and not starting with "d"
3
+ static isValidVarName(name) {
4
+ return (
5
+ name !== "" &&
6
+ !/^-?\d+$/.test(name) &&
7
+ !name.startsWith("d")
8
+ );
9
+ }
10
+
11
+ // Parse an exponent string, only non-negative integers allowed
12
+ static parseExponent(str, errorCode) {
13
+ if (!/^\d+$/.test(str)) {
14
+ throw new Error(errorCode);
15
+ }
16
+
17
+ return BigInt(str);
18
+ }
19
+
20
+ // Parse into state and ops with params
21
+ static parse(code) {
22
+ const lines = code
23
+ .split(/\r?\n/) // Split into lines
24
+ .map(line => line.trim()) // Trim whitespace of each line
25
+ .filter(line => line.length !== 0) // Gather commands (non-empty lines)
26
+ // Convert each command into ops
27
+ .map(line => line.split(/\s+/));
28
+
29
+ if (lines.length === 0) {
30
+ throw new Error("EMPTY_PROGRAM");
31
+ }
32
+
33
+ // Get state
34
+ const stateTokens = lines[0];
35
+
36
+ const state = {
37
+ coeff: 1n,
38
+ vars: new Map()
39
+ };
40
+
41
+ for (const token of stateTokens) {
42
+ // Plain integers multiply into the coefficient
43
+ if (/^-?\d+$/.test(token)) {
44
+ state.coeff *= BigInt(token);
45
+ continue;
46
+ }
47
+
48
+ const args = token.split("^");
49
+ const varName = args[0];
50
+
51
+ if (!Interpreter.isValidVarName(varName)) {
52
+ throw new Error("INVALID_STATE_VARIABLE_NAME");
53
+ }
54
+
55
+ let power = 1n;
56
+
57
+ if (args.length === 2) {
58
+ power = Interpreter.parseExponent(args[1], "INVALID_STATE_EXPONENT");
59
+ } else if (args.length !== 1) {
60
+ throw new Error("INVALID_STATE");
61
+ }
62
+
63
+ state.vars.set(varName, (state.vars.get(varName) ?? 0n) + power);
64
+ }
65
+
66
+ // Get commands
67
+ const commands = [];
68
+
69
+ for (let index = 1; index < lines.length; index++) {
70
+ const command = [];
71
+ const ops = lines[index];
72
+
73
+ for (const op of ops) {
74
+ const args = op.split("^");
75
+ const opName = args[0];
76
+
77
+ let multiplier = 1n;
78
+
79
+ if (args.length === 2) {
80
+ multiplier = Interpreter.parseExponent(args[1], "INVALID_OP_EXPONENT");
81
+ } else if (args.length !== 1) {
82
+ throw new Error("INVALID_OP");
83
+ }
84
+
85
+ // "d" + variable name is a derivative
86
+ if (opName.startsWith("d")) {
87
+ const varName = opName.slice(1);
88
+
89
+ if (!Interpreter.isValidVarName(varName)) {
90
+ throw new Error("INVALID_OP_VARIABLE_NAME");
91
+ }
92
+
93
+ command.push({ type: "diff", varName, times: multiplier });
94
+ } else {
95
+ if (!Interpreter.isValidVarName(opName)) {
96
+ throw new Error("INVALID_OP_VARIABLE_NAME");
97
+ }
98
+
99
+ command.push({ type: "mul", varName: opName, times: multiplier });
100
+ }
101
+ }
102
+
103
+ commands.push(command);
104
+ }
105
+
106
+ return { state, commands };
107
+ }
108
+
109
+ // Run a command on a state, returns the new state or null if it is zero
110
+ static runCommand(state, command) {
111
+ if (state.coeff === 0n) {
112
+ return null;
113
+ }
114
+
115
+ let coeff = state.coeff;
116
+ const vars = new Map(state.vars);
117
+
118
+ for (const { type, varName, times } of command) {
119
+ const power = vars.get(varName) ?? 0n;
120
+
121
+ if (type === "mul") {
122
+ vars.set(varName, power + times);
123
+ } else {
124
+ // Differentiating more times than the power gives zero
125
+ if (power < times) {
126
+ return null;
127
+ }
128
+
129
+ // d/dx^n of x^p = p(p-1)...(p-n+1) x^(p-n)
130
+ for (let k = 0n; k < times; k++) {
131
+ coeff *= power - k;
132
+ }
133
+
134
+ vars.set(varName, power - times);
135
+ }
136
+ }
137
+
138
+ return { coeff, vars };
139
+ }
140
+
141
+ // Run a parsed program
142
+ static run(program, log = true, steps = Infinity) {
143
+ const { commands } = program;
144
+ let state = program.state;
145
+
146
+ if (log) {
147
+ console.log(Interpreter.stateToString(state));
148
+ }
149
+
150
+ for (let step = 0; step < steps; step++) {
151
+ let nextState = null;
152
+
153
+ for (let index = 0; index < commands.length; index++) {
154
+ nextState = Interpreter.runCommand(state, commands[index]);
155
+
156
+ if (nextState !== null) {
157
+ break;
158
+ }
159
+ }
160
+
161
+ // No command gave a non-zero state, halt
162
+ if (nextState === null) {
163
+ break;
164
+ }
165
+
166
+ state = nextState;
167
+
168
+ if (log) {
169
+ console.log(Interpreter.stateToString(state));
170
+ }
171
+ }
172
+
173
+ return state;
174
+ }
175
+
176
+ // Parse a state object to string
177
+ static stateToString(state) {
178
+ const fields = [];
179
+
180
+ for (const [key, power] of state.vars) {
181
+ if (power === 0n) continue;
182
+
183
+ fields.push(power === 1n ? key : `${key}^${power}`);
184
+ }
185
+
186
+ return [state.coeff.toString(), ...fields].join(" ");
187
+ }
188
+ }
189
+
190
+ module.exports = { Interpreter };
package/package.json CHANGED
@@ -1,6 +1,33 @@
1
1
  {
2
2
  "name": "difftran",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "An esolang inspired by Fractran based on multiplication and differentiation",
5
+ "keywords": [
6
+ "difftran",
7
+ "fractran",
8
+ "esolang",
9
+ "interpreter",
10
+ "runtime",
11
+ "math"
12
+ ],
13
+ "homepage": "https://github.com/nguyenphuminh/difftran#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/nguyenphuminh/difftran/issues"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/nguyenphuminh/difftran.git"
20
+ },
21
+ "license": "MIT",
22
+ "author": "nguyenphuminh",
23
+ "type": "commonjs",
24
+ "main": "interpreter.js",
25
+ "scripts": {
26
+ "test": "echo \"Error: no test specified\" && exit 1"
27
+ },
28
+ "files": [
29
+ "interpreter.js",
30
+ "LICENSE",
31
+ "README.md"
32
+ ]
33
+ }