@o-lang/olang 1.0.12 → 1.0.13

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/package.json +1 -1
  2. package/src/parser.js +61 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@o-lang/olang",
3
- "version": "1.0.12",
3
+ "version": "1.0.13",
4
4
  "author": "Olalekan Ogundipe <info@workfily.com>",
5
5
  "description": "O-Lang: A governance language for user-directed, rule-enforced agent workflows",
6
6
  "main": "./src/index.js",
package/src/parser.js CHANGED
@@ -106,41 +106,72 @@ function parse(code, fileName = null) {
106
106
  continue;
107
107
  }
108
108
 
109
- // ---------------------------
110
- // Return statement (smart math detection)
111
- // ---------------------------
112
- const returnMatch = line.match(/^Return\s+(.+)$/i);
113
- if (returnMatch) {
114
- workflow.returnValues = returnMatch[1].split(',').map(v => v.trim());
115
- workflow.returnValues.forEach(v => {
116
- if (v.match(/[A-Z][A-Za-z0-9_]*/)) workflow.__requiresMath = true;
117
- });
118
- i++;
119
- continue;
120
- }
109
+ // ---------------------------
110
+ // Return statement (updated: auto-detect math)
111
+ // ---------------------------
112
+ const returnMatch = line.match(/^Return\s+(.+)$/i);
113
+ if (returnMatch) {
114
+ const returns = returnMatch[1].split(',').map(v => v.trim());
115
+ workflow.returnValues = returns;
116
+
117
+ // --- Check if any return vars come from math steps ---
118
+ for (const retVar of returns) {
119
+ const producedByMath = workflow.steps.some(
120
+ s => s.saveAs === retVar && s.type === 'calculate'
121
+ );
122
+ if (producedByMath) workflow.__requiresMath = true;
123
+ }
124
+
125
+ i++;
126
+ continue;
127
+ }
128
+
121
129
 
122
130
  // ---------------------------
123
- // Steps (smart math detection)
124
- // ---------------------------
125
- const stepMatch = line.match(/^Step\s+(\d+)\s*:\s*(.+)$/i);
126
- if (stepMatch) {
127
- const step = {
128
- type: 'action',
129
- stepNumber: parseInt(stepMatch[1], 10),
130
- actionRaw: stepMatch[2].trim(),
131
- saveAs: null,
132
- constraints: {}
133
- };
131
+ // Steps (updated: auto-detect math + saveAs)
132
+ // ---------------------------
133
+ const stepMatch = line.match(/^Step\s+(\d+)\s*:\s*(.+)$/i);
134
+ if (stepMatch) {
135
+ const stepNum = parseInt(stepMatch[1], 10);
136
+ const raw = stepMatch[2].trim();
137
+
138
+ // --- Detect math inside Step ---
139
+ let mathDetected = null;
140
+ let expr = '';
141
+ let saveVar = null;
142
+
143
+ const mathOps = [
144
+ { re: /^Add\s+\{(.+?)\}\s+and\s+\{(.+?)\}\s+Save as\s+(.+)$/i, fn: 'add' },
145
+ { re: /^Subtract\s+\{(.+?)\}\s+from\s+\{(.+?)\}\s+Save as\s+(.+)$/i, fn: 'subtract' },
146
+ { re: /^Multiply\s+\{(.+?)\}\s+and\s+\{(.+?)\}\s+Save as\s+(.+)$/i, fn: 'multiply' },
147
+ { re: /^Divide\s+\{(.+?)\}\s+by\s+\{(.+?)\}\s+Save as\s+(.+)$/i, fn: 'divide' }
148
+ ];
149
+
150
+ for (const op of mathOps) {
151
+ const m = raw.match(op.re);
152
+ if (m) {
153
+ mathDetected = op.fn;
154
+ saveVar = m[3].trim();
155
+ if (op.fn === 'subtract') expr = `subtract({${m[2]}}, {${m[1]}})`;
156
+ else expr = `${op.fn}({${m[1]}}, {${m[2]}})`;
157
+ break;
158
+ }
159
+ }
134
160
 
135
- workflow.steps.push(step);
161
+ if (mathDetected) workflow.__requiresMath = true;
136
162
 
137
- if (step.actionRaw.match(/\{[A-Za-z_][A-Za-z0-9_]*\}/)) {
138
- workflow.__requiresMath = true;
139
- }
163
+ workflow.steps.push({
164
+ type: mathDetected ? 'calculate' : 'action',
165
+ stepNumber: stepNum,
166
+ actionRaw: mathDetected ? null : raw,
167
+ expression: mathDetected ? expr : undefined,
168
+ saveAs: saveVar,
169
+ constraints: {}
170
+ });
140
171
 
141
- i++;
142
- continue;
143
- }
172
+ i++;
173
+ continue;
174
+ }
144
175
 
145
176
  const saveMatch = line.match(/^Save as\s+(.+)$/i);
146
177
  if (saveMatch && workflow.steps.length > 0) {