0

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.

Hesh
  • 413
  • 1
  • 3
  • 11
  • Did you look at https://github.com/petitparser/dart-petitparser/tree/master/example/lisp and https://www.dartdocs.org/documentation/petitparser/1.6.1/index.html? – coredump Oct 25 '17 at 11:19
  • Yes, I did. Thanks for pointing this out. I should have mentioned it in my question. I also looked at lispweb, implementation in the examples. – Hesh Oct 25 '17 at 11:50
  • I don't know Dart and don't have a proper environment to test it here. Shouldn't you just do something like `lp = new LispParser()` and `lp.parse(string)`? Does it work? Then you need to build an evaluator, but that's another problem. – coredump Oct 25 '17 at 12:41

1 Answers1

1

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.

Lukas Renggli
  • 8,754
  • 23
  • 46