eval

Yifei Kong

可紊 提交于 2019-12-01 16:51:29
Dec 10, 2017 阅读 norvig 文章 (How to Write a (Lisp) Interpreter (in Python)) 的笔记。 作为从数学系转过来的学生,之前并没有学过编译原理,只是自己在一些文章中读过关于编译器的只言片语,借这篇文章了解一些编辑器的基本知识吧。 这篇文章主要是用 Python 实现了一个 lisp 的解释器。lisp 语言的语法非常简单,可以说lisp语言本身就是 AST 。一个解释器基本有两个部分。一部分是 parser,生成AST,另一部分是执行,运行AST。 code --> (parse) --> AST --> (eval) --> result 也就是我们只要去实现 parse 和 eval 两个函数就好了~ 这里使用了几个类型,都是直接衍生自Python 的原生类型 Symbol = str # 变量 Number = (int, float) Atom = (Symbol, Number) List = list Exp = (Atom, List) Env = dict parse parse 传统意义上应该分为两部分,一部分是词法分析(Lexical Analysis),也就是 tokenize,把代码转换成一系列的 token。另一部分是语法分析,也就是合成 AST。常用的工具有 lex,ply 等 在

Use of exec and eval in Python

北战南征 提交于 2019-12-01 16:14:32
So I have understood what exec and eval and also compile do. But why would I need to use them? I am being unclear on the usage scenario. Can anyone give me some examples so that I can better appreciate the concept. Cause right I know it is all theory. I'll give an example in which I have used eval and where I think it was the best choice. I was writing a simple software testing utility ... something to test whether student exercises were conforming to the assignment requirements. The goal was to provide a way for a simple configuration file to serve as a test specification (to get around a

Use of exec and eval in Python

孤街醉人 提交于 2019-12-01 15:57:38
问题 So I have understood what exec and eval and also compile do. But why would I need to use them? I am being unclear on the usage scenario. Can anyone give me some examples so that I can better appreciate the concept. Cause right I know it is all theory. 回答1: I'll give an example in which I have used eval and where I think it was the best choice. I was writing a simple software testing utility ... something to test whether student exercises were conforming to the assignment requirements. The

Why isn't there an `unquote` Lisp primitive?

最后都变了- 提交于 2019-12-01 15:53:34
Lately, I've been thinking a lot about the basis of Lisp; I've read several manuals and/or other materials on the Internet, including The Roots of Lisp by P. ‎Graham: In The Roots of Lisp , quote is described as a primitive that changes code into data, thereby quoting it, but there doesn't seem to be an equivalent inverse primitive, that is an unquote primitive. I thought it might have been eval 's business, but eval often runs the data in a null lexical environment, which is not equivalent to changing data back into code. Ergo, why isn't there an unquote Lisp primitive? unquote is only useful

How override eval function in javascript?

两盒软妹~` 提交于 2019-12-01 15:50:50
For example: (function() { var proxied = window.eval; window.eval = function() { return proxied.apply(this, arguments); }; })(); But this code is not working. T.J. Crowder You can't. (There is a limited way of doing it, but it's quite limited and doesn't maintain the magic that bobince talks about .) eval isn't a real JavaScript function in at least one major implementation (IE's JScript, at least not through IE7; haven't tested the new IE8 version), so right off the bat you're going to run into trouble, because you won't be able to call the original via apply (not that that really matters for

Why isn't there an `unquote` Lisp primitive?

强颜欢笑 提交于 2019-12-01 15:33:05
问题 Lately, I've been thinking a lot about the basis of Lisp; I've read several manuals and/or other materials on the Internet, including The Roots of Lisp by P. ‎Graham: In The Roots of Lisp , quote is described as a primitive that changes code into data, thereby quoting it, but there doesn't seem to be an equivalent inverse primitive, that is an unquote primitive. I thought it might have been eval 's business, but eval often runs the data in a null lexical environment, which is not equivalent

How to limit text string in Eval

不羁的心 提交于 2019-12-01 15:22:14
问题 I have a hyperlink with the navigate property set like this: NavigateUrl='<%# Eval("My Text") %>' How can I limit the string to 140 characters ? I have tried this Eval("My Text").ToString().Substring(0,140) but if the string length is less than 140 characters it throws an exception. 回答1: And yet an other possibility: Eval("My Text").ToString().PadRight(140).Substring(0,140).TrimEnd() Edit: I do like LINQ, too: Eval("My Text").ToString().Take(140).Aggregate("", (x,y) => x + y) 回答2: Use It (: <

What situations demand the use of eval() because there are no alternatives?

北城以北 提交于 2019-12-01 15:01:50
问题 I know eval should be avoided in JavaScript for speed and security reasons. But in the case of PHP, rarely is security ever mentioned. More often, it's your program running slower than it should because of a haphazard use of eval . In what specific situations should you use eval because there is no other way around it? For clarity: We're not talking about user-supplied data. So the question is focused on pure and fully-controlled server-side valid use of eval . 回答1: The security problems of

python高精度

亡梦爱人 提交于 2019-12-01 14:58:53
python高精度 1!+2!+3!...+n! 传送门 n=eval(input())#把字符串转化为数字 ans=0 for i in range(1,n+1): d=1 for j in range(1,i+1): d*=j ans+=d print(ans) a*b 传送门 a=eval(input()) b=eval(input()) print(a*b) a+b 传送门 a=eval(input()) b=eval(input()) print(a+b) a-b 传送门 a=eval(input()) b=eval(input()) print(a-b) 来源: https://www.cnblogs.com/Emcikem/p/11689793.html

How override eval function in javascript?

拈花ヽ惹草 提交于 2019-12-01 14:51:03
问题 For example: (function() { var proxied = window.eval; window.eval = function() { return proxied.apply(this, arguments); }; })(); But this code is not working. 回答1: You can't. (There is a limited way of doing it, but it's quite limited and doesn't maintain the magic that bobince talks about.) eval isn't a real JavaScript function in at least one major implementation (IE's JScript, at least not through IE7; haven't tested the new IE8 version), so right off the bat you're going to run into