I am considering using petitparser for Dart (https://pub.dartlang.org/packages/petitparser) in my project. I want to use it to process Lisp code stored as Strings.
For example, given data like this:
(setq age 20)
(setq livesin "Mississippi")
And a String
that contains a Lisp expression like this:
'(and (< age 21) (string= livesin "Iowa"))'
How can I get a result?
Secondly, do you this is a good approach, and a proper use of petitparser?
Note that I am a Lisp newbie.
You can do this with the included example that provides a simple parser and evaluator. The following Dart code does what you want:
const data = '''
(define age 20)
(define livesin "Mississippi")
''';
const program = '(and (< age 21) (= livesin "Iowa"))';
void main() {
var environment = new NativeEnvironment().create();
evalString(lispParser, environment, data);
var result = evalString(lispParser, environment, program);
print("Result is $result");
}
Note, that I needed to change the function names in your data definition and program slightly, to make it execute. However, by redefining the primitives in example/lisp/src/native.dart
, it should be easy to adapt to your needs.
Have a look at test/lisp_test.dart
for more examples of how to parse code, how to manipulate environments, and how to evaluate code.