ack/lang/m2/comp/declar.g

383 lines
6.5 KiB
Text
Raw Normal View History

1986-03-26 15:11:02 +00:00
/* D E C L A R A T I O N S */
1986-03-20 14:52:03 +00:00
1986-03-26 15:11:02 +00:00
{
1986-03-20 14:52:03 +00:00
static char *RcsId = "$Header$";
1986-03-26 15:11:02 +00:00
#include <em_arith.h>
#include <em_label.h>
1986-03-26 22:46:48 +00:00
#include <assert.h>
1986-03-26 15:11:02 +00:00
#include "idf.h"
#include "misc.h"
#include "LLlex.h"
#include "def.h"
#include "type.h"
#include "scope.h"
}
1986-03-20 14:52:03 +00:00
1986-03-26 15:11:02 +00:00
ProcedureDeclaration
{
1986-03-26 22:46:48 +00:00
struct def *df;
1986-03-26 15:11:02 +00:00
} :
1986-03-26 22:46:48 +00:00
ProcedureHeading(&df, D_PROCEDURE)
1986-03-26 15:11:02 +00:00
';' block IDENT
{ match_id(dot.TOK_IDF, df->df_idf);
close_scope();
}
;
1986-03-26 22:46:48 +00:00
ProcedureHeading(struct def **pdf; int type;)
1986-03-26 15:11:02 +00:00
{
1986-03-27 17:37:41 +00:00
struct type *tp;
struct paramlist *params = 0;
1986-03-26 15:11:02 +00:00
} :
PROCEDURE IDENT
1986-03-26 22:46:48 +00:00
{ assert(type == D_PROCEDURE || type == D_PROCHEAD);
1986-03-27 17:37:41 +00:00
*pdf = define(dot.TOK_IDF, CurrentScope, type);
1986-03-26 22:46:48 +00:00
if (type == D_PROCEDURE) {
open_scope(OPENSCOPE, 0);
}
}
1986-03-27 17:37:41 +00:00
FormalParameters(type, &params, &tp)?
1986-03-20 14:52:03 +00:00
;
block:
declaration* [ BEGIN StatementSequence ]? END
;
declaration:
CONST [ ConstantDeclaration ';' ]*
|
TYPE [ TypeDeclaration ';' ]*
|
VAR [ VariableDeclaration ';' ]*
|
ProcedureDeclaration ';'
|
ModuleDeclaration ';'
;
1986-03-27 17:37:41 +00:00
FormalParameters(int doparams; struct paramlist **pr; struct type **tp;)
{
struct def *df;
register struct paramlist *pr1;
} :
'('
[
FPSection(doparams, pr)
[
{ for (pr1 = *pr; pr1->next; pr1 = pr1->next) ; }
';' FPSection(doparams, &(pr1->next))
]*
]?
')'
{ *tp = 0; }
[ ':' qualident(D_TYPE | D_HTYPE, &df, "type")
{ /* ???? *tp = df->df_type; */ }
1986-03-26 15:11:02 +00:00
]?
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
FPSection(int doparams; struct paramlist **ppr;)
1986-03-20 14:52:03 +00:00
{
struct id_list *FPList;
1986-03-27 17:37:41 +00:00
register struct id_list *pid;
register struct paramlist *pr = 0;
1986-03-26 15:11:02 +00:00
int VARflag = 0;
1986-03-20 14:52:03 +00:00
} :
1986-03-26 15:11:02 +00:00
[
VAR { VARflag = 1; }
]?
IdentList(&FPList) ':' FormalType
{
1986-03-26 22:46:48 +00:00
if (doparams) {
EnterIdList(FPList,
D_VARIABLE,
VARflag,
1986-03-27 17:37:41 +00:00
(struct type *) 0 /* ???? */,
CurrentScope
1986-03-26 22:46:48 +00:00
);
}
1986-03-27 17:37:41 +00:00
*ppr = pr = new_paramlist();
pr->par_type = 0; /* ??? */
pr->par_var = VARflag;
for (pid = FPList->next; pid; pid = pid->next) {
pr->next = new_paramlist();
pr = pr->next;
pr->par_type = 0; /* ??? */
pr->par_var = VARflag;
}
pr->next = 0;
1986-03-26 15:11:02 +00:00
FreeIdList(FPList);
}
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
FormalType
{
struct def *df;
int ARRAYflag = 0;
} :
[ ARRAY OF { ARRAYflag = 1; }
]?
qualident(D_TYPE | D_HTYPE, &df, "type")
1986-03-20 14:52:03 +00:00
;
1986-03-26 15:11:02 +00:00
TypeDeclaration
{
1986-03-26 22:46:48 +00:00
struct def *df;
1986-03-27 17:37:41 +00:00
struct type *tp;
1986-03-26 15:11:02 +00:00
}:
1986-03-27 17:37:41 +00:00
IDENT { df = define(dot.TOK_IDF, CurrentScope, D_TYPE); }
'=' type(&tp)
{ df->df_type = tp;
1986-03-26 22:46:48 +00:00
}
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
type(struct type **ptp;):
SimpleType(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
ArrayType(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
RecordType(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
SetType(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
PointerType(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
ProcedureType(ptp)
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
SimpleType(struct type **ptp;)
{
struct def *df;
} :
qualident(D_TYPE | D_HTYPE, &df, "type")
1986-03-20 14:52:03 +00:00
[
|
1986-03-27 17:37:41 +00:00
SubrangeType(ptp)
1986-03-20 14:52:03 +00:00
/*
* The subrange type is given a base type by the
* qualident (this is new modula-2).
*/
1986-03-27 17:37:41 +00:00
{ /* ???? (*ptp)->next = df->df_type; */ }
1986-03-20 14:52:03 +00:00
]
|
1986-03-27 17:37:41 +00:00
enumeration(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
SubrangeType(ptp)
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
enumeration(struct type **ptp;)
1986-03-20 14:52:03 +00:00
{
struct id_list *EnumList;
} :
'(' IdentList(&EnumList) ')'
1986-03-26 22:46:48 +00:00
{
1986-03-27 17:37:41 +00:00
*ptp = standard_type(ENUMERATION,int_align,int_size);
EnterIdList(EnumList, D_ENUM, 0, *ptp, CurrentScope);
1986-03-26 22:46:48 +00:00
FreeIdList(EnumList);
}
1986-03-20 14:52:03 +00:00
;
IdentList(struct id_list **p;)
{
register struct id_list *q = new_id_list();
} :
1986-03-26 22:46:48 +00:00
IDENT { q->id_ptr = dot.TOK_IDF; *p = q;}
1986-03-20 14:52:03 +00:00
[
',' IDENT { q->next = new_id_list();
q = q->next;
q->id_ptr = dot.TOK_IDF;
}
]*
1986-03-26 22:46:48 +00:00
{ q->next = 0; }
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
SubrangeType(struct type **ptp;)
{
struct type *tp;
}:
1986-03-20 14:52:03 +00:00
/*
This is not exactly the rule in the new report, but see
the rule for "SimpleType".
*/
1986-03-27 17:37:41 +00:00
'[' ConstExpression
UPTO ConstExpression
']'
/*
Evaluate the expressions. Check that they are indeed constant.
???
Leave the basetype of the subrange in tp;
*/
{
/* For the time being: */
tp = int_type;
tp = construct_type(SUBRANGE, tp, (arith) 0);
*ptp = tp;
}
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
ArrayType(struct type **ptp;)
{
struct type *tp;
register struct type *tp2;
} :
ARRAY SimpleType(&tp)
{
*ptp = tp2 = construct_type(ARRAY, tp);
}
[
',' SimpleType(&tp)
{ tp2 = tp2->tp_value.tp_arr.ar_elem =
construct_type(ARRAY, tp);
}
]* OF type(&tp)
{ tp2->tp_value.tp_arr.ar_elem = tp; }
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
RecordType(struct type **ptp;)
{
int scopenr;
}
:
RECORD
{ scopenr = uniq_scope(); }
FieldListSequence(scopenr)
{
*ptp = standard_type(RECORD, record_align, (arith) 0 /* ???? */);
(*ptp)->tp_value.tp_record.rc_scopenr = scopenr;
}
END
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
FieldListSequence(int scopenr;):
FieldList(scopenr)
[
';' FieldList(scopenr)
]*
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
FieldList(int scopenr;)
1986-03-20 14:52:03 +00:00
{
struct id_list *FldList;
1986-03-27 17:37:41 +00:00
struct idf *id;
struct def *df, *df1;
struct type *tp;
1986-03-20 14:52:03 +00:00
} :
[
1986-03-27 17:37:41 +00:00
IdentList(&FldList) ':' type(&tp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
CASE
[
IDENT { id = dot.TOK_IDF; }
|
{ id = gen_anon_idf(); }
] /* Changed rule in new modula-2 */
':' qualident(D_TYPE|D_HTYPE, &df, "type")
{ df1 = define(id, scopenr, D_FIELD);
df1->df_type = df->df_type;
}
OF variant(scopenr)
[
'|' variant(scopenr)
]*
[ ELSE FieldListSequence(scopenr)
]?
1986-03-20 14:52:03 +00:00
END
]?
;
1986-03-27 17:37:41 +00:00
variant(int scopenr;):
[ CaseLabelList ':' FieldListSequence(scopenr) ]?
1986-03-20 14:52:03 +00:00
/* Changed rule in new modula-2 */
;
CaseLabelList:
CaseLabels [ ',' CaseLabels ]*
;
CaseLabels:
ConstExpression [ UPTO ConstExpression ]?
;
1986-03-27 17:37:41 +00:00
SetType(struct type **ptp;)
{
struct type *tp;
} :
SET OF SimpleType(&tp)
{
*ptp = construct_type(SET, tp, (arith) 0 /* ???? */);
}
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
PointerType(struct type **ptp;)
{
struct type *tp;
register struct def *df;
struct def *lookfor();
} :
POINTER TO
[ %if ( (df = lookup(dot.TOK_IDF, CurrentScope)))
IDENT
{
if (!(df->df_kind & (D_TYPE | D_HTYPE))) {
error("\"%s\" is not a type identifier",
df->df_idf->id_text);
}
if (!df->df_type) {
error("type \"%s\" not declared",
df->df_idf->id_text);
}
*ptp = df->df_type;
}
| %if (df = lookfor(dot.TOK_IDF, 0), df->df_kind == D_MODULE)
type(&tp)
{ *ptp = construct_type(POINTER, tp); }
|
IDENT
{ *ptp = construct_type(POINTER, NULLTYPE);
Forward(&dot, &((*ptp)->next));
}
]
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
ProcedureType(struct type **ptp;):
1986-03-20 14:52:03 +00:00
PROCEDURE FormalTypeList?
1986-03-27 17:37:41 +00:00
{ *ptp = 0; }
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
FormalTypeList
{
struct def *df;
} :
1986-03-20 14:52:03 +00:00
'(' [ VAR? FormalType [ ',' VAR? FormalType ]* ]? ')'
1986-03-27 17:37:41 +00:00
[ ':' qualident(1, &df, "type")
]?
1986-03-20 14:52:03 +00:00
;
1986-03-26 15:11:02 +00:00
ConstantDeclaration
{
1986-03-26 22:46:48 +00:00
struct def *df;
struct idf *id;
1986-03-26 15:11:02 +00:00
}:
1986-03-26 22:46:48 +00:00
IDENT { id = dot.TOK_IDF; }
'=' ConstExpression { df = define(id, CurrentScope, D_CONST);
/* ???? */
}
1986-03-20 14:52:03 +00:00
;
VariableDeclaration
{
struct id_list *VarList;
1986-03-27 17:37:41 +00:00
struct type *tp;
1986-03-20 14:52:03 +00:00
} :
1986-03-24 17:29:57 +00:00
IdentList(&VarList)
[
ConstExpression
]?
1986-03-27 17:37:41 +00:00
':' type(&tp)
{ EnterIdList(VarList, D_VARIABLE, 0, tp, CurrentScope);
1986-03-26 22:46:48 +00:00
FreeIdList(VarList);
}
1986-03-20 14:52:03 +00:00
;