Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed src/a.out
Binary file not shown.
62 changes: 40 additions & 22 deletions src/calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ int main() {
//std::string input = "(((a / 4) + (c / 3))";
//std::string input = "88=mgh\n=\n=x\n=4321\nx=\nx = y =\n88 = mph";
//std::string input = "x = y = 0 + 1 + 2 * 3 - 4 / (5 + 6)\nb=13\n(7 - (b = (b + 5)))\n1 % 114514\n\n1=a\n1+1\n";
std::string input = "(x = 10)\n(y = 57)\n(z = 16)\n((((x = 3) + (y = 5)) + w) + (z = 145))\n((x + y) + z)";
//std::string input = "()+)";
// std::string input = "1==false";
// std::string input = "(1 == 2)";
std::string input = "((6 + (4 / ((1 + 2) - 3))) + 8)\n(z = ((32 * awol) >= 5))\n(((88 + 7) < (4 * true)) & (5 >= 2))";
#endif


Expand All @@ -40,7 +41,11 @@ int main() {
expressions.push_back(s);
}


Scope* globalScope = new Scope();
ParserB::ScopeStack.push(globalScope);
int lineCount = 0;
lineCount = lineCount + 1 -1;
for (std::string expression : expressions)
{

Expand All @@ -60,40 +65,53 @@ int main() {
continue;
}

ParserB::setupExpressionInfix(TokenVector);

// ParserB
Node root;
std::pair<std::pair<int, int>, std::string> errorResult = ParserB::MakeTreeInfix(TokenVector, 0, TokenVector.size() - 2, root);

ParserB::setupExpression(TokenVector);
// std::unique_ptr<ExpressionNode> root;
// std::pair<std::pair<int, int>, std::string> errorResult = ParserB::MakeExpressionTree(TokenVector, 0, TokenVector.size() - 2, root);

std::vector<std::unique_ptr<Node>> flows;
// std::cout << TokenVector.at(TokenVector.size()-2).content << std::endl;
TokenVector.pop_back();
TokenVector.push_back(Token(TokenType::SEMICOLON, ";", 0, 0 ));
TokenVector.push_back(Token(TokenType::END, "", 0, 0 ));

std::pair<std::pair<int, int>, std::string> errorResult = ParserB::HandleTokenVector(TokenVector, 0, TokenVector.size()-2, flows);


if (errorResult.first.first != -1)
{
std::cout << "Unexpected token at line " << errorResult.first.first << " column " << errorResult.first.second << ": " << errorResult.second << std::endl;
continue;
}
ParserB::print(root);
ParserB::print_no_semicolon(flows[0].get());
// ParserB::print(flows[0].get(), 0, false);
std::cout << std::endl;

// Calculate
double result;
std::map<std::string, double> originalVariableMap(ParserB::variableMap);
std::map<std::string, bool> originalVariableInitializedMap(ParserB::variableInitializedMap);
std::string errorMessage = ParserB::calculate(root, result);

if (errorMessage == "114514")
{
std::cout << input << std::endl;
}
Result result;
Scope* originalScope = new Scope(*(ParserB::ScopeStack.top()));

std::string errorMessage = ParserB::calculate(flows[0].get(), result);

if (errorMessage.length() != 0)
{
// Error
std::cout << errorMessage << std::endl;
ParserB::variableMap.swap(originalVariableMap);
ParserB::variableInitializedMap.swap(originalVariableInitializedMap);

continue;
delete ParserB::ScopeStack.top();
ParserB::ScopeStack.pop();
ParserB::ScopeStack.push(originalScope);
}
else
{
// No Error
ParserB::printValue(result);
std::cout << std::endl;
delete originalScope;
}
std::cout << result << std::endl;
}

ParserB::clean();
delete ParserB::ScopeStack.top();
return 0;
}
43 changes: 43 additions & 0 deletions src/dumped/lex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "lib/Lexer.h"

int main() {

#if DEBUG == 0
std::string input = "";
std::string line;

while(!std::cin.eof()) {
char c;
std::cin.get(c);
line = std::string(1, c);
input += line;
}
input = input.substr(0, input.size()-1);
#endif
#if DEBUG == 1

//std::string input = "(* (+ 1 2) 3 (/ 4 5 (- 6 7)))\n(* (+ 1 2) 3 (/ 4 5 (- 6 7)))\n(* (+ 1 2) 3 (/ 4 5 (- 6 7)))";
//std::string input = "(= a b 3)";
//std::string input = "(- (= b (+ b 5)) 7)";
//std::string input = "(* a b)";
//std::string input = "(= foo_ b 3)\n( + b 0 )\n(- (= b (+ b 5)) 7)\n(* foo b)\n";
std::string input = R"((x = 42)
(steps = 0)
while (x > 1) {
(steps = (steps + 1))
if ((x % 2) == 0) {
(x = (x / 2))
}
else {
(x = ((3 * x) + 1))
}
}
print steps)";
//std::string input = "(=(n) 9)";
#endif
std::vector<Token> TokenVector;
std::pair<int, int> errorPair = Token::GenTokenVector(input, TokenVector);

