编写一个简单的游戏解释器
我在这个网站和黑客新闻上阅读了许多有关解释器和编译器构建的条目。
所有的主题都在谈论大解释器语言,我希望编写一种过于简化的语言。
语言
- 该语言基于 Herbert [ Imagine Cup ]
- 我希望创建一个在线版本,用户可以以表单形式提交并看到它的机器人在网络窗口中运行。
- 我只能使用 php 、 python 、 ruby 或任何网络语言。
这是我的语言
基本指令
- s - 直
- l - 左
- r - 右
- 您可以在这里使用功能 f : ssrl [ 它们支持递归 f: ssrlf 参数 f(A) : Assrl , f(A) : ssrlf(A-1) (这里 A 是整数)
- 您可以在程序中让多个函数互相调用。 f:ssrlgf
我想尽快实施这一点,什么是完成这项工作的最佳选择。 [我了解 php,没有 python 或 ruby 经验,但愿意为这个项目学习它们]
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
- The language is based on Herbert [ Imagine Cup ]
- I wish to create a online version , user can submit in form and see it robot running in the web window.
- I can only use php , python , ruby or any web language.
Here is my language
Basic Instruction
- s - straight
- l - left
- r - right
- 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) - 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 best 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 ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须编写一个标记器(链)。
类令牌{
静态令牌解析(字符串程序);
无效执行(当前程序状态状态);
首先
有一个程序令牌,它尝试通过多个较小的令牌传递它来解析整个文本,这些较小的令牌从中创建令牌。每个成功传递的令牌都会消耗字符串的一点点,因此最终字符串为空。 (最后,程序令牌有一个有序的令牌列表)。
然后,当调用 Program.Execute() 时,它会跟踪 CurrentProgramState 并将该状态传递给自己的令牌,令牌根据其参数修改游戏状态。
一个小例子。
假设我们有一种语言,只有两种类型的标记
s (对于直接)
数量(多远)
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)
很抱歉在旧线程上发帖,但是,
这是一个有趣的问题,因为它没有解决,我个人会使用 javascript,并且不会费心编写解释器
所以假设输入都是一行,你会得到这个:[警告:可能包含拼写错误,我正在通过我的手机发帖]
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]