开发者

String Searching

开发者 https://www.devze.com 2023-04-12 01:10 出处:网络
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 w

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 th开发者_运维百科e function from "function void start" and "endfunction"?


There is a lot more to writing a compiler than simple string manipulation - for example what if your source instead looked like this:

module myApplication
  string myString = "[EntryPoint]
  function void start
    write("Hello World!\n")
    wait(5) // wait 5 seconds
    ret()
  endfunction"
endModule

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消