luapy (14) lua ast

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guzhou_diaoke/article/details/86435573

1.exp

"""
exp ::=  nil | false | true | Numeral | LiteralString | ‘...’ | functiondef |
    prefixexp | tableconstructor | exp binop exp | unop exp
prefixexp ::= var | functioncall | ‘(’ exp ‘)’
var ::=  Name | prefixexp ‘[’ exp ‘]’ | prefixexp ‘.’ Name
functioncall ::=  prefixexp args | prefixexp ‘:’ Name args
"""


# nil
class NilExp:
    def __init__(self, line):
        self.line = line


# true
class TrueExp:
    def __init__(self, line):
        self.line = line


# false
class FalseExp:
    def __init__(self, line):
        self.line = line


# ...
class VarArgExp:
    def __init__(self, line):
        self.line = line


# Numeral: integer
class IntegerExp:
    def __init__(self, line, val):
        self.line = line
        self.val = val

    def __str__(self):
        s = ''
        s += '"Line": ' + str(self.line) + '\n'
        s += '"Val": ' + str(self.val) + '\n'
        return s


# Numeral: float
class FloatExp:
    def __init__(self, line, val):
        self.line = line
        self.val = val


# Literal String
class StringExp:
    def __init__(self, line, s):
        self.line = line
        self.s = s

    def __str__(self):
        return '"Line": ' + str(self.line) + '\n' + '"Str": ' + '"' + self.s + '"'


# unop exp
class UnopExp:
    def __init__(self, line, op, exp):
        self.line = line
        self.op = op
        self.exp = exp

    def __str__(self):
        s = ''
        s += 'Line: ' + str(self.line) + '\n'
        s += 'Op: ' + str(self.op) + '\n'
        s += 'Exp: ' + '\n'
        for l in str(self.exp).split('\n'):
            if len(l):
                s += '  ' + l + '\n'
        return s


# exp1 op exp2
class BinopExp:
    def __init__(self, line, op, exp1, exp2):
        self.line = line
        self.op = op
        self.exp1 = exp1
        self.exp2 = exp2

    def __str__(self):
        s = '"Line": ' + str(self.line) + '\n'
        s += '"Op": ' + str(self.op) + '\n'
        s += '"exp1": ' '\n'
        for l in str(self.exp1).split('\n'):
            if len(l) > 0:
                s += '  ' + l + '\n'

        s += '"exp2": ' '\n'
        for l in str(self.exp2).split('\n'):
            if len(l) > 0:
                s += '  ' + l + '\n'
        return s


# ..
class ConcatExp:
    def __init__(self, line, exps):
        self.line = line
        self.exps = exps


# tableconstructor ::= '{' [fieldlist] ''}
# fieldlist ::= field {fieldsep field} [fieldsep]
# field ::= '[' exp ']' '=' exp | Name '=' exp | exp
# fieldsep ::= ',' | ';'
class TableConstructorExp:
    def __init__(self, line, last_line, key_exps, val_exps):
        self.line = line
        self.last_line = last_line
        self.key_exps = key_exps
        self.val_exps = val_exps


# functiondef ::= function funcbody
# funcbody ::= '(' [parlist] ')' block end
# parlist ::= namelist [',' '...'] | '...'
# namelist ::= Name {',' Name}
class FuncDefExp:
    def __init__(self, line, last_line, par_list, is_var_arg, block):
        self.line = line
        self.last_line = last_line
        self.par_list = par_list
        self.is_var_arg = is_var_arg
        self.block = block

    def __str__(self):
        s = ''
        s += 'Line: ' + str(self.line) + '\n'
        s += 'LastLine: ' + str(self.last_line) + '\n'
        s += 'ParList: ' + '\n'
        for par in self.par_list:
            for l in str(par).split('\n'):
                if len(l):
                    s += '  ' + l + '\n'
        s += 'IsVarArg: ' + str(self.is_var_arg) + '\n'
        s += 'Block: '
        for l in str(self.block).split('\n'):
            if len(l):
                s += '  ' + l + '\n'
        return s


"""
prefixexp ::= Name |
              ‘(’ exp ‘)’ |
              prefixexp ‘[’ exp ‘]’ |
              prefixexp ‘.’ Name |
              prefixexp ‘:’ Name args |
              prefixexp args
"""


class NameExp:
    def __init__(self, line, name):
        self.line = line
        self.name = name

    def __str__(self):
        return '"Line": ' + str(self.line) + '\n' + '"Name": ' + '"' + self.name + '"'


class ParensExp:
    def __init__(self, exp):
        self.exp = exp


class TableAccessExp:
    def __init__(self, last_line, prefix_exp, key_exp):
        self.last_line = last_line
        self.prefix_exp = prefix_exp
        self.key_exp = key_exp


class FuncCallExp:
    def __init__(self, line, last_line, prefix_exp, name_exp, args):
        self.line = line
        self.last_line = last_line
        self.prefix_exp = prefix_exp
        self.name_exp = name_exp
        self.args = args

    def __str__(self):
        s = ''
        s += '"Line": ' + str(self.line) + ',\n'
        s += '"LastLine": ' + str(self.last_line) + ',\n'
        s += '"PrefixExp": {\n'
        for line in str(self.prefix_exp).split('\n'):
            s += '  ' + line + '\n'
        s += '},\n'
        s += '"NameExp": ' + str(self.name_exp) + ',\n'
        s += '"Args": ' + '['
        for arg in self.args:
            s += '{\n'
            for line in str(arg).split('\n'):
                if len(line):
                    s += '  ' + line + '\n'
            s += '}'
        s += ']'
        return s

