Compare commits
21
Commits
0777faa11d
..
lab2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
79a96aea77 | ||
|
|
ab4afb48e7 | ||
|
|
b15ae562fd | ||
|
|
0329ed8326 | ||
|
|
4c26353876 | ||
|
|
360628d65c | ||
|
|
e837671d15 | ||
|
|
10e0a487ab | ||
|
|
1a258cd051 | ||
|
|
f430b3616a | ||
|
|
ddcf261af7 | ||
|
|
89335fbdc6 | ||
|
|
1b4693029f | ||
|
|
e8c2293b50 | ||
|
|
0d39290709 | ||
|
|
84d28d3845 | ||
|
|
433ee2bb2e | ||
|
|
35e38127e7 | ||
|
|
1d056a736f | ||
|
|
a94e584f35 | ||
|
|
d97160e6ed |
+1
-1
@@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
SET(CMAKE_CXX_FLAGS_DEBUG "-g")
|
||||
SET(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wextra -Wpedantic")
|
||||
|
||||
project(pzks)
|
||||
|
||||
|
||||
@@ -4,3 +4,6 @@
|
||||
#define PRINT_DISABLE_PREFIXES 1
|
||||
|
||||
#define MAX_TOKEN_STRING_LEN 256
|
||||
|
||||
#define LAB2_EXCLUDE_COMPTIME_OP_WRITES 1
|
||||
#define LAB2_PREVENT_NON_FOLDED_DIV_BY_ZERO 1
|
||||
|
||||
+58
-2
@@ -52,6 +52,7 @@ public:
|
||||
}
|
||||
|
||||
explicit TokenValue(void)
|
||||
: s(std::shared_ptr<std::string>())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -144,6 +145,7 @@ public:
|
||||
switch (t)
|
||||
{
|
||||
case VAR:
|
||||
new(&v.s) std::shared_ptr<std::string>;
|
||||
v.s = other.v.s;
|
||||
break;
|
||||
case CONST:
|
||||
@@ -174,7 +176,6 @@ public:
|
||||
return v.s;
|
||||
case CONST:
|
||||
{
|
||||
char buffer[128] = {0};
|
||||
return std::make_shared<std::string>((std::ostringstream() << v.d).str());
|
||||
}
|
||||
case OPERATOR:
|
||||
@@ -192,11 +193,14 @@ public:
|
||||
return v.s;
|
||||
case CONST:
|
||||
{
|
||||
char buffer[128] = {0};
|
||||
return std::make_shared<std::string>((std::ostringstream() << v.d).str());
|
||||
}
|
||||
case OPERATOR:
|
||||
return std::make_shared<std::string>(op_math_str());
|
||||
case CV:
|
||||
return std::make_shared<std::string>((std::ostringstream() << "t" << v.cv.i).str());
|
||||
case NAT:
|
||||
return std::make_shared<std::string>("");
|
||||
default:
|
||||
return std::make_shared<std::string>("Not a valid token!");
|
||||
}
|
||||
@@ -272,6 +276,58 @@ public:
|
||||
return "Invalid operator";
|
||||
}
|
||||
}
|
||||
|
||||
int negative_operator(void) const
|
||||
{
|
||||
if (t != OPERATOR)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (v.o == SUB || v.o == DIV);
|
||||
}
|
||||
|
||||
static int is_negative(Operator o)
|
||||
{
|
||||
return (o == SUB || o == DIV);
|
||||
}
|
||||
|
||||
int is_math_op(void) const
|
||||
{
|
||||
if (t != OPERATOR)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (v.o)
|
||||
{
|
||||
case OPA:
|
||||
case CPA:
|
||||
return 0;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int is_opa(void) const
|
||||
{
|
||||
if (t != OPERATOR)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return v.o == OPA;
|
||||
}
|
||||
|
||||
int is_cpa(void) const
|
||||
{
|
||||
if (t != OPERATOR)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return v.o == CPA;
|
||||
}
|
||||
};
|
||||
|
||||
struct Operation
|
||||
|
||||
@@ -9,5 +9,6 @@ std::vector<Expression::Token> tokenize_input(const char * const input_string);
|
||||
int assert_same(const std::vector<Expression::Token>& a, const std::vector<Expression::Token>& b);
|
||||
void print_token_vector(const std::vector<Expression::Token>& tv);
|
||||
void print_math_vector(const std::vector<Expression::Token>& tv);
|
||||
void print_math_vector(const std::vector<Expression::Token>& tv, const std::vector<int>& highlight_locations);
|
||||
|
||||
}
|
||||
|
||||
+1
-2
@@ -6,8 +6,7 @@
|
||||
namespace Syntax
|
||||
{
|
||||
|
||||
int check_syntax(std::vector<Expression::Token>& tv);
|
||||
void apply_corrections(std::vector<Expression::Token>& tv);
|
||||
int prepare_token_vector(std::vector<Expression::Token>& tv);
|
||||
std::vector<Expression::Operation> build_op_vector(const std::vector<Expression::Token>& tv);
|
||||
Expression::TreeNode ov_to_tree(const std::vector<Expression::Token> tv, const std::vector<Expression::Operation>& ov);
|
||||
void print_op_tree(const std::vector<Expression::Token> tv, const std::vector<Expression::Operation>& ov);
|
||||
|
||||
+1
-3
@@ -10,13 +10,11 @@ int main(int argc, char **argv)
|
||||
|
||||
std::vector<Expression::Token> tv = Parser::tokenize_input(argv[1]);
|
||||
|
||||
if (Syntax::check_syntax(tv))
|
||||
if (Syntax::prepare_token_vector(tv))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
Syntax::apply_corrections(tv);
|
||||
|
||||
std::vector<Expression::Operation> ov = Syntax::build_op_vector(tv);
|
||||
|
||||
Expression::TreeNode root = Syntax::ov_to_tree(tv, ov);
|
||||
|
||||
+29
-4
@@ -35,8 +35,6 @@ class Parser
|
||||
|
||||
int switch_state(enum State e)
|
||||
{
|
||||
pr_debug("Leaving " << state_str(s) << ", entering " << state_str(e));
|
||||
|
||||
int cumulative_backtrack = 0;
|
||||
|
||||
switch (s)
|
||||
@@ -79,8 +77,6 @@ class Parser
|
||||
|
||||
s = e;
|
||||
|
||||
pr_debug("Cumulative backtrack is " << cumulative_backtrack);
|
||||
|
||||
return cumulative_backtrack;
|
||||
}
|
||||
|
||||
@@ -397,4 +393,33 @@ void print_math_vector(const std::vector<Expression::Token>& tv)
|
||||
}
|
||||
}
|
||||
|
||||
void print_math_vector(const std::vector<Expression::Token>& tv, const std::vector<int>& highlight_locations)
|
||||
{
|
||||
int i, l, h, hl;
|
||||
for (i = 0, l = tv.size() - 1, h = 0, hl = highlight_locations.size(); i < l; i++)
|
||||
{
|
||||
if (h < hl && highlight_locations[h] == i)
|
||||
{
|
||||
std::cout << "\033[31m" << *tv[i].to_math_str() << "\033[0m";
|
||||
h++;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << *tv[i].to_math_str();;
|
||||
}
|
||||
}
|
||||
|
||||
if (tv.size())
|
||||
{
|
||||
if (highlight_locations[highlight_locations.size()-1] == (long) (tv.size() - 1))
|
||||
{
|
||||
std::cout << "\033[31m" << *tv[tv.size() - 1].to_math_str() << "\033[0m\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << *tv[tv.size() - 1].to_math_str() << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1051
-480
File diff suppressed because it is too large
Load Diff
+4
-1
@@ -99,7 +99,10 @@ TEST(tree_proper_test_1)
|
||||
TEST(tree_proper_test_2)
|
||||
{
|
||||
std::vector<Expression::Token> o = Parser::tokenize_input("(1**5 * (1-2-3-4-5-6-7-8))");
|
||||
Syntax::apply_corrections(o);
|
||||
if (Syntax::prepare_token_vector(o))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
std::vector<Expression::Operation> ov = Syntax::build_op_vector(o);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user