Exemple #1
0
//------------------------------------------------------------------------------
INT_PTR nseel_lookup(compileContext *ctx, int *typeOfObject)
{
	int i, ti, wb;
	const char *nptr;
	nseel_gettoken(ctx,ctx->yytext, sizeof(ctx->yytext));

	if (!strnicmp(ctx->yytext,"reg",3) && strlen(ctx->yytext) == 5 && isdigit(ctx->yytext[3]) && isdigit(ctx->yytext[4]) && (i=atoi(ctx->yytext+3))>=0 && i<100)
	{
		*typeOfObject=IDENTIFIER;
		return i+NSEEL_GLOBALVAR_BASE;
	}

	i=0;
	for (wb = 0; wb < ctx->varTable_numBlocks; wb ++)
	{
		int namepos=0;
		for (ti = 0; ti < NSEEL_VARS_PER_BLOCK; ti ++)
		{        
			if (!ctx->varTable_Names[wb][namepos]) break;

			if (!strnicmp(ctx->varTable_Names[wb]+namepos,ctx->yytext,NSEEL_MAX_VARIABLE_NAMELEN))
			{
				*typeOfObject = IDENTIFIER;
				return i;
			}

			namepos += NSEEL_MAX_VARIABLE_NAMELEN;
			i++;
		}
		if (ti < NSEEL_VARS_PER_BLOCK) break;
	}


	nptr = ctx->yytext;
	if (!strcasecmp(nptr,"if")) nptr="_if";
	else if (!strcasecmp(nptr,"bnot")) nptr="_not";
	else if (!strcasecmp(nptr,"assign")) nptr="_set";
	else if (!strcasecmp(nptr,"equal")) nptr="_equal";
	else if (!strcasecmp(nptr,"below")) nptr="_below";
	else if (!strcasecmp(nptr,"above")) nptr="_above";
	else if (!strcasecmp(nptr,"megabuf")) nptr="_mem";
	else if (!strcasecmp(nptr,"gmegabuf")) nptr="_gmem";
	else if (!strcasecmp(nptr,"int")) nptr="floor";

	for (i=0;nseel_getFunctionFromTable(i);i++)
	{
		functionType *f=nseel_getFunctionFromTable(i);
		if (!strcasecmp(f->name, nptr))
		{
			switch (f->nParams)
			{
			case 1: *typeOfObject = FUNCTION1; break;
			case 2: *typeOfObject = FUNCTION2; break;
			case 3: *typeOfObject = FUNCTION3; break;
			default: *typeOfObject = IDENTIFIER; break;
			}
			return i;
		}
	}

	*typeOfObject = IDENTIFIER;
	nseel_setLastVar(ctx);
	return nseel_setVar(ctx,-1);
}
Exemple #2
0
//#ln 131 "bison.simple"
int nseel_yyparse(compileContext *ctx, char *exp)
{
  register int yystate;
  register int yyn;
  register short *yyssp;
  register YYSTYPE *yyvsp;
  int yyerrstatus;	/*  number of tokens to shift before error messages enabled */
  int yychar1;		/*  lookahead token as an internal (translated) token number */

  short	yyssa[YYINITDEPTH];	/*  the state stack			*/
  YYSTYPE yyvsa[YYINITDEPTH];	/*  the semantic value stack		*/

  short *yyss = yyssa;		/*  refer to the stacks thru separate pointers */
  YYSTYPE *yyvs = yyvsa;	/*  to allow yyoverflow to reallocate them elsewhere */

  int yystacksize = YYINITDEPTH;

  YYSTYPE yyval;		/*  the variable used to return		*/
				/*  semantic values from the action	*/
				/*  routines				*/

  int yylen;

  ctx->yylval = 0;
  yystate = 0;
  yyerrstatus = 0;
  ctx->yynerrs = 0;
  ctx->yychar = YYEMPTY;		/* Cause a token to be read.  */

  /* Initialize stack pointers.
     Waste one element of value and location stack
     so that they stay on the same level as the state stack.  */

  yyssp = yyss - 1;
  yyvsp = yyvs;

/* Push a new state, which is found in  yystate  .  */
/* In all cases, when you get here, the value and location stacks
   have just been pushed. so pushing a state here evens the stacks.  */
yynewstate:

  *++yyssp = yystate;

  if (yyssp >= yyss + yystacksize - 1)
    {
      /* Give user a chance to reallocate the stack */
      /* Use copies of these so that the &'s don't force the real ones into memory. */
      YYSTYPE *yyvs1 = yyvs;
      short *yyss1 = yyss;

      /* Get the current used size of the three stacks, in elements.  */
      int size = yyssp - yyss + 1;

      if (yystacksize >= YYMAXDEPTH)
    	{
	      YYERROR("internal error: parser stack overflow");
	      return 2;
	    }

      yyssp = yyss + size - 1;
      yyvsp = yyvs + size - 1;


      if (yyssp >= yyss + yystacksize - 1) YYABORT;
    }


// yybackup:

/* Do appropriate processing given the current state.  */
/* Read a lookahead token if we need one and don't already have one.  */
/* yyresume: */

  /* First try to decide what to do without reference to lookahead token.  */

  yyn = yypact[yystate];
  if (yyn == YYFLAG)
    goto yydefault;

  /* Not known => get a lookahead token if don't already have one.  */

  /* yychar is either YYEMPTY or YYEOF
     or a valid token in external form.  */

  if (ctx->yychar == YYEMPTY)
    {
//	yyStackSize = yyssp - (yyss - 1);
	ctx->yychar = YYLEX;
    }

  /* Convert token to internal form (in yychar1) for indexing tables with */

  if (ctx->yychar <= 0)		/* This means end of input. */
    {
      yychar1 = 0;
      ctx->yychar = YYEOF;		/* Don't call YYLEX any more */

    }
  else
    {
      yychar1 = YYTRANSLATE(ctx->yychar);

    }

  yyn += yychar1;
  if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
    goto yydefault;

  yyn = yytable[yyn];

  /* yyn is what to do for this token type in this state.
     Negative => reduce, -yyn is rule number.
     Positive => shift, yyn is new state.
       New state is final state => don't bother to shift,
       just return success.
     0, or most negative number => error.  */

  if (yyn < 0)
    {
      if (yyn == YYFLAG)
	goto yyerrlab;
      yyn = -yyn;
      goto yyreduce;
    }
  else if (yyn == 0)
    goto yyerrlab;

  if (yyn == YYFINAL)
    YYACCEPT;

  /* Shift the lookahead token.  */


  /* Discard the token being shifted unless it is eof.  */
  if (ctx->yychar != YYEOF)
    ctx->yychar = YYEMPTY;

  *++yyvsp = ctx->yylval;

  /* count tokens shifted since error; after three, turn off error status.  */
  if (yyerrstatus) yyerrstatus--;

  yystate = yyn;
  goto yynewstate;

/* Do the default action for the current state.  */
yydefault:

  yyn = yydefact[yystate];
  if (yyn == 0)
    goto yyerrlab;

/* Do a reduction.  yyn is the number of a rule to reduce with.  */
yyreduce:
  yylen = yyr2[yyn];
  yyval = yyvsp[1-yylen]; /* implement default value of the action */


  switch (yyn) {

case 1:
//#ln 32 "cal.y"
{       yyval = yyvsp[0]; ctx->result = yyvsp[0];     ;
    break;}
case 2:
//#ln 34 "cal.y"
{                                {
                                 int i = (int)nseel_setVar(ctx,(int)yyvsp[-2]);
                                 int v=nseel_getVar(ctx,i);

                                 yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_ASSIGN, v, (int)yyvsp[0]);
                                 ctx->result = yyval;
                                 }
                       ;
    break;}
case 3:
//#ln 50 "cal.y"
{ yyval = yyvsp[0] ;
    break;}
case 4:
//#ln 55 "cal.y"
{       yyval = nseel_getVar(ctx,(int)yyvsp[0]);;
    break;}
case 5:
//#ln 57 "cal.y"
{       yyval = yyvsp[0];;
    break;}
case 6:
//#ln 59 "cal.y"
{       yyval = yyvsp[-1];;
    break;}
case 7:
//#ln 64 "cal.y"
{ yyval = yyvsp[0];              ;
    break;}
case 8:
//#ln 66 "cal.y"
{                                 yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_MULTIPLY, yyvsp[-2], yyvsp[0]);
    break;}
