ack/lang/m2/comp/expression.g

280 lines
4.8 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
{
static char *RcsId = "$Header$";
1986-03-27 17:37:41 +00:00
#include <alloc.h>
#include <em_arith.h>
#include <em_label.h>
1986-04-07 17:40:38 +00:00
#include "main.h"
1986-03-27 17:37:41 +00:00
#include "LLlex.h"
#include "idf.h"
#include "def.h"
#include "scope.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"
#include "debug.h"
1986-03-20 14:52:03 +00:00
}
1986-04-07 17:40:38 +00:00
number(struct node **p;)
{
struct type *tp;
} :
1986-04-06 17:42:56 +00:00
[
1986-04-07 17:40:38 +00:00
INTEGER { tp = dot.TOK_INT <= max_int ?
intorcard_type : card_type;
}
1986-03-20 14:52:03 +00:00
|
1986-04-07 17:40:38 +00:00
REAL { tp = real_type; }
] { *p = MkNode(Value, NULLNODE, NULLNODE, &dot);
(*p)->nd_type = tp;
}
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
qualident(int types; struct def **pdf; char *str; struct node **p;)
1986-03-27 17:37:41 +00:00
{
int scope;
1986-04-02 17:34:21 +00:00
int module;
1986-03-27 17:37:41 +00:00
register struct def *df;
struct def *lookfor();
1986-04-07 17:40:38 +00:00
register struct node **pnd;
struct node *nd;
1986-03-27 17:37:41 +00:00
} :
IDENT { if (types) {
1986-04-03 00:44:39 +00:00
df = lookfor(dot.TOK_IDF, CurrentScope, 1);
*pdf = df;
1986-03-29 01:04:49 +00:00
if (df->df_kind == D_ERROR) types = 0;
1986-03-27 17:37:41 +00:00
}
1986-04-07 17:40:38 +00:00
nd = MkNode(Value, NULLNODE, NULLNODE, &dot);
pnd = &nd;
1986-03-27 17:37:41 +00:00
}
[
{ if (types &&!(scope = has_selectors(df))) {
types = 0;
*pdf = ill_df;
}
}
/* selector */
1986-04-07 17:40:38 +00:00
'.' { *pnd = MkNode(Link,*pnd,NULLNODE,&dot);
pnd = &(*pnd)->nd_right;
}
1986-04-06 17:42:56 +00:00
IDENT
1986-04-07 17:40:38 +00:00
{ *pnd = MkNode(Value,NULLNODE,NULLNODE,&dot);
1986-04-06 17:42:56 +00:00
if (types) {
1986-04-02 17:34:21 +00:00
module = (df->df_kind == D_MODULE);
1986-03-27 17:37:41 +00:00
df = lookup(dot.TOK_IDF, scope);
if (!df) {
types = 0;
df = ill_df;
1986-04-03 17:41:26 +00:00
id_not_declared(dot.TOK_IDF);
1986-03-27 17:37:41 +00:00
}
1986-04-02 17:34:21 +00:00
else
if (module &&
!(df->df_flags&(D_EXPORTED|D_QEXPORTED))) {
error("identifier \"%s\" not exported from qualifying module", dot.TOK_IDF->id_text);
}
1986-03-27 17:37:41 +00:00
}
}
]*
{ if (types && !(types & df->df_kind)) {
error("identifier \"%s\" is not a %s",
1986-03-29 01:04:49 +00:00
df->df_idf->id_text, str);
1986-03-27 17:37:41 +00:00
}
1986-04-07 17:40:38 +00:00
if (!p) FreeNode(nd);
else *p = nd;
1986-03-27 17:37:41 +00:00
}
1986-03-20 14:52:03 +00:00
;
1986-04-06 17:42:56 +00:00
/* Inline substituted wherever it occurred
1986-03-20 14:52:03 +00:00
selector:
1986-04-06 17:42:56 +00:00
'.' IDENT
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
ExpList(struct node **pnd;)
{
struct node **nd;
} :
expression(pnd) { nd = pnd; }
[
',' { *nd = MkNode(Link, *nd, NULLNODE, &dot);
nd = &(*nd)->nd_right;
}
expression(nd)
]*
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-04-07 17:40:38 +00:00
{ DO_DEBUG(3,
( debug("Constant expression:"),
PrNode(*pnd)));
}
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 */
[ '=' | '#' | UNEQUAL | '<' | 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:
'=' | '#' | UNEQUAL | '<' | LESSEQUAL | '>' | GREATEREQUAL | IN
;
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
[
[ '+' | '-' ]
{ *pnd = MkNode(Uoper, NULLNODE, NULLNODE, &dot);
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-04-06 17:42:56 +00:00
factor(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)
{ 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-04-07 17:40:38 +00:00
STRING { *p = MkNode(Value, NULLNODE, NULLNODE, &dot);
(*p)->nd_type = string_type;
}
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-04-06 17:42:56 +00:00
NOT { *p = MkNode(Uoper, NULLNODE, NULLNODE, &dot); }
factor(&((*p)->nd_left))
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
} :
'{' {
dot.tk_symb = SET;
1986-04-07 17:40:38 +00:00
*pnd = nd = MkNode(Link, NULLNODE, NULLNODE, &dot);
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-06 17:42:56 +00:00
/* selector */
'.' { *pnd = MkNode(Oper, *pnd, NULLNODE, &dot); }
IDENT { (*pnd)->nd_right =
MkNode(Value, NULLNODE, NULLNODE, &dot);
}
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-04-06 17:42:56 +00:00
visible_designator_tail(struct node **pnd;):
'[' { *pnd = MkNode(Oper, *pnd, NULLNODE, &dot); }
ExpList(&((*pnd)->nd_right))
']'
1986-03-27 17:37:41 +00:00
|
1986-04-06 17:42:56 +00:00
'^' { *pnd = MkNode(Oper, *pnd, NULLNODE, &dot); }
1986-03-20 14:52:03 +00:00
;