Some documentation and little refactoring.
parent
0992a3a67d
commit
02bced0b5d
|
@ -185,13 +185,9 @@ void ASTInspector::visit(ProgramNode * program)
|
|||
depth_level++;
|
||||
std::string visit_notes = "";
|
||||
visit_notes += "ProgramNode:\n";
|
||||
|
||||
write_report(visit_notes);
|
||||
for(auto child: program->children)
|
||||
{
|
||||
child->accept(this);
|
||||
}
|
||||
|
||||
for(auto child: program->children) child->accept(this);
|
||||
write_report("End of ProgramNode\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
@ -232,11 +228,6 @@ void ASTInspector::visit(DeclarationStatement * declaration_statement)
|
|||
write_report(visit_notes);
|
||||
declaration_statement->body->accept(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
write_report("End of DeclarationStatement\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
|
|
@ -11,18 +11,41 @@
|
|||
#include <map>
|
||||
#include "Visitor.h"
|
||||
#include "AST/AllTypesOfASTNodes.h"
|
||||
/**
|
||||
* ASTInspector is a class which is used to inspect nodes of AST.
|
||||
* It's very useful. It provides user information about structure of the AST, how
|
||||
* particular nodes are built, and some other information, more statistical such as,
|
||||
* how many particular nodes is in tree. Thanks to depth awareness output can
|
||||
* be well formatted. Every node is visited in different manner.
|
||||
*/
|
||||
|
||||
class ASTInspector: public Visitor {
|
||||
public:
|
||||
ASTInspector();
|
||||
unsigned int how_many_visits() { return number_of_visits; }
|
||||
/**
|
||||
* For instance: how_many_occurences_of("BasicExpression") return how many times ASTInspector has encountered
|
||||
* BasicExpression node.
|
||||
*/
|
||||
unsigned int how_many_occurences_of(std::string type);
|
||||
|
||||
/**
|
||||
* get_report() returns inspection report. To get report, you first have to
|
||||
* visit some tree, using "visit" function.
|
||||
*/
|
||||
std::string get_report() { return this->inspection_report; }
|
||||
|
||||
virtual ~ASTInspector();
|
||||
|
||||
/**
|
||||
* forget_everything() erases all data which ASTInspector got during inspection.
|
||||
* It's useful when we want to use ASTInspector for more trees than one, and data from previous
|
||||
* shouldn't mix with data from current one.
|
||||
*/
|
||||
void forget_everything();
|
||||
|
||||
void visit(Visitable * node);
|
||||
void visit(ProgramNode * node);
|
||||
|
||||
|
||||
private:
|
||||
unsigned int number_of_visits;
|
||||
|
@ -31,13 +54,20 @@ private:
|
|||
std::string inspection_report;
|
||||
unsigned int depth_level;
|
||||
std::string compute_indent();
|
||||
|
||||
/**
|
||||
* write_report is an internal method used for
|
||||
* generating report in incremental way, using
|
||||
* notes from current visits and formatting it on right depth_level.
|
||||
*/
|
||||
void write_report(std::string visit_notes);
|
||||
|
||||
void visit(ProgramNode * node);
|
||||
void visit(ConstantExpression * node);
|
||||
void visit(BasicExpression * node);
|
||||
void visit(UnaryExpression * node);
|
||||
void visit(PostfixExpression * node);
|
||||
void visit(WhileNode * node);
|
||||
|
||||
void visit(BasicStatement * node);
|
||||
void visit(DeclarationStatement * node);
|
||||
void visit(Assignment * node);
|
||||
|
|
|
@ -16,31 +16,33 @@
|
|||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Context is object used to store variables for some execution context.
|
||||
* It's most useful in function calls. Every function has to have its own execution contexts,
|
||||
* for its variables. Arguments are passed to functions by predefining them in new Context, assigned to
|
||||
* function.
|
||||
*/
|
||||
class Context {
|
||||
public:
|
||||
Context(std::string name);
|
||||
unsigned int index;
|
||||
|
||||
std::string name;
|
||||
std::map<std::string, SenchaObject> object_store;
|
||||
unsigned int add_to_store(SenchaObject & object);
|
||||
SenchaObject get_from_store(unsigned int index);
|
||||
|
||||
typedef SenchaObject (*PointerToNativeFunction)(std::vector<ASTExpression *>);
|
||||
|
||||
std::map<std::string, PointerToNativeFunction> registered_functions;
|
||||
std::map<std::string, SenchaFunction *> registered_sfunctions;
|
||||
|
||||
void register_function(std::string name, PointerToNativeFunction f);
|
||||
void register_function(std::string name, SenchaFunction * f);
|
||||
SenchaObject execute_native_function(std::string name, std::vector<ASTExpression *> arguments);
|
||||
|
||||
std::string debug() ;
|
||||
void add(std::string name, SenchaObject object);
|
||||
void set(std::string name, SenchaObject object);
|
||||
SenchaObject get(std::string name);
|
||||
virtual ~Context();
|
||||
|
||||
private:
|
||||
unsigned int index;
|
||||
|
||||
std::map<std::string, PointerToNativeFunction> registered_functions;
|
||||
std::map<std::string, SenchaFunction *> registered_sfunctions;
|
||||
|
||||
std::map<std::string, SenchaObject> object_store;
|
||||
};
|
||||
|
||||
#endif /* CONTEXT_H_ */
|
||||
|
|
|
@ -12,6 +12,5 @@ new = 120;
|
|||
time = 4;
|
||||
|
||||
println("Old was: ", old, " new is: ", new, " and it changed in: ", time, " seconds" );
|
||||
|
||||
println(easy(time));
|
||||
println(compute(old, new, time));
|
||||
println(compute(old, new, time));
|
||||
println(easy(time));
|
|
@ -66,10 +66,11 @@ bool Parser::read_next()
|
|||
}
|
||||
}
|
||||
|
||||
void Parser::interpret()
|
||||
ProgramNode * Parser::interpret()
|
||||
{
|
||||
read_next();
|
||||
while(tok_value!= "") program->add_statement(statement());
|
||||
return program;
|
||||
}
|
||||
|
||||
bool Parser::peek(string s) {return tok_value == s;}
|
||||
|
|
|
@ -13,22 +13,43 @@ using namespace std;
|
|||
class Parser
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Parser is initialized with pointer or reference to ContextManager,
|
||||
* which is then used to manage contexts in AST created by Parser.
|
||||
*/
|
||||
Parser(ContextManager * context_manager);
|
||||
ContextManager * context_manager;
|
||||
|
||||
virtual ~Parser();
|
||||
void interpret();
|
||||
|
||||
virtual ~Parser();
|
||||
/**
|
||||
* interpret() is essential method in Parser. If given tokens, Parser prepares
|
||||
* AST and returns ProgramNode (root node).
|
||||
*/
|
||||
ProgramNode * interpret();
|
||||
|
||||
string error_message;
|
||||
void add_tokens(vector<Token> tokens);
|
||||
void add_tokens(vector<Token> tokens);
|
||||
|
||||
/**
|
||||
* show_token() shows token which Parser stores and uses for creating an AST.
|
||||
* It's useful during debugging.
|
||||
*/
|
||||
string show_tokens();
|
||||
AST tree;
|
||||
ProgramNode * program;
|
||||
|
||||
/**
|
||||
* resets all info, best to use, when one wants to parse whole new tree
|
||||
*/
|
||||
void erase_all();
|
||||
bool is_function_name();
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
private:
|
||||
//Not used in current implementation, delete?
|
||||
bool is_function_name();
|
||||
Token current_token;
|
||||
string tok_value;
|
||||
vector<Token> token_stream;
|
||||
|
@ -43,19 +64,22 @@ class Parser
|
|||
bool is_type();
|
||||
|
||||
void error(string s);
|
||||
|
||||
|
||||
ASTStatement * statement();
|
||||
|
||||
ASTExpression * log_expr();
|
||||
ASTExpression * mul_expr();
|
||||
ASTExpression * add_expr();
|
||||
ASTExpression * prim_expr();
|
||||
ASTExpression * unary_expr();
|
||||
ASTExpression * postfix_expr();
|
||||
ASTExpression * rel_expr();
|
||||
ASTExpression * eq_expr();
|
||||
/**
|
||||
* statement() is function used for parsing statements,
|
||||
* it is used as a first in sequence of recurrent calls.
|
||||
*/
|
||||
ASTStatement * statement();
|
||||
ASTExpression * expr();
|
||||
ASTExpression * log_expr();
|
||||
ASTExpression * eq_expr();
|
||||
ASTExpression * rel_expr();
|
||||
ASTExpression * add_expr();
|
||||
ASTExpression * mul_expr();
|
||||
ASTExpression * unary_expr();
|
||||
ASTExpression * postfix_expr();
|
||||
ASTExpression * prim_expr();
|
||||
|
||||
};
|
||||
|
||||
#endif // PARSER_H
|
||||
|
|
|
@ -198,9 +198,9 @@ void interactive()
|
|||
if(level_of_depth == 0) {
|
||||
parser.interpret();
|
||||
parser.program->execute_last();
|
||||
inspector.visit(parser.program);
|
||||
inspector.visit(parser.tree.root);
|
||||
//cout << parser.show_tokens();
|
||||
//cout << inspector.get_report();
|
||||
cout << inspector.get_report();
|
||||
inspector.forget_everything();
|
||||
}
|
||||
}
|
||||
|
@ -251,7 +251,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
ASTInspector inspector;
|
||||
parser.interpret();
|
||||
inspector.visit(parser.program);
|
||||
inspector.visit(parser.tree.root);
|
||||
parser.program->execute();
|
||||
//cout << inspector.get_report()<< endl;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue