Tensorium
Loading...
Searching...
No Matches
AST.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <vector>
5#include <unordered_map>
6#include <iostream>
7#include <memory>
8#include "Latex.hpp"
9
10namespace tensorium {
11
12 enum class ASTNodeType {
13 Number,
14 Symbol,
16 UnaryOp,
23 };
24
25 struct ASTNode {
27 std::string value;
28 std::vector<std::shared_ptr<ASTNode>> children;
29
30 ASTNode(ASTNodeType type, const std::string& val = "")
31 : type(type), value(val) {}
32
33 ASTNode(ASTNodeType type, const std::string& val, const std::vector<std::shared_ptr<ASTNode>>& childs)
35 };
36
37 class Parser {
38 public:
39 explicit Parser(const std::vector<Token>& tokens) : tokens(tokens), pos(0) {}
40
41 std::shared_ptr<ASTNode> parse_expression();
42 std::shared_ptr<ASTNode> parse_expression(int precedence = 0) {
43 auto lhs = parse_primary();
44 if (!lhs) return nullptr;
46 }
47
48 private:
49 std::vector<Token> tokens;
50 size_t pos;
51
52 std::shared_ptr<ASTNode> parse_primary();
53 std::shared_ptr<ASTNode> parse_binary_rhs(int prec, std::shared_ptr<ASTNode> lhs);
54
55 Token peek() const { return tokens[pos]; }
56 Token get() { return tokens[pos++]; }
57 bool eof() const { return pos >= tokens.size(); }
58
60 switch (t) {
61 case TokenType::plus:
63 return 10;
64 case TokenType::mult:
65 case TokenType::div:
66 return 20;
67 case TokenType::pow:
68 return 30;
69 default:
70 return -1;
71 }
72 }
73 };
74
75 inline void print_ast(const std::shared_ptr<ASTNode>& node, int indent = 0) {
76 if (!node) return;
77 std::string pad(indent, ' ');
78 std::cout << pad << "Node: " << node->value << " [Type=" << static_cast<int>(node->type) << "]\n";
79 for (const auto& child : node->children)
81 }
82
83}
TokenType
Definition Latex.hpp:8
Definition AST.hpp:37
bool eof() const
Definition AST.hpp:57
Parser(const std::vector< Token > &tokens)
Definition AST.hpp:39
std::vector< Token > tokens
Definition AST.hpp:49
std::shared_ptr< ASTNode > parse_expression(int precedence=0)
Definition AST.hpp:42
std::shared_ptr< ASTNode > parse_binary_rhs(int prec, std::shared_ptr< ASTNode > lhs)
Token peek() const
Definition AST.hpp:55
int get_precedence(TokenType t) const
Definition AST.hpp:59
size_t pos
Definition AST.hpp:50
std::shared_ptr< ASTNode > parse_expression()
std::shared_ptr< ASTNode > parse_primary()
Token get()
Definition AST.hpp:56
Multi-dimensional tensor class with fixed rank and SIMD support.
Definition Tensor.hpp:25
Definition Derivate.hpp:24
void print_ast(const std::shared_ptr< ASTNode > &node, int indent=0)
Definition AST.hpp:75
ASTNodeType
Definition AST.hpp:12
Definition Latex.hpp:35
Definition AST.hpp:25
std::string value
Definition AST.hpp:27
ASTNode(ASTNodeType type, const std::string &val="")
Definition AST.hpp:30
std::vector< std::shared_ptr< ASTNode > > children
Definition AST.hpp:28
ASTNode(ASTNodeType type, const std::string &val, const std::vector< std::shared_ptr< ASTNode > > &childs)
Definition AST.hpp:33
ASTNodeType type
Definition AST.hpp:26