libvata2  [unstable] git snapshot
vm.hh
Go to the documentation of this file.
1 /* vm.hh -- the VATA virtual machine
2  *
3  * Copyright (c) 2018 Ondrej Lengal <ondra.lengal@gmail.com>
4  *
5  * This file is a part of libvata2.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  */
17 
18 #ifndef _VATA2_VM_HH_
19 #define _VATA2_VM_HH_
20 
21 #include <stack>
22 
23 // VATA headers
24 #include <vata2/parser.hh>
25 
26 namespace Vata2
27 {
28 namespace VM
29 {
30 
31 /// Data type representing a pointer to a memory holding a value
32 using VMPointer = const void*;
33 
34 /**
35  * Data type representing a value, which is composed of a type and a pointer to
36  * a general memory
37 */
38 class VMValue
39 { // {{{
40 public:
41  /// name of the type
42  std::string type;
43 
44 private:
45 
46  /// pointer to the object
47  VMPointer ptr;
48 
49 public:
50  /// default constructor
51  VMValue() : type(), ptr() { }
52  /// standard constructor
53  VMValue(const std::string& type, VMPointer ptr) : type(type), ptr(ptr) { }
54  /// copy constructor
55  VMValue(const VMValue& rhs) : type(rhs.type), ptr(rhs.ptr) { }
56  /// assignment operator
57  VMValue& operator=(const VMValue& rhs)
58  { // {{{
59  if (this != &rhs) {
60  this->type = rhs.type;
61  this->ptr = rhs.ptr;
62  }
63 
64  // FIXME: expecting memory issues here
65  return *this;
66  } // operator=() }}}
67 
68  /// returns the included pointer
69  VMPointer get_ptr() const { return this->ptr; }
70 
71  /// conversion to string
72  friend std::ostream& operator<<(std::ostream& os, const VMValue& val)
73  { // {{{
74  os << "<" << val.type << ": ";
75  if ("string" == val.type) {
76  // FIXME: dispatch this call to val.type dispatcher
77  os << *static_cast<const std::string*>(val.get_ptr());
78  } else {
79  os << val.get_ptr();
80  }
81 
82  os << ">";
83  return os;
84  } // operator<<(std::ostream) }}}
85 }; // VMValue }}}
86 
87 /// A dictionary mapping names to values
88 using VMStorage = std::unordered_map<std::string, VMValue>;
89 
90 /// The virtual machine executing VATA code
92 {
93 private:
94 
95  /// The memory assigning values to names
96  VMStorage mem;
97  std::stack<VMValue> exec_stack;
98 
99 public:
100 
101  /// default constructor
102  VirtualMachine() : mem(), exec_stack() { }
103 
104  void run(const Vata2::Parser::Parsed& parsed);
105  void run(const Vata2::Parser::ParsedSection& parsec);
106  void run_code(const Vata2::Parser::ParsedSection& parsec);
107 
108  /// Executes one line of code
109  void execute_line(const Parser::BodyLine& line);
110  void process_token(const std::string& tok);
111  void exec_cmd(const std::vector<VMValue>& exec_vec);
112 
113  /// Cleans the stack
114  void clean_stack();
115 };
116 
117 /// The exception for virtual machine errors
118 class VMException : public std::runtime_error
119 {
120 public:
121  // use base class constructors
122  using std::runtime_error::runtime_error;
123 
124 };
125 
126 // CLOSING NAMESPACES AND GUARDS
127 } /* VM */
128 } /* Vata2 */
129 
130 #endif /* _VATA2_VM_HH_ */
Parsed data (single section)
Definition: parser.hh:41
void exec_cmd(const std::vector< VMValue > &exec_vec)
void execute_line(const Parser::BodyLine &line)
Executes one line of code.
VMValue()
default constructor
Definition: vm.hh:51
std::vector< std::string > BodyLine
Definition: parser.hh:38
std::string type
name of the type
Definition: vm.hh:42
The virtual machine executing VATA code.
Definition: vm.hh:91
VMValue(const VMValue &rhs)
copy constructor
Definition: vm.hh:55
const void * VMPointer
Data type representing a pointer to a memory holding a value.
Definition: vm.hh:32
std::unordered_map< std::string, VMValue > VMStorage
A dictionary mapping names to values.
Definition: vm.hh:88
VMValue(const std::string &type, VMPointer ptr)
standard constructor
Definition: vm.hh:53
void process_token(const std::string &tok)
std::vector< ParsedSection > Parsed
Parsed data.
Definition: parser.hh:97
VirtualMachine()
default constructor
Definition: vm.hh:102
void run(const Vata2::Parser::Parsed &parsed)
VMValue & operator=(const VMValue &rhs)
assignment operator
Definition: vm.hh:57
Data type representing a value, which is composed of a type and a pointer to a general memory...
Definition: vm.hh:38
The exception for virtual machine errors.
Definition: vm.hh:118
void clean_stack()
Cleans the stack.
friend std::ostream & operator<<(std::ostream &os, const VMValue &val)
conversion to string
Definition: vm.hh:72
void run_code(const Vata2::Parser::ParsedSection &parsec)
VMPointer get_ptr() const
returns the included pointer
Definition: vm.hh:69