Silly mistake, and lots of fun, heh :P

functions
Justyna Ilczuk 2012-12-28 17:39:43 +01:00
parent 4d3eaee1b6
commit 93a63d43e9
11 changed files with 57 additions and 152 deletions

View File

@ -8,9 +8,9 @@
#include "PostfixExpression.h"
PostfixExpression::PostfixExpression(std::string name, Context * context):
context(context), name(name), native(false), type("PostfixExpression")
name(name), context(context), native(false)
{
type= "PostfixExpression";
}
PostfixExpression::~PostfixExpression() {
@ -22,11 +22,6 @@ PostfixExpression::~PostfixExpression() {
}
void PostfixExpression::set_name(std::string name)
{
this->name = name;
}
void PostfixExpression::add_argument(ASTExpression * expression)
{
arguments.push_back(expression);

View File

@ -18,8 +18,8 @@
class PostfixExpression : public ASTExpression {
public:
std::string name;
bool native;
Context * context;
bool native;
std::vector<ASTExpression *> arguments;
void add_argument(ASTExpression * expression);

View File

@ -7,9 +7,11 @@
#include "WhileNode.h"
WhileNode::WhileNode() {
body = NULL;
this->type = "WhileNode";
WhileNode::WhileNode(ASTExpression * condition, ASTStatement * body):
body(body)
{
type = "WhileNode";
children.push_back(condition);
}
WhileNode::~WhileNode() {
@ -21,16 +23,6 @@ WhileNode::~WhileNode() {
}
}
void WhileNode::add_condition(ASTExpression * expression)
{
children.push_back(expression);
}
void WhileNode::add_body(ASTStatement * statement)
{
body = statement;
}
void WhileNode::execute()
{

View File

@ -17,11 +17,9 @@
*/
class WhileNode: public ASTStatement {
public:
WhileNode();
WhileNode(ASTExpression * condition, ASTStatement * body);
virtual ~WhileNode();
ASTStatement * body;
void add_condition(ASTExpression * expression);
void add_body(ASTStatement * statement);
virtual void execute();
bool evaluate_condition();

View File

@ -307,10 +307,8 @@ void ASTInspector::write_report(std::string visit_notes)
while(std::getline(stream, line)) {
inspection_report += correct_indentation + line + "\n";
}
}
std::string ASTInspector::compute_indent()
{
std::string indentation = "";

View File

@ -69,16 +69,10 @@ bool Parser::read_next()
void Parser::interpret()
{
read_next();
while(tok_value!= "")
{
program->add_statement(statement());
}
while(tok_value!= "") program->add_statement(statement());
}
bool Parser::peek(string s)
{
return tok_value == s;
}
bool Parser::peek(string s) {return tok_value == s;}
bool Parser::accept(string s)
{
@ -110,10 +104,7 @@ bool Parser::is_type()
read_next();
return true;
}
else
{
return false;
}
else return false;
}
bool Parser::is_function_name()
@ -123,21 +114,16 @@ bool Parser::is_function_name()
read_next();
return true;
}
else
{
return false;
}
else return false;
}
ASTStatement * Parser::statement()
{
BasicStatement * stat = new BasicStatement();
{
if(accept("{"))
{
while(!accept("}"))
{
stat->children.push_back( statement());
}
{
BasicStatement * stat = new BasicStatement();
while(!accept("}")) stat->children.push_back(statement());
return stat;
}
else if(is_type())
{
@ -158,23 +144,14 @@ ASTStatement * Parser::statement()
{
argc++;
is_type();
declaration->add_argument(tok_value);
read_next();
if(peek(")"))
{
break;
}
if(peek(")")) break;
expect(",");
}
expect(")");
if(!accept(";"))
{
declaration->add_body(statement());
}
if(!accept(";")) declaration->add_body(statement());
}
delete stat;
return declaration;
}
else if(accept("if"))
@ -182,11 +159,7 @@ ASTStatement * Parser::statement()
IfNode * ifStatement = new IfNode();
ifStatement->add_condition(expr());
ifStatement->add_body(statement());
if(accept("else"))
{
ifStatement->add_else_block(statement());
}
delete stat;
if(accept("else")) ifStatement->add_else_block(statement());
return ifStatement;
}
else if(accept("repeat"))
@ -194,34 +167,27 @@ ASTStatement * Parser::statement()
RepeatStatement * repeat = new RepeatStatement();
repeat->add_iteration_number(expr()->evaluate());
repeat->add_body(statement());
delete stat;
return repeat;
}
else if(accept("while"))
{
WhileNode * while_node = new WhileNode();
while_node->add_condition(expr());
while_node->add_body(statement());
delete stat;
WhileNode * while_node = new WhileNode(expr(), statement());
return while_node;
}
else if(accept("return"))
{
if(!peek(";"))
{
stat->add_expression(expr());
}
{
BasicStatement * stat = new BasicStatement();
if(!peek(";")) stat->add_expression(expr());
expect(";");
return stat;
}
else
{
{
BasicStatement * stat = new BasicStatement();
stat->add_expression(expr());
while(!expect(";") && tok_value != "") read_next();
return stat;
}
return stat;
}
}
ASTExpression * Parser::prim_expr()
@ -262,18 +228,15 @@ ASTExpression * Parser::prim_expr()
else if(current_token.get_type() == t_symbol)
{
string name = current_token.value;
VariableExpression * ve;
ve = new VariableExpression(name, context);
VariableExpression * ve = new VariableExpression(name, context);
read_next();
return ve;
}
else if(accept("("))
{
BasicExpression * be;
be = static_cast<BasicExpression *>(expr());
ASTExpression * expression = expr();
expect(")");
return be;
return expression;
}
else
{
@ -294,7 +257,6 @@ ASTExpression * Parser::postfix_expr()
{
if(!accept(")"))
{
function_call->add_argument(expr());
while(accept(","))
{
@ -350,9 +312,8 @@ ASTExpression * Parser::eq_expr()
ASTExpression * left = rel_expr();
if(peek("==") || peek("!="))
{
string oper;
if(accept("==")) oper = "==";
else oper = "!=";
string oper = tok_value;
read_next();
return new BasicExpression(left, eq_expr(), oper);
}
else return left;
@ -365,7 +326,7 @@ ASTExpression * Parser::log_expr()
{
string oper;
if(accept("and")) oper = "&&";
else oper = "||";
else if(accept("or")) oper = "||";
ASTExpression * right = log_expr();
return new BasicExpression(left, right, oper);
}

View File

@ -19,8 +19,9 @@ TestASTInspector::~TestASTInspector() {
std::string TestASTInspector::test_inspecting_basic_expression()
{
std::string test_report = "";
BasicExpression * be = new BasicExpression(new ConstantExpression(SenchaObject(9)),
new ConstantExpression(SenchaObject(122)), "+");
BasicExpression * be = build_basic_expression("+", SenchaObject(9), SenchaObject(122));
be->accept(&inspector);
std::string assert_report = "Report:\n" + inspector.get_report();
muu_assert("ASTInspector didn\'t write any report", inspector.get_report() != "");
@ -37,8 +38,8 @@ std::string TestASTInspector::test_inspecting_simple_AST()
{
std::string test_report = "";
auto left = build_basic_expression("-", SenchaObject(88), SenchaObject(-19));
auto right = build_basic_expression("*", SenchaObject(9), SenchaObject(12));
BasicExpression * left = build_basic_expression("-", SenchaObject(88), SenchaObject(-19));
BasicExpression * right = build_basic_expression("*", SenchaObject(9), SenchaObject(12));
BasicExpression * be = build_simple_AST("+", left, right);
be->accept(&inspector);
std::string assert_report = "Report:\n" + inspector.get_report();
@ -56,25 +57,20 @@ std::string TestASTInspector::all_tests()
{
std::string test_report = "";
mu_run_test(test_inspecting_basic_expression);
mu_run_test(test_inspecting_simple_AST);
return test_report;
mu_run_test(test_inspecting_basic_expression);
mu_run_test(test_inspecting_simple_AST);
return test_report;
}
BasicExpression * TestASTInspector::build_basic_expression(std::string oper, SenchaObject arg1, SenchaObject arg2)
{
BasicExpression * be = new BasicExpression();
be->set_operator(oper);
be->set_left_operand(new ConstantExpression( arg1));
be->set_right_operand(new ConstantExpression( arg2));
BasicExpression * be = new BasicExpression(new ConstantExpression( arg1), new ConstantExpression( arg2), oper);
return be;
}
BasicExpression * TestASTInspector::build_simple_AST(std::string oper, BasicExpression * left, BasicExpression * right)
{
BasicExpression * be = new BasicExpression();
be->set_operator(oper);
be->set_left_operand(left);
be->set_right_operand(right);
BasicExpression * be = new BasicExpression(left, right, oper);
return be;
}

View File

@ -9,7 +9,10 @@
#define TESTLEXER_H_
#include "TestSuite.h"
#include "../Lexer.h"
/**
* TextLexer uses small test framework (TestSuite)
* to test some methods of Lexer class.
*/
class TestLexer : public TestSuite {
public:
TestLexer();

View File

@ -28,11 +28,10 @@ std::string TestParser::test_parsing_and_evaluating_logical_expressions()
auto tokens = lexer.parse_line(logical_case.first);
parser.add_tokens(tokens);
parser.interpret();
SenchaObject value = parser.program->evaluate();
SenchaObject value = parser.program->evaluate_last();
parser.erase_all();
muu_assert("Logical value isn't correct", value.truthy == logical_case.second);
}
return test_report;
}

View File

@ -8,6 +8,10 @@
#ifndef MINUNIT_H_
#define MINUNIT_H_
/**
* For my small testing framework I define three macro's
* Here they are:
*/
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
#define muu_assert(message, test) do { if (!(test)) test_report += message "\n"; } while (0)
#define mu_run_test(test) do { std::string message = test(); tests++;\

View File

@ -18,53 +18,11 @@
using namespace std;
void test_lexer()
{
string test_line = "dupa";
string test_line2 = "def how_many_trees = 1; how_many_trees + 3 == 2; num cut_tree( num how_many) {return how_many -1; != <=}";
Lexer lexer;
vector<Token> tokens = lexer.parse_line(test_line);
for(unsigned int i=0; i< tokens.size(); i++)
{
cout << tokens[i].get_value() << " type: " << tokens[i].get_type() << endl;
}
tokens = lexer.parse_line(test_line2);
}
void test_parser()
{
vector<string> lines;
lines.push_back("def i; bulb; i + 3; string banan = \"kartofel\"; banan = \"banan\"; string kaboom(num how_many_times) { def z; }");
lines.push_back("num pun");
lines.push_back("def how_many_trees = 1; how_many_trees + 3 == 2; num cut_tree(num how_many) {return how_many -1}");
Lexer lexer;
Context context;
vector<Token> tokens;
for(unsigned int i=0; i<lines.size(); i++)
{
tokens = lexer.parse_line(lines[i]);
Parser parser(&context);
parser.add_tokens(tokens);
parser.interpret();
cout << "<<<Parsing number: " << i << " >>>" << endl;
cout << "Instructions: " << endl ;
cout << lines[i] << endl << endl;
//cout << parser.report_message;
cout << parser.error_message << endl;
}
}
void run_test_suites()
{
TestLexer test_l;
test_l.run_tests();
TestLexer test_lexer;
test_lexer.run_tests();
TestASTInspector test_inspector;
test_inspector.run_tests();
@ -76,8 +34,9 @@ void run_test_suites()
void run_tests()
{
run_test_suites();
cout << "derp!" << endl;
run_test_suites();
}