Re: eval() function

new topic     » goto parent     » topic index » view thread      » older message » newer message

Alex Chamberlain wrote:

> After my unsuccessful attempt at using OOEU's eval() function,
> I thought would write my own that will work with the normal rds
> download.
> But it won't work!
>
> }}}
<eucode>
> constant EVAL_FILE_NAME = "tmp_evalfile.ex"
> constant EVAL_ERROR = -1
>
> global function eval(sequence data)
> 	integer tmp_fn, ret
> 	sequence plat
>
> 	tmp_fn = open(EVAL_FILE_NAME, "w")
> 	if tmp_fn = -1 then
> 		puts(1, "Could not opne temporary eval file ... Sorry!")
> 		return EVAL_ERROR
> 	end if
>
> 	puts(tmp_fn, data)
>
> 	if platform() = DOS32 then
> 		plat = "ex"
> 	elsif platform() = WIN32 then
> 		plat = "exw"
> 	elsif (platform() = LINUX) or (platform() = FREEBSD) then
> 		plat = "exu"
> 	end if
> 	puts(1, plat & " " & EVAL_FILE_NAME & " > eval.out")
> 	ret = system_exec(plat & " " & EVAL_FILE_NAME, 0)
> 	system("DELE " & EVAL_FILE_NAME, 0)
> 	return ret
> end function
> </eucode>
{{{

>
> Can somebody help me?


When I try to run that program, I get the following error message:
   DOS32 has not been declared
           if platform() = DOS32 then
                               ^
So at least you should write at the beginning of the program:
   include misc.e

Also when you have opened a file, don't forget to close it!

And you have mixed up some things. blink
Besides the missing 'include' statement, the program can not work,
because you are redirecting the wrong command, you never read the output
of the temporary program, and the function returns the wrong value.
Additionally it looks to me that system_exec() can not be used for
output redirection. But redirecting the output is not necessary at all
here.

Make a clear concept, you need 4 steps:
a) write a temporary program for evaluating the expression and writing
   the result to another temporary file
b) run the temporary program (and delete the program file)
c) read the content of the temporary result file, and convert it to
   a number (and delete the file)
d) return this number (do *not* return the return value of
   system_exec()!)

Now you have important hints. I'll not give you the complete program at
the moment. Trying yourself is more fun! smile

Regards,
   Juergen

-- 
Have you read a good program lately?

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu