juggle添加c#版本
此前做过一个c++版的网络层dsl:http://www.cnblogs.com/qianqians/p/4255034.html
现在给这个dsl加入c#的支持,并且对代码的结构做了优化,将语法解析和代码生成做了解耦
语法解析部分
class func(object):
def __init__(self):
self.keyworld = ''
self.func = []
self.argvtuple = None def clear(self):
self.keyworld = ''
self.func = []
self.argvtuple = None def push(self, ch):
if ch == ' ' or ch == '\0':
self.keyworld = deleteNoneSpacelstrip(self.keyworld)
if self.keyworld != '':
if self.argvtuple is None:
self.func.append(deleteNoneSpacelstrip(self.keyworld))
else:
if self.keyworld in ['table', 'array', 'int', 'string', 'float', 'bool']:
self.argvtuple.append(deleteNoneSpacelstrip(self.keyworld))
self.keyworld = ''
return False if ch == ',':
if self.keyworld != '' and self.keyworld in ['table', 'array', 'int', 'string', 'float', 'bool']:
self.argvtuple.append(deleteNoneSpacelstrip(self.keyworld))
self.keyworld = '' return False if ch == '(':
self.keyworld = deleteNoneSpacelstrip(self.keyworld)
if self.keyworld != '':
self.func.append(deleteNoneSpacelstrip(self.keyworld))
self.argvtuple = []
self.keyworld = ''
return False if ch == ')':
if self.keyworld != '' and self.keyworld in ['table', 'array', 'int', 'string', 'float', 'bool']:
self.argvtuple.append(deleteNoneSpacelstrip(self.keyworld)) if self.argvtuple is None:
self.func.append([])
else:
self.func.append(self.argvtuple) self.keyworld = ''
return False if ch == ';':
return True self.keyworld += ch return False class module(object):
def __init__(self):
self.keyworld = ''
self.name = ''
self.module = []
self.machine = None def push(self, ch):
if ch == '}':
self.machine = None
return True if self.machine is not None:
if self.machine.push(ch):
self.module.append(self.machine.func)
self.machine.clear()
else:
if ch == '{':
self.name = deleteNoneSpacelstrip(self.keyworld)
self.keyworld = ''
self.machine = func()
return False self.keyworld += ch return False class statemachine(object):
def __init__(self):
self.keyworld = ''
self.module = {}
self.machine = None def push(self, ch):
if self.machine is not None:
if self.machine.push(ch):
if isinstance(self.machine, module):
self.module[self.machine.name] = self.machine.module
self.machine = None
else:
if ch == ' ' or ch == '\0':
if deleteNoneSpacelstrip(self.keyworld) == 'module':
self.machine = module()
self.keyworld = ''
else:
self.keyworld += ch def getmodule(self):
return self.module def syntaxanalysis(self, genfilestr):
for str in genfilestr:
for ch in str:
self.push(ch)
解析采用状态机机制,逐字符读取代码在读取到关键字符则跳转状态,并且保持读取到的关键字。
读取的关键字采用table方式保持
module:[funcinfo, ...]
在代码生成部分,按解析获取的语法table生成module和caller代码,分别是事件的响应和调用端。
def gencaller(module_name, funcs):
code = "/*this caller file is codegen by juggle*/\n"
code += "using System;\n"
code += "using System.Collections;\n"
code += "using System.IO;\n"
code += "using MsgPack;\n"
code += "using MsgPack.Serialization;\n\n" code += "namespace caller\n"
code += "{\n"
code += " public class " + module_name + " : Icaller \n"
code += " {\n"
code += " public " + module_name + "(Ichannel _ch) : base(_ch)\n"
code += " {\n"
code += " module_name = \"" + module_name + "\";\n"
code += " }\n\n" for i in funcs:
code += " public void " + i[1] + "("
count = 0
for item in i[2]:
code += tools.gentypetocsharp(item) + " argv" + str(count)
count = count + 1
if count < len(i[2]):
code += ","
code += ")\n"
code += " {\n"
code += " ArrayList _argv = new ArrayList();\n"
for n in range(len(i[2])):
code += " _argv.Add(argv" + str(n) + ");\n"
code += " call_module_method(\"" + i[1] + "\", _argv);\n"
code += " }\n\n" code += " }\n"
code += "}\n" return code
def genmodule(module_name, funcs):
code = "/*this module file is codegen by juggle*/\n"
code += "using System;\n"
code += "using System.Collections;\n\n" code += "namespace module\n{\n"
code += " public class " + module_name + " : Imodule \n {\n" code += " public " + module_name + "()\n {\n"
code += " module_name = \"" + module_name + "\";\n"
code += " }\n\n" for i in funcs:
code += " public abstract void " + i[1] + "("
count = 0
for item in i[2]:
code += tools.gentypetocsharp(item) + " argv" + str(count)
count = count + 1
code += ");\n\n" code += " }\n"
code += "}\n" return code
和juggle的上个版本不同,为了简洁的用于开发游戏服务器,这个版本删除了同步调用功能,只保留异步调用,对同步调用感兴趣的 可以阅读darckforce里面的代码:https://github.com/qianqians/darkforce/tree/master/juggle
这个版本的代码地址在:https://github.com/qianqians/abelkhan
我将基于这个版本的juggle开发一个开源的游戏服务器框架,欢迎大家在论坛或博客给我留言提出意见
juggle添加c#版本的更多相关文章
- svn add 添加到版本库
转 svn add-添加到版本库 常用操作1.添加一个文件到工作拷贝:$ svn add foo.c 2.当添加一个目录,svn add缺省的行为方式是递归的:$ svn add testdir 3. ...
- eclipse添加高版本tomcat问题
eclipse添加高版本tomcat会报错,提示无法匹配高版本的容器installation is expected 解决方法: 1.找到tomcat的lib目录下的catalina.jar包,用压缩 ...
- [iOS Xcode8]上传AppStore无法添加构建版本
最近升级到xcode8了,也遇到了一些问题.最近产品需要更新版本,按照以前的流程,我觉得so easy啊,万万没想到啊,这次更新版本差点让我吐血,来来回回不下七次. 首先问题是版本通过xcode上传到 ...
- 3.2. 添加模板版本(Core Data 应用程序实践指南)
为了不像3.1那样崩溃,修改模型之前先创建新的模型版本.添加之后,会生成一个新的xcdatamodel文件,并且跟原来的内容完全一样,这有意思了,但是不要删除原来旧版的模型.旧的模型有助于把原来持久化 ...
- mac sublime切换编辑语言的方法(添加其他版本的python)
在sublime中指定python版本,操作如下: Sublime——tools——build system——new build system 把文件中的内容替换为 { "cmd" ...
- wamp——添加多版本PHP
前言 从下载到部署,手把手教学~ wamp 版本 | 3.0.4 32位 步骤 下载PHP 进入网站https://windows.php.net/download然后选择想要的版本下载 注意点: . ...
- Vs2013中通过Nuget添加不同版本jQuery
vs2013中如果直接更新jQuery则是2X的版本,为了兼容IE浏览器的,一般都是希望使用jQuery1.x版本的jQuery,则可在调出Nuget的控制台,在控制台输入(此例子是下载jQuery ...
- 【添加最新版本的mysql的jdbc连接jar包】java.math.BigInteger cannot be cast to java.lang.Long异常
[问题描述] 从我的电脑把项目拷贝到guo小中的win8电脑,but出现了那个错误,估计他的mysql是最新版本的. [如何下载连接jar包] 链接:https://pan.baidu.com/s/1 ...
- git 忽略已经添加到版本库的文件
第一步: 指令:git rm -r --cached YOUR_PATH YOUR_PATH 即 你的文件,-r 指定了递归所有的子文件夹. 第二步: 修改项目根目录下的 .gitignore 文件, ...
随机推荐
- ECMAScript迭代语句
迭代语句又叫循环语句,声明一组要反复执行的命令,直到满足某些条件为止. 循环通常用于迭代数组的值(因此而得名),或者执行重复的算术任务. do-while, while, for, for-in -- ...
- stl中的容器、迭代器和算法----vector中的find实现
来源 http://blog.csdn.net/huangyimin/article/details/6133650 stl包括容器.迭代器和算法: 容器 用于管理一些相关的数据类型.每种容器都有它的 ...
- Linux图形界面与字符界面切换
1. 启动时进入字符界面,后来想切换到图形界面:使用startx 或 init 5 (注:startx只是在原有运行级别3上加了图形界面,运行级别没变,而init 5 则是切换到运行级别5,所以要重新 ...
- 【Java SE】利用Java的for循环加random制作小学试卷
前期介绍:很多同学以为学习一门编程语言,一定要学到很高深的时候才可以做项目,其实不然,很多时候我们不需要学到面向对象的思想,就可以从事一些小项目的开发,来增加自己对开发的热情,比如现在我就可以利用Ja ...
- XSS攻击及预防
跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者往Web页面里插 ...
- OpenCV探索之路(八):重映射与仿射变换
重映射 重映射就是把一幅图像中某个位置的像素放置到另一个图片中指定位置的过程. 用一个数学公式来表示就是: 其中的 f 就是映射方式,也就说,像素点在另一个图像中的位置是由 f 来计算的. 在Open ...
- OpenCV探索之路(十三):详解掩膜mask
在OpenCV中我们经常会遇到一个名字:Mask(掩膜).很多函数都使用到它,那么这个Mask到底什么呢? 一开始我接触到Mask这个东西时,我还真是一头雾水啊,也对无法理解Mask到底有什么用.经过 ...
- .Net Core应用搭建的分布式邮件系统设计
本篇分享的是由NetCore搭建的分布式邮件系统,主要采用NetCore的Api和控制台应用程序,由于此系统属于公司的所以这里只能分享设计图和一些单纯不设计业务的类或方法: 为什么要在公司中首例采用N ...
- Customer segmentation – LifeCycle Grids with R(转)
I want to share a very powerful approach for customer segmentation in this post. It is based on cust ...
- Mac苹果系统 多系统启动:The rEFInd Boot Manager
苹果系统 多系统启动 下载安装REFIT: 首先安装一下:REFIT, 在这个页面下载: http://refit.sourceforge.net/#download 选择mac disk image ...