Advertisement
tripl3dogdare

MagiStack Interpreter v1.0

Aug 16th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.46 KB | None | 0 0
  1. # MagiStack Interpreter v1.0
  2. # Created by Connor Scialdone
  3. #
  4. # Values are pushed/popped from the stack as a, b, etc.
  5. #   in alphabetical order (a first, then b, and so on)
  6. #
  7. # Commands:
  8. #   0-9: Push value
  9. #   +: Pop a and b, push a+b
  10. #   -: Pop a and b, push b-a
  11. #   *: Pop a and b, push a*b
  12. #   /: Pop a and b, push b/a (floored)
  13. #   %: Pop a and b, push b%a
  14. #   !: Pop a, push boolean !a>0
  15. #   `: Pop a and b, push boolean b>a
  16. #   :: Pop a, push two instances of a
  17. #   \: Pop a and b, push a and b (swap)
  18. #   $: Pop a and discard
  19. #   .: Pop a, output as integer
  20. #   ,: Pop a, output as ASCII character
  21. #   =: Pop a and b, skip next command if a!=b
  22. #   #: Skip to next #, |, or end of program
  23. #   @: Skip to last @, |, or start of program
  24. #   ]: Unconditionally terminate skipping forward
  25. #   [: Unconditionally terminate skipping backward
  26. #   |: Unconditionally terminate skipping either direction
  27. #
  28. # Comments can easily be simulated by wrapping text in #.
  29. # Any non-command characters (newlines, letters, etc.) will
  30. #   be ignored automatically.
  31. #
  32. # A filepath to execute (preferably .mgst) can by given by
  33. #   command line; if not given, the program will prompt for
  34. #   a path.
  35.  
  36. import sys,math,re
  37.  
  38. stack = [] # The stack
  39.  
  40. # Main method (called with the string to process)
  41. def main(prog):
  42.     global stack
  43.     prog = re.sub("\s","",prog)
  44.    
  45.     p = -1 # Counter variable for position in string
  46.     while p < len(prog)-1:
  47.         p,c = p+1,prog[p+1] # Update position and get character
  48.  
  49.         # Command Handling
  50.         if   c.isdigit(): stack.append(int(c)) # Digits
  51.         elif c == "+": # Addition
  52.             checkStackSize(2,p)
  53.             a = stack.pop()
  54.             b = stack.pop()
  55.             stack.append(a+b)
  56.         elif c == "-": # Subtraction
  57.             checkStackSize(2,p)
  58.             a = stack.pop()
  59.             b = stack.pop()
  60.             stack.append(b-a)
  61.         elif c == "*": # Multiplication
  62.             checkStackSize(2,p)
  63.             a = stack.pop()
  64.             b = stack.pop()
  65.             stack.append(a*b)
  66.         elif c == "/": # Division
  67.             checkStackSize(2,p)
  68.             a = stack.pop()
  69.             b = stack.pop()
  70.             stack.append(math.floor(b/a))
  71.         elif c == "%": # Modulus
  72.             checkStackSize(2,p)
  73.             a = stack.pop()
  74.             b = stack.pop()
  75.             stack.append(b%a)
  76.         elif c == "!": # Boolean inverter
  77.             checkStackSize(1,p)
  78.             a = stack.pop()
  79.             stack.append(0 if a == 1 else 1)
  80.         elif c == "`": # Greater than
  81.             checkStackSize(2,p)
  82.             a = stack.pop()
  83.             b = stack.pop()
  84.             stack.append(1 if b>a else 0)
  85.         elif c == ":": # Duplicator
  86.             checkStackSize(1,p)
  87.             a = stack.pop()
  88.             stack.append(a)
  89.             stack.append(a)
  90.         elif c == "\\": # Swapper
  91.             checkStackSize(2,p)
  92.             a = stack.pop()
  93.             b = stack.pop()
  94.             stack.append(a)
  95.             stack.append(b)
  96.         elif c == "$": # Popper
  97.             checkStackSize(1,p)
  98.             a = stack.pop()
  99.         elif c == ".": # Integer output
  100.             checkStackSize(1,p)
  101.             a = stack.pop()
  102.             print(a,end="")
  103.         elif c == ",": # ASCII output
  104.             checkStackSize(1,p)
  105.             a = stack.pop()
  106.             print(chr(a),end="")
  107.         elif c == "=": # Comparison
  108.             checkStackSize(2,p)
  109.             a = stack.pop()
  110.             b = stack.pop()
  111.             if a != b: p += 1
  112.         elif c == "#": # Skip forward
  113.             p += 1
  114.             while p < len(prog) and prog[p] not in ["#","|","]"]: p += 1
  115.         elif c == "@": # Skip backward
  116.             p -= 1
  117.             while p > 0 and prog[p] not in ["@","|","["]: p -= 1
  118.         else: continue # Unknown
  119.        
  120.     stack = [] # Reset stack after program end
  121.  
  122. # Throws an error if the stack contains fewer values than the given "expected" parameter
  123. def checkStackSize(expected,pos):
  124.     if len(stack) < expected:
  125.         print("\n[P:"+str(pos+1)+"] StackError: Expected at least "+str(expected)+" values in stack, got "+str(len(stack)))
  126.         exit()
  127.  
  128. # Testing entry point; enter a program into prog to execute
  129. prog = """
  130. """
  131. #main(prog)
  132.  
  133. # Main entry point; gets the command line argument or prompts for a path, then runs.
  134. main("91+,"+(open(sys.argv[1]).read() if len(sys.argv) > 1 else open(input("Please enter a path:\n> "),"r").read()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement