Пример #1
0
def get_val_line(contents):

    if isinstance(contents[1], str):

        if isinstance(contents[2], tuple):
            place_holder = stack.stringify(stack.gpt(contents[2]))
            evaluated_ph = eval(place_holder)

        else:
            place_holder = contents[2]

            if place_holder.is_integer():
                place_holder = int(place_holder)

            evaluated_ph = place_holder

        if isinstance(place_holder, str):
            type_var = eval(place_holder)

        else:
            type_var = place_holder

        type_var = get_eval_type(type_var, literal_type=True)

        stored_values.variables[contents[1]] = type_var

        stack.empty()

        line_of_code = "\n{0:s}{1:s} {2:s} = {3:s};".format(
            get_indent(), type_var, contents[1], str(place_holder))

        return line_of_code

    else:
        raise ValueError("Invalid variable name given.")
Пример #2
0
def get_put_line(contents, end_char):

    if not stored_values.headers["<stdio.h>"]:
        stored_values.headers["<stdio.h>"] = True

    if isinstance(contents[1], str):

        if contents[1] in stored_values.variables:

            if stored_values.variables[contents[1]] == "int":
                format_value = "%d"

            if stored_values.variables[contents[1]] == "float":
                format_value = "%f"

        else:
            raise ValueError("Invalid variable \"%s\" passed to put keyword." %
                             contents[1])

        line_of_code = "\n{0:s}printf(\"{1:s}{2:s}\", {3:s});".format(
            get_indent(), format_value, end_char, contents[1])

    elif isinstance(contents[1], float):

        if contents[1].is_integer():
            contents_final = int(contents[1])
            format_value = "%d"
        else:
            contents_final = contents[1]
            format_value = "%f"

        line_of_code = "\n{0:s}printf(\"{1:s}{2:s}\", {3});".format(
            get_indent(), format_value, end_char, contents_final)

    elif isinstance(contents[1], tuple):

        if contents[1][0] == "str_const::":

            line_of_code = "\n{0:s}printf(\"{1:s}\");".format(
                get_indent(), contents[1][1] + end_char)

        else:

            place_holder = stack.stringify(stack.gpt(contents[1]))

            type_var = get_eval_type(eval(place_holder))

            line_of_code = "\n{0:s}printf(\"{1:s}{2:s}\", {3:s});".format(
                get_indent(), type_var, end_char, place_holder)

            stack.empty()

    return line_of_code
Пример #3
0
def preOrder(node):
    stack = stack.stack()
    stack.push(node)
    while not stack.empty():
        top = stack.pop()
        print('{}'.format(top.data))
        if top.right != None:
            stack.push(top.right)
        if top.left != None:
            stack.push(top.left)
Пример #4
0
def deliminate(cmd):
    op = cmd[0]
    if op == 'push':
        stack.push(num=int(cmd[1]))
    if op == 'pop':
        print(stack.pop())
    if op == 'size':
        print(stack.size())
    if op == 'empty':
        print(stack.empty())
    if op == 'top':
        print(stack.top())
Пример #5
0
def inOrder(node):
  stack = stack.stack()
  stack.push(node)
  finished = False
  while not stack.empty():
    top = stack.top()
    if finished:
      print('{}'.format(top.data))
      stack.pop()
      if top.right != None:
        stack.push(top.right)
        finished = False
    elif top.left != None:
      stack.push(top.left)
    else:
      print('{}'.format(top.data))
      if top.right != None:
        stack.push(top.right)
        finished = False
      else:
        finished = True # leaf
        stack.pop()
Пример #6
0
def postOrder(node):
  stack = stack.stack()
  stack.push(node)
  lastest = None
  while not stack.empty():
    top = stack.top()
    if top.left == lastest and top.right == None:
      print('{}'.format(top.data))
      lastest = top
      stack.pop()
    elif top.right == lastest:
      print('{}'.format(top.data))
      lastest = top
      stack.pop()
    else:
      if top.right != None:
        stack.push(top.right)
      if top.left != None:
        stack.push(top.left)
      if top.left == None and top.right == None:
        print('{}'.format(top.data))
        lastest = top
        stack.pop()
Пример #7
0
import stack
if not stack.empty():
    x = stack.pop()  # qualify by module name
stack.push(1.23)
Пример #8
0
def buildRPN(lexemes):
    spLexems = []
    i = 0
    lastlexem = ''
    flag = False
    for lexem in lexemes:
        i += 1
        if lexem[0] == 'F':
            lastlexem = lexem[0]
            stack.push(lexem)
            continue
        elif lexem[0] == 'N':
            lastlexem = lexem[0]
            spLexems.append(float(lexem[1:]))
            continue
        elif lexem[0] == ')':
            lastlexem = lexem[0]
            while not stack.empty():
                if stack.peek() == '(':
                    break
                else:
                    spLexems.append(stack.pop())
            stack.pop()
            continue
        elif lexem[0] == '(':
            stack.push('(')
            lastlexem = lexem[0]
            continue
        elif lexem[0] in '!':
            spLexems.append(lexem[0])
        elif lexem[0] == ',':
            j = stack.peek()
            while j != '(':
                spLexems.append(stack.pop())
                j = stack.peek()
            lastlexem = lexem[0]
        else:
            operation = lexem[0]
            if i == 1 or lastlexem in '(,^':
                if operation == '-':
                    operation = 'min'
                stack.push(operation)
                continue
            while not stack.empty():
                stackTop = stack.peek()
                if stackTop == '(':
                    break
                if stackTop[0] == 'F':
                    spLexems.append(stack.pop())
                    continue
                stackTopPriority = prioritet[stackTop]
                operationPriority = prioritet[operation]
                if (stackTopPriority > operationPriority):
                    spLexems.append(stack.pop())
                    continue
                if ((stackTopPriority == operationPriority)
                        and (stackTop in '+-*/')):
                    spLexems.append(stack.pop())
                    continue
                break
            stack.push(operation)
            lastlexem = lexem[0]
    while not stack.empty():
        spLexems.append(stack.pop())
    return spLexems