def encode_dict(prefix,dictionary): """ All key:value pairs in the dict must already be translated into Mogu syntax in order for this to work properly. To determine how to store each 'value', we'll use the following algorithm: 1) Assign string literals a representation, and replace them 2) Assign identifiers a representation, and replace them 3) split the string by a space delimiter, thus having a list of integers. 4) Combine into a single integer string, and create binary map 5) Store the integer and its binary map. """ encoding = "" binary = "" for entry in dictionary: value = MoguString("integral", dictionary[entry]) delimited = value.separate_string_literals() # Replace literal values with assigned symbols for index,literal in enumerate(value.string_literals): value.string_literals[index] = \ str(REGISTERS['LITERALS'][REG_NUM]) + \ str(encode_literal(literal)) if (value.string_literals): value = delimited % tuple(value.string_literals) tokens = value.split(' ') # Any remaining non-integers in the string must necessarily # be identifiers. Replace them. for index,token in enumerate(tokens): if not is_int(token): token_id = encode_identifier(token) tokens[index] = \ str(REGISTERS['IDENTIFIERS'][REG_NUM]) + \ str(token_id) binary += binary_map([entry]) binary += binary_map(tokens) int_string = "".join(tokens) encoding += str(entry) + int_string return {"int" : hex(int(encoding))[:-1], "bin" : binary}
def convert_output(self,container): self.scripts = [] for formatter in container: mogustring = MoguString("integral", str(formatter)) mogustring.translate("script") self.scripts.append(mogustring.script)
from moguio.MoguString import MoguString f = open("syntax_test.mogu") mogustring = MoguString("script", f.read()) f.close() print(mogustring.translate("integral"))
tokens[index] = \ str(REGISTERS['IDENTIFIERS'][REG_NUM]) + \ str(token_id) binary += binary_map([entry]) binary += binary_map(tokens) int_string = "".join(tokens) encoding += str(entry) + int_string return {"int" : hex(int(encoding))[:-1], "bin" : binary} if __name__ == "__main__": from moguio.MoguString import MoguString testdict = { "type" : "text", "text" : "widget foo content", "css" : "\"some css goes here\"" } converteddict = {} for entry in testdict: mentry = MoguString("script",entry) mvalue = MoguString("script",testdict[entry]) mentry.translate('integral') mvalue.translate('integral') converteddict[mentry.integral] = mvalue.integral result = encode_dict('WIDGETS', converteddict) print(result) print(REGISTERS['LITERALS'],REGISTERS['IDENTIFIERS'])