I am trying to make a small interpreter for print statement. Here is a demo of what I have done till now: DEMO
我正在尝试为print语句创建一个小解释器。这是我迄今为止所做的一个演示:DEMO
What I want to reach now is to print multiple string connected with +
like:
我现在想要达到的目的是打印多个与+连接的字符串:
- print "My book" + "is" + "xx"
- print a (where a is varible name)
- print a + "is my number"
打印“我的书”+“是”+“xx”
打印a(其中a是可变名称)
打印+“是我的号码”
I have tried something till now and build something but I have two problems:
我到现在为止尝试了一些东西,但我有两个问题:
-
First I tried to build a new print regexp:
首先,我尝试构建一个新的打印正则表达式:
/^ *print +(?:"([^"]*)"|([a-zA-Z]\w*)) *(.*)$/
/ ^ * print +(?:“([^”] *)“|([a-zA-Z] \ w *))*(。*)$ /
so this will match the print statement, the first argument could be a string("") or a variable name. And then with (.*)
I am trying to catch all other variables or strings and to use them later. ( I have in mind to build a while loop). But my problem is that when I make the match it outputs me an empty value. So when I make alert(t)
the t[2]
value is empty(,,).
所以这将匹配print语句,第一个参数可以是字符串(“”)或变量名称。然后使用(。*)我试图捕获所有其他变量或字符串,并在以后使用它们。 (我想要构建一个while循环)。但我的问题是,当我进行比赛时,它会输出一个空值。因此,当我发出警报(t)时,t [2]值为空(,,)。
- The second problem is that I am not able to test if the first parameter of print is a varible or a string. I tried this:
t1 = t[1].match(rxString);
but it output null ( because in fact it search for""
, but when saved in(t)
they are removed from the string)
第二个问题是我无法测试print的第一个参数是变量还是字符串。我试过这个:t1 = t [1] .match(rxString);但它输出null(因为实际上它搜索“”,但是当保存在(t)中时它们会从字符串中删除)
Here is my DEMO
这是我的演讲
Please can you help me how to solve this this? Thanks in advance
请问你能帮我解决这个问题吗?提前致谢
1 个解决方案
#1
Building interpreters with only regexes is between difficult and impossible. You might rather tokenize your input (e.g. split at white spaces + before/after some selected special characters) and then detect and handle each token individually.
仅使用正则表达式构建解释器是困难的,也是不可能的。您可能更愿意对您的输入进行标记(例如,在某些选定的特殊字符之前/之后在空格处分割),然后分别检测和处理每个标记。
If you want to go more complex you should of course define a grammar for your language and match that using a library like PEG.js as @peter-schneider pointed out.
如果你想变得更复杂,你当然应该为你的语言定义一个语法,并使用像@ peter-schneider指出的像PEG.js这样的库来匹配。
You should be able to detect string literals quite easily by whether the token starts with a "
or '
:
您应该能够通过令牌以“或”开头很容易地检测字符串文字:
if (token[0] == '"' || token[1] == '\'') { /* ... */ }
#1
Building interpreters with only regexes is between difficult and impossible. You might rather tokenize your input (e.g. split at white spaces + before/after some selected special characters) and then detect and handle each token individually.
仅使用正则表达式构建解释器是困难的,也是不可能的。您可能更愿意对您的输入进行标记(例如,在某些选定的特殊字符之前/之后在空格处分割),然后分别检测和处理每个标记。
If you want to go more complex you should of course define a grammar for your language and match that using a library like PEG.js as @peter-schneider pointed out.
如果你想变得更复杂,你当然应该为你的语言定义一个语法,并使用像@ peter-schneider指出的像PEG.js这样的库来匹配。
You should be able to detect string literals quite easily by whether the token starts with a "
or '
:
您应该能够通过令牌以“或”开头很容易地检测字符串文字:
if (token[0] == '"' || token[1] == '\'') { /* ... */ }