void run_repl_stdin(World* world) { Stack* stack = alloc_stack(world); repl_start(stack); printf("Started REPL, type /help for reference.\n"); while (true) { Value input; // Get next line if (!circa_get_line(&input)) break; // Before doing any work, process any pending file changes. file_watch_check_all(world); Value output; repl_run_line(stack, &input, &output); for (int i=0; i < list_length(&output); i++) std::cout << as_cstring(list_get(&output, i)) << std::endl; // Check if we've finished. if (top_frame(stack) == NULL) return; } }
int dm_btree_del(struct dm_btree_info *info, dm_block_t root) { int r; struct del_stack *s; s = kmalloc(sizeof(*s), GFP_NOIO); if (!s) return -ENOMEM; s->tm = info->tm; s->top = -1; r = push_frame(s, root, 0); if (r) goto out; while (unprocessed_frames(s)) { uint32_t flags; struct frame *f; dm_block_t b; r = top_frame(s, &f); if (r) goto out; if (f->current_child >= f->nr_children) { pop_frame(s); continue; } flags = le32_to_cpu(f->n->header.flags); if (flags & INTERNAL_NODE) { b = value64(f->n, f->current_child); f->current_child++; r = push_frame(s, b, f->level); if (r) goto out; } else if (is_internal_level(info, f)) { b = value64(f->n, f->current_child); f->current_child++; r = push_frame(s, b, f->level + 1); if (r) goto out; } else { if (info->value_type.dec) { unsigned i; for (i = 0; i < f->nr_children; i++) info->value_type.dec(info->value_type.context, value_ptr(f->n, i)); } f->current_child = f->nr_children; } } out: kfree(s); return r; }
void for_loop_finish_iteration(Stack* context) { Frame* frame = top_frame(context); Branch* contents = frame->branch; // Find list length caValue* listInput = get_frame_register(frame, 0); // Increment the loop index caValue* index = get_frame_register(frame, for_loop_find_index(contents)->index); set_int(index, as_int(index) + 1); // Check if we are finished if (as_int(index) >= list_length(listInput)) { frame->loop = false; finish_frame(context); return; } // If we're not finished yet, copy rebound outputs back to inputs. for (int i=1;; i++) { Term* input = get_input_placeholder(contents, i); if (input == NULL) break; Term* output = get_output_placeholder(contents, i); copy(get_frame_register(frame, output->index), get_frame_register(frame, input->index)); } // Return to start of loop body frame->pc = 0; frame->nextPc = 0; }
void repl_evaluate_line(Stack* context, std::string const& input, std::ostream& output) { Branch* branch = top_branch(context); int previousHead = branch->length(); parser::compile(branch, parser::statement_list, input); int newHead = branch->length(); bool anyErrors = false; int resultIndex = -1; for (int i=previousHead; i < newHead; i++) { Term* result = branch->get(i); if (has_static_error(result)) { output << "error: "; print_static_error(result, output); output << std::endl; anyErrors = true; break; } Frame* frame = top_frame(context); frame->pc = i; frame->endPc = i + 1; run_interpreter(context); if (error_occurred(context)) { output << "error: "; print_error_stack(context, std::cout); anyErrors = true; break; } resultIndex = i; } // Print results of the last expression if (!anyErrors && resultIndex != -1) { Term* result = branch->get(resultIndex); if (result->type != as_type(VOID_TYPE)) { output << find_stack_value_for_term(context, result, 0)->toString() << std::endl; } } clear_error(context); }
void start_for_loop(caStack* stack, bool enableLoopOutput) { Frame* frame = top_frame(stack); Branch* contents = frame->branch; // Check if top frame actually contains a for-loop (it might be using the #zero branch) if (!is_for_loop(contents)) return; // Initialize the loop index set_int(get_frame_register(frame, for_loop_find_index(contents)), 0); if (enableLoopOutput) { // Initialize output value set_int(get_frame_register(frame, for_loop_find_output_index(contents)), 0); caValue* listInput = circa_input(stack, 0); set_list(get_frame_register_from_end(frame, 0), list_length(listInput)); } // Interpreter will run the contents of the branch }
void for_loop_finish_iteration(Stack* stack, bool enableLoopOutput) { INCREMENT_STAT(LoopFinishIteration); Frame* frame = top_frame(stack); Branch* contents = frame->branch; // Find list length caValue* listInput = get_frame_register(frame, 0); // Increment the loop index caValue* index = get_top_register(stack, for_loop_find_index(contents)); set_int(index, as_int(index) + 1); // Preserve list output if (enableLoopOutput && frame->exitType != name_Discard) { caValue* outputIndex = get_frame_register(frame, for_loop_find_output_index(contents)); Term* outputPlaceholder = get_output_placeholder(contents, 0); caValue* outputList = get_frame_register(frame, outputPlaceholder); caValue* outputValue = find_stack_value_for_term(stack, outputPlaceholder->input(0), 0); if (!is_list(outputList)) set_list(outputList); list_touch(outputList); copy(outputValue, list_get(outputList, as_int(outputIndex))); INCREMENT_STAT(LoopWriteOutput); // Advance output index set_int(outputIndex, as_int(outputIndex) + 1); } // Check if we are finished if (as_int(index) >= list_length(listInput) || frame->exitType == name_Break || frame->exitType == name_Return) { // Possibly truncate output list, in case any elements were discarded. if (enableLoopOutput) { caValue* outputIndex = get_frame_register(frame, for_loop_find_output_index(contents)); Term* outputPlaceholder = get_output_placeholder(contents, 0); caValue* outputList = get_frame_register(frame, outputPlaceholder); list_resize(outputList, as_int(outputIndex)); } else { Term* outputPlaceholder = get_output_placeholder(contents, 0); caValue* outputList = get_frame_register(frame, outputPlaceholder); set_list(outputList, 0); } finish_frame(stack); return; } // If we're not finished yet, copy rebound outputs back to inputs. for (int i=1;; i++) { Term* input = get_input_placeholder(contents, i); if (input == NULL) break; Term* output = get_output_placeholder(contents, i); copy(get_frame_register(frame, output), get_frame_register(frame, input)); INCREMENT_STAT(Copy_LoopCopyRebound); } // Return to start of loop body frame->pc = 0; frame->nextPc = 0; frame->exitType = name_None; }