Eval

Monkey Forums/Monkey Programming/Eval

frank(Posted 2011) [#1]
Is there something like eval? I understand this is kind of hard to do, considering it generates code.

However; consider using the monkey parser (which is written in monkey; see modules/trans/parser.monkey) to write a monkey interpreter, which would just call all commands. After compilation to the target, this all would be compiled too, so you could evaluateMonkey() in that app on JS / CPP etc and it should work or am I wrong?


Hima(Posted 2011) [#2]
If I understand your post correctly, then I think that wouldn't be an eval command. Eval should execute the code at run-time and not compile-time.


ziggy(Posted 2011) [#3]
I would kill for interpreted Monkey...


Samah(Posted 2011) [#4]
The aggressive code stripper might not provide the code you're trying to use, so no.


Tibit(Posted 2011) [#5]
Sorry if it's a stupid question but what is an eval and what is the practical use of it? Sounds interesting :)


Hima(Posted 2011) [#6]
An eval command allows you to pass a string into it and it will execute that code at runtime. Usually this is available only for interpreted language like JavaScript or Ruby.

This can be used to implement 'code as resources' or allow you to make changes to your application without having to recompile the whole thing.


Tibit(Posted 2011) [#7]
So it a way to implement scripts? I can update code in a external text-file or at my server, then do a update from the running game and run that new piece of code?


frank(Posted 2011) [#8]
@Hima I don't think you understand me correctly; my point is that an interpreter written in Monkey, compiled to a target, would run interpreted Monkey on all target platforms, hence it would be possible to do this in a cross platform manner. If that makes sense :)

I am indeed looking for a Monkey REPL / scripting environment, which, most definitely, would speed up development A LOT for a lot of us.


AdamRedwoods(Posted 2011) [#9]
I thought about something like this. monkey -> monkey bytecode target-> monkey bytecode interpreter


frank(Posted 2011) [#10]
But then you would have to compile again (namely to bytecode). I think the advantage of an interpreter is some fast prototyping. When you are done, you put it in your source code and compile it for max performance. What would be the purpose of the bytecode (maybe i'm missing it).


Hima(Posted 2011) [#11]
@Tibit
Yes, that would be possible. :D

@frank
Ah, I understand what you mean now. I figure it'd be really difficult though, but it sounds possible and make sense that writing an interpreter in Monkey would make it work in all platform.


frank(Posted 2011) [#12]
Hmm.

In the sourcecode of Parser there actually is something called Eval;

Running this ;

Print Eval("2+2", Type.intType);

Works, but it can only do very primitive expressions and if you don't comment out ExitApp in Err it doesn't compile (I have no clue why).

It's a start though; kind of shows how to use the parser. Going to play with that some more :)


frank(Posted 2011) [#13]
It seems the parser would need some serious overhaul to use it ; it's really focused at the moment to work with files and to not really be used for something something like this.

I don't know if Mark thought about this (I bet he has); it would not be *too* much work to refactor the parser so it would be able to parse any Monkey string within the current or another specified environment. And after that it wouldn't be too hard to use that to eval the result. At least it doesn't *look* too difficult to me :) If Mark is not doing/planning it, I think i'll fork the parser and try to make it work. That would not be the preferred way of working as it means copying code at this point.

Edit: any way to get Mark his attention to give a short comment on this?


Samah(Posted 2011) [#14]
I don't know if Mark thought about this (I bet he has); it would not be *too* much work to refactor the parser so it would be able to parse any Monkey string within the current or another specified environment. And after that it wouldn't be too hard to use that to eval the result. At least it doesn't *look* too difficult to me :)

It's a HUGE undertaking.

Class Foo
  Method Bar()
  End
End

Function Main()
  Eval("New Foo.Bar()") ' doesn't work, because monkey never translates the Bar method
End

It would require the entire file to be parsed and translated, including dead code. Imagine if you imported an entire third party framework. Would you want it to include all the parts you don't use? This is the primary reason that Monkey doesn't have reflection.


frank(Posted 2011) [#15]
It's a shame if it's not possible though; it's very productive having such a language feature.