From: Jan T. <de...@us...> - 2002-05-02 14:20:07
|
Update of /cvsroot/net-script/netscript2/src/perl/NetScript/Engine In directory usw-pr-cvs1:/tmp/cvs-serv22781 Modified Files: StatementEvaluator.pm Log Message: * added variable substitution mechanism... Index: StatementEvaluator.pm =================================================================== RCS file: /cvsroot/net-script/netscript2/src/perl/NetScript/Engine/StatementEvaluator.pm,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** StatementEvaluator.pm 26 Apr 2002 10:49:06 -0000 1.2 --- StatementEvaluator.pm 2 May 2002 14:20:04 -0000 1.3 *************** *** 19,22 **** --- 19,24 ---- use NetScript::Interpreter; + + #-------------------------------------------------------- # Globals *************** *** 24,27 **** --- 26,48 ---- $VERSION = '1.0'; + + #/** + # Constant for String-Filter. + # @public + # @final + #*/ + sub STRING_FILTER { + "STRING"; + } + + #/** + # Constant for XML-Filter. + # @public + # @final + #*/ + sub XML_FILTER { + "XML"; + } + #/** # Ctor. *************** *** 48,53 **** sub evaluateStatement { my ($this, $statement) = @_; ! $statement =~ s/\$\(test\)/TEST/g; ! $statement; } --- 69,156 ---- sub evaluateStatement { my ($this, $statement) = @_; ! my $aString = $statement; ! my $stringBef = $statement; ! ! # ! # Look if this string consists of more than spaces ! # and newlines. Nearly 50% of all Strings are empty ! # so some time can be saved by this. ! # ! if ($aString =~ /[^ \n]/) { ! ! # Reworked evaluation of variables. ! # Now evaluating strings from the center to the edges. ! # Evaluation is done by searching for the innermost pair ! # of parentheses, evaluating it and then repeat until there ! # are no more parentheses in the string. ! ! # Quite hefty regexp. Does find the innermost pair parentheses which is not quoted. ! while ( $aString =~ /^(.*[^\\])\(((\\\(|\\\)|[^\(\)])*[^\\\)])?\)((\\\(|[^\(])*)$/ ) { ! # Search for the innermost pair of parentheses. ! my $pref = $1; # save string prefix ! my $action = ""; # the action to do ! my $param = $2; # the content of the parentheses ! my $suff = $4; # save the string suffix ! my $result = ""; ! ! # Unquote quoted parentheses ! $param =~ s/\\\(/\(/g; ! $param =~ s/\\\)/\)/g; ! ! # Find reserved words ! if ($pref =~ /^(.*)(\$|eval|@|str|\#)$/) { ! $pref = $1; ! $action = $2; ! } ! # Find quoted reserved words. ! if ($action ne '' && $pref =~ /^(.*)(\\)$/) { ! $action = $2.$action; ! $pref = $1; ! } ! ! # now evaluate the action and generate a result. ! if ($action eq '$' ) { # a variable or array statement ! if ($param =~ /^([^:]+):(.*$)/) { # array statement ! $result = $this -> getArrayValue($1, $2); ! } ! else { ! $result = $this -> getVariableValue( $param ); ! # if (! defined($result)) { ! # die "[standardlib] Error: undefined variable ", $param, "\n"; ! # } ! } ! } ! elsif ($action eq 'eval') { # an eval-statement ! $result = eval($param); ! } ! elsif ($action eq '@') { #last index of an array ! $result = $this -> getLargestArrayIndex( $param ); ! } ! elsif ($action eq '#') { #length of string ! $result = $this -> getStringLength( $param ); ! } ! elsif ($action eq 'str') { # string-filter ! $result = $this -> filter( $param, $this -> STRING_FILTER() ); ! } ! elsif ($action eq 'xml') { # xml-filter ! $result = $this -> filter( $param, $this -> XML_FILTER() ); ! } ! elsif ($action =~ /^\\(.*)$/) { # requote ! $result = $1."\(".$param."\)"; ! } ! else { ! $result = "\(".$param."\)"; ! } ! # Requote remaining parentheses. ! $result =~ s/\(/\\\(/g; ! $result =~ s/\)/\\\)/g; ! $aString = $pref . $result . $suff; ! } ! ! # Unquote all parentheses ! $aString =~ s/\\\(/\(/g; ! $aString =~ s/\\\)/\)/g; ! } ! return $aString; } *************** *** 55,64 **** --- 158,189 ---- #/** # Returns the value of a variable. + # @param the name of the variable # @protected #*/ sub getVariableValue { my ($this, $variable) = @_; + "Wert: " . $variable; } + #/** + # Returns the value of the array with the given index. + # @param the name of the array + # @param the index within the array + # @protected + #*/ + sub getArrayValue { + my ($this, $array, $index) = @_; + "Array [$index]: ". $array; + } + + #/** + # Returns the largest valid index in the given array. + # @param the name of the array + # @protected + #*/ + sub getLargestArrayIndex { + my ( $this, $array ) = @_; + "10"; + } #/** *************** *** 71,74 **** --- 196,212 ---- sub getFunctionResult { my ($this, $function, $arg ) = @_; + } + + + #/** + # Filters the given scalar. + # @param a scalar to filter. + # @param a filter mode + # @return the filtered string + # @protected + #*/ + sub filter { + my ($this, $toFilter, $mode) = @_; + "Filtered ($mode): $toFilter"; } |