AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   javascript eval funciton (https://forums.alliedmods.net/showthread.php?t=269987)

nxlinux 08-13-2015 09:16

javascript eval funciton
 
i what to ahieve some funcion like javascript eval.
such as:

PHP Code:

eval("1+2"); //result is 3 

can soucemod do this?

asherkin 08-13-2015 09:18

Re: javascript eval funciton
 
No.

WildCard65 08-13-2015 10:00

Re: javascript eval funciton
 
Use this:
PHP Code:

#define MAX_COMPLEXITY 2147483647

enum Operators
{
    
Operator_None 0,
    
Operator_Add,
    
Operator_Subtract,
    
Operator_Multiply,
    
Operator_Divide,
    
Operator_Exponental,
};

stock void Operate(ArrayList sumArrayArrayList operatorArrayint bracketfloat value)
{
    
float sum sumArray.Get(bracket);
    switch (
operatorArray.Get(bracket))
    {
        case 
Operator_Add:
            
sum += value;
        case 
Operator_Subtract:
            
sum -= value;
        case 
Operator_Multiply:
            
sum *= value;
        case 
Operator_Divide:
        {
            if (
FloatCompare(value, -0.0000001) == && FloatCompare(value0.0000001) == -1)
                return;
            
sum /= value;
        }
        case 
Operator_Exponental:
            
sum Pow(sumvalue);
        default:
            
sum value;
    }
    
sumArray.Set(bracketsum);
    
operatorArray.Set(bracketOperator_None);
}

stock void OperateString(ArrayList sumArrayArrayList operatorArrayint bracketchar[] valueint size)
{
    if (
StrEqual(value""))
        return;
    
Operate(sumArrayoperatorArraybracketStringToFloat(value));
    
strcopy(valuesize"");
}

stock float eval(const char[] formula)
{
    
int bracket 0size strlen(formula);
    
char buffer[2], strValue[1024];
    
ArrayList sumArray = new ArrayList(.startsize=1), operatorArray = new ArrayList(.startsize=1);
    for (
int i 0<= sizei++)
    {
        
buffer[0] = formula[i];
        switch (
buffer[0])
        {
            case 
' '//Ignore whitespace.
                
continue;
            case 
'\0':
            {
                
OperateString(sumArrayoperatorArraybracketstrValuesizeof(strValue));
                break; 
//As far as I know of, this doesn't break the switch statement and instead targets the for loop.
            
}
            case 
'(':
            {
                if (
bracket == MAX_COMPLEXITY)
                {
                    
LogError("Formula %s is too complex for this stock."formula);
                    return 
0.0;
                }
                
bracket++;
                if (
bracket sumArray.Length)
                {
                    
sumArray.Push(0.0);
                    
operatorArray.Push(Operator_None);
                }
                else
                {
                    
sumArray.Set(bracket0.0);
                    
operatorArray.Set(bracketOperator_None);
                }
            }
            case 
')':
            {
                if (
bracket == 0)
                {
                    
LogError("Formula %s has too many ')'."formula);
                    return 
0.0;
                }
                
OperateString(sumArrayoperatorArraybracketstrValuesizeof(strValue));
                
bracket--;
                
Operate(sumArrayoperatorArraybracketsumArray.Get(bracket+1));
            }
            case 
'+''-''*''/''^':
            {
                
OperateString(sumArrayoperatorArraybracketstrValuesizeof(strValue));
                switch (
buffer[0])
                {
                    case 
'+':
                        
operatorArray.Set(bracketOperator_Add);
                    case 
'-':
                        
operatorArray.Set(bracketOperator_Subtract);
                    case 
'*':
                        
operatorArray.Set(bracketOperator_Multiply);
                    case 
'/':
                        
operatorArray.Set(bracketOperator_Divide);
                    case 
'^':
                        
operatorArray.Set(bracketOperator_Exponental);
                }
            }
            case 
'0''1''2''3''4''5''6''7''8''9''.':
                
StrCat(strValuesizeof(strValue), buffer);
            
//Insert case statements for any custom variables here.
            
default:
            {
                
LogError("Formula %s contains invalid character %c"formulabuffer[0]);
                return 
0.0;
            }
        }
    }
    if (
bracket 0)
    {
        
LogError("Formula %s is invalid, missing some ')'"formula);
        return 
0.0;
    }
    
float ret sumArray.Get(0);
    
delete sumArray;
    
delete operatorArray;
    return 
ret;



Potato Uno 08-13-2015 11:38

Re: javascript eval funciton
 
And here I thought ServerCommand() was bad enough already... lol.

11530 08-13-2015 16:10

Re: javascript eval funciton
 
Quote:

Originally Posted by nxlinux (Post 2332673)
i what to ahieve some funcion like javascript eval.
such as:

PHP Code:

eval("1+2"); //result is 3 

can soucemod do this?

Why do you want this? What's the greater goal you are hoping to achieve?

Potato Uno 08-13-2015 16:15

Re: javascript eval funciton
 
Sourcemod can't do this anyway (I think) because the code in the string would need to be compiled to bytecode before it can be ran in the VM. The VM can't just execute any arbitrary source code - it needs the bytecode compiled version. Only spcomp does the source --> bytecode translation.

Then again I could be talking out of my ass so I dunno. If it can be remade to do that then I'd wonder why smx files + GPLv3 exist together.

nxlinux 08-14-2015 01:51

Re: javascript eval funciton
 
actually i what sourcemod achieve some special function.

For Example:

new String:Skill[2][32] = { "SKILL_1[Client]", "SKILL_2[Client]" };
for(new i = 0; i < 2; i++)
{
eval(Skill[i])++; //here i want SKILL_1[Client]++
}

Miu 08-14-2015 06:42

Re: javascript eval funciton
 
neither of those are very good examples o.o

WildCard65 08-14-2015 10:38

Re: javascript eval funciton
 
Let me get this straight, you want to do math operations on a variable but you only got a string to work with?

nxlinux 08-15-2015 05:02

Re: javascript eval funciton
 
Quote:

Originally Posted by WildCard65 (Post 2333083)
Let me get this straight, you want to do math operations on a variable but you only got a string to work with?

yes, because i have more than 50 variables, if i want achieve my function,i should be use if(..) else if(..) expression, it's too many!


All times are GMT -4. The time now is 11:58.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.