formula is more natural
This commit is contained in:
parent
dcf409cbf7
commit
2472e0db7c
5 changed files with 32 additions and 30 deletions
|
|
@ -10,9 +10,9 @@ If you are a contributor or an agent, please follow [CLAUDE.md](./CLAUDE.md) for
|
||||||
|
|
||||||
# Formula file description
|
# Formula file description
|
||||||
|
|
||||||
The file is a json array of formulas.
|
The file is a dart array of formulas. Each formula is a dart set literal
|
||||||
|
|
||||||
```json
|
```dart
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"name": "Newton's second law (scalar)",
|
"name": "Newton's second law (scalar)",
|
||||||
|
|
@ -29,10 +29,9 @@ The file is a json array of formulas.
|
||||||
"magnitude" : "Force"
|
"magnitude" : "Force"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"d4rt_code": "return m*a;"
|
"d4rt_code": "F = m*a;"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ class FormulaEvaluator {
|
||||||
// Build the complete d4rt source code with variable declarations
|
// Build the complete d4rt source code with variable declarations
|
||||||
final completeSource = _buildCompleteSource(formula, inputValues);
|
final completeSource = _buildCompleteSource(formula, inputValues);
|
||||||
//print( "$formula");
|
//print( "$formula");
|
||||||
//print( "$completeSource" );
|
print( "$completeSource" );
|
||||||
|
|
||||||
// Execute the code using d4rt (no args needed since variables are in source)
|
// Execute the code using d4rt (no args needed since variables are in source)
|
||||||
final result = _interpreter.execute(source: completeSource);
|
final result = _interpreter.execute(source: completeSource);
|
||||||
|
|
@ -119,13 +119,12 @@ class FormulaEvaluator {
|
||||||
buffer.writeln('var $varName = $value;');
|
buffer.writeln('var $varName = $value;');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
buffer.writeln("late var ${getOutputVariableName(formula)};");
|
||||||
// Add a blank line for readability
|
|
||||||
buffer.writeln();
|
|
||||||
|
|
||||||
// Add the formula's d4rt code
|
// Add the formula's d4rt code
|
||||||
buffer.write(formula.d4rtCode);
|
buffer.writeln(formula.d4rtCode);
|
||||||
buffer.write("}");
|
buffer.writeln("return ${getOutputVariableName(formula)};");
|
||||||
|
buffer.writeln("}");
|
||||||
|
|
||||||
return buffer.toString();
|
return buffer.toString();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,17 @@ class Formula {
|
||||||
List<String> inputVarNames() =>
|
List<String> inputVarNames() =>
|
||||||
input.map((v) => v.name).toList(growable: false);
|
input.map((v) => v.name).toList(growable: false);
|
||||||
|
|
||||||
|
factory Formula.fromStringLiteral( String setStringLiteral ){
|
||||||
|
var d4rt = D4rt();
|
||||||
|
final buffer = StringBuffer();
|
||||||
|
buffer.write( "main(){ return $setStringLiteral; }");
|
||||||
|
final code = buffer.toString();
|
||||||
|
|
||||||
|
final Map<Object?, Object?> setLiteral = d4rt.execute(source: code);
|
||||||
|
|
||||||
|
return Formula.fromSet(setLiteral);
|
||||||
|
}
|
||||||
|
|
||||||
factory Formula.fromSet(Map<Object?, Object?> theSet) {
|
factory Formula.fromSet(Map<Object?, Object?> theSet) {
|
||||||
|
|
||||||
Object safeGet(Map<Object?, Object?> map, String key){
|
Object safeGet(Map<Object?, Object?> map, String key){
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ void main() {
|
||||||
],
|
],
|
||||||
output: VariableSpec(name: 'F', magnitude: 'force'),
|
output: VariableSpec(name: 'F', magnitude: 'force'),
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
return a * m;
|
F = a * m;
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -41,7 +41,7 @@ void main() {
|
||||||
],
|
],
|
||||||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
return x + y;
|
result = x + y;
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -56,7 +56,7 @@ void main() {
|
||||||
input: [VariableSpec(name: 'n', magnitude: 'scalar')],
|
input: [VariableSpec(name: 'n', magnitude: 'scalar')],
|
||||||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
return n * n;
|
result = n * n;
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -74,7 +74,7 @@ void main() {
|
||||||
],
|
],
|
||||||
output: VariableSpec(name: 'discriminant', magnitude: 'scalar'),
|
output: VariableSpec(name: 'discriminant', magnitude: 'scalar'),
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
return b * b - 4 * a * c;
|
discriminant = b * b - 4 * a * c;
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -94,7 +94,7 @@ void main() {
|
||||||
VariableSpec(name: 'b', magnitude: 'scalar'),
|
VariableSpec(name: 'b', magnitude: 'scalar'),
|
||||||
],
|
],
|
||||||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||||
d4rtCode: 'return a + b + z;',
|
d4rtCode: 'result = a + b + z;',
|
||||||
);
|
);
|
||||||
|
|
||||||
final order = evaluator.getInputVariableOrder(formula);
|
final order = evaluator.getInputVariableOrder(formula);
|
||||||
|
|
@ -112,7 +112,7 @@ void main() {
|
||||||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
// Variables: a=1, y=2, z=3
|
// Variables: a=1, y=2, z=3
|
||||||
return a * 100 + y * 10 + z;
|
result = a * 100 + y * 10 + z;
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -132,7 +132,7 @@ void main() {
|
||||||
VariableSpec(name: 'y', magnitude: 'scalar'),
|
VariableSpec(name: 'y', magnitude: 'scalar'),
|
||||||
],
|
],
|
||||||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||||
d4rtCode: 'return x + y;',
|
d4rtCode: 'result = x + y;',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
|
|
@ -162,7 +162,7 @@ void main() {
|
||||||
name: 'Test',
|
name: 'Test',
|
||||||
input: [VariableSpec(name: 'x', magnitude: 'scalar')],
|
input: [VariableSpec(name: 'x', magnitude: 'scalar')],
|
||||||
output: VariableSpec(name: 'force', magnitude: 'Newton'),
|
output: VariableSpec(name: 'force', magnitude: 'Newton'),
|
||||||
d4rtCode: 'return x;',
|
d4rtCode: 'force = x;',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(evaluator.getOutputVariableName(formula), 'force');
|
expect(evaluator.getOutputVariableName(formula), 'force');
|
||||||
|
|
@ -175,7 +175,7 @@ void main() {
|
||||||
name: 'Test',
|
name: 'Test',
|
||||||
input: [VariableSpec(name: 'x', magnitude: 'scalar')],
|
input: [VariableSpec(name: 'x', magnitude: 'scalar')],
|
||||||
output: VariableSpec(name: 'force', magnitude: 'Newton'),
|
output: VariableSpec(name: 'force', magnitude: 'Newton'),
|
||||||
d4rtCode: 'return x;',
|
d4rtCode: 'force = x;',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(evaluator.getOutputVariableMagnitude(formula), 'Newton');
|
expect(evaluator.getOutputVariableMagnitude(formula), 'Newton');
|
||||||
|
|
@ -187,7 +187,7 @@ void main() {
|
||||||
name: 'Valid Formula',
|
name: 'Valid Formula',
|
||||||
input: [VariableSpec(name: 'x', magnitude: 'scalar')],
|
input: [VariableSpec(name: 'x', magnitude: 'scalar')],
|
||||||
output: VariableSpec(name: 'result', magnitude: 'Newton'),
|
output: VariableSpec(name: 'result', magnitude: 'Newton'),
|
||||||
d4rtCode: 'return x;',
|
d4rtCode: 'result = x;',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(evaluator.getOutputVariableName(validFormula), 'result');
|
expect(evaluator.getOutputVariableName(validFormula), 'result');
|
||||||
|
|
@ -202,7 +202,7 @@ void main() {
|
||||||
name: 'Integer test',
|
name: 'Integer test',
|
||||||
input: [VariableSpec(name: 'n', magnitude: 'count')],
|
input: [VariableSpec(name: 'n', magnitude: 'count')],
|
||||||
output: VariableSpec(name: 'result', magnitude: 'count'),
|
output: VariableSpec(name: 'result', magnitude: 'count'),
|
||||||
d4rtCode: 'return n + 1;',
|
d4rtCode: 'result = n + 1;',
|
||||||
);
|
);
|
||||||
|
|
||||||
final result = evaluator.evaluate(formula, {'n': 42});
|
final result = evaluator.evaluate(formula, {'n': 42});
|
||||||
|
|
@ -214,7 +214,7 @@ void main() {
|
||||||
name: 'Double test',
|
name: 'Double test',
|
||||||
input: [VariableSpec(name: 'x', magnitude: 'length')],
|
input: [VariableSpec(name: 'x', magnitude: 'length')],
|
||||||
output: VariableSpec(name: 'result', magnitude: 'area'),
|
output: VariableSpec(name: 'result', magnitude: 'area'),
|
||||||
d4rtCode: 'return x * x;',
|
d4rtCode: 'result = x * x;',
|
||||||
);
|
);
|
||||||
|
|
||||||
final result = evaluator.evaluate(formula, {'x': 3.14});
|
final result = evaluator.evaluate(formula, {'x': 3.14});
|
||||||
|
|
|
||||||
|
|
@ -46,14 +46,7 @@ void main() {
|
||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
var d4rt = D4rt();
|
final formula = Formula.fromStringLiteral(literal);
|
||||||
final buffer = StringBuffer();
|
|
||||||
buffer.write( "main(){ return $literal; }");
|
|
||||||
final code = buffer.toString();
|
|
||||||
|
|
||||||
final Map<Object?, Object?> setLiteral = d4rt.execute(source: code);
|
|
||||||
|
|
||||||
final formula = Formula.fromSet(setLiteral);
|
|
||||||
final evaluator = FormulaEvaluator();
|
final evaluator = FormulaEvaluator();
|
||||||
|
|
||||||
final result = evaluator.evaluate(formula, {
|
final result = evaluator.evaluate(formula, {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue