topazc
semantic.hpp
Go to the documentation of this file.
1
6
7#include "../parser/ast.hpp"
8#include <filesystem>
9#include <vector>
10#include <memory>
11#include <stack>
12#include <map>
13
15private:
16 std::string libs_path;
17 std::string file_name;
18 std::string path_to_current_dir;
19 std::vector<AST::StmtPtr>& stmts;
20 bool is_debug;
21
30
39
43 struct Value {
48
49 Value(AST::Type t, AST::Value v, bool vu, bool il) : type(t), value(v), is_value_unknown(vu), is_literal(il) {}
50 };
51
52 std::stack<std::map<std::string, Value>> variables;
53
57 struct FunctionInfo {
59 std::vector<AST::Argument> args;
60 std::vector<AST::StmtPtr> block;
61
62 FunctionInfo(AST::Type rt, std::vector<AST::Argument> a, std::vector<AST::StmtPtr> b) : ret_type(rt), args(std::move(a)), block(std::move(b)) {}
63 };
64 std::map<std::string, std::vector<std::shared_ptr<FunctionInfo>>> functions;
65 std::stack<AST::Type> functions_ret_types;
66 unsigned depth_of_loops;
67 std::vector<std::string> allowed_langs_for_extern {
68 "C",
69 "C++",
70 "Rust"
71 };
72
73public:
77 struct ModuleInfo {
78 std::map<std::string, std::pair<AST::AccessModifier, ModuleInfo*>> modules;
79 std::map<std::string, std::pair<AST::AccessModifier, std::string>> functions;
80 };
81private:
82 std::map<std::string, ModuleInfo*> modules;
83 std::vector<std::string> names_of_imported_modules;
84
88 struct PathPart {
89 std::string name;
90
98 };
99 std::stack<PathPart> current_path;
100
101public:
102 SemanticAnalyzer(std::vector<AST::StmtPtr>& s, std::string lp, std::string fn, bool id) : stmts(s), libs_path(lp), file_name(fn), depth_of_loops(0), is_debug(id) {
103 std::filesystem::path file_path = std::filesystem::absolute(fn);
104 path_to_current_dir = file_path.parent_path().string();
105 variables.push({});
106 }
107
113 void analyze();
114
120 std::map<std::string, ModuleInfo*> get_modules() const {
121 return modules;
122 }
123
129 std::map<std::string, std::vector<std::shared_ptr<FunctionInfo>>> get_functions() const {
130 return functions;
131 }
132
133private:
141 void analyze_stmt(AST::Stmt& stmt);
142
151 void analyze_var_decl_stmt(AST::VarDeclStmt& vds, bool is_func_arg = false);
152
161
170
179
188
197
206
215
226
235
244
253
262
271
281 Value analyze_expr(AST::Expr& expr);
282
293
304
315
326
337
348
359 Value analyze_obj_from_chain(Value target, AST::Expr& obj);
360
371 Value get_function_return_value(std::shared_ptr<FunctionInfo> func, AST::FuncCallExpr& fce);
372
384
396
408
420
431 AST::Value get_default_val_by_type(AST::Type type, uint32_t line);
432
442 std::unique_ptr<Value> get_variable_value(std::string name);
443
453 std::vector<std::shared_ptr<FunctionInfo>> get_function_candidates(std::string name);
454
466 bool has_common_type(AST::Type left, AST::Type right);
467
479 AST::Type get_common_type(AST::Type left, AST::Type right, uint32_t line);
480
492 Value implicitly_cast(Value val, AST::Type type, uint32_t line);
493
506 double binary_two_variants(Value left, Value right, TokenType op, uint32_t line);
507
520 double unary_two_variants(Value value, TokenType op, uint32_t line);
521
532 std::string get_mangled_name(std::string base_name);
533
543 std::vector<PathPart> get_resolved_name(std::string mangled_name);
544};
Header file for defining AST tree elements.
Binary expression container.
Definition ast.hpp:219
Statement of break.
Definition ast.hpp:401
Chain of objects expression container.
Definition ast.hpp:273
Statement of break.
Definition ast.hpp:410
Statement of do-while cycle.
Definition ast.hpp:375
Base class of expression.
Definition ast.hpp:117
Statement of extern calls.
Definition ast.hpp:444
Statement of for cycle.
Definition ast.hpp:387
Function calling expression container.
Definition ast.hpp:261
Statement of functions calling.
Definition ast.hpp:328
Statement of functions declaration.
Definition ast.hpp:313
Statement of control flow operator.
Definition ast.hpp:351
Base class of literal.
Definition ast.hpp:133
Statement of module definition.
Definition ast.hpp:419
Statement of 'return'.
Definition ast.hpp:340
Base class of statement.
Definition ast.hpp:106
Unary expression container.
Definition ast.hpp:234
Statement of import the module.
Definition ast.hpp:433
Statement of assignment of variable.
Definition ast.hpp:300
Statement of variable declaration.
Definition ast.hpp:286
Variable expression container.
Definition ast.hpp:248
Statement of while cycle.
Definition ast.hpp:363
Value * get_function_return_value_from_do_while_cycle(AST::DoWhileCycleStmt &dwcs)
Method for evaluating and returning function returned value from do-while cycle.
std::string get_mangled_name(std::string base_name)
Method for getting mangled name.
Value implicitly_cast(Value val, AST::Type type, uint32_t line)
Method for getting implicitly casted value between two values.
void analyze_break_stmt(AST::BreakStmt &bs)
Method for analyze break statement.
Definition semantic.cpp:505
void analyze_return_stmt(AST::ReturnStmt &rs)
Method for analyze 'return' statement.
Definition semantic.cpp:411
SemanticAnalyzer(std::vector< AST::StmtPtr > &s, std::string lp, std::string fn, bool id)
Definition semantic.hpp:102
Value analyze_unary_expr(AST::UnaryExpr &ue)
Method for analyze unary expression.
Definition semantic.cpp:793
std::map< AST::TypeValue, std::vector< AST::TypeValue > > implicitly_cast_allowed_types
Definition semantic.hpp:31
void analyze()
Method for analyze all statements.
Definition semantic.cpp:15
Value * get_function_return_value_from_for_cycle(AST::ForCycleStmt &fcs)
Method for evaluating and returning function returned value from for cycle.
AST::Value get_default_val_by_type(AST::Type type, uint32_t line)
Method for getting default value by type.
std::map< std::string, ModuleInfo * > modules
Definition semantic.hpp:82
std::vector< AST::StmtPtr > & stmts
Definition semantic.hpp:19
Value analyze_obj_chain_expr(AST::ChainObjects &co)
Method for analyze chain of objects expression.
Definition semantic.cpp:970
void analyze_if_else_stmt(AST::IfElseStmt &ies)
Method for analyze control flow operators.
Definition semantic.cpp:430
void analyze_func_decl_stmt(AST::FuncDeclStmt &fds)
Method for analyze function declaration.
Definition semantic.cpp:241
void analyze_var_decl_stmt(AST::VarDeclStmt &vds, bool is_func_arg=false)
Method for analyze variable declaration.
Definition semantic.cpp:87
std::vector< std::string > allowed_langs_for_extern
Definition semantic.hpp:67
void analyze_var_asgn_stmt(AST::VarAsgnStmt &vas)
Method for analyze variable assignment.
Definition semantic.cpp:170
unsigned depth_of_loops
Definition semantic.hpp:66
std::string path_to_current_dir
Definition semantic.hpp:18
void analyze_use_module_stmt(AST::UseModuleStmt &ums)
Method for analyze import the module.
Definition semantic.cpp:555
std::unique_ptr< Value > get_variable_value(std::string name)
Method for getting value of variable from view scope of variables table.
std::map< std::string, ModuleInfo * > get_modules() const
Method for getting modules from semantic.
Definition semantic.hpp:120
AST::Type get_common_type(AST::Type left, AST::Type right, uint32_t line)
Method for getting common type between two types.
void analyze_extern_stmt(AST::ExternStmt &es)
Method for analyze extern calls.
Definition semantic.cpp:670
Value analyze_func_call_expr(AST::FuncCallExpr &fce)
Method for analyze function calling expression.
Definition semantic.cpp:866
std::vector< std::shared_ptr< FunctionInfo > > get_function_candidates(std::string name)
Method for getting function candidates from functions table.
std::map< std::string, std::vector< std::shared_ptr< FunctionInfo > > > functions
Definition semantic.hpp:64
std::string file_name
Definition semantic.hpp:17
Value analyze_expr(AST::Expr &expr)
Method for analyze expression.
Definition semantic.cpp:681
bool has_common_type(AST::Type left, AST::Type right)
Method for determining whether two types have a common type.
void analyze_do_while_cycle_stmt(AST::DoWhileCycleStmt &dwcs)
Method for analyze do-while cycle.
Definition semantic.cpp:467
void analyze_while_cycle_stmt(AST::WhileCycleStmt &wcs)
Method for analyze while cycle.
Definition semantic.cpp:451
double binary_two_variants(Value left, Value right, TokenType op, uint32_t line)
Method for evaluating binary operations on two values from std::variant.
std::string libs_path
Definition semantic.hpp:16
Value get_function_return_value(std::shared_ptr< FunctionInfo > func, AST::FuncCallExpr &fce)
Method for evaluating and returning function returned value.
std::stack< PathPart > current_path
Definition semantic.hpp:99
void analyze_continue_stmt(AST::ContinueStmt &cs)
Method for analyze continue statement.
Definition semantic.cpp:511
void analyze_stmt(AST::Stmt &stmt)
Method for analyze one statement.
Definition semantic.cpp:21
Value analyze_literal_expr(AST::Literal &lit)
Method for analyze literal.
Definition semantic.cpp:705
std::vector< std::string > names_of_imported_modules
Definition semantic.hpp:83
void analyze_for_cycle_stmt(AST::ForCycleStmt &fcs)
Method for analyze for cycle.
Definition semantic.cpp:483
Value * get_function_return_value_from_while_cycle(AST::WhileCycleStmt &wcs)
Method for evaluating and returning function returned value from while cycle.
Space
Current space (in global, in module or in function).
Definition semantic.hpp:25
void analyze_module_stmt(AST::ModuleStmt &ms)
Method for analyze module definition.
Definition semantic.cpp:517
std::map< std::string, std::vector< std::shared_ptr< FunctionInfo > > > get_functions() const
Method for getting functions from semantic.
Definition semantic.hpp:129
enum SemanticAnalyzer::Space current_space
std::vector< PathPart > get_resolved_name(std::string mangled_name)
Method for getting resolved name by mangled name.
void analyze_func_call_stmt(AST::FuncCallStmt &fcs)
Method for analyze function calling.
Definition semantic.cpp:353
Value analyze_obj_from_chain(Value target, AST::Expr &obj)
Method for analyze object from chain of objects expression.
Definition semantic.cpp:981
std::stack< std::map< std::string, Value > > variables
Definition semantic.hpp:52
Value * get_function_return_value_from_if_else(AST::IfElseStmt &ies)
Method for evaluating and returning function returned value from control flow operators.
double unary_two_variants(Value value, TokenType op, uint32_t line)
Method for evaluating unary operations on two values from std::variant.
Value analyze_binary_expr(AST::BinaryExpr &be)
Method for analyze binary expression.
Definition semantic.cpp:709
Value analyze_var_expr(AST::VarExpr &ve)
Method for analyze variable expression.
Definition semantic.cpp:853
std::stack< AST::Type > functions_ret_types
Definition semantic.hpp:65
@ TYPE_INT
Definition ast.hpp:23
@ TYPE_CHAR
Definition ast.hpp:21
@ TYPE_FLOAT
Definition ast.hpp:25
@ TYPE_BOOL
Definition ast.hpp:20
@ TYPE_LONG
Definition ast.hpp:24
@ TYPE_DOUBLE
Definition ast.hpp:26
@ TYPE_SHORT
Definition ast.hpp:22
Structure for describing the type.
Definition ast.hpp:37
Structure for describing the value.
Definition ast.hpp:71
std::vector< AST::StmtPtr > block
Definition semantic.hpp:60
std::vector< AST::Argument > args
Definition semantic.hpp:59
FunctionInfo(AST::Type rt, std::vector< AST::Argument > a, std::vector< AST::StmtPtr > b)
Definition semantic.hpp:62
Structure of information about module.
Definition semantic.hpp:77
std::map< std::string, std::pair< AST::AccessModifier, ModuleInfo * > > modules
Definition semantic.hpp:78
std::map< std::string, std::pair< AST::AccessModifier, std::string > > functions
Definition semantic.hpp:79
Structure of part of path to object.
Definition semantic.hpp:88
Object
Object from path (module or class).
Definition semantic.hpp:94
enum SemanticAnalyzer::PathPart::Object object
Value(AST::Type t, AST::Value v, bool vu, bool il)
Definition semantic.hpp:49
TokenType
All tokens types.
Definition token.hpp:14