A small standalone Lisp used as a scripting language in the Z2 game engine
Revision | 3af0b14c3dd8033386e215f8736b3c8328c238b7 (tree) |
---|---|
Time | 2019-07-28 06:27:02 |
Author | AlaskanEmily <emily@alas...> |
Commiter | AlaskanEmily |
Rename modules so that the standalone S-expresion parser is turbo_s and the Lisp runtime and interpreter are turbolisp
@@ -0,0 +1,83 @@ | ||
1 | +:- module turbo_s. | |
2 | +%=============================================================================% | |
3 | +% S-expression parser for TurboLisp | |
4 | +:- interface. | |
5 | +%=============================================================================% | |
6 | + | |
7 | +:- use_module io. | |
8 | +:- import_module list. | |
9 | +:- use_module maybe. | |
10 | + | |
11 | +:- include_module turbo_s.parser. | |
12 | +:- include_module turbo_s.string_stream. | |
13 | + | |
14 | +:- use_module turbo_s.string_stream. | |
15 | + | |
16 | +%-----------------------------------------------------------------------------% | |
17 | + | |
18 | +:- type element ---> atom(string) ; list(list.list(element)). | |
19 | + | |
20 | +%-----------------------------------------------------------------------------% | |
21 | + | |
22 | +:- pred parse(io.text_input_stream, list.list(element), io.res(list.list(element)), io.io, io.io). | |
23 | +:- mode parse(in, in, out, di, uo) is det. | |
24 | + | |
25 | +%-----------------------------------------------------------------------------% | |
26 | + | |
27 | +:- pred parse_string_stream(string, | |
28 | + list.list(element), | |
29 | + turbo_s.string_stream.string_range, | |
30 | + maybe.maybe_error(list.list(element))). | |
31 | +:- mode parse_string_stream(in, in, di, out) is det. | |
32 | + | |
33 | +%=============================================================================% | |
34 | +:- implementation. | |
35 | +%=============================================================================% | |
36 | + | |
37 | +:- use_module stream. | |
38 | + | |
39 | +:- use_module turbo_s.parser. | |
40 | + | |
41 | +%-----------------------------------------------------------------------------% | |
42 | + | |
43 | +:- func ss_errors = turbo_s.parser.errors(turbo_s.string_stream.error). | |
44 | +ss_errors = turbo_s.parser.errors( | |
45 | + turbo_s.string_stream.error("Unexpected EOF"), | |
46 | + turbo_s.string_stream.error("Unmatched close paren")). | |
47 | + | |
48 | +%-----------------------------------------------------------------------------% | |
49 | + | |
50 | +parse_string_stream(String, List, StringRangeIn, Result) :- | |
51 | + turbo_s.parser.parse(String, ss_errors, ElementResult, StringRangeIn, StringRangeOut), | |
52 | + ( | |
53 | + ElementResult = stream.ok(Element), | |
54 | + parse_string_stream(String, [Element|List], StringRangeOut, Result) | |
55 | + ; | |
56 | + ElementResult = stream.eof, | |
57 | + Result = maybe.ok(list.reverse(List)) | |
58 | + ; | |
59 | + ElementResult = stream.error(turbo_s.string_stream.error(E)), | |
60 | + Result = maybe.error(E) | |
61 | + ). | |
62 | + | |
63 | +%-----------------------------------------------------------------------------% | |
64 | + | |
65 | +:- func io_errors = turbo_s.parser.errors(io.error). | |
66 | +io_errors = turbo_s.parser.errors( | |
67 | + io.make_io_error("Unexpected EOF"), | |
68 | + io.make_io_error("Unmatched close paren")). | |
69 | + | |
70 | +%-----------------------------------------------------------------------------% | |
71 | + | |
72 | +parse(Stream, List, Result, !IO) :- | |
73 | + turbo_s.parser.parse(Stream, io_errors, ElementResult, !IO), | |
74 | + ( | |
75 | + ElementResult = stream.ok(Element), | |
76 | + parse(Stream, [Element|List], Result, !IO) | |
77 | + ; | |
78 | + ElementResult = stream.eof, | |
79 | + Result = io.ok(list.reverse(List)) | |
80 | + ; | |
81 | + ElementResult = stream.error(E), | |
82 | + Result = io.error(E) | |
83 | + ). | |
\ No newline at end of file |
@@ -1,6 +1,6 @@ | ||
1 | -:- module turbolisp.parser. | |
1 | +:- module turbo_s.parser. | |
2 | 2 | %=============================================================================% |
3 | -% Parser for TurboLisp | |
3 | +% Parser core for TurboLisps's S-expression parser. | |
4 | 4 | :- interface. |
5 | 5 | %=============================================================================% |
6 | 6 |
@@ -1,4 +1,4 @@ | ||
1 | -:- module turbolisp.string_stream. | |
1 | +:- module turbo_s.string_stream. | |
2 | 2 | %=============================================================================% |
3 | 3 | % Types and typeclass instances for streams using string ranges |
4 | 4 | :- interface. |
@@ -16,7 +16,7 @@ | ||
16 | 16 | |
17 | 17 | %-----------------------------------------------------------------------------% |
18 | 18 | |
19 | -:- type tl_err == turbolisp.string_stream.error. | |
19 | +:- type tl_err == turbo_s.string_stream.error. | |
20 | 20 | |
21 | 21 | %-----------------------------------------------------------------------------% |
22 | 22 |
@@ -1,19 +1,17 @@ | ||
1 | 1 | :- module turbolisp. |
2 | 2 | %=============================================================================% |
3 | -% Main module for TurboLisp. Includes wrappers for the parser. | |
4 | -% | |
5 | -% This does NOT include the runtime (see tl_runtime), and instead operates | |
6 | -% simply as an S-expression parser. | |
3 | +% Main module for TurboLisp. Includes wrappers for the S-expression parser. | |
7 | 4 | :- interface. |
8 | 5 | %=============================================================================% |
9 | 6 | |
10 | 7 | :- use_module io. |
11 | 8 | :- import_module list. |
9 | +:- use_module maybe. | |
12 | 10 | |
13 | -:- type element ---> atom(string) ; list(list.list(element)). | |
11 | +:- import_module turbo_s. | |
14 | 12 | |
15 | 13 | %-----------------------------------------------------------------------------% |
16 | - | |
14 | +% Parses using the current text_input_stream | |
17 | 15 | :- pred parse(io.res(list.list(element)), io.io, io.io). |
18 | 16 | :- mode parse(out, di, uo) is det. |
19 | 17 |
@@ -24,12 +22,12 @@ | ||
24 | 22 | |
25 | 23 | %-----------------------------------------------------------------------------% |
26 | 24 | |
27 | -:- pred parse_string(string, io.res(list.list(element))). | |
25 | +:- pred parse_string(string, maybe.maybe_error(list.list(element))). | |
28 | 26 | :- mode parse_string(in, out) is det. |
29 | 27 | |
30 | 28 | %-----------------------------------------------------------------------------% |
31 | 29 | % parse_string_between(Str, Start, End, Result) |
32 | -:- pred parse_string_between(string, int, int, io.res(list.list(element))). | |
30 | +:- pred parse_string_between(string, int, int, maybe.maybe_error(list.list(element))). | |
33 | 31 | :- mode parse_string_between(in, in, in, out) is det. |
34 | 32 | |
35 | 33 | %=============================================================================% |
@@ -38,15 +36,10 @@ | ||
38 | 36 | |
39 | 37 | :- use_module stream. |
40 | 38 | |
41 | -:- include_module turbolisp.parser. | |
42 | -:- include_module turbolisp.string_stream. | |
43 | - | |
44 | -:- use_module turbolisp.parser. | |
45 | -:- use_module turbolisp.string_stream. | |
46 | - | |
47 | -%-----------------------------------------------------------------------------% | |
39 | +:- use_module turbo_s.parser. | |
40 | +:- use_module turbo_s.string_stream. | |
48 | 41 | |
49 | -:- type list == list.list(element). | |
42 | +:- include_module turbolisp.runtime. | |
50 | 43 | |
51 | 44 | %-----------------------------------------------------------------------------% |
52 | 45 |
@@ -58,70 +51,29 @@ | ||
58 | 51 | |
59 | 52 | %-----------------------------------------------------------------------------% |
60 | 53 | |
61 | -parse(Result, !IO) :- io.input_stream(Stream, !IO), parse(Stream, Result, !IO). | |
62 | - | |
63 | -%-----------------------------------------------------------------------------% | |
64 | - | |
65 | -parse(Stream, Result, !IO) :- parse(Stream, [], Result, !IO). | |
54 | +parse(Result, !IO) :- | |
55 | + io.input_stream(Stream, !IO), | |
56 | + parse(Stream, Result, !IO). | |
66 | 57 | |
67 | 58 | %-----------------------------------------------------------------------------% |
68 | 59 | |
69 | -:- func io_errors = turbolisp.parser.errors(io.error). | |
70 | -io_errors = turbolisp.parser.errors( | |
71 | - io.make_io_error("Unexpected EOF"), | |
72 | - io.make_io_error("Unmatched close paren")). | |
73 | - | |
74 | -%-----------------------------------------------------------------------------% | |
75 | - | |
76 | -:- func ss_errors = turbolisp.parser.errors(turbolisp.string_stream.error). | |
77 | -ss_errors = turbolisp.parser.errors( | |
78 | - turbolisp.string_stream.error("Unexpected EOF"), | |
79 | - turbolisp.string_stream.error("Unmatched close paren")). | |
60 | +parse(Stream, Result, !IO) :- | |
61 | + turbo_s.parse(Stream, [], Result, !IO). | |
80 | 62 | |
81 | 63 | %-----------------------------------------------------------------------------% |
82 | 64 | |
83 | 65 | parse(Stream, List, Result, !IO) :- |
84 | - turbolisp.parser.parse(Stream, io_errors, ElementResult, !IO), | |
85 | - ( | |
86 | - ElementResult = stream.ok(Element), | |
87 | - parse(Stream, [Element|List], Result, !IO) | |
88 | - ; | |
89 | - ElementResult = stream.eof, | |
90 | - Result = io.ok(list.reverse(List)) | |
91 | - ; | |
92 | - ElementResult = stream.error(E), | |
93 | - Result = io.error(E) | |
94 | - ). | |
95 | - | |
96 | -%-----------------------------------------------------------------------------% | |
97 | - | |
98 | -:- pred parse_string_stream(string, | |
99 | - list.list(turbolisp.element), | |
100 | - turbolisp.string_stream.string_range, | |
101 | - io.res(list.list(element))). | |
102 | -:- mode parse_string_stream(in, in, di, out) is det. | |
103 | - | |
104 | -parse_string_stream(String, List, StringRangeIn, Result) :- | |
105 | - turbolisp.parser.parse(String, ss_errors, ElementResult, StringRangeIn, StringRangeOut), | |
106 | - ( | |
107 | - ElementResult = stream.ok(Element), | |
108 | - parse_string_stream(String, [Element|List], StringRangeOut, Result) | |
109 | - ; | |
110 | - ElementResult = stream.eof, | |
111 | - Result = io.ok(list.reverse(List)) | |
112 | - ; | |
113 | - ElementResult = stream.error(turbolisp.string_stream.error(E)), | |
114 | - Result = io.error(io.make_io_error(E)) | |
115 | - ). | |
66 | + turbo_s.parse(Stream, List, Result, !IO). | |
116 | 67 | |
117 | 68 | %-----------------------------------------------------------------------------% |
118 | 69 | |
119 | 70 | parse_string(Str, Result) :- |
120 | - parse_string_stream(Str, [], turbolisp.string_stream.init(Str), Result). | |
71 | + turbo_s.parse_string_stream(Str, [], turbo_s.string_stream.init(Str), Result). | |
121 | 72 | |
122 | 73 | %-----------------------------------------------------------------------------% |
123 | 74 | |
124 | 75 | parse_string_between(Str, Start, End, Result) :- |
125 | - parse_string_stream(Str, [], turbolisp.string_stream.init(Str, Start, End), Result). | |
76 | + Stream = turbo_s.string_stream.init(Str, Start, End), | |
77 | + turbo_s.parse_string_stream(Str, [], Stream, Result). | |
126 | 78 | |
127 | 79 | %-----------------------------------------------------------------------------% |
@@ -1,7 +1,7 @@ | ||
1 | 1 | :- module turbolisp.runtime.builtin.arithmetic. |
2 | 2 | |
3 | 3 | %=============================================================================% |
4 | -% Turbolisp implementation details for arithmetic builtins. | |
4 | +% TurboLisp implementation details for arithmetic builtins. | |
5 | 5 | :- interface. |
6 | 6 | %=============================================================================% |
7 | 7 |
@@ -1,7 +1,7 @@ | ||
1 | 1 | :- module turbolisp.runtime.builtin.comparison. |
2 | 2 | |
3 | 3 | %=============================================================================% |
4 | -% Turbolisp implementation details for comparison builtins. | |
4 | +% TurboLisp implementation details for comparison builtins. | |
5 | 5 | :- interface. |
6 | 6 | %=============================================================================% |
7 | 7 |
@@ -1,7 +1,7 @@ | ||
1 | 1 | :- module turbolisp.runtime.builtin. |
2 | 2 | |
3 | 3 | %=============================================================================% |
4 | -% Turbolisp builtins | |
4 | +% TurboLisp builtins | |
5 | 5 | :- interface. |
6 | 6 | %=============================================================================% |
7 | 7 |
@@ -1,6 +1,7 @@ | ||
1 | 1 | :- module turbolisp.runtime. |
2 | 2 | |
3 | 3 | %=============================================================================% |
4 | +% TurboLisp runtime components. | |
4 | 5 | :- interface. |
5 | 6 | %=============================================================================% |
6 | 7 |
@@ -10,12 +11,6 @@ | ||
10 | 11 | :- use_module maybe. |
11 | 12 | |
12 | 13 | %-----------------------------------------------------------------------------% |
13 | - | |
14 | -% TODO: Remove these | |
15 | - | |
16 | -:- type element ---> atom(string) ; list(list.list(element)). | |
17 | - | |
18 | -%-----------------------------------------------------------------------------% | |
19 | 14 | % TODO! |
20 | 15 | :- func nil = element. |
21 | 16 |
@@ -631,7 +626,7 @@ reduce(Element, Result, !Runtime) :- | ||
631 | 626 | turbolisp__runtime__builtin__define(Op), Tag) |
632 | 627 | then |
633 | 628 | ( |
634 | - Op = turbolisp.runtime.builtin.fn | |
629 | + Op = turbolisp.runtime.builtin.fn, | |
635 | 630 | % Super rudimentary inline test. |
636 | 631 | % Only inline fn if we have a body consisting of less than |
637 | 632 | % 64 elements, and all the elements are either atoms or a |