📄 getsym.c
字号:
getch();
lastst = eq;
}
else
lastst = assign;
break;
case '>':
getch();
if (lastch == '=')
{
getch();
lastst = geq;
}
else if (lastch == '>')
{
getch();
if (lastch == '=')
{
getch();
lastst = asrshift;
}
else
lastst = rshift;
}
else
lastst = gt;
break;
case '<':
getch();
if (incconst)
{
for (i = 0; i < MAX_STRLEN; ++i)
{
if (lastch == '>')
break;
if ((j = getsch()) == - 1)
break;
else
laststr[i] = j;
}
laststr[i] = 0;
lastst = sconst;
laststrlen = i;
if (lastch != '>')
generror(ERR_NEEDCHAR, '>', 0);
else
getch();
}
else
if (lastch == '=')
{
getch();
lastst = leq;
}
else if (lastch == '<')
{
getch();
if (lastch == '=')
{
getch();
lastst = aslshift;
}
else
lastst = lshift;
}
else
lastst = lt;
break;
case '\\':
getch();
lastst = backslash;
break;
case '\'':
getch();
j = 0;
k = 0;
ival = 0;
for (i = 0, j = 0, k = 0; i < 4; i++, k += 8)
{
if (lastch == '\'')
break;
j = getsch(); /* get a string char */
ival += j << k;
}
if (i == 0)
{
generror(ERR_CHAR4CHAR, 0, 0);
getch();
}
else if (lastch != '\'')
{
while (*lptr && *lptr != '\'')
lptr++;
if (*lptr)
lptr++;
generror(ERR_CHAR4CHAR, 0, 0);
}
else
getch();
lastst = cconst;
break;
case 0x2d4:
getch();
i = 0;
while (lastch != '\"' && lastch)
i = installphichar(lastch, laststr, i);
if ((lastch &0x7f) != '\"')
generror(ERR_NEEDCHAR, '\"', 0);
else
getch();
laststrlen = i;
size = i; // strlen(laststr);
lastst = sconst;
break;
case '\"':
size = 0;
while (lastch == '\"')
{
getch();
for (i = size; i < MAX_STRLEN; ++i)
{
if (lastch == '\"')
break;
if ((j = getsch()) == - 1)
break;
else
laststr[i] = j;
}
laststr[i] = 0;
laststrlen = size = i;
lastst = sconst;
if (lastch != '\"')
generror(ERR_NEEDCHAR, '\"', 0);
else
{
getch();
while (iswhitespacechar(lastch) && lastch >= 32)
getch();
}
}
break;
case '!':
getch();
if (lastch == '=')
{
getch();
lastst = neq;
}
else
lastst = not;
break;
case '%':
getch();
if (lastch == '=')
{
getch();
lastst = asmodop;
}
else
lastst = modop;
break;
case '~':
getch();
lastst = compl;
break;
case '.':
if (isdigit(*lptr))
getnum();
else
{
getch();
#ifdef CPLUSPLUS
if (prm_cplusplus && lastch == '*')
{
getch();
lastst = dotstar;
}
else
#endif
if (lastch == '.')
{
getch();
if (lastch == '.')
{
getch();
lastst = ellipse;
break;
}
else
{
generror(ERR_ILLCHAR, lastch, 0);
}
}
else
lastst = dot;
}
break;
case ',':
getch();
lastst = comma;
break;
case '&':
getch();
if (lastch == '&')
{
lastst = land;
getch();
}
else if (lastch == '=')
{
lastst = asand;
getch();
}
else
lastst = and;
break;
case '|':
getch();
if (lastch == '|')
{
lastst = lor;
getch();
}
else if (lastch == '=')
{
lastst = asor;
getch();
}
else
lastst = or;
break;
case '(':
getch();
lastst = openpa;
break;
case ')':
getch();
lastst = closepa;
break;
case '[':
getch();
lastst = openbr;
break;
case ']':
getch();
lastst = closebr;
break;
case '{':
getch();
lastst = begin;
break;
case '}':
getch();
lastst = end;
break;
case '?':
getch();
lastst = hook;
break;
default:
if (iscommentchar(lastch))
{
do
{
getch();
}
while (!iscommentchar(lastch) && lastch != '\n');
}
else
generror(ERR_ILLCHAR, lastch, 0);
getch();
return 1;
}
return 0;
}
/*
* getsym - get next symbol from input stream.
*
* getsym is the basic lexical analyzer. It builds
* basic tokens out of the characters on the input
* stream and sets the following global variables:
*
* lastch: A look behind buffer.
* lastst: type of last symbol read.
* laststr: last string constant read.
* lastid: last identifier read.
* ival: last integer constant read.
* rval: last real constant read.
*
* getsym should be called for all your input needs...
*/
void getsym(void)
{
if (backupchar != - 1)
{
lastst = backupchar;
backupchar = - 1;
return ;
}
if (! *lptr)
{
if (lastst == eol && lastch == - 1 || lastst == eof)
lastst = eof;
else
lastst = eol;
getch();
return ;
}
while (iswhitespacechar(lastch))
{
getch();
if (! *lptr)
{
lastst = eol;
getch();
return ;
}
}
if (lastch == - 1)
lastst = eof;
else if (isdigit(lastch))
getnum();
else if (isstartchar(lastch))
{
getid();
searchkw();
}
else
getsym2();
}
//-------------------------------------------------------------------------
void skip_comma(void)
{
if (lastst == comma)
{
getsym();
if (lastst == eol)
getsym();
}
}
//-------------------------------------------------------------------------
void need_eol(void)
{
if (lastst != eol)
generror(ERR_EXTRA_DATA_ON_LINE, 0, 0);
getsym();
}
//-------------------------------------------------------------------------
void need_begin(void)
{
if (lastst != begin && lastst != kw_begin)
generror(ERR_BEGIN_EXPECTED, 0, 0);
getsym();
need_eol();
}
//-------------------------------------------------------------------------
void need_end(void)
{
if (lastst != end && lastst != kw_end)
generror(ERR_END_EXPECTED, 0, 0);
getsym();
need_eol();
}
/*
* when we need specific punctuation, call one of these routines
*/
int needpunc(enum e_sym p, int *skimlist)
{
if (lastst == p)
{
getsym();
return (TRUE);
}
else
expecttoken(p, skimlist);
return (FALSE);
}
//-------------------------------------------------------------------------
int needpuncexp(enum e_sym p, int *skimlist)
{
if (lastst == p)
{
getsym();
return (TRUE);
}
else
expecttokenexp(p, skimlist);
return (FALSE);
}
//-------------------------------------------------------------------------
int is_number(void)
{
switch (lastst)
{
case iconst:
case iuconst:
case lconst:
case luconst:
return TRUE;
case openpa:
case minus:
return TRUE;
default:
return FALSE;
}
}
/*
* having to back up a character is rare, but sometimes...
*/
void backup(int st)
{
backupchar = lastst;
lastst = st;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -