Website Summary Files Commits Branches @commit @diff @stats
path: root / spasm / src / spasm_impl.hpp
blob: 78c5d08cd7e3ab20f63aa4f2bb0b4aecfc8857c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef SPASM_IMPL_HPP
#define SPASM_IMPL_HPP

#include <iostream>

#include "dstack.hpp"
#include "frames.hpp"
#include "types.hpp"

namespace SpasmImpl
{
//! The Abstract Stack Machine
/*!
** The machine contains a data stack for operations and control flow and
** frame stack for function calls (including parameter passing) and local
** variables and return stack for the addresses of the returns.
*/
class Spasm
{
   public:
    Spasm();
    Spasm(PC_t,
          const byte*,
          std::istream& = std::cin,
          std::ostream& = std::cout);
    Spasm(const Spasm&);
    ~Spasm();
    Spasm& operator=(const Spasm&);

    void run();

    const Dstack& get_dstack() const;

   private:
    //! Operations (opcodes) of the machine
    static Operation operations[];

    //! number of operations
    static PC_t op_size;

    //! Program counter - points the current opcode
    PC_t pc;

    //! size of the program bytecode
    PC_t bc_size;

    //! bytecode of the program
    byte* bytecode;

    //! Data stack for arithmetic operations and control flow
    Dstack data_stack;

    //! Return stack for the addresses of the returns
    Rstack_t return_stack;

    //! Stack for the frame of the functions
    FrameStack frame;

    //! Input stream for read () operation
    std::istream* istr;

    //! Output stream for print () opertion
    std::ostream* ostr;

    void push();
    void pop();
    void dup();

    void print();
    void read();

    void plus();
    void minus();
    void multiply();
    void divide();
    void modulus();

    void gotrue();
    void gofalse();
    void go();

    void call();
    void ret();

    void load();
    void store();

    void less();
    void lesseq();

    // private method for copying and deleting an object
    void copybc(const Spasm&);
    void deleteobj();
};

}  // namespace SpasmImpl
#endif  // #ifndef SPASM_IMPL_HPP