main not present in d4rt_code in formula
This commit is contained in:
parent
52590cd2fb
commit
a1fb9057a7
4 changed files with 13 additions and 33 deletions
|
|
@ -23,9 +23,7 @@ void main() {
|
||||||
],
|
],
|
||||||
output: VariableSpec(name: 'F', magnitude: 'force'),
|
output: VariableSpec(name: 'F', magnitude: 'force'),
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
main() {
|
|
||||||
return m * a;
|
return m * a;
|
||||||
}
|
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -57,9 +55,7 @@ void main() {
|
||||||
],
|
],
|
||||||
output : VariableSpec(name: 'discriminant', magnitude: 'scalar'),
|
output : VariableSpec(name: 'discriminant', magnitude: 'scalar'),
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
main() {
|
|
||||||
return b * b - 4 * a * c;
|
return b * b - 4 * a * c;
|
||||||
}
|
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -97,10 +93,8 @@ void main() {
|
||||||
output: VariableSpec(name: 'A', magnitude: 'area'),
|
output: VariableSpec(name: 'A', magnitude: 'area'),
|
||||||
|
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
main() {
|
|
||||||
var pi = 3.14159265359;
|
var pi = 3.14159265359;
|
||||||
return pi * r * r;
|
return pi * r * r;
|
||||||
}
|
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -144,7 +138,6 @@ void main() {
|
||||||
],
|
],
|
||||||
output: VariableSpec(name: 'A', magnitude: 'currency'), // Final amount
|
output: VariableSpec(name: 'A', magnitude: 'currency'), // Final amount
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
main() {
|
|
||||||
// A = P * (1 + r/n)^(n*t)
|
// A = P * (1 + r/n)^(n*t)
|
||||||
var rate_per_period = r / n;
|
var rate_per_period = r / n;
|
||||||
var base = 1 + rate_per_period;
|
var base = 1 + rate_per_period;
|
||||||
|
|
@ -158,7 +151,6 @@ void main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,8 @@ class FormulaEvaluator {
|
||||||
try {
|
try {
|
||||||
// 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( "$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);
|
||||||
|
|
@ -99,7 +101,9 @@ class FormulaEvaluator {
|
||||||
/// before the formula's d4rt code
|
/// before the formula's d4rt code
|
||||||
String _buildCompleteSource(Formula formula, Map<String, dynamic> inputValues) {
|
String _buildCompleteSource(Formula formula, Map<String, dynamic> inputValues) {
|
||||||
final buffer = StringBuffer();
|
final buffer = StringBuffer();
|
||||||
|
|
||||||
|
buffer.writeln("main(){");
|
||||||
|
|
||||||
// Add variable declarations for all input variables
|
// Add variable declarations for all input variables
|
||||||
for (final entry in inputValues.entries) {
|
for (final entry in inputValues.entries) {
|
||||||
final varName = entry.key;
|
final varName = entry.key;
|
||||||
|
|
@ -121,6 +125,7 @@ class FormulaEvaluator {
|
||||||
|
|
||||||
// Add the formula's d4rt code
|
// Add the formula's d4rt code
|
||||||
buffer.write(formula.d4rtCode);
|
buffer.write(formula.d4rtCode);
|
||||||
|
buffer.write("}");
|
||||||
|
|
||||||
return buffer.toString();
|
return buffer.toString();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,7 @@ void main() {
|
||||||
],
|
],
|
||||||
output: VariableSpec(name: 'F', magnitude: 'force'),
|
output: VariableSpec(name: 'F', magnitude: 'force'),
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
main() {
|
|
||||||
return a * m;
|
return a * m;
|
||||||
}
|
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -43,9 +41,7 @@ void main() {
|
||||||
],
|
],
|
||||||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
main() {
|
|
||||||
return x + y;
|
return x + y;
|
||||||
}
|
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -60,9 +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: '''
|
||||||
main() {
|
|
||||||
return n * n;
|
return n * n;
|
||||||
}
|
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -80,9 +74,7 @@ void main() {
|
||||||
],
|
],
|
||||||
output: VariableSpec(name: 'discriminant', magnitude: 'scalar'),
|
output: VariableSpec(name: 'discriminant', magnitude: 'scalar'),
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
main() {
|
|
||||||
return b * b - 4 * a * c;
|
return b * b - 4 * a * c;
|
||||||
}
|
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -102,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: 'main() { return a + b + z; }',
|
d4rtCode: 'return a + b + z;',
|
||||||
);
|
);
|
||||||
|
|
||||||
final order = evaluator.getInputVariableOrder(formula);
|
final order = evaluator.getInputVariableOrder(formula);
|
||||||
|
|
@ -119,10 +111,8 @@ void main() {
|
||||||
],
|
],
|
||||||
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
output: VariableSpec(name: 'result', magnitude: 'scalar'),
|
||||||
d4rtCode: '''
|
d4rtCode: '''
|
||||||
main() {
|
|
||||||
// Variables: a=1, y=2, z=3
|
// Variables: a=1, y=2, z=3
|
||||||
return a * 100 + y * 10 + z;
|
return a * 100 + y * 10 + z;
|
||||||
}
|
|
||||||
''',
|
''',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -142,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: 'main() { return x + y; }',
|
d4rtCode: 'return x + y;',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
|
|
@ -172,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: 'main() { return x; }',
|
d4rtCode: 'return x;',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(evaluator.getOutputVariableName(formula), 'force');
|
expect(evaluator.getOutputVariableName(formula), 'force');
|
||||||
|
|
@ -185,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: 'main() { return x; }',
|
d4rtCode: 'return x;',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(evaluator.getOutputVariableMagnitude(formula), 'Newton');
|
expect(evaluator.getOutputVariableMagnitude(formula), 'Newton');
|
||||||
|
|
@ -197,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: 'main() { return x; }',
|
d4rtCode: 'return x;',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(evaluator.getOutputVariableName(validFormula), 'result');
|
expect(evaluator.getOutputVariableName(validFormula), 'result');
|
||||||
|
|
@ -212,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: 'main() { return n + 1; }',
|
d4rtCode: 'return n + 1;',
|
||||||
);
|
);
|
||||||
|
|
||||||
final result = evaluator.evaluate(formula, {'n': 42});
|
final result = evaluator.evaluate(formula, {'n': 42});
|
||||||
|
|
@ -224,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: 'main() { return x * x; }',
|
d4rtCode: 'return x * x;',
|
||||||
);
|
);
|
||||||
|
|
||||||
final result = evaluator.evaluate(formula, {'x': 3.14});
|
final result = evaluator.evaluate(formula, {'x': 3.14});
|
||||||
|
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
import 'package:test/test.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
test('calculate', () {
|
|
||||||
expect(6 * 7, 42);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue