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
|
||||
|
||||
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)",
|
||||
|
|
@ -29,10 +29,9 @@ The file is a json array of formulas.
|
|||
"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
|
||||
final completeSource = _buildCompleteSource(formula, inputValues);
|
||||
//print( "$formula");
|
||||
//print( "$completeSource" );
|
||||
print( "$completeSource" );
|
||||
|
||||
// Execute the code using d4rt (no args needed since variables are in source)
|
||||
final result = _interpreter.execute(source: completeSource);
|
||||
|
|
@ -119,13 +119,12 @@ class FormulaEvaluator {
|
|||
buffer.writeln('var $varName = $value;');
|
||||
}
|
||||
}
|
||||
|
||||
// Add a blank line for readability
|
||||
buffer.writeln();
|
||||
buffer.writeln("late var ${getOutputVariableName(formula)};");
|
||||
|
||||
// Add the formula's d4rt code
|
||||
buffer.write(formula.d4rtCode);
|
||||
buffer.write("}");
|
||||
buffer.writeln(formula.d4rtCode);
|
||||
buffer.writeln("return ${getOutputVariableName(formula)};");
|
||||
buffer.writeln("}");
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,6 +64,17 @@ class Formula {
|
|||
List<String> inputVarNames() =>
|
||||
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) {
|
||||
|
||||
Object safeGet(Map<Object?, Object?> map, String key){
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ void main() {
|
|||
],
|
||||
output: VariableSpec(name: 'F', magnitude: 'force'),
|
||||
d4rtCode: '''
|
||||
return a * m;
|
||||
F = a * m;
|
||||
''',
|
||||
);
|
||||
|
||||
|
|
@ -41,7 +41,7 @@ void main() {
|
|||
],
|
||||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||
d4rtCode: '''
|
||||
return x + y;
|
||||
result = x + y;
|
||||
''',
|
||||
);
|
||||
|
||||
|
|
@ -56,7 +56,7 @@ void main() {
|
|||
input: [VariableSpec(name: 'n', magnitude: 'scalar')],
|
||||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||
d4rtCode: '''
|
||||
return n * n;
|
||||
result = n * n;
|
||||
''',
|
||||
);
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ void main() {
|
|||
],
|
||||
output: VariableSpec(name: 'discriminant', magnitude: 'scalar'),
|
||||
d4rtCode: '''
|
||||
return b * b - 4 * a * c;
|
||||
discriminant = b * b - 4 * a * c;
|
||||
''',
|
||||
);
|
||||
|
||||
|
|
@ -94,7 +94,7 @@ void main() {
|
|||
VariableSpec(name: 'b', magnitude: 'scalar'),
|
||||
],
|
||||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||
d4rtCode: 'return a + b + z;',
|
||||
d4rtCode: 'result = a + b + z;',
|
||||
);
|
||||
|
||||
final order = evaluator.getInputVariableOrder(formula);
|
||||
|
|
@ -112,7 +112,7 @@ void main() {
|
|||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||
d4rtCode: '''
|
||||
// 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'),
|
||||
],
|
||||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||
d4rtCode: 'return x + y;',
|
||||
d4rtCode: 'result = x + y;',
|
||||
);
|
||||
|
||||
expect(
|
||||
|
|
@ -162,7 +162,7 @@ void main() {
|
|||
name: 'Test',
|
||||
input: [VariableSpec(name: 'x', magnitude: 'scalar')],
|
||||
output: VariableSpec(name: 'force', magnitude: 'Newton'),
|
||||
d4rtCode: 'return x;',
|
||||
d4rtCode: 'force = x;',
|
||||
);
|
||||
|
||||
expect(evaluator.getOutputVariableName(formula), 'force');
|
||||
|
|
@ -175,7 +175,7 @@ void main() {
|
|||
name: 'Test',
|
||||
input: [VariableSpec(name: 'x', magnitude: 'scalar')],
|
||||
output: VariableSpec(name: 'force', magnitude: 'Newton'),
|
||||
d4rtCode: 'return x;',
|
||||
d4rtCode: 'force = x;',
|
||||
);
|
||||
|
||||
expect(evaluator.getOutputVariableMagnitude(formula), 'Newton');
|
||||
|
|
@ -187,7 +187,7 @@ void main() {
|
|||
name: 'Valid Formula',
|
||||
input: [VariableSpec(name: 'x', magnitude: 'scalar')],
|
||||
output: VariableSpec(name: 'result', magnitude: 'Newton'),
|
||||
d4rtCode: 'return x;',
|
||||
d4rtCode: 'result = x;',
|
||||
);
|
||||
|
||||
expect(evaluator.getOutputVariableName(validFormula), 'result');
|
||||
|
|
@ -202,7 +202,7 @@ void main() {
|
|||
name: 'Integer test',
|
||||
input: [VariableSpec(name: 'n', magnitude: 'count')],
|
||||
output: VariableSpec(name: 'result', magnitude: 'count'),
|
||||
d4rtCode: 'return n + 1;',
|
||||
d4rtCode: 'result = n + 1;',
|
||||
);
|
||||
|
||||
final result = evaluator.evaluate(formula, {'n': 42});
|
||||
|
|
@ -214,7 +214,7 @@ void main() {
|
|||
name: 'Double test',
|
||||
input: [VariableSpec(name: 'x', magnitude: 'length')],
|
||||
output: VariableSpec(name: 'result', magnitude: 'area'),
|
||||
d4rtCode: 'return x * x;',
|
||||
d4rtCode: 'result = x * x;',
|
||||
);
|
||||
|
||||
final result = evaluator.evaluate(formula, {'x': 3.14});
|
||||
|
|
|
|||
|
|
@ -46,14 +46,7 @@ void main() {
|
|||
}
|
||||
""";
|
||||
|
||||
var d4rt = D4rt();
|
||||
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 formula = Formula.fromStringLiteral(literal);
|
||||
final evaluator = FormulaEvaluator();
|
||||
|
||||
final result = evaluator.evaluate(formula, {
|
||||
|
|
|
|||
Loading…
Reference in a new issue