Created
September 29, 2024 06:43
-
-
Save bobbicodes/abb2c826702f588472ef0dbdb80bb257 to your computer and use it in GitHub Desktop.
Parsing expression grammar for 6502 assembly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start = line* | |
line = (_* @(instruction / label / directive | |
/ expression / comment)+ comment? '\n'?) / _* '\n' | |
instruction = opcode (_ @argument)? | |
directive = d:('.' name) _? a:argumentlist? { | |
return {directive: d.join(''), args: a} | |
} | |
expression = l:(name / number) _? o:operator _? | |
r:(name / number) { | |
return {operator: o, left: l, right: r} | |
} | |
operator = [+=] | |
comment = ';'+ c:[^\n]* { return {comment: c.join('')} } | |
argument = indirectArg / indexedArg / expression | |
/ (prefix? @(number / name / string)) | |
indexedArg = (expression / name) ',' _ [xy] | |
indirectArg = '(' (expression / name) ')' (',' _ [xy])? | |
argumentlist = @argument|.., argdelimiter| | |
label = n:name ":" { return {label: n} } | |
name = s:[@A-Z0-9_]i+ { return s.join('') } | |
string = '"' s:[^"]* '"' { return s.join('') } | |
prefix = '#' | |
number = decimalnumber / binarynumber / hexnumber | |
binarynumber = '%' [0-1]+ | |
decimalnumber = [0-9]+ | |
hexnumber = '$' [0-9A-F]i+ | |
_ = [ \t]+ | |
argdelimiter = ',' _? | |
opcode = o:("ADC"i / "AND"i / "ASL"i / "BCC"i / "BCS"i | |
/ "BEQ"i / "BIT"i / "BMI"i / "BNE"i / "BPL"i / "BRA"i | |
/ "BRK"i / "BVC"i / "BVS"i / "CLC"i / "CLD"i / "CLI"i | |
/ "CLV"i / "CMP"i / "CPX"i / "CPY"i / "DEC"i / "DEX"i | |
/ "DEY"i / "EOR"i / "INC"i / "INX"i / "INY"i / "JMP"i | |
/ "JSR"i / "LDA"i / "LDY"i / "LDX"i / "LSR"i / "NOP"i | |
/ "ORA"i / "PHA"i / "PHX"i / "PHY"i / "PHP"i / "PLA"i | |
/ "PLP"i / "PLY"i / "ROL"i / "ROR"i / "RTI"i / "RTS"i | |
/ "SBC"i / "SEC"i / "SED"i / "SEI"i / "STA"i / "STX"i | |
/ "STY"i / "STZ"i / "TAX"i / "TAY"i / "TSX"i / "TXA"i | |
/ "TXS"i / "TYA"i) { return {opcode: o} } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment