OLD | NEW |
| (Empty) |
1 /** | |
2 * This package contains a simple grammar and evaluator for LISP. | |
3 * | |
4 * The code is reasonably complete to run and evaluate reasonably complex | |
5 * programs from the console and from the web browser. | |
6 */ | |
7 library lisp; | |
8 | |
9 import 'dart:collection'; | |
10 import 'package:petitparser/petitparser.dart'; | |
11 | |
12 part 'src/lisp/cons.dart'; | |
13 part 'src/lisp/environment.dart'; | |
14 part 'src/lisp/grammar.dart'; | |
15 part 'src/lisp/name.dart'; | |
16 part 'src/lisp/natives.dart'; | |
17 part 'src/lisp/parser.dart'; | |
18 part 'src/lisp/standard.dart'; | |
19 | |
20 /** The standard lisp parser definition. */ | |
21 final lispParser = new LispParser(); | |
22 | |
23 /** The evaluation function. */ | |
24 eval(Environment env, expr) { | |
25 if (expr is Cons) { | |
26 return eval(env, expr.head)(env, expr.tail); | |
27 } else if (expr is Name) { | |
28 return env[expr]; | |
29 } else { | |
30 return expr; | |
31 } | |
32 } | |
33 | |
34 /** Evaluate a cons of instructions. */ | |
35 evalList(Environment env, expr) { | |
36 var result = null; | |
37 while (expr is Cons) { | |
38 result = eval(env, expr.head); | |
39 expr = expr.tail; | |
40 } | |
41 return result; | |
42 } | |
43 | |
44 /** The arguments evaluation function. */ | |
45 evalArguments(Environment env, args) { | |
46 if (args is Cons) { | |
47 return new Cons(eval(env, args.head), evalArguments(env, args.tail)); | |
48 } else { | |
49 return null; | |
50 } | |
51 } | |
52 | |
53 /** Reads and evaluates a [script]. */ | |
54 evalString(Parser parser, Environment env, String script) { | |
55 var result = null; | |
56 for (var cell in parser.parse(script).value) { | |
57 result = eval(env, cell); | |
58 } | |
59 return result; | |
60 } | |
OLD | NEW |