3#ifndef AWKWARD_FORTHMACHINE_H_
4#define AWKWARD_FORTHMACHINE_H_
24 template <
typename T,
typename I>
27 template <
typename TYPE>
using IndexTypeOf =
typename std::vector<TYPE>::size_type;
31 int64_t stack_max_depth=1024,
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);
53 decompiled_segment(int64_t segment_position, const std::
string& indent="",
54 bool endline = true) const;
58 decompiled_at(int64_t bytecode_position, const std::
string& indent="") const;
61 const std::vector<std::
string>
66 stack_max_depth() const noexcept;
70 recursion_max_depth() const noexcept;
74 string_buffer_size() const noexcept;
78 output_initial_size() const noexcept;
82 output_resize_factor() const noexcept;
90 stack_at(int64_t from_top) const noexcept;
94 stack_depth() const noexcept;
98 stack_can_push() const noexcept {
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>
132 variable_index() const;
136 variable_at(const std::
string& name) const;
140 variable_at(int64_t index) const noexcept;
144 input_must_be_writable(const std::
string& name) const;
148 input_position_at(const std::
string& name) const;
152 input_position_at(int64_t index) const noexcept;
159 const std::vector<std::
string>
160 output_index() const noexcept;
164 output_at(const std::
string& name) const;
168 output_at(int64_t index) const noexcept;
172 output_NumpyArray_at(const std::
string& name) const;
176 output_NumpyArray_at(int64_t index) const;
180 output_Index8_at(const std::
string& name) const;
184 output_Index8_at(int64_t index) const;
188 output_IndexU8_at(const std::
string& name) const;
192 output_IndexU8_at(int64_t index) const;
196 output_Index32_at(const std::
string& name) const;
200 output_Index32_at(int64_t index) const;
204 output_IndexU32_at(const std::
string& name) const;
208 output_IndexU32_at(int64_t index) const;
212 output_Index64_at(const std::
string& name) const;
216 output_Index64_at(int64_t index) const;
221 string_at(int64_t index) const noexcept;
237 begin_again(const std::map<std::
string, std::shared_ptr<
ForthInputBuffer>>& inputs,
bool reset_instruction);
257 call(const std::
string& name);
265 maybe_throw(util::ForthError err, const std::set<util::ForthError>& ignore) const;
269 current_bytecode_position() const noexcept;
273 current_recursion_depth() const noexcept;
277 current_instruction() const;
281 count_reset() noexcept;
285 count_instructions() const noexcept;
289 count_reads() const noexcept;
293 count_writes() const noexcept;
297 count_nanoseconds() const noexcept;
301 is_integer(const std::
string& word, int64_t& value) const;
305 is_variable(const std::
string& word) const;
309 is_input(const std::
string& word) const;
313 is_output(const std::
string& word) const;
317 is_nbit(const std::
string& word, I& value) const;
321 is_reserved(const std::
string& word) const;
325 is_defined(const std::
string& word) const;
329 is_ready() const noexcept {
336 return recursion_target_depth_.empty();
342 return !(bytecodes_pointer_where() < (
343 bytecodes_offsets_[(IndexTypeOf<int64_t>)bytecodes_pointer_which() + 1] -
344 bytecodes_offsets_[(IndexTypeOf<int64_t>)bytecodes_pointer_which()]
351 segment_nonempty(int64_t segment_position)
const;
355 bytecodes_per_instruction(int64_t bytecode_position)
const;
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,
388 internal_run(
bool single_step, int64_t recursion_target_depth_top);
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;
404 stack_cannot_push() const 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_;
Definition: ForthMachine.h:25
void stack_clear() noexcept
HERE.
void stack_push(T value) noexcept
HERE.
Definition: ForthMachine.h:110
bool is_segment_done() const noexcept
HERE.
Definition: ForthMachine.h:341
T stack_pop() noexcept
HERE.
Definition: ForthMachine.h:117
bool stack_can_pop() const noexcept
HERE.
Definition: ForthMachine.h:104
bool is_done() const noexcept
HERE.
Definition: ForthMachine.h:335
const std::string source() const noexcept
HERE.
ForthMachineOf(const std::string &source, int64_t stack_max_depth=1024, int64_t recursion_max_depth=1024, int64_t string_buffer_size=1024, int64_t output_initial_size=1024, double output_resize_factor=1.5)
HERE.
Definition: ForthOutputBuffer.h:36
A contiguous, one-dimensional array of integers used to represent data structures,...
Definition: Index.h:82
#define LIBAWKWARD_EXPORT_SYMBOL
Definition: common.h:45
Definition: BitMaskedArray.h:15
std::shared_ptr< Content > ContentPtr
Definition: Content.h:15