Guest User

WPL.py

a guest
Oct 16th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.49 KB | None | 0 0
  1. """
  2. my FIRST attempt at writting a language interpreter
  3. here we go
  4. """
  5.  
  6. d = {}
  7. def calc(line):
  8.     calculated = False
  9.     while not calculated:
  10.         calculated = True
  11.         if line.count("+") or line.count("-") or line.count("=") or line.count("*") or line.count(">") or line.count("<") or line.count("%"):
  12.             calculated = False
  13.  
  14.         for i, word in enumerate(line): #do calculations
  15.            
  16.             if word == "+":  #addition, works with ints and strings
  17.                 try:
  18.                     line[i] = line[i-1]+line[i+1]
  19.                 except TypeError:
  20.                     line[i] = str(line[i-1]) + str(line[i+1])
  21.                 del line[i+1]
  22.                 del line[i-1]
  23.            
  24.             elif word == "-": #subtraction, works with ints
  25.                 line[i] = line[i-1]-line[i+1]
  26.                 del line[i+1]
  27.                 del line[i-1]
  28.  
  29.             elif word == "*": #multiplication, strings and ints
  30.                 line[i] = line[i-1]*line[i+1]
  31.                 del line[i+1]
  32.                 del line[i-1]
  33.  
  34.             elif word == "/": #multiplication, strings and ints, rounds down
  35.                 line[i] = int(line[i-1]/line[i+1])
  36.                 del line[i+1]
  37.                 del line[i-1]
  38.  
  39.             elif word == "%": #mod, ints
  40.                 line[i] = line[i-1]%line[i+1]
  41.                 del line[i+1]
  42.                 del line[i-1]
  43.  
  44.             elif word == ">": #more then check
  45.                 if line[i-1] > line[i+1]:
  46.                     line[i] = "true"
  47.                 else: line[i] = "false"
  48.                 del line[i+1]
  49.                 del line[i-1]
  50.  
  51.             elif word == "<": #less then check
  52.                 if line[i-1] < line[i+1]:
  53.                     line[i] = "true"
  54.                 else: line[i] = "false"
  55.                 del line[i+1]
  56.                 del line[i-1]
  57.  
  58.             elif word == "=": #checks, works with everything
  59.                 if line[i-1] == line[i+1]:
  60.                     line[i] = "true"
  61.                 else: line[i] = "false"
  62.                 del line[i+1]
  63.                 del line[i-1]
  64.  
  65.  
  66.  
  67. def read(string):
  68.     global d
  69.     ln = 0
  70.     if type(string) == str:
  71.         string = string.split("\n")
  72.         for ln, line in enumerate(string):
  73.             string[ln] = line.split(" ")
  74.    
  75.     try:
  76.         for ln, line in enumerate(string):
  77.             linnum = ln
  78.             for i, word in enumerate(line): #make sure everything is the right data type
  79.  
  80.                 try:
  81.                     line[i] = int(line[i])   #ints
  82.                 except: pass                   #you know how it works
  83.  
  84.                 if word == '"':              #Strings
  85.                     x, s = i+1, ""             #turns everything inbetween two
  86.                     while line[x] != '"':      #double quotes into one word
  87.                         s += line[x]+" "       #syntax:
  88.                         del line[x]            # " this is one word "
  89.                     del line[x]                
  90.                     line[i] = s
  91.  
  92.  
  93.             for i, word in enumerate(line): #update variables
  94.  
  95.                 if word in d:                                           #if word has been defined by player,
  96.                     try:                                                #put definition in words place
  97.                         if line[i+1] == "->" or line[i+1] == ":":       #unless it is being reassined
  98.                             pass
  99.                         else:
  100.                             for x in d[line[i]]:      
  101.                                 line.insert(i+1, x)
  102.                             del line[i]
  103.                     except:
  104.                         for x in d[line[i]]:      
  105.                             line.insert(i+1, x)
  106.                         del line[i]
  107.  
  108.                 if word == "while":                         #While loop
  109.                     param = line[i+1:]                      #Syntax:
  110.                     par = []                                #while x:
  111.                     par.append(param+[])                    #do this
  112.                     read(par)                               #do this
  113.                     loop = []                               #end
  114.                     while string[ln+1] != ["end"]:
  115.                         loop.append(string[ln+1])
  116.                         del string[ln+1]
  117.                     if par[0] == ["true"]:
  118.                         looping = True
  119.                         while looping:
  120.                             run = []
  121.                             for x in loop: run.append(x + [])
  122.                             read(run)
  123.                             par = []
  124.                             par.append(param+[])
  125.                             read(par)
  126.                             if par[0] != ["true"]:
  127.                                 looping = False
  128.                     else: pass
  129.  
  130.                 if word == "dotims":                 #dotims (do times)
  131.                     n = int(line[i+1])               # repeats a line of code x amount of times
  132.                     del line[i+1]                    #Syntax:
  133.                     l=[]                             # dotims 3 code code end
  134.                     while line[i+1] != "end":
  135.                         l.append(line[i+1]+"")
  136.                         del line[i+1]
  137.                     del line[i+1]
  138.                     for x in range(n):
  139.                         read([l])
  140.  
  141.                 elif word == ":":                     #functions
  142.                     d[line[i-1]] = []                 #syntax:
  143.                     x = i+1                           # name : do this ;
  144.                     while line[x] != ";":
  145.                         d[line[i-1]].append(line[x])
  146.                         del line[x]
  147.                     del line[x]
  148.                     d[line[i-1]].reverse()
  149.                            
  150.                 elif word == "call":
  151.                     line[i] = input()
  152.  
  153.                 elif word == "//":                     #Comments
  154.                     del line[i:]                       #Syntax: code code  // this is a comment
  155.  
  156.             calc(line)
  157.             for i, word in enumerate(line): #run through, do keywords
  158.  
  159.                 if word == "->":              #set variable
  160.                     d[line[i-1]] = [line[i+1]]  #syntax:
  161.                                                 # three -> 3        
  162.  
  163.  
  164.                 elif word == "disp": #disp prints the next word
  165.                     print(line[i+1])
  166.  
  167.                 elif word == "if":                    #if statement
  168.                     if line[i+1] == "true":           #syntax:
  169.                         pass                          # if true do this end
  170.                     else:                             # if false do this end
  171.                         x = i+2
  172.                         while line[x] != "end":    
  173.                             del line[x]
  174.                         del line[x]
  175.                         del line[line.index("if")]
  176.  
  177.                 elif word == "D!":
  178.                     print(d)
  179.                 elif word == "L!":
  180.                     print(line)
  181.     except:
  182.         print("Error, LN : " + str(linnum))
  183.                                        
  184. if __name__ == "__main__":
  185.     cmd = ""
  186.     while cmd != "quit":
  187.         cmd = input("> ")
  188.         if cmd[0:5] == "while":
  189.             while cmd[-3:] != "end":
  190.                 cmd = cmd + "\n" + input("... ")
  191.            
  192.         read(cmd)
  193.  
  194.  
  195. # I will call this language: Wesley's Programing Language or WPL
Advertisement
Add Comment
Please, Sign In to add comment