开发者

Writing a simple game interpreter

开发者 https://www.devze.com 2023-02-06 11:26 出处:网络
I have read many entries about interpreter and compiler construction on this site and on hacker news.

I have read many entries about interpreter and compiler construction on this site and on hacker news.

All the topics talk about big interpreter language , I wish to write a oversimplified languages.

Language

  1. The language is based on Herbert [ Imagine Cup ]
  2. I wish to create a online version , user can submit in form and see it robot running in the web window.
  3. I can only use php , python , ruby or any web language.

Here is my language

Basic Instruction

  1. s - straight
  2. l - left
  3. r - right
  4. You can have function here f : ssrl [ They support recursion f: ssrlf arguments f(A) : Assrl , f(A) : ssrlf(A-1) ( A is integer here)
  5. You can have multiple functions in the program calling each other. f:ssrlgf

I want to implement this as quickly as possible , what will be the bes开发者_运维知识库t alternative for me to get this job done . [ I know php , don't have experience in python or ruby but willing to learn them for this project ]


You will have to write a tokenizer-(chain).

class Token { static Token Parse(string program); void Execute(CurrentProgramState state); }

First have a Program token that tries to parse the entire text, by passing it by multiple smaller tokens that create tokens from it. Each successfull token passed consumes a little bit of the string, so in the end the string is empty. (And in the end a Program Token has an ordered list of tokens).

Then when Program.Execute() is called it keeps track of the CurrentProgramState and passes that state in order to its own tokens which modify the game state according to its parameters.

A small example.

Say we have a language that has only two types of tokens

s (for straight) number (for how far)

class StraightToken
{
    public StraightToken(NumberToken howFar)
    {
        this.howFar = howFar;
    }
    private NumberToken howFar;
    static Token Parse(string program)
    {
        if(program.StartsWith("s ")
        {
            NumberToken number = NumberToken.Parse(program.substring(2));
            if(howFar != null)
            {                
                return new StraightToken(number);
            }
        }
        return null;
    }

    public void Execute(ProgramState state)
    {
        state.Position += this.howFar.value;
    }
}

class NumberToken
{
    public int value;
    public NumberToken(int value;)
    {
        this.value = value;
    }

    static Token Parse(string program)
    {
        if(IsDigit(program[0]))
        {
            program = program.SubString(1);
            return new NumberToken((int)program[0])
        }
    }
}


Sorry for posting on an old thread but, it's an interesting question and as it isn't solved, I personally would use javascript, and would't bother about writing an interpreter

So assuming the input is all one line, you'd have this: [warning: may contain typos, I'm posting from my phone]

var input = "lllssrb",
      horizontal = 0, vertical = 0;
//now we'll just loop through the input
input.split("").forEach(function(char){
    switch(char){
         case "l":
              horizontal --;
              break;
         case "r"
              horizontal ++;
              break;
         case "s":
              vertical ++;
              break;
          case "b":
              vertical --;
              break;
    }
});

//now do something with our results
//function advance(horizontal, vertical)
advance(horizontal, vertical);
0

精彩评论

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