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