ack/lang/m2/comp/expression.g

269 lines
4.6 KiB
Plaintext
Raw Normal View History

1986-03-27 17:37:41 +00:00
/* E X P R E S S I O N S */
1986-03-20 14:52:03 +00:00
{
1986-05-01 19:06:53 +00:00
#ifndef NORCSID
1986-03-20 14:52:03 +00:00
static char *RcsId = "$Header$";
1986-05-01 19:06:53 +00:00
#endif
#include "debug.h"
1986-03-27 17:37:41 +00:00
#include <alloc.h>
#include <em_arith.h>
#include <em_label.h>
1986-04-09 18:14:49 +00:00
#include <assert.h>
1986-05-01 19:06:53 +00:00
1986-03-27 17:37:41 +00:00
#include "LLlex.h"
#include "idf.h"
#include "def.h"
1986-04-06 17:42:56 +00:00
#include "node.h"
1986-04-07 17:40:38 +00:00
#include "const.h"
#include "type.h"
1986-06-17 12:04:05 +00:00
#include "chk_expr.h"
1986-03-20 14:52:03 +00:00
}
1986-06-17 12:04:05 +00:00
number(struct node **p;) :
1986-04-06 17:42:56 +00:00
[
1986-05-28 18:36:51 +00:00
%default
1986-06-17 12:04:05 +00:00
INTEGER
1986-03-20 14:52:03 +00:00
|
1986-06-17 12:04:05 +00:00
REAL
1986-06-04 09:01:48 +00:00
] { *p = MkLeaf(Value, &dot);
1986-06-17 12:04:05 +00:00
(*p)->nd_type = toktype;
1986-04-07 17:40:38 +00:00
}
1986-03-20 14:52:03 +00:00
;
1986-06-04 09:01:48 +00:00
qualident(int types;
struct def **pdf;
char *str;
struct node **p;
)
1986-03-27 17:37:41 +00:00
{
register struct def *df;
1986-04-07 17:40:38 +00:00
struct node *nd;
1986-03-27 17:37:41 +00:00
} :
1986-06-04 09:01:48 +00:00
IDENT { nd = MkLeaf(Name, &dot); }
1986-03-27 17:37:41 +00:00
[
1986-04-23 22:12:22 +00:00
selector(&nd)
1986-03-27 17:37:41 +00:00
]*
1986-06-04 09:01:48 +00:00
{ if (types) {
df = ill_df;
1986-04-23 22:12:22 +00:00
if (ChkDesignator(nd)) {
1986-06-04 09:01:48 +00:00
if (nd->nd_class != Def) {
node_error(nd, "%s expected", str);
}
else {
df = nd->nd_def;
if ( !((types|D_ERROR) & df->df_kind)) {
if (df->df_kind == D_FORWARD) {
node_error(nd,"%s \"%s\" not declared", str, df->df_idf->id_text);
1986-04-23 22:12:22 +00:00
}
else {
node_error(nd,"identifier \"%s\" is not a %s", df->df_idf->id_text, str);
}
1986-04-08 18:15:46 +00:00
}
1986-06-04 09:01:48 +00:00
}
1986-03-27 17:37:41 +00:00
}
1986-06-04 09:01:48 +00:00
*pdf = df;
}
if (!p) FreeNode(nd);
else *p = nd;
}
1986-03-20 14:52:03 +00:00
;
1986-04-09 18:14:49 +00:00
selector(struct node **pnd;):
'.' { *pnd = MkNode(Link,*pnd,NULLNODE,&dot); }
1986-05-30 18:48:00 +00:00
IDENT { (*pnd)->nd_IDF = dot.TOK_IDF; }
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
ExpList(struct node **pnd;)
{
1986-07-08 14:59:02 +00:00
register struct node *nd;
1986-04-06 17:42:56 +00:00
} :
1986-07-08 14:59:02 +00:00
expression(pnd) { *pnd = nd = MkNode(Link,*pnd,NULLNODE,&dot);
1986-04-10 01:08:49 +00:00
(*pnd)->nd_symb = ',';
}
1986-04-06 17:42:56 +00:00
[
1986-07-08 14:59:02 +00:00
',' { nd->nd_right = MkLeaf(Link, &dot);
nd = nd->nd_right;
1986-04-06 17:42:56 +00:00
}
1986-07-08 14:59:02 +00:00
expression(&(nd->nd_left))
1986-04-06 17:42:56 +00:00
]*
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
ConstExpression(struct node **pnd;):
expression(pnd)
1986-03-20 14:52:03 +00:00
/*
* Changed rule in new Modula-2.
* Check that the expression is a constant expression and evaluate!
*/
1986-06-26 09:39:36 +00:00
{ DO_DEBUG(options['X'], print("CONSTANT EXPRESSION\n"));
DO_DEBUG(options['X'], PrNode(*pnd, 0));
if (ChkExpression(*pnd) &&
1986-04-10 01:08:49 +00:00
((*pnd)->nd_class != Set && (*pnd)->nd_class != Value)) {
error("Constant expression expected");
}
1986-06-26 09:39:36 +00:00
DO_DEBUG(options['X'], print("RESULTS IN\n"));
DO_DEBUG(options['X'], PrNode(*pnd, 0));
1986-04-07 17:40:38 +00:00
}
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
expression(struct node **pnd;)
{
} :
1986-04-07 17:40:38 +00:00
SimpleExpression(pnd)
1986-04-06 17:42:56 +00:00
[
/* relation */
1986-05-28 18:36:51 +00:00
[ '=' | '#' | '<' | LESSEQUAL | '>' | GREATEREQUAL | IN ]
1986-04-07 17:40:38 +00:00
{ *pnd = MkNode(Oper, *pnd, NULLNODE, &dot); }
SimpleExpression(&((*pnd)->nd_right))
1986-04-06 17:42:56 +00:00
]?
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
/* Inline in expression
1986-03-20 14:52:03 +00:00
relation:
1986-05-28 18:36:51 +00:00
'=' | '#' | '<' | LESSEQUAL | '>' | GREATEREQUAL | IN
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
*/
1986-03-20 14:52:03 +00:00
1986-04-06 17:42:56 +00:00
SimpleExpression(struct node **pnd;)
{
} :
1986-04-07 17:40:38 +00:00
[
[ '+' | '-' ]
1986-06-04 09:01:48 +00:00
{ *pnd = MkLeaf(Uoper, &dot);
1986-04-07 17:40:38 +00:00
pnd = &((*pnd)->nd_right);
}
]?
term(pnd)
1986-04-06 17:42:56 +00:00
[
/* AddOperator */
[ '+' | '-' | OR ]
1986-04-07 17:40:38 +00:00
{ *pnd = MkNode(Oper, *pnd, NULLNODE, &dot); }
term(&((*pnd)->nd_right))
1986-04-06 17:42:56 +00:00
]*
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
/* Inline in "SimpleExpression"
1986-03-20 14:52:03 +00:00
AddOperator:
'+' | '-' | OR
;
1986-04-06 17:42:56 +00:00
*/
1986-03-20 14:52:03 +00:00
1986-04-06 17:42:56 +00:00
term(struct node **pnd;)
{
}:
1986-04-07 17:40:38 +00:00
factor(pnd)
1986-04-06 17:42:56 +00:00
[
/* MulOperator */
[ '*' | '/' | DIV | MOD | AND | '&' ]
1986-04-07 17:40:38 +00:00
{ *pnd = MkNode(Oper, *pnd, NULLNODE, &dot); }
factor(&((*pnd)->nd_right))
1986-04-06 17:42:56 +00:00
]*
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
/* inline in "term"
1986-03-20 14:52:03 +00:00
MulOperator:
'*' | '/' | DIV | MOD | AND | '&'
;
1986-04-06 17:42:56 +00:00
*/
1986-03-20 14:52:03 +00:00
1986-07-08 14:59:02 +00:00
factor(register struct node **p;)
1986-03-27 17:37:41 +00:00
{
struct def *df;
1986-04-07 17:40:38 +00:00
struct node *nd;
1986-03-27 17:37:41 +00:00
} :
1986-04-06 17:42:56 +00:00
qualident(0, &df, (char *) 0, p)
1986-03-20 14:52:03 +00:00
[
1986-04-06 17:42:56 +00:00
designator_tail(p)?
[
1986-04-07 17:40:38 +00:00
{ *p = MkNode(Call, *p, NULLNODE, &dot); }
1986-04-06 17:42:56 +00:00
ActualParameters(&((*p)->nd_right))
]?
1986-04-07 17:40:38 +00:00
|
bare_set(&nd)
1986-05-28 18:36:51 +00:00
{ nd->nd_left = *p; *p = nd; }
1986-03-20 14:52:03 +00:00
]
|
1986-04-06 17:42:56 +00:00
bare_set(p)
1986-03-20 14:52:03 +00:00
| %default
1986-04-06 17:42:56 +00:00
number(p)
1986-03-20 14:52:03 +00:00
|
1986-07-08 14:59:02 +00:00
STRING { *p = MkLeaf(Value, &dot);
1986-06-04 09:01:48 +00:00
(*p)->nd_type = toktype;
1986-04-28 18:06:58 +00:00
}
1986-03-20 14:52:03 +00:00
|
1986-04-06 17:42:56 +00:00
'(' expression(p) ')'
1986-03-20 14:52:03 +00:00
|
1986-06-04 09:01:48 +00:00
NOT { *p = MkLeaf(Uoper, &dot); }
1986-04-08 18:15:46 +00:00
factor(&((*p)->nd_right))
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
bare_set(struct node **pnd;)
{
1986-04-07 17:40:38 +00:00
register struct node *nd;
1986-04-06 17:42:56 +00:00
} :
1986-07-08 14:59:02 +00:00
'{' { dot.tk_symb = SET;
1986-06-04 09:01:48 +00:00
*pnd = nd = MkLeaf(Xset, &dot);
1986-04-07 17:40:38 +00:00
nd->nd_type = bitset_type;
1986-04-06 17:42:56 +00:00
}
[
element(nd)
1986-04-07 17:40:38 +00:00
[ { nd = nd->nd_right; }
',' element(nd)
1986-04-06 17:42:56 +00:00
]*
]?
'}'
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
ActualParameters(struct node **pnd;):
'(' ExpList(pnd)? ')'
1986-03-20 14:52:03 +00:00
;
1986-04-07 17:40:38 +00:00
element(struct node *nd;)
{
struct node *nd1;
} :
expression(&nd1)
1986-04-06 17:42:56 +00:00
[
1986-04-07 17:40:38 +00:00
UPTO
{ nd1 = MkNode(Link, nd1, NULLNODE, &dot);}
expression(&(nd1->nd_right))
1986-04-06 17:42:56 +00:00
]?
1986-04-07 17:40:38 +00:00
{ nd->nd_right = MkNode(Link, nd1, NULLNODE, &dot);
nd->nd_right->nd_symb = ',';
}
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
designator(struct node **pnd;)
1986-03-27 17:37:41 +00:00
{
struct def *df;
} :
1986-04-06 17:42:56 +00:00
qualident(0, &df, (char *) 0, pnd)
designator_tail(pnd)?
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
designator_tail(struct node **pnd;):
visible_designator_tail(pnd)
1986-03-27 17:37:41 +00:00
[
1986-04-09 18:14:49 +00:00
selector(pnd)
1986-03-27 17:37:41 +00:00
|
1986-04-06 17:42:56 +00:00
visible_designator_tail(pnd)
1986-03-27 17:37:41 +00:00
]*
1986-03-20 14:52:03 +00:00
;
1986-07-08 14:59:02 +00:00
visible_designator_tail(register struct node **pnd;):
1986-06-10 13:18:52 +00:00
'[' { *pnd = MkNode(Arrsel, *pnd, NULLNODE, &dot); }
1986-04-09 18:14:49 +00:00
expression(&((*pnd)->nd_right))
[
','
1986-06-10 13:18:52 +00:00
{ *pnd = MkNode(Arrsel, *pnd, NULLNODE, &dot);
1986-04-09 18:14:49 +00:00
(*pnd)->nd_symb = '[';
}
expression(&((*pnd)->nd_right))
]*
1986-04-06 17:42:56 +00:00
']'
1986-03-27 17:37:41 +00:00
|
1986-06-10 13:18:52 +00:00
'^' { *pnd = MkNode(Arrow, NULLNODE, *pnd, &dot); }
1986-03-20 14:52:03 +00:00
;