Initial commit
This commit is contained in:
		
							
								
								
									
										24674
									
								
								include/json.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24674
									
								
								include/json.hpp
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										26
									
								
								include/lexer_base.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								include/lexer_base.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
#ifndef MCALC_LEXER_BASE_H
 | 
			
		||||
#define MCALC_LEXER_BASE_H
 | 
			
		||||
 | 
			
		||||
#include <string_view>
 | 
			
		||||
 | 
			
		||||
#ifndef yyFlexLexerOnce
 | 
			
		||||
#include <FlexLexer.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <parser.h>
 | 
			
		||||
 | 
			
		||||
struct Scanner : public yyFlexLexer {
 | 
			
		||||
    using value_type = yy::Parser::value_type;
 | 
			
		||||
    using location_type = yy::Parser::location_type;
 | 
			
		||||
 | 
			
		||||
    explicit Scanner(std::istream *in) : yyFlexLexer(in) {}
 | 
			
		||||
 | 
			
		||||
    using FlexLexer::yylex;
 | 
			
		||||
    virtual int yylex(value_type *lval, location_type * loc);
 | 
			
		||||
 | 
			
		||||
    //double num{};
 | 
			
		||||
    //std::string_view ident{};
 | 
			
		||||
    //value_type *yylval = nullptr;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif //MCALC_LEXER_BASE_H
 | 
			
		||||
							
								
								
									
										53
									
								
								include/node.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								include/node.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
#ifndef MCALC_NODE_H
 | 
			
		||||
#define MCALC_NODE_H
 | 
			
		||||
 | 
			
		||||
#include <optional>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <unordered_set>
 | 
			
		||||
#include <memory>
 | 
			
		||||
#include <memory_resource>
 | 
			
		||||
 | 
			
		||||
struct State;
 | 
			
		||||
 | 
			
		||||
struct Node {
 | 
			
		||||
    enum class type {
 | 
			
		||||
        NUM,
 | 
			
		||||
        VAR,
 | 
			
		||||
        NEG,
 | 
			
		||||
        PLUS,
 | 
			
		||||
        MINUS,
 | 
			
		||||
        MUL,
 | 
			
		||||
        DIV,
 | 
			
		||||
        INVALID
 | 
			
		||||
    } ty = type::INVALID;
 | 
			
		||||
    double num = 0;
 | 
			
		||||
    std::string var{};
 | 
			
		||||
    std::unique_ptr<Node> a{}, b{};
 | 
			
		||||
 | 
			
		||||
    void used_vars(std::unordered_set<std::string>*) const;
 | 
			
		||||
    std::optional<double> eval(State*) const;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
using parser_var_ty = std::variant<double, std::string, Node*>;
 | 
			
		||||
 | 
			
		||||
template<Node::type ty>
 | 
			
		||||
Node *new_node(const parser_var_ty& v) {
 | 
			
		||||
    auto *node = new Node { .ty = ty };
 | 
			
		||||
    if constexpr (ty == Node::type::NUM)
 | 
			
		||||
        node->num = std::get<double>(v);
 | 
			
		||||
    else if constexpr (ty == Node::type::VAR)
 | 
			
		||||
        node->var = std::get<std::string>(v);
 | 
			
		||||
    else
 | 
			
		||||
        node->a.reset(std::get<Node*>(v));
 | 
			
		||||
    return node;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<Node::type ty>
 | 
			
		||||
Node *new_node(const parser_var_ty& a, const parser_var_ty& b) {
 | 
			
		||||
    auto *node = new Node { .ty = ty };
 | 
			
		||||
    node->a.reset(std::get<Node*>(a));
 | 
			
		||||
    node->b.reset(std::get<Node*>(b));
 | 
			
		||||
    return node;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif //MCALC_NODE_H
 | 
			
		||||
		Reference in New Issue
	
	Block a user