1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2025-02-19 08:31:01 +00:00

Added modes to lexer.

This commit is contained in:
jespergravgaard 2019-08-25 13:07:21 +02:00
parent cb7469baf3
commit 87268abc2a
11 changed files with 1250 additions and 866 deletions

View File

@ -276,11 +276,13 @@ public class AsmFormat {
.replace(':', '_') .replace(':', '_')
.replace("#", "_") .replace("#", "_")
.replace("$", "_"); .replace("$", "_");
//param = ""+((Registers.RegisterZp) register).getZp();
return param; return param;
} else { } else {
String param = asmName.replace('@', 'b').replace(':', '_').replace("#", "_").replace("$", "_"); String param = asmName
//param = ""+((Registers.RegisterZp) register).getZp(); .replace('@', 'b')
.replace(':', '_')
.replace("#", "_")
.replace("$", "_");
return param; return param;
} }
} }

View File

@ -160,15 +160,15 @@ public class AsmFragmentInstance {
@Override @Override
public Object visitAsmLabelName(KickCParser.AsmLabelNameContext ctx) { public Object visitAsmLabelName(KickCParser.AsmLabelNameContext ctx) {
program.addLine(new AsmLabel(ctx.NAME().getText())); program.addLine(new AsmLabel(ctx.ASM_NAME().getText()));
return null; return null;
} }
@Override @Override
public Object visitAsmLabelMulti(KickCParser.AsmLabelMultiContext ctx) { public Object visitAsmLabelMulti(KickCParser.AsmLabelMultiContext ctx) {
String label = "!"; String label = "!";
if(ctx.NAME()!=null) { if(ctx.ASM_NAME()!=null) {
label = label + ctx.NAME().getText(); label = label + ctx.ASM_NAME().getText();
} }
program.addLine(new AsmLabel(label)); program.addLine(new AsmLabel(label));
return null; return null;
@ -306,7 +306,7 @@ public class AsmFragmentInstance {
@Override @Override
public AsmParameter visitAsmExprInt(KickCParser.AsmExprIntContext ctx) { public AsmParameter visitAsmExprInt(KickCParser.AsmExprIntContext ctx) {
Number number = NumberParser.parseLiteral(ctx.NUMBER().getText()); Number number = NumberParser.parseLiteral(ctx.ASM_NUMBER().getText());
boolean isZp = SymbolType.BYTE.contains(number.longValue()) || SymbolType.SBYTE.contains(number.longValue()); boolean isZp = SymbolType.BYTE.contains(number.longValue()) || SymbolType.SBYTE.contains(number.longValue());
String param = AsmFormat.getAsmNumber(number); String param = AsmFormat.getAsmNumber(number);
return new AsmParameter(param, isZp); return new AsmParameter(param, isZp);
@ -319,7 +319,7 @@ public class AsmFragmentInstance {
@Override @Override
public AsmParameter visitAsmExprLabel(KickCParser.AsmExprLabelContext ctx) { public AsmParameter visitAsmExprLabel(KickCParser.AsmExprLabelContext ctx) {
String param = ctx.NAME().getSymbol().getText(); String param = ctx.ASM_NAME().getSymbol().getText();
return new AsmParameter(param, false); return new AsmParameter(param, false);
} }
@ -331,7 +331,7 @@ public class AsmFragmentInstance {
@Override @Override
public AsmParameter visitAsmExprReplace(KickCParser.AsmExprReplaceContext ctx) { public AsmParameter visitAsmExprReplace(KickCParser.AsmExprReplaceContext ctx) {
String replaceName = ctx.NAME().getSymbol().getText(); String replaceName = ctx.ASM_NAME().getSymbol().getText();
return bindings.getBoundValue(replaceName); return bindings.getBoundValue(replaceName);
} }
} }

View File

@ -16,6 +16,7 @@ public class AsmParser {
CodePointCharStream fragmentCharStream = CharStreams.fromString(body); CodePointCharStream fragmentCharStream = CharStreams.fromString(body);
CParser cParser = new CParser(null); CParser cParser = new CParser(null);
KickCLexer kickCLexer = new KickCLexer(fragmentCharStream, cParser); KickCLexer kickCLexer = new KickCLexer(fragmentCharStream, cParser);
kickCLexer.pushMode(KickCLexer.ASM_MODE);
KickCParser kickCParser = new KickCParser(new CommonTokenStream(kickCLexer), cParser); KickCParser kickCParser = new KickCParser(new CommonTokenStream(kickCLexer), cParser);
kickCParser.addErrorListener(new BaseErrorListener() { kickCParser.addErrorListener(new BaseErrorListener() {
@Override @Override

View File

@ -41,9 +41,6 @@ public class CParser {
/** Names of typedefs. Used by lexer to know the difference between normal value IDENTIFIERS and TYPEIDENTIFIERS */ /** Names of typedefs. Used by lexer to know the difference between normal value IDENTIFIERS and TYPEIDENTIFIERS */
private List<String> typedefs; private List<String> typedefs;
/** True whenever the lexer/parser is parsing ASM. Enables/disables a lexer rules that might interfere.*/
private boolean modeAsm;
/** True whenever the lexer is expecting an import filename as the next token. */ /** True whenever the lexer is expecting an import filename as the next token. */
private boolean modeImport; private boolean modeImport;
@ -90,14 +87,6 @@ public class CParser {
return typedefs.contains(identifier); return typedefs.contains(identifier);
} }
public boolean isModeAsm() {
return modeAsm;
}
public void setModeAsm(boolean asm) {
modeAsm = asm;
}
public boolean isModeImport() { public boolean isModeImport() {
return modeImport; return modeImport;
} }

View File

@ -9,6 +9,9 @@ tokens { TYPEDEFNAME }
@lexer::members { @lexer::members {
CParser cParser; CParser cParser;
boolean asmEnter = false;
int asmCurlyCount = 0;
public KickCLexer(CharStream input, CParser cParser) { public KickCLexer(CharStream input, CParser cParser) {
this(input); this(input);
this.cParser = cParser; this.cParser = cParser;
@ -16,7 +19,7 @@ tokens { TYPEDEFNAME }
} }
// Special characters // Special characters
CURLY_BEGIN: '{' ; CURLY_BEGIN: '{' { if(asmEnter) { pushMode(ASM_MODE); asmEnter=false; } } ;
CURLY_END: '}' ; CURLY_END: '}' ;
BRACKET_BEGIN : '[' ; BRACKET_BEGIN : '[' ;
BRACKET_END : ']' ; BRACKET_END : ']' ;
@ -81,7 +84,7 @@ SWITCH: 'switch' ;
RETURN: 'return' ; RETURN: 'return' ;
BREAK: 'break' ; BREAK: 'break' ;
CONTINUE: 'continue' ; CONTINUE: 'continue' ;
ASM: 'asm' ; ASM: 'asm' { asmEnter=true; };
DEFAULT : 'default' ; DEFAULT : 'default' ;
CASE : 'case' ; CASE : 'case' ;
STRUCT : 'struct' ; STRUCT : 'struct' ;
@ -99,15 +102,7 @@ SIGNEDNESS : 'signed' | 'unsigned' ;
SIMPLETYPE: 'byte' | 'word' | 'dword' | 'bool' | 'char' | 'short' | 'int' | 'long' | 'void' ; SIMPLETYPE: 'byte' | 'word' | 'dword' | 'bool' | 'char' | 'short' | 'int' | 'long' | 'void' ;
BOOLEAN : 'true' | 'false'; BOOLEAN : 'true' | 'false';
KICKASM_BODY: '{{' .*? '}}'; KICKASM_BODY: '{{' .*? '}}';
ASM_IMM : '#' ;
ASM_BYTE : '.byte' ;
ASM_MNEMONIC:
'brk' | 'ora' | 'kil' | 'slo' | 'nop' | 'asl' | 'php' | 'anc' | 'bpl' | 'clc' | 'jsr' | 'and' | 'rla' | 'bit' | 'rol' | 'pla' | 'plp' | 'bmi' | 'sec' |
'rti' | 'eor' | 'sre' | 'lsr' | 'pha' | 'alr' | 'jmp' | 'bvc' | 'cli' | 'rts' | 'adc' | 'rra' | 'bvs' | 'sei' | 'sax' | 'sty' | 'sta' | 'stx' | 'dey' |
'txa' | 'xaa' | 'bcc' | 'ahx' | 'tya' | 'txs' | 'tas' | 'shy' | 'shx' | 'ldy' | 'lda' | 'ldx' | 'lax' | 'tay' | 'tax' | 'bcs' | 'clv' | 'tsx' | 'las' |
'cpy' | 'cmp' | 'cpx' | 'dcp' | 'dec' | 'inc' | 'axs' | 'bne' | 'cld' | 'sbc' | 'isc' | 'inx' | 'beq' | 'sed' | 'dex' | 'iny' | 'ror'
;
ASM_REL: '!' NAME_CHAR* [+-]+ {cParser.isModeAsm()}? ;
// Strings and chars - with special handling of imports // Strings and chars - with special handling of imports
STRING : '"' ('\\"' | ~'"')* '"' [z]?([ps][mu]?)?[z]? { if(cParser.isModeImport()) { cParser.setModeImport(false); cParser.loadCFile(getText()); } } ; STRING : '"' ('\\"' | ~'"')* '"' [z]?([ps][mu]?)?[z]? { if(cParser.isModeImport()) { cParser.setModeImport(false); cParser.loadCFile(getText()); } } ;
@ -136,4 +131,63 @@ fragment NAME_CHAR : [a-zA-Z0-9_];
WS : [ \t\r\n\u00a0]+ -> channel(1); WS : [ \t\r\n\u00a0]+ -> channel(1);
// Comments on hidden channel 2 // Comments on hidden channel 2
COMMENT_LINE : '//' ~[\r\n]* -> channel(2); COMMENT_LINE : '//' ~[\r\n]* -> channel(2);
COMMENT_BLOCK : '/*' .*? '*/' -> channel(2); COMMENT_BLOCK : '/*' .*? '*/' -> channel(2);
// MODE FOR INLINE ASSEMBLER
mode ASM_MODE;
ASM_BYTE : '.byte' ;
ASM_MNEMONIC:
'brk' | 'ora' | 'kil' | 'slo' | 'nop' | 'asl' | 'php' | 'anc' | 'bpl' | 'clc' | 'jsr' | 'and' | 'rla' | 'bit' | 'rol' | 'pla' | 'plp' | 'bmi' | 'sec' |
'rti' | 'eor' | 'sre' | 'lsr' | 'pha' | 'alr' | 'jmp' | 'bvc' | 'cli' | 'rts' | 'adc' | 'rra' | 'bvs' | 'sei' | 'sax' | 'sty' | 'sta' | 'stx' | 'dey' |
'txa' | 'xaa' | 'bcc' | 'ahx' | 'tya' | 'txs' | 'tas' | 'shy' | 'shx' | 'ldy' | 'lda' | 'ldx' | 'lax' | 'tay' | 'tax' | 'bcs' | 'clv' | 'tsx' | 'las' |
'cpy' | 'cmp' | 'cpx' | 'dcp' | 'dec' | 'inc' | 'axs' | 'bne' | 'cld' | 'sbc' | 'isc' | 'inx' | 'beq' | 'sed' | 'dex' | 'iny' | 'ror'
;
ASM_REL: '!' ASM_NAME_CHAR* [+-]+ ;
ASM_IMM : '#' ;
ASM_COLON : ':';
ASM_EXCL : '!';
ASM_COMMA : ',' ;
ASM_PAR_BEGIN : '(' ;
ASM_PAR_END : ')' ;
ASM_BRACKET_BEGIN: '[' ;
ASM_BRACKET_END: ']' ;
ASM_DOT : '.' ;
ASM_SHIFT_LEFT : '<<' ;
ASM_SHIFT_RIGHT : '>>' ;
ASM_PLUS : '+' ;
ASM_MINUS : '-' ;
ASM_LESS_THAN : '<' ;
ASM_GREATER_THAN : '>' ;
ASM_MULTIPLY : '*' ;
ASM_DIVIDE : '/' ;
ASM_CURLY_BEGIN : '{' { asmCurlyCount++; };
ASM_CURLY_END : '}' { asmCurlyCount--; if(asmCurlyCount<0) { popMode(); } };
// Numbers
ASM_NUMBER : ASM_NUMFLOAT | ASM_NUMINT ;
ASM_NUMFLOAT : ASM_BINFLOAT | ASM_DECFLOAT | ASM_HEXFLOAT;
ASM_BINFLOAT : '%' (ASM_BINDIGIT)* '.' ASM_BINDIGIT+;
ASM_DECFLOAT : (ASM_DECDIGIT)* '.' ASM_DECDIGIT+;
ASM_HEXFLOAT : '$' (ASM_HEXDIGIT)* '.' ASM_HEXDIGIT+;
ASM_NUMINT : (ASM_DECINTEGER | ASM_HEXINTEGER | ASM_BININTEGER ) ;
ASM_BININTEGER : '%' ASM_BINDIGIT+ ;
ASM_DECINTEGER : ASM_DECDIGIT+ ;
ASM_HEXINTEGER : '$' ASM_HEXDIGIT+ ;
fragment ASM_BINDIGIT : [0-1];
fragment ASM_DECDIGIT : [0-9];
fragment ASM_HEXDIGIT : [0-9a-fA-F];
ASM_CHAR : '\'' ('\\'['"rfn] | ~'\'' ) '\'';
//Names
ASM_NAME : ASM_NAME_START ASM_NAME_CHAR* ;
fragment ASM_NAME_START : [a-zA-Z_];
fragment ASM_NAME_CHAR : [a-zA-Z0-9_];
// White space on hidden channel 1
ASM_WS : [ \t\r\n\u00a0]+ -> channel(1);
// Comments on hidden channel 2
ASM_COMMENT_LINE : '//' ~[\r\n]* -> channel(2);
ASM_COMMENT_BLOCK : '/*' .*? '*/' -> channel(2);

File diff suppressed because it is too large Load Diff

View File

@ -80,42 +80,61 @@ SIGNEDNESS=79
SIMPLETYPE=80 SIMPLETYPE=80
BOOLEAN=81 BOOLEAN=81
KICKASM_BODY=82 KICKASM_BODY=82
ASM_IMM=83 STRING=83
ASM_BYTE=84 CHAR=84
ASM_MNEMONIC=85 NUMBER=85
ASM_REL=86 NUMFLOAT=86
STRING=87 BINFLOAT=87
CHAR=88 DECFLOAT=88
NUMBER=89 HEXFLOAT=89
NUMFLOAT=90 NUMINT=90
BINFLOAT=91 BININTEGER=91
DECFLOAT=92 DECINTEGER=92
HEXFLOAT=93 HEXINTEGER=93
NUMINT=94 NAME=94
BININTEGER=95 WS=95
DECINTEGER=96 COMMENT_LINE=96
HEXINTEGER=97 COMMENT_BLOCK=97
NAME=98 ASM_BYTE=98
WS=99 ASM_MNEMONIC=99
COMMENT_LINE=100 ASM_REL=100
COMMENT_BLOCK=101 ASM_IMM=101
'{'=2 ASM_COLON=102
'}'=3 ASM_EXCL=103
'['=4 ASM_COMMA=104
']'=5 ASM_PAR_BEGIN=105
'('=6 ASM_PAR_END=106
')'=7 ASM_BRACKET_BEGIN=107
ASM_BRACKET_END=108
ASM_DOT=109
ASM_SHIFT_LEFT=110
ASM_SHIFT_RIGHT=111
ASM_PLUS=112
ASM_MINUS=113
ASM_LESS_THAN=114
ASM_GREATER_THAN=115
ASM_MULTIPLY=116
ASM_DIVIDE=117
ASM_CURLY_BEGIN=118
ASM_CURLY_END=119
ASM_NUMBER=120
ASM_NUMFLOAT=121
ASM_BINFLOAT=122
ASM_DECFLOAT=123
ASM_HEXFLOAT=124
ASM_NUMINT=125
ASM_BININTEGER=126
ASM_DECINTEGER=127
ASM_HEXINTEGER=128
ASM_CHAR=129
ASM_NAME=130
ASM_WS=131
ASM_COMMENT_LINE=132
ASM_COMMENT_BLOCK=133
';'=8 ';'=8
':'=9
','=10
'..'=11 '..'=11
'?'=12 '?'=12
'.'=13
'->'=14 '->'=14
'+'=15
'-'=16
'*'=17
'/'=18
'%'=19 '%'=19
'++'=20 '++'=20
'--'=21 '--'=21
@ -123,14 +142,10 @@ COMMENT_BLOCK=101
'~'=23 '~'=23
'^'=24 '^'=24
'|'=25 '|'=25
'<<'=26
'>>'=27
'=='=28 '=='=28
'!='=29 '!='=29
'<'=30
'<='=31 '<='=31
'>='=32 '>='=32
'>'=33
'&&'=34 '&&'=34
'||'=35 '||'=35
'='=36 '='=36
@ -174,6 +189,5 @@ COMMENT_BLOCK=101
'clobbers'=75 'clobbers'=75
'bytes'=76 'bytes'=76
'cycles'=77 'cycles'=77
'!'=78 '.byte'=98
'#'=83 '#'=101
'.byte'=84

View File

@ -21,7 +21,6 @@ file
: declSeq EOF : declSeq EOF
; ;
asmFile asmFile
: asmLines EOF : asmLines EOF
; ;
@ -63,7 +62,7 @@ declVariables
declVariableList declVariableList
: declVariableInit : declVariableInit
| declVariableList ',' declVariableInit | declVariableList COMMA declVariableInit
; ;
declVariableInit declVariableInit
@ -72,11 +71,11 @@ declVariableInit
; ;
declFunction declFunction
: declTypes NAME '(' parameterListDecl? ')' '{' stmtSeq? '}' : declTypes NAME PAR_BEGIN parameterListDecl? PAR_END CURLY_BEGIN stmtSeq? CURLY_END
; ;
parameterListDecl parameterListDecl
: parameterDecl (',' parameterDecl)* ; : parameterDecl (COMMA parameterDecl)* ;
parameterDecl parameterDecl
: declTypes NAME #parameterDeclType : declTypes NAME #parameterDeclType
@ -84,25 +83,25 @@ parameterDecl
; ;
globalDirective globalDirective
: (PRAGMA RESERVE) '(' NUMBER ( ',' NUMBER )* ')' #globalDirectiveReserve : (PRAGMA RESERVE) PAR_BEGIN NUMBER ( COMMA NUMBER )* PAR_END #globalDirectiveReserve
| (PRAGMA PC) '(' NUMBER ')' #globalDirectivePc | (PRAGMA PC) PAR_BEGIN NUMBER PAR_END #globalDirectivePc
| (PRAGMA TARGET) '(' NAME ')' #globalDirectivePlatform | (PRAGMA TARGET) PAR_BEGIN NAME PAR_END #globalDirectivePlatform
| (PRAGMA LINK) '(' STRING ')' #globalDirectiveLinkScript | (PRAGMA LINK) PAR_BEGIN STRING PAR_END #globalDirectiveLinkScript
| (PRAGMA CODESEG) '(' NAME ')' #globalDirectiveCodeSeg | (PRAGMA CODESEG) PAR_BEGIN NAME PAR_END #globalDirectiveCodeSeg
| (PRAGMA DATASEG) '(' NAME ')' #globalDirectiveDataSeg | (PRAGMA DATASEG) PAR_BEGIN NAME PAR_END #globalDirectiveDataSeg
| (PRAGMA ENCODING) '(' NAME ')' #globalDirectiveEncoding | (PRAGMA ENCODING) PAR_BEGIN NAME PAR_END #globalDirectiveEncoding
; ;
directive directive
: CONST #directiveConst : CONST #directiveConst
| EXTERN #directiveExtern | EXTERN #directiveExtern
| EXPORT #directiveExport | EXPORT #directiveExport
| ALIGN '(' NUMBER ')' #directiveAlign | ALIGN PAR_BEGIN NUMBER PAR_END #directiveAlign
| REGISTER ( '(' NAME ')')? #directiveRegister | REGISTER ( PAR_BEGIN NAME PAR_END)? #directiveRegister
| INLINE #directiveInline | INLINE #directiveInline
| VOLATILE #directiveVolatile | VOLATILE #directiveVolatile
| INTERRUPT ( '(' NAME ')' )? #directiveInterrupt | INTERRUPT ( PAR_BEGIN NAME PAR_END )? #directiveInterrupt
| RESERVE '(' NUMBER ( ',' NUMBER )* ')' #directiveReserveZp | RESERVE PAR_BEGIN NUMBER ( COMMA NUMBER )* PAR_END #directiveReserveZp
; ;
stmtSeq stmtSeq
@ -111,31 +110,31 @@ stmtSeq
stmt stmt
: declVariables ';' #stmtDeclVar : declVariables ';' #stmtDeclVar
| '{' stmtSeq? '}' #stmtBlock | CURLY_BEGIN stmtSeq? CURLY_END #stmtBlock
| commaExpr ';' #stmtExpr | commaExpr ';' #stmtExpr
| IF '(' commaExpr ')' stmt ( ELSE stmt )? #stmtIfElse | IF PAR_BEGIN commaExpr PAR_END stmt ( ELSE stmt )? #stmtIfElse
| directive* WHILE '(' commaExpr ')' stmt #stmtWhile | directive* WHILE PAR_BEGIN commaExpr PAR_END stmt #stmtWhile
| directive* DO stmt WHILE '(' commaExpr ')' ';' #stmtDoWhile | directive* DO stmt WHILE PAR_BEGIN commaExpr PAR_END ';' #stmtDoWhile
| directive* FOR '(' forLoop ')' stmt #stmtFor | directive* FOR PAR_BEGIN forLoop PAR_END stmt #stmtFor
| SWITCH '(' commaExpr ')' '{' switchCases '}' #stmtSwitch | SWITCH PAR_BEGIN commaExpr PAR_END CURLY_BEGIN switchCases CURLY_END #stmtSwitch
| RETURN commaExpr? ';' #stmtReturn | RETURN commaExpr? ';' #stmtReturn
| BREAK ';' #stmtBreak | BREAK ';' #stmtBreak
| CONTINUE ';' #stmtContinue | CONTINUE ';' #stmtContinue
| ASM asmDirectives? '{' asmLines '}' #stmtAsm | ASM asmDirectives? CURLY_BEGIN asmLines ASM_CURLY_END #stmtAsm
| declKasm #stmtDeclKasm | declKasm #stmtDeclKasm
; ;
switchCases: switchCases:
switchCase+ ( DEFAULT ':' stmtSeq? )? switchCase+ ( DEFAULT COLON stmtSeq? )?
; ;
switchCase: switchCase:
CASE expr ':' stmtSeq? CASE expr COLON stmtSeq?
; ;
forLoop forLoop
: forClassicInit ';' commaExpr ';' commaExpr? #forClassic : forClassicInit ';' commaExpr ';' commaExpr? #forClassic
| declTypes? NAME ':' expr '..' expr #forRange | declTypes? NAME COLON expr '..' expr #forRange
; ;
forClassicInit forClassicInit
@ -144,12 +143,12 @@ forClassicInit
; ;
typeDecl typeDecl
: '(' typeDecl ')' #typePar : PAR_BEGIN typeDecl PAR_END #typePar
| SIMPLETYPE #typeSimple | SIMPLETYPE #typeSimple
| SIGNEDNESS SIMPLETYPE? #typeSignedSimple | SIGNEDNESS SIMPLETYPE? #typeSignedSimple
| typeDecl ASTERISK #typePtr | typeDecl ASTERISK #typePtr
| typeDecl '[' (expr)? ']' #typeArray | typeDecl BRACKET_BEGIN (expr)? BRACKET_END #typeArray
| typeDecl '(' ')' #typeProcedure | typeDecl PAR_BEGIN PAR_END #typeProcedure
| structDef #typeStructDef | structDef #typeStructDef
| structRef #typeStructRef | structRef #typeStructRef
| enumDef #typeEnumDef | enumDef #typeEnumDef
@ -162,7 +161,7 @@ structRef
; ;
structDef structDef
: STRUCT NAME? '{' structMembers+ '}' : STRUCT NAME? CURLY_BEGIN structMembers+ CURLY_END
; ;
structMembers structMembers
@ -174,12 +173,12 @@ enumRef
; ;
enumDef enumDef
: ENUM NAME? '{' enumMemberList '}' : ENUM NAME? CURLY_BEGIN enumMemberList CURLY_END
; ;
enumMemberList enumMemberList
: enumMember : enumMember
| enumMemberList ',' enumMember | enumMemberList COMMA enumMember
; ;
enumMember enumMember
@ -188,36 +187,36 @@ enumMember
commaExpr commaExpr
: expr #commaNone : expr #commaNone
| commaExpr ',' expr #commaSimple | commaExpr COMMA expr #commaSimple
; ;
expr expr
: '(' commaExpr ')' #exprPar : PAR_BEGIN commaExpr PAR_END #exprPar
| expr '.' NAME #exprDot | expr DOT NAME #exprDot
| expr '->' NAME #exprArrow | expr '->' NAME #exprArrow
| expr '(' parameterList? ')' #exprCall | expr PAR_BEGIN parameterList? PAR_END #exprCall
| SIZEOF '(' ( expr | typeDecl ) ')' #exprSizeOf | SIZEOF PAR_BEGIN ( expr | typeDecl ) PAR_END #exprSizeOf
| TYPEID '(' ( expr | typeDecl ) ')' #exprTypeId | TYPEID PAR_BEGIN ( expr | typeDecl ) PAR_END #exprTypeId
| expr '[' commaExpr ']' #exprArray | expr BRACKET_BEGIN commaExpr BRACKET_END #exprArray
| '(' typeDecl ')' expr #exprCast | PAR_BEGIN typeDecl PAR_END expr #exprCast
| ('--' | '++' ) expr #exprPreMod | ('--' | '++' ) expr #exprPreMod
| expr ('--' | '++' ) #exprPostMod | expr ('--' | '++' ) #exprPostMod
| '*' expr #exprPtr | ASTERISK expr #exprPtr
| ('+' | '-' | '!' | '&'| '~' ) expr #exprUnary | (PLUS | MINUS | LOGIC_NOT | '&'| '~' ) expr #exprUnary
| expr ('<<' | '>>' ) expr #exprBinary | expr (SHIFT_LEFT | SHIFT_RIGHT ) expr #exprBinary
| expr ('*' | '/' | '%' ) expr #exprBinary | expr (ASTERISK | DIVIDE | '%' ) expr #exprBinary
| expr ( '+' | '-' ) expr #exprBinary | expr ( PLUS | MINUS ) expr #exprBinary
| ('<' | '>') expr #exprUnary | (LESS_THAN | GREATER_THAN) expr #exprUnary
| expr ( '==' | '!=' | '<' | '<=' | '>=' | '>' ) expr #exprBinary | expr ( '==' | '!=' | LESS_THAN | '<=' | '>=' | GREATER_THAN ) expr #exprBinary
| expr ( '&' ) expr #exprBinary | expr ( '&' ) expr #exprBinary
| expr ( '^' ) expr #exprBinary | expr ( '^' ) expr #exprBinary
| expr ( '|' ) expr #exprBinary | expr ( '|' ) expr #exprBinary
| expr ( '&&' ) expr #exprBinary | expr ( '&&' ) expr #exprBinary
| expr ( '||' ) expr #exprBinary | expr ( '||' ) expr #exprBinary
| expr '?' expr ':' expr #exprTernary | expr '?' expr COLON expr #exprTernary
| <assoc=right> expr '=' expr #exprAssignment | <assoc=right> expr '=' expr #exprAssignment
| <assoc=right> expr ASSIGN_COMPOUND expr #exprAssignmentCompound | <assoc=right> expr ASSIGN_COMPOUND expr #exprAssignmentCompound
| '{' expr (',' expr )* '}' #initList | CURLY_BEGIN expr (COMMA expr )* CURLY_END #initList
| NAME #exprId | NAME #exprId
| NUMBER #exprNumber | NUMBER #exprNumber
| STRING+ #exprString | STRING+ #exprString
@ -226,7 +225,7 @@ expr
; ;
parameterList parameterList
: expr (',' expr)* : expr (COMMA expr)*
; ;
declKasm declKasm
@ -234,7 +233,7 @@ declKasm
; ;
asmDirectives asmDirectives
: '(' asmDirective ( ',' asmDirective )* ')' : PAR_BEGIN asmDirective ( COMMA asmDirective )* PAR_END
; ;
asmDirective asmDirective
@ -247,7 +246,7 @@ asmDirective
; ;
asmLines asmLines
: {cParser.setModeAsm(true);} asmLine* {cParser.setModeAsm(false);} : asmLine*
; ;
asmLine asmLine
@ -257,8 +256,8 @@ asmLine
; ;
asmLabel asmLabel
: NAME ':' #asmLabelName : ASM_NAME ASM_COLON #asmLabelName
| '!' NAME? ':' #asmLabelMulti | ASM_EXCL ASM_NAME? ASM_COLON #asmLabelMulti
; ;
asmInstruction asmInstruction
@ -266,28 +265,28 @@ asmInstruction
; ;
asmBytes asmBytes
: ASM_BYTE asmExpr ( ',' asmExpr)* : ASM_BYTE asmExpr ( ASM_COMMA asmExpr)*
; ;
asmParamMode asmParamMode
: asmExpr #asmModeAbs : asmExpr #asmModeAbs
| '#' asmExpr #asmModeImm | ASM_IMM asmExpr #asmModeImm
| asmExpr ',' NAME #asmModeAbsXY | asmExpr ASM_COMMA ASM_NAME #asmModeAbsXY
| '(' asmExpr ')' ',' NAME #asmModeIndIdxXY | ASM_PAR_BEGIN asmExpr ASM_PAR_END ASM_COMMA ASM_NAME #asmModeIndIdxXY
| '(' asmExpr ',' NAME ')' #asmModeIdxIndXY | ASM_PAR_BEGIN asmExpr ASM_COMMA ASM_NAME ASM_PAR_END #asmModeIdxIndXY
| '(' asmExpr ')' #asmModeInd | ASM_PAR_BEGIN asmExpr ASM_PAR_END #asmModeInd
; ;
asmExpr asmExpr
: '[' asmExpr ']' #asmExprPar : ASM_BRACKET_BEGIN asmExpr ASM_BRACKET_END #asmExprPar
| asmExpr ( '.' ) asmExpr #asmExprBinary | asmExpr ( ASM_DOT ) asmExpr #asmExprBinary
| asmExpr ( '<<'| '>>' ) asmExpr #asmExprBinary | asmExpr ( ASM_SHIFT_LEFT| ASM_SHIFT_RIGHT ) asmExpr #asmExprBinary
| ('+' | '-'| '<' | '>' ) asmExpr #asmExprUnary | (ASM_PLUS | ASM_MINUS| ASM_LESS_THAN | ASM_GREATER_THAN ) asmExpr #asmExprUnary
| asmExpr ('*' | '/' ) asmExpr #asmExprBinary | asmExpr (ASM_MULTIPLY | ASM_DIVIDE ) asmExpr #asmExprBinary
| asmExpr ( '+' | '-' ) asmExpr #asmExprBinary | asmExpr ( ASM_PLUS | ASM_MINUS ) asmExpr #asmExprBinary
| NAME #asmExprLabel | ASM_NAME #asmExprLabel
| ASM_REL #asmExprLabelRel | ASM_REL #asmExprLabelRel
| '{' NAME '}' #asmExprReplace | ASM_CURLY_BEGIN ASM_NAME ASM_CURLY_END #asmExprReplace
| NUMBER #asmExprInt | ASM_NUMBER #asmExprInt
| CHAR #asmExprChar | ASM_CHAR #asmExprChar
; ;

File diff suppressed because it is too large Load Diff

View File

@ -80,42 +80,61 @@ SIGNEDNESS=79
SIMPLETYPE=80 SIMPLETYPE=80
BOOLEAN=81 BOOLEAN=81
KICKASM_BODY=82 KICKASM_BODY=82
ASM_IMM=83 STRING=83
ASM_BYTE=84 CHAR=84
ASM_MNEMONIC=85 NUMBER=85
ASM_REL=86 NUMFLOAT=86
STRING=87 BINFLOAT=87
CHAR=88 DECFLOAT=88
NUMBER=89 HEXFLOAT=89
NUMFLOAT=90 NUMINT=90
BINFLOAT=91 BININTEGER=91
DECFLOAT=92 DECINTEGER=92
HEXFLOAT=93 HEXINTEGER=93
NUMINT=94 NAME=94
BININTEGER=95 WS=95
DECINTEGER=96 COMMENT_LINE=96
HEXINTEGER=97 COMMENT_BLOCK=97
NAME=98 ASM_BYTE=98
WS=99 ASM_MNEMONIC=99
COMMENT_LINE=100 ASM_REL=100
COMMENT_BLOCK=101 ASM_IMM=101
'{'=2 ASM_COLON=102
'}'=3 ASM_EXCL=103
'['=4 ASM_COMMA=104
']'=5 ASM_PAR_BEGIN=105
'('=6 ASM_PAR_END=106
')'=7 ASM_BRACKET_BEGIN=107
ASM_BRACKET_END=108
ASM_DOT=109
ASM_SHIFT_LEFT=110
ASM_SHIFT_RIGHT=111
ASM_PLUS=112
ASM_MINUS=113
ASM_LESS_THAN=114
ASM_GREATER_THAN=115
ASM_MULTIPLY=116
ASM_DIVIDE=117
ASM_CURLY_BEGIN=118
ASM_CURLY_END=119
ASM_NUMBER=120
ASM_NUMFLOAT=121
ASM_BINFLOAT=122
ASM_DECFLOAT=123
ASM_HEXFLOAT=124
ASM_NUMINT=125
ASM_BININTEGER=126
ASM_DECINTEGER=127
ASM_HEXINTEGER=128
ASM_CHAR=129
ASM_NAME=130
ASM_WS=131
ASM_COMMENT_LINE=132
ASM_COMMENT_BLOCK=133
';'=8 ';'=8
':'=9
','=10
'..'=11 '..'=11
'?'=12 '?'=12
'.'=13
'->'=14 '->'=14
'+'=15
'-'=16
'*'=17
'/'=18
'%'=19 '%'=19
'++'=20 '++'=20
'--'=21 '--'=21
@ -123,14 +142,10 @@ COMMENT_BLOCK=101
'~'=23 '~'=23
'^'=24 '^'=24
'|'=25 '|'=25
'<<'=26
'>>'=27
'=='=28 '=='=28
'!='=29 '!='=29
'<'=30
'<='=31 '<='=31
'>='=32 '>='=32
'>'=33
'&&'=34 '&&'=34
'||'=35 '||'=35
'='=36 '='=36
@ -174,6 +189,5 @@ COMMENT_BLOCK=101
'clobbers'=75 'clobbers'=75
'bytes'=76 'bytes'=76
'cycles'=77 'cycles'=77
'!'=78 '.byte'=98
'#'=83 '#'=101
'.byte'=84

View File

@ -1265,15 +1265,15 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
@Override @Override
public Void visitAsmExprLabel(KickCParser.AsmExprLabelContext ctxLabel) { public Void visitAsmExprLabel(KickCParser.AsmExprLabelContext ctxLabel) {
String label = ctxLabel.NAME().toString(); String label = ctxLabel.ASM_NAME().toString();
if(!definedLabels.contains(label)) { if(!definedLabels.contains(label)) {
// Look for the symbol // Look for the symbol
Symbol symbol = getCurrentScope().getSymbol(ctxLabel.NAME().getText()); Symbol symbol = getCurrentScope().getSymbol(ctxLabel.ASM_NAME().getText());
if(symbol != null) { if(symbol != null) {
referenced.put(label, symbol.getRef()); referenced.put(label, symbol.getRef());
} else { } else {
// Either forward reference or a non-existing variable. Create a forward reference for later resolving. // Either forward reference or a non-existing variable. Create a forward reference for later resolving.
referenced.put(label, new ForwardVariableRef(ctxLabel.NAME().getText())); referenced.put(label, new ForwardVariableRef(ctxLabel.ASM_NAME().getText()));
} }
} }
return super.visitAsmExprLabel(ctxLabel); return super.visitAsmExprLabel(ctxLabel);
@ -1294,7 +1294,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
KickCParserBaseVisitor<Void> findDefinedLabels = new KickCParserBaseVisitor<Void>() { KickCParserBaseVisitor<Void> findDefinedLabels = new KickCParserBaseVisitor<Void>() {
@Override @Override
public Void visitAsmLabelName(KickCParser.AsmLabelNameContext ctx) { public Void visitAsmLabelName(KickCParser.AsmLabelNameContext ctx) {
String label = ctx.NAME().getText(); String label = ctx.ASM_NAME().getText();
definedLabels.add(label); definedLabels.add(label);
return super.visitAsmLabelName(ctx); return super.visitAsmLabelName(ctx);
} }