├── .gitignore
├── Cargo.lock
├── Cargo.toml
├── LICENSE
├── README.md
├── examples
└── saw.synt
├── src
├── cli
│ └── main.rs
└── interpreter
│ ├── ast.rs
│ ├── audio
│ ├── filewriter.rs
│ ├── mod.rs
│ └── stream.rs
│ ├── codegen.rs
│ ├── common.rs
│ ├── compiler.rs
│ ├── functions.rs
│ ├── ident.rs
│ ├── issue.rs
│ ├── lexer.rs
│ ├── lib.rs
│ ├── parser.rs
│ ├── scope.rs
│ ├── tests.rs
│ ├── tokens.rs
│ ├── typecheck.rs
│ └── types.rs
└── tests
├── codegen.rs
├── lexer.rs
├── parser.rs
└── typecheck.rs
/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | *.swp
3 |
--------------------------------------------------------------------------------
/Cargo.lock:
--------------------------------------------------------------------------------
1 | [root]
2 | name = "synthizer"
3 | version = "0.0.1"
4 | dependencies = [
5 | "bit-set 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
6 | "cbox 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
7 | "clippy 0.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
8 | "docopt 0.6.72 (registry+https://github.com/rust-lang/crates.io-index)",
9 | "docopt_macros 0.6.72 (registry+https://github.com/rust-lang/crates.io-index)",
10 | "hound 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
11 | "llvm-alt 0.3.3 (git+https://github.com/nwoeanhinnogaehr/llvm-rs)",
12 | "llvm-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
13 | "regex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
14 | "regex_macros 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
15 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
16 | "sound_stream 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
17 | "vec_map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
18 | ]
19 |
20 | [[package]]
21 | name = "advapi32-sys"
22 | version = "0.1.2"
23 | source = "registry+https://github.com/rust-lang/crates.io-index"
24 | dependencies = [
25 | "winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
26 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
27 | ]
28 |
29 | [[package]]
30 | name = "aho-corasick"
31 | version = "0.3.2"
32 | source = "registry+https://github.com/rust-lang/crates.io-index"
33 | dependencies = [
34 | "memchr 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
35 | ]
36 |
37 | [[package]]
38 | name = "bit-set"
39 | version = "0.2.0"
40 | source = "registry+https://github.com/rust-lang/crates.io-index"
41 | dependencies = [
42 | "bit-vec 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
43 | ]
44 |
45 | [[package]]
46 | name = "bit-vec"
47 | version = "0.4.1"
48 | source = "registry+https://github.com/rust-lang/crates.io-index"
49 |
50 | [[package]]
51 | name = "bitflags"
52 | version = "0.3.2"
53 | source = "registry+https://github.com/rust-lang/crates.io-index"
54 |
55 | [[package]]
56 | name = "cbox"
57 | version = "0.2.2"
58 | source = "registry+https://github.com/rust-lang/crates.io-index"
59 | dependencies = [
60 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
61 | ]
62 |
63 | [[package]]
64 | name = "clippy"
65 | version = "0.0.13"
66 | source = "registry+https://github.com/rust-lang/crates.io-index"
67 | dependencies = [
68 | "unicode-normalization 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
69 | ]
70 |
71 | [[package]]
72 | name = "docopt"
73 | version = "0.6.72"
74 | source = "registry+https://github.com/rust-lang/crates.io-index"
75 | dependencies = [
76 | "regex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
77 | "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
78 | "strsim 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
79 | ]
80 |
81 | [[package]]
82 | name = "docopt_macros"
83 | version = "0.6.72"
84 | source = "registry+https://github.com/rust-lang/crates.io-index"
85 | dependencies = [
86 | "docopt 0.6.72 (registry+https://github.com/rust-lang/crates.io-index)",
87 | ]
88 |
89 | [[package]]
90 | name = "gcc"
91 | version = "0.3.13"
92 | source = "registry+https://github.com/rust-lang/crates.io-index"
93 | dependencies = [
94 | "advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
95 | "winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
96 | ]
97 |
98 | [[package]]
99 | name = "hound"
100 | version = "1.0.0"
101 | source = "registry+https://github.com/rust-lang/crates.io-index"
102 |
103 | [[package]]
104 | name = "kernel32-sys"
105 | version = "0.1.4"
106 | source = "registry+https://github.com/rust-lang/crates.io-index"
107 | dependencies = [
108 | "winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
109 | "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
110 | ]
111 |
112 | [[package]]
113 | name = "libc"
114 | version = "0.1.10"
115 | source = "registry+https://github.com/rust-lang/crates.io-index"
116 |
117 | [[package]]
118 | name = "llvm-alt"
119 | version = "0.3.3"
120 | source = "git+https://github.com/nwoeanhinnogaehr/llvm-rs#c2fbd15ca6d46be91166271036027df5fbd0baa3"
121 | dependencies = [
122 | "cbox 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
123 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
124 | "llvm-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
125 | ]
126 |
127 | [[package]]
128 | name = "llvm-sys"
129 | version = "0.2.0"
130 | source = "registry+https://github.com/rust-lang/crates.io-index"
131 | dependencies = [
132 | "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
133 | "gcc 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)",
134 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
135 | "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
136 | ]
137 |
138 | [[package]]
139 | name = "memchr"
140 | version = "0.1.6"
141 | source = "registry+https://github.com/rust-lang/crates.io-index"
142 | dependencies = [
143 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
144 | ]
145 |
146 | [[package]]
147 | name = "num"
148 | version = "0.1.27"
149 | source = "registry+https://github.com/rust-lang/crates.io-index"
150 |
151 | [[package]]
152 | name = "pkg-config"
153 | version = "0.3.5"
154 | source = "registry+https://github.com/rust-lang/crates.io-index"
155 |
156 | [[package]]
157 | name = "portaudio"
158 | version = "0.4.17"
159 | source = "registry+https://github.com/rust-lang/crates.io-index"
160 | dependencies = [
161 | "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
162 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
163 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)",
164 | "pkg-config 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
165 | ]
166 |
167 | [[package]]
168 | name = "regex"
169 | version = "0.1.41"
170 | source = "registry+https://github.com/rust-lang/crates.io-index"
171 | dependencies = [
172 | "aho-corasick 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
173 | "memchr 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
174 | "regex-syntax 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
175 | ]
176 |
177 | [[package]]
178 | name = "regex-syntax"
179 | version = "0.2.1"
180 | source = "registry+https://github.com/rust-lang/crates.io-index"
181 |
182 | [[package]]
183 | name = "regex_macros"
184 | version = "0.1.21"
185 | source = "registry+https://github.com/rust-lang/crates.io-index"
186 | dependencies = [
187 | "regex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
188 | ]
189 |
190 | [[package]]
191 | name = "rustc-serialize"
192 | version = "0.3.16"
193 | source = "registry+https://github.com/rust-lang/crates.io-index"
194 |
195 | [[package]]
196 | name = "sample"
197 | version = "0.1.3"
198 | source = "registry+https://github.com/rust-lang/crates.io-index"
199 |
200 | [[package]]
201 | name = "semver"
202 | version = "0.1.20"
203 | source = "registry+https://github.com/rust-lang/crates.io-index"
204 |
205 | [[package]]
206 | name = "sound_stream"
207 | version = "0.4.3"
208 | source = "registry+https://github.com/rust-lang/crates.io-index"
209 | dependencies = [
210 | "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)",
211 | "portaudio 0.4.17 (registry+https://github.com/rust-lang/crates.io-index)",
212 | "sample 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
213 | "time 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
214 | ]
215 |
216 | [[package]]
217 | name = "strsim"
218 | version = "0.3.0"
219 | source = "registry+https://github.com/rust-lang/crates.io-index"
220 |
221 | [[package]]
222 | name = "time"
223 | version = "0.1.32"
224 | source = "registry+https://github.com/rust-lang/crates.io-index"
225 | dependencies = [
226 | "kernel32-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
227 | "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
228 | "winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
229 | ]
230 |
231 | [[package]]
232 | name = "unicode-normalization"
233 | version = "0.1.1"
234 | source = "registry+https://github.com/rust-lang/crates.io-index"
235 |
236 | [[package]]
237 | name = "vec_map"
238 | version = "0.3.0"
239 | source = "registry+https://github.com/rust-lang/crates.io-index"
240 |
241 | [[package]]
242 | name = "winapi"
243 | version = "0.2.2"
244 | source = "registry+https://github.com/rust-lang/crates.io-index"
245 |
246 | [[package]]
247 | name = "winapi-build"
248 | version = "0.1.1"
249 | source = "registry+https://github.com/rust-lang/crates.io-index"
250 |
251 |
--------------------------------------------------------------------------------
/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 |
3 | name = "synthizer"
4 | version = "0.0.1"
5 | authors = ["Noah Weninger"]
6 |
7 | [dependencies]
8 | rustc-serialize = "*"
9 | regex = "*"
10 | regex_macros = "*"
11 | cbox = "*"
12 | bit-set = "*"
13 | sound_stream = "*"
14 | hound = "*"
15 | vec_map = "*"
16 | docopt = "*"
17 | docopt_macros = "*"
18 | llvm-sys = "*"
19 | clippy = "*"
20 |
21 | [dependencies.llvm-alt]
22 | git = "https://github.com/nwoeanhinnogaehr/llvm-rs"
23 |
24 | [lib]
25 | name = "interpreter"
26 | path = "src/interpreter/lib.rs"
27 |
28 | [[bin]]
29 | name = "synthizer"
30 | path = "src/cli/main.rs"
31 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Mozilla Public License, version 2.0
2 |
3 | 1. Definitions
4 |
5 | 1.1. "Contributor"
6 |
7 | means each individual or legal entity that creates, contributes to the
8 | creation of, or owns Covered Software.
9 |
10 | 1.2. "Contributor Version"
11 |
12 | means the combination of the Contributions of others (if any) used by a
13 | Contributor and that particular Contributor's Contribution.
14 |
15 | 1.3. "Contribution"
16 |
17 | means Covered Software of a particular Contributor.
18 |
19 | 1.4. "Covered Software"
20 |
21 | means Source Code Form to which the initial Contributor has attached the
22 | notice in Exhibit A, the Executable Form of such Source Code Form, and
23 | Modifications of such Source Code Form, in each case including portions
24 | thereof.
25 |
26 | 1.5. "Incompatible With Secondary Licenses"
27 | means
28 |
29 | a. that the initial Contributor has attached the notice described in
30 | Exhibit B to the Covered Software; or
31 |
32 | b. that the Covered Software was made available under the terms of
33 | version 1.1 or earlier of the License, but not also under the terms of
34 | a Secondary License.
35 |
36 | 1.6. "Executable Form"
37 |
38 | means any form of the work other than Source Code Form.
39 |
40 | 1.7. "Larger Work"
41 |
42 | means a work that combines Covered Software with other material, in a
43 | separate file or files, that is not Covered Software.
44 |
45 | 1.8. "License"
46 |
47 | means this document.
48 |
49 | 1.9. "Licensable"
50 |
51 | means having the right to grant, to the maximum extent possible, whether
52 | at the time of the initial grant or subsequently, any and all of the
53 | rights conveyed by this License.
54 |
55 | 1.10. "Modifications"
56 |
57 | means any of the following:
58 |
59 | a. any file in Source Code Form that results from an addition to,
60 | deletion from, or modification of the contents of Covered Software; or
61 |
62 | b. any new file in Source Code Form that contains any Covered Software.
63 |
64 | 1.11. "Patent Claims" of a Contributor
65 |
66 | means any patent claim(s), including without limitation, method,
67 | process, and apparatus claims, in any patent Licensable by such
68 | Contributor that would be infringed, but for the grant of the License,
69 | by the making, using, selling, offering for sale, having made, import,
70 | or transfer of either its Contributions or its Contributor Version.
71 |
72 | 1.12. "Secondary License"
73 |
74 | means either the GNU General Public License, Version 2.0, the GNU Lesser
75 | General Public License, Version 2.1, the GNU Affero General Public
76 | License, Version 3.0, or any later versions of those licenses.
77 |
78 | 1.13. "Source Code Form"
79 |
80 | means the form of the work preferred for making modifications.
81 |
82 | 1.14. "You" (or "Your")
83 |
84 | means an individual or a legal entity exercising rights under this
85 | License. For legal entities, "You" includes any entity that controls, is
86 | controlled by, or is under common control with You. For purposes of this
87 | definition, "control" means (a) the power, direct or indirect, to cause
88 | the direction or management of such entity, whether by contract or
89 | otherwise, or (b) ownership of more than fifty percent (50%) of the
90 | outstanding shares or beneficial ownership of such entity.
91 |
92 |
93 | 2. License Grants and Conditions
94 |
95 | 2.1. Grants
96 |
97 | Each Contributor hereby grants You a world-wide, royalty-free,
98 | non-exclusive license:
99 |
100 | a. under intellectual property rights (other than patent or trademark)
101 | Licensable by such Contributor to use, reproduce, make available,
102 | modify, display, perform, distribute, and otherwise exploit its
103 | Contributions, either on an unmodified basis, with Modifications, or
104 | as part of a Larger Work; and
105 |
106 | b. under Patent Claims of such Contributor to make, use, sell, offer for
107 | sale, have made, import, and otherwise transfer either its
108 | Contributions or its Contributor Version.
109 |
110 | 2.2. Effective Date
111 |
112 | The licenses granted in Section 2.1 with respect to any Contribution
113 | become effective for each Contribution on the date the Contributor first
114 | distributes such Contribution.
115 |
116 | 2.3. Limitations on Grant Scope
117 |
118 | The licenses granted in this Section 2 are the only rights granted under
119 | this License. No additional rights or licenses will be implied from the
120 | distribution or licensing of Covered Software under this License.
121 | Notwithstanding Section 2.1(b) above, no patent license is granted by a
122 | Contributor:
123 |
124 | a. for any code that a Contributor has removed from Covered Software; or
125 |
126 | b. for infringements caused by: (i) Your and any other third party's
127 | modifications of Covered Software, or (ii) the combination of its
128 | Contributions with other software (except as part of its Contributor
129 | Version); or
130 |
131 | c. under Patent Claims infringed by Covered Software in the absence of
132 | its Contributions.
133 |
134 | This License does not grant any rights in the trademarks, service marks,
135 | or logos of any Contributor (except as may be necessary to comply with
136 | the notice requirements in Section 3.4).
137 |
138 | 2.4. Subsequent Licenses
139 |
140 | No Contributor makes additional grants as a result of Your choice to
141 | distribute the Covered Software under a subsequent version of this
142 | License (see Section 10.2) or under the terms of a Secondary License (if
143 | permitted under the terms of Section 3.3).
144 |
145 | 2.5. Representation
146 |
147 | Each Contributor represents that the Contributor believes its
148 | Contributions are its original creation(s) or it has sufficient rights to
149 | grant the rights to its Contributions conveyed by this License.
150 |
151 | 2.6. Fair Use
152 |
153 | This License is not intended to limit any rights You have under
154 | applicable copyright doctrines of fair use, fair dealing, or other
155 | equivalents.
156 |
157 | 2.7. Conditions
158 |
159 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
160 | Section 2.1.
161 |
162 |
163 | 3. Responsibilities
164 |
165 | 3.1. Distribution of Source Form
166 |
167 | All distribution of Covered Software in Source Code Form, including any
168 | Modifications that You create or to which You contribute, must be under
169 | the terms of this License. You must inform recipients that the Source
170 | Code Form of the Covered Software is governed by the terms of this
171 | License, and how they can obtain a copy of this License. You may not
172 | attempt to alter or restrict the recipients' rights in the Source Code
173 | Form.
174 |
175 | 3.2. Distribution of Executable Form
176 |
177 | If You distribute Covered Software in Executable Form then:
178 |
179 | a. such Covered Software must also be made available in Source Code Form,
180 | as described in Section 3.1, and You must inform recipients of the
181 | Executable Form how they can obtain a copy of such Source Code Form by
182 | reasonable means in a timely manner, at a charge no more than the cost
183 | of distribution to the recipient; and
184 |
185 | b. You may distribute such Executable Form under the terms of this
186 | License, or sublicense it under different terms, provided that the
187 | license for the Executable Form does not attempt to limit or alter the
188 | recipients' rights in the Source Code Form under this License.
189 |
190 | 3.3. Distribution of a Larger Work
191 |
192 | You may create and distribute a Larger Work under terms of Your choice,
193 | provided that You also comply with the requirements of this License for
194 | the Covered Software. If the Larger Work is a combination of Covered
195 | Software with a work governed by one or more Secondary Licenses, and the
196 | Covered Software is not Incompatible With Secondary Licenses, this
197 | License permits You to additionally distribute such Covered Software
198 | under the terms of such Secondary License(s), so that the recipient of
199 | the Larger Work may, at their option, further distribute the Covered
200 | Software under the terms of either this License or such Secondary
201 | License(s).
202 |
203 | 3.4. Notices
204 |
205 | You may not remove or alter the substance of any license notices
206 | (including copyright notices, patent notices, disclaimers of warranty, or
207 | limitations of liability) contained within the Source Code Form of the
208 | Covered Software, except that You may alter any license notices to the
209 | extent required to remedy known factual inaccuracies.
210 |
211 | 3.5. Application of Additional Terms
212 |
213 | You may choose to offer, and to charge a fee for, warranty, support,
214 | indemnity or liability obligations to one or more recipients of Covered
215 | Software. However, You may do so only on Your own behalf, and not on
216 | behalf of any Contributor. You must make it absolutely clear that any
217 | such warranty, support, indemnity, or liability obligation is offered by
218 | You alone, and You hereby agree to indemnify every Contributor for any
219 | liability incurred by such Contributor as a result of warranty, support,
220 | indemnity or liability terms You offer. You may include additional
221 | disclaimers of warranty and limitations of liability specific to any
222 | jurisdiction.
223 |
224 | 4. Inability to Comply Due to Statute or Regulation
225 |
226 | If it is impossible for You to comply with any of the terms of this License
227 | with respect to some or all of the Covered Software due to statute,
228 | judicial order, or regulation then You must: (a) comply with the terms of
229 | this License to the maximum extent possible; and (b) describe the
230 | limitations and the code they affect. Such description must be placed in a
231 | text file included with all distributions of the Covered Software under
232 | this License. Except to the extent prohibited by statute or regulation,
233 | such description must be sufficiently detailed for a recipient of ordinary
234 | skill to be able to understand it.
235 |
236 | 5. Termination
237 |
238 | 5.1. The rights granted under this License will terminate automatically if You
239 | fail to comply with any of its terms. However, if You become compliant,
240 | then the rights granted under this License from a particular Contributor
241 | are reinstated (a) provisionally, unless and until such Contributor
242 | explicitly and finally terminates Your grants, and (b) on an ongoing
243 | basis, if such Contributor fails to notify You of the non-compliance by
244 | some reasonable means prior to 60 days after You have come back into
245 | compliance. Moreover, Your grants from a particular Contributor are
246 | reinstated on an ongoing basis if such Contributor notifies You of the
247 | non-compliance by some reasonable means, this is the first time You have
248 | received notice of non-compliance with this License from such
249 | Contributor, and You become compliant prior to 30 days after Your receipt
250 | of the notice.
251 |
252 | 5.2. If You initiate litigation against any entity by asserting a patent
253 | infringement claim (excluding declaratory judgment actions,
254 | counter-claims, and cross-claims) alleging that a Contributor Version
255 | directly or indirectly infringes any patent, then the rights granted to
256 | You by any and all Contributors for the Covered Software under Section
257 | 2.1 of this License shall terminate.
258 |
259 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
260 | license agreements (excluding distributors and resellers) which have been
261 | validly granted by You or Your distributors under this License prior to
262 | termination shall survive termination.
263 |
264 | 6. Disclaimer of Warranty
265 |
266 | Covered Software is provided under this License on an "as is" basis,
267 | without warranty of any kind, either expressed, implied, or statutory,
268 | including, without limitation, warranties that the Covered Software is free
269 | of defects, merchantable, fit for a particular purpose or non-infringing.
270 | The entire risk as to the quality and performance of the Covered Software
271 | is with You. Should any Covered Software prove defective in any respect,
272 | You (not any Contributor) assume the cost of any necessary servicing,
273 | repair, or correction. This disclaimer of warranty constitutes an essential
274 | part of this License. No use of any Covered Software is authorized under
275 | this License except under this disclaimer.
276 |
277 | 7. Limitation of Liability
278 |
279 | Under no circumstances and under no legal theory, whether tort (including
280 | negligence), contract, or otherwise, shall any Contributor, or anyone who
281 | distributes Covered Software as permitted above, be liable to You for any
282 | direct, indirect, special, incidental, or consequential damages of any
283 | character including, without limitation, damages for lost profits, loss of
284 | goodwill, work stoppage, computer failure or malfunction, or any and all
285 | other commercial damages or losses, even if such party shall have been
286 | informed of the possibility of such damages. This limitation of liability
287 | shall not apply to liability for death or personal injury resulting from
288 | such party's negligence to the extent applicable law prohibits such
289 | limitation. Some jurisdictions do not allow the exclusion or limitation of
290 | incidental or consequential damages, so this exclusion and limitation may
291 | not apply to You.
292 |
293 | 8. Litigation
294 |
295 | Any litigation relating to this License may be brought only in the courts
296 | of a jurisdiction where the defendant maintains its principal place of
297 | business and such litigation shall be governed by laws of that
298 | jurisdiction, without reference to its conflict-of-law provisions. Nothing
299 | in this Section shall prevent a party's ability to bring cross-claims or
300 | counter-claims.
301 |
302 | 9. Miscellaneous
303 |
304 | This License represents the complete agreement concerning the subject
305 | matter hereof. If any provision of this License is held to be
306 | unenforceable, such provision shall be reformed only to the extent
307 | necessary to make it enforceable. Any law or regulation which provides that
308 | the language of a contract shall be construed against the drafter shall not
309 | be used to construe this License against a Contributor.
310 |
311 |
312 | 10. Versions of the License
313 |
314 | 10.1. New Versions
315 |
316 | Mozilla Foundation is the license steward. Except as provided in Section
317 | 10.3, no one other than the license steward has the right to modify or
318 | publish new versions of this License. Each version will be given a
319 | distinguishing version number.
320 |
321 | 10.2. Effect of New Versions
322 |
323 | You may distribute the Covered Software under the terms of the version
324 | of the License under which You originally received the Covered Software,
325 | or under the terms of any subsequent version published by the license
326 | steward.
327 |
328 | 10.3. Modified Versions
329 |
330 | If you create software not governed by this License, and you want to
331 | create a new license for such software, you may create and use a
332 | modified version of this License if you rename the license and remove
333 | any references to the name of the license steward (except to note that
334 | such modified license differs from this License).
335 |
336 | 10.4. Distributing Source Code Form that is Incompatible With Secondary
337 | Licenses If You choose to distribute Source Code Form that is
338 | Incompatible With Secondary Licenses under the terms of this version of
339 | the License, the notice described in Exhibit B of this License must be
340 | attached.
341 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Synthizer
2 | A simple experimental language for real time additive audio synthesis, intended for the creation of unique mathematical sounds.
3 |
4 | ## What works so far
5 | - Lexing
6 | - Parsing
7 | - Typechecking
8 | - Codegen (LLVM backend)
9 | - Audio output (WAV)
10 | - Real time audio output
11 |
12 | ## Todo
13 | - Documentation
14 | - GPU backend
15 | - GUI
16 | - MIDI (maybe) or at least some interface for a DAW
17 | - Graphical representations of the sound
18 |
19 | ## Example code
20 | Check the examples directory for example synths.
21 | You can listen to them in real time with:
22 | ```
23 | cargo run -- stream examples/???.synt
24 | ```
25 |
--------------------------------------------------------------------------------
/examples/saw.synt:
--------------------------------------------------------------------------------
1 | pi = 3.141592653589;
2 |
3 | // additively synthesize a sawtooth wave
4 | // n is number of harmonics
5 | saw freq, amp, time, n=50 {
6 | sin(freq*n*time*pi*2)*amp/n/pi;
7 | saw(freq, amp, time, n-1) if n > 1 else 0;
8 | }
9 |
10 | // use simple math to make a sawtooth wave
11 | fastsaw freq, amp, time {
12 | freq*time%1*amp;
13 | }
14 |
15 | main time {
16 | saw [
17 | freq=256,
18 | amp=0.8,
19 | time
20 | ];
21 | }
22 |
--------------------------------------------------------------------------------
/src/cli/main.rs:
--------------------------------------------------------------------------------
1 | #![feature(plugin, optin_builtin_traits)]
2 | #![plugin(regex_macros, docopt_macros)]
3 |
4 | extern crate docopt;
5 | extern crate rustc_serialize;
6 | extern crate vec_map;
7 | #[macro_use]
8 | extern crate interpreter;
9 |
10 | docopt!(Args, "
11 | Usage:
12 | synthizer stream
13 | synthizer write [--length=]
14 | synthizer --help
15 |
16 | Options:
17 | -h, --help Show this message.
18 | -l, --length= Length of audio to render, in seconds [default: 32].
19 | ", flag_length: f32);
20 |
21 | use interpreter::common::{Context, read_file};
22 | use interpreter::compiler::Compiler;
23 | use interpreter::audio::{write_wav, play_stream};
24 |
25 | #[allow(dead_code)]
26 | fn main() {
27 | let args: Args = Args::docopt().decode().unwrap_or_else(|e| e.exit());
28 | let filename = args.arg_input;
29 | let source = read_file(&filename).unwrap();
30 | let ctxt = Context::new(filename, source);
31 | let mut compiler = Compiler::new(&ctxt);
32 | compiler.define_entrypoint("main", make_fn_ty!(&ctxt, fn(time: Number) -> Number));
33 | match compiler.compile() {
34 | Ok(issues) => {
35 | println!("{}", issues);
36 | if args.cmd_write {
37 | write_wav(&compiler, args.arg_output, args.flag_length);
38 | } else if args.cmd_stream {
39 | play_stream(&compiler);
40 | }
41 | },
42 | Err(issues) => println!("Compile Error!\n{}", issues),
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/interpreter/ast.rs:
--------------------------------------------------------------------------------
1 | use super::tokens::{Number, Operator, SourcePos, Node, NodeImpl};
2 | use super::ident::Identifier;
3 |
4 | use std::ops::Deref;
5 |
6 | #[derive(Clone, Debug)]
7 | pub enum Item {
8 | Assignment(Node),
9 | FunctionDef(Node),
10 | }
11 |
12 | impl Item {
13 | pub fn pos(&self) -> SourcePos {
14 | match self {
15 | &Item::Assignment(ref x) => x.pos(),
16 | &Item::FunctionDef(ref x) => x.pos(),
17 | }
18 | }
19 | }
20 |
21 |
22 | #[derive(Clone, Debug)]
23 | pub enum Statement {
24 | Assignment(Node),
25 | Expression(Expression),
26 | }
27 |
28 | impl Statement {
29 | pub fn pos(&self) -> SourcePos {
30 | match self {
31 | &Statement::Assignment(ref x) => x.pos(),
32 | &Statement::Expression(ref x) => x.pos(),
33 | }
34 | }
35 | }
36 |
37 | pub type Root = Vec- ;
38 |
39 | pub type Block = Vec
;
40 |
41 | #[derive(Clone, Debug)]
42 | pub struct FunctionDef {
43 | pub ident: Node,
44 | pub func: Node,
45 | }
46 |
47 | impl FunctionDef {
48 | pub fn ident(&self) -> Identifier { *self.ident.item() }
49 | pub fn ident_pos(&self) -> SourcePos { self.ident.pos() }
50 | }
51 |
52 | impl Deref for FunctionDef {
53 | type Target = Function;
54 |
55 | fn deref<'a>(&'a self) -> &'a Function {
56 | &self.func
57 | }
58 | }
59 |
60 | #[derive(Clone, Debug)]
61 | pub struct Function {
62 | pub args: Node,
63 | pub block: Node,
64 | }
65 |
66 | impl Function {
67 | pub fn args(&self) -> &ArgumentList { &self.args }
68 | pub fn args_pos(&self) -> SourcePos { self.args.pos() }
69 | pub fn block(&self) -> &Block { &self.block }
70 | pub fn block_pos(&self) -> SourcePos { self.block.pos() }
71 | }
72 |
73 | #[derive(Clone, Debug)]
74 | pub struct FunctionCall {
75 | pub callee: Expression,
76 | pub args: Node,
77 | pub ty: CallType,
78 | }
79 |
80 | impl FunctionCall {
81 | pub fn callee(&self) -> &Expression { &self.callee }
82 | pub fn callee_pos(&self) -> SourcePos { self.callee.pos() }
83 | pub fn args(&self) -> &ArgumentList { &self.args }
84 | pub fn args_pos(&self) -> SourcePos { self.args.pos() }
85 | pub fn ty(&self) -> CallType { self.ty.clone() }
86 | }
87 |
88 | #[derive(Copy, Clone, Debug, PartialEq)]
89 | pub enum CallType {
90 | Named,
91 | Ordered,
92 | }
93 |
94 | // At most one of the two can be None
95 | #[derive(Clone, Debug)]
96 | pub enum Argument {
97 | Ident(Node),
98 | Assign(Node, Expression),
99 | OpAssign(Node, Node, Expression),
100 | Expr(Expression),
101 | }
102 |
103 | impl Argument {
104 | pub fn ident(&self) -> Option {
105 | match *self {
106 | Argument::Ident(Node(id, _)) |
107 | Argument::Assign(Node(id, _), _) |
108 | Argument::OpAssign(Node(id, _), _, _) => Some(id),
109 | Argument::Expr(_) => None,
110 | }
111 | }
112 | pub fn pos(&self) -> SourcePos {
113 | match *self {
114 | Argument::Ident(Node(_, pos)) |
115 | Argument::Assign(Node(_, pos), _) |
116 | Argument::OpAssign(Node(_, pos), _, _) => pos,
117 | Argument::Expr(ref e) => e.pos()
118 | }
119 | }
120 | pub fn expr(&self) -> Option<&Expression> {
121 | match *self {
122 | Argument::Ident(_) => None,
123 | Argument::Assign(_, ref expr) |
124 | Argument::Expr(ref expr) |
125 | Argument::OpAssign(_, _, ref expr) => Some(expr),
126 | }
127 | }
128 | }
129 |
130 | pub type ArgumentList = Vec;
131 |
132 | #[derive(Clone, Debug)]
133 | pub struct Assignment {
134 | pub ident: Node,
135 | pub expr: Expression,
136 | }
137 |
138 | impl Assignment {
139 | pub fn ident(&self) -> Identifier { *self.ident.item() }
140 | pub fn ident_pos(&self) -> SourcePos { self.ident.pos() }
141 | pub fn expr(&self) -> &Expression { &self.expr }
142 | pub fn expr_pos(&self) -> SourcePos { self.expr.pos() }
143 | }
144 |
145 | #[derive(Clone, Debug)]
146 | pub struct Conditional {
147 | pub cond: Expression,
148 | pub then: Expression,
149 | pub els: Expression,
150 | }
151 |
152 | impl Conditional {
153 | pub fn cond(&self) -> &Expression { &self.cond }
154 | pub fn cond_pos(&self) -> SourcePos { self.cond.pos() }
155 | pub fn then(&self) -> &Expression { &self.then }
156 | pub fn then_pos(&self) -> SourcePos { self.then.pos() }
157 | pub fn els(&self) -> &Expression { &self.els }
158 | pub fn els_pos(&self) -> SourcePos { self.els.pos() }
159 | }
160 |
161 | #[derive(Clone, Debug)]
162 | pub struct Infix {
163 | pub op: Node,
164 | pub left: Expression,
165 | pub right: Expression,
166 | }
167 |
168 | impl Infix {
169 | pub fn op(&self) -> Operator { *self.op.item() }
170 | pub fn op_pos(&self) -> SourcePos { self.op.pos() }
171 | pub fn left(&self) -> &Expression { &self.left }
172 | pub fn left_pos(&self) -> SourcePos { self.left.pos() }
173 | pub fn right(&self) -> &Expression { &self.right }
174 | pub fn right_pos(&self) -> SourcePos { self.right.pos() }
175 | }
176 |
177 | #[derive(Clone, Debug)]
178 | pub struct Prefix {
179 | pub op: Node,
180 | pub expr: Expression,
181 | }
182 |
183 | impl Prefix {
184 | pub fn op(&self) -> Operator { *self.op.item() }
185 | pub fn op_pos(&self) -> SourcePos { self.op.pos() }
186 | pub fn expr(&self) -> &Expression { &self.expr }
187 | pub fn expr_pos(&self) -> SourcePos { self.expr.pos() }
188 | }
189 |
190 | #[derive(Clone, Debug)]
191 | pub enum Expression {
192 | Constant(Node),
193 | Boolean(Node),
194 | Infix(Box>),
195 | Prefix(Box>),
196 | Variable(Node),
197 | Block(Node),
198 | FunctionCall(Box>),
199 | Conditional(Box>),
200 | Closure(Box>),
201 | }
202 |
203 | impl Expression {
204 | pub fn pos(&self) -> SourcePos {
205 | use self::Expression::*;
206 | match *self {
207 | Constant(ref x) => x.pos(),
208 | Boolean(ref x) => x.pos(),
209 | Infix(ref x) => x.pos(),
210 | Prefix(ref x) => x.pos(),
211 | Variable(ref x) => x.pos(),
212 | Block(ref x) => x.pos(),
213 | FunctionCall(ref x) => x.pos(),
214 | Conditional(ref x) => x.pos(),
215 | Closure(ref x) => x.pos(),
216 | }
217 | }
218 | }
219 |
--------------------------------------------------------------------------------
/src/interpreter/audio/filewriter.rs:
--------------------------------------------------------------------------------
1 | use super::super::compiler::Compiler;
2 | use super::render_samples;
3 |
4 | use hound;
5 |
6 | pub fn write_wav(compiler: &Compiler, filename: String, length: f32) {
7 | let spec = hound::WavSpec {
8 | channels: 1,
9 | sample_rate: 44100,
10 | bits_per_sample: 16
11 | };
12 | let rx = render_samples(compiler, spec.sample_rate).unwrap();
13 |
14 | let mut writer = hound::WavWriter::create(filename, spec).unwrap();
15 | let mut buffer = rx.recv().unwrap();
16 | let mut buf_ptr = 0;
17 | for _ in 0..(length*spec.sample_rate as f32) as usize {
18 | let sample = buffer[buf_ptr].max(-1.0).min(1.0);
19 | let amplitude = ::std::i16::MAX as f32;
20 | writer.write_sample((sample * amplitude) as i16).unwrap();
21 | buf_ptr += 1;
22 | if buf_ptr >= buffer.len() {
23 | buffer = rx.recv().unwrap();
24 | buf_ptr = 0;
25 | }
26 | }
27 | writer.finalize().unwrap();
28 | }
29 |
--------------------------------------------------------------------------------
/src/interpreter/audio/mod.rs:
--------------------------------------------------------------------------------
1 | use super::tokens::Number;
2 | use super::compiler::Compiler;
3 |
4 | use std::thread;
5 | use std::sync::mpsc::{sync_channel, Receiver};
6 | use std::slice;
7 |
8 | mod stream;
9 | mod filewriter;
10 |
11 | //TODO prefered buffer size, num threads, etc..
12 | fn render_samples(compiler: &Compiler, sample_rate: u32) -> Option>> {
13 | compiler.get_init_fn()(());
14 | let main_fn: extern fn(Number) -> Number = unsafe { match compiler.get_fn("main") {
15 | Some(f) => f,
16 | None => return None,
17 | }};
18 |
19 | const POOL_SIZE: usize = 8;
20 | const CHUNK_SIZE: usize = 256;
21 | const BUF_SIZE: usize = CHUNK_SIZE*POOL_SIZE;
22 | let (tx, rx) = sync_channel(8);
23 |
24 | thread::spawn(move || {
25 | for buf_id in 0.. {
26 | let mut buffer = vec![0f32; BUF_SIZE];
27 | {
28 | // this is necessary because the compiler can't reason that the threads are done with
29 | // the buffer after they are joined below.
30 | let buffer = unsafe { slice::from_raw_parts_mut(buffer.as_mut_ptr(), buffer.len()) };
31 | let mut threads = Vec::new();
32 | for (chunk_id, chunk) in buffer.chunks_mut(CHUNK_SIZE).enumerate() {
33 | threads.push(thread::spawn(move || {
34 | for i in 0..CHUNK_SIZE {
35 | let time = (buf_id*BUF_SIZE + chunk_id*CHUNK_SIZE + i) as Number / sample_rate as Number;
36 | chunk[i] = main_fn(time) as f32;
37 | if !chunk[i].is_finite() && i > 0 {
38 | chunk[i] = chunk[i-1];
39 | }
40 | }
41 | }));
42 | }
43 | for thread in threads {
44 | thread.join().unwrap();
45 | }
46 | }
47 | match tx.send(buffer) {
48 | Ok(_) => { },
49 | Err(_) => return,
50 | }
51 | }
52 | });
53 |
54 | Some(rx)
55 | }
56 |
57 | pub use self::stream::play_stream;
58 | pub use self::filewriter::write_wav;
59 |
--------------------------------------------------------------------------------
/src/interpreter/audio/stream.rs:
--------------------------------------------------------------------------------
1 | use super::super::compiler::Compiler;
2 | use super::render_samples;
3 |
4 | use sound_stream::{CallbackFlags, CallbackResult, SoundStream, Settings, StreamParams};
5 |
6 | pub fn play_stream(compiler: &Compiler) {
7 | let rx = render_samples(compiler, 48000).unwrap();
8 | let mut buf_ptr = 0usize;
9 | let mut buffer = rx.recv().unwrap();
10 | let callback = Box::new(move |output: &mut[f32], settings: Settings, _: f64, _: CallbackFlags| {
11 | let mut max = 0f32;
12 | for frame in output.chunks_mut(settings.channels as usize) {
13 | let amp = buffer[buf_ptr];
14 | if amp > max {
15 | max = amp;
16 | }
17 | for channel in frame {
18 | *channel = amp;
19 | }
20 | buf_ptr += 1;
21 | if buf_ptr >= buffer.len() {
22 | buf_ptr = 0;
23 | buffer = rx.recv().unwrap();
24 | }
25 | }
26 | //if max < 0.0001 { CallbackResult::Complete } else { CallbackResult::Continue }
27 | CallbackResult::Continue
28 | });
29 |
30 | // Construct the default, non-blocking output stream and run our callback.
31 | let params = StreamParams::new().suggest_latency(0.05);
32 | let stream = SoundStream::new().output(params).run_callback(callback).unwrap();
33 |
34 | while let Ok(true) = stream.is_active() {}
35 | }
36 |
--------------------------------------------------------------------------------
/src/interpreter/codegen.rs:
--------------------------------------------------------------------------------
1 | use super::common::Context;
2 | use super::ast::*;
3 | use super::tokens::{Number, Boolean, NodeImpl, Node, Operator, SourcePos};
4 | use super::types::{Type, TypeTable};
5 | use super::scope::ScopedTable;
6 | use super::ident::Identifier;
7 | use super::functions::{self, FunctionTable, ExternalFunction, PointerFunction};
8 |
9 | use llvm;
10 | use llvm::{Compile, ExecutionEngine, CastFrom};
11 | use cbox::*;
12 | use std::cell::{RefCell, Ref};
13 | use vec_map::VecMap;
14 | use std::ops::Deref;
15 | use std::mem;
16 | use std::rc::Rc;
17 | use llvm_sys::core;
18 |
19 | #[derive(Clone, Debug)]
20 | struct FnArgument {
21 | default_idx: Option,
22 | sig: Option>>,
23 | ordered_id: usize,
24 | }
25 |
26 | #[derive(Clone, Debug)]
27 | struct FnSignature {
28 | ret: Option>>,
29 | args: VecMap,
30 | }
31 |
32 | #[derive(Clone)]
33 | pub struct ValueWrapper<'a> {
34 | value: &'a llvm::Value,
35 | sig: Option>>,
36 | }
37 |
38 | impl<'a> ValueWrapper<'a> {
39 | fn new(value: &'a llvm::Value, sig: Option>>) -> ValueWrapper<'a> {
40 | ValueWrapper {
41 | value: value,
42 | sig: sig
43 | }
44 | }
45 |
46 | fn map(&self, value: &'a llvm::Value) -> ValueWrapper<'a> {
47 | ValueWrapper {
48 | value: value,
49 | sig: self.sig.clone()
50 | }
51 | }
52 | }
53 |
54 | impl<'a> Into> for &'a llvm::Value {
55 | fn into(self) -> ValueWrapper<'a> {
56 | ValueWrapper::new(self, None)
57 | }
58 | }
59 |
60 | impl<'a> Deref for ValueWrapper<'a> {
61 | type Target = &'a llvm::Value;
62 | fn deref<'b>(&'b self) -> &'b &'a llvm::Value {
63 | &self.value
64 | }
65 | }
66 |
67 | pub const GLOBAL_INIT_FN_NAME: &'static str = "*globalinit*";
68 |
69 | pub fn codegen<'a>(ctxt: &'a Context<'a>) {
70 | //XXX
71 | let cg = CodeGenerator::new(ctxt);
72 | let foo: &'a CodeGenerator<'a> = unsafe { mem::transmute(&cg) };
73 | foo.codegen();
74 | }
75 |
76 | pub struct CodeGenerator<'a> {
77 | ctxt: &'a Context<'a>,
78 | types: Ref<'a, TypeTable>,
79 | functions: Ref<'a, FunctionTable>,
80 | llvm: &'a llvm::Context,
81 | pub module: CSemiBox<'a, llvm::Module>,
82 | builder: CSemiBox<'a, llvm::Builder>,
83 | values: RefCell>>,
84 | }
85 |
86 | impl<'a> CodeGenerator<'a> {
87 | pub fn new(ctxt: &'a Context<'a>) -> CodeGenerator<'a> {
88 | CodeGenerator {
89 | ctxt: ctxt,
90 | types: ctxt.types.borrow(),
91 | functions: ctxt.functions.borrow(),
92 | llvm: &ctxt.llvm,
93 | module: llvm::Module::new(&ctxt.filename, &ctxt.llvm),
94 | builder: llvm::Builder::new(&ctxt.llvm),
95 | values: RefCell::new(ScopedTable::new()),
96 | }
97 | }
98 |
99 | pub fn codegen(&'a self) {
100 | self.codegen_root(&self.ctxt.ast.borrow());
101 |
102 | println!("{:?}", self.module);
103 | self.module.verify().unwrap();
104 | }
105 |
106 | fn codegen_root(&'a self, root: &Root) {
107 | let unit_ty = llvm::Type::get::<()>(self.llvm);
108 | let init_fn = self.module.add_function(GLOBAL_INIT_FN_NAME,
109 | llvm::Type::new_function(unit_ty, &[]));
110 | let block = init_fn.append("entry");
111 | self.builder.position_at_end(block);
112 |
113 | for (ident, func) in &self.functions.map {
114 | match *func {
115 | functions::Function::External(ref def) => {
116 | self.codegen_external_function(ident, def);
117 | },
118 | functions::Function::Pointer(ref def) => {
119 | self.codegen_pointer_function(ident, def, init_fn);
120 | },
121 | _ => { },
122 | }
123 | }
124 |
125 | let mut decls = Vec::new();
126 | for item in root.iter().rev() {
127 | match *item {
128 | Item::FunctionDef(ref func) => {
129 | decls.push(self.declare_global_function(func, init_fn));
130 | }
131 | _ => { },
132 | }
133 | }
134 |
135 | for item in root {
136 | match *item {
137 | Item::Assignment(ref x) => {
138 | self.codegen_global_assignment(x, init_fn);
139 | },
140 | Item::FunctionDef(ref x) => {
141 | let (func_args, llvm_func, sig, g_struct) = decls.pop().unwrap();
142 | self.codegen_global_function(x, func_args, llvm_func, sig, g_struct);
143 | }
144 | }
145 | }
146 |
147 | self.builder.build_ret_void();
148 | }
149 |
150 | fn codegen_external_function(&'a self, ident: Identifier, func: &ExternalFunction) -> ValueWrapper<'a> {
151 | let ty = Type::Function(ident);
152 | let func = self.module.add_function(func.symbol, self.type_to_llvm(ty, false));
153 | let val = ValueWrapper::new(func, self.type_to_signature(ty));
154 | self.store_val(Node(ident, SourcePos::anon()), val.clone());
155 | val
156 | }
157 |
158 | fn codegen_pointer_function(&'a self, ident: Identifier, func: &PointerFunction,
159 | owning_fn: &llvm::Function) -> ValueWrapper<'a> {
160 | let ty = Type::Function(ident);
161 | let llvm_ty = self.type_to_llvm(ty, false);
162 | let sig = self.type_to_signature(ty).unwrap();
163 |
164 | let mut func_args = func.args.clone();
165 | func_args.sort_by(|a, b| a.ident().cmp(&b.ident()));
166 |
167 | let ptr = unsafe {
168 | core::LLVMConstIntToPtr((func.ptr as usize).compile(self.llvm).into(),
169 | llvm::Type::new_pointer(llvm_ty).into()).into()
170 | };
171 |
172 | let mut fn_struct_values: Vec<&llvm::Value> = Vec::new();
173 | fn_struct_values.push(ptr);
174 |
175 | // catalog default args and set their signatures
176 | for arg in func_args {
177 | if let Argument::Assign(_, ref expr) = arg {
178 | let default_val = self.codegen_expr(expr, owning_fn);
179 | fn_struct_values.push(default_val.value);
180 | }
181 | }
182 | let func_struct = llvm::Value::new_struct(self.llvm, &fn_struct_values, true);
183 | let struct_ty = func_struct.get_type();
184 | let name = self.ctxt.lookup_name(ident);
185 | let g_struct = self.module.add_global(&name, struct_ty);
186 | g_struct.set_initializer(func_struct);
187 | let val = ValueWrapper::new(g_struct, Some(sig.clone()));
188 | self.store_val(Node(ident, SourcePos::anon()), val.clone());
189 | val
190 | }
191 |
192 | fn declare_global_function(&'a self, func: &FunctionDef, owning_fn: &llvm::Function)
193 | -> (Vec, &'a llvm::Function, Rc>, &'a llvm::Value) {
194 | let ident = func.ident();
195 | let name = self.ctxt.lookup_name(ident);
196 |
197 | let (llvm_func, func_struct, sig, func_args) = self.codegen_function_decl(func, owning_fn, &name);
198 |
199 | let struct_ty = func_struct.get_type();
200 | let g_struct = self.module.add_global(&name, struct_ty);
201 | g_struct.set_initializer(llvm::Value::new_undef(struct_ty));
202 | self.builder.build_store(func_struct, g_struct);
203 | self.store_val(func.ident, ValueWrapper::new(g_struct, Some(sig.clone())));
204 |
205 | (func_args, llvm_func, sig, g_struct)
206 | }
207 |
208 |
209 | fn codegen_global_function(&'a self, func: &FunctionDef, func_args: Vec,
210 | llvm_func: &'a llvm::Function, sig: Rc>,
211 | g_struct: &'a llvm::Value) -> ValueWrapper<'a> {
212 | let owning_block = self.builder.get_position();
213 | self.codegen_function_body(func, func_args, llvm_func, sig, owning_block, g_struct)
214 | }
215 |
216 | fn codegen_closure(&'a self, func: &FunctionDef, owning_fn: &llvm::Function) -> ValueWrapper<'a> {
217 | let owning_block = self.builder.get_position();
218 | let (llvm_func, func_struct, sig, func_args) = self.codegen_closure_decl(func, owning_fn);
219 | self.codegen_function_body(func, func_args, llvm_func, sig, owning_block, func_struct)
220 | }
221 |
222 | fn codegen_function_body(&'a self, func: &FunctionDef, func_args: Vec,
223 | llvm_func: &'a llvm::Function, sig: Rc>,
224 | owning_block: &llvm::BasicBlock, g_struct: &'a llvm::Value) -> ValueWrapper<'a> {
225 | // setup args
226 | self.values.borrow_mut().push(func.ident_pos().index);
227 | for i in 0..func.args.len() {
228 | let ref arg = func_args[i];
229 | let id = arg.ident().unwrap();
230 | llvm_func[i].set_name(&self.ctxt.lookup_name(id));
231 | self.store_val(Node(id, arg.pos()),
232 | ValueWrapper::new(&llvm_func[i], sig.borrow().args[id].sig.clone()));
233 | }
234 | // codegen block
235 | let entry = llvm_func.append("entry");
236 | self.builder.position_at_end(entry);
237 | let res = self.codegen_block(&func.block, llvm_func);
238 | self.builder.build_ret(res.value);
239 | self.values.borrow_mut().pop();
240 | self.builder.position_at_end(owning_block);
241 |
242 | ValueWrapper::new(g_struct, Some(sig))
243 | }
244 |
245 | fn codegen_function_decl(&'a self, func: &FunctionDef, owning_fn: &llvm::Function, name: &str)
246 | -> (&llvm::Function, &llvm::Value, Rc>, Vec) {
247 | let ident = func.ident();
248 | let synt_ty = self.types.get_symbol(ident).unwrap().val;
249 | let ty = self.type_to_llvm(synt_ty, false);
250 | let sig = self.type_to_signature(synt_ty).unwrap();
251 | let mut func_args = func.args().clone();
252 | func_args.sort_by(|a, b| a.ident().cmp(&b.ident()));
253 | let llvm_func = self.module.add_function(name, ty);
254 | llvm_func.add_attributes(&[llvm::Attribute::NoUnwind]);
255 |
256 | let mut fn_struct_values: Vec<&llvm::Value> = Vec::new();
257 | fn_struct_values.push(llvm_func);
258 |
259 | // catalog default args and set their signatures
260 | for arg in &func_args {
261 | if let Argument::Assign(_, ref expr) = *arg {
262 | let default_val = self.codegen_expr(expr, owning_fn);
263 | fn_struct_values.push(default_val.value);
264 | }
265 | }
266 | let func_struct = llvm::Value::new_struct(self.llvm, &fn_struct_values, true);
267 | (llvm_func, func_struct, sig, func_args)
268 | }
269 |
270 | fn codegen_closure_decl(&'a self, func: &FunctionDef, owning_fn: &llvm::Function)
271 | -> (&llvm::Function, &llvm::Value, Rc>, Vec) {
272 | let ident = func.ident();
273 | let synt_ty = self.types.get_symbol(ident).unwrap().val;
274 | let ty = self.type_to_llvm(synt_ty, false);
275 | let sig = self.type_to_signature(synt_ty).unwrap();
276 | let mut func_args = func.args().clone();
277 | func_args.sort_by(|a, b| a.ident().cmp(&b.ident()));
278 | let llvm_func = self.module.add_function("*closure*", ty);
279 | llvm_func.add_attributes(&[llvm::Attribute::NoUnwind]);
280 |
281 | let mut fn_struct_values: Vec<&llvm::Value> = Vec::new();
282 | fn_struct_values.push(llvm_func);
283 |
284 | // catalog default args and set their signatures
285 | for arg in &func_args {
286 | if let Argument::Assign(_, ref expr) = *arg {
287 | let default_val = self.codegen_expr(expr, owning_fn);
288 | fn_struct_values.push(default_val.value);
289 | }
290 | }
291 | let func_struct = llvm::Value::new_struct(self.llvm, &fn_struct_values, true);
292 | (llvm_func, func_struct, sig, func_args)
293 | }
294 |
295 | fn codegen_global_assignment(&'a self, assign: &Assignment, func: &llvm::Function) {
296 | let val = self.codegen_expr(assign.expr(), func);
297 | let name = &self.ctxt.lookup_name(assign.ident());
298 | let global = self.module.add_global(name, val.get_type());
299 | global.set_initializer(llvm::Value::new_undef(val.get_type()));
300 | self.builder.build_store(val.value, global);
301 | self.store_val(assign.ident, val);
302 | }
303 |
304 | fn store_val(&self, ident: Node, value: ValueWrapper<'a>) {
305 | self.values.borrow_mut().set_val(*ident, ident.pos().index, value);
306 | }
307 |
308 | fn codegen_assignment(&'a self, assign: &Assignment, func: &llvm::Function) {
309 | let name = &self.ctxt.lookup_name(assign.ident());
310 | let val = self.codegen_expr(assign.expr(), func);
311 | val.set_name(name);
312 |
313 | self.store_val(assign.ident, val);
314 | }
315 |
316 | fn codegen_expr(&'a self, expr: &Expression, func: &llvm::Function) -> ValueWrapper<'a> {
317 | match *expr {
318 | Expression::Constant(Node(v, _)) => v.compile(self.llvm).into(),
319 | Expression::Boolean(Node(v, _)) => v.compile(self.llvm).into(),
320 | Expression::Infix(ref v) => self.codegen_infix(v, func),
321 | Expression::Prefix(ref v) => self.codegen_prefix(v, func),
322 | Expression::Variable(ref v) => self.codegen_var(**v, func),
323 | Expression::Conditional(ref v) => self.codegen_conditional(v, func),
324 | Expression::Block(ref v) => self.codegen_block(v, func),
325 | Expression::Closure(ref v) => self.codegen_closure(v, func),
326 | Expression::FunctionCall(ref v) => self.codegen_function_call(v, func),
327 | }
328 | }
329 |
330 | fn codegen_struct_load(&self, val: &llvm::Value, index: usize) -> &llvm::Value {
331 | if val.get_type().is_pointer() {
332 | let ptr = self.builder.build_gep(val,
333 | &[0.compile(self.llvm),
334 | (index as i32).compile(self.llvm)]);
335 | self.builder.build_load(ptr)
336 | } else {
337 | self.builder.build_extract_value(val, index)
338 | }
339 | }
340 |
341 | fn codegen_function_call(&'a self, call: &FunctionCall, func: &llvm::Function) -> ValueWrapper<'a> {
342 | let callee_expr = self.codegen_expr(call.callee(), func);
343 | let callee = match llvm::Function::cast(callee_expr.value) {
344 | Some(callee) => callee,
345 | None => llvm::Function::cast(self.codegen_struct_load(callee_expr.value, 0)).unwrap(),
346 | };
347 | let sig = callee_expr.sig.as_ref().unwrap().borrow();
348 | let mut call_args = VecMap::new();
349 | match call.ty {
350 | CallType::Named => {
351 | for (id, ref sig_arg) in sig.args.iter() {
352 | let mut found = false;
353 | for arg in call.args() {
354 | match *arg {
355 | Argument::Assign(ident, ref expr) => {
356 | if id == *ident {
357 | call_args.insert(id, self.codegen_expr(expr, func).value);
358 | found = true;
359 | }
360 | },
361 | Argument::OpAssign(ident, Node(op, _), ref expr) => {
362 | if id == *ident {
363 | let lhs = self.codegen_var(*ident, func);
364 | let rhs = self.codegen_expr(expr, func);
365 | let arg_value = self.codegen_binary_op(op, lhs, rhs);
366 | call_args.insert(id, arg_value.value);
367 | found = true;
368 | }
369 | }
370 | Argument::Ident(ident) => {
371 | if id == *ident {
372 | call_args.insert(id, self.codegen_var(*ident, func).value);
373 | found = true;
374 | }
375 | }
376 | _ => unreachable!(),
377 | }
378 | }
379 | if !found {
380 | call_args.insert(id, self.codegen_struct_load(callee_expr.value,
381 | sig_arg.default_idx.unwrap()));
382 | }
383 | }
384 | },
385 | CallType::Ordered => {
386 | let mut sig_args = sig.args.iter();
387 | for arg in call.args() {
388 | match *arg {
389 | Argument::Expr(ref expr) => {
390 | call_args.insert(sig_args.next().unwrap().1.ordered_id,
391 | self.codegen_expr(expr, func).value);
392 | }
393 | _ => unreachable!(),
394 | }
395 | }
396 | for (id, ref sig_arg) in sig_args {
397 | call_args.insert(id, self.codegen_struct_load(callee_expr.value,
398 | sig_arg.default_idx.unwrap()));
399 | }
400 | }
401 | }
402 |
403 | let arg_vec: Vec<_> = call_args.values().map(|x| *x).collect();
404 | ValueWrapper::new(self.builder.build_call(callee, &arg_vec), sig.ret.clone())
405 | }
406 |
407 | fn codegen_block(&'a self, block: &Node, func: &llvm::Function) -> ValueWrapper<'a> {
408 | self.values.borrow_mut().push(block.pos().index);
409 | let mut value = None;
410 | for stmnt in block.item() {
411 | match *stmnt {
412 | Statement::Assignment(ref v) => {
413 | self.codegen_assignment(v, func);
414 | },
415 | Statement::Expression(ref v) => {
416 | let expr = self.codegen_expr(v, func);
417 | if expr.get_type().is_float() {
418 | value = match value {
419 | None => Some(expr),
420 | Some(v) => Some(v.map(self.builder.build_add(*v, *expr))),
421 | };
422 | } else {
423 | value = Some(expr);
424 | }
425 | },
426 | }
427 | }
428 | self.values.borrow_mut().pop();
429 | value.unwrap()
430 | }
431 |
432 | fn codegen_var(&'a self, ident: Identifier, _: &llvm::Function) -> ValueWrapper<'a> {
433 | let (value, sig) = {
434 | let values = self.values.borrow();
435 | let ref sym = values.get_symbol(ident).unwrap().val;
436 | (sym.value, sym.sig.clone())
437 | };
438 | ValueWrapper::new(
439 | if llvm::GlobalValue::cast(value).is_some() &&
440 | llvm::Function::cast(value).is_none() { // it will only be a function if it's an intrinsic,
441 | // which we can't load.
442 | self.builder.build_load(value)
443 | } else {
444 | value
445 | }, sig)
446 | }
447 |
448 | fn codegen_conditional(&'a self, cond: &Conditional, func: &llvm::Function) -> ValueWrapper<'a> {
449 | let cond_val = self.codegen_expr(cond.cond(), func);
450 | let then_block = func.append("then");
451 | let else_block = func.append("else");
452 | self.builder.build_cond_br(*cond_val, then_block, Some(else_block));
453 |
454 | let merge_block = func.append("merge");
455 |
456 | self.builder.position_at_end(then_block);
457 | let then_val = self.codegen_expr(cond.then(), func);
458 | self.builder.build_br(merge_block);
459 | let then_block = self.builder.get_position();
460 |
461 | self.builder.position_at_end(else_block);
462 | let else_val = self.codegen_expr(cond.els(), func);
463 | self.builder.build_br(merge_block);
464 | let else_block = self.builder.get_position();
465 |
466 | self.builder.position_at_end(merge_block);
467 | assert_eq!(then_val.get_type(), else_val.get_type());
468 | let phi = self.builder.build_phi(then_val.get_type(), "iftmp");
469 | phi.add_incoming(*then_val, then_block);
470 | phi.add_incoming(*else_val, else_block);
471 | then_val.map(phi)
472 | }
473 |
474 | fn codegen_infix(&'a self, infix: &Infix, func: &llvm::Function) -> ValueWrapper<'a> {
475 | let lhs = self.codegen_expr(infix.left(), func);
476 | let rhs = self.codegen_expr(infix.right(), func);
477 | self.codegen_binary_op(infix.op(), lhs, rhs)
478 | }
479 |
480 | fn codegen_binary_op(&self, op: Operator, lhs: ValueWrapper, rhs: ValueWrapper) -> ValueWrapper {
481 | let lhs = *lhs;
482 | let rhs = *rhs;
483 | match op {
484 | Operator::Add => self.builder.build_add(lhs, rhs),
485 | Operator::Sub => self.builder.build_sub(lhs, rhs),
486 | Operator::Mul => self.builder.build_mul(lhs, rhs),
487 | Operator::Div => self.builder.build_div(lhs, rhs),
488 | Operator::Less => self.builder.build_cmp(lhs, rhs, llvm::Predicate::LessThan),
489 | Operator::Greater => self.builder.build_cmp(lhs, rhs, llvm::Predicate::GreaterThan),
490 | Operator::LessEqual => self.builder.build_cmp(lhs, rhs, llvm::Predicate::LessThanOrEqual),
491 | Operator::GreaterEqual => self.builder.build_cmp(lhs, rhs, llvm::Predicate::GreaterThanOrEqual),
492 | Operator::Equal => self.builder.build_cmp(lhs, rhs, llvm::Predicate::Equal),
493 | Operator::NotEqual => self.builder.build_cmp(lhs, rhs, llvm::Predicate::NotEqual),
494 | Operator::Or => self.builder.build_or(lhs, rhs),
495 | Operator::And => self.builder.build_and(lhs, rhs),
496 | Operator::Xor => self.builder.build_xor(lhs, rhs),
497 | Operator::Mod => self.builder.build_rem(lhs, rhs),
498 | Operator::Exp => {
499 | let pow_fn = self.module.get_function("llvm.pow.f64").unwrap();
500 | self.builder.build_call(pow_fn, &[lhs, rhs])
501 | }
502 | _ => unreachable!(),
503 | }.into()
504 | }
505 |
506 | fn codegen_prefix(&'a self, prefix: &Prefix, func: &llvm::Function) -> ValueWrapper<'a> {
507 | let expr = self.codegen_expr(prefix.expr(), func);
508 | expr.map(match prefix.op() {
509 | Operator::Sub => self.builder.build_sub(0f64.compile(self.llvm), *expr),
510 | Operator::Not => self.builder.build_not(*expr),
511 | _ => unreachable!(),
512 | })
513 | }
514 |
515 | fn maybe_make_fn_ptr<'c>(&'c self, ty: &'c llvm::Type) -> &llvm::Type {
516 | if ty.is_function() {
517 | llvm::Type::new_pointer(ty)
518 | } else {
519 | ty
520 | }
521 | }
522 |
523 | fn type_to_signature(&self, ty: Type) -> Option>> {
524 | match ty {
525 | Type::Number => None,
526 | Type::Boolean => None,
527 | Type::Function(id) => {
528 | let func = self.functions.get(id).unwrap();
529 | let ty = func.ty().unwrap();
530 | let mut default_count = 0;
531 | let args = ty.args.iter().zip(func.args().iter()).map(|((id, &ty), arg)| {
532 | (id, FnArgument {
533 | default_idx:
534 | if arg.expr().is_some() {
535 | default_count += 1;
536 | Some(default_count)
537 | } else {
538 | None
539 | },
540 | sig: self.type_to_signature(ty),
541 | ordered_id: arg.ident().unwrap()
542 | })
543 | }).collect();
544 | let ret = self.type_to_signature(ty.returns);
545 | Some(Rc::new(RefCell::new(FnSignature {
546 | ret: ret,
547 | args: args,
548 | })))
549 | }
550 | _ => unreachable!(),
551 | }
552 | }
553 |
554 | fn type_to_llvm(&self, ty: Type, make_fn_struct: bool) -> &llvm::Type {
555 | match ty {
556 | Type::Number => llvm::Type::get::(self.llvm),
557 | Type::Boolean => llvm::Type::get::(self.llvm),
558 | Type::Function(id) => {
559 | let func = self.functions.get(id).unwrap();
560 | let ty = func.ty().unwrap();
561 | let arg_map: VecMap<&llvm::Type> = ty.args.iter().map(|(id, &ty)|
562 | (id, self.maybe_make_fn_ptr(self.type_to_llvm(ty, true)))).collect();
563 | let args: Vec<&llvm::Type> = arg_map.values().map(|x| *x).collect();
564 | let ret = self.maybe_make_fn_ptr(self.type_to_llvm(ty.returns, true));
565 | let func_ty = llvm::Type::new_function(ret, &args);
566 | if make_fn_struct {
567 | let mut struct_types: Vec<&llvm::Type> = vec![llvm::Type::new_pointer(func_ty)];
568 | for (ty, arg) in args.iter().zip(func.args().iter()) {
569 | match *arg {
570 | Argument::Assign(..) => struct_types.push(ty),
571 | _ => { },
572 | }
573 | }
574 | let struct_ty = llvm::StructType::new(self.llvm, &struct_types, true);
575 | struct_ty
576 | } else {
577 | func_ty
578 | }
579 | }
580 | _ => unreachable!(),
581 | }
582 | }
583 | }
584 |
--------------------------------------------------------------------------------
/src/interpreter/common.rs:
--------------------------------------------------------------------------------
1 | use super::issue::{IssueTracker, Level};
2 | use super::tokens::{Token, SourcePos, Node};
3 | use super::ast::Root;
4 | use super::types::{TypeTable, FunctionType};
5 | use super::ident::{Identifier, NameTable};
6 | use super::functions::{FunctionTable, CallStack};
7 |
8 | use std::cell::RefCell;
9 | use std::borrow::Cow;
10 | use vec_map::VecMap;
11 |
12 | use llvm;
13 | use cbox::CBox;
14 |
15 | pub struct Context<'a> {
16 | pub filename: String,
17 | pub source: String,
18 | pub issues: RefCell>,
19 | pub types: RefCell,
20 | pub names: RefCell>,
21 | pub functions: RefCell,
22 | pub tokens: RefCell>>,
23 | pub ast: RefCell,
24 | pub callstack: RefCell,
25 | pub entrypoints: RefCell>,
26 | pub llvm: CBox,
27 | }
28 |
29 | impl<'a> Context<'a> {
30 | pub fn new(filename: String, source: String) -> Context<'a> {
31 | Context {
32 | filename: filename,
33 | source: source,
34 | issues: RefCell::new(IssueTracker::new()),
35 | types: RefCell::new(TypeTable::new()),
36 | names: RefCell::new(NameTable::new()),
37 | functions: RefCell::new(FunctionTable::new()),
38 | tokens: RefCell::new(Vec::new()),
39 | ast: RefCell::new(Vec::new()),
40 | callstack: RefCell::new(CallStack::new()),
41 | entrypoints: RefCell::new(VecMap::new()),
42 | llvm: llvm::Context::new(),
43 | }
44 | }
45 |
46 | pub fn emit_error(&'a self, msg: T, pos: SourcePos) where T: Into> {
47 | self.issues.borrow_mut().new_issue(self, pos, Level::Error, msg);
48 | }
49 | pub fn emit_warning(&'a self, msg: T, pos: SourcePos) where T: Into> {
50 | self.issues.borrow_mut().new_issue(self, pos, Level::Warning, msg);
51 | }
52 |
53 | pub fn lookup_name(&'a self, id: Identifier) -> String {
54 | self.names.borrow().get_name(id).unwrap().into()
55 | }
56 | }
57 |
58 | pub fn read_file(filename: &str) -> Result {
59 | use std::fs::File;
60 | use std::io::Read;
61 |
62 | let mut file = match File::open(filename) {
63 | Err(why) => {
64 | return Err(format!("couldn't open {}: {}", filename, why));
65 | }
66 | Ok(file) => file,
67 | };
68 | let mut code = String::new();
69 | match file.read_to_string(&mut code) {
70 | Err(why) => {
71 | return Err(format!("couldn't read {}: {}", filename, why));
72 | }
73 | Ok(_) => { }
74 | }
75 | return Ok(code);
76 | }
77 |
--------------------------------------------------------------------------------
/src/interpreter/compiler.rs:
--------------------------------------------------------------------------------
1 | use super::common::Context;
2 | use super::codegen::{CodeGenerator, GLOBAL_INIT_FN_NAME};
3 | use super::lexer::lex;
4 | use super::parser::parse;
5 | use super::typecheck::typecheck;
6 | use super::types::{Type, FunctionType};
7 | use super::functions::{ExternalFunction, PointerFunction, Function};
8 | use super::issue::IssueTracker;
9 | use super::ast;
10 | use super::tokens::{Number, SourcePos, Node};
11 |
12 | use llvm;
13 | use llvm::ExecutionEngine;
14 | use std::mem;
15 |
16 | pub struct Compiler<'a> {
17 | ctxt: &'a Context<'a>,
18 | codegen: Option>,
19 | engine: Option>,
20 | stage: Stage,
21 | }
22 |
23 | #[derive(Debug, PartialEq)]
24 | pub enum Stage {
25 | Lex,
26 | Parse,
27 | Typecheck,
28 | Codegen,
29 | Complete
30 | }
31 |
32 | impl<'a> Compiler<'a> {
33 | pub fn new(ctxt: &'a Context<'a>) -> Compiler<'a> {
34 | Compiler {
35 | ctxt: ctxt,
36 | codegen: None,
37 | engine: None,
38 | stage: Stage::Lex,
39 | }
40 | }
41 |
42 | pub fn compile(&mut self) -> Result, IssueTracker<'a>> {
43 | // front end
44 | self.define_intrinsics();
45 | if !self.lex() {
46 | return Err(self.ctxt.issues.borrow().clone());
47 | }
48 | if !self.parse() {
49 | return Err(self.ctxt.issues.borrow().clone());
50 | }
51 | if !self.typecheck() {
52 | return Err(self.ctxt.issues.borrow().clone());
53 | }
54 | if !self.codegen() {
55 | return Err(self.ctxt.issues.borrow().clone());
56 | }
57 | Ok(self.ctxt.issues.borrow().clone())
58 | }
59 |
60 | pub fn lex(&mut self) -> bool {
61 | assert_eq!(self.stage, Stage::Lex);
62 | self.stage = Stage::Parse;
63 | lex(self.ctxt);
64 | return if self.ctxt.issues.borrow().has_errors() {
65 | false
66 | } else {
67 | self.stage = Stage::Parse;
68 | true
69 | }
70 | }
71 | pub fn parse(&mut self) -> bool {
72 | assert_eq!(self.stage, Stage::Parse);
73 | parse(self.ctxt);
74 | return if self.ctxt.issues.borrow().has_errors() {
75 | false
76 | } else {
77 | self.stage = Stage::Typecheck;
78 | true
79 | }
80 | }
81 | pub fn typecheck(&mut self) -> bool {
82 | assert_eq!(self.stage, Stage::Typecheck);
83 | typecheck(self.ctxt);
84 | return if self.ctxt.issues.borrow().has_errors() {
85 | false
86 | } else {
87 | self.stage = Stage::Codegen;
88 | true
89 | }
90 | }
91 | pub fn codegen(&mut self) -> bool {
92 | assert_eq!(self.stage, Stage::Codegen);
93 | let cg = CodeGenerator::new(self.ctxt);
94 | let cg_ptr: &'a CodeGenerator<'a> = unsafe { mem::transmute(&cg) };
95 | cg_ptr.codegen();
96 | self.engine = Some(llvm::JitEngine::new(&cg_ptr.module, llvm::JitOptions { opt_level: 3 }).unwrap());
97 | self.codegen = Some(cg);
98 | return if self.ctxt.issues.borrow().has_errors() {
99 | false
100 | } else {
101 | self.stage = Stage::Complete;
102 | true
103 | }
104 | }
105 |
106 | pub unsafe fn define_pointer_function(&self, name: &'static str, ty: FunctionType, ptr: *mut ()) {
107 | assert_eq!(self.stage, Stage::Lex);
108 | let id = self.ctxt.names.borrow_mut().new_id(name);
109 | let func = Function::Pointer(PointerFunction::new(ty, mem::transmute(ptr)));
110 | let ty = Type::Function(id);
111 | self.ctxt.functions.borrow_mut().insert(id, func);
112 | self.ctxt.types.borrow_mut().set_val(id, 0, ty);
113 | }
114 |
115 | pub fn define_external_function(&self, name: &'static str, symbol: &'static str, ty: FunctionType) {
116 | assert_eq!(self.stage, Stage::Lex);
117 | let id = self.ctxt.names.borrow_mut().new_id(name);
118 | let func = Function::External(ExternalFunction::new(symbol, ty));
119 | let ty = Type::Function(id);
120 | self.ctxt.functions.borrow_mut().insert(id, func);
121 | self.ctxt.types.borrow_mut().set_val(id, 0, ty);
122 | }
123 |
124 | pub fn define_global_constant(&self, name: &'static str, value: Number) {
125 | // must be done before typecheck
126 | let id = self.ctxt.names.borrow_mut().new_id(name);
127 | assert!(self.stage != Stage::Complete && self.stage != Stage::Codegen);
128 | self.ctxt.ast.borrow_mut().insert(0, ast::Item::Assignment(Node(ast::Assignment {
129 | ident: Node(id, SourcePos::anon()),
130 | expr: ast::Expression::Constant(Node(value, SourcePos::anon())),
131 | }, SourcePos::anon())));
132 | }
133 |
134 | pub fn define_intrinsics(&self) {
135 | let num_num_ty = &make_fn_ty!(self.ctxt, fn(x: Number) -> Number);
136 | let num_2num_ty = &make_fn_ty!(self.ctxt, fn(x: Number, y: Number) -> Number);
137 |
138 | self.define_external_function("sin", "llvm.sin.f64", num_num_ty.clone());
139 | self.define_external_function("cos", "llvm.cos.f64", num_num_ty.clone());
140 | self.define_external_function("log", "llvm.log.f64", num_num_ty.clone());
141 | self.define_external_function("log10", "llvm.log10.f64", num_num_ty.clone());
142 | self.define_external_function("log2", "llvm.log2.f64", num_num_ty.clone());
143 | self.define_external_function("exp", "llvm.exp.f64", num_num_ty.clone());
144 | self.define_external_function("exp2", "llvm.exp2.f64", num_num_ty.clone());
145 | self.define_external_function("sqrt", "llvm.sqrt.f64", num_num_ty.clone());
146 | self.define_external_function("abs", "llvm.fabs.f64", num_num_ty.clone());
147 | self.define_external_function("floor", "llvm.floor.f64", num_num_ty.clone());
148 | self.define_external_function("ceil", "llvm.ceil.f64", num_num_ty.clone());
149 | self.define_external_function("trunc", "llvm.trunc.f64", num_num_ty.clone());
150 | self.define_external_function("round", "llvm.round.f64", num_num_ty.clone());
151 |
152 | self.define_external_function("pow", "llvm.pow.f64", num_2num_ty.clone());
153 | self.define_external_function("min", "llvm.minnum.f64", num_2num_ty.clone());
154 | self.define_external_function("max", "llvm.maxnum.f64", num_2num_ty.clone());
155 | }
156 |
157 | /// Defines a function as externally accessible through get_fn after compilation
158 | pub fn define_entrypoint(&self, name: &'a str, ty: FunctionType) {
159 | // must be done before typecheck
160 | assert!(self.stage != Stage::Complete && self.stage != Stage::Codegen);
161 | let id = self.ctxt.names.borrow_mut().new_id(name);
162 | self.ctxt.entrypoints.borrow_mut().insert(id, ty);
163 | }
164 |
165 | pub unsafe fn get_fn(&self, name: &str) -> Option R> {
166 | assert_eq!(self.stage, Stage::Complete);
167 | match self.codegen.as_ref().unwrap().module.get_function(name) {
168 | Some(f) => {
169 | Some(self.engine.as_ref().unwrap().get_function(f))
170 | },
171 | None => None,
172 | }
173 | }
174 |
175 | pub fn get_init_fn(&self) -> extern fn(()) {
176 | unsafe {
177 | self.get_fn(GLOBAL_INIT_FN_NAME).unwrap()
178 | }
179 | }
180 | }
181 |
--------------------------------------------------------------------------------
/src/interpreter/functions.rs:
--------------------------------------------------------------------------------
1 | use super::ast;
2 | use super::types::{FunctionType};
3 | use super::ident::Identifier;
4 | use super::tokens::{Node, SourcePos};
5 |
6 | use vec_map::VecMap;
7 | use std::ops::Deref;
8 | use bit_set::BitSet;
9 |
10 | #[derive(Debug, Clone)]
11 | pub enum Function {
12 | User(UserFunction),
13 | Pointer(PointerFunction),
14 | External(ExternalFunction),
15 | }
16 |
17 | impl Function {
18 | pub fn has_concrete_type(&self) -> bool {
19 | match *self {
20 | Function::User(ref f) => f.ty.is_some(),
21 | Function::Pointer(_) |
22 | Function::External(_) => true,
23 | }
24 | }
25 | pub fn args(&self) -> &ast::ArgumentList {
26 | match *self {
27 | Function::User(ref def) => { def.args() },
28 | Function::Pointer(ref def) => { &def.args }
29 | Function::External(ref def) => { &def.args }
30 | }
31 | }
32 | pub fn ty(&self) -> Option<&FunctionType> {
33 | match *self {
34 | Function::User(ref def) => { def.ty.as_ref() },
35 | Function::Pointer(ref def) => { Some(&def.ty) }
36 | Function::External(ref def) => { Some(&def.ty) }
37 | }
38 | }
39 | pub fn set_ty(&mut self, ty: FunctionType) {
40 | match *self {
41 | Function::User(ref mut def) => { def.ty = Some(ty) }
42 | Function::Pointer(ref mut def) => { def.ty = ty }
43 | Function::External(ref mut def) => { def.ty = ty }
44 | }
45 | }
46 | }
47 |
48 | #[derive(Debug, Clone)]
49 | pub struct UserFunction {
50 | pub ty: Option,
51 | pub node: Node,
52 | }
53 |
54 | impl Deref for UserFunction {
55 | type Target = Node;
56 |
57 | fn deref<'a>(&'a self) -> &'a Node {
58 | &self.node
59 | }
60 | }
61 |
62 | #[derive(Debug, Clone)]
63 | pub struct ExternalFunction {
64 | pub ty: FunctionType,
65 | pub args: ast::ArgumentList,
66 | pub symbol: &'static str,
67 | }
68 |
69 | impl ExternalFunction {
70 | pub fn new(symbol: &'static str, ty: FunctionType) -> ExternalFunction {
71 | let mut args = Vec::new();
72 | for (id, _) in &ty.args {
73 | args.push(ast::Argument::Ident(Node(id, SourcePos::anon())));
74 | }
75 | ExternalFunction {
76 | ty: ty,
77 | args: args,
78 | symbol: symbol,
79 | }
80 | }
81 | }
82 |
83 | #[derive(Debug, Clone)]
84 | #[allow(raw_pointer_derive)]
85 | pub struct PointerFunction {
86 | pub ty: FunctionType,
87 | pub args: ast::ArgumentList,
88 | pub ptr: *mut (),
89 | }
90 |
91 | impl PointerFunction {
92 | pub fn new(ty: FunctionType, ptr: *mut ()) -> PointerFunction {
93 | let mut args = Vec::new();
94 | for (id, _) in &ty.args {
95 | args.push(ast::Argument::Ident(Node(id, SourcePos::anon())));
96 | }
97 | PointerFunction {
98 | ty: ty,
99 | args: args,
100 | ptr: ptr,
101 | }
102 | }
103 | }
104 |
105 | #[derive(Debug)]
106 | pub struct FunctionTable {
107 | pub map: VecMap, // from Identifier.
108 | }
109 |
110 | // Holds the actual implementation details of functions.
111 | impl FunctionTable {
112 | pub fn new() -> FunctionTable {
113 | FunctionTable {
114 | map: VecMap::new(),
115 | }
116 | }
117 |
118 | pub fn insert(&mut self, ident: Identifier, func: Function) {
119 | self.map.insert(ident, func);
120 | }
121 |
122 | pub fn get<'a>(&'a self, ident: Identifier) -> Option<&'a Function> {
123 | self.map.get(&ident)
124 | }
125 |
126 | pub fn get_mut<'a>(&'a mut self, ident: Identifier) -> Option<&'a mut Function> {
127 | self.map.get_mut(&ident)
128 | }
129 | }
130 |
131 | #[derive(Debug)]
132 | pub struct CallStack {
133 | stack: Vec,
134 | recursive: BitSet,
135 | }
136 |
137 | impl CallStack {
138 | pub fn new() -> CallStack {
139 | CallStack {
140 | stack: Vec::new(),
141 | recursive: BitSet::new(),
142 | }
143 | }
144 | pub fn push(&mut self, id: Identifier) {
145 | for &func in &self.stack {
146 | if func == id {
147 | self.recursive.insert(id);
148 | break;
149 | }
150 | }
151 | self.stack.push(id);
152 | }
153 | pub fn pop(&mut self) {
154 | self.stack.pop();
155 | }
156 | pub fn is_recursive(&self, id: Identifier) -> bool {
157 | self.recursive.contains(&id)
158 | }
159 | }
160 |
--------------------------------------------------------------------------------
/src/interpreter/ident.rs:
--------------------------------------------------------------------------------
1 | use std::collections::HashMap;
2 |
3 | /// Represents an identifier name in program source code.
4 | pub type Identifier = usize;
5 |
6 | #[derive(Debug)]
7 | pub struct NameTable<'a> {
8 | identifier_names: HashMap,
9 | identifier_ids: HashMap<&'a str, Identifier>,
10 | max_id: usize,
11 | }
12 |
13 | const ANON_NAME: &'static str = "*anon*";
14 |
15 | impl<'a> NameTable<'a> {
16 | pub fn new() -> NameTable<'a> {
17 | NameTable {
18 | identifier_names: HashMap::new(),
19 | identifier_ids: HashMap::new(),
20 | max_id: 0,
21 | }
22 | }
23 |
24 | pub fn new_id(&mut self, name: &'a str) -> Identifier {
25 | if let Some(id) = self.get_id(name) {
26 | return id;
27 | }
28 | let id = self.max_id;
29 | self.max_id += 1;
30 | self.identifier_names.insert(id, name);
31 | self.identifier_ids.entry(name).or_insert(id);
32 | id
33 | }
34 | /// Creates a new identifier which cannot be looked up by name.
35 | pub fn new_anon(&mut self) -> Identifier {
36 | let id = self.max_id;
37 | self.max_id += 1;
38 | self.identifier_names.insert(id, ANON_NAME);
39 | id
40 | }
41 | pub fn is_anon(&self, id: Identifier) -> Option {
42 | self.identifier_names.get(&id).map(|&x| x == ANON_NAME)
43 | }
44 | pub fn get_id(&'a self, name: &'a str) -> Option {
45 | self.identifier_ids.get(name).map(|x| *x)
46 | }
47 | pub fn get_name(&'a self, id: Identifier) -> Option<&'a str> {
48 | self.identifier_names.get(&id).map(|x| *x)
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/interpreter/issue.rs:
--------------------------------------------------------------------------------
1 | use super::tokens::SourcePos;
2 | use super::common::Context;
3 |
4 | use std::fmt;
5 | use std::borrow::Cow;
6 |
7 | #[derive(Copy, Clone, Debug, PartialEq)]
8 | pub enum Level {
9 | Error,
10 | Warning,
11 | }
12 |
13 | #[derive(Debug, Clone)]
14 | pub struct Issue<'a> {
15 | pub source: &'a str,
16 | pub filename: &'a str,
17 | pub pos: SourcePos,
18 | pub msg: Cow<'static, str>,
19 | pub ty: Level,
20 | }
21 |
22 | impl<'a> Issue<'a> {
23 | pub fn new(source: &'a str,
24 | filename: &'a str,
25 | pos: SourcePos,
26 | ty: Level,
27 | msg: Cow<'static, str>) -> Issue<'a> {
28 | Issue {
29 | source: source,
30 | filename: filename,
31 | pos: pos,
32 | msg: msg,
33 | ty: ty,
34 | }
35 | }
36 | }
37 |
38 | impl<'a> fmt::Display for Issue<'a> {
39 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 | // oh god why
41 | let line = &self.source[self.pos.line_index..];
42 | let line = line[..line.find('\n').unwrap_or(line.len())].to_string();
43 | let line = line.replace("\t", " ");
44 | let align = self.filename.len() + self.pos.to_string().len() + 3;
45 | write!(f, "{0}+{1} ┬ {2:?}: {3}\n{4:>5$} {6}\n{7:>5$}{8:─>9$}┘",
46 | self.filename, self.pos, self.ty, self.msg,
47 | "┃", align, line,
48 | "└", "", self.pos.index - self.pos.line_index + 1
49 | )
50 | }
51 | }
52 |
53 | #[derive(Debug, Clone)]
54 | pub struct IssueTracker<'a> {
55 | issues: Vec>,
56 | }
57 |
58 | impl<'a> IssueTracker<'a> {
59 | pub fn new() -> IssueTracker<'a> {
60 | IssueTracker {
61 | issues: Vec::new(),
62 | }
63 | }
64 |
65 | pub fn new_issue(&mut self, ctxt: &'a Context, pos: SourcePos, ty: Level, msg: T)
66 | where T: Into> {
67 | let issue = Issue::new(&ctxt.source, &ctxt.filename, pos, ty, msg.into());
68 | self.issues.push(issue);
69 | }
70 |
71 | pub fn has_errors(&self) -> bool {
72 | self.issues.iter().fold(false, |acc, ref item| acc | (item.ty == Level::Error))
73 | }
74 | pub fn has_warnings(&self) -> bool {
75 | self.issues.iter().fold(false, |acc, ref item| acc | (item.ty == Level::Warning))
76 | }
77 |
78 | pub fn clear(&mut self) {
79 | self.issues.clear();
80 | }
81 | }
82 |
83 | impl<'a> fmt::Display for IssueTracker<'a> {
84 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85 | if self.issues.len() == 0 {
86 | write!(f, "No issues!")
87 | } else {
88 | for issue in self.issues.iter() {
89 | try!(write!(f, "{}\n", issue));
90 | }
91 | Ok(())
92 | }
93 | }
94 | }
95 |
96 |
--------------------------------------------------------------------------------
/src/interpreter/lexer.rs:
--------------------------------------------------------------------------------
1 | use super::common::Context;
2 | use super::tokens::*;
3 |
4 | use regex::Regex;
5 | use std::str::FromStr;
6 |
7 | static IDENT_REGEX: Regex = regex!(r"[a-zA-Z_~']+[a-zA-Z_~0-9']*");
8 | static WHITESPACE_REGEX: Regex = regex!(r"[ \t]+");
9 | static CONST_REGEX: Regex = regex!(r"([0-9]+\.?[0-9]*|[0-9]*\.?[0-9]+)([eE]-?[0-9]+)?");
10 | static OPERATOR_REGEX: Regex = regex!(r"\^\^|>=|<=|!=|[\+\*/\^>(ctxt: &'a Context<'a>) {
17 | let mut walk = &ctxt.source[..];
18 | let mut tokens = ctxt.tokens.borrow_mut();
19 | let mut pos = SourcePos::new();
20 |
21 | while walk.len() > 0 {
22 | // Strip whitespace
23 | if let Some((0, x)) = WHITESPACE_REGEX.find(walk) {
24 | walk = &walk[x..];
25 | pos.add_chars(x);
26 | continue;
27 | }
28 |
29 | // Strip comments
30 | if let Some((0, x)) = COMMENT_REGEX.find(walk) {
31 | walk = &walk[x..];
32 | pos.add_chars(x);
33 | continue;
34 | }
35 |
36 | // Add operators
37 | if let Some((0, x)) = OPERATOR_REGEX.find(walk) {
38 | // If this fails either the regex or the parser is wrong.
39 | let op = Operator::parse(&walk[0..x]).unwrap();
40 | tokens.push(Node(Token::Operator(op), pos));
41 | walk = &walk[x..];
42 | pos.add_chars(x);
43 | continue;
44 | }
45 |
46 | // Add symbols
47 | if let Some((0, x)) = SYMBOL_REGEX.find(walk) {
48 | // If this fails either the regex or the parser is wrong.
49 | let sym = Symbol::parse(&walk[0..x]).unwrap();
50 | tokens.push(Node(Token::Symbol(sym), pos));
51 | walk = &walk[x..];
52 | pos.add_chars(x);
53 | continue;
54 | }
55 |
56 | // Add boolean literals
57 | if let Some((0, x)) = BOOLEAN_REGEX.find(walk) {
58 | // If this fails either the regex or the parser is wrong.
59 | let val = bool::from_str(&walk[0..x]).unwrap();
60 | tokens.push(Node(Token::Boolean(val), pos));
61 | walk = &walk[x..];
62 | pos.add_chars(x);
63 | continue;
64 | }
65 |
66 | // Add identifiers
67 | if let Some((0, x)) = IDENT_REGEX.find(walk) {
68 | let id = ctxt.names.borrow_mut().new_id(&walk[0..x]);
69 | tokens.push(Node(Token::Ident(id), pos));
70 | walk = &walk[x..];
71 | pos.add_chars(x);
72 | continue;
73 | }
74 |
75 | // Strip newlines
76 | if let Some((0, x)) = NEWLINE_REGEX.find(walk) {
77 | walk = &walk[x..];
78 | pos.add_line();
79 | continue;
80 | }
81 |
82 | if let Some((0, x)) = CONST_REGEX.find(walk) {
83 | let v = walk[0..x].parse().unwrap(); // If this fails either the regex or the parser is wrong.
84 | tokens.push(Node(Token::Const(v), pos));
85 | walk = &walk[x..];
86 | pos.add_chars(x);
87 | continue;
88 | }
89 |
90 | // If none of the checks above found a token, then it's not supported.
91 | ctxt.emit_error("unrecognized token", pos);
92 | walk = &walk[1..];
93 | pos.add_chars(1);
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/src/interpreter/lib.rs:
--------------------------------------------------------------------------------
1 | #![feature(plugin, optin_builtin_traits, vec_push_all)]
2 | #![plugin(regex_macros, docopt_macros)]
3 |
4 | extern crate regex;
5 | extern crate llvm;
6 | extern crate cbox;
7 | extern crate bit_set;
8 | extern crate sound_stream;
9 | extern crate hound;
10 | extern crate vec_map;
11 | extern crate llvm_sys;
12 |
13 | pub mod common;
14 | pub mod ident;
15 | #[macro_use] pub mod types;
16 | pub mod tokens;
17 | pub mod ast;
18 | pub mod issue;
19 | pub mod lexer;
20 | pub mod parser;
21 | pub mod functions;
22 | pub mod typecheck;
23 | pub mod codegen;
24 | pub mod scope;
25 | pub mod compiler;
26 | pub mod audio;
27 |
28 | #[macro_use]
29 | pub mod tests;
30 |
--------------------------------------------------------------------------------
/src/interpreter/parser.rs:
--------------------------------------------------------------------------------
1 | use super::issue::Level;
2 | use super::tokens::{SourcePos, Token, Symbol, Bracket, Associativity, Node, NodeImpl};
3 | use super::ident::Identifier;
4 | use super::common::Context;
5 | use super::ast::*;
6 | use super::functions::{self, FunctionTable};
7 |
8 | use std::borrow::Cow;
9 | use std::cell::{Ref, RefMut};
10 |
11 | macro_rules! try_opt(
12 | ( $val:expr ) => {
13 | match $val {
14 | Some(x) => x,
15 | None => return None,
16 | }
17 | }
18 | );
19 |
20 | macro_rules! expect_value(
21 | ( $tokens:ident, $ty:path ) => {
22 | $tokens.next().and_then(|t| {
23 | match t.item() {
24 | &$ty(v) => Some(Node(v, t.pos())),
25 | _ => None
26 | }
27 | })
28 | };
29 | );
30 |
31 | macro_rules! expect(
32 | ( $tokens:ident, $ty:pat ) => {
33 | $tokens.next().and_then(|x| {
34 | match x.item() {
35 | &$ty => Some(x),
36 | _ => None,
37 | }
38 | })
39 | };
40 | );
41 |
42 | pub fn parse<'a>(ctxt: &'a Context<'a>) {
43 | let mut parser = Parser::new(ctxt);
44 | parser.parse();
45 | }
46 |
47 | struct Parser<'a> {
48 | ctxt: &'a Context<'a>,
49 | tokens: Ref<'a, Vec>>,
50 | functions: RefMut<'a, FunctionTable>,
51 | sub_stack: Vec<(usize, usize, usize)>,
52 | }
53 |
54 | impl<'a> Parser<'a> {
55 | fn new(ctxt: &'a Context<'a>) -> Parser<'a> {
56 | let len = ctxt.tokens.borrow().len();
57 | Parser {
58 | ctxt: ctxt,
59 | tokens: ctxt.tokens.borrow(),
60 | functions: ctxt.functions.borrow_mut(),
61 | sub_stack: vec![(0, 0, len)],
62 | }
63 | }
64 |
65 | fn next(&mut self) -> Option> {
66 | self.seek(1);
67 | self.peek(-1)
68 | }
69 |
70 | fn next_token(&mut self) -> Option {
71 | self.seek(1);
72 | self.peek(-1).item()
73 | }
74 |
75 | fn is_empty(&self) -> bool {
76 | self.index() >= self.end_index()
77 | }
78 |
79 | fn index(&self) -> usize {
80 | self.sub_stack.last().unwrap().0
81 | }
82 |
83 | fn start_index(&self) -> usize {
84 | self.sub_stack.last().unwrap().1
85 | }
86 |
87 | fn end_index(&self) -> usize {
88 | self.sub_stack.last().unwrap().2
89 | }
90 |
91 | fn len(&self) -> usize {
92 | self.end_index() - self.start_index()
93 | }
94 |
95 | fn enter_subsection(&mut self, from: usize, to: usize) {
96 | //println!("enter {} {}", from, to);
97 | assert!(from >= self.start_index() && to <= self.end_index() && from <= to);
98 | let index = self.index();
99 | self.sub_stack.push((index, from, to));
100 | }
101 |
102 | // returns index subsection was left at
103 | fn leave_subsection(&mut self) -> usize {
104 | //println!("leave");
105 | let section = self.sub_stack.pop().unwrap();
106 | section.0
107 | }
108 |
109 | // leaves subsection and replaces index with index of subsection
110 | fn integrate_subsection(&mut self) {
111 | let index = self.leave_subsection();
112 | self.set_index(index);
113 | }
114 |
115 | fn offset(&self, offset: isize) -> usize {
116 | ((self.index() as isize) + offset) as usize
117 | }
118 |
119 | fn set_index(&mut self, index: usize) {
120 | self.sub_stack.last_mut().unwrap().0 = index;
121 | }
122 |
123 | fn seek(&mut self, offset: isize) {
124 | let index = self.offset(offset);
125 | self.set_index(index);
126 | }
127 |
128 | fn in_bounds(&self, index: usize) -> bool {
129 | if index >= self.end_index() || index < self.start_index() {
130 | false
131 | } else {
132 | true
133 | }
134 | }
135 |
136 | fn peek(&self, offset: isize) -> Option> {
137 | let index = self.offset(offset);
138 | if self.in_bounds(index) {
139 | self.peek_index(index)
140 | } else {
141 | None
142 | }
143 | }
144 |
145 | fn peek_index(&self, index: usize) -> Option> {
146 | if self.in_bounds(index) {
147 | Some(self.tokens[index])
148 | } else {
149 | None
150 | }
151 | }
152 |
153 | fn peek_token(&self, offset: isize) -> Option {
154 | self.peek(offset).item()
155 | }
156 |
157 | fn peek_source_pos(&self, offset: isize) -> Option {
158 | self.peek(offset).pos()
159 | }
160 |
161 | fn peek_source_pos_or_end(&self, offset: isize) -> SourcePos {
162 | self.peek_source_pos(offset).unwrap_or(self.end_source_pos())
163 | }
164 |
165 | fn end_source_pos(&self) -> SourcePos {
166 | if self.tokens.len() > 0 {
167 | let index = self.end_index() - 1;
168 | if index < self.tokens.len() {
169 | let mut pos = self.tokens[index].pos();
170 | pos.add_chars(1);
171 | pos
172 | } else {
173 | panic!("internal error");
174 | }
175 | } else {
176 | SourcePos::new()
177 | }
178 | }
179 |
180 | // Advances to the matching parenthesis
181 | // Expects the first opening paren to already have been consumed.
182 | fn match_bracket(&mut self) -> bool {
183 | let open = match self.peek_token(-1) {
184 | Some(x) => x,
185 | _ => return false,
186 | };
187 | let close = match open {
188 | Token::Symbol(Symbol::LeftBracket(x)) =>
189 | Token::Symbol(Symbol::RightBracket(x)),
190 | _ => return false,
191 | };
192 |
193 | let mut depth = 1i32;
194 | while depth > 0 {
195 | match self.next_token() {
196 | Some(x) if x == open => {
197 | depth += 1;
198 | }
199 | Some(x) if x == close => {
200 | depth -= 1;
201 | }
202 | None => {
203 | self.emit_error_here(format!("expected `{}`", close));
204 | return false;
205 | }
206 | _ => { }
207 | }
208 | }
209 | return true;
210 | }
211 |
212 | fn emit_error_here(&self, msg: S) where S: Into> {
213 | self.ctxt.issues.borrow_mut().new_issue(self.ctxt, self.peek_source_pos_or_end(-1),
214 | Level::Error, msg);
215 | }
216 |
217 | pub fn parse(&mut self) {
218 | let mut items = self.ctxt.ast.borrow_mut();
219 | while !self.is_empty() {
220 | match self.parse_item() {
221 | Some(i) => items.push(i),
222 | None => return,
223 | }
224 | }
225 | }
226 |
227 | fn parse_expression(&mut self) -> Option {
228 | let expr = try_opt!(self.pratt_expression(0));
229 | self.seek(1);
230 | Some(expr)
231 | }
232 |
233 | // Implements a Pratt parser for parsing expressions
234 | fn pratt_expression(&mut self, rbp: i32) -> Option {
235 | //println!("expr {}", rbp);
236 | let mut token = self.next();
237 | let mut left = try_opt!(self.pratt_nud(token));
238 | let mut next = self.next();
239 | while rbp < try_opt!(self.pratt_lbp(next)) {
240 | token = next;
241 | left = try_opt!(self.pratt_led(left, token));
242 | next = self.next();
243 | }
244 | self.seek(-1); // when we break out of the loop, next hasn't been consumed yet, so we need to step back
245 | Some(left)
246 | }
247 |
248 | fn pratt_nud(&mut self, token: Option>) -> Option {
249 | //println!("nud {:?}", token);
250 | match token.item() {
251 | Some(Token::Const(v)) => Some(Expression::Constant(Node(v, token.pos().unwrap()))),
252 |
253 | Some(Token::Boolean(v)) => Some(Expression::Boolean(Node(v, token.pos().unwrap()))),
254 |
255 | Some(Token::Ident(id)) => Some(Expression::Variable(Node(id, token.pos().unwrap()))),
256 |
257 | // unary operator
258 | Some(Token::Operator(op)) if op.can_take_x_args(1) => {
259 | Some(Expression::Prefix(Box::new(Node(Prefix {
260 | op: Node(op, token.pos().unwrap()),
261 | expr: try_opt!(self.pratt_expression(100))
262 | }, token.pos().unwrap()))))
263 | }
264 |
265 | // start of group
266 | Some(Token::Symbol(Symbol::LeftBracket(Bracket::Round))) => {
267 | let expr = try_opt!(self.pratt_expression(1));
268 | if expect!(self, Token::Symbol(Symbol::RightBracket(Bracket::Round))).is_none() {
269 | self.emit_error_here("expected `)`");
270 | return None;
271 | }
272 | Some(expr)
273 | }
274 |
275 | // start of block
276 | Some(Token::Symbol(Symbol::LeftBracket(Bracket::Curly))) => {
277 | self.seek(-1);
278 | let block = try_opt!(self.parse_block());
279 | Some(Expression::Block(block))
280 | }
281 |
282 | // closure
283 | Some(Token::Symbol(Symbol::Backslash)) => {
284 | self.seek(-1);
285 | let def = try_opt!(self.parse_closure());
286 | Some(Expression::Closure(Box::new(def)))
287 | }
288 |
289 | _ => {
290 | self.emit_error_here("expected constant, variable, opening bracket or unary operator");
291 | return None;
292 | }
293 | }
294 | }
295 |
296 | fn pratt_led(&mut self, left: Expression, right: Option>) -> Option {
297 | //println!("led {:?} {:?}", left, right);
298 | match right.item() {
299 | // binary operator
300 | Some(Token::Operator(op)) => {
301 | if !op.can_take_x_args(2) {
302 | self.emit_error_here("expected binary operator");
303 | return None;
304 | }
305 | let precedence = op.precedence() -
306 | if op.associativity() == Associativity::Right { 1 } else { 0 };
307 | let pos = left.pos();
308 | Some(Expression::Infix(Box::new(Node(Infix {
309 | op: Node(op, right.pos().unwrap()),
310 | left: left,
311 | right: try_opt!(self.pratt_expression(precedence)),
312 | }, pos))))
313 | }
314 |
315 | // extra closing paren
316 | Some(Token::Symbol(Symbol::RightBracket(Bracket::Round))) => {
317 | self.emit_error_here("unexpected `)`");
318 | return None;
319 | }
320 |
321 | // function call
322 | Some(Token::Symbol(Symbol::LeftBracket(Bracket::Round))) |
323 | Some(Token::Symbol(Symbol::LeftBracket(Bracket::Square))) => {
324 | self.seek(-1);
325 | let call = try_opt!(self.parse_function_call(left));
326 | Some(Expression::FunctionCall(Box::new(call)))
327 | }
328 |
329 | // if
330 | Some(Token::Symbol(Symbol::If)) => {
331 | let pos = left.pos();
332 | let then = left;
333 | let cond = try_opt!(self.pratt_expression(1));
334 | if let Some(Token::Symbol(Symbol::Else)) = self.next_token() {
335 | let els = try_opt!(self.pratt_expression(1));
336 | Some(Expression::Conditional(Box::new(Node(Conditional {
337 | cond: cond,
338 | then: then,
339 | els: els,
340 | }, pos))))
341 | } else {
342 | self.seek(-1);
343 | self.emit_error_here("expected `else`");
344 | None
345 | }
346 | }
347 |
348 | _ => {
349 | panic!("expected left denotation");
350 | }
351 | }
352 | }
353 |
354 | fn pratt_lbp(&mut self, token: Option>) -> Option {
355 | //println!("lbp {:?}", token);
356 | match token.item() {
357 | Some(Token::Operator(op)) => Some(op.precedence()),
358 |
359 | Some(Token::Symbol(Symbol::If)) => Some(2),
360 | Some(Token::Symbol(Symbol::Else)) => Some(0),
361 |
362 | // end of group
363 | Some(Token::Symbol(Symbol::RightBracket(Bracket::Round))) => Some(1),
364 |
365 | // function call
366 | Some(Token::Symbol(Symbol::LeftBracket(Bracket::Round))) |
367 | Some(Token::Symbol(Symbol::LeftBracket(Bracket::Square))) => Some(1000),
368 |
369 | None => Some(0),
370 | _ => {
371 | self.emit_error_here("expected binary operator or function call");
372 | return None;
373 | }
374 | }
375 | }
376 |
377 | fn parse_assignment(&mut self) -> Option> {
378 | let pos = self.peek_source_pos_or_end(0);
379 | let ident = try_opt!(self.parse_ident());
380 | try_opt!(self.parse_symbol(Symbol::Equals));
381 | let expr = try_opt!(self.parse_expression());
382 |
383 | Some(Node(Assignment {
384 | ident: ident,
385 | expr: expr
386 | }, pos))
387 | }
388 |
389 | fn parse_symbol(&mut self, symbol: Symbol) -> Option<()> {
390 | match self.next_token() {
391 | Some(Token::Symbol(x)) if x == symbol => Some(()),
392 | _ => {
393 | self.emit_error_here(format!("expected {}", symbol));
394 | None
395 | },
396 | }
397 | }
398 |
399 | fn parse_ident(&mut self) -> Option> {
400 | match expect_value!(self, Token::Ident) {
401 | None => {
402 | self.emit_error_here("expected identifier");
403 | None
404 | }
405 | x => x,
406 | }
407 | }
408 |
409 | fn parse_function_def(&mut self) -> Option> {
410 | let pos = self.peek_source_pos_or_end(0);
411 | let ident = try_opt!(self.parse_ident());
412 | let func = try_opt!(self.parse_function());
413 |
414 | self.functions.insert(*ident, functions::Function::User(
415 | functions::UserFunction {
416 | ty: None,
417 | node: Node(func.item().clone(), pos),
418 | }));
419 |
420 | Some(Node(FunctionDef {
421 | ident: ident,
422 | func: func,
423 | }, pos))
424 | }
425 |
426 | fn parse_closure(&mut self) -> Option> {
427 | let pos = self.peek_source_pos_or_end(0);
428 | try_opt!(self.parse_symbol(Symbol::Backslash));
429 | let func = try_opt!(self.parse_function());
430 | let ident = self.ctxt.names.borrow_mut().new_anon();
431 | self.functions.insert(ident, functions::Function::User(
432 | functions::UserFunction {
433 | ty: None,
434 | node: Node(func.item().clone(), pos),
435 | }));
436 |
437 | Some(Node(FunctionDef {
438 | ident: Node(ident, pos),
439 | func: func,
440 | }, pos))
441 | }
442 |
443 | fn find_smart(&mut self, search: Token) -> Option {
444 | let start_idx = self.index();
445 | loop {
446 | let token = self.next_token();
447 | match token {
448 | Some(t) if t == search => {
449 | break;
450 | }
451 | Some(Token::Symbol(Symbol::LeftBracket(_))) => {
452 | if !self.match_bracket() {
453 | self.set_index(start_idx);
454 | return None;
455 | }
456 | },
457 | Some(Token::Symbol(Symbol::Backslash)) => {
458 | let closure_block = try_opt!(self.find_smart(
459 | Token::Symbol(Symbol::LeftBracket(Bracket::Curly))));
460 | self.set_index(closure_block+1);
461 | if !self.match_bracket() {
462 | self.set_index(start_idx);
463 | return None;
464 | }
465 | }
466 | None => {
467 | self.set_index(start_idx);
468 | return None;
469 | },
470 | _ => { }
471 | }
472 | }
473 | self.seek(-1);
474 | let index = self.index();
475 | assert!(self.peek_token(0).unwrap() == search);
476 | self.set_index(start_idx);
477 | Some(index)
478 | }
479 |
480 | fn parse_function(&mut self) -> Option> {
481 | let pos = self.peek_source_pos_or_end(0);
482 |
483 | let idx = self.index();
484 | let block_idx = match self.find_smart(Token::Symbol(Symbol::LeftBracket(Bracket::Curly))) {
485 | Some(x) => x,
486 | None => {
487 | self.ctxt.emit_error("expected `{` to start function block", self.end_source_pos());
488 | return None;
489 | }
490 | };
491 | // parse arg list (everything until start of block)
492 | self.enter_subsection(idx, block_idx);
493 | let args = try_opt!(self.parse_arg_list(None));
494 | self.integrate_subsection();
495 |
496 | let block = try_opt!(self.parse_block());
497 |
498 | Some(Node(Function {
499 | args: args,
500 | block: block,
501 | }, pos))
502 | }
503 |
504 | fn parse_block(&mut self) -> Option> {
505 | let pos = self.peek_source_pos_or_end(0);
506 | try_opt!(self.parse_symbol(Symbol::LeftBracket(Bracket::Curly)));
507 | let brace = self.find_smart(Token::Symbol(Symbol::RightBracket(Bracket::Curly)));
508 | if brace.is_none() {
509 | self.ctxt.emit_error("expected `}`", self.end_source_pos());
510 | return None;
511 | }
512 | let mut stmts = Vec::new();
513 | let idx = self.index();
514 | self.enter_subsection(idx, brace.unwrap());
515 | loop {
516 | let semi = self.find_smart(Token::Symbol(Symbol::Semicolon));
517 | let semi_idx = semi.unwrap_or(self.end_index());
518 | if semi_idx == self.index() {
519 | break;
520 | }
521 | let idx = self.index();
522 | self.enter_subsection(idx, semi_idx);
523 | let stmt = try_opt!(self.parse_statement());
524 | stmts.push(stmt);
525 | self.integrate_subsection();
526 | if semi.is_none() {
527 | self.seek(-1);
528 | break;
529 | }
530 | }
531 | self.integrate_subsection();
532 | self.seek(1);
533 | Some(Node(stmts, pos))
534 | }
535 |
536 | fn parse_statement(&mut self) -> Option {
537 | match (self.next_token(), self.next_token()) {
538 | (Some(Token::Ident(_)), Some(Token::Symbol(Symbol::Equals))) => {
539 | self.seek(-2);
540 | Some(Statement::Assignment(try_opt!(self.parse_assignment())))
541 | },
542 | _ => {
543 | self.seek(-2);
544 | Some(Statement::Expression(try_opt!(self.parse_expression())))
545 | }
546 | }
547 | }
548 |
549 | // an item is a top level construct: either an assignment or a function definition
550 | fn parse_item(&mut self) -> Option- {
551 | try_opt!(self.parse_ident());
552 |
553 | match self.next_token() {
554 | Some(Token::Symbol(Symbol::Equals)) => {
555 | self.seek(-2);
556 | let semi = self.find_smart(Token::Symbol(Symbol::Semicolon));
557 | if semi.is_none() {
558 | self.ctxt.emit_error("expected `;`", self.end_source_pos());
559 | return None;
560 | }
561 | let idx = self.index();
562 | self.enter_subsection(idx, semi.unwrap());
563 | let assign = try_opt!(self.parse_assignment());
564 | self.integrate_subsection();
565 | Some(Item::Assignment(assign))
566 | },
567 | _ => {
568 | self.seek(-1);
569 | let brace = self.find_smart(Token::Symbol(Symbol::LeftBracket(Bracket::Curly)));
570 | if brace.is_some() {
571 | self.seek(-1);
572 | Some(Item::FunctionDef(try_opt!(self.parse_function_def())))
573 | } else {
574 | self.emit_error_here("expected assignment or function definition");
575 | None
576 | }
577 | }
578 | }
579 | }
580 |
581 | fn parse_arg_list(&mut self, calltype: Option
) -> Option> {
582 | let pos = self.peek_source_pos_or_end(0);
583 | let mut args = Vec::new();
584 | if self.len() == 0 {
585 | return Some(Node(args, pos))
586 | }
587 | loop {
588 | let idx = self.index();
589 | let comma = self.find_smart(Token::Symbol(Symbol::Comma));
590 | let token_idx = comma.unwrap_or(self.end_index());
591 | self.enter_subsection(idx, token_idx);
592 | let arg = match calltype {
593 | None => try_opt!(self.parse_arg_named(false)),
594 | Some(CallType::Named) => try_opt!(self.parse_arg_named(true)),
595 | Some(CallType::Ordered) => try_opt!(self.parse_arg_ordered()),
596 |
597 | };
598 | if let Some(id) = arg.ident() {
599 | if args.iter().any(|x| x.ident().unwrap() == id) {
600 | self.ctxt.emit_error("argument already previously defined", arg.pos());
601 | }
602 | }
603 | args.push(arg);
604 | self.integrate_subsection();
605 | if comma.is_none() {
606 | break;
607 | }
608 | }
609 | self.seek(-1); // no comma on the last one
610 | Some(Node(args, pos))
611 | }
612 |
613 | fn parse_arg_named(&mut self, allow_op: bool) -> Option {
614 | let (one, two, three) = (self.next(), self.next(), self.next());
615 | match (one.item(), two.item(), three.item()) {
616 | (Some(Token::Ident(id)),
617 | Some(Token::Operator(op)),
618 | Some(Token::Symbol(Symbol::Equals))) if allow_op => {
619 | if !op.can_take_x_args(2) {
620 | self.ctxt.emit_error("expected binary operator", two.pos().unwrap());
621 | return None;
622 | }
623 | let expr = try_opt!(self.parse_expression());
624 | Some(Argument::OpAssign(Node(id, one.pos().unwrap()), Node(op, two.pos().unwrap()), expr))
625 | },
626 | (Some(Token::Ident(id)),
627 | Some(Token::Symbol(Symbol::Equals)),
628 | _) => {
629 | self.seek(-1);
630 | let expr = try_opt!(self.parse_expression());
631 | Some(Argument::Assign(Node(id, one.pos().unwrap()), expr))
632 | },
633 | (Some(Token::Ident(id)),
634 | None,
635 | _) => {
636 | self.seek(-1);
637 | Some(Argument::Ident(Node(id, one.pos().unwrap())))
638 | },
639 | _ => {
640 | self.ctxt.emit_error("expected argument", self.peek_source_pos_or_end(-3));
641 | None
642 | }
643 | }
644 | }
645 |
646 | fn parse_arg_ordered(&mut self) -> Option {
647 | let expr = try_opt!(self.parse_expression());
648 | Some(Argument::Expr(expr))
649 | }
650 |
651 | fn parse_function_call(&mut self, callee: Expression) -> Option> {
652 | let pos = callee.pos();
653 | let (ty, brace) = match self.next_token() {
654 | Some(Token::Symbol(Symbol::LeftBracket(Bracket::Round))) =>
655 | (CallType::Ordered, Bracket::Round),
656 | Some(Token::Symbol(Symbol::LeftBracket(Bracket::Square))) =>
657 | (CallType::Named, Bracket::Square),
658 | _ => {
659 | self.emit_error_here("expected `(` or `[`");
660 | return None;
661 | }
662 | };
663 |
664 | let end = self.find_smart(Token::Symbol(Symbol::RightBracket(brace)));
665 | if end.is_none() {
666 | self.ctxt.emit_error(format!("expected `{}`", Symbol::RightBracket(brace)),
667 | self.end_source_pos());
668 | return None;
669 | }
670 | let idx = self.index();
671 | self.enter_subsection(idx, end.unwrap());
672 | let args = try_opt!(self.parse_arg_list(Some(ty)));
673 | self.integrate_subsection();
674 | self.seek(1); //consume closing brace
675 | Some(Node(FunctionCall {
676 | callee: callee,
677 | args: args,
678 | ty: ty,
679 | }, pos))
680 | }
681 | }
682 |
--------------------------------------------------------------------------------
/src/interpreter/scope.rs:
--------------------------------------------------------------------------------
1 | use super::ident::Identifier;
2 |
3 | use std::collections::{HashMap};
4 | use vec_map::VecMap;
5 |
6 | /// Represents a single scope, translates to source code locations.
7 | pub type ScopeId = usize;
8 |
9 | /// Holds an exact location within a ScopedTable.
10 | #[derive(Clone, Debug)]
11 | pub struct ScopePos {
12 | pub scope: Vec,
13 | pub scope_lengths: Vec,
14 | }
15 |
16 | #[derive(Clone, Debug)]
17 | pub struct Symbol where T: Clone {
18 | pub scope: ScopePos,
19 | pub val: T,
20 | }
21 |
22 | pub struct ScopedTable where T: Clone {
23 | symbols: HashMap>>,
24 | scope: Vec,
25 | scope_lengths: Vec,
26 | }
27 |
28 | impl ScopedTable where T: Clone {
29 | pub fn new() -> ScopedTable {
30 | let mut symbols = HashMap::new();
31 | symbols.insert(0, VecMap::new());
32 | ScopedTable {
33 | symbols: symbols,
34 | scope: vec![0],
35 | scope_lengths: vec![0],
36 | }
37 | }
38 |
39 | /// Push a list of scopes which will all be popped off at once when pop() is called.
40 | pub fn push_scope(&mut self, scopes: &[ScopeId]) {
41 | self.scope.push_all(scopes);
42 | self.scope_lengths.push(scopes.len());
43 | }
44 |
45 | /// Push a single scope, which will be popped off when pop() is called.
46 | pub fn push(&mut self, scope: ScopeId) {
47 | self.scope.push(scope);
48 | self.scope_lengths.push(1);
49 | self.symbols.entry(scope).or_insert(VecMap::new());
50 | }
51 |
52 | /// Push a single scope onto the end of the current group.
53 | /// It will be popped off along with the rest of the group when pop() is called.
54 | pub fn push_to_scope(&mut self, scope: ScopeId) {
55 | self.scope.push(scope);
56 | *self.scope_lengths.last_mut().unwrap() += 1;
57 | self.symbols.entry(scope).or_insert(VecMap::new());
58 | }
59 |
60 | /// Pops the topmost group of scopes from the stack.
61 | pub fn pop(&mut self) {
62 | assert!(self.scope_lengths.len() > 1, "tried to leave the outermost scope!");
63 | for _ in 0..self.scope_lengths.pop().unwrap() {
64 | self.scope.pop().unwrap();
65 | }
66 | }
67 |
68 | /// Returns the current stack of scopes.
69 | pub fn get_scope_pos(&self) -> ScopePos {
70 | ScopePos {
71 | scope: self.scope.clone(),
72 | scope_lengths: self.scope_lengths.clone(),
73 | }
74 | }
75 |
76 | /// Sets a source identifer with the given scope identifier to a value.
77 | pub fn set_val(&mut self, id: Identifier, scope: ScopeId, val: T) {
78 | self.push_to_scope(scope);
79 | let scope_pos = self.get_scope_pos();
80 | let id_map = self.symbols.get_mut(&scope).unwrap();
81 | id_map.insert(id, Symbol {
82 | scope: scope_pos,
83 | val: val,
84 | });
85 | }
86 |
87 | /// Searches backwards through the scope stack until a symbol with the given identifier is
88 | /// found, and returns the symbol.
89 | pub fn get_symbol(&self, id: Identifier) -> Option<&Symbol> {
90 | for block_pos in self.scope.iter().rev() {
91 | let id_map = self.symbols.get(&block_pos).unwrap();
92 | if let Some(symbol) = id_map.get(&id) {
93 | return Some(symbol)
94 | }
95 | }
96 | None
97 | }
98 |
99 | /// Searches backwards through the scope stack until a symbol with the given identifier is
100 | /// found, and returns the depth down the stack it was found at.
101 | pub fn get_symbol_depth(&self, id: Identifier) -> Option {
102 | self.get_symbol_height(id).map(|x| self.scope_lengths.len() - x - 1)
103 | }
104 |
105 | /// Searches backwards through the scope stack until a symbol with the given identifier is
106 | /// found, and returns the height up the stack it was found at.
107 | pub fn get_symbol_height(&self, id: Identifier) -> Option {
108 | let mut len_iter = self.scope_lengths.iter().rev();
109 | let mut len = match len_iter.next() {
110 | Some(x) => *x,
111 | None => return None
112 | };
113 | let mut count = len_iter.len();
114 | for block_pos in self.scope.iter().rev() {
115 | len -= 1;
116 | if len == 0 {
117 | len = match len_iter.next() {
118 | Some(x) => *x,
119 | None => return None
120 | };
121 | count -= 1;
122 | }
123 | let id_map = self.symbols.get(&block_pos).unwrap();
124 | if let Some(_) = id_map.get(&id) {
125 | return Some(count)
126 | }
127 | }
128 | None
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/src/interpreter/tests.rs:
--------------------------------------------------------------------------------
1 | /// should_pass: expects no errors.
2 | /// should_fail: expects errors.
3 | /// should_warn: expects warnings.
4 | #[macro_export]
5 | macro_rules! run_test {
6 | ( $( $prop:ident ( $( $val:ident ),* ) ),*
7 | => $source:expr ) => {{
8 | use ::interpreter::common::Context;
9 | use ::interpreter::compiler::Compiler;
10 |
11 | use std::collections::HashSet;
12 |
13 | let mut should_pass: HashSet<&'static str> = HashSet::new();
14 | let mut should_fail: HashSet<&'static str> = HashSet::new();
15 | let mut should_warn: HashSet<&'static str> = HashSet::new();
16 | let mut should_run: HashSet<&'static str> = HashSet::new();
17 | $(
18 | $(
19 | let val = stringify!($val);
20 | should_run.insert(val);
21 | match stringify!($prop) {
22 | "should_pass" => { should_pass.insert(val); },
23 | "should_fail" => { should_fail.insert(val); },
24 | "should_warn" => { should_warn.insert(val); },
25 | _ => panic!("unknown property"),
26 | }
27 | )*
28 | )*
29 | let ctxt = Context::new("".into(), $source.into());
30 | let mut compiler = Compiler::new(&ctxt);
31 | compiler.define_intrinsics();
32 |
33 | if should_run.contains(&"lex") {
34 | compiler.lex();
35 | let err = ctxt.issues.borrow().has_errors();
36 | let warn = ctxt.issues.borrow().has_warnings();
37 | if should_pass.contains(&"lex") && err {
38 | panic!("lex should have passed:\n{}", *ctxt.issues.borrow());
39 | }
40 | if should_fail.contains(&"lex") && !err {
41 | panic!("lex should have produced errors:\n{}", *ctxt.issues.borrow());
42 | }
43 | if should_warn.contains(&"lex") && !warn {
44 | panic!("lex should have produced warnings:\n{}", *ctxt.issues.borrow());
45 | }
46 | ctxt.issues.borrow_mut().clear();
47 | }
48 |
49 | if should_run.contains(&"parse") {
50 | compiler.parse();
51 | let err = ctxt.issues.borrow().has_errors();
52 | let warn = ctxt.issues.borrow().has_warnings();
53 | if should_pass.contains(&"parse") && err {
54 | panic!("parse should have passed:\n{}", *ctxt.issues.borrow());
55 | }
56 | if should_fail.contains(&"parse") && !err {
57 | panic!("parse should have produced errors:\n{}", *ctxt.issues.borrow());
58 | }
59 | if should_warn.contains(&"parse") && !warn {
60 | panic!("parse should have produced warnings:\n{}", *ctxt.issues.borrow());
61 | }
62 | ctxt.issues.borrow_mut().clear();
63 | }
64 |
65 | if should_run.contains(&"typecheck") {
66 | compiler.typecheck();
67 | let err = ctxt.issues.borrow().has_errors();
68 | let warn = ctxt.issues.borrow().has_warnings();
69 | if should_pass.contains(&"typecheck") && err {
70 | panic!("typecheck should have passed:\n{}", *ctxt.issues.borrow());
71 | }
72 | if should_fail.contains(&"typecheck") && !err {
73 | panic!("typecheck should have produced errors:\n{}", *ctxt.issues.borrow());
74 | }
75 | if should_warn.contains(&"typecheck") && !warn {
76 | panic!("typecheck should have produced warnings:\n{}", *ctxt.issues.borrow());
77 | }
78 | ctxt.issues.borrow_mut().clear();
79 | }
80 |
81 | if should_run.contains(&"codegen") {
82 | compiler.codegen();
83 | let err = ctxt.issues.borrow().has_errors();
84 | let warn = ctxt.issues.borrow().has_warnings();
85 | if should_pass.contains(&"codegen") && err {
86 | panic!("codegen should have passed:\n{}", *ctxt.issues.borrow());
87 | }
88 | if should_fail.contains(&"codegen") && !err {
89 | panic!("codegen should have produced errors:\n{}", *ctxt.issues.borrow());
90 | }
91 | if should_warn.contains(&"codegen") && !warn {
92 | panic!("codegen should have produced warnings:\n{}", *ctxt.issues.borrow());
93 | }
94 | }
95 | }};
96 | }
97 |
--------------------------------------------------------------------------------
/src/interpreter/tokens.rs:
--------------------------------------------------------------------------------
1 | use super::ident::Identifier;
2 |
3 | use std::fmt;
4 | use std::ops::Deref;
5 |
6 | pub type Number = f64;
7 | pub type Boolean = bool;
8 |
9 | /// The various types that a token can be
10 | #[derive(Debug, Copy, PartialEq, Clone)]
11 | pub enum Token {
12 | Ident(Identifier),
13 | Const(Number),
14 | Boolean(Boolean),
15 | Operator(Operator),
16 | Symbol(Symbol),
17 | }
18 |
19 | #[derive(Debug, Copy, PartialEq, Clone)]
20 | pub enum Operator {
21 | Add,
22 | Sub,
23 | Mul,
24 | Div,
25 | Exp,
26 | Mod,
27 | Less,
28 | Greater,
29 | Equal,
30 | NotEqual,
31 | ApproxEqual,
32 | Not,
33 | And,
34 | Or,
35 | Xor,
36 | GreaterEqual,
37 | LessEqual,
38 | }
39 |
40 | #[derive(PartialEq)]
41 | pub enum Associativity {
42 | Left,
43 | Right,
44 | }
45 |
46 | impl Operator {
47 | pub fn parse(s: &str) -> Option {
48 | use self::Operator::*;
49 | Some(match s {
50 | "+" => Add,
51 | "-" => Sub,
52 | "*" => Mul,
53 | "/" => Div,
54 | "^" => Exp,
55 | "%" => Mod,
56 | "==" => Equal,
57 | "!=" => NotEqual,
58 | "~=" => ApproxEqual,
59 | "<" => Less,
60 | ">" => Greater,
61 | "<=" => LessEqual,
62 | ">=" => GreaterEqual,
63 | "!" => Not,
64 | "&&" => And,
65 | "||" => Or,
66 | "^^" => Xor,
67 | _ => return None,
68 | })
69 | }
70 | pub fn precedence(&self) -> i32 {
71 | use self::Operator::*;
72 | match *self {
73 | And | Or | Xor => 10,
74 | Equal | NotEqual | ApproxEqual => 20,
75 | Less | Greater | GreaterEqual | LessEqual => 30,
76 | Add | Sub => 40,
77 | Mul | Div | Mod => 50,
78 | Not | Exp => 60,
79 | }
80 | }
81 |
82 | pub fn can_take_x_args(&self, x: i32) -> bool {
83 | use self::Operator::*;
84 | match *self {
85 | Not => x == 1,
86 | Sub => x == 1 || x == 2,
87 | _ => x == 2,
88 | }
89 | }
90 |
91 | pub fn associativity(&self) -> Associativity {
92 | use self::Operator::*;
93 | match *self {
94 | Exp => Associativity::Right,
95 | _ => Associativity::Left,
96 | }
97 | }
98 | }
99 |
100 | #[derive(Debug, Copy, PartialEq, Clone)]
101 | pub enum Symbol {
102 | Period,
103 | Comma,
104 | Equals,
105 | Colon,
106 | Semicolon,
107 | QuestionMark,
108 | Backslash,
109 | If,
110 | Else,
111 | At,
112 | LeftBracket(Bracket),
113 | RightBracket(Bracket),
114 | }
115 |
116 | impl Symbol {
117 | pub fn parse(s: &str) -> Option {
118 | use self::Symbol::*;
119 | Some(match s {
120 | "." => Period,
121 | "," => Comma,
122 | "=" => Equals,
123 | ":" => Colon,
124 | ";" => Semicolon,
125 | "?" => QuestionMark,
126 | "\\" => Backslash,
127 | "if" => If,
128 | "else" => Else,
129 | "@" => At,
130 | "(" => LeftBracket(Bracket::Round),
131 | ")" => RightBracket(Bracket::Round),
132 | "{" => LeftBracket(Bracket::Curly),
133 | "}" => RightBracket(Bracket::Curly),
134 | "[" => LeftBracket(Bracket::Square),
135 | "]" => RightBracket(Bracket::Square),
136 | _ => return None,
137 | })
138 | }
139 | }
140 |
141 | impl fmt::Display for Symbol {
142 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
143 | use self::Symbol::*;
144 | let string = match *self {
145 | Period => ".",
146 | Comma => ",",
147 | Equals => "=",
148 | Colon => ":",
149 | Semicolon => ";",
150 | QuestionMark => "?",
151 | Backslash => "\\",
152 | If => "if",
153 | Else => "else",
154 | At => "@",
155 | LeftBracket(Bracket::Round) => "(",
156 | RightBracket(Bracket::Round) => ")",
157 | LeftBracket(Bracket::Curly) => "{",
158 | RightBracket(Bracket::Curly) => "}",
159 | LeftBracket(Bracket::Square) => "[",
160 | RightBracket(Bracket::Square) => "]",
161 | };
162 | write!(f, "{}", string)
163 | }
164 | }
165 |
166 | impl fmt::Display for Token {
167 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
168 | use self::Token::*;
169 | match *self {
170 | Ident(x) => write!(f, "Id({})", x),
171 | Operator(x) => write!(f, "{:?}", x),
172 | Const(x) => write!(f, "{}", x),
173 | Symbol(x) => write!(f, "{}", x),
174 | Boolean(x) => write!(f, "{}", x),
175 | }
176 | }
177 | }
178 |
179 | #[derive(Debug, Copy, PartialEq, Clone)]
180 | pub enum Bracket {
181 | Round,
182 | Square,
183 | Curly,
184 | }
185 |
186 | #[derive(Copy, Clone, PartialEq)]
187 | pub struct SourcePos {
188 | pub line: isize,
189 | pub column: usize,
190 | pub index: usize,
191 | pub line_index: usize, //index of first character of line
192 | }
193 |
194 | static mut anon_count: isize = 0;
195 |
196 | impl SourcePos {
197 | pub fn new() -> SourcePos {
198 | SourcePos {
199 | line: 1,
200 | column: 1,
201 | index: 0,
202 | line_index: 0,
203 | }
204 | }
205 | pub fn anon() -> SourcePos {
206 | unsafe { anon_count -= 1; }
207 | SourcePos {
208 | line: unsafe { anon_count },
209 | column: 0,
210 | index: 0,
211 | line_index: 0,
212 | }
213 | }
214 | /// Requires col to have reached the end of line for indices to be properly incremented.
215 | pub fn add_line(&mut self) {
216 | self.line += 1;
217 | self.column = 1;
218 | self.index += 1; // newline
219 | self.line_index = self.index;
220 | }
221 |
222 | pub fn add_chars(&mut self, num: usize) {
223 | self.column += num;
224 | self.index += num;
225 | }
226 |
227 | pub fn is_anon(&self) -> bool {
228 | self.line < 0
229 | }
230 | }
231 |
232 | impl fmt::Display for SourcePos {
233 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234 | if self.is_anon() {
235 | write!(f, "")
236 | } else {
237 | write!(f, "{}:{}", self.line, self.column)
238 | }
239 | }
240 | }
241 |
242 | impl fmt::Debug for SourcePos {
243 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
244 | if self.is_anon() {
245 | write!(f, "")
246 | } else {
247 | write!(f, "{}:{}/idx={}", self.line, self.column, self.index)
248 | }
249 | }
250 | }
251 |
252 | pub struct Node(pub T, pub SourcePos);
253 |
254 | pub trait NodeImpl<'a> {
255 | type Item;
256 | type Pos;
257 | fn item(&'a self) -> ::Item;
258 | fn pos(&'a self) -> ::Pos;
259 | }
260 |
261 | impl<'a, T: 'a> NodeImpl<'a> for Node {
262 | type Item = &'a T;
263 | type Pos = SourcePos;
264 | fn item(&'a self) -> &'a T {
265 | &self.0
266 | }
267 | fn pos(&self) -> SourcePos {
268 | self.1
269 | }
270 | }
271 |
272 | impl<'a, T> NodeImpl<'a> for Option> where T: Copy {
273 | type Item = Option;
274 | type Pos = Option;
275 | fn item(&self) -> Option {
276 | self.map(|x| x.0)
277 | }
278 | fn pos(&self) -> Option {
279 | self.map(|x| x.1)
280 | }
281 | }
282 |
283 | impl fmt::Debug for Node where T: fmt::Debug {
284 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
285 | f.debug_tuple(&format!("Node+{}", self.1))
286 | .field(&self.0)
287 | .finish()
288 | }
289 | }
290 |
291 | impl fmt::Display for Node where T: fmt::Display {
292 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
293 | write!(f, "{} @ {}", self.0, self.1)
294 | }
295 | }
296 |
297 | impl Copy for Node where T: Copy { }
298 |
299 | impl Clone for Node where T: Clone {
300 | fn clone(&self) -> Node {
301 | Node(self.0.clone(), self.1)
302 | }
303 | }
304 |
305 | impl Deref for Node {
306 | type Target = T;
307 |
308 | fn deref<'a>(&'a self) -> &'a T {
309 | &self.0
310 | }
311 | }
312 |
313 |
--------------------------------------------------------------------------------
/src/interpreter/typecheck.rs:
--------------------------------------------------------------------------------
1 | use super::ast::*;
2 | use super::types::*;
3 | use super::tokens::{Operator, Node, NodeImpl};
4 | use super::common::Context;
5 | use super::ident::Identifier;
6 | use super::functions;
7 |
8 | use std::cell::RefMut;
9 | use vec_map::VecMap;
10 |
11 | pub fn typecheck<'a>(ctxt: &'a Context<'a>) {
12 | let mut t = TypeChecker::new(ctxt);
13 | t.check();
14 | }
15 |
16 | pub struct TypeChecker<'a> {
17 | types: RefMut<'a, TypeTable>,
18 | ctxt: &'a Context<'a>,
19 | }
20 |
21 | impl<'a> TypeChecker<'a> {
22 | pub fn new(ctxt: &'a Context<'a>) -> TypeChecker<'a> {
23 | TypeChecker {
24 | ctxt: ctxt,
25 | types: ctxt.types.borrow_mut(),
26 | }
27 | }
28 |
29 | pub fn check(&mut self) {
30 | self.check_root(&mut *self.ctxt.ast.borrow_mut());
31 | }
32 |
33 | fn check_root(&mut self, root: &mut Root) {
34 | for item in root.iter() {
35 | match *item {
36 | Item::FunctionDef(ref f) => {
37 | self.typeof_function_def(&f);
38 | }
39 | Item::Assignment(ref a) => {
40 | self.typeof_assignment(&a);
41 | }
42 | };
43 | }
44 | root.retain(|item|
45 | match *item {
46 | Item::FunctionDef(ref f) => {
47 | if !self.ctxt.functions.borrow().get(f.ident()).unwrap().has_concrete_type() {
48 | self.ctxt.emit_warning("function is never used", f.pos());
49 | false
50 | } else {
51 | true
52 | }
53 | },
54 | _ => true
55 | }
56 | );
57 | }
58 |
59 | pub fn typeof_assignment(&mut self, assign: &Node) -> Option {
60 | let ty = self.typeof_expr(&assign.expr());
61 | match ty {
62 | None =>
63 | self.ctxt.emit_error("could not determine type of assignment", assign.pos()),
64 | Some(ty) => {
65 | if let Some(old_sym) = self.types.get_symbol(assign.ident()) {
66 | if old_sym.val != ty && Some(0) == self.types.get_symbol_depth(assign.ident()) {
67 | self.ctxt.emit_warning(format!("variable was previously assigned type `{}`",
68 | old_sym.val),
69 | assign.pos());
70 | }
71 | }
72 | if let Type::Indeterminate = ty {
73 | self.ctxt.emit_error("expression references a function with ambiguous type",
74 | assign.expr_pos());
75 | // TODO should this set ty=None?
76 | }
77 | self.types.set_val(assign.ident(), assign.pos().index, ty);
78 | }
79 | }
80 | ty
81 | }
82 |
83 | pub fn typeof_function_def(&mut self, def: &Node) -> Option {
84 | if let Some(0) = self.types.get_symbol_depth(def.ident()) {
85 | self.ctxt.emit_warning("function declaration shadows previous declaration of same name", def.pos());
86 | }
87 | let ty = Type::Function(def.ident());
88 | self.types.set_val(def.ident(), 0, ty);
89 | if let Some(fn_ty) = self.ctxt.entrypoints.borrow().get(&def.ident()) {
90 | match self.typeof_predeclared_function(def, fn_ty) {
91 | None => return None,
92 | Some(_) => { }
93 | }
94 | }
95 | Some(ty)
96 | }
97 |
98 | pub fn typeof_expr(&mut self, expr: &Expression) -> Option {
99 | match *expr {
100 | Expression::Constant(_) => Some(Type::Number),
101 | Expression::Boolean(_) => Some(Type::Boolean),
102 | Expression::Variable(ref id) => self.typeof_var(id),
103 | Expression::Infix(ref v) => self.typeof_infix(v),
104 | Expression::Prefix(ref v) => self.typeof_prefix(v),
105 | Expression::Conditional(ref c) => self.typeof_conditional(c),
106 | Expression::Block(ref b) => self.typeof_block(b),
107 | Expression::FunctionCall(ref c) => self.typeof_function_call(c),
108 | Expression::Closure(ref c) => self.typeof_function_def(c),
109 | }
110 | }
111 |
112 | pub fn typeof_predeclared_function(&mut self, def: &Node, fn_ty: &FunctionType) -> Option {
113 | // check to make sure the identifiers of the args match, and that there's no defaults
114 | // set the types of all the args, and check the type of the block.
115 | let name = self.ctxt.lookup_name(def.ident());
116 | let mut args_match = true;
117 | for (id, pos) in def.args().iter().map(|x| (x.ident().unwrap(), Some(x.pos())))
118 | .chain(fn_ty.args.keys().map(|x| (x, None))) {
119 | let arg_name = self.ctxt.lookup_name(id);
120 | if !def.args().iter().any(|x| x.ident().unwrap() == id) {
121 | self.ctxt.emit_error(format!("expected argument `{}` for entrypoint `{}`", arg_name, name),
122 | def.args_pos());
123 | args_match = false;
124 | }
125 | if !fn_ty.args.contains_key(&id) {
126 | self.ctxt.emit_error(format!("unexpected argument `{}` for entrypoint `{}`", arg_name, name),
127 | pos.unwrap());
128 | args_match = false;
129 | }
130 | }
131 | if !args_match {
132 | return None;
133 | }
134 | let type_def = self.types.get_symbol(def.ident()).unwrap().clone();
135 | self.types.push_scope(&type_def.scope.scope);
136 | for ((id, ty), ref arg) in fn_ty.args.iter().zip(def.args().iter()) {
137 | self.types.set_val(id, arg.pos().index, *ty);
138 | if let Type::Function(func_id) = *ty {
139 | self.types.set_val(func_id, arg.pos().index, Type::Function(func_id));
140 | }
141 | }
142 | let ty = self.typeof_block(&def.block);
143 | self.types.pop();
144 | let return_ty = match ty {
145 | Some(ty) => Some(ty),
146 | None => {
147 | self.ctxt.emit_error("could not determine return type of function", def.pos());
148 | return None;
149 | }
150 | };
151 | self.ctxt.functions.borrow_mut().get_mut(def.ident()).unwrap().set_ty(fn_ty.clone());
152 | return_ty
153 | }
154 |
155 | pub fn typeof_function_call(&mut self, call: &Node) -> Option {
156 | let func_id = match self.typeof_expr(call.callee()) {
157 | Some(Type::Function(f)) => f,
158 |
159 | Some(ref ty) => {
160 | self.ctxt.emit_error(format!("expected function, got type `{}`", ty), call.callee_pos());
161 | return None;
162 | },
163 |
164 | None => {
165 | self.ctxt.emit_error("could not determine type of function", call.callee_pos());
166 | return None;
167 | },
168 | };
169 | let func_def = self.types.get_symbol(func_id).unwrap().clone();
170 | let func = self.ctxt.functions.borrow().get(func_id).unwrap().clone();
171 |
172 | let mut def_args = Vec::new();
173 |
174 | let mut undef_args = func.args().clone();
175 | // check that the args in the call match the args in the def
176 | for arg in call.args() {
177 | if let Some(id) = arg.ident() {
178 | match undef_args.iter().position(|x| x.ident().unwrap() == id) {
179 | Some(pos) => {
180 | def_args.push((arg.clone(), false));
181 | undef_args.remove(pos);
182 | }
183 | None => {
184 | self.ctxt.emit_error(format!("unexpected argument `{}`",
185 | self.ctxt.lookup_name(id)), arg.pos());
186 | return None;
187 | }
188 | }
189 | } else {
190 | if undef_args.is_empty() {
191 | self.ctxt.emit_error("unexpected argument", arg.pos());
192 | return None;
193 | }
194 | let expr = match *arg {
195 | Argument::Expr(ref expr) => { expr.clone() },
196 | _ => unreachable!(),
197 | };
198 | let id = undef_args.remove(0).ident().unwrap();
199 | def_args.push((Argument::Assign(Node(id, expr.pos()), expr), false));
200 | }
201 | }
202 | // set default args that weren't set previously
203 | undef_args.retain(|arg|
204 | match *arg {
205 | Argument::Assign(_, _) => {
206 | def_args.push((arg.clone(), true));
207 | false
208 | }
209 | _ => true
210 | }
211 | );
212 |
213 | // if any arguments were not defined above, die
214 | for unassigned in undef_args.iter() {
215 | self.ctxt.emit_error(format!("argument `{}` is required",
216 | self.ctxt.lookup_name(unassigned.ident().unwrap())),
217 | call.args_pos());
218 | }
219 | if undef_args.len() > 0 {
220 | return None
221 | }
222 |
223 | let mut arg_types = VecMap::new();
224 |
225 | let recursive = self.ctxt.callstack.borrow().is_recursive(func_id);
226 | if recursive {
227 | return Some(Type::Indeterminate);
228 | }
229 |
230 | // determine the type of the arguments
231 | for &(ref arg, is_default) in &def_args {
232 | if is_default {
233 | self.types.push_scope(&func_def.scope.scope);
234 | }
235 | match *arg {
236 | Argument::OpAssign(id, op, ref expr) => {
237 | let ty = self.typeof_expr(&Expression::Infix(Box::new(Node(Infix {
238 | op: op,
239 | left: Expression::Variable(id),
240 | right: expr.clone(),
241 | }, arg.pos()))));
242 | match ty {
243 | None => return None,
244 | Some(ty) => {
245 | arg_types.insert(*id, ty);
246 | }
247 | }
248 | }
249 | Argument::Assign(id, ref expr) => {
250 | let ty = self.typeof_expr(expr);
251 | match ty {
252 | None => return None,
253 | Some(ty) => {
254 | arg_types.insert(*id, ty);
255 | }
256 | }
257 | }
258 | Argument::Ident(id) => {
259 | let ty = self.typeof_var(&id);
260 | match ty {
261 | None => return None,
262 | Some(ty) => {
263 | arg_types.insert(*id, ty);
264 | }
265 | }
266 | }
267 | _ => unreachable!(),
268 | }
269 | if is_default {
270 | self.types.pop();
271 | }
272 | }
273 |
274 | self.ctxt.callstack.borrow_mut().push(func_id);
275 |
276 | let return_ty = match func {
277 | functions::Function::User(ref def) => {
278 | self.types.push_scope(&func_def.scope.scope);
279 | for ((id, ty), &(ref arg, _)) in arg_types.iter().zip(def_args.iter()) {
280 | self.types.set_val(id, arg.pos().index, *ty);
281 | if let Type::Function(func_id) = *ty {
282 | self.types.set_val(func_id, arg.pos().index, Type::Function(func_id));
283 | }
284 | }
285 | let ty = self.typeof_block(&def.block);
286 | self.types.pop();
287 | match ty {
288 | Some(ty) => ty,
289 | None => {
290 | self.ctxt.emit_error("could not determine return type of function", def.pos());
291 | return None;
292 | }
293 | }
294 | }
295 |
296 | functions::Function::Pointer(ref def) => { def.ty.returns }
297 | functions::Function::External(ref def) => { def.ty.returns }
298 | };
299 |
300 | self.ctxt.callstack.borrow_mut().pop();
301 |
302 | let calcd_type = FunctionType::new(arg_types, return_ty);
303 | if let Some(def_type) = func.ty() {
304 | let mut types_match = true;
305 | for ((old, new), &(ref arg, _)) in def_type.args.iter().zip(calcd_type.args.iter()).zip(def_args.iter()) {
306 | if let ((_, &Type::Function(_)), (_, &Type::Function(_))) = (new, old) {
307 | // If they are both functions, do nothing.
308 | // Their compatibility will already have been validated
309 | } else if old != new {
310 | self.ctxt.emit_error(format!("expected type `{}` for argument `{}`, got `{}`",
311 | old.1,
312 | self.ctxt.lookup_name(arg.ident().unwrap()),
313 | new.1),
314 | arg.pos());
315 | types_match = false;
316 | }
317 | }
318 | if !types_match {
319 | return None;
320 | }
321 | }
322 | self.ctxt.functions.borrow_mut().get_mut(func_id).unwrap().set_ty(calcd_type);
323 | Some(return_ty)
324 | }
325 |
326 | pub fn typeof_block(&mut self, block: &Node) -> Option {
327 | self.types.push(block.pos().index);
328 | let count = block.iter().filter(|&x| {
329 | match x {
330 | &Statement::Expression(..) => true,
331 | _ => false,
332 | }
333 | }).count();
334 | let mut ty = None;
335 | for stmnt in block.item() {
336 | match stmnt {
337 | &Statement::Assignment(ref a) => {
338 | if self.typeof_assignment(a).is_none() {
339 | self.ctxt.emit_error("could not determine type of block assignment", a.pos());
340 | return None
341 | }
342 | }
343 | &Statement::Expression(ref e) => {
344 | ty = self.typeof_expr(e);
345 | match ty {
346 | Some(Type::Number) => { },
347 | Some(Type::Indeterminate) => {
348 | if count > 1 {
349 | ty = Some(Type::Number);
350 | } else {
351 | self.ctxt.emit_error("type of expression is ambiguous", e.pos());
352 | ty = None;
353 | }
354 | }
355 | Some(_) => {
356 | if count > 1 {
357 | self.ctxt.emit_error(format!("expected type `Number`, got `{}`", ty.unwrap()), e.pos());
358 | ty = None;
359 | }
360 | }
361 | None => {
362 | self.ctxt.emit_error("could not determine type of statement", e.pos());
363 | }
364 |
365 | }
366 | }
367 | }
368 | }
369 | self.types.pop();
370 | ty
371 | }
372 |
373 | pub fn typeof_conditional(&mut self, cond: &Node) -> Option {
374 | match self.typeof_expr(&cond.cond()) {
375 | Some(x) => {
376 | if x != Type::Boolean {
377 | self.ctxt.emit_error(format!("expected type `Boolean`, got `{}`", x), cond.cond_pos());
378 | return None;
379 | }
380 | },
381 | None => {
382 | self.ctxt.emit_error("type of condition could not be determined", cond.cond_pos());
383 | return None;
384 | }
385 | }
386 | let then_ty = match self.typeof_expr(cond.then()) {
387 | Some(x) => x,
388 | None => {
389 | self.ctxt.emit_error("type of conditional then expression could not be determined", cond.then_pos());
390 | return None;
391 | }
392 | };
393 | let else_ty = match self.typeof_expr(cond.els()) {
394 | Some(x) => x,
395 | None => {
396 | self.ctxt.emit_error("type of conditional else expression could not be determined", cond.els_pos());
397 | return None;
398 | }
399 | };
400 | if else_ty == Type::Indeterminate && then_ty != Type::Indeterminate {
401 | return Some(then_ty);
402 | }
403 | if then_ty == Type::Indeterminate && else_ty != Type::Indeterminate {
404 | return Some(else_ty);
405 | }
406 | if let (Type::Function(_), Type::Function(_)) = (then_ty, else_ty) {
407 | return Some(then_ty);
408 | //unimplemented!();
409 | }
410 | if else_ty != then_ty {
411 | self.ctxt.emit_error(format!(
412 | "then branch of conditional is of type `{}` but else branch is of type `{}`",
413 | then_ty, else_ty), cond.els_pos());
414 | return None;
415 | }
416 |
417 | Some(then_ty)
418 | }
419 |
420 | pub fn typeof_var(&mut self, ident: &Node) -> Option {
421 | match self.types.get_symbol(*ident.item()) {
422 | Some(s) => {
423 | Some(s.val)
424 | }
425 | None => {
426 | self.ctxt.emit_error(format!("no variable named `{}` is in scope",
427 | self.ctxt.lookup_name(*ident.item())),
428 | ident.pos());
429 | None
430 | }
431 | }
432 | }
433 |
434 | pub fn typeof_infix(&mut self, infix: &Node) -> Option {
435 | let lhs_ty = match self.typeof_expr(infix.left()) {
436 | Some(x) => x,
437 | None => {
438 | self.ctxt.emit_error("type of infix lhs could not be determined",
439 | infix.left_pos());
440 | return None;
441 | }
442 | };
443 | let rhs_ty = match self.typeof_expr(infix.right()) {
444 | Some(x) => x,
445 | None => {
446 | self.ctxt.emit_error("type of infix rhs could not be determined",
447 | infix.right_pos());
448 | return None;
449 | }
450 | };
451 | match infix.op() {
452 | Operator::Add |
453 | Operator::Sub |
454 | Operator::Mul |
455 | Operator::Div |
456 | Operator::Exp |
457 | Operator::Mod => {
458 | if lhs_ty == rhs_ty && (lhs_ty == Type::Number || lhs_ty == Type::Indeterminate) {
459 | Some(lhs_ty)
460 | } else if lhs_ty == Type::Number && rhs_ty == Type::Indeterminate ||
461 | rhs_ty == Type::Number && lhs_ty == Type::Indeterminate {
462 | Some(Type::Number)
463 | } else {
464 | self.ctxt.emit_error(format!(
465 | "cannot apply numerical operator to types `{}` and `{}`",
466 | lhs_ty, rhs_ty), infix.op_pos());
467 | None
468 | }
469 | }
470 | Operator::Less |
471 | Operator::ApproxEqual |
472 | Operator::GreaterEqual |
473 | Operator::LessEqual |
474 | Operator::Greater => {
475 | if lhs_ty == rhs_ty && lhs_ty == Type::Number {
476 | Some(Type::Boolean)
477 | } else if lhs_ty == rhs_ty && lhs_ty == Type::Indeterminate {
478 | Some(Type::Indeterminate)
479 | } else if lhs_ty == Type::Number && rhs_ty == Type::Indeterminate ||
480 | rhs_ty == Type::Number && lhs_ty == Type::Indeterminate {
481 | Some(Type::Boolean)
482 | } else {
483 | self.ctxt.emit_error(format!(
484 | "cannot apply comparison operator to types `{}` and `{}`",
485 | lhs_ty, rhs_ty), infix.op_pos());
486 | None
487 | }
488 | }
489 | Operator::Equal |
490 | Operator::NotEqual => {
491 | if lhs_ty == rhs_ty &&
492 | (lhs_ty == Type::Number || lhs_ty == Type::Boolean) {
493 | Some(Type::Boolean)
494 | } else if lhs_ty == rhs_ty && lhs_ty == Type::Indeterminate {
495 | Some(Type::Indeterminate)
496 | } else if (lhs_ty == Type::Number || lhs_ty == Type::Boolean)
497 | && rhs_ty == Type::Indeterminate ||
498 | (rhs_ty == Type::Number || rhs_ty == Type::Boolean)
499 | && lhs_ty == Type::Indeterminate {
500 | Some(Type::Boolean)
501 | } else {
502 | self.ctxt.emit_error(format!(
503 | "cannot apply equality operator to types `{}` and `{}`",
504 | lhs_ty, rhs_ty), infix.op_pos());
505 | None
506 | }
507 | }
508 | Operator::And |
509 | Operator::Or |
510 | Operator::Xor => {
511 | if lhs_ty == rhs_ty && (lhs_ty == Type::Boolean || lhs_ty == Type::Indeterminate) {
512 | Some(lhs_ty)
513 | } else if lhs_ty == Type::Boolean && rhs_ty == Type::Indeterminate ||
514 | rhs_ty == Type::Boolean && lhs_ty == Type::Indeterminate {
515 | Some(Type::Boolean)
516 | } else {
517 | self.ctxt.emit_error(format!(
518 | "cannot apply logical operator to types `{}` and `{}`",
519 | lhs_ty, rhs_ty), infix.op_pos());
520 | None
521 | }
522 | }
523 | _ => {
524 | unreachable!();
525 | }
526 | }
527 | }
528 |
529 | pub fn typeof_prefix(&mut self, prefix: &Node) -> Option {
530 | let expr_ty = match self.typeof_expr(prefix.expr()) {
531 | Some(x) => x,
532 | None => {
533 | self.ctxt.emit_error("type of prefix expression could not be determined", prefix.expr_pos());
534 | return None;
535 | }
536 | };
537 | match prefix.op() {
538 | Operator::Sub => {
539 | if expr_ty == Type::Number || expr_ty == Type::Indeterminate {
540 | Some(Type::Number)
541 | } else {
542 | self.ctxt.emit_error(format!("expected `Number`, got `{}`", expr_ty),
543 | prefix.expr_pos());
544 | None
545 | }
546 | }
547 | Operator::Not => {
548 | if expr_ty == Type::Boolean || expr_ty == Type::Indeterminate {
549 | Some(Type::Boolean)
550 | } else {
551 | self.ctxt.emit_error(format!("expected `Boolean`, got `{}`", expr_ty),
552 | prefix.expr_pos());
553 | None
554 | }
555 | }
556 | _ => {
557 | unreachable!();
558 | }
559 | }
560 | }
561 | }
562 |
--------------------------------------------------------------------------------
/src/interpreter/types.rs:
--------------------------------------------------------------------------------
1 | use super::ident::Identifier;
2 | use super::scope::ScopedTable;
3 |
4 | use vec_map::VecMap;
5 | use std::fmt;
6 |
7 | #[derive(Copy, Clone, Debug, PartialEq)]
8 | pub enum Type {
9 | Number,
10 | Boolean,
11 | Function(Identifier),
12 |
13 | /// With recursive functions, it may not be possible to tell exactly what the type is without
14 | /// further information. This is different from a None type, which means that this is a
15 | /// logically inconsistent or indeterminable type. The user should never see this.
16 | Indeterminate,
17 | }
18 |
19 | impl fmt::Display for Type {
20 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 | match *self {
22 | Type::Number => write!(f, "Number"),
23 | Type::Boolean => write!(f, "Boolean"),
24 | Type::Function(_) => write!(f, "Function"),
25 | Type::Indeterminate => write!(f, "Indeterminate"),
26 | }
27 | }
28 | }
29 |
30 | #[derive(Clone, Debug)]
31 | pub struct FunctionType {
32 | pub args: VecMap, // From Identifier to Type
33 | pub returns: Type,
34 | }
35 |
36 | impl FunctionType {
37 | pub fn new(args: VecMap, returns: Type) -> FunctionType {
38 | FunctionType {
39 | args: args,
40 | returns: returns,
41 | }
42 | }
43 | }
44 |
45 | pub type TypeTable = ScopedTable;
46 |
47 | #[macro_export]
48 | macro_rules! make_fn_ty {
49 | ( $ctxt:expr, fn ( $( $name:ident : $ty:ident ),* ) -> $ret:ident ) => {{
50 | use vec_map::VecMap;
51 | use $crate::types::FunctionType;
52 | use $crate::types::Type::*;
53 | let mut arg_map = VecMap::new();
54 | $(
55 | arg_map.insert($ctxt.names.borrow_mut().new_id(stringify!($name)), $ty);
56 | )*
57 | FunctionType {
58 | returns: $ret,
59 | args: arg_map
60 | }
61 | }}
62 | }
63 |
--------------------------------------------------------------------------------
/tests/codegen.rs:
--------------------------------------------------------------------------------
1 | #[macro_use(run_test)]
2 | extern crate interpreter;
3 |
4 | #[test]
5 | fn simple_assignment() {
6 | run_test!(
7 | should_pass(lex, parse, typecheck, codegen)
8 | => r"
9 | a = 1;
10 | b = true;
11 | "
12 | );
13 | }
14 |
15 | #[test]
16 | fn constant_arithmetic() {
17 | run_test!(
18 | should_pass(lex, parse, typecheck, codegen)
19 | => r"
20 | a = (-(1+1)*(2/(3-8)))%0.3; // 0.8
21 | "
22 | );
23 | }
24 |
25 | #[test]
26 | fn comparisons() {
27 | run_test!(
28 | should_pass(lex, parse, typecheck, codegen)
29 | => r"
30 | a = 1 < 1;
31 | b = 1 <= 1;
32 | c = 1 > 1;
33 | d = 1 >= 1;
34 | e = 1 == 2;
35 | f = 1 != 2;
36 | g = true == false;
37 | h = true != false;
38 |
39 | "
40 | );
41 | }
42 |
43 | #[test]
44 | fn boolean_arithmetic() {
45 | run_test!(
46 | should_pass(lex, parse, typecheck, codegen)
47 | => r"
48 | a = true || !true;
49 | b = true && !true;
50 | c = true ^^ !true;
51 | "
52 | );
53 | }
54 |
55 | #[test]
56 | fn reassign() {
57 | run_test!(
58 | should_pass(lex, parse, typecheck, codegen)
59 | => r"
60 | a = 1;
61 | a = 2;
62 | "
63 | );
64 | }
65 |
66 | #[test]
67 | fn global_ref() {
68 | run_test!(
69 | should_pass(lex, parse, typecheck, codegen)
70 | => r"
71 | a = 1;
72 | b = a + 1;
73 | "
74 | );
75 | }
76 |
77 | #[test]
78 | fn conditional() {
79 | run_test!(
80 | should_pass(lex, parse, typecheck, codegen)
81 | => r"
82 | a = 2;
83 | b = 3;
84 | c = (a if true else a)
85 | if a + b < a * b
86 | else (a if false else b);
87 | d = true if true else true;
88 | "
89 | );
90 | }
91 |
92 | #[test]
93 | fn block() {
94 | run_test!(
95 | should_pass(lex, parse, typecheck, codegen)
96 | => r"
97 | a = { true };
98 | b = { 1; 2; 3 };
99 | c = { 1; { 2; 3 } };
100 | "
101 | );
102 | }
103 |
104 | #[test]
105 | fn block_assignment() {
106 | run_test!(
107 | should_pass(lex, parse, typecheck, codegen)
108 | => r"
109 | a = {
110 | b = true;
111 | 5 if b else 6;
112 | b = !b;
113 | 5 if b else 6;
114 | };
115 | "
116 | );
117 | }
118 |
119 | #[test]
120 | fn function_def() {
121 | run_test!(
122 | should_pass(lex, parse, typecheck, codegen)
123 | => r"
124 | a x, y, z, w {
125 | x+y*z-w;
126 | }
127 | b = a[y=2, z=3, w=4, x=1];
128 | "
129 | );
130 | }
131 |
132 | #[test]
133 | fn closure() {
134 | run_test!(
135 | should_pass(lex, parse, typecheck, codegen)
136 | => r"
137 | a = \x { x+5 };
138 | b = a;
139 | c = b[x=5];
140 | "
141 | );
142 | }
143 |
144 | #[test]
145 | fn default_args() {
146 | run_test!(
147 | should_pass(lex, parse, typecheck, codegen)
148 | => r"
149 | a x=2, y { x+y }
150 | b = a[y=5];
151 | ");
152 | run_test!(
153 | should_pass(lex, parse, typecheck, codegen)
154 | => r"
155 | a x, y=2 { x+y }
156 | b = a[x=5];
157 | c = a(5);
158 | ");
159 | }
160 |
161 | #[test]
162 | fn factorial() {
163 | run_test!(
164 | should_pass(lex, parse, typecheck, codegen)
165 | => r"
166 | fact n {
167 | n*fact[n -= 1] if n > 1 else 1;
168 | }
169 | x = fact(10);
170 | ");
171 | }
172 |
173 | #[test]
174 | fn fib() {
175 | run_test!(
176 | should_pass(lex, parse, typecheck, codegen)
177 | => r"
178 | fib n {
179 | fib[n -= 1] + fib[n -= 2] if n > 2 else 1;
180 | }
181 | x = fib(10);
182 | ");
183 | }
184 |
185 | #[test]
186 | fn closure_default_args() {
187 | run_test!(
188 | should_pass(lex, parse, typecheck, codegen)
189 | => r"
190 | foo bar=\y{y*2} { bar }
191 | a = foo()(2);
192 | ");
193 | }
194 |
195 | #[test]
196 | fn unused_function() {
197 | run_test!(
198 | should_pass(lex, parse, typecheck, codegen)
199 | => r"
200 | fn x {
201 | x*5;
202 | }
203 | ");
204 | }
205 |
206 | // not passing because the testing macro doesn't define any intrinsics
207 | #[test]
208 | fn intrinsics() {
209 | run_test!(
210 | should_pass(lex, parse, typecheck, codegen)
211 | => r"
212 | x = 1.5;
213 | y = sin(x) + cos(x) + sqrt(x) + log(x) + log2(x) + floor(x) + exp(x);
214 | ");
215 | }
216 |
217 | #[test]
218 | fn closure_capture() {
219 | run_test!(
220 | should_pass(lex, parse, typecheck, codegen)
221 | => r"
222 | x = 5;
223 | y = \{x+5};
224 | z = {
225 | x = true;
226 | y();
227 | };
228 | ");
229 | }
230 |
231 | #[test]
232 | fn return_closure_capture() {
233 | run_test!(
234 | should_pass(lex, parse, typecheck, codegen)
235 | => r"
236 | fn x {
237 | z = 5;
238 | \w=x{w+z};
239 | }
240 | y = fn(5);
241 | z = fn(6);
242 | x = y() + z(); // x == 21
243 | ");
244 | }
245 |
246 | // it's currently unclear whether this is okay or not.
247 | // if it's an easy fix it'll work soon.
248 | #[test]
249 | fn use_funtion_before_def() {
250 | run_test!(
251 | should_pass(lex, parse, typecheck, codegen)
252 | => r"
253 | fn1 {
254 | fn2()
255 | }
256 | fn2 {
257 | 42
258 | }
259 | x = fn1();
260 | ");
261 | }
262 |
263 | #[test]
264 | fn pass_function() {
265 | run_test!(
266 | should_pass(lex, parse, typecheck, codegen)
267 | => r"
268 | fiver fn {
269 | fn(5)
270 | }
271 |
272 | square x {
273 | x*x
274 | }
275 |
276 | x = fiver(square);
277 | ");
278 | }
279 |
280 | #[test]
281 | fn pass_closure() {
282 | run_test!(
283 | should_pass(lex, parse, typecheck, codegen)
284 | => r"
285 | fiver fn {
286 | fn(5)
287 | }
288 |
289 | x = fiver(\x { x*x });
290 | ");
291 | }
292 |
293 | #[test]
294 | fn return_function() {
295 | run_test!(
296 | should_pass(lex, parse, typecheck, codegen)
297 | => r"
298 | passthru x {
299 | x
300 | }
301 |
302 | squarer {
303 | \x { x*x }
304 | }
305 |
306 | square = passthru(squarer());
307 | x = square(5);
308 | y = square(6);
309 | ");
310 | }
311 |
312 | #[test]
313 | fn return_closure() {
314 | run_test!(
315 | should_pass(lex, parse, typecheck, codegen)
316 | => r"
317 | squarer {
318 | \x { x*x }
319 | }
320 |
321 | square = squarer();
322 | x = square(5);
323 | y = square(6);
324 | ");
325 | }
326 |
327 | #[test]
328 | fn argument_ordering() {
329 | run_test!(
330 | should_pass(lex, parse, typecheck, codegen)
331 | => r"
332 | fn1 x, y {
333 | x if y else -x;
334 | }
335 | fn2 y, x {
336 | x if y else -x;
337 | }
338 | fn3 = \x, y {
339 | x if y else -x;
340 | };
341 | fn4 = \y, x {
342 | x if y else -x;
343 | };
344 | a = fn1[x=1, y=true];
345 | b = fn1(1, true);
346 | c = fn2[y=true, x=1];
347 | d = fn2(true, 1);
348 | e = fn3[x=1, y=true];
349 | f = fn3(1, true);
350 | g = fn4[y=true, x=1];
351 | h = fn4(true, 1);
352 | ");
353 | }
354 |
355 | #[test]
356 | fn complex_closure_capture() {
357 | run_test!(
358 | should_pass(lex, parse, typecheck, codegen)
359 | => r"
360 | fn x {
361 | y = 2^x if x < 1 else x^2;
362 | \n { y%n };
363 | }
364 | x = fn(3)(2);
365 | ");
366 | }
367 |
--------------------------------------------------------------------------------
/tests/lexer.rs:
--------------------------------------------------------------------------------
1 | #[macro_use(run_test)]
2 | extern crate interpreter;
3 |
4 | #[test]
5 | fn all_tokens() {
6 | run_test!(
7 | should_pass(lex)
8 | => r"
9 | 1
10 | 1.1
11 | .1
12 | 1.
13 | 1.1e5
14 | 1e5
15 | .1e5
16 | 1.e5
17 | 1E5
18 |
19 | abcABC_~'0123
20 |
21 | + - * / ^ ^^ >= <= < > ! % && || == !=
22 | if else . , = : ; ? ( ) { } [ ] \ @
23 | true false
24 | // #&*GR^@&(G#^&(G@&*YFD*B@Y^(VT@^(f367g9@&*
25 | "
26 | );
27 | }
28 |
29 | #[test]
30 | fn non_tokens() {
31 | run_test!(
32 | should_fail(lex)
33 | => "` # $ & | ' \""
34 | );
35 | }
36 |
--------------------------------------------------------------------------------
/tests/parser.rs:
--------------------------------------------------------------------------------
1 | #[macro_use(run_test)]
2 | extern crate interpreter;
3 |
4 | #[test]
5 | fn unclosed_paren() {
6 | run_test!(
7 | should_fail(parse),
8 | should_pass(lex)
9 | => r"
10 | x = (5 + (5);
11 | ");
12 | }
13 |
14 | #[test]
15 | fn missing_assignment_semicolon() {
16 | run_test!(
17 | should_fail(parse),
18 | should_pass(lex)
19 | => r"
20 | x = 5
21 | ");
22 | }
23 |
24 | #[test]
25 | fn bad_item() {
26 | run_test!(
27 | should_fail(parse),
28 | should_pass(lex)
29 | => r"
30 | a;
31 | ");
32 | run_test!(
33 | should_fail(parse),
34 | should_pass(lex)
35 | => r"
36 | 5 = a;
37 | ");
38 | }
39 |
40 | #[test]
41 | fn empty_assignment_expr() {
42 | run_test!(
43 | should_fail(parse),
44 | should_pass(lex)
45 | => r"
46 | a = ;
47 | ");
48 | }
49 |
50 | #[test]
51 | fn extra_closing_paren() {
52 | run_test!(
53 | should_fail(parse),
54 | should_pass(lex)
55 | => r"
56 | a = (5+5));
57 | ");
58 | }
59 |
60 | #[test]
61 | fn unary_operator_used_as_binary() {
62 | run_test!(
63 | should_fail(parse),
64 | should_pass(lex)
65 | => r"
66 | a = true ! false;
67 | ");
68 | }
69 |
70 | #[test]
71 | fn binary_operator_used_as_unary() {
72 | run_test!(
73 | should_fail(parse),
74 | should_pass(lex)
75 | => r"
76 | a = 5 + * 5;
77 | ");
78 | run_test!(
79 | should_fail(parse),
80 | should_pass(lex)
81 | => r"
82 | a = 5 + 5 -;
83 | ");
84 | run_test!(
85 | should_fail(parse),
86 | should_pass(lex)
87 | => r"
88 | a = + 5 + 5;
89 | ");
90 | }
91 |
92 | #[test]
93 | fn closure_missing_block() {
94 | run_test!(
95 | should_fail(parse),
96 | should_pass(lex)
97 | => r"
98 | a = \x,y,z;
99 | ");
100 | }
101 |
102 | #[test]
103 | fn block_not_closed() {
104 | run_test!(
105 | should_fail(parse),
106 | should_pass(lex)
107 | => r"
108 | a = \x,y,z { x; { y; z; }
109 | ");
110 | run_test!(
111 | should_fail(parse),
112 | should_pass(lex)
113 | => r"
114 | a = { 5 + 5;
115 | ");
116 | }
117 |
118 | #[test]
119 | fn extra_semicolon() {
120 | run_test!(
121 | should_fail(parse),
122 | should_pass(lex)
123 | => r"
124 | a = 5;;
125 | ");
126 | run_test!(
127 | should_fail(parse),
128 | should_pass(lex)
129 | => r"
130 | a = {
131 | 5;
132 | ;
133 | };
134 | ");
135 | }
136 |
137 | #[test]
138 | fn empty_program() {
139 | run_test!(
140 | should_pass(lex, parse)
141 | => "");
142 | }
143 |
144 | #[test]
145 | fn empty_argument() {
146 | run_test!(
147 | should_fail(parse),
148 | should_pass(lex)
149 | => r"
150 | a = fn(5,);
151 | ");
152 | }
153 |
154 | #[test]
155 | fn wrong_bracket_type() {
156 | run_test!(
157 | should_fail(parse),
158 | should_pass(lex)
159 | => r"
160 | a = fn(5];
161 | ");
162 | run_test!(
163 | should_fail(parse),
164 | should_pass(lex)
165 | => r"
166 | a = fn[5);
167 | ");
168 | }
169 |
170 | #[test]
171 | fn bad_argument() {
172 | run_test!(
173 | should_fail(parse),
174 | should_pass(lex)
175 | => r"
176 | a = fn[5=a];
177 | ");
178 | run_test!(
179 | should_fail(parse),
180 | should_pass(lex)
181 | => r"
182 | b = fn[=5];
183 | ");
184 | run_test!(
185 | should_fail(parse),
186 | should_pass(lex)
187 | => r"
188 | d = fn[=];
189 | ");
190 | }
191 |
192 | #[test]
193 | fn closure_in_default_arg() {
194 | run_test!(
195 | should_pass(lex, parse)
196 | => r"
197 | fn y=\x{x<5} { }
198 | ");
199 | run_test!(
200 | should_pass(lex, parse)
201 | => r"
202 | fn z=\x=\{5},y=\{6}{x() r"
211 | a = fn[x=3, cond=\{x>5}];
212 | a = fn(3, \{x>5});
213 | ");
214 | }
215 |
216 | #[test]
217 | fn block_in_default_arg() {
218 | run_test!(
219 | should_fail(parse),
220 | should_pass(lex) // hopefully this will be supported at some point for completeness
221 | // but right now implementing it is a major pain
222 | => r"
223 | fn y=99-{1+2+3+4+5} { }
224 | ");
225 | }
226 |
227 | #[test]
228 | fn block_in_arg() {
229 | run_test!(
230 | should_pass(lex, parse)
231 | => r"
232 | a = fn[y={1+2+3+4+5}];
233 | b = fn({1+2+3+4+5});
234 | ");
235 | }
236 |
237 | #[test]
238 | fn closure_with_semicolon() {
239 | run_test!(
240 | should_pass(lex, parse)
241 | => r"
242 | a = \x, y { x; y; };
243 | ");
244 | run_test!(
245 | should_pass(lex, parse)
246 | => r"
247 | a = \x=\c{c;}, y { x; y; };
248 | ");
249 | }
250 |
251 | #[test]
252 | fn call_types() {
253 | run_test!(
254 | should_pass(lex, parse)
255 | => r"
256 | a = fn[x, y];
257 | b = fn[x=1, y=2];
258 | c = fn[x+=1, y+=1];
259 | c = fn(x+y, x-y);
260 | ");
261 | }
262 |
263 | #[test]
264 | fn conditional_missing_else() {
265 | run_test!(
266 | should_fail(parse),
267 | should_pass(lex)
268 | => r"
269 | x = y if z;
270 | ");
271 | }
272 |
273 | #[test]
274 | fn conditional() {
275 | run_test!(
276 | should_pass(lex, parse)
277 | => r"
278 | x = x + y if z && w else x - 2*y;
279 | ");
280 | }
281 |
282 | #[test]
283 | fn same_arg_twice() {
284 | run_test!(
285 | should_pass(lex),
286 | should_fail(parse)
287 | => r"
288 | fn x, x {
289 | x + x
290 | }
291 | ");
292 | }
293 |
--------------------------------------------------------------------------------
/tests/typecheck.rs:
--------------------------------------------------------------------------------
1 | #[macro_use]
2 | extern crate interpreter;
3 |
4 | #[test]
5 | fn reassign_different_type() {
6 | run_test!(
7 | should_warn(typecheck),
8 | should_pass(lex, parse)
9 | => r"
10 | x = 1;
11 | x = true;
12 | ");
13 | }
14 |
15 | #[test]
16 | fn shadow_function() {
17 | run_test!(
18 | should_warn(typecheck),
19 | should_pass(lex, parse)
20 | => r"
21 | fn x { x }
22 | fn x { x*2 }
23 | ");
24 | }
25 |
26 | #[test]
27 | fn call_non_function() {
28 | run_test!(
29 | should_fail(typecheck),
30 | should_pass(lex, parse)
31 | => r"
32 | x = 1;
33 | y = x();
34 | ");
35 | }
36 |
37 | #[test]
38 | fn wrong_num_arguments_named() {
39 | run_test!(
40 | should_fail(typecheck),
41 | should_pass(lex, parse)
42 | => r"
43 | f x, y=1 { x+y }
44 | x = f[x=1, y=2, z=3];
45 | ");
46 | run_test!(
47 | should_fail(typecheck),
48 | should_pass(lex, parse)
49 | => r"
50 | f x, y=1 { x+y }
51 | x = f[];
52 | ");
53 | }
54 |
55 | #[test]
56 | fn wrong_num_arguments_ordered() {
57 | run_test!(
58 | should_fail(typecheck),
59 | should_pass(lex, parse)
60 | => r"
61 | f x, y=1 { x+y }
62 | x = f(1, 2, 3);
63 | ");
64 | run_test!(
65 | should_fail(typecheck),
66 | should_pass(lex, parse)
67 | => r"
68 | f x, y=1 { x+y }
69 | x = f();
70 | ");
71 | }
72 |
73 | #[test]
74 | fn misspelled_argument() {
75 | run_test!(
76 | should_fail(typecheck),
77 | should_pass(lex, parse)
78 | => r"
79 | fn x { x }
80 | x = fn[y=5];
81 | ");
82 | }
83 |
84 | #[test]
85 | fn ambiguous_recursion() {
86 | run_test!(
87 | should_fail(typecheck),
88 | should_pass(lex, parse)
89 | => r"
90 | fn x { fn(x) }
91 | y = fn(1);
92 | ");
93 | }
94 |
95 | #[test]
96 | fn wrong_arg_type() {
97 | run_test!(
98 | should_fail(typecheck),
99 | should_pass(lex, parse)
100 | => r"
101 | fn x { x }
102 | x = fn(1);
103 | y = fn(true);
104 | ");
105 | run_test!(
106 | should_fail(typecheck),
107 | should_pass(lex, parse)
108 | => r"
109 | fn x { x }
110 | z = fn;
111 | x = z(1);
112 | y = fn(true);
113 | ");
114 | }
115 |
116 | #[test]
117 | fn wrong_block_type() {
118 | run_test!(
119 | should_fail(typecheck),
120 | should_pass(lex, parse)
121 | => r"
122 | x = {
123 | true;
124 | false;
125 | };
126 | ");
127 | }
128 |
129 | #[test]
130 | fn non_boolean_condition() {
131 | run_test!(
132 | should_fail(typecheck),
133 | should_pass(lex, parse)
134 | => r"
135 | x = true if 1 else false;
136 | ");
137 | }
138 |
139 | #[test]
140 | fn reassignment() {
141 | run_test!(
142 | should_pass(lex, parse, typecheck)
143 | => r"
144 | x = true;
145 | y = x ^^ true;
146 | x = 1;
147 | y = x * 5;
148 | ");
149 | run_test!(
150 | should_pass(lex, parse, typecheck)
151 | => r"
152 | x = true;
153 | a { x ^^ true }
154 | x = 1;
155 | b { x * 2 }
156 | w = b();
157 | z = a();
158 | ");
159 | }
160 |
161 | #[test]
162 | fn variable_not_in_scope() {
163 | run_test!(
164 | should_fail(typecheck),
165 | should_pass(lex, parse)
166 | => r"
167 | x = a;
168 | ");
169 | run_test!(
170 | should_fail(typecheck),
171 | should_pass(lex, parse)
172 | => r"
173 | fn {
174 | a = 5;
175 | }
176 | x = a;
177 | ");
178 | }
179 |
180 | #[test]
181 | fn mismatched_conditional_types() {
182 | run_test!(
183 | should_fail(typecheck),
184 | should_pass(lex, parse)
185 | => r"
186 | x = 1 if true else false;
187 | ");
188 | }
189 |
190 | #[test]
191 | fn wrong_type_numerical_op() {
192 | run_test!(
193 | should_fail(typecheck),
194 | should_pass(lex, parse)
195 | => r"
196 | a = 5 + true;
197 | ");
198 | }
199 |
200 | #[test]
201 | fn wrong_type_comparison_op() {
202 | run_test!(
203 | should_fail(typecheck),
204 | should_pass(lex, parse)
205 | => r"
206 | a = 5 + true;
207 | ");
208 | }
209 |
210 | #[test]
211 | fn wrong_type_equality_op() {
212 | run_test!(
213 | should_fail(typecheck),
214 | should_pass(lex, parse)
215 | => r"
216 | a = 5 == true;
217 | ");
218 | }
219 |
220 | #[test]
221 | fn wrong_type_boolean_op() {
222 | run_test!(
223 | should_fail(typecheck),
224 | should_pass(lex, parse)
225 | => r"
226 | a = 5 && true;
227 | ");
228 | }
229 |
230 | #[test]
231 | fn wrong_type_prefix_op() {
232 | run_test!(
233 | should_fail(typecheck),
234 | should_pass(lex, parse)
235 | => r"
236 | a = -true;
237 | ");
238 | run_test!(
239 | should_fail(typecheck),
240 | should_pass(lex, parse)
241 | => r"
242 | a = !5;
243 | ");
244 | }
245 |
246 | #[test]
247 | fn internal_block_shadowing_captured_vars() {
248 | run_test!(
249 | should_pass(lex, parse, typecheck)
250 | => r"
251 | x = 5;
252 | c = \{x+5};
253 | z = {
254 | x = true;
255 | c();
256 | };
257 | ");
258 | }
259 |
260 | #[test]
261 | fn default_args_evald_in_new_scope() {
262 | run_test!(
263 | should_pass(lex, parse, typecheck)
264 | => r"
265 | x = 5;
266 | fn z=x {
267 | z%2;
268 | }
269 | w = {
270 | x = true;
271 | fn();
272 | };
273 | ");
274 | }
275 |
276 | #[test]
277 | fn default_args() {
278 | run_test!(
279 | should_pass(lex, parse, typecheck)
280 | => r"
281 | fn x, y=3 { x + y }
282 | a = fn[x=1];
283 | a = fn[x=1, y=2];
284 | a = fn[y=1, x=2];
285 | a = fn(1, 2);
286 | ");
287 | }
288 |
289 | #[test]
290 | fn recursion_numeric() {
291 | run_test!(
292 | should_pass(lex, parse, typecheck)
293 | => r"
294 | a b {
295 | b;
296 | a[b-=1] if b > 0 else 0;
297 | }
298 | z = a[b=5];
299 | ");
300 | }
301 |
302 | #[test]
303 | fn call_nonexistant_function() {
304 | run_test!(
305 | should_pass(lex, parse),
306 | should_fail(typecheck)
307 | => r"
308 | x = f();
309 | ");
310 | }
311 |
312 | #[test]
313 | fn reference_nonexistant_variable() {
314 | run_test!(
315 | should_pass(lex, parse),
316 | should_fail(typecheck)
317 | => r"
318 | x = f;
319 | ");
320 | }
321 |
322 | #[test]
323 | fn call_default_arg() {
324 | run_test!(
325 | should_pass(lex, parse, typecheck)
326 | => r"
327 | fn d=\e{e} {
328 | d(5);
329 | }
330 | a = fn();
331 | ");
332 | }
333 |
334 | #[test]
335 | fn indirect_recursion() {
336 | run_test!(
337 | should_pass(lex, parse),
338 | should_fail(typecheck)
339 | => r"
340 | a { b() }
341 | b { a() }
342 | x = a();
343 | ");
344 | }
345 |
346 | #[test]
347 | fn unused_function() {
348 | run_test!(
349 | should_pass(lex, parse),
350 | should_warn(typecheck)
351 | => r"
352 | f x { x }
353 | ");
354 | }
355 |
356 | #[test]
357 | fn recursion_closure_issue() {
358 | run_test!(
359 | should_pass(lex, parse, typecheck)
360 | => r"
361 | fact n {
362 | n*fact(n-1) if n > 1 else 1;
363 | }
364 | summer fn, n {
365 | fn(n);
366 | summer(fn, n-1) if n > 0 else 0;
367 | }
368 | x = summer(\n { fact(n) }, 5);
369 | ");
370 | }
371 |
--------------------------------------------------------------------------------