preparado para expresiones en los valores de entrada
This commit is contained in:
parent
daa8b31879
commit
5a8f9de2a1
4 changed files with 40 additions and 14 deletions
|
|
@ -56,7 +56,9 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
||||||
try {
|
try {
|
||||||
final inputValues = <String, dynamic>{};
|
final inputValues = <String, dynamic>{};
|
||||||
for (final input in widget.formula.input) {
|
for (final input in widget.formula.input) {
|
||||||
final value = double.tryParse(_inputControllers[input.name]!.text) ?? 0.0;
|
final text = _inputControllers[input.name]!.text;
|
||||||
|
//final value = double.tryParse(text) ?? 0.0;
|
||||||
|
final value = FormulaEvaluator.evaluateExpression(text);
|
||||||
|
|
||||||
// Convert input to base unit if needed
|
// Convert input to base unit if needed
|
||||||
// Always convert from dropdown unit to variable's base unit
|
// Always convert from dropdown unit to variable's base unit
|
||||||
|
|
|
||||||
|
|
@ -21,34 +21,36 @@ class FormulaEvaluationException implements Exception {
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyMath{
|
class MyMath{
|
||||||
static Number log(Number x) => Math.log(x);
|
static Number myLog(Number x) => Math.log(x);
|
||||||
static Number pow(Number b, Number e) => Math.pow(b,e) as Number;
|
static Number myPow(Number b, Number e) => Math.pow(b,e) as Number;
|
||||||
}
|
}
|
||||||
|
|
||||||
class FormulaEvaluator {
|
class FormulaEvaluator {
|
||||||
final D4rt _interpreter;
|
final D4rt _interpreter;
|
||||||
|
|
||||||
FormulaEvaluator([D4rt? interpreter]) : _interpreter = interpreter ?? D4rt(){
|
static D4rt createDefaultInterpreter() => D4rt();
|
||||||
|
|
||||||
|
FormulaEvaluator([D4rt? interpreter]) : _interpreter = interpreter ?? createDefaultInterpreter(){
|
||||||
prepareInterpreter(_interpreter);
|
prepareInterpreter(_interpreter);
|
||||||
}
|
}
|
||||||
|
|
||||||
Number getNumberValueOf(String s){
|
static Number getNumberValueOf(String s){
|
||||||
return double.parse(s);
|
return double.parse(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareInterpreter(D4rt interpreter){
|
static void prepareInterpreter(D4rt interpreter){
|
||||||
final myMathDefinition = BridgedClass(
|
final myMathDefinition = BridgedClass(
|
||||||
nativeType: MyMath,
|
nativeType: MyMath,
|
||||||
name: 'Math',
|
name: 'MyMath',
|
||||||
staticMethods: {
|
staticMethods: {
|
||||||
'pow': (visitor, positionalArgs, namedArgs) {
|
'myPow': (visitor, positionalArgs, namedArgs) {
|
||||||
final Number base = getNumberValueOf( positionalArgs[0].toString() );
|
final Number base = getNumberValueOf( positionalArgs[0].toString() );
|
||||||
final Number exp = getNumberValueOf( positionalArgs[1].toString() );
|
final Number exp = getNumberValueOf( positionalArgs[1].toString() );
|
||||||
return MyMath.pow(base,exp);
|
return MyMath.myPow(base,exp);
|
||||||
},
|
},
|
||||||
'log': (visitor, positionalArgs, namedArgs) {
|
'myLog': (visitor, positionalArgs, namedArgs) {
|
||||||
final Number x = getNumberValueOf( positionalArgs[0].toString() );
|
final Number x = getNumberValueOf( positionalArgs[0].toString() );
|
||||||
return MyMath.log(x);
|
return MyMath.myLog(x);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
@ -56,6 +58,20 @@ class FormulaEvaluator {
|
||||||
interpreter.registerBridgedClass(myMathDefinition, "package:d4rt_formulas.dart");
|
interpreter.registerBridgedClass(myMathDefinition, "package:d4rt_formulas.dart");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static dynamic evaluateExpression(String code) {
|
||||||
|
final interpreter = createDefaultInterpreter();
|
||||||
|
prepareInterpreter(interpreter);
|
||||||
|
final d4rtCode = """
|
||||||
|
import 'dart:math';
|
||||||
|
import "package:d4rt_formulas.dart";
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
return $code;
|
||||||
|
}""";
|
||||||
|
final result = interpreter.execute(source: d4rtCode);
|
||||||
|
return result.toDouble();
|
||||||
|
}
|
||||||
|
|
||||||
dynamic evaluate(Formula formula, Map<String, dynamic> inputValues) {
|
dynamic evaluate(Formula formula, Map<String, dynamic> inputValues) {
|
||||||
_validateInputValues(formula, inputValues);
|
_validateInputValues(formula, inputValues);
|
||||||
|
|
@ -98,9 +114,8 @@ class FormulaEvaluator {
|
||||||
|
|
||||||
buffer.writeln("""
|
buffer.writeln("""
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
import "package:d4rt_formulas.dart";
|
||||||
|
|
||||||
//log(x) => M.log(x);
|
|
||||||
//pow(a,b) => M.pow(a,b);
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -102,7 +102,6 @@ class UnitSpec {
|
||||||
class VariableSpec {
|
class VariableSpec {
|
||||||
final String name;
|
final String name;
|
||||||
final String unit;
|
final String unit;
|
||||||
static final MAGNITUDELESS = "magnitudeless";
|
|
||||||
|
|
||||||
VariableSpec({required this.name, required this.unit});
|
VariableSpec({required this.name, required this.unit});
|
||||||
|
|
||||||
|
|
|
||||||
10
test/dart_test.dart
Normal file
10
test/dart_test.dart
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'package:d4rt/d4rt.dart';
|
||||||
|
import 'dart:math' as Math;
|
||||||
|
|
||||||
|
|
||||||
|
main(){
|
||||||
|
test('for dart grammar tests', () {
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue