Skip to content

Commit b56b9fa

Browse files
committed
ast -> stack, stack -> ast
1 parent 8543b5f commit b56b9fa

9 files changed

Lines changed: 166 additions & 9 deletions

File tree

‎src/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set(SOURCE_FILES
22
"graphics/graphics.cpp"
33
"graphics/graphics.h"
4-
"types/types.h" "parser/tokenizer.h" "parser/checker.h" "parser/rpn.h" "parser/parser.h" "parser/checker.cpp" "parser/parser.cpp" "parser/rpn.cpp" "parser/tokenizer.cpp" "parser/simplifier.h" "parser/simplifier.cpp" "transformer/constant.h" "transformer/constant.cpp" "preprocessor/preprocessor.h" "preprocessor/string_builder.h" "transformer/operator.h" "transformer/operator.cpp" "transformer/transformer.h" "transformer/transformer.cpp" "preprocessor/string_builder.cpp" "preprocessor/preprocessor.cpp" "interactions/interactions.h" "interactions/interactions.cpp" "parser/validator.cpp" "parser/validator.h" "graphics/ui_init.h" "graphics/ui_init.cpp" "graphics/ui.h" "graphics/ui.cpp" "types/type_mapper.h" "types/type_mapper.cpp" "compiler/compiler_shader.h" "compiler/compiler_shader.cpp")
4+
"types/types.h" "parser/tokenizer.h" "parser/checker.h" "parser/rpn.h" "parser/parser.h" "parser/checker.cpp" "parser/parser.cpp" "parser/rpn.cpp" "parser/tokenizer.cpp" "parser/simplifier.h" "parser/simplifier.cpp" "transformer/constant.h" "transformer/constant.cpp" "preprocessor/preprocessor.h" "preprocessor/string_builder.h" "transformer/operator.h" "transformer/operator.cpp" "transformer/transformer.h" "transformer/transformer.cpp" "preprocessor/string_builder.cpp" "preprocessor/preprocessor.cpp" "interactions/interactions.h" "interactions/interactions.cpp" "parser/validator.cpp" "parser/validator.h" "graphics/ui_init.h" "graphics/ui_init.cpp" "graphics/ui.h" "graphics/ui.cpp" "types/type_mapper.h" "types/type_mapper.cpp" "compiler/compiler_shader.h" "compiler/compiler_shader.cpp" "transformer/tree.h" "transformer/tree.cpp" "higher_order/derivative.h" "higher_order/higher_order.h")
55

66
add_executable(complex-plotter ${SOURCE_FILES} "main.cpp")
77
find_package(OpenGL REQUIRED)

‎src/higher_order/derivative.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <types/types.h>
2+
#include <higher_order/higher_order.h>
3+
#include <transformer/tree.h>
4+
#include <map>
5+
6+
using std::vector, std::unique_ptr, std::shared_ptr;
7+
8+
void derivative(unique_ptr<AstNode> node) {
9+
10+
}

