1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-09-08 17:54:40 +00:00

Working on a C preprocessor proof-of-concept at the lexer level. #169

This commit is contained in:
jespergravgaard 2020-04-01 12:56:28 +02:00
parent bc4dd9ea47
commit 9eb17e7ed1
5 changed files with 39 additions and 17 deletions

View File

@ -39,4 +39,4 @@ IDENTIFIER: [a-zA-Z_]+ ;
NUMBER: [0-9]+ ;
DEFINE: '#define' ;
DEFINE_CONTINUE: '\\\n' ;
WHITESPACE: [ \t\r\n]+ -> skip ;
WHITESPACE: [ \t\r\n]+ -> channel(1) ; // skip; //

View File

@ -53,4 +53,4 @@ mode names:
DEFAULT_MODE
atn:
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 15, 80, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 5, 9, 51, 10, 9, 3, 10, 6, 10, 54, 10, 10, 13, 10, 14, 10, 55, 3, 11, 6, 11, 59, 10, 11, 13, 11, 14, 11, 60, 3, 12, 3, 12, 3, 12, 3, 12, 3, 12, 3, 12, 3, 12, 3, 12, 3, 13, 3, 13, 3, 13, 3, 14, 6, 14, 75, 10, 14, 13, 14, 14, 14, 76, 3, 14, 3, 14, 2, 2, 15, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 27, 15, 3, 2, 5, 5, 2, 67, 92, 97, 97, 99, 124, 3, 2, 50, 59, 5, 2, 11, 12, 15, 15, 34, 34, 2, 83, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 2, 27, 3, 2, 2, 2, 3, 29, 3, 2, 2, 2, 5, 31, 3, 2, 2, 2, 7, 33, 3, 2, 2, 2, 9, 35, 3, 2, 2, 2, 11, 37, 3, 2, 2, 2, 13, 39, 3, 2, 2, 2, 15, 41, 3, 2, 2, 2, 17, 50, 3, 2, 2, 2, 19, 53, 3, 2, 2, 2, 21, 58, 3, 2, 2, 2, 23, 62, 3, 2, 2, 2, 25, 70, 3, 2, 2, 2, 27, 74, 3, 2, 2, 2, 29, 30, 7, 61, 2, 2, 30, 4, 3, 2, 2, 2, 31, 32, 7, 42, 2, 2, 32, 6, 3, 2, 2, 2, 33, 34, 7, 43, 2, 2, 34, 8, 3, 2, 2, 2, 35, 36, 7, 44, 2, 2, 36, 10, 3, 2, 2, 2, 37, 38, 7, 49, 2, 2, 38, 12, 3, 2, 2, 2, 39, 40, 7, 45, 2, 2, 40, 14, 3, 2, 2, 2, 41, 42, 7, 47, 2, 2, 42, 16, 3, 2, 2, 2, 43, 44, 7, 101, 2, 2, 44, 45, 7, 106, 2, 2, 45, 46, 7, 99, 2, 2, 46, 51, 7, 116, 2, 2, 47, 48, 7, 107, 2, 2, 48, 49, 7, 112, 2, 2, 49, 51, 7, 118, 2, 2, 50, 43, 3, 2, 2, 2, 50, 47, 3, 2, 2, 2, 51, 18, 3, 2, 2, 2, 52, 54, 9, 2, 2, 2, 53, 52, 3, 2, 2, 2, 54, 55, 3, 2, 2, 2, 55, 53, 3, 2, 2, 2, 55, 56, 3, 2, 2, 2, 56, 20, 3, 2, 2, 2, 57, 59, 9, 3, 2, 2, 58, 57, 3, 2, 2, 2, 59, 60, 3, 2, 2, 2, 60, 58, 3, 2, 2, 2, 60, 61, 3, 2, 2, 2, 61, 22, 3, 2, 2, 2, 62, 63, 7, 37, 2, 2, 63, 64, 7, 102, 2, 2, 64, 65, 7, 103, 2, 2, 65, 66, 7, 104, 2, 2, 66, 67, 7, 107, 2, 2, 67, 68, 7, 112, 2, 2, 68, 69, 7, 103, 2, 2, 69, 24, 3, 2, 2, 2, 70, 71, 7, 94, 2, 2, 71, 72, 7, 12, 2, 2, 72, 26, 3, 2, 2, 2, 73, 75, 9, 4, 2, 2, 74, 73, 3, 2, 2, 2, 75, 76, 3, 2, 2, 2, 76, 74, 3, 2, 2, 2, 76, 77, 3, 2, 2, 2, 77, 78, 3, 2, 2, 2, 78, 79, 8, 14, 2, 2, 79, 28, 3, 2, 2, 2, 7, 2, 50, 55, 60, 76, 3, 8, 2, 2]
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 15, 80, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 3, 9, 5, 9, 51, 10, 9, 3, 10, 6, 10, 54, 10, 10, 13, 10, 14, 10, 55, 3, 11, 6, 11, 59, 10, 11, 13, 11, 14, 11, 60, 3, 12, 3, 12, 3, 12, 3, 12, 3, 12, 3, 12, 3, 12, 3, 12, 3, 13, 3, 13, 3, 13, 3, 14, 6, 14, 75, 10, 14, 13, 14, 14, 14, 76, 3, 14, 3, 14, 2, 2, 15, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 27, 15, 3, 2, 5, 5, 2, 67, 92, 97, 97, 99, 124, 3, 2, 50, 59, 5, 2, 11, 12, 15, 15, 34, 34, 2, 83, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 2, 27, 3, 2, 2, 2, 3, 29, 3, 2, 2, 2, 5, 31, 3, 2, 2, 2, 7, 33, 3, 2, 2, 2, 9, 35, 3, 2, 2, 2, 11, 37, 3, 2, 2, 2, 13, 39, 3, 2, 2, 2, 15, 41, 3, 2, 2, 2, 17, 50, 3, 2, 2, 2, 19, 53, 3, 2, 2, 2, 21, 58, 3, 2, 2, 2, 23, 62, 3, 2, 2, 2, 25, 70, 3, 2, 2, 2, 27, 74, 3, 2, 2, 2, 29, 30, 7, 61, 2, 2, 30, 4, 3, 2, 2, 2, 31, 32, 7, 42, 2, 2, 32, 6, 3, 2, 2, 2, 33, 34, 7, 43, 2, 2, 34, 8, 3, 2, 2, 2, 35, 36, 7, 44, 2, 2, 36, 10, 3, 2, 2, 2, 37, 38, 7, 49, 2, 2, 38, 12, 3, 2, 2, 2, 39, 40, 7, 45, 2, 2, 40, 14, 3, 2, 2, 2, 41, 42, 7, 47, 2, 2, 42, 16, 3, 2, 2, 2, 43, 44, 7, 101, 2, 2, 44, 45, 7, 106, 2, 2, 45, 46, 7, 99, 2, 2, 46, 51, 7, 116, 2, 2, 47, 48, 7, 107, 2, 2, 48, 49, 7, 112, 2, 2, 49, 51, 7, 118, 2, 2, 50, 43, 3, 2, 2, 2, 50, 47, 3, 2, 2, 2, 51, 18, 3, 2, 2, 2, 52, 54, 9, 2, 2, 2, 53, 52, 3, 2, 2, 2, 54, 55, 3, 2, 2, 2, 55, 53, 3, 2, 2, 2, 55, 56, 3, 2, 2, 2, 56, 20, 3, 2, 2, 2, 57, 59, 9, 3, 2, 2, 58, 57, 3, 2, 2, 2, 59, 60, 3, 2, 2, 2, 60, 58, 3, 2, 2, 2, 60, 61, 3, 2, 2, 2, 61, 22, 3, 2, 2, 2, 62, 63, 7, 37, 2, 2, 63, 64, 7, 102, 2, 2, 64, 65, 7, 103, 2, 2, 65, 66, 7, 104, 2, 2, 66, 67, 7, 107, 2, 2, 67, 68, 7, 112, 2, 2, 68, 69, 7, 103, 2, 2, 69, 24, 3, 2, 2, 2, 70, 71, 7, 94, 2, 2, 71, 72, 7, 12, 2, 2, 72, 26, 3, 2, 2, 2, 73, 75, 9, 4, 2, 2, 74, 73, 3, 2, 2, 2, 75, 76, 3, 2, 2, 2, 76, 74, 3, 2, 2, 2, 76, 77, 3, 2, 2, 2, 77, 78, 3, 2, 2, 2, 78, 79, 8, 14, 2, 2, 79, 28, 3, 2, 2, 2, 7, 2, 50, 55, 60, 76, 3, 2, 3, 2]

