BenchCookie  0.0.1
A simple, intuitive open source language.
command.cc
Go to the documentation of this file.
1 
7 #include "command.h"
8 
18 void Command::addPrintInt(char * _int)
19 {
20  m_print_ints.push_back(std::string(_int));
21  m_execOrder.push_back(cmd_type::PRINT_NUM);
22 }
23 
30 void Command::addPrintString(char * arg)
31 {
32  m_print_strings.push_back(std::string(arg));
33  m_execOrder.push_back(cmd_type::PRINT_STR);
34 }
35 
41 void Command::addPrintLiteral(char * arg)
42 {
43  m_literals.push_back(std::string(arg));
44  m_execOrder.push_back(cmd_type::PRINT);
45 }
46 
56 void Command::addLiteral(char * arg)
57 {
58  m_literals.push_back(std::string(arg));
59  m_execOrder.push_back(cmd_type::NOPRINT);
60 }
61 
67 void Command::addReadInt(char * arg)
68 {
69  m_int_vars.push_back(std::string(arg));
70  m_execOrder.push_back(cmd_type::READ_INT);
71 }
72 
78 void Command::addReadLine(char * arg)
79 {
80  m_string_vars.push_back(std::string(arg));
81  m_execOrder.push_back(cmd_type::READ_LINE);
82 }
83 
92 void Command::addToArgList(char * type, char * name)
93 {
94  std::string stype = std::string(type);
95  std::string sname = std::string(name);
96 
97  if (stype == std::string("int")) {
98  m_int_vars.push_back(sname);
99  } else if (stype == std::string("pchar")) {
100  m_char_pointers.push_back(sname);
101  } else if (stype == std::string("pint")) {
102  m_int_pointers.push_back(sname);
103  }
104 }
105 
115 {
116  m_evaluations.push_back(new std::stack<math_expression>(m_current_stack));
117  for(;!m_current_stack.empty();m_current_stack.pop());
118 }
119 
125 void Command::declInt(char * arg)
126 {
127  m_int_declarations.push_back(std::string(arg));
128  m_int_vars.push_back(std::string(arg));
129  m_execOrder.push_back(cmd_type::DECL_INT);
130 }
131 
137 void Command::declString(char * arg)
138 {
139  m_string_declarations.push_back(std::string(arg));
140  m_string_vars.push_back(std::string(arg));
141  m_execOrder.push_back(cmd_type::DECL_STRING);
142 }
143 
153 void Command::declPointer(char * dom, char * cod)
154 {
155  std::string point_to = std::string(cod);
156  int x;
157 
158  for (x = 0; x < m_int_vars.size(); ++x) {
159  if (m_int_vars[x] == point_to) break;
160  } if (x != m_int_vars.size()) {
161  m_int_pointers.push_back(std::string(dom));
162  pointer_assignment _push;
163  _push.vardex = x;
164  _push.pointdex = m_int_pointers.size() - 1;
165  _push.var_type = var_type::HINT;
166  m_pointer_assigns.push_back(_push);
167  m_execOrder.push_back(cmd_type::PTRGETS);
168  return;
169  }
170 
171  for (x = 0; x < m_string_vars.size(); ++x) {
172  if (m_string_vars[x] == point_to) break;
173  } if (x != m_string_vars.size()) {
174  m_char_pointers.push_back(std::string(dom));
175  pointer_assignment _push;
176  _push.vardex = x;
177  _push.pointdex = m_char_pointers.size() - 1;
178  _push.var_type = var_type::HCHAR;
179  m_pointer_assigns.push_back(_push);
180  m_execOrder.push_back(cmd_type::PTRGETS);
181  return;
182  }
183 
184  std::cerr<<"Error! Could not resolve pointer target \""<<point_to;
185  std::cerr<<"\" in pointer assignment."<<std::endl;
186  exit(7);
187 }
188 
197 {
198 
199 }
200 
205 {
206  m_execOrder.push_back(cmd_type::BEGIN_IF);
207 }
208 
212 void Command::endIfBlock() { m_execOrder.push_back(cmd_type::END_IF); }
213 
219 void Command::markForLoop(char * arg)
220 {
221  m_for_deps.push_back(std::string(arg));
222  m_execOrder.push_back(cmd_type::BEGIN_FOR);
223 }
224 
228 void Command::markEndFor() { m_execOrder.push_back(cmd_type::END_FOR); }
229 
233 void Command::markEndOfFunction() { m_execOrder.push_back(cmd_type::ENDFUNC); }
234 
240 void Command::addToReturnList(char * arg)
241 {
242  m_current_returns.push_back(arg);
243 }
244 
250 void Command::setFunctionName(char * arg)
251 { m_function_name = std::string(arg); }
252 
259 {
260  std::string as_string(arg);
261  math_expression toPush;
262  bool is_integer;
263 
264  auto p = [&is_integer] (std::string arg) -> bool {
265  return !arg.empty() && std::find_if(arg.begin(),
266  arg.end(), [](char c) { return !std::isdigit(c); }) == arg.end();
267  };
268 
269  if (as_string == "+") {
270  toPush.expr_type = exp_type::ADD;
271  } else if (as_string == "-") {
272  toPush.expr_type = exp_type::SUB;
273  } else if (as_string == "*") {
274  toPush.expr_type = exp_type::MUL;
275  } else if (as_string == "/") {
276  toPush.expr_type = exp_type::DIV;
277  } else if (as_string == "%") {
278  toPush.expr_type = exp_type::MOD;
279  } else if (as_string == "==") {
280  toPush.expr_type = exp_type::EQ;
281  } else if (as_string == "!=") {
282  toPush.expr_type = exp_type::NEQ;
283  } else if (as_string == "<=") {
284  toPush.expr_type = exp_type::LEQ;
285  } else if (as_string == ">=") {
286  toPush.expr_type = exp_type::GEQ;
287  } else if (as_string == ">") {
288  toPush.expr_type = exp_type::GT;
289  } else if (as_string == "<") {
290  toPush.expr_type = exp_type::LT;
291  } else if (as_string == "!") {
292  toPush.expr_type = exp_type::LOGNOT;
293  } else if (as_string == "&&") {
294  toPush.expr_type = exp_type::LOGAND;
295  } else if (as_string == "&") {
296  toPush.expr_type = exp_type::BITAND;
297  } else if (as_string == "|") {
298  toPush.expr_type = exp_type::BITOR;
299  } else if (as_string == "^^") {
300  toPush.expr_type = exp_type::LOGXOR;
301  } else if (as_string == "^") {
302  toPush.expr_type = exp_type::BITXOR;
303  } else if (as_string == "||") {
304  toPush.expr_type = exp_type::LOGOR;
305  } else if (as_string == "deref") {
306  toPush.expr_type = exp_type::PTRDEREF;
307  } else if (as_string == "literal") {
308  toPush.expr_type = exp_type::LITERAL;
309  m_exp_literals.push(m_literals.size() - 1);
310  } else if (p(as_string)) {
311  toPush.expr_type = exp_type::AN_INT;
312  toPush.int_arg = std::stoi(as_string);
313  } else {
314  toPush.expr_type = exp_type::VAR;
315  toPush.pirate_name = as_string;
316  }
317 
318  m_current_stack.push(toPush);
319 }
320 
326 void Command::addReadString(char * arg)
327 {
328  m_string_vars.push_back(std::string(arg));
329  m_execOrder.push_back(cmd_type::READ_STRING);
330 }
331 
340 void Command::addIntAssignment(char * varname) {
341  std::string var(varname);
342  // If we have made it to this point, then yacc has
343  // already done the expression. So, we just mark the top.
344  for (int x = 0; x < m_int_vars.size(); ++x) {
345  if (m_int_vars[x] == var) {
346  m_int_assigns.push_back(var);
347  m_execOrder.push_back(cmd_type::INTGETS);
348  return;
349  }
350  }
351 
352  for (int x = 0; x < m_string_vars.size(); ++x) {
353  if (m_string_vars[x] == var) {
354  m_string_assigns.push_back(var);
355  m_execOrder.push_back(cmd_type::STRINGGETS);
356  return;
357  }
358  }
359 
360  std::cerr<<"Error! Variable name \""<<var<<"\" could not be resolved!\n";
361  exit(3);
362 }
363 
364 #ifdef __arm__
365 #include "arm_compiler.cpp"
366 #elif __X86__
367 #include "x86_compiler.cpp"
368 #else
369 #include "x86_64_compiler.cpp"
370 #endif
371 
372 int yyparse(void);
373 
375 ssize_t Command::line_num = 0;
376 
377 int main(int argc, char ** argv)
378 {
379  if (argc != 2) {
380  std::cerr<<"Invalid arguments!"<<std::endl;
381  std::cerr<<"Usage: bcc output.s"<<std::endl;
382  return 1;
383  }
384  // get the filename
385  Command::cmd.setFilename(std::string(argv[1]));
386 
387  //parse the code
388  yyparse();
389 
390  Command::cmd.writeAssembly();
391 
392  // fork you
393  int ret = fork();
394 
395  if (ret == 0) {
396  const char *exec_args[5];
397 
398  char * output = strndup(argv[1], strstr(argv[1], ".") - argv[1]);
399 
400 
401  exec_args[0] = "gcc";
402 
403  if (Command::cmd.isCCallable()) {
404  // Create a c-callable function.
405  std::string out2 = std::string(output);
406  out2 += std::string(".o");
407  free(output); // output was malloc'd
408  output = strdup(out2.c_str());
409  exec_args[1] = "-c";
410  exec_args[2] = argv[1];
411  exec_args[3] = "-o";
412  exec_args[4] = output;
413  exec_args[5] = NULL;
414  } else {
415  exec_args[1] = argv[1];
416  exec_args[2] = "-o";
417  exec_args[3] = output;
418  exec_args[4] = NULL;
419  }
420  //execute the compile command.
421  execvp(exec_args[0], (char * const *) (exec_args));
422  free(output); // output was malloc'd
423  } else if (ret < 0) {
424  std::cerr<<"WARNING: Failed to fork process for GCC!"<<std::endl;
425  } else {
426  return 0;
427  }
428 }
void addToExpressionStack(char *arg)
Add to expression stack.
Definition: command.cc:258
void markEndOfFunction()
Mark the end of a function.
Definition: command.cc:233
void addReadInt(char *arg)
Add a command to read an integer.
Definition: command.cc:67
void endIfBlock()
Mark the end of an if block.
Definition: command.cc:212
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
void addReadLine(char *arg)
Add a read-line command.
Definition: command.cc:78
static Command cmd
Definition: command.h:115
int yyparse(void)
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
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
void addIntAssignment(char *arg)
Add int assignment command.
Definition: command.cc:340
void addLiteral(char *arg)
Add a literal to be printed.
Definition: command.cc:56
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
int main(int argc, char **argv)
Definition: command.cc:377
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
void startIfBlock()
Mark the beginning of an if block.
Definition: command.cc:204
static ssize_t line_num
Definition: command.h:116
void declString(char *arg)
Declare a string variable.
Definition: command.cc:137
void writeAssembly()
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