#include <iostream>
#include "interpret"
using namespace std;
int main(){
translator N("ReadFile");
N.interpret();
return 0;
}
#ifndef __CLASS_INTERPRET__
#define __CLASS_INTERPRET__
#include <fstream>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <new>
#include <cstdlib>
// This is My classes..
#include "vector_/vector.hpp"
#include "Calc/calc.h"
// Function IDX AREA
#define PRINT 0
#define INPUT 1
// Function IDX END AREA
typedef unsigned long long DWORD;
// Interpreter Pattern?
// N_interpert - Function::print, input
class N_interpret{
public:
bool is_command(std::string ic, unsigned int &c_NUM);
bool shell(std::string command);
virtual bool interpret() = 0;
};
class translator : public N_interpret{
private:
std::ifstream FS;
protected:
vector<std::string> buf;
public:
translator() { }
translator(std::string path) {
FS.open(path);
std::string _b;
while(!FS.eof()){
getline(FS, _b);
if(_b.length() != 0)
buf.push_back(_b); // File Getline and Save Vector<string>
}
FS.close();
}
virtual bool interpret();
};
class com_print{
public:
std::string trim(std::string &str);
virtual std::string interpret(std::string str){
if(str[1] == '\"')
return "STRING";
else if(str[1] >= '0' && str[1] <= '9')
return "CALC";
else
return "";
}
};
class print_str : public com_print{
public:
virtual std::string interpret(std::string str){
std::string arg;
Calculator<std::string> *calc = new Calculator<std::string>(str, STRING);
arg = calc->in2post();
arg = calc->calc(arg);
std::cout << arg << std::endl;
return arg;
}
};
// Perpect Calculator! XD But.. type_string is not working this situation..
class print_calc : public com_print{
public:
std::string interpret(std::string str){
std::string post;
std::stringstream ss;
DWORD ret;
Calculator<long long> *calc = new Calculator<long long>(trim(str));
post = calc->in2post();
ret = calc->calc(post);
ss << ret;
std::cout << ret << std::endl;
return ss.str();
}
};
class print_func : public com_print{
public:
std::string interpret(std::string str){
return str;
}
};
class print_ptr : public com_print{
public:
std::string interpret(std::string str){
return str;
}
};
bool N_interpret::is_command(std::string ic, unsigned int &c_NUM){
if(ic == "print"){
c_NUM = PRINT;
return true;
}
else
c_NUM = 9505;
return false;
}
bool N_interpret::shell(std::string command){
std::string arg, com;
char *_command = (char *)(command.c_str());
unsigned int NUM, i;
while(*_command){
if(*_command != ' '){
com += *_command;
_command++;
}
else
break;
}
// Check command list.. (#define)
// PRINT : 0
// INPUT : 1
if(is_command(com, NUM)){
switch(NUM){
case PRINT:
com_print *cPrint = new com_print;
std::string option = cPrint->interpret(_command);
if(option == "STRING"){
print_str *strPrint = new print_str;
if(*_command == ' ')
_command++;
strPrint->interpret(_command);
delete strPrint;
}
else if(option == "CALC"){
print_calc *calcPrint = new print_calc;
if(*_command == ' ')
_command++;
calcPrint->interpret(_command);
delete calcPrint;
}
else if(option == "FUNC"){
}
else if(option == "PTR"){
}
delete cPrint;
return true;
}
}
else
std::cout << "UnKnown command... ;(" << std::endl;
return false;
}
bool translator::interpret(){
std::string _b;
vector<std::string>::iterator iter;
for(iter = buf.begin(); iter != buf.end(); iter++){
if(shell(*iter)){
}else break;
}
}
std::string com_print::trim(std::string &str){
std::string ret = "";
int i;
for(i = 0; i < str.length(); i++){
if(str[i] != ' ')
ret += str[i];
}
return ret;
}
#endif
/*
Calculator!!
- KuroNeko
:: Expression ::
Nope..
*/
#include <iostream>
#include <stack>
#include <string>
#include <cstring>
#include "../vector_/vector.hpp"
typedef unsigned long long DWORD;
#define DEFAULT 0
#define STRING 1
template<typename T>
class Calculator{
private:
std::string buf;
public:
Calculator(std::string expr){
if(expr.length() > 0)
buf = trim(expr);
else
buf = "Unknown";
}
Calculator(std::string expr, int type){
if(expr.length() > 0){
if(type == DEFAULT)
buf = trim(expr);
else
buf = expr;
}
else
buf = "Unknown";
}
std::string in2post();
int op_pri(char a){
if(a == '+' || a == '-' || a == ' ')
return 1;
else if(a == '*' || a == '/')
return 2;
else if(a == '(' || a == ')' || a == '\"' || a == '\'')
return 0;
else return -1;
}
T calc(std::string post_expr);
DWORD pow(int a, int b){
DWORD ret = 1, i;
for(i = 0; i < b; i++)
ret *= a;
return ret;
}
std::string trim(std::string &str){
std::string ret = "";
int i;
for(i = 0; i < str.length(); i++){
if(str[i] != ' ')
ret += str[i];
}
return ret;
}
};
template<typename T>
std::string Calculator<T>::in2post(){
if(buf == "Unknown"){
std::cout << "syntax error.." << std::endl;
return "Unknown";
}
int i, k, pri;
char top_op;
std::string ret = "";
std::stack<char> st;
for(i = 0; i < buf.length(); i++){
switch(buf[i]){
case '(': st.push(buf[i]); break;
case ')':
while(st.top() != '('){
ret += st.top();
st.pop();
}
st.pop();
break;
case '+': case '-': case '*': case '/':
while(!st.empty() && op_pri(st.top()) >= op_pri(buf[i])){
ret += st.top();
st.pop();
}
st.push(buf[i]);
st.push(' ');
ret += ' ';
break;
default:
ret += buf[i];
break;
}
}
while(!st.empty()){
ret += st.top();
st.pop();
}
return ret;
}
template<>
std::string Calculator<std::string>::in2post(){
if(buf == "Unknown"){
std::cout << "syntax error.." << std::endl;
return "Unknown";
}
int i, q, cnt = 1;
char top_op;
std::string str, ret = "";
std::stack<char> st;
long long quote = 0;
for(i = 0; i < buf.length(); i++){
switch(buf[i]){
case '\"':
if(buf[i] == '\"'){
ret += '\"';
i++;
while(buf[i] != '\"')
ret += buf[i++];
ret += buf[i];
quote++;
}
else if(quote % 2 + 1 == 2) ret += buf[i];
break;
case '+':
while(!st.empty() && op_pri(st.top()) >= op_pri(buf[i])){
ret += st.top();
st.pop();
}
st.push(buf[i]);
break;
}
}
while(!st.empty()){
ret += st.top();
st.pop();
}
return ret;
}
template<typename T>
T Calculator<T>::calc(std::string post_expr){
if(post_expr != "Unknown" || buf != "Unknown"){
std::stack<T> st;
std::string str;
DWORD i = 0, cnt = 0;
T value, op1, op2;
for(i = 0; i < post_expr.length(); i++){
if(post_expr[i] >= '0' && post_expr[i] <= '9'){
str = "";
while(post_expr[i + cnt] != ' '){
cnt++, str+= post_expr[i + cnt - 1];
}
i += cnt;
value = strtol(str.c_str(), NULL, 10);
cnt = 0;
st.push(value);
}
else{
if(post_expr[i] != ' '){
op2 = st.top();
st.pop();
op1 = st.top();
st.pop();
switch(post_expr[i]){
case '+': value = op1 + op2; break;
case '-': value = op1 - op2; break;
case '*': value = op1 * op2; break;
case '/': value = op1 / op2; break;
}
st.push(value);
}
}
}
return st.top();
}
else
std::cout << "syntax error.." << std::endl;
}
template<>
std::string Calculator<std::string>::calc(std::string post_expr){
if(post_expr != "Unknown" || buf != "Unknown"){
std::stack<std::string> st;
std::string str;
DWORD i = 0, cnt = 1;
std::string value, op1, op2;
char *p;
for(i = 0; i < post_expr.length(); i++)
if(post_expr[i] == '\"')
cnt++;
if(cnt % 2 == 2)
return "Error..";
value = std::string(strtok((char *)post_expr.c_str(), "\""));
st.push(value);
while(p = strtok(NULL, "\"")){
if(strchr(p, '+') != NULL){
op2 = st.top();
st.pop();
op1 = st.top();
st.pop();
st.push(op1 + op2);
}
else{
st.push(std::string(p));
}
}
return st.top();
}
else
std::cout << "syntax error.." << std::endl;
}
아직 함수와 포인터 부분을 구현하지 않았지만 나중에 점점 추가할 예정이다.
사용 방법은 아래와 같다.