Permalink
Browse files

Make Indexing a function

  • Loading branch information...
Chad Austin
Chad Austin committed Jan 3, 2012
1 parent 596fcc1 commit 631b34307e15ddf2e368efbc7f72673fc9b0eff0
Showing with 31 additions and 38 deletions.
  1. +1 −1 ren2/ren/API.h
  2. +0 −12 ren2/ren/AST.h
  3. +19 −5 ren2/ren/Function.h
  4. +7 −19 ren2/ren/GLBinding.h
  5. +4 −1 ren2/ren/Type.h
View
@@ -188,7 +188,7 @@ namespace ren {
{}
T operator[](const int_& index) const {
- return T(std::make_shared<Index>(expression, index.expression));
+ return T(std::make_shared<FunctionCall>(&vec4_index, expression, index.expression));
}
};
View
@@ -13,7 +13,6 @@ namespace ren {
public:
virtual void pushInput(const ID& id, Frequency frequency, Type type, AbstractValuePtr value) = 0;
virtual void swizzle(const char* swizzle) = 0;
- virtual void index() = 0;
virtual void apply(const FunctionBase* function, unsigned argCount) = 0;
};
@@ -89,17 +88,6 @@ namespace ren {
const char* const swizzle;
};
- class Index : public Expression {
- public:
- Index(const ExpressionPtr& base, const ExpressionPtr& index)
- : Expression(base->type.withoutArray(), {base, index})
- {}
-
- void walk(ExpressionWalker& w) {
- w.index();
- }
- };
-
class FunctionCall : public Expression {
public:
FunctionCall(const FunctionBase* fn, const ExpressionPtr& a0, const ExpressionPtr& a1)
View
@@ -5,29 +5,42 @@
namespace ren {
+ enum FunctionType {
+ FUNCTION,
+ OPERATOR,
+ INDEX,
+ };
+
struct FunctionBase {
- FunctionBase(bool is_operator, const char* glsl_name, const Signature& signature)
- : is_operator(is_operator)
+ FunctionBase(FunctionType function_type, const char* glsl_name, const Signature& signature)
+ : function_type(function_type)
, glsl_name(glsl_name)
, signature(signature)
{}
- const bool is_operator;
+ const FunctionType function_type;
const char* const glsl_name;
const Signature signature;
};
template<typename SignatureT>
struct Function : FunctionBase {
Function(const char* name)
- : FunctionBase(false, name, getSignature<SignatureT>())
+ : FunctionBase(FUNCTION, name, getSignature<SignatureT>())
{}
};
template<typename SignatureT>
struct Operator : FunctionBase {
Operator(const char* name)
- : FunctionBase(true, name, getSignature<SignatureT>())
+ : FunctionBase(OPERATOR, name, getSignature<SignatureT>())
+ {}
+ };
+
+ template<typename SignatureT>
+ struct Index : FunctionBase {
+ Index()
+ : FunctionBase(INDEX, "???", getSignature<SignatureT>())
{}
};
@@ -42,4 +55,5 @@ namespace ren {
Operator<mat4(mat4,mat4)> mat4_times_mat4("*");
Operator<vec4(mat4,vec4)> mat4_times_vec4("*");
Operator<ivec4(int,ivec4)> int_times_ivec4("*");
+ Index<vec4(vec4[],int)> vec4_index;
}
View
@@ -93,38 +93,26 @@ namespace ren {
stack.push((*p)[id].first.second);
}
- void pushInt(int i) {
- char p[80];
- sprintf(p, "%d", i);
- stack.push(p);
- }
-
- void pushFloat(float f) {
- char p[80];
- sprintf(p, "%f", f);
- stack.push(p);
- }
-
void swizzle(const char* swizzle) {
std::string base(popTop());
stack.push(base + "." + swizzle);
}
- void index() {
- std::string index(popTop());
- std::string base(popTop());
- stack.push(base + "[" + index + "]");
- }
-
void apply(const FunctionBase* function, unsigned argCount) {
std::vector<std::string> args;
while (argCount--) {
args.push_back(popTop());
}
- if (function->is_operator) {
+ if (OPERATOR == function->function_type) {
verify(2 == function->signature.arguments.size());
verify(2 == args.size());
stack.push(std::string("(") + args[1] + " " + function->glsl_name + " " + args[0] + ")");
+ } else if (INDEX == function->function_type) {
+ verify(2 == function->signature.arguments.size());
+ verify(2 == args.size());
+ std::string index = args[0];
+ std::string array = args[1];
+ stack.push(array + "[" + index + "]");
} else {
std::string n(function->glsl_name);
n += "(";
View
@@ -80,7 +80,10 @@ namespace ren {
const Type MAT4(Type::matrix(Type::FLOAT, 4, 4));
template<typename T> struct TypeMap;
-#define REN_TYPEMAP(c, g) template<> struct TypeMap<c> { static Type get() { return g; } };
+#define REN_TYPEMAP(c, g) \
+ template<> struct TypeMap<c> { static Type get() { return g; } }; \
+ template<> struct TypeMap<c*> { static Type get() { Type r = TypeMap<c>::get(); r.is_array = true; return r; } };
+
REN_TYPEMAP(bool, BOOL);
REN_TYPEMAP(class bvec2, BVEC2);
REN_TYPEMAP(class bvec3, BVEC3);

0 comments on commit 631b343

Please sign in to comment.