diff options
author | Justyna Ilczuk <justyna.ilczuk@gmail.com> | 2013-01-04 11:58:59 +0100 |
---|---|---|
committer | Justyna Ilczuk <justyna.ilczuk@gmail.com> | 2013-01-04 11:58:59 +0100 |
commit | 02bced0b5dbcd1dc9ccdc6f7bcd5c7ba09b28179 (patch) | |
tree | bcf89c39753a616d6b477c5e01b40ccee1a5e312 | |
parent | 0992a3a67db500c2ed88218e47043482ab3829ac (diff) | |
download | sencha-lang-02bced0b5dbcd1dc9ccdc6f7bcd5c7ba09b28179.tar.gz sencha-lang-02bced0b5dbcd1dc9ccdc6f7bcd5c7ba09b28179.tar.bz2 sencha-lang-02bced0b5dbcd1dc9ccdc6f7bcd5c7ba09b28179.tar.xz sencha-lang-02bced0b5dbcd1dc9ccdc6f7bcd5c7ba09b28179.zip |
Some documentation and little refactoring.
-rw-r--r-- | Sencha-lang/ASTInspector.cpp | 11 | ||||
-rw-r--r-- | Sencha-lang/ASTInspector.h | 34 | ||||
-rw-r--r-- | Sencha-lang/Context.h | 26 | ||||
-rw-r--r-- | Sencha-lang/Examples/example5.se | 5 | ||||
-rw-r--r-- | Sencha-lang/Examples/example6.se | 0 | ||||
-rw-r--r-- | Sencha-lang/Parser.cpp | 3 | ||||
-rw-r--r-- | Sencha-lang/Parser.h | 54 | ||||
-rw-r--r-- | Sencha-lang/main.cpp | 6 |
8 files changed, 93 insertions, 46 deletions
diff --git a/Sencha-lang/ASTInspector.cpp b/Sencha-lang/ASTInspector.cpp index 04506fd..ba1f0e7 100644 --- a/Sencha-lang/ASTInspector.cpp +++ b/Sencha-lang/ASTInspector.cpp @@ -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--; } diff --git a/Sencha-lang/ASTInspector.h b/Sencha-lang/ASTInspector.h index a77f4a1..6e399dd 100644 --- a/Sencha-lang/ASTInspector.h +++ b/Sencha-lang/ASTInspector.h @@ -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); diff --git a/Sencha-lang/Context.h b/Sencha-lang/Context.h index 92187f0..916cbd4 100644 --- a/Sencha-lang/Context.h +++ b/Sencha-lang/Context.h @@ -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_ */ diff --git a/Sencha-lang/Examples/example5.se b/Sencha-lang/Examples/example5.se index 48707f5..b153319 100644 --- a/Sencha-lang/Examples/example5.se +++ b/Sencha-lang/Examples/example5.se @@ -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));
\ No newline at end of file +println(compute(old, new, time)); +println(easy(time));
\ No newline at end of file diff --git a/Sencha-lang/Examples/example6.se b/Sencha-lang/Examples/example6.se new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Sencha-lang/Examples/example6.se diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp index 81c8d84..0bcbc2f 100644 --- a/Sencha-lang/Parser.cpp +++ b/Sencha-lang/Parser.cpp @@ -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;} diff --git a/Sencha-lang/Parser.h b/Sencha-lang/Parser.h index 849e816..261d3ef 100644 --- a/Sencha-lang/Parser.h +++ b/Sencha-lang/Parser.h @@ -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(); + /**
+ * 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 * mul_expr(); - ASTExpression * add_expr(); + ASTExpression * eq_expr();
+ ASTExpression * rel_expr();
+ ASTExpression * add_expr();
+ ASTExpression * mul_expr();
+ ASTExpression * unary_expr();
+ ASTExpression * postfix_expr();
ASTExpression * prim_expr();
- ASTExpression * unary_expr(); - ASTExpression * postfix_expr(); - ASTExpression * rel_expr(); - ASTExpression * eq_expr(); - ASTExpression * expr();
+
};
#endif // PARSER_H
diff --git a/Sencha-lang/main.cpp b/Sencha-lang/main.cpp index b5283d0..a186ebb 100644 --- a/Sencha-lang/main.cpp +++ b/Sencha-lang/main.cpp @@ -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; } |