提问人:Cocosbeans 提问时间:11/16/2023 更新时间:11/16/2023 访问量:25
在 Nearley 中创建 if 语句或一般逻辑的最佳方法是什么?
What is the most optimal way to create an if statement, or logic in general, in Nearley?
问:
使用 NodeJS 的 Nearley 模块,我想创建一个如下所示的 if 语句:
if condition: /* code */ endif;
所以在Nearley,我写道:
# grammar.ne
@builtin "whitespace.ne"
# Condition -> etc etc
IfStatement -> "if" __ Condition __ ":" (_ Logic:*):? __ "endif;"
我不知道该如何定义逻辑。我希望它用于描述变量声明的任何实例、if 语句(所以可能使用递归,但我不知道如何)或调用函数。
请注意,我的项目主要不是脚本语言。我正在向 Infocom 的 ZIL 风格漂移。上面提到的逻辑只能在函数中使用,所以它看起来像这样:
(FunctionName OptionalParameter1 OptionalParameter2)
<Logic>
[Conclude]
/* Example:
* (AddDigits x y z)
* <set a = x + y + z;
* return a;>
* [Conclude]
*
* Functions begin with a header including its Name and Parameters (if any);
* They include any code encased with <>;
* They end with the [Conclude] tag.
*/
ConcludeTag -> "[Conclude]"
FunctionDeclare -> FunctionHeader "\n" FunctionBody ConcludeTag
FunctionHeader -> "(" [a-zA-Z0-9_]:* ((__ [a-zA-Z0-9_]:*):*):? ")"
FunctionBody -> # Logic needs to be defined first!!
我最初的想法是使用递归。但是,我不确定这是否是一种好的方法,甚至是一种有效的方法。我写了这个:
# Define integers, strings and booleans as valid Value types
Value -> [0-9]:*
| "\"" . "\""
| ("true" | "false")
# Define an extended Value type as anything listed above PLUS function calling e.g. myfunc()
ExtendedValue -> Value
| [a-zA-Z0-9_]:* _ "(" (_ (Value | (Value "," _):* Value) _):? ")"
# I stopped here because I was stuck. There might be more to add but I do not know yet
Logic -> IfStatement
| [a-zA-Z0-9_]:* _ "=" _ ExtendedValue _ ";"
答: 暂无答案
评论