diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d9f7800 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pychache__ +errors.txt +declarations.txt diff --git a/experiments/antlr/Declaration.g4 b/experiments/antlr/Declaration.g4 new file mode 100644 index 0000000..2834207 --- /dev/null +++ b/experiments/antlr/Declaration.g4 @@ -0,0 +1,26 @@ +grammar Declaration; + + +WS: [ \t\r\n]+ -> skip ; + +VAR: 'var' ; +FUNC: 'func' ; +STRUCT: 'struct' ; +ENUM: 'enum' ; +ALIAS: 'alias' ; +MEMBER: 'memb' ; + +ID: [a-zA-Z0-9_]+('[]' | '*')* ; + +start : declaration* ; + +declaration + : type=ALIAS name=ID ':' typename=ID + | type=VAR name=ID ':' typename=ID + | type=FUNC name=ID ':' return_typename=ID ('->' arg_typenames+=ID (',' arg_typenames+=ID)* )? + | type=STRUCT name=ID ':' member* + | type=ENUM name=ID ':' enum_values+=ID (',' enum_values+=ID)* + ; + +member + : MEMBER name=ID ':' typename=ID ; \ No newline at end of file diff --git a/experiments/declparser/Declaration.interp b/experiments/declparser/Declaration.interp new file mode 100644 index 0000000..9c00fa7 --- /dev/null +++ b/experiments/declparser/Declaration.interp @@ -0,0 +1,36 @@ +token literal names: +null +':' +'->' +',' +null +'var' +'func' +'struct' +'enum' +'alias' +'memb' +null + +token symbolic names: +null +null +null +null +WS +VAR +FUNC +STRUCT +ENUM +ALIAS +MEMBER +ID + +rule names: +start +declaration +member + + +atn: +[4, 1, 11, 63, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 1, 0, 5, 0, 8, 8, 0, 10, 0, 12, 0, 11, 9, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 29, 8, 1, 10, 1, 12, 1, 32, 9, 1, 3, 1, 34, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 40, 8, 1, 10, 1, 12, 1, 43, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 51, 8, 1, 10, 1, 12, 1, 54, 9, 1, 3, 1, 56, 8, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0, 0, 3, 0, 2, 4, 0, 0, 68, 0, 9, 1, 0, 0, 0, 2, 55, 1, 0, 0, 0, 4, 57, 1, 0, 0, 0, 6, 8, 3, 2, 1, 0, 7, 6, 1, 0, 0, 0, 8, 11, 1, 0, 0, 0, 9, 7, 1, 0, 0, 0, 9, 10, 1, 0, 0, 0, 10, 1, 1, 0, 0, 0, 11, 9, 1, 0, 0, 0, 12, 13, 5, 9, 0, 0, 13, 14, 5, 11, 0, 0, 14, 15, 5, 1, 0, 0, 15, 56, 5, 11, 0, 0, 16, 17, 5, 5, 0, 0, 17, 18, 5, 11, 0, 0, 18, 19, 5, 1, 0, 0, 19, 56, 5, 11, 0, 0, 20, 21, 5, 6, 0, 0, 21, 22, 5, 11, 0, 0, 22, 23, 5, 1, 0, 0, 23, 33, 5, 11, 0, 0, 24, 25, 5, 2, 0, 0, 25, 30, 5, 11, 0, 0, 26, 27, 5, 3, 0, 0, 27, 29, 5, 11, 0, 0, 28, 26, 1, 0, 0, 0, 29, 32, 1, 0, 0, 0, 30, 28, 1, 0, 0, 0, 30, 31, 1, 0, 0, 0, 31, 34, 1, 0, 0, 0, 32, 30, 1, 0, 0, 0, 33, 24, 1, 0, 0, 0, 33, 34, 1, 0, 0, 0, 34, 56, 1, 0, 0, 0, 35, 36, 5, 7, 0, 0, 36, 37, 5, 11, 0, 0, 37, 41, 5, 1, 0, 0, 38, 40, 3, 4, 2, 0, 39, 38, 1, 0, 0, 0, 40, 43, 1, 0, 0, 0, 41, 39, 1, 0, 0, 0, 41, 42, 1, 0, 0, 0, 42, 56, 1, 0, 0, 0, 43, 41, 1, 0, 0, 0, 44, 45, 5, 8, 0, 0, 45, 46, 5, 11, 0, 0, 46, 47, 5, 1, 0, 0, 47, 52, 5, 11, 0, 0, 48, 49, 5, 3, 0, 0, 49, 51, 5, 11, 0, 0, 50, 48, 1, 0, 0, 0, 51, 54, 1, 0, 0, 0, 52, 50, 1, 0, 0, 0, 52, 53, 1, 0, 0, 0, 53, 56, 1, 0, 0, 0, 54, 52, 1, 0, 0, 0, 55, 12, 1, 0, 0, 0, 55, 16, 1, 0, 0, 0, 55, 20, 1, 0, 0, 0, 55, 35, 1, 0, 0, 0, 55, 44, 1, 0, 0, 0, 56, 3, 1, 0, 0, 0, 57, 58, 5, 10, 0, 0, 58, 59, 5, 11, 0, 0, 59, 60, 5, 1, 0, 0, 60, 61, 5, 11, 0, 0, 61, 5, 1, 0, 0, 0, 6, 9, 30, 33, 41, 52, 55] \ No newline at end of file diff --git a/experiments/declparser/Declaration.tokens b/experiments/declparser/Declaration.tokens new file mode 100644 index 0000000..4f90447 --- /dev/null +++ b/experiments/declparser/Declaration.tokens @@ -0,0 +1,20 @@ +T__0=1 +T__1=2 +T__2=3 +WS=4 +VAR=5 +FUNC=6 +STRUCT=7 +ENUM=8 +ALIAS=9 +MEMBER=10 +ID=11 +':'=1 +'->'=2 +','=3 +'var'=5 +'func'=6 +'struct'=7 +'enum'=8 +'alias'=9 +'memb'=10 diff --git a/experiments/declparser/DeclarationLexer.interp b/experiments/declparser/DeclarationLexer.interp new file mode 100644 index 0000000..7f648ec --- /dev/null +++ b/experiments/declparser/DeclarationLexer.interp @@ -0,0 +1,50 @@ +token literal names: +null +':' +'->' +',' +null +'var' +'func' +'struct' +'enum' +'alias' +'memb' +null + +token symbolic names: +null +null +null +null +WS +VAR +FUNC +STRUCT +ENUM +ALIAS +MEMBER +ID + +rule names: +T__0 +T__1 +T__2 +WS +VAR +FUNC +STRUCT +ENUM +ALIAS +MEMBER +ID + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[4, 0, 11, 82, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 4, 3, 32, 8, 3, 11, 3, 12, 3, 33, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 10, 4, 10, 71, 8, 10, 11, 10, 12, 10, 72, 1, 10, 1, 10, 1, 10, 5, 10, 78, 8, 10, 10, 10, 12, 10, 81, 9, 10, 0, 0, 11, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8, 17, 9, 19, 10, 21, 11, 1, 0, 2, 3, 0, 9, 10, 13, 13, 32, 32, 4, 0, 48, 57, 65, 90, 95, 95, 97, 122, 85, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 15, 1, 0, 0, 0, 0, 17, 1, 0, 0, 0, 0, 19, 1, 0, 0, 0, 0, 21, 1, 0, 0, 0, 1, 23, 1, 0, 0, 0, 3, 25, 1, 0, 0, 0, 5, 28, 1, 0, 0, 0, 7, 31, 1, 0, 0, 0, 9, 37, 1, 0, 0, 0, 11, 41, 1, 0, 0, 0, 13, 46, 1, 0, 0, 0, 15, 53, 1, 0, 0, 0, 17, 58, 1, 0, 0, 0, 19, 64, 1, 0, 0, 0, 21, 70, 1, 0, 0, 0, 23, 24, 5, 58, 0, 0, 24, 2, 1, 0, 0, 0, 25, 26, 5, 45, 0, 0, 26, 27, 5, 62, 0, 0, 27, 4, 1, 0, 0, 0, 28, 29, 5, 44, 0, 0, 29, 6, 1, 0, 0, 0, 30, 32, 7, 0, 0, 0, 31, 30, 1, 0, 0, 0, 32, 33, 1, 0, 0, 0, 33, 31, 1, 0, 0, 0, 33, 34, 1, 0, 0, 0, 34, 35, 1, 0, 0, 0, 35, 36, 6, 3, 0, 0, 36, 8, 1, 0, 0, 0, 37, 38, 5, 118, 0, 0, 38, 39, 5, 97, 0, 0, 39, 40, 5, 114, 0, 0, 40, 10, 1, 0, 0, 0, 41, 42, 5, 102, 0, 0, 42, 43, 5, 117, 0, 0, 43, 44, 5, 110, 0, 0, 44, 45, 5, 99, 0, 0, 45, 12, 1, 0, 0, 0, 46, 47, 5, 115, 0, 0, 47, 48, 5, 116, 0, 0, 48, 49, 5, 114, 0, 0, 49, 50, 5, 117, 0, 0, 50, 51, 5, 99, 0, 0, 51, 52, 5, 116, 0, 0, 52, 14, 1, 0, 0, 0, 53, 54, 5, 101, 0, 0, 54, 55, 5, 110, 0, 0, 55, 56, 5, 117, 0, 0, 56, 57, 5, 109, 0, 0, 57, 16, 1, 0, 0, 0, 58, 59, 5, 97, 0, 0, 59, 60, 5, 108, 0, 0, 60, 61, 5, 105, 0, 0, 61, 62, 5, 97, 0, 0, 62, 63, 5, 115, 0, 0, 63, 18, 1, 0, 0, 0, 64, 65, 5, 109, 0, 0, 65, 66, 5, 101, 0, 0, 66, 67, 5, 109, 0, 0, 67, 68, 5, 98, 0, 0, 68, 20, 1, 0, 0, 0, 69, 71, 7, 1, 0, 0, 70, 69, 1, 0, 0, 0, 71, 72, 1, 0, 0, 0, 72, 70, 1, 0, 0, 0, 72, 73, 1, 0, 0, 0, 73, 79, 1, 0, 0, 0, 74, 75, 5, 91, 0, 0, 75, 78, 5, 93, 0, 0, 76, 78, 5, 42, 0, 0, 77, 74, 1, 0, 0, 0, 77, 76, 1, 0, 0, 0, 78, 81, 1, 0, 0, 0, 79, 77, 1, 0, 0, 0, 79, 80, 1, 0, 0, 0, 80, 22, 1, 0, 0, 0, 81, 79, 1, 0, 0, 0, 5, 0, 33, 72, 77, 79, 1, 6, 0, 0] \ No newline at end of file diff --git a/experiments/declparser/DeclarationLexer.py b/experiments/declparser/DeclarationLexer.py new file mode 100644 index 0000000..abe04ad --- /dev/null +++ b/experiments/declparser/DeclarationLexer.py @@ -0,0 +1,83 @@ +# Generated from Declaration.g4 by ANTLR 4.13.1 +from antlr4 import * +from io import StringIO +import sys +if sys.version_info[1] > 5: + from typing import TextIO +else: + from typing.io import TextIO + + +def serializedATN(): + return [ + 4,0,11,82,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,2,3,7,3,2,4,7,4,2,5,7,5,2, + 6,7,6,2,7,7,7,2,8,7,8,2,9,7,9,2,10,7,10,1,0,1,0,1,1,1,1,1,1,1,2, + 1,2,1,3,4,3,32,8,3,11,3,12,3,33,1,3,1,3,1,4,1,4,1,4,1,4,1,5,1,5, + 1,5,1,5,1,5,1,6,1,6,1,6,1,6,1,6,1,6,1,6,1,7,1,7,1,7,1,7,1,7,1,8, + 1,8,1,8,1,8,1,8,1,8,1,9,1,9,1,9,1,9,1,9,1,10,4,10,71,8,10,11,10, + 12,10,72,1,10,1,10,1,10,5,10,78,8,10,10,10,12,10,81,9,10,0,0,11, + 1,1,3,2,5,3,7,4,9,5,11,6,13,7,15,8,17,9,19,10,21,11,1,0,2,3,0,9, + 10,13,13,32,32,4,0,48,57,65,90,95,95,97,122,85,0,1,1,0,0,0,0,3,1, + 0,0,0,0,5,1,0,0,0,0,7,1,0,0,0,0,9,1,0,0,0,0,11,1,0,0,0,0,13,1,0, + 0,0,0,15,1,0,0,0,0,17,1,0,0,0,0,19,1,0,0,0,0,21,1,0,0,0,1,23,1,0, + 0,0,3,25,1,0,0,0,5,28,1,0,0,0,7,31,1,0,0,0,9,37,1,0,0,0,11,41,1, + 0,0,0,13,46,1,0,0,0,15,53,1,0,0,0,17,58,1,0,0,0,19,64,1,0,0,0,21, + 70,1,0,0,0,23,24,5,58,0,0,24,2,1,0,0,0,25,26,5,45,0,0,26,27,5,62, + 0,0,27,4,1,0,0,0,28,29,5,44,0,0,29,6,1,0,0,0,30,32,7,0,0,0,31,30, + 1,0,0,0,32,33,1,0,0,0,33,31,1,0,0,0,33,34,1,0,0,0,34,35,1,0,0,0, + 35,36,6,3,0,0,36,8,1,0,0,0,37,38,5,118,0,0,38,39,5,97,0,0,39,40, + 5,114,0,0,40,10,1,0,0,0,41,42,5,102,0,0,42,43,5,117,0,0,43,44,5, + 110,0,0,44,45,5,99,0,0,45,12,1,0,0,0,46,47,5,115,0,0,47,48,5,116, + 0,0,48,49,5,114,0,0,49,50,5,117,0,0,50,51,5,99,0,0,51,52,5,116,0, + 0,52,14,1,0,0,0,53,54,5,101,0,0,54,55,5,110,0,0,55,56,5,117,0,0, + 56,57,5,109,0,0,57,16,1,0,0,0,58,59,5,97,0,0,59,60,5,108,0,0,60, + 61,5,105,0,0,61,62,5,97,0,0,62,63,5,115,0,0,63,18,1,0,0,0,64,65, + 5,109,0,0,65,66,5,101,0,0,66,67,5,109,0,0,67,68,5,98,0,0,68,20,1, + 0,0,0,69,71,7,1,0,0,70,69,1,0,0,0,71,72,1,0,0,0,72,70,1,0,0,0,72, + 73,1,0,0,0,73,79,1,0,0,0,74,75,5,91,0,0,75,78,5,93,0,0,76,78,5,42, + 0,0,77,74,1,0,0,0,77,76,1,0,0,0,78,81,1,0,0,0,79,77,1,0,0,0,79,80, + 1,0,0,0,80,22,1,0,0,0,81,79,1,0,0,0,5,0,33,72,77,79,1,6,0,0 + ] + +class DeclarationLexer(Lexer): + + atn = ATNDeserializer().deserialize(serializedATN()) + + decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ] + + T__0 = 1 + T__1 = 2 + T__2 = 3 + WS = 4 + VAR = 5 + FUNC = 6 + STRUCT = 7 + ENUM = 8 + ALIAS = 9 + MEMBER = 10 + ID = 11 + + channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN" ] + + modeNames = [ "DEFAULT_MODE" ] + + literalNames = [ "", + "':'", "'->'", "','", "'var'", "'func'", "'struct'", "'enum'", + "'alias'", "'memb'" ] + + symbolicNames = [ "", + "WS", "VAR", "FUNC", "STRUCT", "ENUM", "ALIAS", "MEMBER", "ID" ] + + ruleNames = [ "T__0", "T__1", "T__2", "WS", "VAR", "FUNC", "STRUCT", + "ENUM", "ALIAS", "MEMBER", "ID" ] + + grammarFileName = "Declaration.g4" + + def __init__(self, input=None, output:TextIO = sys.stdout): + super().__init__(input, output) + self.checkVersion("4.13.1") + self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache()) + self._actions = None + self._predicates = None + + diff --git a/experiments/declparser/DeclarationLexer.tokens b/experiments/declparser/DeclarationLexer.tokens new file mode 100644 index 0000000..4f90447 --- /dev/null +++ b/experiments/declparser/DeclarationLexer.tokens @@ -0,0 +1,20 @@ +T__0=1 +T__1=2 +T__2=3 +WS=4 +VAR=5 +FUNC=6 +STRUCT=7 +ENUM=8 +ALIAS=9 +MEMBER=10 +ID=11 +':'=1 +'->'=2 +','=3 +'var'=5 +'func'=6 +'struct'=7 +'enum'=8 +'alias'=9 +'memb'=10 diff --git a/experiments/declparser/DeclarationListener.py b/experiments/declparser/DeclarationListener.py new file mode 100644 index 0000000..45cebda --- /dev/null +++ b/experiments/declparser/DeclarationListener.py @@ -0,0 +1,39 @@ +# Generated from Declaration.g4 by ANTLR 4.13.1 +from antlr4 import * +if "." in __name__: + from .DeclarationParser import DeclarationParser +else: + from DeclarationParser import DeclarationParser + +# This class defines a complete listener for a parse tree produced by DeclarationParser. +class DeclarationListener(ParseTreeListener): + + # Enter a parse tree produced by DeclarationParser#start. + def enterStart(self, ctx:DeclarationParser.StartContext): + pass + + # Exit a parse tree produced by DeclarationParser#start. + def exitStart(self, ctx:DeclarationParser.StartContext): + pass + + + # Enter a parse tree produced by DeclarationParser#declaration. + def enterDeclaration(self, ctx:DeclarationParser.DeclarationContext): + pass + + # Exit a parse tree produced by DeclarationParser#declaration. + def exitDeclaration(self, ctx:DeclarationParser.DeclarationContext): + pass + + + # Enter a parse tree produced by DeclarationParser#member. + def enterMember(self, ctx:DeclarationParser.MemberContext): + pass + + # Exit a parse tree produced by DeclarationParser#member. + def exitMember(self, ctx:DeclarationParser.MemberContext): + pass + + + +del DeclarationParser \ No newline at end of file diff --git a/experiments/declparser/DeclarationParser.py b/experiments/declparser/DeclarationParser.py new file mode 100644 index 0000000..9b60229 --- /dev/null +++ b/experiments/declparser/DeclarationParser.py @@ -0,0 +1,369 @@ +# Generated from Declaration.g4 by ANTLR 4.13.1 +# encoding: utf-8 +from antlr4 import * +from io import StringIO +import sys +if sys.version_info[1] > 5: + from typing import TextIO +else: + from typing.io import TextIO + +def serializedATN(): + return [ + 4,1,11,63,2,0,7,0,2,1,7,1,2,2,7,2,1,0,5,0,8,8,0,10,0,12,0,11,9,0, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 5,1,29,8,1,10,1,12,1,32,9,1,3,1,34,8,1,1,1,1,1,1,1,1,1,5,1,40,8, + 1,10,1,12,1,43,9,1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,51,8,1,10,1,12,1, + 54,9,1,3,1,56,8,1,1,2,1,2,1,2,1,2,1,2,1,2,0,0,3,0,2,4,0,0,68,0,9, + 1,0,0,0,2,55,1,0,0,0,4,57,1,0,0,0,6,8,3,2,1,0,7,6,1,0,0,0,8,11,1, + 0,0,0,9,7,1,0,0,0,9,10,1,0,0,0,10,1,1,0,0,0,11,9,1,0,0,0,12,13,5, + 9,0,0,13,14,5,11,0,0,14,15,5,1,0,0,15,56,5,11,0,0,16,17,5,5,0,0, + 17,18,5,11,0,0,18,19,5,1,0,0,19,56,5,11,0,0,20,21,5,6,0,0,21,22, + 5,11,0,0,22,23,5,1,0,0,23,33,5,11,0,0,24,25,5,2,0,0,25,30,5,11,0, + 0,26,27,5,3,0,0,27,29,5,11,0,0,28,26,1,0,0,0,29,32,1,0,0,0,30,28, + 1,0,0,0,30,31,1,0,0,0,31,34,1,0,0,0,32,30,1,0,0,0,33,24,1,0,0,0, + 33,34,1,0,0,0,34,56,1,0,0,0,35,36,5,7,0,0,36,37,5,11,0,0,37,41,5, + 1,0,0,38,40,3,4,2,0,39,38,1,0,0,0,40,43,1,0,0,0,41,39,1,0,0,0,41, + 42,1,0,0,0,42,56,1,0,0,0,43,41,1,0,0,0,44,45,5,8,0,0,45,46,5,11, + 0,0,46,47,5,1,0,0,47,52,5,11,0,0,48,49,5,3,0,0,49,51,5,11,0,0,50, + 48,1,0,0,0,51,54,1,0,0,0,52,50,1,0,0,0,52,53,1,0,0,0,53,56,1,0,0, + 0,54,52,1,0,0,0,55,12,1,0,0,0,55,16,1,0,0,0,55,20,1,0,0,0,55,35, + 1,0,0,0,55,44,1,0,0,0,56,3,1,0,0,0,57,58,5,10,0,0,58,59,5,11,0,0, + 59,60,5,1,0,0,60,61,5,11,0,0,61,5,1,0,0,0,6,9,30,33,41,52,55 + ] + +class DeclarationParser ( Parser ): + + grammarFileName = "Declaration.g4" + + atn = ATNDeserializer().deserialize(serializedATN()) + + decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ] + + sharedContextCache = PredictionContextCache() + + literalNames = [ "", "':'", "'->'", "','", "", "'var'", + "'func'", "'struct'", "'enum'", "'alias'", "'memb'" ] + + symbolicNames = [ "", "", "", "", + "WS", "VAR", "FUNC", "STRUCT", "ENUM", "ALIAS", "MEMBER", + "ID" ] + + RULE_start = 0 + RULE_declaration = 1 + RULE_member = 2 + + ruleNames = [ "start", "declaration", "member" ] + + EOF = Token.EOF + T__0=1 + T__1=2 + T__2=3 + WS=4 + VAR=5 + FUNC=6 + STRUCT=7 + ENUM=8 + ALIAS=9 + MEMBER=10 + ID=11 + + def __init__(self, input:TokenStream, output:TextIO = sys.stdout): + super().__init__(input, output) + self.checkVersion("4.13.1") + self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache) + self._predicates = None + + + + + class StartContext(ParserRuleContext): + __slots__ = 'parser' + + def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1): + super().__init__(parent, invokingState) + self.parser = parser + + def declaration(self, i:int=None): + if i is None: + return self.getTypedRuleContexts(DeclarationParser.DeclarationContext) + else: + return self.getTypedRuleContext(DeclarationParser.DeclarationContext,i) + + + def getRuleIndex(self): + return DeclarationParser.RULE_start + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterStart" ): + listener.enterStart(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitStart" ): + listener.exitStart(self) + + + + + def start(self): + + localctx = DeclarationParser.StartContext(self, self._ctx, self.state) + self.enterRule(localctx, 0, self.RULE_start) + self._la = 0 # Token type + try: + self.enterOuterAlt(localctx, 1) + self.state = 9 + self._errHandler.sync(self) + _la = self._input.LA(1) + while (((_la) & ~0x3f) == 0 and ((1 << _la) & 992) != 0): + self.state = 6 + self.declaration() + self.state = 11 + self._errHandler.sync(self) + _la = self._input.LA(1) + + except RecognitionException as re: + localctx.exception = re + self._errHandler.reportError(self, re) + self._errHandler.recover(self, re) + finally: + self.exitRule() + return localctx + + + class DeclarationContext(ParserRuleContext): + __slots__ = 'parser' + + def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1): + super().__init__(parent, invokingState) + self.parser = parser + self.type_ = None # Token + self.name = None # Token + self.typename = None # Token + self.return_typename = None # Token + self._ID = None # Token + self.arg_typenames = list() # of Tokens + self.enum_values = list() # of Tokens + + def ALIAS(self): + return self.getToken(DeclarationParser.ALIAS, 0) + + def ID(self, i:int=None): + if i is None: + return self.getTokens(DeclarationParser.ID) + else: + return self.getToken(DeclarationParser.ID, i) + + def VAR(self): + return self.getToken(DeclarationParser.VAR, 0) + + def FUNC(self): + return self.getToken(DeclarationParser.FUNC, 0) + + def STRUCT(self): + return self.getToken(DeclarationParser.STRUCT, 0) + + def member(self, i:int=None): + if i is None: + return self.getTypedRuleContexts(DeclarationParser.MemberContext) + else: + return self.getTypedRuleContext(DeclarationParser.MemberContext,i) + + + def ENUM(self): + return self.getToken(DeclarationParser.ENUM, 0) + + def getRuleIndex(self): + return DeclarationParser.RULE_declaration + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterDeclaration" ): + listener.enterDeclaration(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitDeclaration" ): + listener.exitDeclaration(self) + + + + + def declaration(self): + + localctx = DeclarationParser.DeclarationContext(self, self._ctx, self.state) + self.enterRule(localctx, 2, self.RULE_declaration) + self._la = 0 # Token type + try: + self.state = 55 + self._errHandler.sync(self) + token = self._input.LA(1) + if token in [9]: + self.enterOuterAlt(localctx, 1) + self.state = 12 + localctx.type_ = self.match(DeclarationParser.ALIAS) + self.state = 13 + localctx.name = self.match(DeclarationParser.ID) + self.state = 14 + self.match(DeclarationParser.T__0) + self.state = 15 + localctx.typename = self.match(DeclarationParser.ID) + pass + elif token in [5]: + self.enterOuterAlt(localctx, 2) + self.state = 16 + localctx.type_ = self.match(DeclarationParser.VAR) + self.state = 17 + localctx.name = self.match(DeclarationParser.ID) + self.state = 18 + self.match(DeclarationParser.T__0) + self.state = 19 + localctx.typename = self.match(DeclarationParser.ID) + pass + elif token in [6]: + self.enterOuterAlt(localctx, 3) + self.state = 20 + localctx.type_ = self.match(DeclarationParser.FUNC) + self.state = 21 + localctx.name = self.match(DeclarationParser.ID) + self.state = 22 + self.match(DeclarationParser.T__0) + self.state = 23 + localctx.return_typename = self.match(DeclarationParser.ID) + self.state = 33 + self._errHandler.sync(self) + _la = self._input.LA(1) + if _la==2: + self.state = 24 + self.match(DeclarationParser.T__1) + self.state = 25 + localctx._ID = self.match(DeclarationParser.ID) + localctx.arg_typenames.append(localctx._ID) + self.state = 30 + self._errHandler.sync(self) + _la = self._input.LA(1) + while _la==3: + self.state = 26 + self.match(DeclarationParser.T__2) + self.state = 27 + localctx._ID = self.match(DeclarationParser.ID) + localctx.arg_typenames.append(localctx._ID) + self.state = 32 + self._errHandler.sync(self) + _la = self._input.LA(1) + + + + pass + elif token in [7]: + self.enterOuterAlt(localctx, 4) + self.state = 35 + localctx.type_ = self.match(DeclarationParser.STRUCT) + self.state = 36 + localctx.name = self.match(DeclarationParser.ID) + self.state = 37 + self.match(DeclarationParser.T__0) + self.state = 41 + self._errHandler.sync(self) + _la = self._input.LA(1) + while _la==10: + self.state = 38 + self.member() + self.state = 43 + self._errHandler.sync(self) + _la = self._input.LA(1) + + pass + elif token in [8]: + self.enterOuterAlt(localctx, 5) + self.state = 44 + localctx.type_ = self.match(DeclarationParser.ENUM) + self.state = 45 + localctx.name = self.match(DeclarationParser.ID) + self.state = 46 + self.match(DeclarationParser.T__0) + self.state = 47 + localctx._ID = self.match(DeclarationParser.ID) + localctx.enum_values.append(localctx._ID) + self.state = 52 + self._errHandler.sync(self) + _la = self._input.LA(1) + while _la==3: + self.state = 48 + self.match(DeclarationParser.T__2) + self.state = 49 + localctx._ID = self.match(DeclarationParser.ID) + localctx.enum_values.append(localctx._ID) + self.state = 54 + self._errHandler.sync(self) + _la = self._input.LA(1) + + pass + else: + raise NoViableAltException(self) + + except RecognitionException as re: + localctx.exception = re + self._errHandler.reportError(self, re) + self._errHandler.recover(self, re) + finally: + self.exitRule() + return localctx + + + class MemberContext(ParserRuleContext): + __slots__ = 'parser' + + def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1): + super().__init__(parent, invokingState) + self.parser = parser + self.name = None # Token + self.typename = None # Token + + def MEMBER(self): + return self.getToken(DeclarationParser.MEMBER, 0) + + def ID(self, i:int=None): + if i is None: + return self.getTokens(DeclarationParser.ID) + else: + return self.getToken(DeclarationParser.ID, i) + + def getRuleIndex(self): + return DeclarationParser.RULE_member + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterMember" ): + listener.enterMember(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitMember" ): + listener.exitMember(self) + + + + + def member(self): + + localctx = DeclarationParser.MemberContext(self, self._ctx, self.state) + self.enterRule(localctx, 4, self.RULE_member) + try: + self.enterOuterAlt(localctx, 1) + self.state = 57 + self.match(DeclarationParser.MEMBER) + self.state = 58 + localctx.name = self.match(DeclarationParser.ID) + self.state = 59 + self.match(DeclarationParser.T__0) + self.state = 60 + localctx.typename = self.match(DeclarationParser.ID) + except RecognitionException as re: + localctx.exception = re + self._errHandler.reportError(self, re) + self._errHandler.recover(self, re) + finally: + self.exitRule() + return localctx + + + + + diff --git a/experiments/init_experiment.py b/experiments/init_experiment.py new file mode 100644 index 0000000..50e58e5 --- /dev/null +++ b/experiments/init_experiment.py @@ -0,0 +1,209 @@ +from antlr4 import * +from declparser.DeclarationLexer import DeclarationLexer +from declparser.DeclarationParser import DeclarationParser +import json +import re +import os + +def parse_errors(file): + errors = {} + + typename_matches = list(set(re.findall("unknown type name '(.+)'", file))) + identifier_matches = list(set(re.findall("use of undeclared identifier '(.+)'", file))) + function_matches = list(set(re.findall("call to undeclared function '(.+)'", file))) + + if len(typename_matches) != 0: + errors["unknown_type_name"] = typename_matches + + if len(identifier_matches) != 0: + errors["use_undeclared_identifier"] = identifier_matches + + if len(function_matches) != 0: + errors["call_undeclared_function"] = function_matches + + return errors + +def parse_declarations(file): + input_stream = InputStream(file) + lexer = DeclarationLexer(input_stream) + token_stream = CommonTokenStream(lexer) + + parser = DeclarationParser(token_stream) + tree = parser.start() + + #print(tree.toStringTree(recog=parser)) + + return tree + +def get_declarations_as_json(tree): + declarations = [] + + for node in tree.children: + if isinstance(tree, TerminalNode): + return + + declaration_type = node.type_.text + + if declaration_type == "var": + declarations.append( + { + "type": "var", + "name": node.name.text, + "typename": node.typename.text + } + ) + elif declaration_type == "func": + declarations.append( + { + "type": "func", + "name": node.name.text, + "return_typename": node.return_typename.text, + "argument_typenames": [typename.text for typename in node.arg_typenames] + } + ) + elif declaration_type == "struct": + members = [] + for child in node.children: + if isinstance(child, DeclarationParser.MemberContext): + members.append({ + "name": child.name.text, + "typename": child.typename.text + }) + declarations.append({ + "type": "struct", + "name": node.name.text, + "members": members + }) + + elif declaration_type == "enum": + declarations.append( + { + "type": "enum", + "name": node.name.text, + "values": [enum_value.text for enum_value in node.enum_values] + } + ) + elif declaration_type == "alias": + declarations.append( + { + "type": "alias", + "name": node.name.text, + "typename": node.typename.text + } + ) + + #print(declarations) + + return declarations + +def main(): + experiment = {} + + metadata_path = input("Enter experiment metadata path (press enter for default path \"./metadata\"): ") + + metadata_path = "./metadata" if metadata_path == "" else metadata_path + + if os.path.exists(metadata_path): + if os.path.isfile(metadata_path): + print("Provided metadata path is a file, not a directory") + return + else: + os.mkdir(metadata_path) + + project_path = input("Enter experiment project path (relative to metadata path): ") + + project_path = os.path.join(metadata_path, project_path) + + if os.path.exists(project_path): + if os.path.isfile(project_path): + print("Provided experiment project path is a file") + return + + project_metadata_path = os.path.join(project_path, "project.json") + + try: + with open(project_metadata_path, "r") as metadata_file: + project_metadata = json.load(metadata_file) + metadata_file.close() + except Exception: + print("Couldn't open project metadata") + return + + else: + project_metadata = { + "org": input("Enter project organization: "), + "project": input("Enter project name: ") + } + + os.mkdir(project_path) + + project_metadata_path = os.path.join(project_path, "project.json") + + try: + with open(project_metadata_path, 'w') as metadata_file: + metadata_file.write(json.dumps(project_metadata, indent=4)) + metadata_file.close() + except Exception: + print("Couldn't create project metadata") + return + + project = project_metadata["project"] + org = project_metadata["org"] + print(f"Using project {project} from organization {org}") + + experiment["name"] = input("Enter the experiment name (press enter for same name as the snippet): ") + experiment["snippet"] = input("Enter the snippet name: ") + experiment["name"] = experiment["snippet"] if experiment["name"] == "" else experiment["name"] + experiment["function_name"] = input("Enter the function name: ") + experiment["function_path"] = input("Enter the function path: ") + + while True: + experiment["language"] = input("Enter the language (c/cpp): ") + + if experiment["language"] in ["c", "cpp"]: + break + + print("Invalid language. Please enter a valid language.") + + errors_filepath = input("Enter the path to the LLVM errors file: ") + declarations_filepath = input("Enter the path to the LLVM declarations file: ") + + #errors_filepath = "./errors.txt" + #declarations_filepath = "./declarations.txt" + + try: + with open(errors_filepath, 'r') as file: + errors_file = file.read() + experiment["errors"] = parse_errors(errors_file) + file.close() + except FileNotFoundError: + print("Errors file does not exist.") + return + + try: + with open(declarations_filepath, 'r') as file: + declarations_file = file.read() + #print(declarations_file) + tree = parse_declarations(declarations_file) + experiment["declarations"] = get_declarations_as_json(tree) + file.close() + except FileNotFoundError: + print("Declarations file does not exist.") + return + + experiment_name = experiment["name"] + experiment_metadata_path = os.path.join(project_path, f"{experiment_name}.json") + + try: + with open(experiment_metadata_path, 'w') as experiment_file: + experiment_file.write(json.dumps(experiment, indent=4)) + experiment_file.close() + except Exception: + print("Couldn't create experiment file") + return + + print("Experiment created correctly") + #print(json.dumps(experiment, indent=4)) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/experiments/metadata/doom/am_map_1.json b/experiments/metadata/doom/am_map_1.json new file mode 100644 index 0000000..e0ee3fb --- /dev/null +++ b/experiments/metadata/doom/am_map_1.json @@ -0,0 +1,97 @@ +{ + "name": "am_map_1", + "snippet": "am_map_1", + "function_name": "AM_getIslope", + "function_path": "/linuxdoom-1.10/am_map.c", + "language": "c", + + "errors": { + "unknown_type_name": [ + "mline_t", "islope_t" + ], + "use_undeclared_identifier": [ + "MAXINT" + ], + "call_undeclared_function": [ + "FixedDiv" + ] + }, + + "declarations": [ + { + "type": "struct", + "name": "TYPE1", + "members": [ + { + "typename": "int", + "name": "x" + }, + { + "typename": "int", + "name": "y" + } + ] + }, + + { + "type": "struct", + "name": "TYPE2", + "members": [ + { + "typename": "int", + "name": "x" + }, + { + "typename": "int", + "name": "y" + } + ] + }, + + { + "type": "struct", + "name": "mline_t", + "members": [ + { + "typename": "TYPE1", + "name": "a" + }, + { + "typename": "TYPE2", + "name": "b" + } + ] + }, + + { + "type": "struct", + "name": "islope_t", + "members": [ + { + "typename": "int", + "name": "slp" + }, + { + "typename": "int", + "name": "islp" + } + ] + }, + + { + "type": "var", + "name": "MAXINT", + "typename": "int" + }, + + { + "type": "func", + "name": "FixedDiv", + "return_typename": "int", + "argument_typenames": [ + "int", + "int" + ] + } + ] +} \ No newline at end of file diff --git a/experiments/metadata/doom/am_map_2.json b/experiments/metadata/doom/am_map_2.json new file mode 100644 index 0000000..53675ae --- /dev/null +++ b/experiments/metadata/doom/am_map_2.json @@ -0,0 +1,75 @@ +{ + "name": "am_map_2", + "snippet": "am_map_2", + "function_name": "AM_activateNewScale", + "function_path": "/linuxdoom-1.10/am_map.c", + "language": "c", + + "errors": { + "use_undeclared_identifier": [ + "m_x", "m_w", "m_y", "m_h", "f_w", "f_h", "m_x2", "m_y2" + ], + "call_undeclared_function": [ + "FTOM" + ] + }, + + "declarations": [ + { + "type": "var", + "name": "m_x", + "typename": "int" + }, + + { + "type": "var", + "name": "m_w", + "typename": "int" + }, + + { + "type": "var", + "name": "m_y", + "typename": "int" + }, + + { + "type": "var", + "name": "m_h", + "typename": "int" + }, + + { + "type": "var", + "name": "f_w", + "typename": "int" + }, + + { + "type": "var", + "name": "f_h", + "typename": "int" + }, + + { + "type": "var", + "name": "m_x2", + "typename": "int" + }, + + { + "type": "var", + "name": "m_y2", + "typename": "int" + }, + + { + "type": "func", + "name": "FTOM", + "return_typename": "int", + "argument_typenames": [ + "int" + ] + } + ] +} \ No newline at end of file diff --git a/experiments/metadata/doom/am_map_3.json b/experiments/metadata/doom/am_map_3.json new file mode 100644 index 0000000..f42c1a6 --- /dev/null +++ b/experiments/metadata/doom/am_map_3.json @@ -0,0 +1,71 @@ +{ + "name": "am_map_3", + "snippet": "am_map_3", + "function_name": "AM_saveScaleAndLoc", + "function_path": "/linuxdoom-1.10/am_map.c", + "language": "c", + + + "errors": { + "use_undeclared_identifier": [ + "old_m_x", + "m_x", + "old_m_y", + "m_y", + "old_m_w", + "m_w", + "old_m_h", + "m_h" + ] + }, + + "declarations": [ + { + "type": "var", + "name": "old_m_x", + "typename": "UNRESOLVED" + }, + + { + "type": "var", + "name": "m_x", + "typename": "UNRESOLVED" + }, + + { + "type": "var", + "name": "old_m_y", + "typename": "UNRESOLVED" + }, + + { + "type": "var", + "name": "m_y", + "typename": "UNRESOLVED" + }, + + { + "type": "var", + "name": "old_m_w", + "typename": "UNRESOLVED" + }, + + { + "type": "var", + "name": "m_w", + "typename": "UNRESOLVED" + }, + + { + "type": "var", + "name": "old_m_h", + "typename": "UNRESOLVED" + }, + + { + "type": "var", + "name": "m_h", + "typename": "UNRESOLVED" + } + ] +} \ No newline at end of file diff --git a/experiments/metadata/doom/am_map_4.json b/experiments/metadata/doom/am_map_4.json new file mode 100644 index 0000000..59419c9 --- /dev/null +++ b/experiments/metadata/doom/am_map_4.json @@ -0,0 +1,160 @@ +{ + "name": "am_map_4", + "snippet": "am_map_4", + "function_name": "AM_restoreScaleAndLoc", + "function_path": "/linuxdoom-1.10/am_map.c", + "language": "c", + + "errors": { + "use_undeclared_identifier": [ + "m_w", "old_m_w", "m_h", "old_m_h", "followplayer", + "m_x", "old_m_x", "m_y", "old_m_y", "plr", "m_x2", "m_y2", + "scale_mtof", "f_w", "FRACBITS", "scale_ftom", "FRACUNIT" + ], + "call_undeclared_function": [ + "FixedDiv" + ] + }, + + "declarations": [ + { + "type": "var", + "name": "m_w", + "typename": "int" + }, + + { + "type": "var", + "name": "old_m_w", + "typename": "int" + }, + + { + "type": "var", + "name": "m_h", + "typename": "int" + }, + + { + "type": "var", + "name": "old_m_h", + "typename": "int" + }, + + { + "type": "var", + "name": "followplayer", + "typename": "int" + }, + + { + "type": "var", + "name": "m_x", + "typename": "int" + }, + + { + "type": "var", + "name": "old_m_x", + "typename": "int" + }, + + { + "type": "var", + "name": "m_y", + "typename": "int" + }, + + { + "type": "var", + "name": "old_m_y", + "typename": "int" + }, + + { + "type": "struct", + "name": "TYPE1", + "members": [ + { + "type": "memb", + "name": "x", + "typename": "int" + }, + { + "type": "memb", + "name": "y", + "typename": "int" + } + ] + }, + + { + "type": "struct", + "name": "TYPE2", + "members": [ + { + "type": "memb", + "name": "mo", + "typename": "TYPE1*" + } + ] + }, + + { + "type": "var", + "name": "plr", + "typename": "TYPE2*" + }, + + { + "type": "var", + "name": "m_x2", + "typename": "int" + }, + + { + "type": "var", + "name": "m_y2", + "typename": "int" + }, + + { + "type": "var", + "name": "scale_mtof", + "typename": "int" + }, + + { + "type": "var", + "name": "scale_ftom", + "typename": "int" + }, + + { + "type": "var", + "name": "f_w", + "typename": "int" + }, + + { + "type": "var", + "name": "FRACBITS", + "typename": "int" + }, + + { + "type": "var", + "name": "FRACUNIT", + "typename": "int" + }, + + { + "type": "func", + "name": "FixedDiv", + "return_typename": "int", + "argument_typenames": [ + "int", "int" + ] + } + ] +} \ No newline at end of file diff --git a/experiments/metadata/doom/am_map_5.json b/experiments/metadata/doom/am_map_5.json new file mode 100644 index 0000000..ce51a74 --- /dev/null +++ b/experiments/metadata/doom/am_map_5.json @@ -0,0 +1,36 @@ +{ + "name": "am_map_5", + "snippet": "am_map_5", + "function_name": "AM_addMark", + "function_path": "/linuxdoom-1.10/am_map.c", + "language": "c", + + "errors": { + "use_undeclared_identifier": [ + "markpoints", "markpointnum", "m_x", "m_w", "m_y", "m_h", "AM_NUMMARKPOINTS" + ] + }, + + "declarations": [ + { + "type": "var", + "name": "markpointnum", + "typename": "int" + }, + + { + "type": "struct", + "name": "TYPE1", + "members": [ + { + "typename": "int", + "name": "x" + }, + { + "typename": "int", + "name": "y" + } + ] + } + ] +} \ No newline at end of file diff --git a/experiments/metadata/doom/am_map_6.json b/experiments/metadata/doom/am_map_6.json new file mode 100644 index 0000000..41c46e8 --- /dev/null +++ b/experiments/metadata/doom/am_map_6.json @@ -0,0 +1,149 @@ +{ + "name": "am_map_6", + "snippet": "am_map_6", + "function_name": "AM_findMinMaxBoundaries", + "function_path": "/linuxdoom-1.10/am_map.c", + "language": "c", + "errors": { + "use_undeclared_identifier": [ + "max_y", + "max_x", + "fixed_t", + "FRACBITS", + "vertexes", + "min_w", + "numvertexes", + "f_h", + "MAXINT", + "min_scale_mtof", + "PLAYERRADIUS", + "max_scale_mtof", + "max_w", + "f_w", + "max_h", + "min_y", + "min_h", + "min_x", + "a", + "b" + ], + "call_undeclared_function": [ + "FixedDiv" + ] + }, + "declarations": [ + { + "type": "alias", + "name": "fixed_t", + "typename": "int" + }, + { + "type": "var", + "name": "min_x", + "typename": "int" + }, + { + "type": "var", + "name": "min_y", + "typename": "int" + }, + { + "type": "var", + "name": "MAXINT", + "typename": "int" + }, + { + "type": "var", + "name": "max_x", + "typename": "int" + }, + { + "type": "var", + "name": "max_y", + "typename": "int" + }, + { + "type": "var", + "name": "numvertexes", + "typename": "int" + }, + { + "type": "struct", + "name": "TYPE1", + "members": [ + { + "name": "x", + "typename": "int" + }, + { + "name": "y", + "typename": "int" + } + ] + }, + { + "type": "var", + "name": "vertexes", + "typename": "TYPE1[]" + }, + { + "type": "var", + "name": "max_w", + "typename": "int" + }, + { + "type": "var", + "name": "max_h", + "typename": "int" + }, + { + "type": "var", + "name": "min_w", + "typename": "int" + }, + { + "type": "var", + "name": "min_h", + "typename": "int" + }, + { + "type": "var", + "name": "PLAYERRADIUS", + "typename": "int" + }, + { + "type": "func", + "name": "FixedDiv", + "return_typename": "int", + "argument_typenames": [ + "int", + "int" + ] + }, + { + "type": "var", + "name": "f_w", + "typename": "int" + }, + { + "type": "var", + "name": "FRACBITS", + "typename": "int" + }, + { + "type": "var", + "name": "f_h", + "typename": "int" + }, + { + "type": "var", + "name": "min_scale_mtof", + "typename": "int" + }, + { + "type": "var", + "name": "max_scale_mtof", + "typename": "int" + } + ] +} \ No newline at end of file diff --git a/experiments/metadata/doom/project.json b/experiments/metadata/doom/project.json new file mode 100644 index 0000000..c4e7c2f --- /dev/null +++ b/experiments/metadata/doom/project.json @@ -0,0 +1,4 @@ +{ + "project": "DOOM", + "org": "id-Software" +} \ No newline at end of file diff --git a/experiments/snippets/doom/am_map_1 b/experiments/snippets/doom/am_map_1 new file mode 100644 index 0000000..61a539c --- /dev/null +++ b/experiments/snippets/doom/am_map_1 @@ -0,0 +1,14 @@ +// Calculates the slope and slope according to the x-axis of a line +// segment in map coordinates (with the upright y-axis n' all) so +// that it can be used with the brain-dead drawing stuff. +void AM_getIslope(mline_t *ml, islope_t *is) +{ + int dx, dy; + + dy = ml->a.y - ml->b.y; + dx = ml->b.x - ml->a.x; + if (!dy) is->islp = (dx<0?-MAXINT:MAXINT); + else is->islp = FixedDiv(dx, dy); + if (!dx) is->slp = (dy<0?-MAXINT:MAXINT); + else is->slp = FixedDiv(dy, dx); +} \ No newline at end of file diff --git a/experiments/snippets/doom/am_map_2 b/experiments/snippets/doom/am_map_2 new file mode 100644 index 0000000..47b6daf --- /dev/null +++ b/experiments/snippets/doom/am_map_2 @@ -0,0 +1,11 @@ +void AM_activateNewScale(void) +{ + m_x += m_w/2; + m_y += m_h/2; + m_w = FTOM(f_w); + m_h = FTOM(f_h); + m_x -= m_w/2; + m_y -= m_h/2; + m_x2 = m_x + m_w; + m_y2 = m_y + m_h; +} \ No newline at end of file diff --git a/experiments/snippets/doom/am_map_3 b/experiments/snippets/doom/am_map_3 new file mode 100644 index 0000000..fa5972f --- /dev/null +++ b/experiments/snippets/doom/am_map_3 @@ -0,0 +1,7 @@ +void AM_saveScaleAndLoc(void) +{ + old_m_x = m_x; + old_m_y = m_y; + old_m_w = m_w; + old_m_h = m_h; +} \ No newline at end of file diff --git a/experiments/snippets/doom/am_map_4 b/experiments/snippets/doom/am_map_4 new file mode 100644 index 0000000..e9b3bea --- /dev/null +++ b/experiments/snippets/doom/am_map_4 @@ -0,0 +1,18 @@ +void AM_restoreScaleAndLoc(void) +{ + m_w = old_m_w; + m_h = old_m_h; + if (!followplayer) { + m_x = old_m_x; + m_y = old_m_y; + } else { + m_x = plr->mo->x - m_w/2; + m_y = plr->mo->y - m_h/2; + } + m_x2 = m_x + m_w; + m_y2 = m_y + m_h; + + // Change the scaling multipliers + scale_mtof = FixedDiv(f_w< max_x) + max_x = vertexes[i].x; + + if (vertexes[i].y < min_y) + min_y = vertexes[i].y; + else if (vertexes[i].y > max_y) + max_y = vertexes[i].y; + } + + max_w = max_x - min_x; + max_h = max_y - min_y; + + min_w = 2*PLAYERRADIUS; // const? never changed? + min_h = 2*PLAYERRADIUS; + + a = FixedDiv(f_w<powers, 0, sizeof (p->powers)); + memset (p->cards, 0, sizeof (p->cards)); + p->mo->flags &= ~MF_SHADOW; // cancel invisibility + p->extralight = 0; // cancel gun flashes + p->fixedcolormap = 0; // cancel ir gogles + p->damagecount = 0; // no palette changes + p->bonuscount = 0; +} \ No newline at end of file diff --git a/experiments/snippets/doom/g_game_3 b/experiments/snippets/doom/g_game_3 new file mode 100644 index 0000000..21d519a --- /dev/null +++ b/experiments/snippets/doom/g_game_3 @@ -0,0 +1,34 @@ +void G_PlayerReborn(int player) +{ + player_t* p; + int i; + int frags[MAXPLAYERS]; + int killcount; + int itemcount; + int secretcount; + + memcpy (frags,players[player].frags,sizeof(frags)); + killcount = players[player].killcount; + itemcount = players[player].itemcount; + secretcount = players[player].secretcount; + + p = &players[player]; + memset (p, 0, sizeof(*p)); + + memcpy (players[player].frags, frags, sizeof(players[player].frags)); + players[player].killcount = killcount; + players[player].itemcount = itemcount; + players[player].secretcount = secretcount; + + p->usedown = p->attackdown = true; // don't do anything immediately + p->playerstate = PST_LIVE; + p->health = MAXHEALTH; + p->readyweapon = p->pendingweapon = wp_pistol; + p->weaponowned[wp_fist] = true; + p->weaponowned[wp_pistol] = true; + p->ammo[am_clip] = 50; + + for (i=0 ; imaxammo[i] = maxammo[i]; + +} \ No newline at end of file diff --git a/experiments/snippets/doom/i_video_1 b/experiments/snippets/doom/i_video_1 new file mode 100644 index 0000000..3c76a27 --- /dev/null +++ b/experiments/snippets/doom/i_video_1 @@ -0,0 +1,69 @@ +// +// Translates the key currently in X_event +// + +int xlatekey(void) +{ + + int rc; + + switch(rc = XKeycodeToKeysym(X_display, X_event.xkey.keycode, 0)) + { + case XK_Left: rc = KEY_LEFTARROW; break; + case XK_Right: rc = KEY_RIGHTARROW; break; + case XK_Down: rc = KEY_DOWNARROW; break; + case XK_Up: rc = KEY_UPARROW; break; + case XK_Escape: rc = KEY_ESCAPE; break; + case XK_Return: rc = KEY_ENTER; break; + case XK_Tab: rc = KEY_TAB; break; + case XK_F1: rc = KEY_F1; break; + case XK_F2: rc = KEY_F2; break; + case XK_F3: rc = KEY_F3; break; + case XK_F4: rc = KEY_F4; break; + case XK_F5: rc = KEY_F5; break; + case XK_F6: rc = KEY_F6; break; + case XK_F7: rc = KEY_F7; break; + case XK_F8: rc = KEY_F8; break; + case XK_F9: rc = KEY_F9; break; + case XK_F10: rc = KEY_F10; break; + case XK_F11: rc = KEY_F11; break; + case XK_F12: rc = KEY_F12; break; + + case XK_BackSpace: + case XK_Delete: rc = KEY_BACKSPACE; break; + + case XK_Pause: rc = KEY_PAUSE; break; + + case XK_KP_Equal: + case XK_equal: rc = KEY_EQUALS; break; + + case XK_KP_Subtract: + case XK_minus: rc = KEY_MINUS; break; + + case XK_Shift_L: + case XK_Shift_R: + rc = KEY_RSHIFT; + break; + + case XK_Control_L: + case XK_Control_R: + rc = KEY_RCTRL; + break; + + case XK_Alt_L: + case XK_Meta_L: + case XK_Alt_R: + case XK_Meta_R: + rc = KEY_RALT; + break; + + default: + if (rc >= XK_space && rc <= XK_asciitilde) + rc = rc - XK_space + ' '; + if (rc >= 'A' && rc <= 'Z') + rc = rc - 'A' + 'a'; + break; + } + + return rc; +} \ No newline at end of file diff --git a/experiments/snippets/doom/i_video_2 b/experiments/snippets/doom/i_video_2 new file mode 100644 index 0000000..f0ddc4b --- /dev/null +++ b/experiments/snippets/doom/i_video_2 @@ -0,0 +1,12 @@ +void I_ShutdownGraphics(void) { + // Detach from X server + if (!XShmDetach(X_display, &X_shminfo)) + I_Error("XShmDetach() failed in I_ShutdownGraphics()"); + + // Release shared memory. + shmdt(X_shminfo.shmaddr); + shmctl(X_shminfo.shmid, IPC_RMID, 0); + + // Paranoia. + image->data = NULL; +} \ No newline at end of file diff --git a/experiments/snippets/doom/i_video_3 b/experiments/snippets/doom/i_video_3 new file mode 100644 index 0000000..4b7dae8 --- /dev/null +++ b/experiments/snippets/doom/i_video_3 @@ -0,0 +1,27 @@ +void I_StartTic(void) { + + if (!X_display) return; + + while (XPending(X_display)) + I_GetEvent(); + + // Warp the pointer back to the middle of the window + // or it will wander off - that is, the game will + // loose input focus within X11. + if (grabMouse) + { + if (!--doPointerWarp) + { + XWarpPointer(X_display, + None, + X_mainWindow, + 0, 0, + 0, 0, + X_width/2, X_height/2); + + doPointerWarp = POINTER_WARP_COUNTDOWN; + } + } + + mousemoved = false; +} \ No newline at end of file