Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- my FIRST attempt at writting a language interpreter
- here we go
- """
- d = {}
- def calc(line):
- calculated = False
- while not calculated:
- calculated = True
- if line.count("+") or line.count("-") or line.count("=") or line.count("*") or line.count(">") or line.count("<") or line.count("%"):
- calculated = False
- for i, word in enumerate(line): #do calculations
- if word == "+": #addition, works with ints and strings
- try:
- line[i] = line[i-1]+line[i+1]
- except TypeError:
- line[i] = str(line[i-1]) + str(line[i+1])
- del line[i+1]
- del line[i-1]
- elif word == "-": #subtraction, works with ints
- line[i] = line[i-1]-line[i+1]
- del line[i+1]
- del line[i-1]
- elif word == "*": #multiplication, strings and ints
- line[i] = line[i-1]*line[i+1]
- del line[i+1]
- del line[i-1]
- elif word == "/": #multiplication, strings and ints, rounds down
- line[i] = int(line[i-1]/line[i+1])
- del line[i+1]
- del line[i-1]
- elif word == "%": #mod, ints
- line[i] = line[i-1]%line[i+1]
- del line[i+1]
- del line[i-1]
- elif word == ">": #more then check
- if line[i-1] > line[i+1]:
- line[i] = "true"
- else: line[i] = "false"
- del line[i+1]
- del line[i-1]
- elif word == "<": #less then check
- if line[i-1] < line[i+1]:
- line[i] = "true"
- else: line[i] = "false"
- del line[i+1]
- del line[i-1]
- elif word == "=": #checks, works with everything
- if line[i-1] == line[i+1]:
- line[i] = "true"
- else: line[i] = "false"
- del line[i+1]
- del line[i-1]
- def read(string):
- global d
- ln = 0
- if type(string) == str:
- string = string.split("\n")
- for ln, line in enumerate(string):
- string[ln] = line.split(" ")
- try:
- for ln, line in enumerate(string):
- linnum = ln
- for i, word in enumerate(line): #make sure everything is the right data type
- try:
- line[i] = int(line[i]) #ints
- except: pass #you know how it works
- if word == '"': #Strings
- x, s = i+1, "" #turns everything inbetween two
- while line[x] != '"': #double quotes into one word
- s += line[x]+" " #syntax:
- del line[x] # " this is one word "
- del line[x]
- line[i] = s
- for i, word in enumerate(line): #update variables
- if word in d: #if word has been defined by player,
- try: #put definition in words place
- if line[i+1] == "->" or line[i+1] == ":": #unless it is being reassined
- pass
- else:
- for x in d[line[i]]:
- line.insert(i+1, x)
- del line[i]
- except:
- for x in d[line[i]]:
- line.insert(i+1, x)
- del line[i]
- if word == "while": #While loop
- param = line[i+1:] #Syntax:
- par = [] #while x:
- par.append(param+[]) #do this
- read(par) #do this
- loop = [] #end
- while string[ln+1] != ["end"]:
- loop.append(string[ln+1])
- del string[ln+1]
- if par[0] == ["true"]:
- looping = True
- while looping:
- run = []
- for x in loop: run.append(x + [])
- read(run)
- par = []
- par.append(param+[])
- read(par)
- if par[0] != ["true"]:
- looping = False
- else: pass
- if word == "dotims": #dotims (do times)
- n = int(line[i+1]) # repeats a line of code x amount of times
- del line[i+1] #Syntax:
- l=[] # dotims 3 code code end
- while line[i+1] != "end":
- l.append(line[i+1]+"")
- del line[i+1]
- del line[i+1]
- for x in range(n):
- read([l])
- elif word == ":": #functions
- d[line[i-1]] = [] #syntax:
- x = i+1 # name : do this ;
- while line[x] != ";":
- d[line[i-1]].append(line[x])
- del line[x]
- del line[x]
- d[line[i-1]].reverse()
- elif word == "call":
- line[i] = input()
- elif word == "//": #Comments
- del line[i:] #Syntax: code code // this is a comment
- calc(line)
- for i, word in enumerate(line): #run through, do keywords
- if word == "->": #set variable
- d[line[i-1]] = [line[i+1]] #syntax:
- # three -> 3
- elif word == "disp": #disp prints the next word
- print(line[i+1])
- elif word == "if": #if statement
- if line[i+1] == "true": #syntax:
- pass # if true do this end
- else: # if false do this end
- x = i+2
- while line[x] != "end":
- del line[x]
- del line[x]
- del line[line.index("if")]
- elif word == "D!":
- print(d)
- elif word == "L!":
- print(line)
- except:
- print("Error, LN : " + str(linnum))
- if __name__ == "__main__":
- cmd = ""
- while cmd != "quit":
- cmd = input("> ")
- if cmd[0:5] == "while":
- while cmd[-3:] != "end":
- cmd = cmd + "\n" + input("... ")
- read(cmd)
- # I will call this language: Wesley's Programing Language or WPL
Advertisement
Add Comment
Please, Sign In to add comment