字符串搜索
好的,我正在用 C# 编写一个语言编译器,它将我的语言转换为 MSIL,然后调用 MSIL 编译器。但这不是重点。我创建了一些代码来向您展示我将要使用的内容:
module myApplication
[EntryPoint]
function void start
write("Hello World!\n")
wait(5) // wait 5 seconds
ret()
endfunction
endModule
好的,我正在尝试扫描该代码并找到“[EntryPoint]”所在的位置,然后找到其后的所有“function void start”。所以我可以找到函数类型(“void”)和名称(“start”)。 [EntryPoint] 标记将始终位于定义为应用程序入口点的函数之上。我必须让它修剪空白和其他东西。这也是使用 '\n' 进行扫描,因此该函数必须位于该标签下。
我还有另一个问题,如何将函数内的代码与“function void start”和“endfunction”分开?
Okay, I'm writing a language compiler in C# that converts my language into MSIL and then calls the MSIL compiler. But that's beside the point. I created some code to show you want I'm going to be working with:
module myApplication
[EntryPoint]
function void start
write("Hello World!\n")
wait(5) // wait 5 seconds
ret()
endfunction
endModule
Okay, I'm trying to scan through that code and find where "[EntryPoint]" is and then find all the "function void start" after it. So I can find the function type ("void") and name ("start"). The [EntryPoint] tag will always be above the function that will be defined as the applications entry point. And I will have to make it trim the whitespace and stuff. This is also scanning through using '\n' so the function MUST be under the tag.
And I also have another question, how would I make it separate the code inside the function from "function void start" and "endfunction"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编写编译器比简单的字符串操作要复杂得多 - 例如,如果您的源代码看起来像这样:
即您的源代码定义了一个包含程序源代码的字符串 - 您不能只是仔细寻找看起来像入口点的东西,您需要实际解析您的源代码。
请参阅学习编写编译器以获取有关入门的优质资源。
There is a lot more to writing a compiler than simple string manipulation - for example what if your source instead looked like this:
I.e. your source defines a string containing the source for a program - you can't just look through for something that looks like an entry point, you need to actually parse your source.
See Learning to write a compiler for good resources on getting started.