2.stat

"""
stat ::=  ‘;’ |
    varlist ‘=’ explist |
    functioncall |
    label |
    break |
    goto Name |
    do block end |
    while exp do block end |
    repeat block until exp |
    if exp then block {elseif exp then block} [else block] end |
    for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end |
    for namelist in explist do block end |
    function funcname funcbody |
    local function Name funcbody |
    local namelist [‘=’ explist]
"""


# ';'
class EmptyStat:
    def __init__(self, line):
        self.line = line


# break
class BreakStat:
    def __init__(self, line):
        self.line = line


# '::' Name '::'
class LabelStat:
    def __init__(self, name):
        self.name = name


# goto Name
class GotoStat:
    def __init__(self, name):
        self.name = name


# do block end
class DoStat:
    def __init__(self, block):
        self.block = block


# functioncall
class FuncCallStat:
    def __init__(self, line):
        self.line = line


# if exp then block {elseif exp then block} [else block] end
class IfStat:
    def __init__(self, exps, blocks):
        self.exps = exps
        self.blocks = blocks

    def __str__(self):
        s = ''
        s += '"Exps": ' + '\n'
        for exp in self.exps:
            for l in str(exp).split('\n'):
                s += '  ' + l + '\n'

        s += '"Blocks": ' + '\n'
        for block in self.blocks:
            for l in str(block).split('\n'):
                s += '  ' + l + '\n'

        return s


# while exp do block end
class WhileStat:
    def __init__(self, exp, block):
        self.exp = exp
        self.block = block

    def __str__(self):
        s = '"While": \n'
        s += '  "Exp": \n'
        for l in str(self.exp).split('\n'):
            if len(l):
                s += '    ' + l + '\n'
        s += '  "Block": \n'
        for l in str(self.block).split('\n'):
            if len(l):
                s += '    ' + l + '\n'
        return s


# repeat block until exp
class RepeatStat:
    def __init__(self, block, exp):
        self.block = block
        self.exp = exp

    def __str__(self):
        s = '"Repeat": \n'
        s += '  "Block": \n'
        for l in str(self.block).split('\n'):
            if len(l):
                s += '    ' + l + '\n'
        s += '  "Exp": \n'
        for l in str(self.exp).split('\n'):
            if len(l):
                s += '    ' + l + '\n'
        return s


# for Name '=' exp ',' exp [',' exp] do block end
class ForNumStat:
    def __init__(self, line_of_for, line_of_do, var_name, init_exp, limit_exp, step_exp, block):
        self.line_of_for = line_of_for
        self.line_of_do = line_of_do
        self.var_name = var_name
        self.init_exp = init_exp
        self.limit_exp = limit_exp
        self.step_exp = step_exp
        self.block = block

    def __str__(self):
        s = ''
        s += 'Line of for: ' + str(self.line_of_for) + '\n'
        s += 'Line of do: ' + str(self.line_of_do) + '\n'
        s += 'Var name: ' + str(self.var_name) + '\n'
        s += 'Init exp: ' + '\n'
        for l in str(self.init_exp).split('\n'):
            s += '  ' + l + '\n'
        s += 'Limit exp: ' + '\n'
        for l in str(self.limit_exp).split('\n'):
            s += '  ' + l + '\n'
        s += 'Step exp: ' + '\n'
        for l in str(self.step_exp).split('\n'):
            s += '  ' + l + '\n'
        s += 'Block: ' + '\n'
        for l in str(self.block).split('\n'):
            if len(l):
                s += '  ' + l + '\n'

        return s


# for namelist in explist do block end
# namelist ::= Name {',' Name}
# explist ::= exp {',' exp}
class ForInStat:
    def __init__(self, line_of_do, name_list, exp_list, block):
        self.line_of_do = line_of_do
        self.name_list = name_list
        self.exp_list = exp_list
        self.block = block


# varlist '=' explist
# varlist ::= var {',' var}
# var ::= Name | prefixexp '[' exp ']' | prefixexp '.' Name
class AssignStat:
    def __init__(self, last_line, var_list, exp_list):
        self.last_line = last_line
        self.var_list = var_list
        self.exp_list = exp_list

    def __str__(self):
        s = ''
        s += '"LastLine": ' + str(self.last_line) + '\n'
        s += '"VarList": ' + '\n'
        for var in self.var_list:
            for l in str(var).split('\n'):
                if len(l):
                    s += '  ' + l + '\n'

        s += '"ExpList": ' + '\n'
        for exp in self.exp_list:
            for l in str(exp).split('\n'):
                if len(l):
                    s += '  ' + l + '\n'

        return s


# local namelist ['=' explist]
# namelist ::= Name {',' Name}
# explist ::= exp {',' exp}
class LocalVarDeclStat:
    def __init__(self, last_line, name_list, exp_list):
        self.last_line = last_line
        self.name_list = name_list
        self.exp_list = exp_list


# local function Name funcbody
class LocalFuncDefStat:
    def __init__(self, name, exp):
        self.name = name
        self.exp = exp

猜你喜欢

转载自blog.csdn.net/guzhou_diaoke/article/details/86435573
LUA