platform/packages/combinator/test/recursion_test.dart

58 lines
1.2 KiB
Dart
Raw Normal View History

2021-03-17 23:04:36 +00:00
import 'package:combinator/combinator.dart';
import 'package:string_scanner/string_scanner.dart';
import 'package:test/test.dart';
void main() {}
/*
void main() {
2021-05-07 04:42:02 +00:00
var number = match( RegExp(r'-?[0-9]+(\.[0-9]+)?'))
2021-03-17 23:04:36 +00:00
.map<num>((r) => num.parse(r.span.text));
var term = reference<num>();
2021-05-07 04:42:02 +00:00
var r = Recursion<num>();
2021-03-17 23:04:36 +00:00
r.prefix = [number];
r.infix.addAll({
match('*'): (l, r, _) => l * r,
match('/'): (l, r, _) => l / r,
match('+'): (l, r, _) => l + r,
match('-'): (l, r, _) => l - r,
match('-'): (l, r, _) => l - r,
match('+'): (l, r, _) => l + r,
match('/'): (l, r, _) => l / r,
match('*'): (l, r, _) => l * r,
});
term.parser = r.precedence(0);
num parse(String text) {
2021-05-07 04:42:02 +00:00
var scanner = SpanScanner(text);
2021-03-17 23:04:36 +00:00
var result = term.parse(scanner);
print(result.span.highlight());
return result.value;
}
test('prefix', () {
expect(parse('24'), 24);
});
test('infix', () {
expect(parse('12/6'), 2);
expect(parse('24+23'), 47);
expect(parse('24-23'), 1);
expect(parse('4*3'), 12);
});
test('precedence', () {
expect(parse('2+3*5*2'), 15);
//expect(parse('2+3+5-2*2'), 15);
});
}
*/