Sublime Forum

Evaluate

#1

So somebody asked about an Evaluate plugin, such as that in Textmate. I decided to take it on, and actually got around to looking into how wbond wrote the Prefixr plugin (awesome by the way). Iā€™ve been meaning to check it out for some time now, and this has given me a reason.

I wrote it because I was intrigued by Python and Sublimes API. I stripped out a few things because I couldnā€™t find a need for them. Itā€™s all threaded for multiple selections.

Yeah, anyway, I give all credit to wbond for this plugin, even though he doesnā€™t know about it!

Check it out.

0 Likes

Evaluate Selection?
#2

And yes, itā€™s alpha. I know it doesnā€™t work with spaces or with a ā€˜/ā€™ symbol. But I donā€™t know why this is. So if somebody could explain?

Edit: Actually, it seems temperamental to what works and what doesnā€™tā€¦

0 Likes

#3

So Iā€™ve been messing with this for the past few minutes and figured the following with the current implementation.

For some reason, you can not do the following:

x=10
x*100

Yet, if you try to evaluate x on its own, youā€™ll get an undefined error.

You can do the following, because itā€™s funā€¦

__import__('os').getcwd()

Which for me, evaluates to /

Or you can do some more advanced math functionsā€¦

__import__('math').log(10)

Which is interesting.

So yeah. Variables donā€™t seem to work. Imports do. Anyone think of what Iā€™ve missed, how to improve?

Iā€™m thinking of implementing a simple top-down parser, with a few inbuilt functions etc etc.

0 Likes

#4

Oh, you can also concatenate strings!

'hello' + 'world'

Evaluates to helloworld :smile:

I donā€™t know why eval rounds, because itā€™d be nice to have 19/4 (example from Ideas and Feature Requests topic)

0 Likes

#5

[quote=ā€œjbrooksukā€]
I donā€™t know why eval rounds, because itā€™d be nice to have 19/4 (example from Ideas and Feature Requests topic)[/quote]

Same as console, by default Python return integer for integer/integer division.
Try

19/4.

You probably better go:

tmp_global = {} code = compile('10/4.', '<string>', 'eval') out = eval(code, tmp_global)
Or more versatile:

tmp_global = {} code = compile('x = 10\ny = 4.\nout = x/y\n', '<string>', 'exec') eval(code, tmp_global) out = tmp_global'out']
Check Python documentation for compile and eval/exec methods.

0 Likes

#6

Apparently,

from http://www.python.org/dev/peps/pep-0238/.

Also from there:

0 Likes

#7

Iā€™ve fixed decimal division bug.

For anyone interestedā€¦

from __future__ import division
0 Likes

#8

Just pushed a few additions. Evaluate now appears under the Edit menu and in the Command Palette :smile:

0 Likes