Skip to content

Instantly share code, notes, and snippets.

@losophy
losophy / A1Notation.py
Last active May 24, 2021 13:15
实现A1引用样式
#!/usr/bin/python
#-*- coding: UTF-8 -*-
def getA1Notation(num):
if num == 0 :
print('A')
notations = []
alphabetNum = 26
while num != 0:
@losophy
losophy / errorlevel.bat
Last active May 24, 2021 13:12
cmd 错误等级控制
echo off
python errorlevel.py
echo %ERRORLEVEL%
pause
@losophy
losophy / GEventMgr.lua
Last active May 24, 2021 12:45
事件驱动器
local GEventMgr = {}
local ClassModule = require "Class"
local Traceback = Traceback
local globalEventRegCache = {}--eventName<eventName<obj,true>>
function GEventMgr.RegEvent( eventName, obj )
if not obj[eventName] then
logwarning("GEventMgr.RegEvent","eventName:",eventName,"is not in obj:",ClassModule.GetClassName(obj))
end
local eventRegs = globalEventRegCache[eventName]
@losophy
losophy / luaClass.lua
Created May 24, 2021 12:53
lua类的实现
function class(classname, super)
--superType获取父类类型,可以使nil、function以及table.
local superType = type(super)
local cls
--如果父类既不是函数也不是table则说明父类为空
if superType ~= "function" and superType ~= "table" then
superType = nil
super = nil
end
@losophy
losophy / lstring.c
Last active July 26, 2021 10:09
lua-5.1.1中的字符串实现
void luaS_resize (lua_State *L, int newsize) {
GCObject **newhash;
stringtable *tb;
int i;
if (G(L)->gcstate == GCSsweepstring)
return; /* cannot resize during GC traverse */
newhash = luaM_newvector(L, newsize, GCObject *);
tb = &G(L)->strt;
for (i=0; i<newsize; i++) newhash[i] = NULL;
/* rehash */
@losophy
losophy / ltable.c
Last active August 9, 2021 08:17
lua-5.1.1中的table实现
/*
** search function for integers
*/
const TValue *luaH_getnum (Table *t, int key) {
/* (1 <= key && key <= t->sizearray) */
if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))//如果输入的Key是一个正整数,并且它的位大于0并且少等于或等于数组的大小,尝试在数组部分查找
return &t->array[key-1];
else {//如果不是,尝试在散列表部分查找,计算出该`Key`的散列值,根据此散列值访问`Node`数组得到散列桶所在的位置,遍历该散列桶下的所有链表元素,直到找到该`Key`为止。
lua_Number nk = cast_num(key);
Node *n = hashnum(t, nk);
@losophy
losophy / lgc.c
Last active May 28, 2021 02:40
lua-5.1.1中的gc的实现
void luaC_link (lua_State *L, GCObject *o, lu_byte tt) {
global_State *g = G(L);
o->gch.next = g->rootgc;
g->rootgc = o;//将对象挂载到 rootgc链表上
o->gch.marked = luaC_white(g);//设置颜色为白色
o->gch.tt = tt;//设置数据的类型
}
void luaC_linkupval (lua_State *L, UpVal *uv) {
global_State *g = G(L);
@losophy
losophy / lvm.c
Last active May 28, 2021 13:44
lua-5.1.1中的vm的实现
static void f_parser (lua_State *L, void *ud) {
int i;
Proto *tf;
Closure *cl;
struct SParser *p = cast(struct SParser *, ud);
int c = luaZ_lookahead(p->z);
luaC_checkGC(L);
tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
&p->buff, p->name);
cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
@losophy
losophy / ltypes.h
Last active May 28, 2021 13:53
lua-5.1.1中的动态类型的实现
/*
** basic types
*/
#define LUA_TNONE (-1)
#define LUA_TNIL 0
#define LUA_TBOOLEAN 1
#define LUA_TLIGHTUSERDATA 2 //void *
#define LUA_TNUMBER 3 //lua3后采用double精度表示数字整型
#define LUA_TSTRING 4 //TString
@losophy
losophy / lthread.c
Last active August 5, 2021 01:45
lua-5.1.1中的协程的实现
LUA_API lua_State *lua_newthread (lua_State *L) {
lua_State *L1;
lua_lock(L);
luaC_checkGC(L);
L1 = luaE_newthread(L);
setthvalue(L, L->top, L1);
api_incr_top(L);
lua_unlock(L);
luai_userstatethread(L, L1);
return L1;