Skip to content

Commit

Permalink
Cleanups for better type checking, generate better
Browse files Browse the repository at this point in the history
code for return statements (don't jump if last statement before
a ret)


git-svn-id: https://svn.parrot.org/parrot/trunk@1320 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
Melvin Smith committed Apr 5, 2002
1 parent de9075e commit 26be489
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 207 deletions.
34 changes: 19 additions & 15 deletions languages/cola/cola.h
Expand Up @@ -60,6 +60,7 @@ enum ASTTYPE {
/* Identifiers, etc. */
typedef struct _SymbolTable SymbolTable;
typedef struct _AST AST;
typedef struct _Type Type;

/* Symbol structure */

Expand All @@ -79,7 +80,7 @@ typedef struct _Symbol {
* one of TYPE, LITERAL, VARIABLE, METHOD, NAMESPACE
*/
int kind;
struct _Symbol *type;
Type *type;
int size;
AST * init_expr;
int is_lval;
Expand All @@ -103,9 +104,10 @@ struct _AST {
*/
char *start_label,
*end_label;
enum ASTKIND kind; /* General node class (STATEMENT) */
enum ASTKIND kind; /* General node class (STATEMENT) */
int asttype; /* Specific node type (IF|ASSIGN|...)*/
int op; /* Operation */
int op; /* Operation */
Type *type;
Symbol *sym;
/* Expression generic nodes */
Symbol *targ;
Expand Down Expand Up @@ -140,12 +142,13 @@ struct _SymbolTable {
Symbol *table[ HASH_SIZE ];
};

typedef struct _Type {
struct _Type {
int kind; /* scalar, array, class, pointer/reference */
int typeid;
int parentid;
int size;
Symbol *sym; /* Pointer to symbol table representing name of type */
} Type;
};


/*
Expand All @@ -172,7 +175,7 @@ extern Symbol *namespace_stack;
extern Symbol *current_namespace;
extern int scope;
/* Pointers to the builtin type entries in the symbol table */
extern Symbol *t_void,
extern Type *t_void,
*t_string,
*t_int,
*t_float,
Expand All @@ -198,21 +201,22 @@ Symbol *lookup_symbol_scope(SymbolTable *, const char *, int);
Symbol *lookup_namespace(SymbolTable * tab, const char * name);
Symbol *lookup_class(SymbolTable * tab, const char * name);
void store_symbol(SymbolTable *, Symbol *);
Symbol *store_identifier(SymbolTable *, const char * name, int kind, Symbol * type);
Symbol *store_type(const char * name, int size);
Symbol *lookup_type(const char * name);
Symbol *lookup_type_symbol(Symbol * identifier);
const char *type_name(Symbol *);
Symbol *store_identifier(SymbolTable *, const char * name, int kind, Type * t);
int push_scope();
Symbol *pop_scope(SymbolTable *);
void coerce_operands(Symbol ** t1, Symbol ** t2);

/*
* Type related utilities
*/

extern Type **type_table;
extern int type_table_size;
extern Type *new_type(int kind, const char * name, const char * parent_name);

Type *store_type(const char * name, int size);
Type *lookup_type(const char * name);
Symbol *lookup_type_symbol(Symbol * id);
const char *type_name(Type *);
void coerce_operands(Type ** t1, Type ** t2);


char *str_dup(const char *);
Expand Down Expand Up @@ -249,7 +253,7 @@ void gen_method_decl(AST * ast);
void gen_block(AST * ast);
void gen_statement(AST * ast);
void gen_assign(AST * ast);
void gen_expr(AST * ast, Symbol * lval, Symbol * type);
void gen_expr(AST * ast, Symbol * lval, Type * t);
void gen_call(AST *);
void gen_if(AST *);
void gen_while(AST *);
Expand All @@ -261,7 +265,7 @@ void emit_op_expr(Symbol * res, Symbol * arg1, char * op, Symbol
void emit_unary_expr(Symbol * res, Symbol * arg1, char * op);

char *new_rval();
Symbol *make_rval(Symbol * type);
Symbol *make_rval(Type * t);

void reset_temps();
char *get_label();
Expand Down
155 changes: 78 additions & 77 deletions languages/cola/cola.l
Expand Up @@ -21,62 +21,63 @@ int yyerror( char * );

%option outfile="lexer.c"

LETTER [a-zA-Z_]
DIGIT [0-9]
LETTERDIGIT [a-zA-Z0-9_]
SIGN [-+]
STRINGCONSTANT \"[^"\n]*["\n]
LETTER [a-zA-Z_]
DIGIT [0-9]
LETTERDIGIT [a-zA-Z0-9_]
SIGN [-+]
STRINGCONSTANT \"[^"\n]*["\n]
CHARCONSTANT \'[^'\n]*\'

%%

[\n] { line++; }
\/\/.*\n { line++; /* COMMENT */}


"__asm" { return(ASM); }
"bool" { return(BOOL); }
"break" { return(BREAK); }
"byte" { return(BYTE); }
"const" { return(CONST); }
"continue" { return(CONTINUE); }
"char" { return(CHAR); }
"class" { return(CLASS); }
"decimal" { return(DECIMAL);}
"__asm" { return(ASM); }
"bool" { return(BOOL); }
"break" { return(BREAK); }
"byte" { return(BYTE); }
"const" { return(CONST); }
"continue" { return(CONTINUE); }
"char" { return(CHAR); }
"class" { return(CLASS); }
"decimal" { return(DECIMAL); }
"double" { return(DOUBLE); }
"else" { return(ELSE); }
"false" { return(FALSE); }
"float" { return(FLOAT); }
"get" { return(GET); }
"goto" { return(GOTO); }
"for" { return(FOR); }
"if" { return(IF); }
"int" { return(INT); }
"long" { return(LONG); }
"namespace" { return(NAMESPACE); }
"new" { return(NEW); }
"null" { return(NULLVAL);}
"out" { return(OUT); }
"else" { return(ELSE); }
"false" { return(FALSE); }
"float" { return(FLOAT); }
"get" { return(GET); }
"goto" { return(GOTO); }
"for" { return(FOR); }
"if" { return(IF); }
"int" { return(INT); }
"long" { return(LONG); }
"namespace" { return(NAMESPACE); }
"new" { return(NEW); }
"null" { return(NULLVAL); }
"out" { return(OUT); }
"object" { return(OBJECT); }
"override" { return(OVERRIDE); }
"override" { return(OVERRIDE); }
"public" { return(PUBLIC); }
"private" { return(PRIVATE); }
"protected" { return(PROTECTED); }
"readonly" { return(READONLY); }
"ref" { return(REF); }
"private" { return(PRIVATE); }
"protected" { return(PROTECTED); }
"readonly" { return(READONLY); }
"ref" { return(REF); }
"return" { return(RETURN); }
"set" { return(SET); }
"sbyte" { return(SBYTE); }
"short" { return(SHORT); }
"set" { return(SET); }
"sbyte" { return(SBYTE); }
"short" { return(SHORT); }
"static" { return(STATIC); }
"string" { return(STRING); }
"true" { return(TRUE); }
"uint" { return(UINT); }
"ulong" { return(ULONG); }
"true" { return(TRUE); }
"uint" { return(UINT); }
"ulong" { return(ULONG); }
"ushort" { return(USHORT); }
"using" { return(USING); }
"virtual" { return(VIRTUAL);}
"void" { return(VOID); }
"while" { return(WHILE); }
"using" { return(USING); }
"virtual" { return(VIRTUAL); }
"void" { return(VOID); }
"while" { return(WHILE); }


{LETTER}{LETTERDIGIT}* {
Expand Down Expand Up @@ -108,42 +109,42 @@ STRINGCONSTANT \"[^"\n]*["\n]
return(LITERAL);
}

"++" { return(INC); }
"--" { return(DEC); }
"||" { return(LOGICAL_OR); }
"&&" { return(LOGICAL_AND); }
"==" { return(LOGICAL_EQ); }
"!=" { return(LOGICAL_NE); }
"<<" { return(LEFT_SHIFT); }
">>" { return(RIGHT_SHIFT); }
"." { return('.'); }
"[" { return('['); }
"]" { return(']'); }
"," { return(','); }
":" { return(':'); }
";" { return(';'); }
"<=" { return(LOGICAL_LTE); }
">=" { return(LOGICAL_GTE); }
"<" { return(LOGICAL_LT); }
">" { return(LOGICAL_GT); }
"(" { return('('); }
")" { return(')'); }
"{" { return('{'); }
"}" { return('}'); }
"=" { return('='); }
"+" { return('+'); }
"-" { return('-'); }
"/" { return('/'); }
"*" { return('*'); }
"!" { return('!'); }
"?" { return('?'); }
"%" { return('%'); }
"|" { return('|'); }
"&" { return('&'); }
"^" { return('^'); }
"~" { return('~'); }
"++" { return(INC); }
"--" { return(DEC); }
"||" { return(LOGICAL_OR);}
"&&" { return(LOGICAL_AND);}
"==" { return(LOGICAL_EQ);}
"!=" { return(LOGICAL_NE);}
"<<" { return(LEFT_SHIFT);}
">>" { return(RIGHT_SHIFT);}
"." { return('.'); }
"[" { return('['); }
"]" { return(']'); }
"," { return(','); }
":" { return(':'); }
";" { return(';'); }
"<=" { return(LOGICAL_LTE);}
">=" { return(LOGICAL_GTE);}
"<" { return(LOGICAL_LT);}
">" { return(LOGICAL_GT);}
"(" { return('('); }
")" { return(')'); }
"{" { return('{'); }
"}" { return('}'); }
"=" { return('='); }
"+" { return('+'); }
"-" { return('-'); }
"/" { return('/'); }
"*" { return('*'); }
"!" { return('!'); }
"?" { return('?'); }
"%" { return('%'); }
"|" { return('|'); }
"&" { return('&'); }
"^" { return('^'); }
"~" { return('~'); }
[\t\f ]+ ;
. { /* ignore */ }
. { /* ignore */ }
%%

#ifdef yywrap
Expand Down
34 changes: 17 additions & 17 deletions languages/cola/cola.y
Expand Up @@ -14,21 +14,27 @@
#include <stdio.h>
#include <stdlib.h>
#include "cola.h"

int yyerror(char *);
int yylex();
extern char yytext[];
long line;
int indent;
int yyerror(char *);
int yylex();
AST * ast_start = NULL;
long line;
int indent;

AST *ast_start = NULL;

/* Pointers to the builtin type entries in the symbol table */
Symbol * t_void, * t_string, * t_int, * t_float;
Type *t_void,
*t_string,
*t_int,
*t_float;

%}

%union {
int ival;
Symbol * sym;
Type * type;
AST * ast;
}

Expand All @@ -47,8 +53,8 @@ Symbol * t_void, * t_string, * t_int, * t_float;

%token TYPE METHOD


%type <sym> type ref_type array_type return_type
%type <type> type return_type
%type <sym> ref_type array_type
%type <sym> member_name qualified_identifier
%type <sym> namespace_scope_start class_scope_start
%type <sym> var_declarator var_declarators
Expand Down Expand Up @@ -447,16 +453,10 @@ type:
{ $$ = lookup_type("float"); }
| IDENTIFIER
{
Symbol * t;
Type * t;
t = lookup_type($1->name);
if(t != NULL) {
if(t->kind == TYPE) {
printf("Kind [%s] found in type list.\n", t->name);
}
else {
printf("Error, unknown type [%s]\n", $1->name);
exit(0);
}
fprintf(stderr, "Kind [%s] found in type list.\n", t->sym->name);
$$ = t;
}
else {
Expand Down Expand Up @@ -561,7 +561,7 @@ new_object_expression:
NEW type '(' arg_list ')'
{
$$ = new_expression(ASTT_NEW_OBJECT, $4, NULL);
$$->sym = $2;
$$->type = $2;
}
;

Expand Down

0 comments on commit 26be489

Please sign in to comment.