Commit 23a4afab authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

support assign operators support assign operators and attributes visitor

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

	* vala/scanner.l: support assign operators
	* vala/parser.y: support assign operators and attributes
	* vala/valaattributeprocessor.vala: visitor to process compiler
	  attributes
	* vala/valacodegenerator.vala: support structs, enums, and methods
	* vala/valacodenode.vala: support attributes
	* vala/valaattribute.vala
	* vala/valamemberaccessibility.vala
	* vala/valamethod.vala
	* vala/valastruct.vala: support CCode (cname) attribute
	* vala/vala.h: update
	* compiler/valacompiler.vala: invoke AttributeProcessor
	* ccode/valaccodeblock.vala
	* ccode/valaccodeenum.vala
	* ccode/valaccodemethod.vala
	* ccode/valaccodemodifiers.vala
	* bindings/GLib.vala: add string cmp method
	* valac/scanner.l: support BITWISE_AND
	* valac/parser.y: support bitwise and
	* valac/context.h: add bitwise and
	* valac/generator.c: support enum dependencies and bitwise and

svn path=/trunk/; revision=16
parent 78d5467a
2006-05-15 Jürg Billeter <j@bitron.ch>
* vala/scanner.l: support assign operators
* vala/parser.y: support assign operators and attributes
* vala/valaattributeprocessor.vala: visitor to process compiler
attributes
* vala/valacodegenerator.vala: support structs, enums, and methods
* vala/valacodenode.vala: support attributes
* vala/valaattribute.vala
* vala/valamemberaccessibility.vala
* vala/valamethod.vala
* vala/valastruct.vala: support CCode (cname) attribute
* vala/vala.h: update
* compiler/valacompiler.vala: invoke AttributeProcessor
* ccode/valaccodeblock.vala
* ccode/valaccodeenum.vala
* ccode/valaccodemethod.vala
* ccode/valaccodemodifiers.vala
* bindings/GLib.vala: add string cmp method
* valac/scanner.l: support BITWISE_AND
* valac/parser.y: support bitwise and
* valac/context.h: add bitwise and
* valac/generator.c: support enum dependencies and bitwise and
2006-05-14 Jürg Billeter <j@bitron.ch>
* vala/scanner.l: support OPEN_CAST_PARENS, IS, WHILE
......
......@@ -110,6 +110,8 @@ public struct string {
public int len ();
[CCode (cname = "g_strcompress")]
public string# compress ();
[CCode (cname = "strcmp")]
public int cmp ();
}
[ReferenceType ()]
......
/* valaccodeblock.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public class CCodeBlock : CCodeNode {
List<string#># statements;
public void add_statement (string statement) {
statements.append (statement);
}
public override void write (CCodeWriter writer) {
writer.write_begin_block ();
foreach (string statement in statements) {
writer.write_indent ();
writer.write_string (statement);
writer.write_string (";\n");
}
writer.write_end_block ();
}
}
}
/* valaccodeenum.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public class CCodeEnum : CCodeNode {
public readonly ref string name;
ref List<ref string> values;
public void add_value (string name, string value) {
values.append ("%s = %s".printf (name, value));
}
public override void write (CCodeWriter writer) {
writer.write_string ("enum ");
writer.write_string (name);
writer.write_begin_block ();
foreach (string value in values) {
writer.write_indent ();
writer.write_string (value);
writer.write_string (",\n");
}
writer.write_end_block ();
writer.write_string (";\n");
}
}
}
/* valaccodemethod.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public class CCodeMethod : CCodeNode {
public readonly ref string name;
public CCodeModifiers modifiers;
public readonly ref string return_type;
ref List<ref string> parameters;
public ref CCodeBlock block;
public void add_parameter (string type, string name) {
parameters.append ("%s %s".printf (type, name));
}
public override void write (CCodeWriter writer) {
if ((modifiers & CCodeModifiers.STATIC) == CCodeModifiers.STATIC) {
writer.write_string ("static ");
}
writer.write_string (return_type);
writer.write_string (" ");
writer.write_string (name);
writer.write_string (" (");
foreach (string parameter in parameters) {
writer.write_string (parameter);
}
writer.write_string (")");
if (block == null) {
writer.write_string (";\n");
} else {
block.write (writer);
}
}
}
}
/* valaccodemodifiers.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public enum /* flags */ CCodeModifiers {
NONE,
STATIC
}
}
......@@ -52,6 +52,9 @@ namespace Vala {
var resolver = new SymbolResolver ();
resolver.resolve (context);
var attributeprocessor = new AttributeProcessor ();
attributeprocessor.process (context);
var code_generator = new CodeGenerator ();
code_generator.emit (context);
}
......
......@@ -69,6 +69,7 @@ static void yyerror (YYLTYPE *locp, ValaParser *parser, const char *msg);
ValaLocalVariableDeclaration *local_variable_declaration;
ValaVariableDeclarator *variable_declarator;
ValaTypeParameter *type_parameter;
ValaAttribute *attribute;
ValaNamedArgument *named_argument;
}
......@@ -86,6 +87,17 @@ static void yyerror (YYLTYPE *locp, ValaParser *parser, const char *msg);
%token HASH "#"
%token INTERR "?"
%token ASSIGN_BITWISE_OR "|="
%token ASSIGN_BITWISE_AND "&="
%token ASSIGN_BITWISE_XOR "^="
%token ASSIGN_ADD "+="
%token ASSIGN_SUB "-="
%token ASSIGN_MUL "*="
%token ASSIGN_DIV "/="
%token ASSIGN_PERCENT "%="
%token ASSIGN_SHIFT_LEFT "<<="
%token ASSIGN_SHIFT_RIGHT ">>="
%token OP_INC "++"
%token OP_DEC "--"
%token OP_EQ "=="
......@@ -219,6 +231,13 @@ static void yyerror (YYLTYPE *locp, ValaParser *parser, const char *msg);
%type <method> method_declaration
%type <method> method_header
%type <statement> method_body
%type <list> opt_attributes
%type <list> attributes
%type <list> attribute_sections
%type <list> attribute_section
%type <list> attribute_list
%type <attribute> attribute
%type <str> attribute_name
%type <list> opt_named_argument_list
%type <list> named_argument_list
%type <named_argument> named_argument
......@@ -530,6 +549,16 @@ assignment
assignment_operator
: ASSIGN
| ASSIGN_BITWISE_OR
| ASSIGN_BITWISE_AND
| ASSIGN_BITWISE_XOR
| ASSIGN_ADD
| ASSIGN_SUB
| ASSIGN_MUL
| ASSIGN_DIV
| ASSIGN_PERCENT
| ASSIGN_SHIFT_LEFT
| ASSIGN_SHIFT_RIGHT
;
opt_expression
......@@ -965,6 +994,7 @@ method_header
: comment opt_attributes opt_access_modifier opt_modifiers opt_ref type identifier_or_new OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS
{
$$ = vala_method_new ($7, src_com (@7, $1));
VALA_CODE_NODE($$)->attributes = $2;
}
;
......@@ -1054,6 +1084,7 @@ struct_header
for (l = $6; l != NULL; l = l->next) {
vala_struct_add_type_parameter ($$, l->data);
}
VALA_CODE_NODE($$)->attributes = $2;
}
;
......@@ -1153,6 +1184,9 @@ flags_member_declaration
opt_attributes
: /* empty */
{
$$ = NULL;
}
| attributes
;
......@@ -1163,24 +1197,48 @@ attributes
attribute_sections
: attribute_section
| attribute_sections attribute_section
{
$$ = g_list_concat ($1, $2);
}
;
attribute_section
: OPEN_BRACKET attribute_list CLOSE_BRACKET
{
$$ = $2;
}
| OPEN_BRACKET error CLOSE_BRACKET
{
$$ = NULL;
}
;
attribute_list
: attribute
{
$$ = g_list_append (NULL, $1);
}
| attribute_list COMMA attribute
{
$$ = g_list_append ($1, $3);
}
;
attribute
: attribute_name OPEN_PARENS opt_named_argument_list CLOSE_PARENS
{
GList *l;
$$ = vala_attribute_new ($1, src (@1));
for (l = $3; l != NULL; l = l->next) {
vala_attribute_add_argument ($$, l->data);
}
}
;
attribute_name
: type_name
: IDENTIFIER
;
opt_named_argument_list
......
......@@ -69,6 +69,17 @@ literal ({literal_integer}|{literal_character}|{literal_string})
"#" { uploc; return HASH; }
"?" { uploc; return INTERR; }
"|=" { uploc; return ASSIGN_BITWISE_OR; }
"&=" { uploc; return ASSIGN_BITWISE_AND; }
"^=" { uploc; return ASSIGN_BITWISE_XOR; }
"+=" { uploc; return ASSIGN_ADD; }
"-=" { uploc; return ASSIGN_SUB; }
"*=" { uploc; return ASSIGN_MUL; }
"/=" { uploc; return ASSIGN_DIV; }
"%=" { uploc; return ASSIGN_PERCENT; }
"<<=" { uploc; return ASSIGN_SHIFT_LEFT; }
">>=" { uploc; return ASSIGN_SHIFT_RIGHT; }
"++" { uploc; return OP_INC; }
"--" { uploc; return OP_DEC; }
"==" { uploc; return OP_EQ; }
......
#include <vala/valaattribute.h>
#include <vala/valablock.h>
#include <vala/valabooleanliteral.h>
#include <vala/valacastexpression.h>
......
/* valaattribute.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public class Attribute : CodeNode {
public readonly ref string name;
public readonly ref SourceReference source_reference;
public ref List<ref NamedArgument> args;
public static ref Attribute new (string name, SourceReference source) {
return (new Attribute (name = name, source_reference = source));
}
public void add_argument (NamedArgument arg) {
args.append (arg);
}
}
}
/* valaattributeprocessor.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public class AttributeProcessor : CodeVisitor {
public void process (CodeContext context) {
context.accept (this);
}
public override void visit_begin_struct (Struct st) {
st.process_attributes ();
}
}
}
......@@ -34,6 +34,8 @@ namespace Vala {
CCodeFragment# source_type_member_definition;
CCodeStruct# instance_struct;
CCodeStruct# class_struct;
ref CCodeBlock block;
TypeReference reference; // dummy for dependency resolution
......@@ -82,12 +84,32 @@ namespace Vala {
}
public override void visit_begin_class (Class cl) {
instance_struct = new CCodeStruct (name = cl.name);
instance_struct = new CCodeStruct (name = "_%s".printf (cl.name));
class_struct = new CCodeStruct (name = "_%sClass".printf (cl.name));
if (cl.source_reference.comment != null) {
header_type_definition.append (new CCodeComment (text = cl.source_reference.comment));
}
header_type_definition.append (instance_struct);
header_type_definition.append (class_struct);
}
public override void visit_begin_struct (Struct st) {
instance_struct = new CCodeStruct (name = "_%s".printf (st.name));
if (st.source_reference.comment != null) {
header_type_definition.append (new CCodeComment (text = st.source_reference.comment));
}
header_type_definition.append (instance_struct);
}
public override void visit_enum (Enum en) {
instance_struct = new CCodeEnum (name = "_%s".printf (en.name));
if (en.source_reference.comment != null) {
header_type_definition.append (new CCodeComment (text = en.source_reference.comment));
}
header_type_definition.append (instance_struct);
}
public override void visit_field (Field f) {
......@@ -95,6 +117,20 @@ namespace Vala {
}
public override void visit_method (Method m) {
CCodeMethod cmethod_decl = new CCodeMethod (name = m.name, return_type = "void");
CCodeMethod cmethod_def = new CCodeMethod (name = m.name, return_type = "void");
if (m.access == MemberAccessibility.PUBLIC) {
header_type_member_declaration.append (cmethod_decl);
} else {
cmethod_decl.modifiers |= CCodeModifiers.STATIC;
cmethod_def.modifiers |= CCodeModifiers.STATIC;
source_type_member_declaration.append (cmethod_decl);
}
if (m.source_reference.comment != null) {
source_type_member_definition.append (new CCodeComment (text = m.source_reference.comment));
}
source_type_member_definition.append (cmethod_def);
}
}
}
......@@ -20,9 +20,12 @@
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public abstract class CodeNode {
public Symbol# symbol;
public ref List<ref Attribute> attributes;
public abstract void accept (CodeVisitor visitor);
}
......
/* valamemberaccessibility.vala
*
* Copyright (C) 2006 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
namespace Vala {
public enum MemberAccessibility {
PRIVATE,
PUBLIC
}
}
......@@ -24,7 +24,7 @@ using GLib;
namespace Vala {
public class Method : CodeNode {
public readonly string# name;
public readonly ref string name;
public readonly SourceReference# source_reference;
public CodeNode parent_type;
Statement# _body;
......@@ -36,6 +36,8 @@ namespace Vala {
_body = value;
}
}
public MemberAccessibility access;
public bool instance = true;
public static Method# @new (string name, SourceReference source) {
return (new Method (name = name, source_reference = source));
......@@ -44,5 +46,8 @@ namespace Vala {
public override void accept (CodeVisitor visitor) {
visitor.visit_method (this);
}
public string get_cname () {
}
}
}
......@@ -32,6 +32,8 @@ namespace Vala {
List<Field#># fields;
List<Method#># methods;
public ref string cname;
public static Struct# new (string name, SourceReference source) {
return (new Struct (name = name, source_reference = source));
}
......@@ -74,11 +76,40 @@ namespace Vala {
}
public override string get_cname () {
return name;
if (cname == null) {
cname = name;
}
return cname;
}
public void set_cname (string cname) {
this.cname = cname;
}
public override bool is_reference_type () {
return false;
}
void process_ccode_attribute (Attribute a) {
foreach (NamedArgument arg in a.args) {
if (arg.name.cmp ("cname") == 0) {
/* this will already be checked during semantic analysis */
if (arg.argument is LiteralExpression) {
var lit = ((LiteralExpression) arg.argument).literal;
if (lit is StringLiteral) {
set_cname (((StringLiteral) lit).eval ());
}
}
}
}
}
public void process_attributes () {
foreach (Attribute a in attributes) {
if (a.name.cmp ("CCode") == 0) {
process_ccode_attribute (a);
}
}
}
}
}
......@@ -132,6 +132,7 @@ enum _ValaOpType {
VALA_OP_TYPE_GE,
VALA_OP_TYPE_NEG,
VALA_OP_TYPE_AND,
VALA_OP_TYPE_BITWISE_AND,
VALA_OP_TYPE_OR,
};
......
......@@ -310,6 +310,9 @@ vala_code_generator_process_operation_expression (ValaCodeGenerator *generator,
case VALA_OP_TYPE_AND:
cop = "&&";
break;
case VALA_OP_TYPE_BITWISE_AND:
cop = "&";
break;
case VALA_OP_TYPE_OR:
cop = "||";
break;
......@@ -1813,6 +1816,8 @@ vala_code_generator_process_source_file (ValaCodeGenerator *generator, ValaSourc
fprintf (generator->h_file, "typedef struct _%s %s;\n", class->cname, class->cname);
fprintf (generator->h_file, "typedef struct _%sClass %sClass;\n", class->cname, class->cname);
fprintf (generator->h_file, "#endif\n");
} else if (sym->type == VALA_SYMBOL_TYPE_ENUM) {
vala_code_generator_process_dep_type (generator, generator->h_file, l->data, &dep_files);
}
vala_code_generator_process_dep_type (generator, generator->c_file, l->data, &dep_files);
}
......
......@@ -167,6 +167,7 @@ ValaLocation *get_location (int lineno, int colno)
%token OP_NEG "!"
%token OP_OR "||"
%token OP_AND "&&"
%token BITWISE_AND "&"
%token ASSIGN "="
%token PLUS "+"
......@@ -265,6 +266,7 @@ ValaLocation *get_location (int lineno, int colno)
%type <expression> object_creation_expression
%type <expression> equality_expression
%type <expression> relational_expression
%type <expression> and_expression
%type <expression> conditional_and_expression
%type <expression> conditional_or_expression
%type <expression> post_increment_expression
......@@ -1108,9 +1110,22 @@ equality_expression
}
;
conditional_and_expression
and_expression
: equality_expression
| conditional_and_expression OP_AND equality_expression
| and_expression BITWISE_AND equality_expression
{
$$ = g_new0 (ValaExpression, 1);
$$->type = VALA_EXPRESSION_TYPE_OPERATION;
$$->location = current_location (@1);
$$->op.left = $1;
$$->op.type = VALA_OP_TYPE_BITWISE_AND;
$$->op.right = $3;
}
;
conditional_and_expression
: and_expression
| conditional_and_expression OP_AND and_expression
{
$$ = g_new0 (ValaExpression, 1);
$$->type = VALA_EXPRESSION_TYPE_OPERATION;
......
......@@ -74,6 +74,7 @@ literal ({literal_integer}|{literal_character}|{literal_string})
">" { uploc; return OP_GT; }
"!" { uploc; return OP_NEG; }
"&&" { uploc; return OP_AND; }
"&" { uploc; return BITWISE_AND; }
"||" { uploc; return OP_OR; }
"=" { uploc; return ASSIGN; }
......
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