View File

@ -132,8 +132,8 @@ public class MacrosLexer extends Lexer {
"\24\3\2\2\29;\t\3\2\2:9\3\2\2\2;<\3\2\2\2<:\3\2\2\2<=\3\2\2\2=\26\3\2"+
"\2\2>?\7%\2\2?@\7f\2\2@A\7g\2\2AB\7h\2\2BC\7k\2\2CD\7p\2\2DE\7g\2\2E\30"+
"\3\2\2\2FG\7^\2\2GH\7\f\2\2H\32\3\2\2\2IK\t\4\2\2JI\3\2\2\2KL\3\2\2\2"+
"LJ\3\2\2\2LM\3\2\2\2MN\3\2\2\2NO\b\16\2\2O\34\3\2\2\2\7\2\62\67<L\3\b"+
"\2\2";
"LJ\3\2\2\2LM\3\2\2\2MN\3\2\2\2NO\b\16\2\2O\34\3\2\2\2\7\2\62\67<L\3\2"+
"\3\2";
public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static {

View File

@ -7,6 +7,7 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
@ -14,6 +15,8 @@ import static org.junit.Assert.assertEquals;
public class TestMacrosParser {
public static final int CHANNEL_WHITESPACE = 1;
/**
* Test some parsing without macros
*/
@ -63,32 +66,48 @@ public class TestMacrosParser {
}
});
Map<String, Token> macros = new LinkedHashMap<>();
/*
Map<String, List<Token>> macros = new LinkedHashMap<>();
final ArrayList<Token> finalTokens = new ArrayList<>();
Token token = lexer.nextToken();
while(token.getType() != Token.EOF) {
if(token.getType() == MacrosLexer.DEFINE) {
// Define a new macro
final Token name = lexer.nextToken();
// TODO: Gobble tokens until newline
final Token value = lexer.nextToken();
//System.out.println("Defining " + name.getText() + " to " + value.getText());
macros.put(name.getText(), value);
// Find value by gobbling tokens until the line ends
final ArrayList<Token> macroValue = new ArrayList<>();
boolean macroRead = true;
while(macroRead) {
final Token value = lexer.nextToken();
final String text = value.getText();
if(value.getChannel()== CHANNEL_WHITESPACE && value.getText().contains("\n")) {
macroRead = false;
} else {
macroValue.add(value);
}
}
macros.put(name.getText(), macroValue);
} else if(token.getType()==MacrosLexer.IDENTIFIER && macros.containsKey(token.getText())){
// Unfold a macro
final CommonToken newToken = new CommonToken(token);
final Token unfold = macros.get(token.getText());
newToken.setText(unfold.getText());
newToken.setType(unfold.getType());
newToken.setChannel(unfold.getChannel());
finalTokens.add(newToken);
final List<Token> macroValue = macros.get(token.getText());
for(Token macroToken : macroValue) {
final CommonToken newToken = new CommonToken(token);
newToken.setText(macroToken.getText());
newToken.setType(macroToken.getType());
newToken.setChannel(macroToken.getChannel());
finalTokens.add(newToken);
}
} else {
finalTokens.add(token);
}
token = lexer.nextToken();
}
MacrosParser parser = new MacrosParser(new BufferedTokenStream(new ListTokenSource(finalTokens)));
*/
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
MacrosParser parser = new MacrosParser(tokenStream);
parser.setBuildParseTree(true);
parser.addErrorListener(new BaseErrorListener() {
@Override

View File

@ -23,7 +23,10 @@ char align(0x100) YSIN[0x100] = kickasm {{
}};
char align(0x100) XMOVEMENT[0x200] = kickasm {{
.lohifill $100, round(344-i*344/$100-86*sin(toRadians(360*i/$100)))
//.lohifill $100, round(344-i*344/$100-86*sin(toRadians(360*i/$100)))
//.lohifill $100, round(344-i*344/$100-129*sin(toRadians(360*i/$100)))
.lohifill $100, round(344-i*344/$100 -86*sin(toRadians(360*i/$100)) -43*sin(toRadians(360*i/$80)))
//.lohifill $100, round(344-i*344/$100-86*sin(toRadians(360*i/$80)))
}};
// The high-value table