‎src/higher_order/higher_order.h‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <types/types.h>
2+
#include <parser/parser.h>
3+
#include <transformer/tree.h>
4+
5+
using std::vector, std::string, std::unique_ptr;
6+
7+
8+
unique_ptr<AstNode> clone(const AstNode* node) {
9+
if (!node) return nullptr;
10+
auto new_node = std::make_unique<AstNode>(node->op);
11+
for (const auto& child : node->children) {
12+
new_node->children.push_back(clone(child.get()));
13+
}
14+
return new_node;
15+
}
16+
17+
/// <summary>
18+
/// Builds a syntax subtree expression from a given parser string.
19+
/// Warning: Avoid passing expressions with other higher order functions, and with arity higher than 2.
20+
/// </summary>
21+
/// <param name="expr">The expression to be evaluated, in infix. This follows the same syntax rules as expression passed to the parser.</param>
22+
/// <returns></returns>
23+
AstNode build_expression(const string& expr) {
24+
vector<TokenOperator> stack = parser::parse(expr);
25+
return *stack_to_syntax_tree(stack);
26+
}
27+
28+
29+
30+
unique_ptr<AstNode> substitute(unique_ptr<AstNode> node, const Operator replaced, const AstNode& replacement) {
31+
if (!node) return nullptr;
32+
if (node->op.op == replaced) {
33+
return clone(&replacement);
34+
}
35+
for (auto& child : node->children) {
36+
child = substitute(std::move(node), replaced, replacement);
37+
}
38+
return node;
39+
}
40+
41+
unique_ptr<AstNode> substitute_many(unique_ptr<AstNode> node, const std::map<Operator, const AstNode*>& replacements) {
42+
if (!node) return nullptr;
43+
auto it = replacements.find(node->op.op);
44+
if (it != replacements.end()) {
45+
return clone(it->second);
46+
}
47+
48+
for (auto& child : node->children) {
49+
child = substitute_many(std::move(child), replacements);
50+
}
51+
return node;
52+
}

‎src/parser/parser.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <parser/parser.h>
22
#include <iostream>
3-
vector<TokenOperator> parser::parse(const string& s) {
3+
vector<TokenOperator> parser::parse(const string& s, const bool evaluate_higher_order = true) {
44
vector<TokenOperator> tokens = tokenize(s);
55
tokens = to_rpn(tokens);
66
tokens = simplify(tokens);

‎src/parser/parser.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <parser/validator.h>
66

77
namespace parser {
8-
vector<TokenOperator> parse(const string& s);
8+
vector<TokenOperator> parse(const string& s, const bool b = true);
99
string stack_to_str(const vector<TokenOperator>& stack);
1010
}
1111

‎src/shaders/plotter.frag‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,38 @@ vec3 hsl2rgb(vec3 hsl) {
221221
return hsl.z + hsl.y * (rgb - 0.5) * (1.0 - abs(2.0 * hsl.z - 1.0));
222222
}
223223

224-
vec3 domain_color(in vec2 z){
225-
float angle = atan(z.y,z.x);
226-
float hue = (angle/(2.0 * PI)) + 0.5f;
227-
float light = (TWO_OVER_PI) * atan(length(z));
228-
return vec3(hue,1.0f,light);
224+
float get_discontinuous_light(vec2 f) {
225+
// 1. Safety Clamp (prevents infinity artifacts)
226+
float dist = abs(f.x) > 9e+10 || abs(f.y) > 9e+10 ? 9e+10 : length(f);
227+
228+
// 2. Logarithmic Cycle
229+
// log2 means the pattern repeats every time distance doubles (1, 2, 4, 8...)
230+
float logval = mod(log2(dist), 1.0);
231+
232+
float decimal_exp = 1.0;
233+
if (dist != 0.0) {
234+
// Logic: decimal_exp becomes (1.0 - fractional_part_of_log)
235+
// This creates a "sawtooth" wave for brightness
236+
decimal_exp = -(logval - floor(logval) - 1.0);
237+
}
238+
239+
// 3. Shaping Curve
240+
// pow(..., 0.2) softens the curve (gamma), and -0.15 shifts the darkness
241+
return mod(1.0 / (pow(decimal_exp, 0.2) + 1.0) - 0.15, 1.0);
242+
}
243+
244+
// Main Domain Coloring Function
245+
vec3 domain_color(in vec2 z) {
246+
// 1. Hue (Standard Phase)
247+
float hue = atan(z.y, z.x) / (2.0 * PI);
248+
249+
// 2. Saturation (Keep distinct)
250+
float sat = 1.0;
251+
252+
// 3. Lightness (Using your discontinuous logic)
253+
float light = get_discontinuous_light(z);
254+
255+
return vec3(hue, sat, light);
229256
}
230257

231258
vec2 convert_coordinates(in vec2 pos, in vec2 resolution, in float range){

‎src/transformer/tree.cpp‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <transformer/tree.h>
2+
3+
unique_ptr<AstNode> stack_to_syntax_tree(const vector<TokenOperator>& stack) {
4+
std::stack<unique_ptr<AstNode>> operator_stack;
5+
for (const TokenOperator& op : stack) {
6+
auto current_node = std::make_unique<AstNode>(op);
7+
if (op.arity == Arity::NULLARY) {
8+
operator_stack.push(current_node);
9+
continue;
10+
}
11+
unsigned int arity_val = static_cast<unsigned int>(op.arity);
12+
AstNode curr = { op };
13+
vector<unique_ptr<AstNode>> temp_children;
14+
while (arity_val--) {
15+
temp_children.push_back(std::move(operator_stack.top()));
16+
operator_stack.pop();
17+
}
18+
// 2 3 ^ is 3 ^ 2, not 2 ^ 3
19+
std::reverse(temp_children.begin(), temp_children.end());
20+
for (const auto& child : temp_children) {
21+
current_node->children.push_back(std::move(child));
22+
}
23+
operator_stack.push(std::move(current_node));
24+
}
25+
if (operator_stack.size() != 1) {
26+
throw std::runtime_error("Malformed expression");
27+
}
28+
return std::move(operator_stack.top());
29+
}
30+
31+
void traverse(const AstNode* node, vector<TokenOperator>& result_stack) {
32+
if (!node) return;
33+
for (const auto& child : node->children) {
34+
traverse(child.get(), result_stack);
35+
}
36+
result_stack.push_back(node->op);
37+
}
38+
39+
vector<TokenOperator> syntax_tree_to_stack(const unique_ptr<AstNode> head) {
40+
if (!head) {
41+
throw std::runtime_error("Syntax tree is empty");
42+
}
43+
vector<TokenOperator> stack;
44+
traverse(head.get(), stack);
45+
return stack;
46+
}

‎src/transformer/tree.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <vector>
2+
#include <stack>
3+
#include <memory>
4+
#include <types/types.h>
5+
#include <stdexcept>
6+
7+
using std::vector, std::unique_ptr;
8+
9+
struct AstNode {
10+
TokenOperator op;
11+
vector<unique_ptr<AstNode>> children;
12+
};
13+
14+
unique_ptr<AstNode> stack_to_syntax_tree(const vector<TokenOperator>& stack);
15+
16+
void traverse(const AstNode* node, vector<TokenOperator>& result_stack);
17+
18+
vector<TokenOperator> syntax_tree_to_stack(const unique_ptr<AstNode> head);

‎src/types/types.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ enum Operator{
3434
COMMA,
3535
SIN,
3636
COS,
37-
TAN
37+
TAN,
38+
DERIVATIVE
3839
};
3940

4041
typedef struct TokenOperator {
@@ -44,6 +45,7 @@ typedef struct TokenOperator {
4445
std::string str_repr = "";
4546
unsigned int precedence = 0u;
4647
glm::vec2 value = glm::vec2(0.0f);
48+
bool higher_order = false;
4749
} TokenOperator;
4850

4951
typedef struct FullOperator {
@@ -77,6 +79,8 @@ inline std::vector<FullOperator> full_operators = {
7779
{ {Arity::NULLARY, Associativity::NONE, Operator::VARIABLET, "t", 0}, 0, "vec2(time,0.0f)", "VARIABLET"},
7880
{ {Arity::PAREN, Associativity::NONE, Operator::LPAREN, "(", 0}, 0, "" },
7981
{ {Arity::PAREN, Associativity::NONE, Operator::RPAREN, ")", 0}, 0, "" },
82+
{ {Arity::UNARY, Associativity::RIGHT, Operator::RPAREN, "derivative", 0,glm::vec2(0.0f),true}, 0, ""},
83+
8084
};
8185

8286

0 commit comments

Comments
 (0)