GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 Re: Script Submission » Math expression evaluation » 2016-06-22 07:25:41

How is it possible that this lags so much with more operators? Can anyone figure out why this script causes so much lag?

#2 Script Submission » Math expression evaluation » 2016-06-22 06:02:52

Yambam
Replies: 2
Expand/// infix_evaluate(infix,ret_fromstack,constants)
//
//  Evalutes infix expression. It returns either a postfix
//  expression (string), or it returns the answer from the
//  main stack depending on the ret_fromstack argument.
//
//      infix          expression (infix, e.g. 5*8), string
//      ret_fromstack  return postfix or value from stack, boolean
//      constants      map containing constants like pi and e, ds_map
//
/// GMLscripts.com/license
{
    var str,ret_fromstack,destroy,i,j,k,l,arg,d,len,newstr,newstr2,op,push,do_push;
    str=argument0;
    
    push=ds_queue_create();
    do_push=0;
    
    if argument3=0
    {
        pcount=1+string_count('^',str)+2*(string_count('*',str)+string_count('/',str))+3*(string_count('+',str)+string_count('-',str));
        str=string_repeat('(',pcount)+string_replace_all(string_replace_all(string_replace_all(string_replace_all(string_replace_all(string_replace_all(string_replace_all(str,'(','((((('),')',')))))'),'^',')^('),'*','))*(('),'/','))/(('),'+',')))+((('),'-',')))-(((')+string_repeat(')',pcount);
        argument3=50;
        argument4=ds_stack_create();
        argument5=ds_queue_create();
        global.ret_stack=argument4;
        global.ret_stack_exec=argument5;
        ret_fromstack=argument1;
        destroy=1;
    }
    else
    {
        ret_fromstack=0;
        destroy=0;
    }
    
    if string_char_at(str,1)!="("
    {
        if ds_map_exists(argument2,str)
            ds_stack_push(argument4,ds_map_find_value(argument2,str));
        else
        {
            if string_length(str)=string_length(string_digits(str))+string_count('-',str)+string_count('.',str)
                ds_stack_push(argument4,real(str));
            else
                ds_stack_push(argument4,0);
        }
        return str;
    }
    
    if argument3=1
        return "";
    
    newstr="";
    newstr2="";
    i=1;
    j=1;
    for(k=0;k<50;k+=1)
    {
        if string_char_at(str,i)="("
        {
            i+=1;
            j=i;
            d=1;
            len=string_length(str);
            while(d&&i<=len)
            {
                char=string_char_at(str,i);
                if char="("
                    d+=1;
                if char=")"
                    d-=1;
                i+=1;
            }
        }
        
        op[k]=string_char_at(str,i);
        
        newstr+=infix_evaluate(string_copy(str,j,i-1-j),argument1,argument2,argument3-1,argument4,argument5)+" ";
        if string_pos(op[k],'^*/+-([]')
        {
            newstr2+=" "+op[k];
            
            do_push=1;
            i+=1;
        }
        else
            break;
    }
    
    k_max=k-1;
    
    for(k=k_max;k>=0;k-=1)
    {
        if do_push
        {
            b=ds_stack_pop(argument4);
            a=ds_stack_pop(argument4);
            
            if op[k]='^'
                ds_queue_enqueue(push,power(a,b));
            else if op[k]='*'
                ds_queue_enqueue(push,a*b);
            else if op[k]='/'
            {
                if b=0
                    ds_queue_enqueue(push,a);
                else
                    ds_queue_enqueue(push,a/b);
            }
            else if op[k]='+'
                ds_queue_enqueue(push,a+b);
            else if op[k]='-'
                ds_queue_enqueue(push,a-b);
            else if op[k]=','
            {
                ds_queue_enqueue(argument5,b);
                do_push=0;
            }
            
            if do_push while(!ds_queue_empty(push))
                ds_stack_push(argument4,ds_queue_dequeue(push));
        }
    }
    
    ds_queue_destroy(push);
    
    value=ds_stack_pop(argument4);
    if 0//destroy
    {
        ds_stack_destroy(argument4);
        ds_stack_destroy(argument5);
    }
    if ret_fromstack
        return value;
    
    while(string_count('  ',newstr))
        newstr=string_replace_all(newstr,'  ',' ');
    return newstr+newstr2;
}

Board footer

Powered by FluxBB