ceevlang/src_old/lang.cc

67 lines
2.1 KiB
C++
Raw Permalink Normal View History

#include "lang.h"
int read_file(std::deque<std::string> args) {
if (args.empty()) {
std::cout << bg_red(bold("ERROR")) << ": No file input" << std::endl;
return 1;
}
fs::path filename = args[0];
args.pop_front();
std::ifstream file(filename);
if (!file) {
std::cout << bg_red(bold("ERROR")) << ": Could not find file"
<< std::endl;
return 1;
}
std::string line;
while (std::getline(file, line)) {
std::cout << line << std::endl;
}
return 0;
}
int parse_line(std::string line) {
struct parsed_token parsed {
empty_token, empty_token
};
if (int sub = line.find("//"))
line = line.substr(sub);
std::deque<std::string> tokens;
int start = 0;
size_t end = line.find(" ");
while (end != std::string::npos) {
tokens.push_back(line.substr(start, end - start));
start = end + 1;
end = line.find(" ", start);
}
for (std::string& token : tokens) {
try {
Token curr_token = CV_Tokens.at(token);
parsed.lastToken = parsed.currentToken;
if (curr_token == parsed.lastToken &&
curr_token.type == TokenType::SPACE)
continue;
else {
// TODO: Line number handling for better errors
std::cerr << "Invalid token `" << token << "'." << std::endl;
return 1;
}
parsed.currentToken = curr_token;
} catch (std::out_of_range& oor) {
// this is most likely a variable or a string
if (parsed.lastToken.type == TokenType::INTRINSIC &&
parsed.lastToken.intrinsic == Intrinsic::LET) {
VariableToken var{TokenType::VARIABLE, Intrinsic::EMPTY,
Constant::EMPTY, "", token};
parsed.currentToken = var;
var_tokens.push_back(var);
}
// string parsing
std::cerr << "Not implemented. Found `" << token << "'."
<< std::endl;
continue;
}
}
return 0;
}