77 lines
1.6 KiB
C++
77 lines
1.6 KiB
C++
//
|
|
// interpreter.hpp
|
|
// curly
|
|
//
|
|
// Created by Bob Polis at 2020-09-05
|
|
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
|
//
|
|
|
|
#ifndef _interpreter_H_
|
|
#define _interpreter_H_
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <map>
|
|
#include <stdexcept>
|
|
#include <sstream>
|
|
|
|
class syntax_error : public std::runtime_error {
|
|
public:
|
|
syntax_error(const std::string& what_arg) : std::runtime_error(what_arg) {}
|
|
};
|
|
|
|
class interpreter {
|
|
public:
|
|
interpreter() = default;
|
|
|
|
std::string eval(std::istream& in, bool& done);
|
|
|
|
private:
|
|
std::vector<std::string> _prog;
|
|
std::vector<std::string> _stack;
|
|
std::map<std::string, int> _labels;
|
|
std::map<std::string, std::string> _vars;
|
|
std::vector<int> _calls;
|
|
std::vector<std::string>::size_type _pc {0};
|
|
|
|
void reset();
|
|
void exec_instruction(const std::string& code, bool& done);
|
|
|
|
// integer operations
|
|
void add();
|
|
void sub();
|
|
void mul();
|
|
void div();
|
|
void mod();
|
|
void abs();
|
|
void neg();
|
|
void inc();
|
|
void dec();
|
|
|
|
// string operations
|
|
void dup();
|
|
void rev();
|
|
void slc();
|
|
void idx();
|
|
void cat();
|
|
void len();
|
|
void rot();
|
|
void enl();
|
|
|
|
// tests & jumps
|
|
void gto();
|
|
void geq();
|
|
void gne();
|
|
void glt();
|
|
void gle();
|
|
void ggt();
|
|
void gge();
|
|
|
|
// subroutines
|
|
void fun();
|
|
void ret();
|
|
};
|
|
|
|
#endif // _interpreter_H_
|