Page Menu
Home
Xreate
Search
Configure Global Search
Log In
Docs
Questions
Repository
Issues
Patches
Internal API
Files
F4000742
ast.h
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Jul 9, 4:45 AM
Size
5 KB
Mime Type
text/x-c++
Expires
Sat, Jul 11, 4:45 AM (1 d, 3 h)
Engine
blob
Format
Raw Data
Handle
272879
Attached To
rXR Xreate
ast.h
View Options
#ifndef AST_H
#define AST_H
#include <vector>
#include <stdlib.h>
#include <string>
#include "llvmlayer.h"
#include <list>
struct String_t{};
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[32];
wcstombs(buffer, value.c_str(), 32);
__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);
}
double get()const {return __value; }
private:
double __value;
};
template<> class Atom<String_t>
{
public:
Atom(const std::wstring& value)
: __value(value.begin(), value.end())
{
}
const std::string& get() const {return __value; }
private:
std::string __value;
};
enum TimePrimitive {Bool, Int, Float, Num, String};
template<> class Atom<Type_t>
{
public:
Atom(wchar_t* value)
{
char buffer_[32];
wcstombs(buffer_, value, 32);
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
{
friend class Function;
friend class ClaspLayer;
friend class CFGPass;
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;
};
typedef std::list<Expression> ExpressionList;
enum class TagModifier
{NONE, ASSERT, REQUIRE};
enum class DomainAnnotation
{FUNCTION, VARIABLE};
class RuleArguments: public std::vector<std::pair<std::string, DomainAnnotation>>
{
public:
void add(const Atom<Identifier_t>& name, DomainAnnotation typ);
};
class RuleGuards: public std::vector<Expression>
{
public:
void add(Expression&& e);
};
class ClaspLayer;
class MetaRuleAbstract
{
public:
MetaRuleAbstract(RuleArguments&& args, RuleGuards&& guards);
virtual ~MetaRuleAbstract();
virtual void compile(ClaspLayer& layer) =0;
protected:
RuleArguments __args;
RuleGuards __guards;
};
class RuleWarning: public MetaRuleAbstract
{
friend class ClaspLayer;
public:
RuleWarning(RuleArguments&& args, RuleGuards&& guards, Expression&& condition, Atom<String_t>&& message);
virtual void compile(ClaspLayer& layer);
~RuleWarning();
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 Atom<Identifier_t>& name);
void setReturnType(const TypeAnnotation& rtyp);
void addArg(const Atom<Identifier_t>& vname, const TypeAnnotation& typ);
void setBody(const Expression& body);
void addTag(const Atom<Identifier_t>& aname, const TagModifier amod);
const std::vector<std::pair<std::string, TagModifier>>& getAnnotations() const;
void addDeclaration(const Atom<Identifier_t> & vname, const TypeAnnotation& typ, const Expression& e);
void addListDeclaration(const Atom<Identifier_t> & 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, TagModifier>> __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(const Expression& expr, LLVMLayer& l, std::string* hintRetVar);
};
typedef unsigned int FID;
class AST
{
friend class Function;
friend class CFGPass;
friend class RulesPass;
public:
AST();
void add(Function& f);
void add(MetaRuleAbstract* r);
void compile(LLVMLayer& l);
void run(LLVMLayer& l);
std::string getModuleName();
FID getFunctionsCount() const;
const Function& getFunctionById(FID id) const;
private:
std::list<std::unique_ptr<MetaRuleAbstract>> __rules;
std::vector<Function> __functions;
std::map<std::string, FID> __indexFunctions;
};
#endif // AST_H
Event Timeline
Log In to Comment