case 9:
//#ln 72 "cal.y"
{                                 yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_DIVIDE, yyvsp[-2], yyvsp[0]);
    break;}
case 10:
//#ln 78 "cal.y"
{                                  yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_MODULO, yyvsp[-2], yyvsp[0]);
    break;}
case 11:
//#ln 84 "cal.y"
{                                 yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_ADD, yyvsp[-2], yyvsp[0]);
    break;}
case 12:
//#ln 90 "cal.y"
{                                 yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_SUB, yyvsp[-2], yyvsp[0]);
    break;}
case 13:
//#ln 96 "cal.y"
{                                  yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_AND, yyvsp[-2], yyvsp[0]);
    break;}
case 14:
//#ln 102 "cal.y"
{                                  yyval = nseel_createCompiledFunction2(ctx,MATH_SIMPLE, FN_OR, yyvsp[-2], yyvsp[0]);
    break;}
case 15:
//#ln 108 "cal.y"
{                                  yyval = nseel_createCompiledFunction1(ctx,MATH_SIMPLE, FN_UMINUS, yyvsp[0]);
    break;}
case 16:
//#ln 114 "cal.y"
{                                 yyval = nseel_createCompiledFunction1(ctx,MATH_SIMPLE, FN_UPLUS, yyvsp[0]);
    break;}
