Skip to content

Serialize

Flavio Lionel Rita edited this page Apr 25, 2021 · 5 revisions

Serialize

from py_expression.core import Exp
exp = Exp()
operand =exp.parse(('i=0;'
                 'while(i<=6){'
                 '  output=i*2;'
                 '  i=i+1;'
                 '}'))

serialized = exp.serialize(operand)
print(serialized)

Result:

{
    "n": "block",
    "t": "Block",
    "c": [
        {
            "n": "=",
            "t": "Assigment",
            "c": [
                {
                    "n": "i",
                    "t": "Variable"
                },
                {
                    "n": 0,
                    "t": "Constant"
                }
            ]
        },
        {
            "n": "while",
            "t": "While",
            "c": [
                {
                    "n": "<=",
                    "t": "LessThanOrEqual",
                    "c": [
                        {
                            "n": "i",
                            "t": "Variable"
                        },
                        {
                            "n": 6,
                            "t": "Constant"
                        }
                    ]
                },
                {
                    "n": "block",
                    "t": "Block",
                    "c": [
                        {
                            "n": "=",
                            "t": "Assigment",
                            "c": [
                                {
                                    "n": "output",
                                    "t": "Variable"
                                },
                                {
                                    "n": "*",
                                    "t": "Multiplication",
                                    "c": [
                                        {
                                            "n": "i",
                                            "t": "Variable"
                                        },
                                        {
                                            "n": 2,
                                            "t": "Constant"
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "n": "=",
                            "t": "Assigment",
                            "c": [
                                {
                                    "n": "i",
                                    "t": "Variable"
                                },
                                {
                                    "n": "+",
                                    "t": "Addition",
                                    "c": [
                                        {
                                            "n": "i",
                                            "t": "Variable"
                                        },
                                        {
                                            "n": 1,
                                            "t": "Constant"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

Deserialize

from py_expression.core import Exp
exp = Exp()

serialized = {'n': 'block', 't': 'Block', 'c': [{'n': '=', 't': 'Assigment', 'c': [{'n': 'i', 't': 'Variable'}, {'n': 0, 't': 'Constant'}]}, {'n': 'while', 't': 'While', 'c': [{'n': '<=', 't': 'LessThanOrEqual', 'c': [{'n': 'i', 't': 'Variable'}, {'n': 6, 't': 'Constant'}]}, {'n': 'block', 't': 'Block', 'c': [{'n': '=', 't': 'Assigment', 'c': [{'n': 'output', 't': 'Variable'}, {'n': '*', 't': 'Multiplication', 'c': [{'n': 'i', 't': 'Variable'}, {'n': 2, 't': 'Constant'}]}]}, {'n': '=', 't': 'Assigment', 'c': [{'n': 'i', 't': 'Variable'}, {'n': '+', 't': 'Addition', 'c': [{'n': 'i', 't': 'Variable'}, {'n': 1, 't': 'Constant'}]}]}]}]}]}

operand2= exp.deserialize(serialized)
context = {}
exp.eval(operand2,context)
print(context['output'])

result:

12
Clone this wiki locally