try use new way to generate code

This commit is contained in:
yyc12345 2023-08-17 15:48:30 +08:00
parent a6152b19d3
commit c1aac0beda
10 changed files with 76 additions and 213 deletions

11
.gitignore vendored
View File

@ -3,7 +3,6 @@
*.cmo
*.nms
*.vmo
CodeGen/dest/*.txt
PyCmoOld/
out/
@ -11,6 +10,16 @@ temp/
.vscode/
## Special Treat of CodeGen
CodeGen.old/
CodeGen/dest/*.txt
CodeGen/CKGeneralLexer*.*
CodeGen/CKEnumParser*.*
CodeGen/CKErrorParser*.*
CodeGen/CKClassidParser*.*
!CodeGen/*.g4
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##

View File

@ -0,0 +1,6 @@
parser grammar CKClassidParser;
options { tokenVocab = CKGeneralLexer; }
prog: definePair+ ;
definePair: CKGENERAL_DEFINE CKGENERAL_ID CKGENERAL_NUM ;

View File

@ -1,34 +0,0 @@
full_error = []
annotation = ''
with open('src/CKERROR.txt', 'r', encoding='utf-8') as fr:
while True:
ln = fr.readline()
if ln == '':
break
ln.strip()
if ln == '':
continue
if ln.startswith('#define'):
sp = ln[len('#define'):].strip().split(' ')
# name, value, description
full_error.append((sp[0], sp[-1], annotation))
annotation = ''
elif ln.startswith('//'):
annotation = ln[len('//'):].strip()
fr.close()
with open('dest/CKERROR.txt', 'w', encoding='utf-8') as fw:
for item in full_error:
fw.write('{{ LibCmo::CKERROR::{}, {{"{}", "{}"}} }},\n'.format(
item[0],
item[0],
item[-1])
)
fw.write('\n')
for item in full_error:
fw.write(f'{item[0]} = {item[1]},\n')
fw.close()

14
CodeGen/CKEnumParser.g4 Normal file
View File

@ -0,0 +1,14 @@
parser grammar CKEnumParser;
options { tokenVocab = CKGeneralLexer; }
prog: enumBody+ ;
enumBody: CKGENERAL_TYPEDEF? CKGENERAL_ENUM CKGENERAL_ID CKGENERAL_LBRACKET
entryPair (CKGENERAL_COMMA entryPair)*
CKGENERAL_RBRACKET CKGENERAL_ID? CKGENERAL_SEMICOLON ;
entryPair: CKGENERAL_ID (CKGENERAL_EQUAL entryValue)? ;
entryValue: CKGENERAL_NUM (CKGENERAL_LSHIFT CKGENERAL_NUM)* # entryDirectValue
| CKGENERAL_ID (CKGENERAL_OR CKGENERAL_ID)* # entryRelativeValue
;

6
CodeGen/CKErrorParser.g4 Normal file
View File

@ -0,0 +1,6 @@
parser grammar CKErrorParser;
options { tokenVocab = CKGeneralLexer; }
prog: definePair+ ;
definePair: CKGENERAL_DEFINE CKGENERAL_ID CKGENERAL_NUM ;

26
CodeGen/CKGeneralLexer.g4 Normal file
View File

@ -0,0 +1,26 @@
lexer grammar CKGeneralLexer;
channels { COMMENTS, WHITESPACE }
// keywords
CKGENERAL_TYPEDEF: 'typedef' ;
CKGENERAL_DEFINE: '#define' ;
CKGENERAL_ENUM: 'enum' ;
// symbols
CKGENERAL_LBRACKET: '{' ;
CKGENERAL_RBRACKET: '}' ;
CKGENERAL_EQUAL: '=';
CKGENERAL_SEMICOLON: ';' ;
CKGENERAL_LSHIFT: '<<' ;
CKGENERAL_OR: '|' ;
CKGENERAL_COMMA: ',' ;
// identifider and number
CKGENERAL_ID: [_a-zA-Z][_a-zA-Z0-9]* ;
CKGENERAL_NUM: '-'? ('0' [xX])? [0-9a-fA-F]+ [uUlL]* ;
// comments
CKGENERAL_LINE_COMMENT: '//' ~[\r\n]* -> channel(COMMENTS);
CKGENERAL_BLOCK_COMMENT: '/*' .*? '*/' -> channel(COMMENTS);
// whitespace
CKGENERAL_WS: [ \t\r\n]+ -> channel(WHITESPACE);

View File

