BenchCookie  0.0.1
A simple, intuitive open source language.
command.h
Go to the documentation of this file.
1 #ifndef command_h
2 #define command_h
3 #include <functional>
4 #include <algorithm>
5 #include <typeinfo>
6 #include <iostream>
7 #include <fstream>
8 #include <vector>
9 #include <string>
10 #include <memory>
11 #include <queue>
12 #include <stack>
13 // For the forks
14 #include <unistd.h>
15 #include <string.h>
16 
17 struct func {
18  func(std::string _name = std::string("main")) {
19  m_name = _name;
20  }
21 
22  std::string m_name;
23  std::vector<func> m_calls;
24 };
25 
26 struct string_var {
27  ssize_t cmd_num = -1;
28  std::string m_name;
29 };
30 
31 struct int_assign {
32  std::string m_name;
33  int new_value;
34 };
35 
36 struct string_assign {
37  std::string m_name;
38  std::string new_value;
39 };
40 
42  int int_arg;
43  double double_arg;
44  char char_arg;
45  std::string pirate_name;
46  int expr_type = -1;
47 };
48 
50  ssize_t vardex;
51  ssize_t pointdex;
52  int var_type;
53 };
54 
55 class Command
56 {
57 public:
58  Command(){};
59 
64 
68 
70 
71  void addPrintInt(char * arg);
72  void addPrintString(char * arg);
73  void addPrintLiteral(char * arg);
74  void addToArgList(char *, char *);
75  void addLiteral(char * arg);
76  void addToReturnList(char *);
77 
78  void addReadInt(char * arg);
79  void addReadString(char * arg);
80  void addReadLine(char * arg);
81 
82  void declDouble(char * arg);
83  void declString(char * arg);
84  void declBool(char * arg);
85  void declInt(char * arg);
86  void declPointer(char * dom, char * cod);
87 
88  void addIntAssignment(char * arg);
89  void addToExpressionStack(char * arg);
90 
91  void markFunctionEnding();
92  void markEndOfExpression();
93  void markForLoop(char * arg);
94  void markEndFor();
95 
96  void startFunctionBody(char * arg);
97  void markEndOfFunction();
98 
99  void setFunctionName(char * arg);
100 
101  void setFilename(const std::string & _filename)
102  { m_filename = _filename; }
103 
104  void setCFunc(const bool & _is_c_callable)
105  { m_is_c_callable = _is_c_callable; }
106 
107  void writeAssembly();
108 
109  void startIfBlock();
110  void endIfBlock();
111 
112  const bool & isCCallable(){ return m_is_c_callable; }
113 
114  static std::string current_string;
115  static Command cmd;
116  static ssize_t line_num;
117 
118  void exitWithBug() {
119  std::cerr<<"Something is very wrong..."<<std::endl;
120  std::cerr<<"Please file a full bug report: go to "<<std::endl;
121  std::cerr<<"\thttps:/github.com/allenh1/BenchCookieCompiler"<<std::endl;
122  std::cerr<<"and file an issue request including your full source"<<std::endl;
123  std::cerr<<"as well as compiler generated source, if applicable."<<std::endl;
124  exit(69);
125  }
126 
127 private:
128  void doBSS(std::ostream & file);
129  void push_variable(std::string var_name, std::stack<unsigned short> & vartypes, std::ostream & file);
130  void doData(std::ostream & file);
131  void doMain(std::ostream & file);
132 
133  void evaluate_expression(std::ostream & file);
134 
135  var_type getVarType(std::string varname);
136  std::string m_filename;
137 
138  std::vector<std::string> m_string_vars;
139  std::vector<std::string> m_int_vars;
140  std::vector<std::string> m_literals;
141 
142  func m_main;
143 
144  std::string m_function_name;
145 
146  std::vector<std::string> m_print_strings;
147  std::queue<ssize_t> m_exp_literals;
148  std::vector<std::string> m_print_ints;
149  std::vector<std::string> m_print_bools;
150  std::vector<std::string> m_lines;
151  std::vector<std::string> m_int_assigns;
152  std::vector<std::string> m_bool_assigns;
153  std::vector<std::string> m_string_assigns;
154  std::vector<pointer_assignment> m_pointer_assigns;
155  std::vector<std::string> m_if_deps;
156  std::vector<std::string> m_for_deps;
157  std::vector<std::string> m_bool_vars;
158  std::vector<std::string> m_fileContents;
159  std::vector<std::string> m_int_declarations;
160  std::vector<std::string> m_string_declarations;
161  std::vector<std::string> m_int_pointers;
162  std::vector<std::string> m_char_pointers;
163  std::vector<cmd_type> m_execOrder;
164  std::stack<math_expression> m_current_stack;
165  std::vector<std::string> m_current_args;
166  std::vector<std::string> m_current_returns;
167  std::vector<std::stack<math_expression> *> m_evaluations;
168 
169  bool m_is_c_callable = false;
170 };
171 #endif
void markEndOfFunction()
Mark the end of a function.
Definition: command.cc:233
void addToExpressionStack(char *arg)
Add to expression stack.
Definition: command.cc:258
void addReadInt(char *arg)
Add a command to read an integer.
Definition: command.cc:67
std::string m_name
Definition: command.h:22
char char_arg
Definition: command.h:44
void endIfBlock()
Mark the end of an if block.
Definition: command.cc:212
std::vector< func > m_calls
Definition: command.h:23
ssize_t vardex
Definition: command.h:50
void markEndOfExpression()
Mark the end of an expression.
Definition: command.cc:114
void addPrintInt(char *arg)
Add a command to print a number.
Definition: command.cc:18
void startFunctionBody(char *arg)
Start a function&#39;s body.
Definition: command.cc:196
void markEndFor()
Mark the end of a for loop.
Definition: command.cc:228
std::string new_value
Definition: command.h:38
void addReadLine(char *arg)
Add a read-line command.
Definition: command.cc:78
std::string m_name
Definition: command.h:32
static Command cmd
Definition: command.h:115
void addToReturnList(char *)
Add a variable to the current function&#39;s return list.
Definition: command.cc:240
std::string pirate_name
Definition: command.h:45
const bool & isCCallable()
Definition: command.h:112
ssize_t pointdex
Definition: command.h:51
void declPointer(char *dom, char *cod)
Declare a pointer.
Definition: command.cc:153
void setFilename(const std::string &_filename)
Definition: command.h:101
double double_arg
Definition: command.h:43
void addIntAssignment(char *arg)
Add int assignment command.
Definition: command.cc:340
cmd_type
Definition: command.h:60
void declDouble(char *arg)
void addLiteral(char *arg)
Add a literal to be printed.
Definition: command.cc:56
int new_value
Definition: command.h:33
void addToArgList(char *, char *)
Adds a variable to a function&#39;s argument list.
Definition: command.cc:92
void addPrintLiteral(char *arg)
Add a literal to be printed.
Definition: command.cc:41
void declBool(char *arg)
var_type
Definition: command.h:69
void addReadString(char *arg)
Add a read string command.
Definition: command.cc:326
void markForLoop(char *arg)
Mark the beginning of a for loop.
Definition: command.cc:219
Definition: command.h:17
void startIfBlock()
Mark the beginning of an if block.
Definition: command.cc:204
void setCFunc(const bool &_is_c_callable)
Definition: command.h:104
void exitWithBug()
Definition: command.h:118
ssize_t cmd_num
Definition: command.h:27
func(std::string _name=std::string("main"))
Definition: command.h:18
Command()
Definition: command.h:58
static ssize_t line_num
Definition: command.h:116
void markFunctionEnding()
void declString(char *arg)
Declare a string variable.
Definition: command.cc:137
void writeAssembly()
exp_type
Definition: command.h:65
void declInt(char *arg)
Declare an integer variable.
Definition: command.cc:125
void setFunctionName(char *arg)
Set the current function&#39;s name.
Definition: command.cc:250
void addPrintString(char *arg)
Add a string to be printed.
Definition: command.cc:30
std::string m_name
Definition: command.h:28
std::string m_name
Definition: command.h:37
static std::string current_string
Definition: command.h:114