Commit 470d5004 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter
Browse files

complete support for prefix increment and decrement operator

2006-10-05  Jürg Billeter  <j@bitron.ch>

	* vala/parser.y, vala/valasemanticanalyzer.vala: complete support for
	  prefix increment and decrement operator

svn path=/trunk/; revision=142
parent f6dad615
2006-10-05 Jürg Billeter <j@bitron.ch>
* vala/parser.y, vala/valasemanticanalyzer.vala: complete support for
prefix increment and decrement operator
2006-10-05 Jürg Billeter <j@bitron.ch>
* vala/valasemanticanalyzer.vala: transform compund assignments into
......
......@@ -232,6 +232,8 @@ static void yyerror (YYLTYPE *locp, ValaParser *parser, const char *msg);
%type <expression> object_creation_expression
%type <expression> typeof_expression
%type <expression> unary_expression
%type <expression> pre_increment_expression
%type <expression> pre_decrement_expression
%type <expression> cast_expression
%type <expression> multiplicative_expression
%type <expression> additive_expression
......@@ -818,35 +820,43 @@ unary_expression
g_object_unref (src);
g_object_unref ($2);
}
| OP_INC unary_expression
| pre_increment_expression
| pre_decrement_expression
| REF unary_expression
{
ValaSourceReference *src = src(@1);
$$ = VALA_EXPRESSION (vala_unary_expression_new (VALA_UNARY_OPERATOR_INCREMENT, $2, src));
g_object_unref ($2);
$$ = VALA_EXPRESSION (vala_unary_expression_new (VALA_UNARY_OPERATOR_REF, $2, src));
g_object_unref (src);
g_object_unref ($2);
}
| OP_DEC unary_expression
| OUT unary_expression
{
ValaSourceReference *src = src(@1);
$$ = VALA_EXPRESSION (vala_unary_expression_new (VALA_UNARY_OPERATOR_DECREMENT, $2, src));
g_object_unref ($2);
$$ = VALA_EXPRESSION (vala_unary_expression_new (VALA_UNARY_OPERATOR_OUT, $2, src));
g_object_unref (src);
g_object_unref ($2);
}
| REF unary_expression
| cast_expression
;
pre_increment_expression
: OP_INC unary_expression
{
ValaSourceReference *src = src(@1);
$$ = VALA_EXPRESSION (vala_unary_expression_new (VALA_UNARY_OPERATOR_REF, $2, src));
g_object_unref (src);
$$ = VALA_EXPRESSION (vala_unary_expression_new (VALA_UNARY_OPERATOR_INCREMENT, $2, src));
g_object_unref ($2);
g_object_unref (src);
}
| OUT unary_expression
;
pre_decrement_expression
: OP_DEC unary_expression
{
ValaSourceReference *src = src(@1);
$$ = VALA_EXPRESSION (vala_unary_expression_new (VALA_UNARY_OPERATOR_OUT, $2, src));
g_object_unref (src);
$$ = VALA_EXPRESSION (vala_unary_expression_new (VALA_UNARY_OPERATOR_DECREMENT, $2, src));
g_object_unref ($2);
g_object_unref (src);
}
| cast_expression
;
cast_expression
......@@ -1366,6 +1376,8 @@ statement_expression
| assignment
| post_increment_expression
| post_decrement_expression
| pre_increment_expression
| pre_decrement_expression
;
selection_statement
......
......@@ -1131,6 +1131,24 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
check_arguments (expr, m.symbol, m.get_parameters (), expr.get_argument_list ());
}
}
private bool is_numeric_type (TypeReference! type) {
if (!(type.data_type is Struct)) {
return false;
}
var st = (Struct) type.data_type;
return st.is_integer_type () || st.is_floating_type ();
}
private bool is_integer_type (TypeReference! type) {
if (!(type.data_type is Struct)) {
return false;
}
var st = (Struct) type.data_type;
return st.is_integer_type ();
}
public override void visit_unary_expression (UnaryExpression! expr) {
if (expr.inner.error) {
......@@ -1141,24 +1159,55 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
if (expr.operator == UnaryOperator.PLUS || expr.operator == UnaryOperator.MINUS) {
// integer or floating point type
if (!is_numeric_type (expr.inner.static_type)) {
expr.error = true;
Report.error (expr.source_reference, "Operator not supported for `%s'".printf (expr.inner.static_type.to_string ()));
return;
}
expr.static_type = expr.inner.static_type;
} else if (expr.operator == UnaryOperator.LOGICAL_NEGATION) {
// boolean type
if (expr.inner.static_type.data_type != bool_type.data_type) {
expr.error = true;
Report.error (expr.source_reference, "Operator not supported for `%s'".printf (expr.inner.static_type.to_string ()));
return;
}
expr.static_type = expr.inner.static_type;
} else if (expr.operator == UnaryOperator.BITWISE_COMPLEMENT) {
// integer type
if (!is_integer_type (expr.inner.static_type)) {
expr.error = true;
Report.error (expr.source_reference, "Operator not supported for `%s'".printf (expr.inner.static_type.to_string ()));
return;
}
expr.static_type = expr.inner.static_type;
} else if (expr.operator == UnaryOperator.INCREMENT) {
// integer type
expr.static_type = expr.inner.static_type;
} else if (expr.operator == UnaryOperator.DECREMENT) {
} else if (expr.operator == UnaryOperator.INCREMENT ||
expr.operator == UnaryOperator.DECREMENT) {
// integer type
expr.static_type = expr.inner.static_type;
if (!is_integer_type (expr.inner.static_type)) {
expr.error = true;
Report.error (expr.source_reference, "Operator not supported for `%s'".printf (expr.inner.static_type.to_string ()));
return;
}
if (!(expr.inner is MemberAccess)) {
expr.error = true;
Report.error (expr.source_reference, "Prefix operators currently not supported for this expression");
return;
}
var ma = (MemberAccess) expr.inner;
var old_value = new MemberAccess (ma.inner, ma.member_name);
var bin = new BinaryExpression (expr.operator == UnaryOperator.INCREMENT ? BinaryOperator.PLUS : BinaryOperator.MINUS, old_value, new LiteralExpression (new IntegerLiteral ("1")));
var assignment = new Assignment (expr.inner, bin);
expr.parent_node.replace (expr, assignment);
assignment.accept (this);
return;
} else if (expr.operator == UnaryOperator.REF) {
// value type
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment