support single-line comments, ASSIGN, PLUS, RETURN, LITERAL_INTEGER,
authorJürg Billeter <j@bitron.ch>
Mon, 24 Apr 2006 14:00:03 +0000 (14:00 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Mon, 24 Apr 2006 14:00:03 +0000 (14:00 +0000)
2006-04-24  Jürg Billeter  <j@bitron.ch>

* valac/scanner.l: support single-line comments, ASSIGN, PLUS, RETURN,
  LITERAL_INTEGER, LITERAL_STRING
* valac/parser.y: add basic statements and expressions
* valac/context.h: add ValaStatement, ValaVariableDeclaration,
  ValaVariableDeclarator, and ValaExpression structs
* valac/context.c: remove unreachable line

svn path=/trunk/; revision=4

vala/ChangeLog
vala/valac/context.c
vala/valac/context.h
vala/valac/parser.y
vala/valac/scanner.l

index 019cd38..ee8e8ff 100644 (file)
@@ -1,5 +1,14 @@
 2006-04-24  Jürg Billeter  <j@bitron.ch>
 
+       * valac/scanner.l: support single-line comments, ASSIGN, PLUS, RETURN,
+         LITERAL_INTEGER, LITERAL_STRING
+       * valac/parser.y: add basic statements and expressions
+       * valac/context.h: add ValaStatement, ValaVariableDeclaration,
+         ValaVariableDeclarator, and ValaExpression structs
+       * valac/context.c: remove unreachable line
+
+2006-04-24  Jürg Billeter  <j@bitron.ch>
+
        * valac/scanner.l: set token location, support OPEN_PARENS,
          CLOSE_PARENS, SEMICOLON, PUBLIC, STATIC
        * valac/parser.y: save symbol location, output exact error location,
index baa534d..c39a28b 100644 (file)
@@ -176,7 +176,6 @@ vala_context_resolve_type_reference (ValaContext *context, ValaNamespace *namesp
                } else {
                        /* specified namespace not found */
                        err (type_reference->location, "error: specified type ´%s´ not found", type_reference->type_name);
-                       exit (1);
                }
        } else {
                /* namespace has been explicitly specified */
index b1c3ecb..d81a493 100644 (file)
@@ -24,6 +24,9 @@
 
 typedef enum _ValaSymbolType ValaSymbolType;
 typedef enum _ValaMethodFlags ValaMethodFlags;
+typedef enum _ValaStatementType ValaStatementType;
+typedef enum _ValaExpressionType ValaExpressionType;
+typedef enum _ValaOpType ValaOpType;
 
 typedef struct _ValaContext ValaContext;
 typedef struct _ValaSymbol ValaSymbol;
@@ -32,6 +35,10 @@ typedef struct _ValaLocation ValaLocation;
 typedef struct _ValaNamespace ValaNamespace;
 typedef struct _ValaClass ValaClass;
 typedef struct _ValaMethod ValaMethod;
+typedef struct _ValaStatement ValaStatement;
+typedef struct _ValaVariableDeclaration ValaVariableDeclaration;
+typedef struct _ValaVariableDeclarator ValaVariableDeclarator;
+typedef struct _ValaExpression ValaExpression;
 typedef struct _ValaTypeReference ValaTypeReference;
 typedef struct _ValaFormalParameter ValaFormalParameter;
 
@@ -47,6 +54,30 @@ enum _ValaMethodFlags {
        VALA_METHOD_STATIC = 0x02,
 };
 
+enum _ValaStatementType {
+       VALA_STATEMENT_TYPE_BLOCK,
+       VALA_STATEMENT_TYPE_EXPRESSION,
+       VALA_STATEMENT_TYPE_RETURN,
+       VALA_STATEMENT_TYPE_VARIABLE_DECLARATION,
+};
+
+enum _ValaExpressionType {
+       VALA_EXPRESSION_TYPE_ADDITIVE,
+       VALA_EXPRESSION_TYPE_ASSIGNMENT,
+       VALA_EXPRESSION_TYPE_EXPRESSION,
+       VALA_EXPRESSION_TYPE_INVOCATION,
+       VALA_EXPRESSION_TYPE_LITERAL_INTEGER,
+       VALA_EXPRESSION_TYPE_LITERAL_STRING,
+       VALA_EXPRESSION_TYPE_MEMBER_ACCESS,
+       VALA_EXPRESSION_TYPE_PARENTHESIZED,
+       VALA_EXPRESSION_TYPE_RETURN,
+       VALA_EXPRESSION_TYPE_SIMPLE_NAME,
+};
+
+enum _ValaOpType {
+       VALA_OP_TYPE_PLUS,
+};
+
 struct _ValaContext {
        GList *source_files;
        ValaSymbol *root;
@@ -100,6 +131,54 @@ struct _ValaMethod {
        ValaMethodFlags modifiers;
        char *cdecl1;
        char *cdecl2;
+       ValaStatement *body;
+};
+
+struct _ValaStatement {
+       ValaStatementType type;
+       union {
+               struct {
+                       GList *statements;
+               } block;
+               ValaExpression *expr;
+               ValaVariableDeclaration *variable_declaration;
+       };
+};
+
+struct _ValaVariableDeclaration {
+       ValaTypeReference *type;
+       ValaVariableDeclarator *declarator;
+};
+
+struct _ValaVariableDeclarator {
+       char *name;
+       ValaLocation *location;
+       ValaExpression *initializer;
+};
+
+struct _ValaExpression {
+       ValaExpressionType type;
+       union {
+               char *str;
+               ValaExpression *inner;
+               struct {
+                       ValaExpression *left;
+                       ValaOpType op;
+                       ValaExpression *right;
+               } additive;
+               struct {
+                       ValaExpression *left;
+                       char *right;
+               } member_access;
+               struct {
+                       ValaExpression *call;
+                       GList *argument_list;
+               } invocation;
+               struct {
+                       ValaExpression *left;
+                       ValaExpression *right;
+               } assignment;
+       };
 };
 
 struct _ValaTypeReference {
index 7b36ec5..f356d0a 100644 (file)
@@ -115,6 +115,10 @@ ValaLocation *get_location (int lineno, int colno)
        GList *list;
        ValaTypeReference *type_reference;
        ValaFormalParameter *formal_parameter;
+       ValaStatement *statement;
+       ValaVariableDeclaration *variable_declaration;
+       ValaVariableDeclarator *variable_declarator;
+       ValaExpression *expression;
 }
 
 %token OPEN_BRACE "{"
@@ -125,24 +129,56 @@ ValaLocation *get_location (int lineno, int colno)
 %token COLON ":"
 %token COMMA ","
 %token SEMICOLON ";"
+%token ASSIGN "="
+%token PLUS "+"
 
-%token NAMESPACE "namespace"
 %token CLASS "class"
+%token NAMESPACE "namespace"
 %token PUBLIC "public"
+%token RETURN "return"
 %token STATIC "static"
+
 %token <str> IDENTIFIER "identifier"
+%token <str> LITERAL_INTEGER "integer"
+%token <str> LITERAL_STRING "string"
 
 %type <list> opt_class_base
 %type <list> class_base
 %type <list> type_list
-%type <type_reference> type_name
 %type <list> opt_formal_parameter_list
 %type <list> formal_parameter_list
 %type <list> fixed_parameters
-%type <formal_parameter> fixed_parameter
+%type <list> opt_statement_list
+%type <list> statement_list
+%type <list> opt_argument_list
+%type <list> argument_list
 %type <num> opt_method_modifiers
 %type <num> method_modifiers
 %type <num> method_modifier
+%type <type_reference> type_name
+%type <expression> variable_initializer
+%type <expression> opt_expression
+%type <expression> expression
+%type <expression> additive_expression
+%type <expression> assignment
+%type <expression> primary_expression
+%type <expression> literal
+%type <expression> simple_name
+%type <expression> parenthesized_expression
+%type <expression> member_access
+%type <expression> invocation_expression
+%type <expression> statement_expression
+%type <expression> argument
+%type <statement> block
+%type <statement> statement
+%type <statement> declaration_statement
+%type <statement> embedded_statement
+%type <statement> expression_statement
+%type <statement> jump_statement
+%type <statement> return_statement
+%type <formal_parameter> fixed_parameter
+%type <variable_declaration> variable_declaration
+%type <variable_declarator> variable_declarator
 
 %%
 
@@ -374,11 +410,302 @@ fixed_parameter
 
 method_body
        : block
+         {
+               current_method->body = $1;
+         }
        | SEMICOLON
        ;
 
 block
-       : OPEN_BRACE CLOSE_BRACE
+       : OPEN_BRACE opt_statement_list CLOSE_BRACE
+         {
+               $$ = g_new0 (ValaStatement, 1);
+               $$->type = VALA_STATEMENT_TYPE_BLOCK;
+               $$->block.statements = $2;
+         }
+       ;
+
+opt_statement_list
+       : /* empty */
+         {
+               $$ = NULL;
+         }
+       | statement_list
+         {
+               $$ = $1;
+         }
+       ;
+
+statement_list
+       : statement
+         {
+               $$ = g_list_append (NULL, $1);
+         }
+       | statement_list statement
+         {
+               $$ = g_list_append ($1, $2);
+         }
+       ;
+
+statement
+       : declaration_statement
+         {
+               $$ = $1;
+         }
+       | embedded_statement
+         {
+               $$ = $1;
+         }
+       ;
+
+declaration_statement
+       : variable_declaration SEMICOLON
+         {
+               $$ = g_new0 (ValaStatement, 1);
+               $$->type = VALA_STATEMENT_TYPE_VARIABLE_DECLARATION;
+               $$->variable_declaration = $1;;
+         }
+       ;
+
+variable_declaration
+       : type_name variable_declarator
+         {
+               $$ = g_new0 (ValaVariableDeclaration, 1);
+               $$->type = $1;
+               $$->declarator = $2;
+         }
+       ;
+
+variable_declarator
+       : IDENTIFIER
+         {
+               $$ = g_new0 (ValaVariableDeclarator, 1);
+               $$->name = $1;
+         }
+       | IDENTIFIER ASSIGN variable_initializer
+         {
+               $$ = g_new0 (ValaVariableDeclarator, 1);
+               $$->name = $1;
+               $$->initializer = $3;
+         }
+       ;
+
+variable_initializer
+       : expression
+         {
+               $$ = $1;
+         }
+       ;
+
+opt_expression
+       : /* empty */
+         {
+               $$ = NULL;
+         }
+       | expression
+         {
+               $$ = $1;
+         }
+       ;
+
+expression
+       : additive_expression
+         {
+               $$ = $1;
+         }
+       | assignment
+         {
+               $$ = $1;
+         }
+       ;
+
+additive_expression
+       : primary_expression
+         {
+               $$ = $1;
+         }
+       | additive_expression PLUS primary_expression
+         {
+               $$ = g_new0 (ValaExpression, 1);
+               $$->type = VALA_EXPRESSION_TYPE_ADDITIVE;
+               $$->additive.left = $1;
+               $$->additive.op = VALA_OP_TYPE_PLUS;
+               $$->additive.right = $3;
+         }
+       ;
+
+primary_expression
+       : literal
+         {
+               $$ = $1;
+         }
+       | simple_name
+         {
+               $$ = $1;
+         }
+       | parenthesized_expression
+         {
+               $$ = $1;
+         }
+       | member_access
+         {
+               $$ = $1;
+         }
+       | invocation_expression
+         {
+               $$ = $1;
+         }
+       ;
+
+literal
+       : LITERAL_INTEGER
+         {
+               $$ = g_new0 (ValaExpression, 1);
+               $$->type = VALA_EXPRESSION_TYPE_LITERAL_INTEGER;
+               $$->str = $1;
+         }
+       | LITERAL_STRING
+         {
+               $$ = g_new0 (ValaExpression, 1);
+               $$->type = VALA_EXPRESSION_TYPE_LITERAL_STRING;
+               $$->str = $1;
+         }
+       ;
+
+simple_name
+       : IDENTIFIER
+         {
+               $$ = g_new0 (ValaExpression, 1);
+               $$->type = VALA_EXPRESSION_TYPE_SIMPLE_NAME;
+               $$->str = $1;
+         }
+       ;
+
+parenthesized_expression
+       : OPEN_PARENS expression CLOSE_PARENS
+         {
+               $$ = g_new0 (ValaExpression, 1);
+               $$->type = VALA_EXPRESSION_TYPE_PARENTHESIZED;
+               $$->inner = $2;
+         }
+       ;
+       
+member_access
+       : primary_expression DOT IDENTIFIER
+         {
+               $$ = g_new0 (ValaExpression, 1);
+               $$->type = VALA_EXPRESSION_TYPE_MEMBER_ACCESS;
+               $$->member_access.left = $1;
+               $$->member_access.right = $3;
+         }
+       ;
+
+invocation_expression
+       : primary_expression OPEN_PARENS opt_argument_list CLOSE_PARENS
+         {
+               $$ = g_new0 (ValaExpression, 1);
+               $$->type = VALA_EXPRESSION_TYPE_INVOCATION;
+               $$->invocation.call = $1;
+               $$->invocation.argument_list = $3;
+         }
+       ;
+
+opt_argument_list
+       : /* empty */
+         {
+               $$ = NULL;
+         }
+       | argument_list
+         {
+               $$ = $1;
+         }
+       ;
+
+argument_list
+       : argument
+         {
+               $$ = g_list_append (NULL, $1);
+         }
+       | argument_list COMMA argument
+         {
+               $$ = g_list_append ($1, $3);
+         }
+       ;
+
+argument
+       : expression
+         {
+               $$ = $1;
+         }
+       ;
+
+embedded_statement
+       : block
+         {
+               $$ = $1;
+         }
+       | empty_statement
+         {
+               $$ = NULL;
+         }
+       | expression_statement
+         {
+               $$ = $1;
+         }
+       | jump_statement
+         {
+               $$ = $1;
+         }
+       ;
+
+empty_statement
+       : SEMICOLON
+       ;
+
+expression_statement
+       : statement_expression SEMICOLON
+         {
+               $$ = g_new0 (ValaStatement, 1);
+               $$->type = VALA_STATEMENT_TYPE_EXPRESSION;
+               $$->expr = $1;
+         }
+       ;
+
+statement_expression
+       : invocation_expression
+         {
+               $$ = $1;
+         }
+       | assignment
+         {
+               $$ = $1;
+         }
+       ;
+
+assignment
+       : primary_expression ASSIGN expression
+         {
+               $$ = g_new0 (ValaExpression, 1);
+               $$->type = VALA_EXPRESSION_TYPE_ASSIGNMENT;
+               $$->assignment.left = $1;
+               $$->assignment.right = $3;
+         }
+       ;
+
+jump_statement
+       : return_statement
+         {
+               $$ = $1;
+         }
+       ;
+
+return_statement
+       : RETURN opt_expression SEMICOLON
+         {
+               $$ = g_new0 (ValaStatement, 1);
+               $$->type = VALA_STATEMENT_TYPE_RETURN;
+               $$->expr = $2;
+         }
        ;
 
 %%
index 0a41db5..64dc066 100644 (file)
@@ -34,6 +34,8 @@
 
 %%
 
+"//".*         { uploc; }
+
 "{"            { uploc; return OPEN_BRACE; }
 "}"            { uploc; return CLOSE_BRACE; }
 "("            { uploc; return OPEN_PARENS; }
 ":"            { uploc; return COLON; }
 ","            { uploc; return COMMA; }
 ";"            { uploc; return SEMICOLON; }
+"="            { uploc; return ASSIGN; }
+"+"            { uploc; return PLUS; }
 
-"namespace"    { uploc; return NAMESPACE; }
 "class"                { uploc; return CLASS; }
+"namespace"    { uploc; return NAMESPACE; }
 "public"       { uploc; return PUBLIC; }
+"return"       { uploc; return RETURN; }
 "static"       { uploc; return STATIC; }
 
+[[:digit:]]+   { uploc; yylval->str = strdup (yytext); return LITERAL_INTEGER; }
+
+\"([^\"\\]|\\[\'\"\?\\abfnrtv])*\"     { uploc; yylval->str = strdup (yytext); return LITERAL_STRING; }
+
 [[:alnum:]_]+  { uploc; yylval->str = strdup (yytext); return IDENTIFIER; }
 
 [ \t]+         { uploc; /* eat up whitespace */ }