コード例 #1
0
ファイル: h_language.py プロジェクト: jqb/progpac
 def __init__(self, program, level=None):
     
     self.bug = Bug(level)
     self.code = []
     self.code_debug = []
     self.body = None
     self.ast = None
     self.funcs = {}
     self.error = None
     self.program = program
     self.program_length = len(re.sub("[\s:]", "", self.program))
     
     try:
         self.ast = parser.parse(program)[0]
         try:
             self.body = filter(lambda x: x.__class__ == Body, self.ast)[0]
         except IndexError:
             self.body = ""
         self.funcs = dict(
             map(lambda x: (x[0], x),
             filter(lambda x: isinstance(x,FuncDef), self.ast)))
         
         self.go(self.body)
     except (Error, FullFirstMatchException) as e:
         self.error = "Line: %s, Character: %s. %s" % (
             e.lineno, e.offset, e.msg)
コード例 #2
0
ファイル: h_language.py プロジェクト: jqb/progpac
class Parser(object):

    def __init__(self, program, level=None):
        
        self.bug = Bug(level)
        self.code = []
        self.code_debug = []
        self.body = None
        self.ast = None
        self.funcs = {}
        self.error = None
        self.program = program
        self.program_length = len(re.sub("[\s:]", "", self.program))
        
        try:
            self.ast = parser.parse(program)[0]
            try:
                self.body = filter(lambda x: x.__class__ == Body, self.ast)[0]
            except IndexError:
                self.body = ""
            self.funcs = dict(
                map(lambda x: (x[0], x),
                filter(lambda x: isinstance(x,FuncDef), self.ast)))
            
            self.go(self.body)
        except (Error, FullFirstMatchException) as e:
            self.error = "Line: %s, Character: %s. %s" % (
                e.lineno, e.offset, e.msg)

    def go(self, body, loc=None):
        if loc is None:
            loc = {}

        for element in body:
            if "@" in self.code:
                break
                
            if isinstance(element, Move):
                move = element[0]
                if self.bug:
                    move = self.bug.move(move)
                    self.code.append(move)
                    self.code_debug.append(
                        (element.in_lineno, element.in_char, move))
                else:
                    self.code.append(move)
                
            elif isinstance(element, Variable):
                pass
            
            elif isinstance(element, FuncCall):
                function_call = element

                try:
                    function_def = self.funcs[element.name]
                except KeyError:
                    raise Error("Function %s not defined." % element.name,
                                {"in_lineno": element.in_lineno,
                                 "in_offset": element.in_char})

                if len(function_def.args) != len(element.args):
                    raise Error("Wrong arguments in function %s call." % element.name,
                                {"in_lineno": element.in_lineno,
                                 "in_offset": element.in_char})
                args = dict(zip(function_def.args, function_call.args))
                try:
                    self.go(function_def.body, args)
                except RuntimeError:
                    self.code.append("!")