else_if(false)
{ }
-string ProgramCompiler::Formatter::format_expression(Expression &expr)
+void ProgramCompiler::Formatter::visit(Literal &literal)
{
- return join(expr.tokens.begin(), expr.tokens.end(), string());
+ formatted += literal.token;
+}
+
+void ProgramCompiler::Formatter::visit(ParenthesizedExpression &parexpr)
+{
+ formatted += '(';
+ parexpr.expression->visit(*this);
+ formatted += ')';
+}
+
+void ProgramCompiler::Formatter::visit(VariableReference &var)
+{
+ formatted += var.name;
+}
+
+void ProgramCompiler::Formatter::visit(MemberAccess &memacc)
+{
+ memacc.left->visit(*this);
+ formatted += format(".%s", memacc.member);
+}
+
+void ProgramCompiler::Formatter::visit(UnaryExpression &unary)
+{
+ if(unary.prefix)
+ formatted += unary.oper;
+ unary.expression->visit(*this);
+ if(!unary.prefix)
+ formatted += unary.oper;
+}
+
+void ProgramCompiler::Formatter::visit(BinaryExpression &binary)
+{
+ binary.left->visit(*this);
+ if(binary.assignment)
+ formatted += format(" %s ", binary.oper);
+ else
+ formatted += binary.oper;
+ binary.right->visit(*this);
+ formatted += binary.after;
+}
+
+void ProgramCompiler::Formatter::visit(FunctionCall &call)
+{
+ formatted += format("%s(", call.name);
+ for(vector<NodePtr<Expression> >::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
+ {
+ if(i!=call.arguments.begin())
+ formatted += ", ";
+ (*i)->visit(*this);
+ }
+ formatted += ')';
}
void ProgramCompiler::Formatter::visit(ExpressionStatement &expr)
{
- formatted += format("%s;", format_expression(expr.expression));
+ expr.expression->visit(*this);
+ formatted += ';';
}
void ProgramCompiler::Formatter::visit(Block &block)
bool change_indent = (!formatted.empty() && !else_if);
indent += change_indent;
string spaces(indent*2, ' ');
- for(vector<Node *>::const_iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ for(vector<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
{
if(i!=block.body.begin())
formatted += '\n';
formatted += format("%s ", var.interface);
formatted += format("%s %s", var.type, var.name);
if(var.array)
- formatted += format("[%s]", format_expression(var.array_size));
- if(!var.init_expression.empty())
- formatted += format(" = %s", format_expression(var.init_expression));
+ {
+ formatted += '[';
+ if(var.array_size)
+ var.array_size->visit(*this);
+ formatted += ']';
+ }
+ if(var.init_expression)
+ {
+ formatted += " = ";
+ var.init_expression->visit(*this);
+ }
if(!parameter_list)
formatted += ';';
}
{
formatted += format("%s %s(", func.return_type, func.name);
parameter_list = true;
- for(vector<VariableDeclaration *>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
+ for(vector<NodePtr<VariableDeclaration> >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
{
if(i!=func.parameters.begin())
formatted += ", ";
formatted += ' ';
else_if = false;
}
- formatted += format("if(%s)\n", format_expression(cond.condition));
+
+ formatted += "if(";
+ cond.condition->visit(*this);
+ formatted += ")\n";
+
cond.body.visit(*this);
if(!cond.else_body.body.empty())
{
{
formatted += "for(";
iter.init_statement->visit(*this);
- formatted += format(" %s; %s)\n", format_expression(iter.condition), format_expression(iter.loop_expression));
+ formatted += ' ';
+ iter.condition->visit(*this);
+ formatted += "; ";
+ iter.loop_expression->visit(*this);
+ formatted += ")\n";
iter.body.visit(*this);
}
void ProgramCompiler::Formatter::visit(Return &ret)
{
- formatted += format("return %s;", format_expression(ret.expression));
+ formatted += "return ";
+ ret.expression->visit(*this);
+ formatted += ';';
}
} // namespace GL
using namespace ProgramSyntax;
+ProgramParser::Operator ProgramParser::operators[] =
+{
+ { "[", 2, BINARY, LEFT_TO_RIGHT },
+ { "(", 2, BINARY, LEFT_TO_RIGHT },
+ { ".", 2, BINARY, LEFT_TO_RIGHT },
+ { "++", 2, POSTFIX, LEFT_TO_RIGHT },
+ { "--", 2, POSTFIX, LEFT_TO_RIGHT },
+ { "++", 3, PREFIX, RIGHT_TO_LEFT },
+ { "--", 3, PREFIX, RIGHT_TO_LEFT },
+ { "+", 3, PREFIX, RIGHT_TO_LEFT },
+ { "-", 3, PREFIX, RIGHT_TO_LEFT },
+ { "~", 3, PREFIX, RIGHT_TO_LEFT },
+ { "!", 3, PREFIX, RIGHT_TO_LEFT },
+ { "*", 4, BINARY, LEFT_TO_RIGHT },
+ { "/", 4, BINARY, LEFT_TO_RIGHT },
+ { "%", 4, BINARY, LEFT_TO_RIGHT },
+ { "+", 5, BINARY, LEFT_TO_RIGHT },
+ { "-", 5, BINARY, LEFT_TO_RIGHT },
+ { "<<", 6, BINARY, LEFT_TO_RIGHT },
+ { ">>", 6, BINARY, LEFT_TO_RIGHT },
+ { "<", 7, BINARY, LEFT_TO_RIGHT },
+ { ">", 7, BINARY, LEFT_TO_RIGHT },
+ { "<=", 7, BINARY, LEFT_TO_RIGHT },
+ { ">=", 7, BINARY, LEFT_TO_RIGHT },
+ { "==", 8, BINARY, LEFT_TO_RIGHT },
+ { "!=", 8, BINARY, LEFT_TO_RIGHT },
+ { "&", 9, BINARY, LEFT_TO_RIGHT },
+ { "^", 10, BINARY, LEFT_TO_RIGHT },
+ { "|", 11, BINARY, LEFT_TO_RIGHT },
+ { "&&", 12, BINARY, LEFT_TO_RIGHT },
+ { "^^", 13, BINARY, LEFT_TO_RIGHT },
+ { "||", 14, BINARY, LEFT_TO_RIGHT },
+ { "?", 15, BINARY, RIGHT_TO_LEFT },
+ { ":", 15, BINARY, RIGHT_TO_LEFT },
+ { "=", 16, BINARY, RIGHT_TO_LEFT },
+ { "+=", 16, BINARY, RIGHT_TO_LEFT },
+ { "-=", 16, BINARY, RIGHT_TO_LEFT },
+ { "*=", 16, BINARY, RIGHT_TO_LEFT },
+ { "/=", 16, BINARY, RIGHT_TO_LEFT },
+ { "%=", 16, BINARY, RIGHT_TO_LEFT },
+ { "<<=", 16, BINARY, RIGHT_TO_LEFT },
+ { ">>=", 16, BINARY, RIGHT_TO_LEFT },
+ { "&=", 16, BINARY, RIGHT_TO_LEFT },
+ { "^=", 16, BINARY, RIGHT_TO_LEFT },
+ { "|=", 16, BINARY, RIGHT_TO_LEFT },
+ { ",", 17, BINARY, LEFT_TO_RIGHT },
+ { { 0 }, 18, NO_OPERATOR, LEFT_TO_RIGHT }
+};
+
Module &ProgramParser::parse(const string &s)
{
source = s;
string ProgramParser::parse_other()
{
- string token;
- while(iter!=source.end())
+ if(iter==source.end())
+ return string();
+
+ string token(1, *iter++);
+ for(unsigned i=1; (i<3 && iter!=source.end()); ++i)
{
- if(isalnum(*iter) || *iter=='_' || isspace(*iter))
+ bool matched = false;
+ for(const Operator *j=operators; (!matched && j->type); ++j)
+ {
+ matched = (j->token[i]==*iter);
+ for(unsigned k=0; (matched && k<i && j->token[k]); ++k)
+ matched = (j->token[k]==token[k]);
+ }
+
+ if(!matched)
break;
+
token += *iter++;
- if(*iter==';' || *iter=='(' || *iter==')' || *iter=='[' || *iter==']')
- break;
}
return token;
string ProgramParser::expect_identifier()
{
- static Regex re("^[a-zA-Z_][a-zA-Z0-9_]*$");
string token = parse_token();
- if(!re.match(token))
+ if(!is_identifier(token))
throw runtime_error(format("Parse error at '%s': expected an identifier", token));
return token;
}
return is_builtin_type(token) || cur_module->structs.count(token);
}
+bool ProgramParser::is_identifier(const string &token)
+{
+ static Regex re("^[a-zA-Z_][a-zA-Z0-9_]*$");
+ return re.match(token);
+}
+
Node *ProgramParser::parse_global_declaration()
{
string token = peek_token();
else if(!token.empty())
{
RefPtr<ExpressionStatement> expr = new ExpressionStatement;
- parse_expression(expr->expression);
+ expr->expression = parse_expression();
expect(";");
return expr.release();
if(have_braces)
expect("{");
- while(1)
+ if(have_braces)
{
- string token = peek_token();
- if(token=="}")
- break;
-
- block.body.push_back(parse_statement());
- if(!have_braces)
- break;
+ while(peek_token()!="}")
+ block.body.push_back(parse_statement());
}
+ else
+ block.body.push_back(parse_statement());
block.use_braces = (require_braces || block.body.size()!=1);
expect("}");
}
-void ProgramParser::parse_expression(Expression &expr)
+Expression *ProgramParser::parse_expression(unsigned precedence)
{
- unsigned nesting_level = 0;
- while(iter!=source.end())
+ RefPtr<Expression> left;
+ VariableReference *left_var = 0;
+ while(1)
{
string token = peek_token();
- if(token=="(" || token=="[")
- ++nesting_level;
- else if(token==")" || token=="]")
+
+ const Operator *oper = 0;
+ for(Operator *i=operators; (!oper && i->type); ++i)
+ if(token==i->token && (!left || i->type!=PREFIX) && (left || i->type!=POSTFIX))
+ oper = i;
+
+ if(token==";" || token==")" || token=="]" || token=="," || (oper && precedence && oper->precedence>=precedence))
{
- if(!nesting_level)
- break;
- --nesting_level;
+ if(left)
+ return left.release();
+ else
+ throw runtime_error(format("Parse error at '%s': expected an expression", token));
}
- else if(token==";")
- break;
+ else if(left)
+ {
+ if(token=="(")
+ {
+ if(!left_var)
+ throw runtime_error(format("Parse error at '%s': function name must be an identifier", token));
+ left = parse_function_call(left_var);
+ }
+ else if(token==".")
+ {
+ RefPtr<MemberAccess> memacc = new MemberAccess;
+ memacc->left = left.release();
+ parse_token();
+ memacc->member = expect_identifier();
+ left = memacc;
+ }
+ else if(oper && oper->type==POSTFIX)
+ {
+ RefPtr<UnaryExpression> unary = new UnaryExpression;
+ unary->oper = parse_token();
+ unary->prefix = false;
+ unary->expression = left.release();
+ left = unary;
+ }
+ else if(oper && oper->type==BINARY)
+ left = parse_binary(left.release(), oper);
+ else
+ throw runtime_error(format("Parse error at '%s': expected an operator", token));
+ left_var = 0;
+ }
+ else
+ {
+ if(token=="(")
+ {
+ parse_token();
+ RefPtr<ParenthesizedExpression> parexpr = new ParenthesizedExpression;
+ parexpr->expression = parse_expression();
+ expect(")");
+ left = parexpr;
+ }
+ else if(is_identifier(token))
+ {
+ RefPtr<VariableReference> var = new VariableReference;
+ var->name = expect_identifier();
+ left = var;
+ left_var = var.get();
+ }
+ else if(oper && oper->type==PREFIX)
+ {
+ RefPtr<UnaryExpression> unary = new UnaryExpression;
+ unary->oper = parse_token();
+ unary->prefix = true;
+ unary->expression = parse_expression(oper->precedence);
+ left = unary;
+ }
+ else if(isdigit(token[0]))
+ {
+ RefPtr<Literal> literal = new Literal;
+ literal->token = parse_token();
+ left = literal;
+ }
+ else
+ throw runtime_error(format("Parse error at '%s': expected an expression", token));
+ }
+ }
+}
- parse_token();
- expr.tokens.push_back(token);
+BinaryExpression *ProgramParser::parse_binary(Expression *left, const Operator *oper)
+{
+ RefPtr<BinaryExpression> binary = new BinaryExpression;
+ binary->left = left;
+ binary->oper = parse_token();
+ if(binary->oper=="[")
+ {
+ binary->right = parse_expression();
+ expect("]");
+ binary->after = "]";
}
+ else
+ binary->right = parse_expression(oper->precedence+(oper->assoc==RIGHT_TO_LEFT));
+ binary->assignment = (oper->precedence==16);
+ return binary.release();
+}
+
+FunctionCall *ProgramParser::parse_function_call(VariableReference *var)
+{
+ RefPtr<FunctionCall> call = new FunctionCall;
+ call->name = var->name;
+ call->constructor = is_type(call->name);
+ expect("(");
+ while(peek_token()!=")")
+ {
+ if(!call->arguments.empty())
+ expect(",");
+ call->arguments.push_back(parse_expression());
+ }
+ expect(")");
+ return call.release();
}
StructDeclaration *ProgramParser::parse_struct_declaration()
var->array = true;
if(!check("]"))
{
- parse_expression(var->array_size);
+ var->array_size = parse_expression();
expect("]");
}
}
if(check("="))
- parse_expression(var->init_expression);
+ var->init_expression = parse_expression();
expect(";");
return var.release();
void ProgramParser::parse_function_parameter_list(FunctionDeclaration &func)
{
expect("(");
- while(1)
+ while(peek_token()!=")")
{
- string token = peek_token();
- if(token==")")
- break;
- else if(!func.parameters.empty())
+ if(!func.parameters.empty())
expect(",");
RefPtr<VariableDeclaration> var = new VariableDeclaration;
expect("if");
expect("(");
RefPtr<Conditional> cond = new Conditional;
- parse_expression(cond->condition);
+ cond->condition = parse_expression();
expect(")");
parse_block(cond->body, false);
else
{
RefPtr<ExpressionStatement> expr = new ExpressionStatement;
- parse_expression(expr->expression);
+ expr->expression = parse_expression();
expect(";");
loop->init_statement = expr.release();
}
- parse_expression(loop->condition);
+ loop->condition = parse_expression();
expect(";");
- parse_expression(loop->loop_expression);
+ loop->loop_expression = parse_expression();
expect(")");
parse_block(loop->body, false);
{
expect("return");
RefPtr<Return> ret = new Return;
- parse_expression(ret->expression);
+ ret->expression = parse_expression();
expect(";");
return ret.release();
}
struct Node
{
+private:
+ Node &operator=(const Node &);
+public:
virtual ~Node() { }
+ virtual Node *clone() const = 0;
virtual void visit(NodeVisitor &) = 0;
};
+template<typename T>
+class NodePtr
+{
+private:
+ T *node;
+
+public:
+ NodePtr(T *n = 0): node(n) { }
+ NodePtr(const NodePtr &p): node(clone(p.node)) { }
+ NodePtr &operator=(const NodePtr &p) { delete node; node = clone(p.node); return *this; }
+ ~NodePtr() { delete node; }
+
+private:
+ static T *clone(T *n) { return n ? n->clone() : 0; }
+
+public:
+ T *operator->() { return node; }
+ const T *operator->() const { return node; }
+ operator void *() const { return node; }
+};
+
+struct VariableDeclaration;
+
struct Block: Node
{
- std::vector<Node *> body;
+ std::vector<NodePtr<Node> > body;
bool use_braces;
Block();
- virtual ~Block();
+ virtual Block *clone() const { return new Block(*this); }
+ virtual void visit(NodeVisitor &);
+};
+
+struct Expression: Node
+{
+ virtual Expression *clone() const = 0;
+};
+
+struct Literal: Expression
+{
+ std::string token;
+
+ virtual Literal *clone() const { return new Literal(*this); }
+ virtual void visit(NodeVisitor &);
+};
+
+struct ParenthesizedExpression: Expression
+{
+ NodePtr<Expression> expression;
+
+ virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
+ virtual void visit(NodeVisitor &);
+};
+
+struct VariableReference: Expression
+{
+ std::string name;
+
+ virtual VariableReference *clone() const { return new VariableReference(*this); }
+ virtual void visit(NodeVisitor &);
+};
+
+struct MemberAccess: Expression
+{
+ NodePtr<Expression> left;
+ std::string member;
+
+ virtual MemberAccess *clone() const { return new MemberAccess(*this); }
+ virtual void visit(NodeVisitor &);
+};
+
+struct UnaryExpression: Expression
+{
+ std::string oper;
+ NodePtr<Expression> expression;
+ bool prefix;
+
+ UnaryExpression();
+
+ virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
virtual void visit(NodeVisitor &);
};
-struct Expression
+struct BinaryExpression: Expression
{
- std::vector<std::string> tokens;
+ NodePtr<Expression> left;
+ std::string oper;
+ NodePtr<Expression> right;
+ std::string after;
+ bool assignment;
+
+ BinaryExpression();
- bool empty() const { return tokens.empty(); }
+ virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
+ virtual void visit(NodeVisitor &);
+};
+
+struct FunctionCall: Expression
+{
+ std::string name;
+ bool constructor;
+ std::vector<NodePtr<Expression> > arguments;
+
+ FunctionCall();
+
+ virtual FunctionCall *clone() const { return new FunctionCall(*this); }
+ virtual void visit(NodeVisitor &);
};
struct ExpressionStatement: Node
{
- Expression expression;
+ NodePtr<Expression> expression;
+ virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
virtual void visit(NodeVisitor &);
};
std::vector<Qualifier> qualifiers;
std::string interface;
+ virtual Layout *clone() const { return new Layout(*this); }
virtual void visit(NodeVisitor &);
};
StructDeclaration();
+ virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
virtual void visit(NodeVisitor &);
};
std::string type;
std::string name;
bool array;
- Expression array_size;
- Expression init_expression;
+ NodePtr<Expression> array_size;
+ NodePtr<Expression> init_expression;
VariableDeclaration();
+ virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
virtual void visit(NodeVisitor &);
};
InterfaceBlock();
+ virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
virtual void visit(NodeVisitor &);
};
{
std::string return_type;
std::string name;
- std::vector<VariableDeclaration *> parameters;
+ std::vector<NodePtr<VariableDeclaration> > parameters;
bool definition;
Block body;
FunctionDeclaration();
- ~FunctionDeclaration();
+ virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
virtual void visit(NodeVisitor &);
};
struct Conditional: Node
{
- Expression condition;
+ Expression *condition;
Block body;
Block else_body;
+ virtual Conditional *clone() const { return new Conditional(*this); }
virtual void visit(NodeVisitor &);
};
struct Iteration: Node
{
- Node *init_statement;
- Expression condition;
- Expression loop_expression;
+ NodePtr<Node> init_statement;
+ NodePtr<Expression> condition;
+ NodePtr<Expression> loop_expression;
Block body;
- Iteration();
- virtual ~Iteration();
-
+ virtual Iteration *clone() const { return new Iteration(*this); }
virtual void visit(NodeVisitor &);
};
struct Return: Node
{
- Expression expression;
+ NodePtr<Expression> expression;
+ virtual Return *clone() const { return new Return(*this); }
virtual void visit(NodeVisitor &);
};
virtual ~NodeVisitor() { }
virtual void visit(Block &) { }
+ virtual void visit(Literal &) { }
+ virtual void visit(ParenthesizedExpression &) { }
+ virtual void visit(VariableReference &) { }
+ virtual void visit(MemberAccess &) { }
+ virtual void visit(UnaryExpression &) { }
+ virtual void visit(BinaryExpression &) { }
+ virtual void visit(FunctionCall &) { }
virtual void visit(ExpressionStatement &) { }
virtual void visit(Layout &) { }
virtual void visit(StructDeclaration &) { }