Token::printLexer(TokenVector);
return 0;
}
File renamed without changes.
81 changes: 81 additions & 0 deletions src/format.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "lib/ParserB.h"

int main() {
#if DEBUG == 0
std::string input = "";
std::string line;

while(!std::cin.eof()) {
char c;
std::cin.get(c);
line = std::string(1, c);
input += line;
}
input = input.substr(0, input.size()-1);
#endif
#if DEBUG == 1
std::string input = R"(
(z = 42);
def foo(x, y) {
def square(value) {
return (value * value);
}
print square(((x + y) + z));
}
(z = 108);
(f = foo);
(result = f(1, 2));
if (result != null) {
print result;
}

def p() {
print a;
return;
}
p();
def p() {
return null;
}
)";

#endif
// std::cout << "===================" << std::endl;
// std::cout << input << std::endl;
// std::cout << "===================" << std::endl;
std::vector<Token> TokenVector;
std::pair<int, int> errorPair = Token::GenTokenVector(input, TokenVector);

if (errorPair.first != -1)
{
std::cout << "Syntax error on line " << errorPair.first << " column " << errorPair.second << "." << std::endl;
exit(1);
}
// Token::printLexer(TokenVector);
std::vector<std::unique_ptr<Node>> flows;
std::pair<std::pair<int, int>, std::string> errorResult = ParserB::HandleTokenVector(TokenVector, 0, TokenVector.size()-2, flows);
if (errorResult.first.first != -1)
{
std::cout << "Unexpected token at line " << errorResult.first.first << " column " << errorResult.first.second << ": " << errorResult.second << std::endl;
exit(2);
}

for (int i=0; i < (int)flows.size(); i++)
{
ParserB::print(flows[i].get());
// if (flows[i].get()->value.type == TokenType::ARRAY) {
// ArrayNode* Array = dynamic_cast<ArrayNode*>(flows[i].get());
// if (Array->ArrayContent[2]->value.type == TokenType::NUMBER) {
// std::cout << "yes" << std::endl;
// }
// std::cout << Array->ArrayContent[2]->value << std::endl;
// }
// if (flows[i].get()->value.type == TokenType::VARIABLE) {
// std::cout << flows[i].get()->value.content << std::endl;
// }


std::cout << std::endl;
}
return 0;
}
24 changes: 17 additions & 7 deletions src/lex.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "lib/Lexer.h"

#include "lib/Lexer.h"
int main() {

#if DEBUG == 0
std::string input = "";
std::string line;
Expand All @@ -18,28 +17,39 @@ int main() {
// std::cout << "LOLOLOLOLOLOLOLOLO" << std::endl;
#endif
#if DEBUG == 1

//std::string input = "(* (+ 1 2) 3 (/ 4 5 (- 6 7)))\n(* (+ 1 2) 3 (/ 4 5 (- 6 7)))\n(* (+ 1 2) 3 (/ 4 5 (- 6 7)))";
//std::string input = "(= a b 3)";
//std::string input = "(- (= b (+ b 5)) 7)";
//std::string input = "(* a b)";
//std::string input = "(= foo_ b 3)\n( + b 0 )\n(- (= b (+ b 5)) 7)\n(* foo b)\n";
std::string input = " !===!==>=<-=>=<<>>==^^!=|&<>";
//std::string input = "a\n";
// std::string input = R"((x = 42)
// (steps = 0)
// while (x > 1) {
// (steps = (steps + 1))
// if ((x % 2) == 0) {
// (x = (x / 2))
// }
// else {
// (x = ((3 * x) + 1))
// }
// }
// print steps)";
//std::string input = "(=(n) 9)";
std::string input = "[9]";
#endif

std::vector<Token> TokenVector;
std::pair<int, int> errorPair = Token::GenTokenVector(input, TokenVector);

int errorLine = errorPair.first;
int errorIndex = errorPair.second;

if (errorLine != -1)
{
std::cout << "Syntax error on line " << errorLine << " column " << errorIndex << "." << std::endl;
exit(1);
}

Token::printLexer(TokenVector);
return 0;
}
}
Loading