gzdoom-gles/src/c_expr.cpp

812 lines
22 KiB
C++
Raw Normal View History

/*
** c_expr.cpp
** Console commands dealing with mathematical expressions
**
**---------------------------------------------------------------------------
** Copyright 1998-2006 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
// HEADER FILES ------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "c_dispatch.h"
#include "c_cvars.h"
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
enum EProductionType
{
PROD_String, PROD_Double
};
struct FProduction
{
EProductionType Type;
};
struct FStringProd : public FProduction
{
char Value[1];
};
struct FDoubleProd : public FProduction
{
double Value;
};
struct FProducer
{
char Token[4];
FProduction *(*DoubleProducer) (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *(*StringProducer) (FStringProd *prod1, FStringProd *prod2);
};
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
bool IsFloat (const char *str);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static FProduction *ParseExpression (FCommandLine &argv, int &parsept);
static const char *IsNum (const char *str);
static FStringProd *NewStringProd (const char *str);
static FStringProd *NewStringProd (size_t len);
static FDoubleProd *NewDoubleProd (double val);
static FStringProd *DoubleToString (FProduction *prod);
static FDoubleProd *StringToDouble (FProduction *prod);
void MaybeStringCoerce (FProduction *&prod1, FProduction *&prod2);
void MustStringCoerce (FProduction *&prod1, FProduction *&prod2);
void DoubleCoerce (FProduction *&prod1, FProduction *&prod2);
FProduction *ProdAddDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdAddStr (FStringProd *prod1, FStringProd *prod2);
FProduction *ProdSubDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdMulDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdDivDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdModDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdPowDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdLTDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdLTEDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdGTDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdGTEDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdEqDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdNeqDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdXorDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdAndDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdOrDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdLAndDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdLOrDbl (FDoubleProd *prod1, FDoubleProd *prod2);
FProduction *ProdLTStr (FStringProd *prod1, FStringProd *prod2);
FProduction *ProdLTEStr (FStringProd *prod1, FStringProd *prod2);
FProduction *ProdGTStr (FStringProd *prod1, FStringProd *prod2);
FProduction *ProdGTEStr (FStringProd *prod1, FStringProd *prod2);
FProduction *ProdEqStr (FStringProd *prod1, FStringProd *prod2);
FProduction *ProdNeqStr (FStringProd *prod1, FStringProd *prod2);
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static FProducer Producers[] =
{
{ "+", ProdAddDbl, ProdAddStr },
{ "-", ProdSubDbl, NULL },
{ "*", ProdMulDbl, NULL },
{ "/", ProdDivDbl, NULL },
{ "%", ProdModDbl, NULL },
{ "^", ProdPowDbl, NULL },
{ "<", ProdLTDbl, ProdLTStr },
{ "<=", ProdLTEDbl, ProdLTEStr },
{ ">", ProdGTDbl, ProdGTStr },
{ ">=", ProdGTEDbl, ProdGTEStr },
{ "=", ProdEqDbl, ProdEqStr },
{ "==", ProdEqDbl, ProdEqStr },
{ "!=", ProdNeqDbl, ProdNeqStr },
{ "<>", ProdNeqDbl, ProdNeqStr },
{ "xor", ProdXorDbl, NULL },
{ "&", ProdAndDbl, NULL },
{ "|", ProdOrDbl, NULL },
{ "&&", ProdLAndDbl, NULL },
{ "||", ProdLOrDbl, NULL }
};
// CODE --------------------------------------------------------------------
//==========================================================================
//
// ParseExpression
//
// Builds a production from an expression. The supported syntax is LISP-like
// but without parentheses.
//
//==========================================================================
static FProduction *ParseExpression (FCommandLine &argv, int &parsept)
{
if (parsept >= argv.argc())
return NULL;
const char *token = argv[parsept++];
FProduction *prod1 = NULL, *prod2 = NULL, *prod3 = NULL;
if (IsFloat (token))
{
return NewDoubleProd (atof(token));
}
else if (stricmp (token, "true") == 0)
{
return NewDoubleProd (1.0);
}
else if (stricmp (token, "false") == 0)
{
return NewDoubleProd (0.0);
}
else
{
- Fixed compilation with mingw again. - Added multiple-choice sound sequences. These overcome one of the major deficiences of the Hexen-inherited SNDSEQ system while still being Hexen compatible: Custom door sounds can now use different opening and closing sequences, for both normal and blazing speeds. - Added a serializer for TArray. - Added a countof macro to doomtype.h. See the1's blog to find out why it's implemented the way it is. <http://blogs.msdn.com/the1/articles/210011.aspx> - Added a new method to FRandom for getting random numbers larger than 255, which lets me: - Fixed: SNDSEQ delayrand commands could delay for no more than 255 tics. - Fixed: If you're going to have sector_t.SoundTarget, then they need to be included in the pointer cleanup scans. - Ported back newer name code from 2.1. - Fixed: Using -warp with only one parameter in Doom and Heretic to select a map on episode 1 no longer worked. - New: Loading a multiplayer save now restores the players based on their names rather than on their connection order. Using connection order was sensible when -net was the only way to start a network game, but with -host/-join, it's not so nice. Also, if there aren't enough players in the save, then the extra players will be spawned normally, so you can continue a saved game with more players than you started it with. - Added some new SNDSEQ commands to make it possible to define Heretic's ambient sounds in SNDSEQ: volumerel, volumerand, slot, randomsequence, delayonce, and restart. With these, it is basically possible to obsolete all of the $ambient SNDINFO commands. - Fixed: Sound sequences would only execute one command each time they were ticked. - Fixed: No bounds checking was done on the volume sound sequences played at. - Fixed: The tic parameter to playloop was useless and caused it to act like a redundant playrepeat. I have removed all the logic that caused playloop to play repeating sounds, and now it acts like an infinite sequence of play/delay commands until the sequence is stopped. - Fixed: Sound sequences were ticked every frame, not every tic, so all the delay commands were timed incorrectly and varied depending on your framerate. Since this is useful for restarting looping sounds that got cut off, I have not changed this. Instead, the delay commands now record the tic when execution should resume, not the number of tics left to delay. SVN r57 (trunk)
2006-04-21 01:22:55 +00:00
for (size_t i = 0; i < countof(Producers); ++i)
{
if (strcmp (Producers[i].Token, token) == 0)
{
prod1 = ParseExpression (argv, parsept);
prod2 = ParseExpression (argv, parsept);
if (prod1 == NULL || prod2 == NULL)
{
goto missing;
}
if (Producers[i].StringProducer == NULL)
{
DoubleCoerce (prod1, prod2);
}
else if (Producers[i].DoubleProducer == NULL)
{
MustStringCoerce (prod1, prod2);
}
else
{
MaybeStringCoerce (prod1, prod2);
}
if (prod1->Type == PROD_String)
{
prod3 = Producers[i].StringProducer ((FStringProd *)prod1, (FStringProd *)prod2);
}
else
{
prod3 = Producers[i].DoubleProducer ((FDoubleProd *)prod1, (FDoubleProd *)prod2);
}
goto done;
}
}
if (strcmp ("!", token) == 0)
{
prod1 = ParseExpression (argv, parsept);
if (prod1 == NULL)
{
goto missing;
}
if (prod1->Type == PROD_String)
{
prod1 = StringToDouble (prod1);
}
prod3 = NewDoubleProd (!static_cast<FDoubleProd *>(prod1)->Value);
goto done;
}
return NewStringProd (token);
}
missing:
Printf ("Missing argument to %s\n", token);
done:
if (prod2 != NULL) M_Free (prod2);
if (prod1 != NULL) M_Free (prod1);
return prod3;
}
//==========================================================================
//
// IsFloat
//
//==========================================================================
bool IsFloat (const char *str)
{
const char *pt;
if (*str == '+' || *str == '-')
str++;
if (*str == '.')
{
pt = str;
}
else
{
pt = IsNum (str);
if (pt == NULL)
return false;
}
if (*pt == '.')
{
pt = IsNum (pt+1);
if (pt == NULL)
return false;
}
if (*pt == 'e' || *pt == 'E')
{
pt++;
if (*pt == '+' || *pt == '-')
pt++;
pt = IsNum (pt);
}
return pt != NULL && *pt == 0;
}
//==========================================================================
//
// IsNum
//
//==========================================================================
static const char *IsNum (const char *str)
{
const char *start = str;
while (*str)
{
if (*str >= '0' && *str <= '9')
str++;
else
break;
}
return (str > start) ? str : NULL;
}
//==========================================================================
//
// NewStringProd (from a string)
//
//==========================================================================
static FStringProd *NewStringProd (const char *str)
{
FStringProd *prod = (FStringProd *)M_Malloc (sizeof(FStringProd)+strlen(str));
prod->Type = PROD_String;
strcpy (prod->Value, str);
return prod;
}
//==========================================================================
//
// NewStringProd (from a length)
//
//==========================================================================
static FStringProd *NewStringProd (size_t len)
{
FStringProd *prod = (FStringProd *)M_Malloc (sizeof(FStringProd)+len);
prod->Type = PROD_String;
prod->Value[0] = 0;
return prod;
}
//==========================================================================
//
// NewDoubleProd
//
//==========================================================================
static FDoubleProd *NewDoubleProd (double val)
{
FDoubleProd *prod = (FDoubleProd *)M_Malloc (sizeof(FDoubleProd));
prod->Type = PROD_Double;
prod->Value = val;
return prod;
}
//==========================================================================
//
// DoubleToString
//
//==========================================================================
static FStringProd *DoubleToString (FProduction *prod)
{
char buf[128];
FStringProd *newprod;
sprintf (buf, "%g", static_cast<FDoubleProd *>(prod)->Value);
newprod = NewStringProd (buf);
M_Free (prod);
return newprod;
}
//==========================================================================
//
// StringToDouble
//
//==========================================================================
static FDoubleProd *StringToDouble (FProduction *prod)
{
FDoubleProd *newprod;
newprod = NewDoubleProd (atof (static_cast<FStringProd *>(prod)->Value));
M_Free (prod);
return newprod;
}
//==========================================================================
//
// MaybeStringCoerce
//
// If one of the parameters is a string, convert the other to a string.
//
//==========================================================================
void MaybeStringCoerce (FProduction *&prod1, FProduction *&prod2)
{
if (prod1->Type == PROD_String)
{
if (prod2->Type == PROD_Double)
{
prod2 = DoubleToString (prod2);
}
}
else if (prod2->Type == PROD_String)
{
prod1 = DoubleToString (prod1);
}
}
//==========================================================================
//
// MustStringCoerce
//
// Ensures that both parameters are strings
//
//==========================================================================
void MustStringCoerce (FProduction *&prod1, FProduction *&prod2)
{
if (prod1->Type == PROD_Double)
{
prod1 = DoubleToString (prod1);
}
if (prod2->Type == PROD_Double)
{
prod2 = DoubleToString (prod2);
}
}
//==========================================================================
//
// DoubleCoerce
//
// Ensures that both parameters are doubles
//
//==========================================================================
void DoubleCoerce (FProduction *&prod1, FProduction *&prod2)
{
if (prod1->Type == PROD_String)
{
prod1 = StringToDouble (prod1);
}
if (prod2->Type == PROD_String)
{
prod2 = StringToDouble (prod2);
}
}
//==========================================================================
//
// ProdAddDbl
//
//==========================================================================
FProduction *ProdAddDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (prod1->Value + prod2->Value);
}
//==========================================================================
//
// ProdAddStr
//
//==========================================================================
FProduction *ProdAddStr (FStringProd *prod1, FStringProd *prod2)
{
size_t len = strlen (prod1->Value) + strlen (prod2->Value) + 1;
FStringProd *prod = NewStringProd (len);
strcpy (prod->Value, prod1->Value);
strcat (prod->Value, prod2->Value);
return prod;
}
//==========================================================================
//
// ProdSubDbl
//
//==========================================================================
FProduction *ProdSubDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (prod1->Value - prod2->Value);
}
//==========================================================================
//
// ProdMulDbl
//
//==========================================================================
FProduction *ProdMulDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (prod1->Value * prod2->Value);
}
//==========================================================================
//
// ProdDivDbl
//
//==========================================================================
FProduction *ProdDivDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (prod1->Value / prod2->Value);
}
//==========================================================================
//
// ProdModDbl
//
//==========================================================================
FProduction *ProdModDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (fmod (prod1->Value, prod2->Value));
}
//==========================================================================
//
// ProdPowDbl
//
//==========================================================================
FProduction *ProdPowDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (pow (prod1->Value, prod2->Value));
}
//==========================================================================
//
// ProdLTDbl
//
//==========================================================================
FProduction *ProdLTDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (prod1->Value < prod2->Value);
}
//==========================================================================
//
// ProdLTEDbl
//
//==========================================================================
FProduction *ProdLTEDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (prod1->Value <= prod2->Value);
}
//==========================================================================
//
// ProdGTDbl
//
//==========================================================================
FProduction *ProdGTDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (prod1->Value > prod2->Value);
}
//==========================================================================
//
// ProdGTEDbl
//
//==========================================================================
FProduction *ProdGTEDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (prod1->Value >= prod2->Value);
}
//==========================================================================
//
// ProdEqDbl
//
//==========================================================================
FProduction *ProdEqDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (prod1->Value == prod2->Value);
}
//==========================================================================
//
// ProdNeqDbl
//
//==========================================================================
FProduction *ProdNeqDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd (prod1->Value != prod2->Value);
}
//==========================================================================
//
// ProdLTStr
//
//==========================================================================
FProduction *ProdLTStr (FStringProd *prod1, FStringProd *prod2)
{
return NewDoubleProd (stricmp (prod1->Value, prod2->Value) < 0);
}
//==========================================================================
//
// ProdLTEStr
//
//==========================================================================
FProduction *ProdLTEStr (FStringProd *prod1, FStringProd *prod2)
{
return NewDoubleProd (stricmp (prod1->Value, prod2->Value) <= 0);
}
//==========================================================================
//
// ProdGTStr
//
//==========================================================================
FProduction *ProdGTStr (FStringProd *prod1, FStringProd *prod2)
{
return NewDoubleProd (stricmp (prod1->Value, prod2->Value) > 0);
}
//==========================================================================
//
// ProdGTEStr
//
//==========================================================================
FProduction *ProdGTEStr (FStringProd *prod1, FStringProd *prod2)
{
return NewDoubleProd (stricmp (prod1->Value, prod2->Value) >= 0);
}
//==========================================================================
//
// ProdEqStr
//
//==========================================================================
FProduction *ProdEqStr (FStringProd *prod1, FStringProd *prod2)
{
return NewDoubleProd (stricmp (prod1->Value, prod2->Value) == 0);
}
//==========================================================================
//
// ProdNeqStr
//
//==========================================================================
FProduction *ProdNeqStr (FStringProd *prod1, FStringProd *prod2)
{
return NewDoubleProd (stricmp (prod1->Value, prod2->Value) != 0);
}
//==========================================================================
//
// ProdXorDbl
//
//==========================================================================
FProduction *ProdXorDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd ((double)((SQWORD)prod1->Value ^ (SQWORD)prod2->Value));
}
//==========================================================================
//
// ProdAndDbl
//
//==========================================================================
FProduction *ProdAndDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd ((double)((SQWORD)prod1->Value & (SQWORD)prod2->Value));
}
//==========================================================================
//
// ProdOrDbl
//
//==========================================================================
FProduction *ProdOrDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd ((double)((SQWORD)prod1->Value | (SQWORD)prod2->Value));
}
//==========================================================================
//
// ProdLAndDbl
//
//==========================================================================
FProduction *ProdLAndDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd ((double)((SQWORD)prod1->Value && (SQWORD)prod2->Value));
}
//==========================================================================
//
// ProdLOrDbl
//
//==========================================================================
FProduction *ProdLOrDbl (FDoubleProd *prod1, FDoubleProd *prod2)
{
return NewDoubleProd ((double)((SQWORD)prod1->Value || (SQWORD)prod2->Value));
}
//==========================================================================
//
//
//
//==========================================================================
//==========================================================================
//
// CCMD test
//
// If <expr> is non-zero, execute <true cmd>.
// If <expr> is zero, execute [false cmd] if specified.
//
//==========================================================================
CCMD (test)
{
int parsept = 1;
FProduction *prod = ParseExpression (argv, parsept);
if (prod == NULL || parsept >= argv.argc())
{
Printf ("Usage: test <expr> <true cmd> [false cmd]\n");
}
else
{
if (prod->Type == PROD_String)
{
prod = StringToDouble (prod);
}
if (static_cast<FDoubleProd *>(prod)->Value != 0.0)
{
AddCommandString (argv[parsept]);
}
else if (++parsept < argv.argc())
{
AddCommandString (argv[parsept]);
}
}
if (prod != NULL)
{
M_Free (prod);
}
}
//==========================================================================
//
// CCMD eval
//
// Evaluates an expression and either prints it to the console or stores
// it in an existing cvar.
//
//==========================================================================
CCMD (eval)
{
if (argv.argc() >= 2)
{
int parsept = 1;
FProduction *prod = ParseExpression (argv, parsept);
if (prod != NULL)
{
if (parsept < argv.argc())
{
FBaseCVar *var = FindCVar (argv[parsept], NULL);
if (var == NULL)
{
Printf ("Unknown variable %s\n", argv[parsept]);
}
else
{
UCVarValue val;
if (prod->Type == PROD_Double)
{
val.Float = (float)static_cast<FDoubleProd *>(prod)->Value;
var->SetGenericRep (val, CVAR_Float);
}
else
{
val.String = static_cast<FStringProd *>(prod)->Value;
var->SetGenericRep (val, CVAR_String);
}
}
}
else
{
if (prod->Type == PROD_Double)
{
Printf ("%g\n", static_cast<FDoubleProd *>(prod)->Value);
}
else
{
Printf ("%s\n", static_cast<FStringProd *>(prod)->Value);
}
}
M_Free (prod);
return;
}
}
Printf ("Usage: eval <expression> [variable]\n");
}