Compare commits
25
Commits
65eb6cf0b1
...
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 | ||
|
|
0777faa11d | ||
|
|
8bc359fed6 | ||
|
|
afb4b5de20 | ||
|
|
b365e1d05c |
+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
|
||||
|
||||
+70
-15
@@ -7,18 +7,6 @@
|
||||
|
||||
namespace Expression {
|
||||
|
||||
#if 0
|
||||
namespace TokenType {
|
||||
|
||||
enum TokenType {
|
||||
VAR,
|
||||
CONST,
|
||||
OPERATOR
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
class Token {
|
||||
public:
|
||||
enum TokenType {
|
||||
@@ -64,6 +52,7 @@ public:
|
||||
}
|
||||
|
||||
explicit TokenValue(void)
|
||||
: s(std::shared_ptr<std::string>())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -156,6 +145,7 @@ public:
|
||||
switch (t)
|
||||
{
|
||||
case VAR:
|
||||
new(&v.s) std::shared_ptr<std::string>;
|
||||
v.s = other.v.s;
|
||||
break;
|
||||
case CONST:
|
||||
@@ -186,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:
|
||||
@@ -204,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!");
|
||||
}
|
||||
@@ -284,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
|
||||
@@ -310,7 +354,18 @@ class TreeNode {
|
||||
public:
|
||||
TreeNode *l; // left hand
|
||||
TreeNode *r; // right hand
|
||||
Token o; // operator
|
||||
Token o; // if operator, left and right hands are valid
|
||||
// if value, left and right hands should be null
|
||||
|
||||
explicit TreeNode(void)
|
||||
: l(nullptr), r(nullptr), o()
|
||||
{
|
||||
}
|
||||
|
||||
explicit TreeNode(const Token t)
|
||||
: l(nullptr), r(nullptr), o(t)
|
||||
{
|
||||
}
|
||||
|
||||
~TreeNode()
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
+3
-2
@@ -6,8 +6,9 @@
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -10,12 +10,12 @@ 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);
|
||||
}
|
||||
|
||||
+39
-9
@@ -1,10 +1,9 @@
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "expression.hpp"
|
||||
#include "print.h"
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
namespace Parser {
|
||||
|
||||
@@ -36,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)
|
||||
@@ -80,8 +77,6 @@ class Parser
|
||||
|
||||
s = e;
|
||||
|
||||
pr_debug("Cumulative backtrack is " << cumulative_backtrack);
|
||||
|
||||
return cumulative_backtrack;
|
||||
}
|
||||
|
||||
@@ -378,7 +373,10 @@ void print_token_vector(const std::vector<Expression::Token>& tv)
|
||||
std::cout << "(" << *tv[i].to_str() << ") ";
|
||||
}
|
||||
|
||||
if (tv.size())
|
||||
{
|
||||
std::cout << "(" << *tv[i].to_str() << ")\n";
|
||||
}
|
||||
}
|
||||
|
||||
void print_math_vector(const std::vector<Expression::Token>& tv)
|
||||
@@ -389,7 +387,39 @@ void print_math_vector(const std::vector<Expression::Token>& tv)
|
||||
std::cout << *tv[i].to_math_str(); //<< " ";
|
||||
}
|
||||
|
||||
if (tv.size())
|
||||
{
|
||||
std::cout << *tv[i].to_math_str() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1003
-307
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