如何在运行时将字符串转换为代码 [英] How can I convert string to code at run time

查看:69
本文介绍了如何在运行时将字符串转换为代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成包含 if else 表达式的字符串.我想知道我是否可以在运行时执行此表达式?这是一个示例:

I am generating strings containing if else expressions. I was wondering if I can execute this expressions at run time? Here's an example:

string s = "if(x > 10) {Fly = true;} else {Fly = False;}";
Execute (s); 

是否有可能做到这一点?

Is it even possible to do this?

推荐答案

可以使用TCC( http://bellard.org/tcc/).它允许在运行时本地编译和运行代码.另一种方法是使用解释器,那里有很多(LUA,Python等,请参见列表 wiki ).

It is possible to use TCC ( http://bellard.org/tcc/ ). It allows to compile and run code natively at runtime. Another approach is to use an interpreter, there are plenty out there (LUA, Python etc, see list wiki).

这篇关于如何在运行时将字符串转换为代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