45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
"""
|
|
Directives for explicitly modifying the program memory.
|
|
"""
|
|
|
|
from abc import ABC, abstractmethod
|
|
from collections import deque
|
|
|
|
from .util import can_convert_to_int, autoint
|
|
|
|
class AbstractDirective(ABC):
|
|
@abstractmethod
|
|
def parse(self, assembler, tokenizer):
|
|
"""
|
|
Parse the directive by converting the text to a list of words.
|
|
Returns a list of 16bit words.
|
|
"""
|
|
pass
|
|
|
|
|
|
|
|
class SetDirective(AbstractDirective):
|
|
def parse(self, assembler, tokenizer):
|
|
words = deque()
|
|
should_be_bracket = next(tokenizer)
|
|
if(not should_be_bracket == "["):
|
|
assembler.raise_unexpected_token(".set", "'['", should_be_bracket)
|
|
|
|
while(True):
|
|
should_be_value = next(tokenizer)
|
|
if(not can_convert_to_int(should_be_value)):
|
|
assembler.raise_unexpected_token(".set"
|
|
, "integer or character value"
|
|
, should_be_value)
|
|
words.append(autoint(should_be_value))
|
|
|
|
comma_or_bracket = next(tokenizer)
|
|
if(not comma_or_bracket in (",", "]")):
|
|
assembler.raise_unexpected_token(".set"
|
|
, "',' or ']'"
|
|
, comma_or_bracket)
|
|
|
|
if(comma_or_bracket == "]"):
|
|
break
|
|
return list(words)
|