Raised This Month: $12 Target: $400
 3% 

javascript eval funciton


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
nxlinux
Member
Join Date: Oct 2013
Location: China
Old 08-13-2015 , 09:16   javascript eval funciton
Reply With Quote #1

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

PHP Code:
eval("1+2"); //result is 3 
can soucemod do this?
nxlinux is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 08-13-2015 , 09:18   Re: javascript eval funciton
Reply With Quote #2

No.
__________________
asherkin is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 08-13-2015 , 10:00   Re: javascript eval funciton
Reply With Quote #3

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;

__________________
WildCard65 is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 08-13-2015 , 11:38   Re: javascript eval funciton
Reply With Quote #4

And here I thought ServerCommand() was bad enough already... lol.
Potato Uno is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 08-13-2015 , 16:10   Re: javascript eval funciton
Reply With Quote #5

Quote:
Originally Posted by nxlinux View Post
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?
__________________
11530 is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 08-13-2015 , 16:15   Re: javascript eval funciton
Reply With Quote #6

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.

Last edited by Potato Uno; 08-13-2015 at 16:16.
Potato Uno is offline
nxlinux
Member
Join Date: Oct 2013
Location: China
Old 08-14-2015 , 01:51   Re: javascript eval funciton
Reply With Quote #7

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]++
}
nxlinux is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 08-14-2015 , 06:42   Re: javascript eval funciton
Reply With Quote #8

neither of those are very good examples o.o
Miu is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 08-14-2015 , 10:38   Re: javascript eval funciton
Reply With Quote #9

Let me get this straight, you want to do math operations on a variable but you only got a string to work with?
__________________
WildCard65 is offline
nxlinux
Member
Join Date: Oct 2013
Location: China
Old 08-15-2015 , 05:02   Re: javascript eval funciton
Reply With Quote #10

Quote:
Originally Posted by WildCard65 View Post
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!
nxlinux is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:32.


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