Native functions like sine and cosine!

functions
Justyna Att Ilczuk 2012-12-10 11:45:09 +01:00
parent 9847a9ead0
commit 1eee8a0039
6 changed files with 150 additions and 23 deletions

View File

@ -7,8 +7,9 @@
#include "PostfixExpression.h"
PostfixExpression::PostfixExpression(ASTNode * parent) {
PostfixExpression::PostfixExpression(ASTNode * parent, Context * context) {
this->parent = parent;
this->context = context;
name = "";
native = false;
}
@ -31,23 +32,13 @@ void PostfixExpression::add_argument(ASTExpression * expression)
SenchaObject PostfixExpression::evaluate()
{
return SenchaObject();
return context->execute_native_function(name, arguments);
}
void PostfixExpression::execute() {
if(name == "print")
{
std::cout << "Program says: ";
for (auto argument: arguments)
{
auto value = argument->evaluate();
context->execute_native_function(name, arguments);
std::cout << value.str();
}
std::cout << std::endl;
}
evaluate(); }
}

View File

@ -9,11 +9,13 @@
#define POSTFIXEXPRESSION_H_
#include "ASTExpression.h"
#include "ASTStatement.h"
#include "../Context.h"
class PostfixExpression : public ASTExpression {
public:
std::string name;
bool native;
Context * context;
std::vector<ASTExpression *> arguments;
void set_name(std::string name);
void add_argument(ASTExpression * expression);
@ -26,7 +28,7 @@ public:
std::string debug() ;
PostfixExpression(ASTNode * parent);
PostfixExpression(ASTNode * parent, Context * context);
virtual ~PostfixExpression();
};

View File

@ -16,6 +16,19 @@ Context::~Context() {
// TODO Auto-generated destructor stub
}
void Context::register_function(std::string name, PointerToNativeFunction f)
{
registered_functions[name] = f;
}
SenchaObject Context::execute_native_function(std::string name, std::vector<ASTExpression *> arguments)
{
SenchaObject result;
if(registered_functions.count(name) == 1)
result = registered_functions[name](arguments);
return result;
}
ObjectIndex Context::add_to_store(SenchaObject & object)
{
index++;

View File

@ -8,9 +8,12 @@
#ifndef CONTEXT_H_
#define CONTEXT_H_
#include <map>
#include "AST/SenchaObject.h"
#include <vector>
#include <string>
#include <iostream>
#include "AST/SenchaObject.h"
#include "AST/ASTExpression.h"
typedef unsigned long ObjectIndex;
typedef unsigned long FunctionIndex;
@ -25,6 +28,14 @@ public:
ObjectIndex add_to_store(SenchaObject & object);
SenchaObject get_from_store(ObjectIndex index);
typedef SenchaObject (*PointerToNativeFunction)(std::vector<ASTExpression *>);
std::map<std::string, PointerToNativeFunction> registered_functions;
void register_function(std::string name, PointerToNativeFunction f);
SenchaObject execute_native_function(std::string name, std::vector<ASTExpression *> arguments);
//Overload it to use contexts
void add(std::string name, SenchaObject object);
void set(std::string name, SenchaObject object);

View File

@ -131,14 +131,21 @@ bool Parser::is_type()
bool Parser::is_function_name()
{
if(tok_value == "print")
//check in registered functions
//cout << tok_value << " is it registered?" << endl;
if(context->registered_functions.count(tok_value) == 1)
{
//check in registered functions
//cout << "Yes" << endl;
read_next();
return true;
}
else
{
//cout << "No" << endl;
return false;
}
}
ASTStatement * Parser::statement(ASTNode * parent)
@ -233,7 +240,7 @@ ASTStatement * Parser::statement(ASTNode * parent)
}
else if(accept("while"))
{
//similar stuff
return stat;
}
else if(accept("return"))
@ -332,7 +339,7 @@ ASTExpression * Parser::postfix_expr(ASTNode * expression)
auto name = tok_value;
if(is_function_name())
{
PostfixExpression * function_call = new PostfixExpression(expression);
PostfixExpression * function_call = new PostfixExpression(expression, context);
function_call->set_name(name);
if(accept("("))
{

View File

@ -1,5 +1,7 @@
#include <fstream>
#include <iostream>
#include <string>
#include <string>
#include <math.h>
#include "Token.h"
#include "Lexer.h"
#include "Parser.h"
@ -8,6 +10,80 @@
using namespace std;
SenchaObject print(vector<ASTExpression *> arguments)
{
std::cout << "Program says: ";
for (auto argument: arguments)
{
auto value = argument->evaluate();
std::cout << value.str();
}
std::cout << std::endl;
return SenchaObject();
}
SenchaObject s_sin(vector<ASTExpression *> arguments)
{
SenchaObject result;
SenchaObject sin_argument = arguments[0]->evaluate();
if(sin_argument.type == SenchaObject::integer_number)
{
result = SenchaObject(sin(sin_argument.integer));
}
else if(sin_argument.type == SenchaObject::float_number)
{
result = SenchaObject(sin(sin_argument.number));
}
else
{
result.type = SenchaObject::invalid;
}
return result;
}
SenchaObject s_cos(vector<ASTExpression *> arguments)
{
SenchaObject result;
SenchaObject cos_argument = arguments[0]->evaluate();
if(cos_argument.type == SenchaObject::integer_number)
{
result = SenchaObject(cos(cos_argument.integer));
}
else if(cos_argument.type == SenchaObject::float_number)
{
result = SenchaObject(cos(cos_argument.number));
}
else
{
result.type = SenchaObject::invalid;
}
return result;
}
SenchaObject s_tan(vector<ASTExpression *> arguments)
{
SenchaObject result;
SenchaObject tan_argument = arguments[0]->evaluate();
if(tan_argument.type == SenchaObject::integer_number)
{
result = SenchaObject(tan(tan_argument.integer));
}
else if(tan_argument.type == SenchaObject::float_number)
{
result = SenchaObject(tan(tan_argument.number));
}
else
{
result.type = SenchaObject::invalid;
}
return result;
}
void test_lexer()
{
string test_line = "dupa";
@ -77,6 +153,11 @@ void interactive()
Lexer lexer;
Context context;
context.register_function("print", print);
context.register_function("sin", s_sin);
context.register_function("cos", s_cos);
context.register_function("tan", s_tan);
Parser parser(&context);
vector<Token> tokens;
@ -126,10 +207,32 @@ int main(int argc, char *argv[])
}
else
{
for (int count = 1; count < argc; count++)
auto name = argv[1];
Lexer lexer;
Context context;
context.register_function("print", print);
context.register_function("sin", s_sin);
context.register_function("cos", s_cos);
context.register_function("tan", s_tan);
Parser parser(&context);
vector<Token> tokens;
string line;
ifstream input_file (name);
if (input_file.is_open())
{
while ( input_file.good() )
{
printf("argv[%d] = %s\n", count, argv[count]);
getline (input_file,line);
tokens = lexer.parse_line(line);
parser.add_tokens(tokens);
}
input_file.close();
}
parser.interpret();
parser.program->execute();
}
return 0;