Beispiel #1
0
    def run_interpreter(self, max=100000):
        now = datetime.datetime.now()
        c = 0
        try:
            if(len(self.s) > 0):
                for c, _ in enumerate(self.parse(self.initial_state)):
                    if c >= max:
                        return self.rulecount, c, False, (datetime.datetime.now() - now).total_seconds()
        except EmptyInputException:
            self.sequence.add(FinalState(err=True))
            self.sequence.add_after(FinalState(err=True), Error())
            return self.rulecount, c, False, (datetime.datetime.now() - now).total_seconds()

        return self.rulecount, c, True, (datetime.datetime.now() - now).total_seconds()
Beispiel #2
0
    def apply(self, state: State) -> (State, Base):
        # Check whether the state is an actual State object, and the statement inside is of the right form.
        if not self.applicable(state):
            raise Exception(f"State does not support using the {self} rule")

        # There exist two Comp rules because where we choose to split up the statement can be arbitrary,
        # as regardless of where we split we always get the same execution.
        # However, in our implementation, we can go for the simplest option which is to just take the first
        # legal statement and try to execute that.
        # This method will produce the smallest possible trees and chains.

        # Unpack the state values
        s, a, p, i, o = state.unpack()
        # Copy the array to avoid impacting the old state
        a = a.copy()
        # Get first legal statement
        index = 1
        if s[0] == "[":
            count = 1
            # Get the corresponding ]
            for c in s[1:]:
                if c == "]":
                    count -= 1
                elif c == "[":
                    count += 1
                index += 1
                if count <= 0:
                    break
            #try:
            #    index = s.rindex("]") + 1
            #except ValueError:
            #    raise Exception("Syntax error detected in program, missing ]")
        # Split the statement into two separate statements
        s1 = s[:index]
        s2 = s[index:]
        # Get the new state from applying a rule
        before_state = State(s1, a, p, i, o)
        try:
            after_state, rule = self.interpreter.apply_rule(before_state)
        except:
            return FinalState(err=True), Error()

        self.interpreter.sequence.add_nested(before_state, rule, after_state)

        if isinstance(after_state, State):
            # If the new state is not final, then we append the remaining statement
            # back to statement s2
            ns, na, np, ni, no = after_state.unpack()
            s2 = ns + s2
            return State(s2, na, np, ni, no), self.co

        else:
            na, np, ni, no = after_state.unpack()
            # Otherwise, we simply keep s2 and use the new state values
            return State(s2, na, np, ni, no), self.ct
Beispiel #3
0
        def apply(self, state: State) -> (FinalState, Base):
            # Check whether the state is an actual State object, and the statement inside is of the right form.
            if not self.applicable(state):
                raise Exception(f"State does not support using the {self} rule")

            # Unpack the information from the given state
            s, a, p, i, o = state.unpack()
            # Copy the array to avoid impacting the old state
            a = a.copy()
            # And create a FinalState with these state values
            return FinalState(a, p, i, o), self
Beispiel #4
0
 def apply(self, state: State) -> (FinalState, Base):
     # Check whether the state is an actual State object, and the statement inside is of the right form.
     if not self.applicable(state):
         raise Exception(f"State does not support using the {self} rule")
     
     # Unpack the state
     s, a, p, i, o = state.unpack()
     # Copy the array to avoid impacting the old state
     a = a.copy()
     # Apply the Left rule by decrementing data pointer
     p -= 1
     # Return a FinalState with the correct information
     return FinalState(a, p, i, o), self
Beispiel #5
0
    def apply(self, state: State) -> (FinalState, Base):
        # Check whether the state is an actual State object, and the statement inside is of the right form.
        if not self.applicable(state):
            raise Exception(f"State does not support using the {self} rule")

        # Unpack the state
        s, a, p, i, o = state.unpack()
        # Copy the array to avoid impacting the old state
        a = a.copy()
        # Apply the Dot rule by adding the char version of the currently
        # pointed at value to the output
        o += chr(a[p])
        # Return a FinalState with the correct information
        return FinalState(a, p, i, o), self
Beispiel #6
0
    def apply(self, state: State) -> (FinalState, Base):
        # Check whether the state is an actual State object, and the statement inside is of the right form.
        if not self.applicable(state):
            raise Exception(f"State does not support using the {self} rule")

        # Unpack the state
        s, a, p, i, o = state.unpack()
        # Copy the array to avoid impacting the old state
        a = a.copy()
        # Apply the Comma rule by setting the currently pointed to value to
        # the ordinal value corresponding to the first input character
        # and then updating i to remove this first character.
        a[p] = ord(i[0])
        i = i[1:]
        # Return a FinalState with the correct information
        return FinalState(a, p, i, o), self