Commit d198ab2d authored by Victor Yacovlev's avatar Victor Yacovlev

Added forgotten files

parent 6b050da4
#ifndef CONTEXT_H
#define CONTEXT_H
#include <vector>
#include "variant.hpp"
#include "vm_instruction.hpp"
#include "vm_tableelem.hpp"
namespace VM {
enum ContextRunMode {
CRM_UntilReturn,
CRM_ToEnd,
CRM_OneStep
};
struct Context {
inline Context() {
IP = -1; type = Bytecode::EL_FUNCTION;
runMode = CRM_ToEnd; lineNo = -1;
algId = -1;
}
int registers[201];
int IP;
std::vector<Variable> locals;
const std::vector<Bytecode::Instruction> * program;
Bytecode::ElemType type;
ContextRunMode runMode;
uint8_t moduleId;
int algId;
int lineNo;
};
}
#endif
#ifndef STACK_HPP
#define STACK_HPP
#include <cstdlib>
#include <vector>
namespace VM {
template <class T> class Stack
{
public:
inline void push(const T& t)
{
i_currentIndex ++;
if (i_currentIndex>=p_data.size()) {
p_data.resize(p_data.size()+i_deltaSize);
}
p_data[i_currentIndex] = t;
}
inline T pop()
{
i_currentIndex--;
return p_data[i_currentIndex+1];
}
inline T& top()
{
return p_data[i_currentIndex];
}
inline const T& top() const
{
return p_data[i_currentIndex];
}
inline T& at(int index)
{
return p_data[index];
}
inline const T& at(int index) const
{
return p_data[index];
}
inline int size() const { return i_currentIndex+1; }
inline void reset()
{
p_data = std::vector<T>(i_initialSize);
i_currentIndex = -1;
}
inline ~Stack()
{
p_data.clear();
}
private:
size_t i_initialSize = 100;
size_t i_deltaSize = 100;
size_t i_currentIndex = 0;
std::vector<T> p_data = std::vector<T>(100);
};
}
#endif
This diff is collapsed.
#include "vm.hpp"
\ No newline at end of file
#ifndef VM_ENUMS_H
#define VM_ENUMS_H
namespace Bytecode {
enum ElemType {
EL_NONE = 0x00, // stream delimeter
EL_LOCAL = 0x01, // Local variable
EL_GLOBAL = 0x02, // Global variable
EL_CONST = 0x03, // Constant
EL_FUNCTION = 0x04, // Local defined function
EL_EXTERN = 0x05, // External module name
EL_INIT = 0x06, // Local defined module initializer
EL_MAIN = 0x07, // Main (usually - first) function
EL_TESTING = 0x08, // Testing function
EL_BELOWMAIN= 0x09 // Function evaluated below main function
};
enum ValueType {
VT_void = 0x00,
VT_int = 0x01,
VT_real = 0x02,
VT_char = 0x03,
VT_bool = 0x04,
VT_string = 0x05
};
enum ValueKind {
VK_Plain = 0x00,
VK_In = 0x01,
VK_InOut = 0x02,
VK_Out = 0x03
};
}
#endif
set(SRC
main.cpp
)
set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}")
add_executable(kumir2-run WIN32 ${SRC})
target_link_libraries(kumir2-run)
if (XCODE OR MSVC_IDE)
set_target_properties (kumir2-run PROPERTIES PREFIX "../")
endif(XCODE OR MSVC_IDE)
install(TARGETS kumir2-run DESTINATION ${EXEC_DIR})
#include <iostream>
#include <fstream>
#include "stdlib/kumirstdlib.hpp"
#include "vm/variant.hpp"
#include "vm/vm_bytecode.hpp"
#include "vm/vm.hpp"
#if !defined(WIN32) && !defined(_WIN32)
#define LOCALE Kumir::UTF8
#else
#define LOCALE Kumir::CP866
#endif
int usage(const char * programName)
{
std::cerr << "Usage: " << std::endl;
std::cerr << "\t" << programName << " FILENAME.kod | FILENAME.ks [ARG1 [ARG2 ... [ARGn]]]" << std::endl << std::endl;
std::cerr << "\tFILENAME.kod\tKumir runtime code file name" << std::endl;
std::cerr << "\tFILENAME.ks\tKumir assembler input file name" << std::endl;
std::cerr << "\tARG1...ARGn\tKumir program main algorithm arguments" << std::endl;
return 127;
}
int main(int argc, char *argv[])
{
// Look at arguments
std::string programName;
std::deque<std::string> args;
bool forceTextForm = false;
for (int i=1; i<argc; i++) {
std::string arg(argv[i]);
static const std::string minuss("-s");
static const std::string minusS("-S");
if (programName.empty()) {
if (arg==minuss || arg==minusS)
forceTextForm = true;
else
programName = arg;
}
else {
args.push_back(arg);
}
}
if (programName.empty())
return usage(argv[0]);
// Load a program
std::ifstream programFile(programName);
if (!programFile.is_open()) {
std::cerr << "Can't open program file: " << programName << std::endl;
return 1;
}
const std::string suffix = programName.substr(programName.length()-2);
bool textForm = suffix=="ks" || forceTextForm;
Bytecode::Data programData;
try {
if (textForm)
Bytecode::bytecodeFromTextStream(programFile, programData);
else
Bytecode::bytecodeFromDataStream(programFile, programData);
}
catch (std::string e) {
std::cerr << "Can't load program file: " << e << std::endl;
return 2;
}
catch (Kumir::String e) {
std::cerr << "Can't load program file: " << Kumir::Coder::encode(LOCALE, e) << std::endl;
return 2;
}
catch (...) {
std::cerr << "Can't load program file" << std::endl;
return 2;
}
programFile.close();
// Prepare runner
VM::KumirVM vm;
vm.setProgram(programData);
vm.reset();
// Main loop
while (vm.hasMoreInstructions()) {
vm.evaluateNextInstruction();
if (Kumir::Core::getError().length()>0) {
static const Kumir::String RUNTIME_ERROR = Kumir::Core::fromUtf8("ОШИБКА ВЫПОЛНЕНИЯ: ");
const Kumir::String message = RUNTIME_ERROR + Kumir::Core::getError();
const std::string localMessage = Kumir::Coder::encode(LOCALE, message);
std::cerr << localMessage << std::endl;
return 10;
}
}
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment