Skeleton for the assembler tool
This commit is contained in:
parent
223111e7da
commit
57404c6fe8
|
@ -0,0 +1,111 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from rply import LexerGenerator, ParserGenerator
|
||||
|
||||
|
||||
_lexer = LexerGenerator()
|
||||
_lexer.add("DIRECTIVE", r"\.org|\.entry|\.dw|\.ds|\.dsz|\.dsp")
|
||||
_lexer.add("OPCODE2", r"SET|ADD|SUB|MUL|MLI|DIV|MOD|MDI|AND|BOR|XOR|SHR|ASR|SHL|IFB|IFC|IFE|IFN|IFG|IFA|IFL|IFU|ADX|SBX|STI|STD")
|
||||
_lexer.add("OPCODE1", r"JSR|INT|IAG|IAS|RFI|IAQ|HWN|HWQ|HWI")
|
||||
_lexer.add("OPCODE0", r"HLT")
|
||||
_lexer.add("GREG", r"A|B|C|X|Y|Z|I|J")
|
||||
_lexer.add("SREG", r"SP|PC|EX")
|
||||
_lexer.add("STACK", r"PUSH|POP|PEEK|PICK")
|
||||
_lexer.add("COMMA", r",")
|
||||
_lexer.add("COLON", r":")
|
||||
_lexer.add("OPENSB", r"\[")
|
||||
_lexer.add("CLOSESB", r"\]")
|
||||
_lexer.add("BINOP", r"[+*/-]|>>|<<")
|
||||
_lexer.add("OPENRB", r"\(")
|
||||
_lexer.add("CLOSERB", r"\)")
|
||||
_lexer.add("NUMBER", r"0x[0-9a-fA-F]+|[0-9]+")
|
||||
_lexer.add("SYMBOL", r"[a-zA-Z._@][a-zA-Z._@0-9]*")
|
||||
#_lexer.add("EOL", r"([;#][^\n]*)?(\r?\n)+")
|
||||
#_lexer.ignore(r"[ \t]+")
|
||||
_lexer.ignore(r"\s+|([;#][^\n]*)?\n")
|
||||
lexer = _lexer.build()
|
||||
|
||||
|
||||
_parser = ParserGenerator([x.name for x in lexer.rules])
|
||||
|
||||
@_parser.production("expression : NUMBER")
|
||||
def expression_number(p):
|
||||
n = p[0].getstr()
|
||||
if n.startswith('0x'):
|
||||
return int(n, 16)
|
||||
else:
|
||||
return int(n, 10)
|
||||
|
||||
@_parser.production("expression : SYMBOL")
|
||||
def expression_symbol(p):
|
||||
return ('SYMBOL', p[0].getstr())
|
||||
|
||||
@_parser.production("expression : OPENRB expression CLOSERB")
|
||||
def expression_parens(p):
|
||||
return p[1]
|
||||
|
||||
@_parser.production("expression : expression BINOP expression")
|
||||
def expression_binop(p):
|
||||
a, op, b = tuple(p)
|
||||
print(op,a,b)
|
||||
return (op, a, b)
|
||||
|
||||
@_parser.production("value : GREG")
|
||||
@_parser.production("value : SREG")
|
||||
def value_reg(p):
|
||||
return p[1]
|
||||
|
||||
@_parser.production("value : OPENSB GREG CLOSESB")
|
||||
def value_greg_indirect(p):
|
||||
pass
|
||||
|
||||
@_parser.production("value : OPENSB GREG BINOP expression CLOSESB")
|
||||
def value_greg_indirect_offset(p):
|
||||
pass
|
||||
|
||||
@_parser.production("code : OPCODE0")
|
||||
def opcode0(p):
|
||||
pass
|
||||
|
||||
@_parser.production("code : OPCODE1 value")
|
||||
def opcode1(p):
|
||||
pass
|
||||
|
||||
@_parser.production("code : OPCODE2 value COMMA value")
|
||||
def opcode2(p):
|
||||
pass
|
||||
|
||||
@_parser.production("directive : DIRECTIVE expression")
|
||||
def directive1(p):
|
||||
pass
|
||||
|
||||
@_parser.production("source : LABEL")
|
||||
def source_label(p):
|
||||
pass
|
||||
|
||||
@_parser.production("source : code")
|
||||
def source_code(p):
|
||||
pass
|
||||
|
||||
@_parser.production("source : directive")
|
||||
def source_directive(p):
|
||||
pass
|
||||
|
||||
parser = _parser.build()
|
||||
|
||||
|
||||
def assemble(filename):
|
||||
with open(filename, "r") as f:
|
||||
text = f.read()
|
||||
print(len(text))
|
||||
for token in lexer.lex(text):
|
||||
print(token.source_pos, token)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
|
||||
for f in sys.argv[1:]:
|
||||
assemble(f)
|
||||
|
||||
# vim:et:sw=4:ts=4:sts=4:
|
Loading…
Reference in New Issue