1

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"?

tr0yspradling
  • 529
  • 1
  • 9
  • 20
  • 1
    You shouldn't use string search here but a language parser – Daniel Hilgarth Oct 11 '11 at 14:27
  • 2
    It sounds like you really need to read up on how parsers are generally structured. There's a lot to take in about parsers, which won't really be applicable to answer on Stack Overflow. – Jon Skeet Oct 11 '11 at 14:28

1 Answers1

3

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.

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367