@ -1,102 +0,0 @@
def RemoveAnnotation(strl: str) -> str:
idx = strl.find("//")
if idx == -1: return strl.strip()
return strl[:idx].strip()
def Underline2Camel(orig: str) -> str:
return ''.join(map(
lambda strl: strl[0].upper() + strl[1:].lower(),
orig.split('_')
))
class EnumEntry():
def __init__(self, name: str, val: str):
self.name: str = name
self.val: str = val
def __repr__(self) -> str:
return f'<{self.name}: {self.val}>'
class EnumBody():
def __init__(self, name: str, is_flag: bool):
self.name: str = name
self.camel_name: str = Underline2Camel(name)
self.is_flag: bool = is_flag
self.entries: list[EnumEntry] = []
def __repr__(self) -> str:
return self.entries.__repr__()
full_enum: list[EnumBody] = []
current_body: EnumEntry = None
with open('src/CKMISC.txt', 'r', encoding='utf-8') as fr:
while True:
ln = fr.readline()
if ln == '':
break
ln = RemoveAnnotation(ln)
if ln == '':
continue
if ln.startswith('enum'):
ln = ln[len('enum'):].strip()
is_flag = ln[0] == '!'
name = ln[1:] if is_flag else ln
if current_body:
full_enum.append(current_body)
current_body = EnumBody(name, is_flag)
else:
sp = ln.replace(',', '').split('=')
if len(sp) == 1:
entry = EnumEntry(sp[0].strip(), '')
else:
entry = EnumEntry(sp[0].strip(), sp[-1].strip())
current_body.entries.append(entry)
fr.close()
if current_body:
full_enum.append(current_body)
with open('dest/CKMISC.txt', 'w', encoding='utf-8') as fw:
# write define
for item in full_enum:
fw.write('enum class {} : int32_t {{\n'.format(item.name))
fw.write(',\n'.join(map(
lambda x: x.name if x.val == '' else (x.name + ' = ' + x.val),
item.entries
)))
fw.write('\n};\n')
fw.write('\n')
# write decl
fw.write('namespace EnumDesc {\n')
for item in full_enum:
fw.write('extern const EnumDescPairArray<LibCmo::{}> {};\n'.format(
item.name, item.name
))
fw.write('}')
fw.write('\n')
# write vector
fw.write('namespace EnumDesc {\n')
for item in full_enum:
fw.write('const EnumDescPairArray<LibCmo::{}> {} {{\n'.format(
item.name, item.name
))
fw.write(',\n'.join(map(
lambda x: '{{ LibCmo::{}::{}, "{}" }}'.format(item.name, x.name, x.name),
item.entries
)))
fw.write('\n};\n')
fw.write('}')
fw.write('\n')
fw.close()

1
CodeGen/CKMainGen.java Normal file
View File

@ -0,0 +1 @@
// todo: WIP

View File

@ -1,76 +0,0 @@
class CKClass():
def __init__(self, name: str, value: int):
self.name: str = name
self.value: int = value
self.parents: tuple[str] = None
def GetLevel(strl: str):
counter = 0
for c in strl:
if c == '\t':
counter += 1
else:
break
return counter
def BuildClass(strl: str) -> CKClass:
strl = strl.replace('#define', '\t').replace(' ', '\t').strip()
sp = strl.split('\t')
return CKClass(sp[0], sp[-1])
def GetParents(ls: list[CKClass]) -> tuple[str]:
return tuple(
map(lambda x: x.name, ls)
)
full_classes = []
with open('src/CK_CLASSID.txt', 'r', encoding='utf-8') as fr:
level = 0
node_stack: list[CKClass] = [None]
while True:
ln = fr.readline()
if ln == '':
break
if ln.strip() == '':
continue
ln = ln.strip('\n')
new_item = BuildClass(ln)
full_classes.append(new_item)
this_level = GetLevel(ln)
if this_level > level:
# level up
level += 1
node_stack.append(new_item)
new_item.parents = GetParents(node_stack)
elif this_level == level:
node_stack.pop()
node_stack.append(new_item)
new_item.parents = GetParents(node_stack)
elif this_level < level:
for i in range(level - this_level + 1):
node_stack.pop()
level = this_level
node_stack.append(new_item)
new_item.parents = GetParents(node_stack)
fr.close()
with open('dest/CK_CLASSID.txt', 'w', encoding='utf-8') as fw:
for item in full_classes:
fw.write('{{ LibCmo::CK_CLASSID::{}, {{{}}} }},\n'.format(
item.parents[-1],
', '.join(
map(lambda x: '"' + x + '"', item.parents)
)
))
fw.write('\n')
for item in full_classes:
fw.write(f'{item.name} = {item.value},\n')
fw.close()

13
CodeGen/README.md Normal file
View File

@ -0,0 +1,13 @@
# Code Gen
A helper program to generate some definations.
```
antlr4 CKGeneralLexer.g4
antlr4 CKEnumParser.g4
antlr4 CKErrorParser.g4
antlr4 CKClassidParser.g4
javac CK*.java
java CKMainGen
```