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 文件, ...
随机推荐
- hdu2594 Simpsons’ Hidden Talents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路: 其实就是求相同的最长前缀与最长后缀 KMP算法的简单应用: 假设输入的两个字符串分别是s ...
- c#接口使用详解
c#接口使用详解 c#中接口隐式与显示实现 c#中接口可以隐式实现.显示实现,隐式实现更常使用.显示实现较少使用 其区别在于 显示实现避免接口函数签名冲突 显示实现只可以以接口形式调用 显示实现其子类 ...
- SSH抛出org.apache.ibatis.exceptions.PersistenceException: 异常
抛出的异常类容如下 如果遇到这个异常,那么肯定是你在配置事物切面时出错,或者是你的写的事物的方法名称没有和这里的配置对应: 你需要注意如下几点: 1.你的名称必须是以英文开头 2.在你用着事物方法的名 ...
- How to change current process to background process
Situation: there is a script or command is running, but we need to close current box/windows to do o ...
- mysql一库多表查询主键
mysql> show databases; mysql> use information_schema; mysql> show tables; mysql> select ...
- 白话讲session
什么是session Session一般译作会话,牛津词典对其的解释是进行某活动连续的一段时间.从不同的层面看待session,它有着类似但不全然相同的含义.比如,在web应用的用户看来,他打开浏览器 ...
- Fragment回调接口应用间分享数据
package com.example.mydemo; import java.util.List; import android.app.Activity; import android.app.A ...
- Swift 入门之简单语法(三)
集合 数组 数组使用 [] 定义,这一点与 OC 相同 //: [Int] let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 遍历 for num in nu ...
- 关于Laravel中的artisan命令
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; color: #454545 } p.p2 { margin: 0.0p ...
- Mac下修改环境变量并保存
1. 终端执行 touch ~/.bash_profile 2.终端执行 vim ~/.bash_profile 3.点一下向下箭头,点E 进入bash_profile文件 4.点i进入编辑模式,进行 ...