#ifndef AST_H
#define AST_H

#include <vector>
#include <stdlib.h>
#include <string>
#include "llvmlayer.h"

struct Identifier_t {};
struct Number_t {};
struct Type_t {};

template<typename A>
class Atom{

};

template<> class Atom<Identifier_t>
{
public:
    Atom(const std::wstring& value)
    {
        char buffer[1000];
        wcstombs(buffer, value.c_str(), 1000);

        __value = buffer;
    }

    const std::string&  get() const{return __value; }
private:
    std::string __value;
};

template<> class Atom<Number_t>
{
public:
    Atom(wchar_t* value)
    {
        __value = wcstol(value, 0, 10);
    }
    int get()const {return __value; }
private:
   double __value;
};

enum TimePrimitive {Bool, Int, Float, Num, String};

template<> class Atom<Type_t>
{
public:
    Atom(wchar_t* value)
    {
        char buffer_[1000];
        wcstombs(buffer_, value, 1000);
        std::string buffer(buffer_);

        if (buffer=="bool"){
            __value = Bool;
        } else if (buffer=="int") {
            __value = Int;
        } else if (buffer=="float") {
            __value = Float;
        } else if (buffer=="num") {
            __value = Num;
        } else if (buffer=="string") {
            __value = String;
        }
    }

    Atom ()
    {
    }

    TimePrimitive get() const
    {
        return __value;
    }

private:
    TimePrimitive __value;
};

typedef Atom<Type_t> TypeAtom;


enum class TypeOperator{LIST};
class TypeAnnotation
{
public:
    TypeAnnotation (const Atom<Type_t>& typ);
    TypeAnnotation (const TypeOperator& op, const TypeAnnotation& typ);

    TypeAnnotation();

    llvm::Type* toLLVMType();

private:
    TimePrimitive __value;
};

enum class Operator
{
ADD, SUB, MUL, DIV, EQU, LSS, GTR, NEG, LIST, CALL, NONE, IMPL/* implication */
};

class Function;
class AST;

class Expression
{
public:
    Expression(const Operator &op, std::initializer_list<Expression> params);
    Expression(const Atom<Identifier_t>& ident);
    Expression(const Atom<Number_t>& number);
    Expression();

    void setOp(Operator op);
    void addArg(Expression&& arg);
protected:
    Operator __op;
    std::vector<Expression> operands;

    std::string __valueS;
    double __valueD;

    enum {INVALID, COMPOUND, IDENT, NUMBER, STRING} __state;
};

enum class AnnotationModifier
{NONE, ASSERT, REQUIRE};

enum class DomainAnnotation
{FUNCTION, VARIABLE};

class RuleArguments: public std::vector<std::pair<std::string, DomainAnnotation>>
{
public:
    void add(std::string&& name, DomainAnnotation typ);
};

class RuleGuards: public std::vector<Expression>
{
public:
    void add(Expression&& e);
};

class MetaRuleAbstract
{
protected:
    RuleArguments __args;
    RuleGuards __guards;
};

class ClaspLayer;
class RuleWarning: public MetaRuleAbstract
{
    friend class ClaspLayer;
public:
    void compile(ClaspLayer& layer);

private:
    std::string __message;
    Expression __condition;
};

typedef unsigned int VID;



/*
class Expression: ExpressionAbstract
{
    friend class CFGPass;

public:
    llvm::Value* compile(LLVMLayer& l, Function* f,  std::string* hintRetVar=0) const;
};
*/



typedef std::pair<VID, TypeAnnotation> VariableDefinition;
typedef std::pair<VID, Expression> VariableDeclaration;
class Function
{
    friend class Expression;
    friend class CFGPass;
public:
    Function(const std::wstring& name);
    void setReturnType(const TypeAnnotation& rtyp);
    void addArg(const std::wstring& vname, const TypeAnnotation& typ);
    void setBody(const Expression& body);

    void addAnnotation(const std::wstring& aname, const AnnotationModifier amod);
    const std::vector<std::pair<std::string, AnnotationModifier>>& getAnnotations() const;

    void addDeclaration(const std::wstring& vname, const TypeAnnotation& typ, const Expression& e);
    void addListDeclaration(const std::wstring& vname, const TypeAnnotation& typ, const Expression& e);

    const std::string& getName() const;
    llvm::Function* compile(LLVMLayer& l);

    AST* root;
private:

    std::string __name;
    TypeAnnotation __retType;
    std::vector<std::string> __args;
    std::vector<std::pair<std::string, AnnotationModifier>>  __tags;
    std::map<VariableDefinition::first_type, VariableDefinition::second_type> __definitions;
    std::map<VariableDeclaration::first_type, VariableDeclaration::second_type> __declarations;
    std::map<std::string, VID> __vartable;
    Expression __body;

    llvm::Function* __raw;
    std::map<VID,llvm::Value*> __rawVars;

    VID __vCounter;

    VID registerVar(const std::string& vname);

    llvm::Value* compileExpression(Expression& expr, LLVMLayer& l, std::string* hintRetVar) const;
};

typedef unsigned int FID;
class AST
{
    friend class Expression;
    friend class CFGPass;

public:
    AST();
    void add(Function& f);

    void compile(LLVMLayer& l);
    void run(LLVMLayer& l);

    std::string getModuleName();

    FID getFunctionsCount() const;
    const Function& getFunctionById(FID id) const;
private:
    std::vector<Function> __functions;
    std::map<std::string, FID> __indexFunctions;
};


#endif // AST_H
