27 template <
typename TYPE>
using IndexTypeOf =
typename std::vector<TYPE>::size_type;
32 int64_t recursion_max_depth=1024,
33 int64_t string_buffer_size=1024,
34 int64_t output_initial_size=1024,
35 double output_resize_factor=1.5);
61 const std::vector<std::
string>
99 return stack_depth_ < stack_max_depth_;
105 return stack_depth_ > 0;
111 stack_buffer_[stack_depth_] = value;
119 return stack_buffer_[stack_depth_];
127 const std::map<std::
string, T>
131 const std::vector<std::
string>
159 const std::vector<std::
string>
336 return recursion_target_depth_.empty();
342 return !(bytecodes_pointer_where() < (
359 err_linecol(
const std::vector<std::pair<int64_t, int64_t>>&
linecol,
362 const std::string&
message)
const;
366 tokenize(std::vector<std::string>&
tokenized,
367 std::vector<std::pair<int64_t, int64_t>>&
linecol);
371 compile(
const std::vector<std::string>&
tokenized,
372 const std::vector<std::pair<int64_t, int64_t>>&
linecol);
376 parse(
const std::string&
defn,
377 const std::vector<std::string>&
tokenized,
378 const std::vector<std::pair<int64_t, int64_t>>&
linecol,
381 std::vector<I>& bytecodes,
382 std::vector<std::vector<I>>& dictionary,
392 write_from_stack(
int64_t num, T*
top)
noexcept;
396 write_add_from_stack(
int64_t num, T*
top)
noexcept;
400 print_number(T num)
noexcept;
405 return stack_depth_ == stack_max_depth_;
410 stack_cannot_pop() const noexcept {
411 return stack_depth_ == 0;
416 stack_cannot_pop2() const noexcept {
417 return stack_depth_ < 2;
422 stack_cannot_pop3() const noexcept {
423 return stack_depth_ < 3;
428 stack_pop2() noexcept {
430 return &stack_buffer_[stack_depth_];
435 stack_pop2_before_pushing1() noexcept {
437 return &stack_buffer_[stack_depth_ - 1];
442 stack_peek() const noexcept {
443 return &stack_buffer_[stack_depth_ - 1];
448 bytecode_get() const noexcept {
449 int64_t start = bytecodes_offsets_[(IndexTypeOf<int64_t>)bytecodes_pointer_which()];
450 return bytecodes_[(IndexTypeOf<I>)(start + bytecodes_pointer_where())];
455 bytecodes_pointer_push(int64_t which)
noexcept {
456 current_which_[recursion_current_depth_] = which;
457 current_where_[recursion_current_depth_] = 0;
458 recursion_current_depth_++;
463 bytecodes_pointer_pop() noexcept {
464 recursion_current_depth_--;
469 bytecodes_pointer_which() const noexcept {
470 return current_which_[recursion_current_depth_ - 1];
475 bytecodes_pointer_where() const noexcept {
476 return current_where_[recursion_current_depth_ - 1];
481 do_loop_push(int64_t start, int64_t stop)
noexcept {
482 do_recursion_depth_[do_current_depth_] = recursion_current_depth_;
483 do_stop_[do_current_depth_] = stop;
484 do_i_[do_current_depth_] = start;
490 do_steploop_push(int64_t start, int64_t stop)
noexcept {
491 do_recursion_depth_[do_current_depth_] = ~recursion_current_depth_;
492 do_stop_[do_current_depth_] = stop;
493 do_i_[do_current_depth_] = start;
499 do_recursion_depth() const noexcept {
500 return do_recursion_depth_[do_current_depth_ - 1];
505 do_abs_recursion_depth() const noexcept {
506 int64_t out = do_recursion_depth_[do_current_depth_ - 1];
517 do_loop_is_step() const noexcept {
518 return do_recursion_depth_[do_current_depth_ - 1] < 0;
523 do_stop() const noexcept {
524 return do_stop_[do_current_depth_ - 1];
529 do_i() const noexcept {
530 return do_i_[do_current_depth_ - 1];
535 do_j() const noexcept {
536 return do_i_[do_current_depth_ - 2];
541 do_k() const noexcept {
542 return do_i_[do_current_depth_ - 3];
546 int64_t output_initial_size_;
547 double output_resize_factor_;
550 int64_t stack_depth_;
551 int64_t stack_max_depth_;
553 std::vector<std::string> variable_names_;
554 std::vector<T> variables_;
556 std::vector<std::string> input_names_;
557 std::vector<bool> input_must_be_writable_;
558 std::vector<std::string> output_names_;
559 std::vector<util::dtype> output_dtypes_;
561 std::vector<std::string> strings_;
562 std::vector<std::string> dictionary_names_;
563 std::vector<I> dictionary_bytecodes_;
564 std::vector<int64_t> bytecodes_offsets_;
565 std::vector<I> bytecodes_;
567 char* string_buffer_;
568 int64_t string_buffer_size_;
570 std::vector<std::shared_ptr<ForthInputBuffer>> current_inputs_;
571 std::vector<std::shared_ptr<ForthOutputBuffer>> current_outputs_;
574 int64_t* current_which_;
575 int64_t* current_where_;
576 int64_t recursion_current_depth_;
577 std::stack<int64_t> recursion_target_depth_;
578 int64_t recursion_max_depth_;
580 int64_t* do_recursion_depth_;
583 int64_t do_current_depth_;
585 util::ForthError current_error_;
587 int64_t count_instructions_;
588 int64_t count_reads_;
589 int64_t count_writes_;
590 int64_t count_nanoseconds_;