added assignment expressions, for loop nodes, boolean expressions, ForNode class, BoolExpressionNode class, AssignmentNode class
git-svn-id: svn://anubis/fart/branches/scene-file-scripting@326 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
442a7bdeda
commit
741ca57e11
@ -44,3 +44,29 @@ double BinOpNode::getNumber()
|
|||||||
exit(-3);
|
exit(-3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int BoolExpressionNode::getInteger()
|
||||||
|
{
|
||||||
|
double o, t;
|
||||||
|
if (m_op != '!')
|
||||||
|
{
|
||||||
|
o = one->getNumber();
|
||||||
|
t = two->getNumber();
|
||||||
|
}
|
||||||
|
switch (m_op)
|
||||||
|
{
|
||||||
|
case '<':
|
||||||
|
return o < t;
|
||||||
|
case '>':
|
||||||
|
return o > t;
|
||||||
|
case '=':
|
||||||
|
return o == t;
|
||||||
|
case 'n':
|
||||||
|
return o != t;
|
||||||
|
case '!':
|
||||||
|
return ! one->getInteger();
|
||||||
|
case 'T':
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
@ -425,6 +425,20 @@ class ExpressionNode : public Node
|
|||||||
virtual int getInteger() { return getNumber(); }
|
virtual int getInteger() { return getNumber(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class AssignmentNode : public ExpressionNode
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AssignmentNode(refptr<Node> varref, refptr<Node> expr)
|
||||||
|
: m_varref(varref), m_expr(expr)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
std::string getString() { return m_varref->getString(); }
|
||||||
|
double getNumber() { return m_expr->getNumber(); }
|
||||||
|
protected:
|
||||||
|
refptr<Node> m_varref;
|
||||||
|
refptr<Node> m_expr;
|
||||||
|
};
|
||||||
|
|
||||||
class BinOpNode : public ExpressionNode
|
class BinOpNode : public ExpressionNode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -439,6 +453,20 @@ class BinOpNode : public ExpressionNode
|
|||||||
refptr<Node> two;
|
refptr<Node> two;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class BoolExpressionNode : public Node
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BoolExpressionNode(char op, refptr<Node> one, refptr<Node> two)
|
||||||
|
: m_op(op), one(one), two(two)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
int getInteger();
|
||||||
|
protected:
|
||||||
|
char m_op;
|
||||||
|
refptr<Node> one;
|
||||||
|
refptr<Node> two;
|
||||||
|
};
|
||||||
|
|
||||||
class VarRefNode : public Node
|
class VarRefNode : public Node
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -448,5 +476,16 @@ class VarRefNode : public Node
|
|||||||
std::string m_string;
|
std::string m_string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ForNode : public Node
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ForNode(refptr<Node> e1, refptr<Node> e2, refptr<Node> e3)
|
||||||
|
: m_e1(e1), m_e2(e2), m_e3(e3)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
protected:
|
||||||
|
refptr<Node> m_e1, m_e2, m_e3;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -17,6 +17,10 @@
|
|||||||
\* return TIMES;
|
\* return TIMES;
|
||||||
\/ return DIVIDE;
|
\/ return DIVIDE;
|
||||||
% return MOD;
|
% return MOD;
|
||||||
|
:= return ASSIGN;
|
||||||
|
= return EQUALS;
|
||||||
|
!= return NOTEQUALS;
|
||||||
|
! return NOT;
|
||||||
|
|
||||||
; return SEMICOLON;
|
; return SEMICOLON;
|
||||||
: return COLON;
|
: return COLON;
|
||||||
@ -81,6 +85,12 @@ up return UP;
|
|||||||
vfov return VFOV;
|
vfov return VFOV;
|
||||||
width return WIDTH;
|
width return WIDTH;
|
||||||
|
|
||||||
|
else return ELSE;
|
||||||
|
elsif return ELSIF;
|
||||||
|
for return FOR;
|
||||||
|
if return IF;
|
||||||
|
while return WHILE;
|
||||||
|
|
||||||
[a-zA-Z_][a-zA-Z_0-9]* {
|
[a-zA-Z_][a-zA-Z_0-9]* {
|
||||||
*yylval = new IdentifierNode(yytext);
|
*yylval = new IdentifierNode(yytext);
|
||||||
return IDENTIFIER;
|
return IDENTIFIER;
|
||||||
|
@ -36,6 +36,10 @@ static refptr<Node> parsed_scene_node;
|
|||||||
%token TIMES;
|
%token TIMES;
|
||||||
%token DIVIDE;
|
%token DIVIDE;
|
||||||
%token MOD;
|
%token MOD;
|
||||||
|
%token ASSIGN;
|
||||||
|
%token EQUALS;
|
||||||
|
%token NOTEQUALS;
|
||||||
|
%token NOT;
|
||||||
|
|
||||||
%token SEMICOLON;
|
%token SEMICOLON;
|
||||||
%token COLON;
|
%token COLON;
|
||||||
@ -103,6 +107,13 @@ static refptr<Node> parsed_scene_node;
|
|||||||
%token IDENTIFIER;
|
%token IDENTIFIER;
|
||||||
%token VARREF;
|
%token VARREF;
|
||||||
|
|
||||||
|
%token ELSE;
|
||||||
|
%token ELSIF;
|
||||||
|
%token FOR;
|
||||||
|
%token IF;
|
||||||
|
%token WHILE;
|
||||||
|
|
||||||
|
%right ASSIGN
|
||||||
%left PLUS MINUS
|
%left PLUS MINUS
|
||||||
%left TIMES DIVIDE
|
%left TIMES DIVIDE
|
||||||
%left UMINUS
|
%left UMINUS
|
||||||
@ -218,6 +229,21 @@ extrude_item: polygon { $$ = $1; }
|
|||||||
| shape_item { $$ = $1; }
|
| shape_item { $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
general_items: /* empty */
|
||||||
|
| general_item general_items {
|
||||||
|
$$ = new ItemsNode();
|
||||||
|
$$->addChild($1);
|
||||||
|
$$->addChildren($2);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
general_item: transform_block { $$ = $1; }
|
||||||
|
| material_definition { $$ = $1; }
|
||||||
|
| shape_definition { $$ = $1; }
|
||||||
|
| shape { $$ = $1; }
|
||||||
|
| for { $$ = $1; }
|
||||||
|
;
|
||||||
|
|
||||||
intersect: INTERSECT LCURLY bool_items RCURLY {
|
intersect: INTERSECT LCURLY bool_items RCURLY {
|
||||||
$$ = new IntersectNode();
|
$$ = new IntersectNode();
|
||||||
$$->addChildren($3);
|
$$->addChildren($3);
|
||||||
@ -437,12 +463,9 @@ scene_items: /* empty */
|
|||||||
;
|
;
|
||||||
|
|
||||||
scene_item: camera { $$ = $1; }
|
scene_item: camera { $$ = $1; }
|
||||||
| shape { $$ = $1; }
|
|
||||||
| options { $$ = $1; }
|
|
||||||
| light { $$ = $1; }
|
| light { $$ = $1; }
|
||||||
| transform_block { $$ = $1; }
|
| options { $$ = $1; }
|
||||||
| material_definition { $$ = $1; }
|
| general_item { $$ = $1; }
|
||||||
| shape_definition { $$ = $1; }
|
|
||||||
;
|
;
|
||||||
|
|
||||||
shape: plane { $$ = $1; }
|
shape: plane { $$ = $1; }
|
||||||
@ -523,37 +546,24 @@ transform: TRANSLATE vector3 {
|
|||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
transform_block: TRANSLATE vector3 LCURLY transform_block_items RCURLY {
|
transform_block: TRANSLATE vector3 LCURLY general_items RCURLY {
|
||||||
$$ = new TranslateBlockNode($2);
|
$$ = new TranslateBlockNode($2);
|
||||||
$$->addChildren($4);
|
$$->addChildren($4);
|
||||||
}
|
}
|
||||||
| ROTATE expression COMMA vector3 LCURLY transform_block_items RCURLY {
|
| ROTATE expression COMMA vector3 LCURLY general_items RCURLY {
|
||||||
$$ = new RotateBlockNode($2, $4);
|
$$ = new RotateBlockNode($2, $4);
|
||||||
$$->addChildren($6);
|
$$->addChildren($6);
|
||||||
}
|
}
|
||||||
| SCALE vector3 LCURLY transform_block_items RCURLY {
|
| SCALE vector3 LCURLY general_items RCURLY {
|
||||||
$$ = new ScaleBlockNode($2);
|
$$ = new ScaleBlockNode($2);
|
||||||
$$->addChildren($4);
|
$$->addChildren($4);
|
||||||
}
|
}
|
||||||
| SCALE expression LCURLY transform_block_items RCURLY {
|
| SCALE expression LCURLY general_items RCURLY {
|
||||||
$$ = new ScaleBlockNode(new VectorNode($2, $2, $2));
|
$$ = new ScaleBlockNode(new VectorNode($2, $2, $2));
|
||||||
$$->addChildren($4);
|
$$->addChildren($4);
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
transform_block_items: /* empty */
|
|
||||||
| transform_block_item transform_block_items {
|
|
||||||
$$ = new ItemsNode();
|
|
||||||
$$->addChild($1);
|
|
||||||
$$->addChildren($2);
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
transform_block_item: transform_block { $$ = $1; }
|
|
||||||
| shape { $$ = $1; }
|
|
||||||
| shape_definition { $$ = $1; }
|
|
||||||
;
|
|
||||||
|
|
||||||
union: UNION LCURLY bool_items RCURLY {
|
union: UNION LCURLY bool_items RCURLY {
|
||||||
$$ = new UnionNode();
|
$$ = new UnionNode();
|
||||||
$$->addChildren($3);
|
$$->addChildren($3);
|
||||||
@ -576,8 +586,26 @@ expression: term { $$ = $1; }
|
|||||||
| MINUS expression %prec UMINUS {
|
| MINUS expression %prec UMINUS {
|
||||||
$$ = new BinOpNode('-', new NumberNode(0.0), $2);
|
$$ = new BinOpNode('-', new NumberNode(0.0), $2);
|
||||||
}
|
}
|
||||||
|
| assignment { $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
bool_expression: expression LESS expression {
|
||||||
|
$$ = new BoolExpressionNode('<', $1, $3);
|
||||||
|
}
|
||||||
|
| expression GREATER expression {
|
||||||
|
$$ = new BoolExpressionNode('>', $1, $3);
|
||||||
|
}
|
||||||
|
| expression EQUALS expression {
|
||||||
|
$$ = new BoolExpressionNode('=', $1, $3);
|
||||||
|
}
|
||||||
|
| expression NOTEQUALS expression {
|
||||||
|
$$ = new BoolExpressionNode('n', $1, $3);
|
||||||
|
}
|
||||||
|
| NOT bool_expression {
|
||||||
|
$$ = new BoolExpressionNode('!', $2, NULL);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
term: factor { $$ = $1; }
|
term: factor { $$ = $1; }
|
||||||
| term PLUS factor { $$ = new BinOpNode('+', $1, $3); }
|
| term PLUS factor { $$ = new BinOpNode('+', $1, $3); }
|
||||||
| term MINUS factor { $$ = new BinOpNode('-', $1, $3); }
|
| term MINUS factor { $$ = new BinOpNode('-', $1, $3); }
|
||||||
@ -588,6 +616,17 @@ factor: number { $$ = $1; }
|
|||||||
| LPAREN expression RPAREN { $$ = $2; }
|
| LPAREN expression RPAREN { $$ = $2; }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
assignment: VARREF ASSIGN expression {
|
||||||
|
$$ = new AssignmentNode($1, $3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
for: FOR LPAREN expression SEMICOLON bool_expression SEMICOLON expression RPAREN LCURLY general_items RCURLY {
|
||||||
|
$$ = new ForNode($3, $5, $7);
|
||||||
|
$$->addChildren($10);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
refptr<Node> parse(const char * fileName)
|
refptr<Node> parse(const char * fileName)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user