Re: eval() function
- Posted by Alex Chamberlain <alex.chamberlain at tiscali.co.uk> Nov 06, 2005
- 570 views
Ok here's my reply... --eval.e
include misc.e include file.e global constant EVAL_RAND_NUM = sprint(rand(999999)), EVAL_PROG_NAME = "eval_prg" & EVAL_RAND_NUM & ".tmp", EVAL_RES_NAME = "eval_res" & EVAL_RAND_NUM & ".tmp", EVAL_FLAG_NAME = "eval_flag" & EVAL_RAND_NUM & ".tmp", -- EVAL_BATCH_NAME = "eval_batch" & EVAL_RAND_NUM & ".bat", EVAL_ERR_RET = -1, EVAL_WRITE_ERR = 1, EVAL_READ_ERR = 2, EVAL_BATCH_ERR = 3 global integer EVAL_ERROR EVAL_ERROR = 0 global function eval (sequence script) sequence plat, ret, del integer tmp_fn object line, eu -- write a temporary program for evaluating the expression and -- writing the result to another temporary file tmp_fn = open(EVAL_PROG_NAME, "w") if tmp_fn = -1 then EVAL_ERROR = EVAL_WRITE_ERR return EVAL_ERR_RET end if -- puts(tmp_fn, "include get.e\n") puts(tmp_fn, script) puts(tmp_fn, "\n\ninteger eval_flag_fn\n" & "eval_flag_fn = open(\"" & EVAL_FLAG_NAME & "\", \"w\")\n" & "print(eval_flag_fn, 123)\n" & "close(eval_flag_fn)\n" -- & "abort(wait_key()*0)" ) close(tmp_fn) -- run the temporary program if platform() = DOS32 then plat = "ex" del = "DEL" elsif platform() = WIN32 then plat = "exwc" del = "DEL" elsif (platform() = LINUX) or (platform() = FREEBSD) then plat = "exu" del = "rm" end if eu = getenv("EUDIR") if equal(plat, "exu") and atom(eu) then system("./" & plat & " " & EVAL_PROG_NAME & " > " & EVAL_RES_NAME, 2) -- elsif equal(plat, "exw") then -- tmp_fn = open(EVAL_BATCH_NAME, "w") -- if tmp_fn = -1 then -- EVAL_ERROR = EVAL_BATCH_ERR -- return EVAL_ERR_RET -- end if -- -- puts(tmp_fn, "@echo off\n") -- puts(tmp_fn, "exw " & EVAL_PROG_NAME & " > " & EVAL_RES_NAME) -- -- close(tmp_fn) -- -- system(EVAL_BATCH_NAME, 2) else system(plat & " " & EVAL_PROG_NAME & " > " & EVAL_RES_NAME, 2) end if while atom(dir(EVAL_FLAG_NAME)) do end while -- read the content of the temporary result file, -- and convert it to a number tmp_fn = open(EVAL_RES_NAME, "r") if tmp_fn = -1 then EVAL_ERROR = EVAL_READ_ERR return EVAL_ERR_RET end if ret = {} while 1 do line = gets(tmp_fn) if atom(line) then exit end if ret &= line end while close(tmp_fn) system(del & " *" & EVAL_RAND_NUM & ".tmp", 2) -- if equal(plat, "exw") then -- system("DEL " & EVAL_BATCH_NAME, 2) -- end if return ret end function
The demo remains unchange, but what has changed about eval.e? Well now the eval.e can be used by multiple scripts, or the same script running multiple times, at the same time as a random number is added to the file name. On Windows XP, redirection was not working. exw is not a console program, so I replaced the command to exwc (I even tried a batch file - code included)! And it works on Linux if exu is installed in the same directory instead of EUDIR. This idea of working together on the same script, even though its only two or three of us, is what I want to get over with my new website at euphoria.yourpixels.co.uk (I own yourpixels.co.uk). Please visit but do understand it is far off completion! Thanks, Alex