如何用JavaScript / jQuery解释textarea中的字符串? [英] How can I interpret strings in textarea with JavaScript/jQuery?

查看:63
本文介绍了如何用JavaScript / jQuery解释textarea中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript / jQuery创建一个小型解释程序。所以我想要的是当用户在textarea中输入一些文本时,程序应该解释该文本并在另一个文本区域中打印输出。
直到现在我已经实现了这个目标:

I am trying to make a small interpreting program with JavaScript/jQuery. So what I want is that when the user enter some text in the textarea the program should interpret that text and print the output in another text area. Till now I have achieved this:

https: //jsfiddle.net/7462hbv1/

有了这个,我能够捕获用户在文本区域中输入的每个字符串。但现在我想要用户例如输入:

With this I am able to catch each of the string that the user inputs in the text area. But now I want that when the user for example enter:

number a =1
number b=2
number sum=0
sum =a +b
print sum

程序应该解释这个,在这种情况下输出应该是 3
任何人都可以告诉我我该怎么办?我正在考虑构建一个二维数组并保存每一行(每行有类型,名称,值),然后使用此数组进行计算。
我将不胜感激任何帮助。提前谢谢

the program should interpret this and the output should be 3 in this case. Can anyone give me any idea how can I do this? I am thinking of building a two dimensional array and save there each row (for each row to have type, name, value) and then make calculation with this array. I would appreciate any help. Thank you in advance

更新

我使用了我的示例,特别是印刷声明。我已经打印了多个字符串或变量(用+连接)并在缺少+时打印错误消息。
我现在有两个问题:

I have worked with my example and specifically with print statement. I have made it to print multiple strings or varibles(connected with +) and to print error message if + is missing. I have two problems now:


  1. 我想在尝试打印未定义的变量时出现错误消息输出undefined就像在这种情况下(我希望在 #errors textarea中有这个消息):

  1. I want to have a error message when try to print undefined variable and not output undefined like in this case( I want to have that messsage in the #errorstextarea):

a = 240
b = 120
打印+ c
输出 240 undefined

我希望将字符 \iri 改为'\ n',以便print语句转到新的线。我用
var result2 = result1.replace('\'','\ n'); 完成了这项工作,但它不起作用。

I want to have the character \iri instead '\n' for the print statement to go to new line. I have done this with var result2= result1.replace('\iri','\n'); but it does not function.

这是我的演示( DEMO

你能帮我吗?

更新

我解决了第二个问题。这是 DEMO 。你能帮我解决第一个问题吗?

I solve the second problem. Here is the DEMO. Can you please help me with the first one?

推荐答案

这不是编写器/解释器的编写方式,但它应该用于一种简单的语言:

This is not how compilers / interpreters are written, but it should do for an easy language:


  1. 为每个语句定义正则表达式(例如,如果您只允许打印变量 / ^ print([az] +)$ / )。

将每一行与表达式匹配并决定做什么(例如,如果您翻译为javascript,您的print语句可能会变成 $('#output')。append(variablename +'< br>'); 和数学公式不会需要翻译,只需要经过验证)。

Match each line against the expressions and decide what to do (e.g. if you translate to javascript your print statement could become $('#output').append(variablename + '<br>'); and mathematical formulas don't need to be translated at all, just validated).

如果一切正常,请执行脚本。可能的问题:覆盖系统变量或名为关键字的变量(→在生成的代码中为变量添加前缀或将它们全部存储在同一个数组/对象中),脚本注入(→ escape '在你的字符串中( \')并替换< > & lt; & gt; 以及可能的其他限制。)

If everything is correct execute script. Possible problems: overwriting system variables or variables named like keywords (→ prefix your variables in the generated code or store them all in the same array / object), script injection (→ escape ' in your strings (\') and replace < and > with &lt; and &gt; and possibly other restrictions).

这是计算最大公约数的一个非常简单的例子:

Here is a very simple example calculating the greatest common divisor:

https://jsfiddle.net/7462hbv1/6/

一些评论:


  1. 这是一种非常愚蠢的语言,但我认为这是图灵完成的。

  2. 应该进行各种检查(例如,只检查各行,如果 正确关闭则不会检查)和有意义的错误消息。

  3. 仅支持一种数据类型:整数

  4. 变量名字可以有小写和大写字母但没有别的

  5. 语法(不完整):

  1. It's a pretty stupid language but I think it is Turing complete.
  2. There should be various checks (e.g. only the individual lines are checked, not if an if is correctly closed) and meaningful error messages.
  3. Only one datatype is supported: integer
  4. Variable names can have lower and upper case letters but nothing else
  5. grammar (incomplete):

<int> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<integer> ::= <int>[<int>*]
<assignment> ::= <variable> = <formula>
<value> ::= <integer> | <variable>
<formula> ::= [<formulaPart>*] <value>
<operator> ::= + | - | * | / | %
<formulaPart> ::= <value> <operator>
<while> ::= while <variable>
<endWhile> ::= end while
<comparator> ::= > | < | =
<if> ::= if <variable> <comparator> <value>
<else> ::= else
<endIf> ::= end if
<print> ::= print "<message>" [<variable>]

请注意,没有括号,您必须拆分代码行直到你的数学不再需要括号。

Note that there are no brackets, you have to split the lines of code until your math needs no more brackets.

以下是这种语言的(工作!)GCD程序的样子:

Here is how the (working!) GCD program looks like in this language:

print "Euclidean algorithm"
a = 240
print "a = " a
b = 360
print "b = " b
if a > 0
  while b
    if a > b
      a = a - b
    else
      b = b - a
    end if
  end while
  print "gcd: " a
else
  print "gcd: " b
end if


  • 这是实际执行的代码:

  • And this is the code actually executed:

    myProgram=function(){var variables=[];var pOut='';pOut+='Euclidean algorithm\n';variables['a']=240;pOut+='a = '+variables['a']+'\n';variables['b']=360;pOut+='b = '+variables['b']+'\n';if(variables['a']>0){while(variables['b']){if(variables['a']>variables['b']){variables['a']=variables['a']-variables['b'];}else{variables['b']=variables['b']-variables['a'];}}pOut+='gcd: '+variables['a']+'\n';}else{pOut+='gcd: '+variables['b']+'\n';}$('#output').html(pOut);};myProgram();
    


  • 这篇关于如何用JavaScript / jQuery解释textarea中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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