Hello, i was solving this -> kata on CodeWars.
I am using a VusialStudio 2019 to write code. When i start this project in VS no erros is presented to me. The program even works correctly for the first test case. But when i am testing it on problem's page it gives me an error:
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000429d24 bp 0x7ffc2f3f72b0 sp 0x7ffc2f3f7150 T1)
==1==The signal is caused by a READ memory access.
==1==Hint: address points to the zero page.
==1==WARNING: invalid path to external symbolizer!
==1==WARNING: Failed to use and restart external symbolizer!
#0 0x429d23 (/workspace/test+0x429d23)
#1 0x42ab1d (/workspace/test+0x42ab1d)
#2 0x429fa3 (/workspace/test+0x429fa3)
#3 0x42e2f2 (/workspace/test+0x42e2f2)
#4 0x42c9ce (/workspace/test+0x42c9ce)
#5 0x42c529 (/workspace/test+0x42c529)
#6 0x42c11b (/workspace/test+0x42c11b)
#7 0x431425 (/workspace/test+0x431425)
#8 0x42a08d (/workspace/test+0x42a08d)
#9 0x7f1b94116bf6 (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)
#10 0x4053e9 (/workspace/test+0x4053e9)
UndefinedBehaviorSanitizer can not provide additional info.
==1==ABORTING
For a while i was turning on and off parts of program to determen where the problem is. I nereved it down to this line
Program::Status_enum Program::ExecuteComand(Comand_Class* ComandToExe)
{
else if (ComandToExe->name == "div")
{
this->Registers[ComandToExe->args[0]] /= GetValue(ComandToExe->args[1]); // error here
}
}
Leter i found out that error was showing up even if i chage line to
this->Registers.begin()->second = 3; // same error
this->Registers["a"] = 2; // same error
It looks to me that any change of value causes it to throw an error. But reaading for example was ok
int a = Registers["a"];
std::cout << a; // will print a right value
So this basikly all i was able to find out by my self. Here is structer of my code.
Basikly my goal is to write a Asmbler intorpritator
// All Comands saved as this class
class Comand_Class
{
public:
std::string name; // for example mov, div, etc.
std::vector<std::string> args; // data for the comand
Comand_Class(std::string Name, std::string arg1 = "", std::string arg2 = "")
{
//..
}
Comand_Class(std::string Name, std::vector<std::string> Args)
{
//..
}
};
// I use this class to store functions cod as a small program itself
class Function_Class
{
public:
std::vector<Comand_Class*> FunctionProgram;
};
// the main class
class Program
{
std::string MSG = "-1"; // for outout
std::unordered_map <std::string, int> Registers; // map in which the problem exsist
std::vector<Comand_Class*> ProgramLines; // Starting code
std::map<std::string,Function_Class*> CallList; // Name of Function and Her code to exe
// .exe is giving me back the status, after executing a Comand_Class line, this how i know what to do next.
enum Status_enum
{
END = 0,
CALL = 1,
JUMP = 2,
RET = 3,
CONTINUE = 10,
ERROR = -1,
};
public:
std::string Start()
{
exe(this -> ProgramLines );
return MSG;
}
int GetValue(std::string arg)
{
if ('a' <= arg[0] && arg[0] <= 'z')
{
//register
return this->Registers[arg];
}
else
{
int a = std::stoi(arg);
return a;
}
}
// not related to the problem,
void preprocesing(std::string program);
Status_enum ExecuteComand(Comand_Class* ComandToExe);
void exe(std::vector<Comand_Class*> ProgramLinesToExe);
~Program()
{
for (unsigned long i = 0; i < ProgramLines.size(); i++)
{
Comand_Class* del = ProgramLines.at(i);
delete del;
}
for (auto it = CallList.begin(); it != CallList.end(); it++)
{
for (unsigned long i = 0; i < it->second->FunctionProgram.size(); i++)
{
Comand_Class* del = it->second->FunctionProgram.at(i);
delete del;
}
}
// add CallList to clear
}
};
// this function is recurseve, Can this be a reson why it brakes?
void Program::exe(std::vector<Comand_Class*> ProgramLinesToExe)
{
Status_enum Status;
unsigned long int SubPC = 0; // Comand to exe
while (SubPC < ProgramLinesToExe.size())
{
Status = ExecuteComand(ProgramLinesToExe[SubPC]);
if (Status != CONTINUE)
{
if(Status == JUMP)
{
exe(CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram);
}
else if (Status == CALL)
{
auto test = CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram;
exe(CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram);
}
else if (Status == RET)
{
return;
}
else if (Status == END)
{
break;
}
else if (Status == ERROR)
{
std::cout << "Unknown comand! " << ProgramLinesToExe[SubPC]->name;
}
}
SubPC++;
}
}
// Untill this function called second time (recursively) all works good
Program::Status_enum Program::ExecuteComand(Comand_Class* ComandToExe)
{
if (ComandToExe->name == "mov")
{
this->Registers[ComandToExe->args[0]] = GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "inc")
{
this->Registers[ComandToExe->args[0]]++;
}
else if (ComandToExe->name == "dec")
{
this->Registers[ComandToExe->args[0]]--;
}
else if (ComandToExe->name == "add")
{
this->Registers[ComandToExe->args[0]] += GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "sub")
{
this->Registers[ComandToExe->args[0]] -= GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "mil")
{
this->Registers[ComandToExe->args[0]] *= GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "div")
{
//this->Registers[ComandToExe->args[0]] /= GetValue(ComandToExe->args[1]); error
}
else if (ComandToExe->name == "cmp")
{
int x = GetValue(ComandToExe->args[0]);
int y = GetValue(ComandToExe->args[1]);
this->CompareFlugs.Equal = (x == y);
this->CompareFlugs.Less = (x < y);
}
else if (ComandToExe->name == "jne") // if the values of the previous cmp command were not equal.
{
if (this->CompareFlugs.Equal == false)
{
return JUMP;
}
}
else if (ComandToExe->name == "je") // if the values of the previous cmp command were equal.
{
if (this->CompareFlugs.Equal == true)
{
return JUMP;
}
}
else if (ComandToExe->name == "jge") // if x was greater or equal than y in the previous cmp command.
{
if (this->CompareFlugs.Less == false)
{
return JUMP;
}
}
else if (ComandToExe->name == "jg") // f x was greater than y in the previous cmp command.
{
if ((this->CompareFlugs.Less == false) && (this->CompareFlugs.Equal == false))
{
return JUMP;
}
}
else if (ComandToExe->name == "jle") // if x was less or equal than y in the previous cmp command.
{
if ((this->CompareFlugs.Less == true) || (this->CompareFlugs.Equal == true))
{
return JUMP;
}
}
else if (ComandToExe->name == "jl") // if x was less than y in the previous cmp command.
{
if (this->CompareFlugs.Less == true)
{
return JUMP;
}
}
else if (ComandToExe->name == "call")
{
return CALL;
}
else if (ComandToExe->name == "ret")
{
return RET;
}
else if (ComandToExe->name == "end")
{
return END;
}
else if (ComandToExe->name == "msg")
{
MSG = "";
for (unsigned long int i = 0; i < ComandToExe->args.size(); i++)
{
if (ComandToExe->args[i][0] == '\'')
{
MSG += ComandToExe->args[i].substr(1);
}
else
{
MSG += std::to_string(GetValue(ComandToExe->args[i]));
}
}
}
else
{
return ERROR;
}
return CONTINUE;
}
That's all. Also i am new to stackoverflow, so if i did somting very bad, do not judge strictly.
And Here all the code, becase i cut parts i think was usless:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <unordered_map>
#include <regex>
class Comand_Class
{
public:
std::string name;
std::vector<std::string> args;
Comand_Class(std::string Name, std::string arg1 = "", std::string arg2 = "")
{
name = Name;
args.push_back(arg1);
if (!arg2.empty())
{
args.push_back(arg2);
}
}
Comand_Class(std::string Name, std::vector<std::string> Args)
{
name = Name;
args = Args;
}
};
class Function_Class
{
public:
//Comand_Class* ReturnControlTo = NULL;
std::vector<Comand_Class*> FunctionProgram;
};
class Program
{
//int PC = 0;
std::string MSG = "-1";
std::unordered_map <std::string, int> Registers;
std::vector<Comand_Class*> ProgramLines;
std::map<std::string,Function_Class*> CallList;
class CompareFlugs_Class
{
public:
bool Less = false;
bool Equal = false;
}CompareFlugs;
enum Status_enum
{
END = 0,
CALL = 1,
JUMP = 2,
RET = 3,
CONTINUE = 10,
ERROR = -1,
};
const std::string AllowedCommands = "mov inc dev add sub mul div cmp";
public:
std::string Start()
{
exe(this -> ProgramLines );
return MSG;
}
int GetValue(std::string arg)
{
if ('a' <= arg[0] && arg[0] <= 'z')
{
//register
return this->Registers[arg];
}
else
{
int a = std::stoi(arg);
return a;
}
}
void preprocesing(std::string program);
Status_enum ExecuteComand(Comand_Class* ComandToExe);
void exe(std::vector<Comand_Class*> ProgramLinesToExe);
~Program()
{
for (unsigned long i = 0; i < ProgramLines.size(); i++)
{
Comand_Class* del = ProgramLines.at(i);
delete del;
}
for (auto it = CallList.begin(); it != CallList.end(); it++)
{
for (unsigned long i = 0; i < it->second->FunctionProgram.size(); i++)
{
Comand_Class* del = it->second->FunctionProgram.at(i);
delete del;
}
}
// add CallList to clear
}
};
void Program::preprocesing(std::string program)
{
std::string read;
Comand_Class*NewProgramLine;
std::istringstream iss(program);
std::smatch matches;
std::regex Comand(";(.*)|call\\s(.*)|(end|ret)|msg(.*)[;]?|(\\w+):|(j\\w{1,2})\\s+(\\w+)|(\\w+)\\s+(\\w),?\\s?(\\d+|\\w)?");
bool WtireToFunction = false;
std::string ActiveLabel;
Function_Class* NewFunction = nullptr;// = new Function_Class;
while (!iss.eof())
{
std::getline(iss, read);
std::regex_search(read, matches, Comand);
/*
if (matches.size() != 0)
std::cout << "Comand is " << matches[0] << "\n";
*/
// parcing
std::string Coment = matches[1];
std::string CallLabel = matches[2];
std::string End = matches[3];
std::string Message = matches[4]; // may contain a comment in the end
std::string Label = matches[5];
std::string JumpComand = matches[6];
std::string JumpLabel = matches[7];
std::string ComandName = matches[8];
std::string arg1 = matches[9];
std::string arg2 = matches[10];
if ((!ComandName.empty()) && (AllowedCommands.find(ComandName) != std::string::npos))
{
//std::cout << "Comand is " << matches[0] << "\n";
std::cout << "Comand is " << ComandName << " " << arg1 << " " << arg2 << "\n";
NewProgramLine = new Comand_Class (ComandName,arg1,arg2);
//NewProgramLine->name = ComandName;
//NewProgramLine->args.push_back(arg1);
if (WtireToFunction == false)
{
this->ProgramLines.push_back(NewProgramLine);
}
else
{
NewFunction->FunctionProgram.push_back(NewProgramLine);
}
}
else if (!JumpComand.empty()) // && (AllowedJumpComands.find(JumpComand) != std::string::npos))
{
std::cout << "Comand is " << JumpComand << " " << JumpLabel << "\n";
NewProgramLine = new Comand_Class(JumpComand, JumpLabel);
}
else if (!CallLabel.empty()) // on call
{
NewProgramLine = new Comand_Class("call", CallLabel);
this->ProgramLines.push_back(NewProgramLine); // add Call to the ProgramLines
}
else if (!Label.empty()) // Generate New SubProgram
{
WtireToFunction = true;
ActiveLabel = Label;
NewFunction = new Function_Class;
this->CallList[ActiveLabel] = NewFunction;
}
else if (!Message.empty())
{
std::regex MessageParcing("('.*)'|,\\s(\\w)");
std::smatch matches;
std::sregex_iterator it(Message.begin(), Message.end(), MessageParcing), end;
std::vector<std::string> args;
while (it!= end)
{
matches = *it;
if (!matches[1].str().empty())
{
args.push_back(matches[1].str());
}
else if (!matches[2].str().empty())
{
args.push_back(matches[2].str());
}
else
{
std::cout << "Error: cannot format msg\n";
}
it++;
}
NewProgramLine = new Comand_Class("msg",args);
this->ProgramLines.push_back(NewProgramLine);
}
else if (!End.empty())
{
if (End == "end")
{
NewProgramLine = new Comand_Class("end");
this->ProgramLines.push_back(NewProgramLine); // add Call to the ProgramLines
}
if (End == "ret")
{
if (NewFunction == nullptr)
std::cout << "No Call Detected, but ret is found\n";
else
{
NewProgramLine = new Comand_Class("ret");
NewFunction->FunctionProgram.push_back(NewProgramLine);
}
}
}
}
}
Program::Status_enum Program::ExecuteComand(Comand_Class* ComandToExe)
{
if (ComandToExe->name == "mov")
{
this->Registers[ComandToExe->args[0]] = GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "inc")
{
this->Registers[ComandToExe->args[0]]++;
}
else if (ComandToExe->name == "dec")
{
this->Registers[ComandToExe->args[0]]--;
}
else if (ComandToExe->name == "add")
{
this->Registers[ComandToExe->args[0]] += GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "sub")
{
this->Registers[ComandToExe->args[0]] -= GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "mil")
{
this->Registers[ComandToExe->args[0]] *= GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "div")
{
int a = GetValue(ComandToExe->args[1]);
auto t = ComandToExe->args[0];
int b = 6 / 2;
this->MSG = "Hi";
this->Registers[t] = b;
this->Registers.begin()->second = 3;
//this->Registers[ComandToExe->args[0]] /= GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "cmp")
{
int x = GetValue(ComandToExe->args[0]);
int y = GetValue(ComandToExe->args[1]);
this->CompareFlugs.Equal = (x == y);
this->CompareFlugs.Less = (x < y);
}
else if (ComandToExe->name == "jne") // if the values of the previous cmp command were not equal.
{
if (this->CompareFlugs.Equal == false)
{
return JUMP;
}
}
else if (ComandToExe->name == "je") // if the values of the previous cmp command were equal.
{
if (this->CompareFlugs.Equal == true)
{
return JUMP;
}
}
else if (ComandToExe->name == "jge") // if x was greater or equal than y in the previous cmp command.
{
if (this->CompareFlugs.Less == false)
{
return JUMP;
}
}
else if (ComandToExe->name == "jg") // f x was greater than y in the previous cmp command.
{
if ((this->CompareFlugs.Less == false) && (this->CompareFlugs.Equal == false))
{
return JUMP;
}
}
else if (ComandToExe->name == "jle") // if x was less or equal than y in the previous cmp command.
{
if ((this->CompareFlugs.Less == true) || (this->CompareFlugs.Equal == true))
{
return JUMP;
}
}
else if (ComandToExe->name == "jl") // if x was less than y in the previous cmp command.
{
if (this->CompareFlugs.Less == true)
{
return JUMP;
}
}
else if (ComandToExe->name == "call")
{
// написать адресс для возврата в структуру с функциями
return CALL;
}
else if (ComandToExe->name == "ret")
{
return RET;
}
else if (ComandToExe->name == "end")
{
return END;
}
else if (ComandToExe->name == "msg")
{
MSG = "";
for (unsigned long int i = 0; i < ComandToExe->args.size(); i++)
{
if (ComandToExe->args[i][0] == '\'')
{
MSG += ComandToExe->args[i].substr(1);
}
else
{
MSG += std::to_string(GetValue(ComandToExe->args[i]));
}
}
}
else
{
return ERROR;
}
return CONTINUE;
}
void Program::exe(std::vector<Comand_Class*> ProgramLinesToExe)
{
Status_enum Status;
unsigned long int SubPC = 0;
while (SubPC < ProgramLinesToExe.size())
{
Status = ExecuteComand(ProgramLinesToExe[SubPC]);
if (Status != CONTINUE)
{
if(Status == JUMP)
{
exe(CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram);
}
else if (Status == CALL)
{
auto test = CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram;
exe(CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram);
}
else if (Status == RET)
{
return;
}
else if (Status == END)
{
break;
}
else if (Status == ERROR)
{
std::cout << "Unknown comand! " << ProgramLinesToExe[SubPC]->name;
}
}
SubPC++;
}
}
std::string assembler_interpreter(std::string program)
{
Program Main;
Main.preprocesing(program);
return Main.Start();
}
Related
I use Google’s dense_hash_map as a cache of some data. When I use the find function to find a key, I find that it doesn’t exist, but when traversing dense_hash_map, I can see that the key exists. Is it wrong where I used it? By the way, I have rewritten the EqualKey class required by dense_hash_map.
This is my test code:
class StringVal {
public:
StringVal(string &str) {
len = str.length();
ptr = new uint8_t[len];
memcpy(ptr, str.c_str(), len);
}
StringVal(uint8_t* ptr = NULL, int len = 0) : len(len), ptr(ptr) {
if(ptr == NULL) {
is_null = NULL;
}
}
StringVal(const char* ptr) : len(strlen(ptr)),ptr(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(ptr))) {
if(ptr == NULL) {
is_null = NULL;
}
}
~StringVal() {
delete[] ptr;
}
uint8_t* ptr;
int len;
bool is_null;
static StringVal null() {
StringVal sv;
sv.is_null = true;
return sv;
}
bool operator==(const StringVal& other) const {
if (is_null != other.is_null) return false;
if (is_null) return true;
if (len != other.len) return false;
return ptr == other.ptr || memcmp(ptr, other.ptr, len) == 0;
}
bool operator!=(const StringVal& other) const { return !(*this == other); }
};
struct NewStringValCmp {
bool operator()(const StringVal* s1, const StringVal* s2) const {
if (s1 == NULL && s2 == NULL) return 1;
if (s1 == NULL || s2 == NULL) return 0;
if (s1->is_null != s2->is_null) return false;
if (s1->is_null) return true;
if (s1->len != s2->len) return false;
return memcmp(s1->ptr, s2->ptr, s1->len) == 0;
}
};
typedef dense_hash_map<StringVal*, StringVal*, std::hash<StringVal*>, NewStringValCmp> new_dict_map_string_t;
vector<string> data = {
"test1","1",
"test2","2",
"test3","3",
"test4","4",
"test5","5",
"test6","6",
"test7","7",
"test8","8",
"test9","9",
"test10","10"
};
int main() {
new_dict_map_string_t* cm = new new_dict_map_string_t();
cm->set_empty_key(NULL);
for (int i = 0; (i + 1) < data.size(); i+=2) {
StringVal *key = new StringVal(data[i]);
StringVal *value = new StringVal(data[i+1]);
(*cm)[key] = value;
}
string input = "";
while (1) {
cin >> input;
if (input == "q") {
break;
}
StringVal *target = new StringVal(input);
for (auto itor: *cm) {
if (*(itor.first) == *target) {
cout << "find target" << endl;
}
}
auto itor = cm->find(target);
if (itor == cm->end()) {
cout << "cm->find nothing" << endl;
} else {
cout << itor->second->ptr << endl;
}
delete target;
input = "";
}
for (auto iter : *cm) {
delete iter.first;
delete iter.second;
}
delete cm;
return 0;
}
I was trying to create a special assembler that will run with a specific machine type. I was trying to compile the program, but got a linker error:
/usr/bin/ld: lexer.o: in function `Lexer::lex(std::__cxx11::basic_string, std::allocator >)':
lexer.cpp:(.text+0x2ee): undefined reference to `Lexer::isSpecial(char)'
collect2: error: ld returned 1 exit status
Screenshot here
I have 3 main files: lexer.h, lexer.cpp, sasm.cpp. Here's the code.
lexer.h:
#ifndef LEXER_H
#define LEXER_H
#include <iostream>
#include <vector>
// type definitions
typedef uint8_t byte;
typedef std::vector<std::string> strings;
enum State : byte {
START,
READCHAR,
READBLOCK,
SKIP,
DUMP,
COMMENT,
END
};
// Instructions definitions
#define ADD 0x40000001
#define SUB 0x40000002
#define TIME 0x40000003
#define DIVIDE 0x40000004
#define HALT 0x40000000
class Lexer {
private:
bool isSpace(char c);
bool isSpecial(char c);
bool isGroup(char c);
char end_char, beg_char;
public:
strings lex(std::string s);
};
#endif
lexer.cpp:
#include "lexer.h"
strings Lexer::lex(std::string s) {
strings strLst;
char lexEme[256];
int i = 0;
int j = 0;
State state = START;
int done = 0;
int len = s.length();
int balance = 0;
while (i < len) {
switch (state) {
case START:
if (isSpace(s[i])) {
state = SKIP;
} else if (isGroup(s[i])) {
if (s[i] == '"') {
lexEme[j] = s[i];
j++;
i++;
}
state = READBLOCK;
} else if (s[i] == '/' && s[i + 1] == '/') {
i += 2;
state = COMMENT;
} else {
state = READCHAR;
}
break;
case READCHAR:
if (isSpace(s[i])) {
state = DUMP;
} else if (s[i] == '\\') {
i += 2;
} else if (isGroup(s[i])) {
if (s[i] == '"') {
lexEme[j] = s[i];
j++;
i++;
}
state = READBLOCK;
} else if (isSpecial(s[i])) {
if (j == 0) {
lexEme[j] = s[i];
j++;
i++;
}
state = DUMP;
} else if (s[i] == '/' && s[i + 1] == '/') {
i += 2;
state = COMMENT;
} else {
lexEme[j] = s[i];
j++;
i++;
}
break;
case READBLOCK:
if (s[i] == beg_char && s[i] != '"') {
balance++;
lexEme[j] = s[i];
j++;
i++;
} else if (s[i] == end_char) {
balance--;
lexEme[j] = s[i];
j++;
i++;
if (balance <= 0) {
state = DUMP;
}
} else if (end_char == '"' && s[i] == '\\') {
// TODO: fix this to actually record the chars
i += 2;
} else {
lexEme[j] = s[i];
j++;
i++;
}
break;
case SKIP:
if (isSpace(s[i])) {
i++;
} else {
state = READCHAR;
}
break;
case DUMP:
if (j < 0) {
lexEme[j] = 0;
strLst.push_back(lexEme);
j = 0;
}
state = START;
break;
case COMMENT:
if (s[i] != '\n') {
i++;
} else {
state = READCHAR;
}
break;
case END:
i = len;
break;
}
}
if (j > 0) {
lexEme[j] = 0;
strLst.push_back(lexEme);
}
return strLst;
}
// This function allows us what a space is
bool Lexer::isSpace(char c) {
switch (c) {
case '\n':
case '\r':
case '\t':
case '\v':
case ' ':
case '\f':
return true;
default:
return false;
}
}
bool Lexer::isGroup(char c) {
beg_char = c;
switch (c) {
case '"':
end_char = '"';
return true;
case '(' :
end_char = ')';
return true;
case ')':
return true;
default:
return false;
}
}
bool isSpecial(char c) {
switch (c) {
case '[':
case ']':
return true;
default:
return false;
}
}
sasm.cpp:
#include <fstream>
#include "lexer.h"
typedef uint32_t i32;
using namespace std;
vector<i32> compile(strings s);
bool isInteger(string s);
bool isPrimitive(string s);
i32 mapToNumber(string s);
int main (int argc, char* argv[]) {
// Check for input errors
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <sasm-file>" << endl;
exit(1);
}
// Read input file
ifstream infile;
infile.open(argv[1]);
if (!infile.is_open()) {
cerr << argv[1] << ": file can't be found" << endl;
exit(1);
}
string line;
string contents;
while (getline(infile, line)) {
contents += line + "\n";
}
infile.close();
// Parse infile
Lexer lexer;
strings lexEmes = lexer.lex(contents);
// Compile binary
vector<i32> instructions = compile(lexEmes);
// Write instructions to file
ofstream ofile;
ofile.open("out.bin", ios::binary);
for (i32 i = 0; i < instructions.size(); i++) {
ofile.write(reinterpret_cast<char*>(&instructions[i]), sizeof(i32));
}
ofile.close();
return 0;
}
vector<i32> compile(strings s) {
vector<i32> instructions;
for (i32 i = 0; i < s.size(); i++) {
if (isInteger(s[i])) {
instructions.push_back(stoi(s[i]));
} else {
i32 instruction = mapToNumber(s[i]);
if (instruction != -1) {
instructions.push_back(instruction);
} else {
cerr << "\033[1;31m" << s[i] << "\033[0m Invalid instruction" << std::endl;
}
}
}
return instructions;
}
bool isInteger(string s) {
for (i32 i = 0; i < s.length(); i++) {
if (!isdigit(s[i])) {
return false;
}
}
return true;
}
i32 mapToNumber(string s) {
if (s == "+") {
return ADD;
} else if (s == "-") {
return SUB;
} else if (s == "*") {
return TIME;
} else if (s == "/") {
return DIVIDE;
}
return -1; // Invalid instruction
}
Thanks for your answers.
You are missing the class reference in the function definition.
bool Lexer::isSpecial(char c)
^^^^^^^
So I am working on some homework for my uni and need to convert a string to a float. For whatever reason g++ is complaining that the 'stof' function doesn't exist. Although I have included the required header. Here is my code, the error is on the line that says
holder = stof(x.substr(0, end_of_num));
#include <iostream>
#include <string>
#include <list>
using namespace std;
float process_func(string x);
bool isPartOfNum(char x);
int main() {
string x;
while (true) {
cout << "input a string" << endl;
getline(cin, x);
cout << process_func(x);
}
return 0;
}
float process_func(string x) {
int end_of_num =0;// used to find last index from num
int negMult = 1; //used to multiply value at end if there was a negative
bool onNum = false; //used to
list <float> numList;
list <char> operList;
if ((x.at(0) < 48 || x.at(0) > 57) && x.at(0) != '-') //check if start of string doesnt have a number or negative symbol
return -1;
if (x.at(0) != '-')
negMult = -1;
float holder;// temp holder for floats
int i = 0;
while (i<x.length()) {
if (isPartOfNum(x.at(i))) {
end_of_num++;
onNum = true;
}
else if (onNum) {
holder = stof(x.substr(0, end_of_num));
numList.push_back(holder); //adds num as float to list
x.erase(0, end_of_num + 1); //+1 removes the space after the number before the operator
end_of_num = 0;
onNum = false;
}
if (x.at(i) == '+' || x.at(i) == '-' || x.at(i) == '*' || x.at(i) == '/') {
operList.push_back(x.at(i));
}
} //at this point both lists should be full of all needed pieces of info
int answer = 0;
int temp;
bool firstOper=true; // used to hold first operation
while (numList.size() >=2) { //requires at least 2 entries for last operation
while (!operList.empty()) {
temp = numList.front();
numList.pop_front();
if (operList.front() == '+') {
if (firstOper) {
answer = temp + numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer += temp;
}
}
else if (operList.front() == '-') {
if (firstOper) {
answer = temp - numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer -= temp;
}
}
else if (operList.front() == '*') {
if (firstOper) {
answer = temp * numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer *= temp;
}
}
else if (operList.front() == '/') {
if (firstOper) {
answer = temp / numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer /= temp;
}
}
operList.pop_front();
}
}
return answer;
}
bool isPartOfNum(char x) {
if ((x >= 48 && x <= 57) || (x == '-' || x == '.'))
return true;
return false;
}
Solved by compiling using c++ 11
I am getting a segmentation fault when trying to convert an infix expression into prefix order. I have a postfix using the same logic that works fine the issue seems to be where i try to read the infix expression in reverse but I am not exactly sure why it is faulting. Any help would greatly appreciated Here is the relevant code:
int ParseTree::prefix(string infix)
{
vector<string> exp;
stringstream ss(infix);
string tok;
while(getline(ss,tok,' '))
{
exp.push_back(tok);
}
vector<string> prefix;
stack<string> s;
for(unsigned int i = exp.size() - 1; i >= 0 ; i--)
{
if(parseTry(exp[i]))
{
prefix.push_back(exp[i]);
}
if(exp[i] == "(")
{
s.push(exp[i]);
}
if(exp[i] == ")")
{
while(!s.empty() && s.top() != "(")
{
prefix.push_back(s.top());
s.pop();
}
s.pop();
}
if(isOperator(exp[i]) == true)
{
while(!s.empty() && priority(s.top()) >= priority(exp[i]))
{
prefix.push_back(s.top());
s.pop();
}
s.push(exp[i]);
}
}
while(!s.empty())
{
prefix.push_back(s.top());
s.pop();
}
for(unsigned int i = 0; i < prefix.size(); i++)
{
cout << prefix[i] << " ";
}
return 0;
}
int ParseTree::priority(const string &s)
{
if(s == "*" || s == "/")
{
return 2;
}
if(s == "+" || s == "-")
{
return 1;
}
else
{
return 0;
}
}
bool ParseTree::parseTry(const string &s)
{
bool number = false;
for(unsigned int i = 0; i < s.size(); i++)
{
if(!isdigit(s[i]))
{
number = false;
}
else
{
number = true;
}
}
return number;
}
bool ParseTree::isOperator(const string &s)
{
return (s == "+" || s == "-" || s == "*" || s == "/");
}
I've been following some tutorials for allegro 5 and Im trying to load a tile map and I keep getting a vector subscript out of range error. I've been trying for so long to get this fixed but I cannot figure it out. The filemanager is supposed to read from a map.txt then go through layer.cpp then actually make the map.
EDIT:
It stops after the 5th loop of contents[i][j]
FileManager.CPP
#include "FileManager.h"
FileManager::FileManager()
{
identifierFound = false;
}
FileManager::~FileManager()
{
}
void FileManager::LoadContent(const char *filename, std::vector<std::vector<std::string>> &attributes, std::vector<std::vector<std::string>> &contents)
{
std::ifstream openFile(filename);
std::string line, newLine;
if(openFile.is_open())
{
while(std::getline(openFile, line))
{
std::stringstream str;
if(line.find("Load=") != std::string::npos)
{
type = LoadType::Attributes;
line = line.erase(0, line.find("=") + 1);
tempAttributes.clear();
}
else
{
type = LoadType::Contents;
tempContents.clear();
}
str << line;
while(std::getline(str, newLine, ']'))
{
newLine.erase(std::remove(newLine.begin(), newLine.end(), '['), newLine.end());
std::string erase = " \t\n\r";
newLine.erase(newLine.find_last_not_of(erase) + 1);
if(type == LoadType::Attributes)
tempAttributes.push_back(newLine);
else
tempContents.push_back(newLine);
std::cout << newLine << std::endl;
}
if(type == LoadType::Contents && tempContents.size() > 0)
{
attributes.push_back(tempAttributes);
contents.push_back(tempContents);
}
}
}
else
{
}
}
void FileManager::LoadContent(const char *filename, std::vector<std::vector<std::string>> &attributes, std::vector<std::vector<std::string>> &contents, std::string identifier)
{
std::ifstream openFile(filename);
std::string line, newLine;
if(openFile.is_open())
{
while(std::getline(openFile, line))
{
std::stringstream str;
if(line.find("EndLoad=") != std::string::npos && line.find(identifier) != std::string::npos)
{
identifierFound = false;
break;
}
else if(line.find("Load=") != std::string::npos && line.find(identifier) != std::string::npos)
{
identifierFound = true;
}
if(identifierFound)
{
if(line.find("Load=") != std::string::npos)
{
type = LoadType::Attributes;
line = line.erase(0, line.find("=") + 1);
tempAttributes.clear();
}
else
{
type = LoadType::Contents;
tempContents.clear();
}
str << line;
while(std::getline(str, newLine, ']'))
{
newLine.erase(std::remove(newLine.begin(), newLine.end(), '['), newLine.end());
std::string erase = " \t\n\r";
newLine.erase(newLine.find_last_not_of(erase) + 1);
if(type == LoadType::Attributes)
tempAttributes.push_back(newLine);
else
tempContents.push_back(newLine);
std::cout << newLine << std::endl;
}
if(type == LoadType::Contents && tempContents.size() > 0)
{
attributes.push_back(tempAttributes);
contents.push_back(tempContents);
}
}
}
}
else
{
std::cout << "Error reading file!" << std::endl;
}
}
Layer.CPP
#include "Layer.h"
Layer::Layer(void)
{
}
Layer::~Layer(void)
{
}
std::pair<int, int> Layer::SetTiles(std::string tileString)
{
std::pair<int, int> tile;
tile.first = atoi(tileString.substr(0, tileString.find(',')).c_str());
tile.second = atoi(tileString.substr(tileString.find(',') + 1).c_str());
return tile;
}
void Layer::LoadContent(std::string layerID, std::string mapID)
{
std::string fileName = "Maps/"+mapID+".txt";
fileManager.LoadContent(fileName.c_str(), attributes, contents, layerID);
int indexY = 0;
for(int i = 0; i < attributes.size(); i++)
{
for(int j = 0; j < contents[i].size(); j++)
{
std::cout << attributes[i][j] << std::endl << std::endl;
std::cout << contents[i][j] << std::endl << std::endl;
if(attributes[i][j] == "SolidTiles")
solidTiles.push_back(SetTiles(contents[i][j]));
else if(attributes[i][j] == "TileSheet")
{
tileSheet = al_load_bitmap(contents[i][j].c_str());
}
else if(attributes[i][j] == "StartLayer")
{
for(int k = 0; k < contents[i].size(); k++)
{
if(contents[i][k] != "---")
{
std::cout << contents[i][k] << std::endl << std::endl;
ALLEGRO_BITMAP *tileImage;
Tile::State tempState = Tile::State::Passive;
std::pair<int, int> tile = SetTiles(contents[i][k]);
if(std::find(solidTiles.begin(), solidTiles.end(), tile) != solidTiles.end())
{
tempState = Tile::State::Solid;
}
tileImage = al_create_sub_bitmap(tileSheet, tile.first * 32, tile.second * 32, 32, 32);
std::pair<float, float> position(k * 32, indexY * 32);
Tile tileInstance;
tiles.push_back(tileInstance);
tiles[tiles.size()-1].SetContent(tileImage, tempState, position);
}
}
indexY++;
}
}
}
}
void Layer::UnloadContent()
{
for(int i = 0; i < tiles.size(); i++)
tiles[i].UnloadContent();
al_destroy_bitmap(tileSheet);
}
void Layer::Update()
{
}
void Layer::Draw(ALLEGRO_DISPLAY *display)
{
for(int i = 0; i < tiles.size(); i++)
tiles[i].Draw(display);
}
map1.txt
Load=[MapProperties]
EndLoad=[MapProperties]
Load=[Layer1]
Load=[SolidTiles]
[2,0]
[1,0]
Load=[TileSheet]
[TileSheets/tilesheet1.png]
Load=[StartLayer]
[2,0][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][2,0][2,0][2,0][---][---][---][---]
[---][---][---][---][---][---][---][2,0][---][---][---][---][---][---][---]
[---][---][---][---][---][---][2,0][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[---][---][2,0][2,0][2,0][---][---][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0]
Load=[EndLayer]
[dummy]
EndLoad=[Layer1]
Load=[PlayerPosition]
[0,0]