|
1 | | -#include <complex> |
2 | 1 | #include <lbd/intp/interpreter.h> |
| 2 | +#include <lbd/intp/builtins.h> |
3 | 3 | #include <lbd/error.h> |
4 | | -#include <utility> |
5 | | - |
6 | | -#include "lbd/intp/builtins.h" |
7 | 4 |
|
8 | 5 | namespace intp::interp { |
9 | 6 | std::ostream &operator<<(std::ostream &os, const Closure &closure) { |
10 | | - // TODO: Improve this |
11 | 7 | return os << "<closure " << closure.param << ">"; |
12 | 8 | } |
13 | 9 |
|
14 | 10 | std::ostream &operator<<(std::ostream &os, const NativeFunction &native_fn) { |
15 | | - // TODO: Improve this |
16 | 11 | return os << "<native_fn " << native_fn.name << " " << native_fn.arity << ">"; |
17 | 12 | } |
18 | 13 |
|
19 | 14 | std::ostream &operator<<(std::ostream &os, const Value &value) { |
20 | 15 | std::visit([&]<typename T0>(T0 &&arg) { |
21 | 16 | using T = std::decay_t<T0>; |
22 | | - if constexpr (std::is_same_v<T, std::string> |
23 | | - || std::is_same_v<T, double> |
24 | | - || std::is_same_v<T, Closure> |
25 | | - || std::is_same_v<T, NativeFunction>) { |
| 17 | + if constexpr (std::is_same_v<T, std::string> || |
| 18 | + std::is_same_v<T, double> || |
| 19 | + std::is_same_v<T, Closure> || |
| 20 | + std::is_same_v<T, NativeFunction>) { |
26 | 21 | os << arg; |
27 | 22 | } else { |
28 | 23 | STATIC_ASSERT_UNREACHABLE_T(T, "unhandled runtime value"); |
@@ -132,154 +127,115 @@ namespace intp::interp { |
132 | 127 | }, expr.value); |
133 | 128 | } |
134 | 129 |
|
135 | | - // |
136 | | - // Value apply_fn_apl(Value fn_value, const std::vector<std::shared_ptr<Thunk> > &args, |
137 | | - // const std::shared_ptr<Env> &call_site_env, const std::optional<fe::loc::Loc> &call_loc) { |
138 | | - // Value current_fn = std::move(fn_value); |
139 | | - // size_t idx = 0; |
140 | | - // std::cout << "debug: fn: " << current_fn << std::endl; |
141 | | - // while (idx < args.size()) { |
142 | | - // // Apply Argument to Closure (Lambda Expression) |
143 | | - // std::optional<Value> resultant_value = std::nullopt; |
144 | | - // if (std::holds_alternative<Closure>(current_fn)) { |
145 | | - // const auto [param, body, env] = std::get<Closure>(current_fn); |
146 | | - // const auto child_env = std::make_shared<Env>(env); |
147 | | - // child_env->bind(param, args[idx]); |
148 | | - // // Evaluate body in child Environment |
149 | | - // resultant_value = eval_expr(*body, child_env); |
150 | | - // ++idx; |
151 | | - // } |
152 | | - // // Apply Argument to Native Function |
153 | | - // if (std::holds_alternative<NativeFunction>(current_fn)) { |
154 | | - // // Using by reference and not creating a copy of Native Function |
155 | | - // const auto &[arity, name, impl] = std::get<NativeFunction>(current_fn); |
156 | | - // if (args.size() - idx < arity) { |
157 | | - // if (call_loc) { |
158 | | - // std::cerr << *call_loc << ": "; |
159 | | - // } |
160 | | - // std::cerr << "runtime error: native function " |
161 | | - // << name << " expects " |
162 | | - // << arity << " argument(s), found " |
163 | | - // << args.size() - idx |
164 | | - // << std::endl; |
165 | | - // exit(EXIT_FAILURE); |
166 | | - // } |
167 | | - // std::vector<std::shared_ptr<Thunk> > slice; |
168 | | - // slice.reserve(arity); |
169 | | - // for (size_t i = 0; i < arity; ++i) { |
170 | | - // slice.push_back(args[idx + i]); |
171 | | - // } |
172 | | - // resultant_value = impl(slice, call_site_env); |
173 | | - // idx += arity; |
174 | | - // } |
175 | | - // if (resultant_value) { |
176 | | - // if (std::holds_alternative<Closure>(*resultant_value) || |
177 | | - // std::holds_alternative<NativeFunction>(*resultant_value)) { |
178 | | - // current_fn = std::move(*resultant_value); |
179 | | - // continue; |
180 | | - // } |
181 | | - // } |
182 | | - // if (call_loc) { |
183 | | - // std::cerr << *call_loc << ": "; |
184 | | - // } |
185 | | - // std::cerr << "runtime error: trying to apply non-function value " |
186 | | - // << current_fn |
187 | | - // << std::endl; |
188 | | - // exit(EXIT_FAILURE); |
189 | | - // } |
190 | | - // return current_fn; |
191 | | - // } |
192 | | - |
193 | | - Value apply_fn_apl(Value fn_value, |
194 | | - const std::vector<std::shared_ptr<Thunk> > &args, |
195 | | - const std::shared_ptr<Env> &call_site_env, |
196 | | - const std::optional<fe::loc::Loc> &call_loc) { |
197 | | - Value current_fn = std::move(fn_value); |
198 | | - size_t idx = 0; |
199 | | - |
200 | | - std::cout << "debug: fn: " << current_fn << std::endl; |
| 130 | + static std::shared_ptr<Thunk> value_to_thunk(const Value &v) { |
| 131 | + auto t = std::make_shared<Thunk>(); |
| 132 | + t->cached = v; |
| 133 | + return t; |
| 134 | + } |
201 | 135 |
|
202 | | - while (idx < args.size()) { |
203 | | - std::optional<Value> resultant_value; // value returned by applying one step |
| 136 | + static std::ostream &print_loc_prefix(std::ostream &os, const std::optional<fe::loc::Loc> &loc) { |
| 137 | + if (loc) { |
| 138 | + return os << *loc << ": "; |
| 139 | + } |
| 140 | + return os; |
| 141 | + } |
204 | 142 |
|
205 | | - // --- Closure (user lambda) branch --- |
| 143 | + Value apply_fn_apl(Value fn_value, const std::vector<std::shared_ptr<Thunk> > &args, |
| 144 | + const std::shared_ptr<Env> &call_site_env, const std::optional<fe::loc::Loc> &call_loc) { |
| 145 | + // Local mutable copy of the args, for inserting evaluated values as Thunks when needed. |
| 146 | + std::vector<std::shared_ptr<Thunk> > work_args = args; |
| 147 | + // Frame Stack: Functions to which we are currently applying Arguments. |
| 148 | + // Start with the initial Function. |
| 149 | + std::vector<Value> frames; |
| 150 | + frames.push_back(std::move(fn_value)); |
| 151 | + size_t idx = 0; // Index of next Thunk to consume from work_args |
| 152 | + while (true) { |
| 153 | + // If there are no frames left, that's unexpected (shouldn't happen), bail. |
| 154 | + if (frames.empty()) { |
| 155 | + print_loc_prefix(std::cerr, call_loc) |
| 156 | + << "internal error: no function frame to apply" << std::endl; |
| 157 | + std::exit(EXIT_FAILURE); |
| 158 | + } |
| 159 | + // If we've consumed all available Argument Thunks: |
| 160 | + if (idx >= work_args.size()) { |
| 161 | + // If top frame is a function, and we have no more args to feed it, |
| 162 | + // return that function (partial application) or its value as-is. |
| 163 | + // If it's not a function (shouldn't happen), return it as value. |
| 164 | + return frames.back(); |
| 165 | + } |
| 166 | + // Work on the top-most frame |
| 167 | + Value current_fn = frames.back(); |
| 168 | + std::optional<Value> resultant_value; |
| 169 | + // Closure case: Closure consumes exactly one Argument (its Param) |
206 | 170 | if (std::holds_alternative<Closure>(current_fn)) { |
207 | 171 | const auto [param, body, env] = std::get<Closure>(current_fn); |
208 | | - auto child_env = std::make_shared<Env>(env); |
209 | | - child_env->bind(param, args[idx]); |
210 | | - |
211 | | - // Evaluate the lambda body in the child env |
| 172 | + const auto &arg_thunk = work_args[idx++]; |
| 173 | + const auto child_env = std::make_shared<Env>(env); |
| 174 | + child_env->bind(param, arg_thunk); |
212 | 175 | resultant_value = eval_expr(*body, child_env); |
213 | | - ++idx; // we consumed one argument (the parameter) |
214 | 176 | } |
215 | | - // --- Native function branch --- |
| 177 | + // Native Function case: Consumes its arity-many Argument Thunks |
216 | 178 | else if (std::holds_alternative<NativeFunction>(current_fn)) { |
217 | | - const auto &nf = std::get<NativeFunction>(current_fn); |
218 | | - const auto &arity = nf.arity; |
219 | | - const auto &name = nf.name; |
220 | | - const auto &impl = nf.impl; |
221 | | - |
222 | | - if (args.size() - idx < arity) { |
223 | | - if (call_loc) std::cerr << *call_loc << ": "; |
224 | | - std::cerr << "runtime error: native function " |
225 | | - << name << " expects " << arity |
226 | | - << " argument(s), found " << (args.size() - idx) |
227 | | - << std::endl; |
| 179 | + const auto &[arity, name, impl] = std::get<NativeFunction>(current_fn); |
| 180 | + if (work_args.size() - idx < arity) { |
| 181 | + print_loc_prefix(std::cerr, call_loc) |
| 182 | + << "runtime error: native function " << name |
| 183 | + << " expects " << arity << " argument(s), found " |
| 184 | + << (work_args.size() - idx) << std::endl; |
228 | 185 | std::exit(EXIT_FAILURE); |
229 | 186 | } |
230 | | - |
231 | | - // collect the next `arity` thunks |
232 | 187 | std::vector<std::shared_ptr<Thunk> > slice; |
233 | 188 | slice.reserve(arity); |
234 | | - for (size_t i = 0; i < arity; ++i) slice.push_back(args[idx + i]); |
235 | | - |
| 189 | + for (size_t i = 0; i < arity; ++i) { |
| 190 | + slice.push_back(work_args[idx + i]); |
| 191 | + } |
236 | 192 | resultant_value = impl(slice, call_site_env); |
237 | | - idx += arity; // consumed `arity` args |
| 193 | + idx += arity; |
| 194 | + } else { |
| 195 | + // Top frame is not a Function (Closure, NativeFunction) Value but there are still Arguments left |
| 196 | + print_loc_prefix(std::cerr, call_loc) |
| 197 | + << "runtime error: trying to apply non-function value " |
| 198 | + << frames.back() << std::endl; |
| 199 | + std::exit(EXIT_FAILURE); |
238 | 200 | } |
239 | | - // --- Not a function when there are still args left: error --- |
240 | | - else { |
241 | | - if (call_loc) std::cerr << *call_loc << ": "; |
242 | | - std::cerr << "runtime error: trying to apply non-function value " |
243 | | - << current_fn << std::endl; |
| 201 | + if (!resultant_value) { |
| 202 | + print_loc_prefix(std::cerr, call_loc) |
| 203 | + << "internal error: application produced no result" << std::endl; |
244 | 204 | std::exit(EXIT_FAILURE); |
245 | 205 | } |
246 | | - |
247 | | - // If the application produced a value (it always should) |
248 | | - if (resultant_value.has_value()) { |
249 | | - std::cout << "debug: result: " << *resultant_value << std::endl; |
250 | | - |
251 | | - // If the returned value is a function, curry: continue applying remaining args. |
252 | | - if (std::holds_alternative<Closure>(*resultant_value) || |
253 | | - std::holds_alternative<NativeFunction>(*resultant_value)) { |
254 | | - current_fn = std::move(*resultant_value); |
255 | | - continue; // try to apply remaining args to this new function |
256 | | - } |
257 | | - |
258 | | - // Otherwise the returned value is a terminal (non-function). |
259 | | - // If we've consumed all arguments for the original call, return it. |
260 | | - if (idx >= args.size()) { |
261 | | - return *resultant_value; |
| 206 | + if (std::holds_alternative<Closure>(*resultant_value) || |
| 207 | + std::holds_alternative<NativeFunction>(*resultant_value)) { |
| 208 | + // Replace the top Frame with the returned Function (curry) |
| 209 | + frames.back() = std::move(*resultant_value); |
| 210 | + continue; |
| 211 | + } |
| 212 | + // Resultant is a concrete Value (different from Function Value i.e. double, string). |
| 213 | + // Pop the frame that produced it, and insert this Value as a thunk at the current idx |
| 214 | + // so the previous Frame (if any) will consume it. |
| 215 | + Value concrete = std::move(*resultant_value); |
| 216 | + frames.pop_back(); |
| 217 | + // If there are no more Frames after popping, then this Value is the final result of the |
| 218 | + // whole Function Application, but only valid if there are no further Argument Thunks remaining. |
| 219 | + if (frames.empty()) { |
| 220 | + if (idx < work_args.size()) { |
| 221 | + print_loc_prefix(std::cerr, call_loc) |
| 222 | + << "runtime error: too many arguments applied to non-function value " |
| 223 | + << concrete << std::endl; |
| 224 | + std::exit(EXIT_FAILURE); |
262 | 225 | } |
263 | | - |
264 | | - // Otherwise there are still args left but we got a non-function — that's an error. |
265 | | - if (call_loc) std::cerr << *call_loc << ": "; |
266 | | - std::cerr << "runtime error: too many arguments applied to non-function value " |
267 | | - << *resultant_value << std::endl; |
268 | | - std::exit(EXIT_FAILURE); |
| 226 | + // No Thunks remaining then concrete Value is the result |
| 227 | + return concrete; |
269 | 228 | } |
270 | | - |
271 | | - // Defensive: we should never reach here, but keep an explicit guard. |
272 | | - if (call_loc) std::cerr << *call_loc << ": "; |
273 | | - std::cerr << "runtime error: internal apply error" << std::endl; |
274 | | - std::exit(EXIT_FAILURE); |
| 229 | + // There is a previous Frame; insert the concrete Value as a Thunk at the |
| 230 | + // current index so the previous Frame will consume it on the next iteration. |
| 231 | + auto inj_th = value_to_thunk(concrete); |
| 232 | + work_args.insert(work_args.begin() + static_cast<int>(idx), inj_th); |
| 233 | + // Do NOT advance idx: the inserted thunk is at position idx and will be consumed |
| 234 | + // by the previous frame on the next iteration. |
| 235 | + // Loop will continue with frames.back() being the previous frame. |
275 | 236 | } |
276 | | - |
277 | | - // If we consumed all args and still have a function value (e.g., partial application), |
278 | | - // return the function (curried function). |
279 | | - return current_fn; |
280 | 237 | } |
281 | 238 |
|
282 | | - |
283 | 239 | // Creates placeholder Thunk then set body so recursion can refer to it during lazy evaluation |
284 | 240 | static void bind_def_ast_node_lazy(const fe::ast::DefAstNode &def_ast_node, const std::shared_ptr<Env> &env) { |
285 | 241 | const auto thunk = std::make_shared<Thunk>(); |
|
0 commit comments