case 17:
//#ln 120 "cal.y"
{       yyval = yyvsp[0];     
    break;}
case 18:
//#ln 125 "cal.y"
{                                  yyval = nseel_createCompiledFunction1(ctx,MATH_FN, (int)yyvsp[-3], yyvsp[-1]);
    break;}
case 19:
//#ln 131 "cal.y"
{                                 yyval = nseel_createCompiledFunction2(ctx,MATH_FN, (int)yyvsp[-5], yyvsp[-3], yyvsp[-1]);
    break;}
case 20:
//#ln 137 "cal.y"
{                                 yyval = nseel_createCompiledFunction3(ctx,MATH_FN, (int)yyvsp[-7], yyvsp[-5], yyvsp[-3], yyvsp[-1]);
    break;}
}
   /* the action file gets copied in in place of this dollarsign */
//#ln 362 "bison.simple"

  yyvsp -= yylen;
  yyssp -= yylen;

  *++yyvsp = yyval;


  /* Now "shift" the result of the reduction.
     Determine what state that goes to,
     based on the state we popped back to
     and the rule number reduced by.  */

  yyn = yyr1[yyn];

  yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
  if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
    yystate = yytable[yystate];
  else
    yystate = yydefgoto[yyn - YYNTBASE];

  goto yynewstate;

yyerrlab:   /* here on detecting error */

  if (! yyerrstatus)
    /* If not already recovering from an error, report this error.  */
    {
      ++ctx->yynerrs;

#ifdef YYERROR_VERBOSE
      yyn = yypact[yystate];

      if (yyn > YYFLAG && yyn < YYLAST)
	{
	  int size = 0;
	  char *msg;
	  int x, count;

	  count = 0;
	  for (x = 0; x < (sizeof(yytname) / sizeof(char *)); x++)
	    if (yycheck[x + yyn] == x)
	      size += strlen(yytname[x]) + 15, count++;
#error this should not compile
      msg = (char *) xmalloc(size + 15);
	  strcpy(msg, "syntax error");

	  if (count < 5)
	    {
	      count = 0;
	      for (x = 0; x < (sizeof(yytname) / sizeof(char *)); x++)
		if (yycheck[x + yyn] == x)
		  {
		    strcat(msg, count == 0 ? ", expecting `" : " or `");
		    strcat(msg, yytname[x]);
		    strcat(msg, "'");
		    count++;
		  }
	    }
	  YYERROR(msg);
	  free(msg);
	}
      else
#endif /* YYERROR_VERBOSE */
	YYERROR("syntax error");
    }

//yyerrlab1:   /* here on error raised explicitly by an action */

  if (yyerrstatus == 3)
    {
      /* if just tried and failed to reuse lookahead token after an error, discard it.  */

      /* return failure if at end of input */
      if (ctx->yychar == YYEOF) YYABORT;

      ctx->yychar = YYEMPTY;
    }

  /* Else will try to reuse lookahead token
     after shifting the error token.  */

  yyerrstatus = 3;		/* Each real token shifted decrements this */

  goto yyerrhandle;

yyerrdefault:  /* current state does not do anything special for the error token. */

#if 0
  /* This is wrong; only states that explicitly want error tokens
     should shift them.  */
  yyn = yydefact[yystate];  /* If its default is to accept any token, ok.  Otherwise pop it.*/
  if (yyn) goto yydefault;
#endif

yyerrpop:   /* pop the current state because it cannot handle the error token */

  if (yyssp == yyss) YYABORT;
  yyvsp--;
  yystate = *--yyssp;


yyerrhandle:

  yyn = yypact[yystate];
  if (yyn == YYFLAG)
    goto yyerrdefault;

  yyn += YYTERROR;
  if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
    goto yyerrdefault;

  yyn = yytable[yyn];
  if (yyn < 0)
    {
      if (yyn == YYFLAG)
	goto yyerrpop;
      yyn = -yyn;
      goto yyreduce;
    }
  else if (yyn == 0)
    goto yyerrpop;

  if (yyn == YYFINAL)
    YYACCEPT;

  *++yyvsp = ctx->yylval;

  yystate = yyn;
  goto yynewstate;
}