ack/lang/m2/comp/statement.g

294 lines
5.5 KiB
Plaintext
Raw Normal View History

/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*
* Author: Ceriel J.H. Jacobs
*/
1986-03-26 15:11:02 +00:00
/* S T A T E M E N T S */
1994-06-24 14:02:31 +00:00
/* $Id$ */
1986-03-20 14:52:03 +00:00
{
1986-06-26 09:39:36 +00:00
#include <assert.h>
#include "em_arith.h"
#include "em_label.h"
1986-04-28 18:06:58 +00:00
#include "parameters.h"
1986-04-22 22:36:16 +00:00
#include "idf.h"
1986-04-06 17:42:56 +00:00
#include "LLlex.h"
1986-04-22 22:36:16 +00:00
#include "scope.h"
#include "def.h"
1986-04-17 09:28:09 +00:00
#include "type.h"
#include "error.h"
1986-04-06 17:42:56 +00:00
#include "node.h"
1986-04-15 17:51:53 +00:00
static int loopcount = 0; /* Count nested loops */
extern struct node *EmptyStatement;
1986-03-20 14:52:03 +00:00
}
statement(register struct node **pnd;)
1986-04-06 17:42:56 +00:00
{
register struct node *nd;
1986-10-06 20:36:30 +00:00
extern int return_occurred;
1986-04-06 17:42:56 +00:00
} :
1986-03-20 14:52:03 +00:00
/*
* This part is not in the reference grammar. The reference grammar
* states : assignment | ProcedureCall | ...
* but this gives LL(1) conflicts
*/
1986-04-17 09:28:09 +00:00
designator(pnd)
[ { nd = dot2node(Stat, *pnd, NULLNODE);
1986-04-17 09:28:09 +00:00
nd->nd_symb = '(';
1990-09-14 14:37:24 +00:00
nd->nd_lineno = (*pnd)->nd_lineno;
1986-04-09 18:14:49 +00:00
}
ActualParameters(&(nd->nd_RIGHT))?
1986-03-20 14:52:03 +00:00
|
[ BECOMES
| %erroneous '='
{ error("':=' expected instead of '='");
DOT = BECOMES;
}
]
{ nd = dot2node(Stat, *pnd, NULLNODE); }
expression(&(nd->nd_RIGHT))
1986-03-20 14:52:03 +00:00
]
1986-04-17 09:28:09 +00:00
{ *pnd = nd; }
1986-03-20 14:52:03 +00:00
/*
* end of changed part
*/
|
1986-04-15 17:51:53 +00:00
IfStatement(pnd)
1986-03-20 14:52:03 +00:00
|
1986-04-15 17:51:53 +00:00
CaseStatement(pnd)
1986-03-20 14:52:03 +00:00
|
WHILE { *pnd = nd = dot2leaf(Stat); }
expression(&(nd->nd_LEFT))
1987-08-03 18:20:55 +00:00
DO
StatementSequence(&(nd->nd_RIGHT))
1987-08-03 18:20:55 +00:00
END
1986-03-20 14:52:03 +00:00
|
REPEAT { *pnd = nd = dot2leaf(Stat); }
StatementSequence(&(nd->nd_LEFT))
1987-08-03 18:20:55 +00:00
UNTIL
expression(&(nd->nd_RIGHT))
1986-03-20 14:52:03 +00:00
|
1986-04-15 17:51:53 +00:00
{ loopcount++; }
LOOP { *pnd = nd = dot2leaf(Stat); }
StatementSequence(&((*pnd)->nd_RIGHT))
1987-08-03 18:20:55 +00:00
END
1986-04-15 17:51:53 +00:00
{ loopcount--; }
1986-03-20 14:52:03 +00:00
|
1986-04-15 17:51:53 +00:00
ForStatement(pnd)
1986-03-20 14:52:03 +00:00
|
1991-03-13 13:49:56 +00:00
WITH { *pnd = nd = dot2leaf(Stat); }
designator(&(nd->nd_LEFT))
DO
StatementSequence(&(nd->nd_RIGHT))
END
1986-03-20 14:52:03 +00:00
|
EXIT
1986-05-30 18:48:00 +00:00
{ if (!loopcount) error("EXIT not in a LOOP");
*pnd = dot2leaf(Stat);
1986-04-15 17:51:53 +00:00
}
1986-03-20 14:52:03 +00:00
|
1986-05-30 18:48:00 +00:00
ReturnStatement(pnd)
1986-10-06 20:36:30 +00:00
{ return_occurred = 1; }
1986-06-04 09:01:48 +00:00
|
/* empty */ { *pnd = EmptyStatement; }
1986-03-20 14:52:03 +00:00
;
/*
* The next two rules in-line in "Statement", because of an LL(1) conflict
assignment:
designator BECOMES expression
;
ProcedureCall:
designator ActualParameters?
;
*/
StatementSequence(register struct node **pnd;)
1986-06-04 09:01:48 +00:00
{
struct node *nd;
register struct node *nd1;
1986-06-04 09:01:48 +00:00
} :
1986-04-15 17:51:53 +00:00
statement(pnd)
1986-09-25 19:39:06 +00:00
[ %persistent
';'
statement(&nd)
{ if (nd != EmptyStatement) {
nd1 = dot2node(Link, *pnd, nd);
*pnd = nd1;
nd1->nd_symb = ';';
pnd = &(nd1->nd_RIGHT);
}
1986-04-15 17:51:53 +00:00
}
]*
1986-03-20 14:52:03 +00:00
;
IfStatement(struct node **pnd;)
1986-04-06 17:42:56 +00:00
{
register struct node *nd;
1986-04-06 17:42:56 +00:00
} :
IF { nd = dot2leaf(Stat);
1986-04-15 17:51:53 +00:00
*pnd = nd;
}
expression(&(nd->nd_LEFT))
THEN { nd->nd_RIGHT = dot2leaf(Link);
nd = nd->nd_RIGHT;
1986-04-15 17:51:53 +00:00
}
StatementSequence(&(nd->nd_LEFT))
1986-04-15 17:51:53 +00:00
[
ELSIF { nd->nd_RIGHT = dot2leaf(Stat);
nd = nd->nd_RIGHT;
1986-04-15 17:51:53 +00:00
nd->nd_symb = IF;
}
expression(&(nd->nd_LEFT))
THEN { nd->nd_RIGHT = dot2leaf(Link);
nd = nd->nd_RIGHT;
1986-04-15 17:51:53 +00:00
}
StatementSequence(&(nd->nd_LEFT))
1986-04-15 17:51:53 +00:00
]*
[
ELSE
StatementSequence(&(nd->nd_RIGHT))
1988-02-10 14:06:34 +00:00
|
]
1986-03-20 14:52:03 +00:00
END
;
CaseStatement(struct node **pnd;)
1986-04-06 17:42:56 +00:00
{
register struct node *nd;
struct type *tp = 0;
1986-04-06 17:42:56 +00:00
} :
CASE { *pnd = nd = dot2leaf(Stat); }
expression(&(nd->nd_LEFT))
1986-04-15 17:51:53 +00:00
OF
case(&(nd->nd_RIGHT), &tp)
{ nd = nd->nd_RIGHT; }
1986-04-15 17:51:53 +00:00
[
'|'
case(&(nd->nd_RIGHT), &tp)
{ nd = nd->nd_RIGHT; }
1986-04-15 17:51:53 +00:00
]*
[ ELSE StatementSequence(&(nd->nd_RIGHT))
1988-02-10 14:06:34 +00:00
|
]
1986-03-20 14:52:03 +00:00
END
;
case(struct node **pnd; struct type **ptp;) :
1986-04-17 09:28:09 +00:00
[ CaseLabelList(ptp, pnd)
':' { *pnd = dot2node(Link, *pnd, NULLNODE); }
StatementSequence(&((*pnd)->nd_RIGHT))
1988-02-10 14:06:34 +00:00
|
]
{ *pnd = dot2node(Link, *pnd, NULLNODE);
1986-04-15 17:51:53 +00:00
(*pnd)->nd_symb = '|';
}
1986-03-20 14:52:03 +00:00
;
1987-08-03 18:20:55 +00:00
/* inline in statement; lack of space
WhileStatement(struct node **pnd;)
1986-04-06 17:42:56 +00:00
{
register struct node *nd;
1986-04-06 17:42:56 +00:00
}:
WHILE { *pnd = nd = dot2leaf(Stat); }
expression(&(nd->nd_LEFT))
1986-04-15 17:51:53 +00:00
DO
StatementSequence(&(nd->nd_RIGHT))
1986-04-15 17:51:53 +00:00
END
1986-03-20 14:52:03 +00:00
;
RepeatStatement(struct node **pnd;)
1986-04-06 17:42:56 +00:00
{
register struct node *nd;
1986-04-06 17:42:56 +00:00
}:
REPEAT { *pnd = nd = dot2leaf(Stat); }
StatementSequence(&(nd->nd_LEFT))
1986-04-15 17:51:53 +00:00
UNTIL
expression(&(nd->nd_RIGHT))
1986-03-20 14:52:03 +00:00
;
1987-08-03 18:20:55 +00:00
*/
1986-03-20 14:52:03 +00:00
ForStatement(struct node **pnd;)
1986-04-06 17:42:56 +00:00
{
register struct node *nd, *nd1;
1986-04-06 17:42:56 +00:00
}:
FOR { *pnd = nd = dot2leaf(Stat); }
IDENT { nd1 = dot2leaf(Name); }
BECOMES { nd->nd_LEFT = dot2node(Stat, nd1, dot2leaf(Link));
nd1 = nd->nd_LEFT->nd_RIGHT;
nd1->nd_symb = TO;
}
expression(&(nd1->nd_LEFT))
1986-05-30 18:48:00 +00:00
TO
expression(&(nd1->nd_RIGHT))
{ nd->nd_RIGHT = nd1 = dot2leaf(Link);
nd1->nd_symb = BY;
}
1986-04-15 17:51:53 +00:00
[
1986-05-30 18:48:00 +00:00
BY
ConstExpression(&(nd1->nd_LEFT))
{ if (!(nd1->nd_LEFT->nd_type->tp_fund & T_INTORCARD)) {
1986-05-30 18:48:00 +00:00
error("illegal type in BY clause");
}
1986-04-15 17:51:53 +00:00
}
|
{ nd1->nd_LEFT = dot2leaf(Value);
nd1->nd_LEFT->nd_INT = 1;
}
1986-04-15 17:51:53 +00:00
]
DO
StatementSequence(&(nd1->nd_RIGHT))
1986-04-15 17:51:53 +00:00
END
1986-03-20 14:52:03 +00:00
;
1987-08-03 18:20:55 +00:00
/* inline in Statement; lack of space
LoopStatement(struct node **pnd;):
LOOP { *pnd = dot2leaf(Stat); }
StatementSequence(&((*pnd)->nd_RIGHT))
1986-04-15 17:51:53 +00:00
END
1986-03-20 14:52:03 +00:00
;
WithStatement(struct node **pnd;)
1986-04-06 17:42:56 +00:00
{
register struct node *nd;
1986-04-06 17:42:56 +00:00
}:
WITH { *pnd = nd = dot2leaf(Stat); }
designator(&(nd->nd_LEFT))
1986-04-15 17:51:53 +00:00
DO
StatementSequence(&(nd->nd_RIGHT))
1986-04-15 17:51:53 +00:00
END
1986-03-20 14:52:03 +00:00
;
1991-03-13 13:49:56 +00:00
*/
1986-05-30 18:48:00 +00:00
ReturnStatement(struct node **pnd;)
1986-05-30 18:48:00 +00:00
{
register struct def *df = CurrentScope->sc_definedby;
register struct type *tp = df->df_type ? ResultType(df->df_type) : 0;
register struct node *nd;
1986-05-30 18:48:00 +00:00
} :
RETURN { *pnd = nd = dot2leaf(Stat); }
1986-05-30 18:48:00 +00:00
[
expression(&(nd->nd_RIGHT))
1986-05-30 18:48:00 +00:00
{ if (scopeclosed(CurrentScope)) {
error("a module body cannot return a value");
1986-05-30 18:48:00 +00:00
}
else if (! tp) {
error("procedure \"%s\" is not a function, so cannot return a value", df->df_idf->id_text);
1986-05-30 18:48:00 +00:00
}
}
|
{ if (tp) {
error("function procedure \"%s\" must return a value", df->df_idf->id_text);
1986-05-30 18:48:00 +00:00
}
}
]
;