Skip to content

Instantly share code, notes, and snippets.

@aurorapar
Last active April 28, 2017 11:32
Show Gist options
  • Save aurorapar/8e11c65c0ac62636974482ebeeab05e1 to your computer and use it in GitHub Desktop.
Save aurorapar/8e11c65c0ac62636974482ebeeab05e1 to your computer and use it in GitHub Desktop.
Interpreter for class
'''
PREFACE
This is probably the worst code I have ever written. The optimiation is non-existant,
the rules have little forethought and planning, and only the minimal of the procedures
that were studied in class were applied.
This software should not be taken as representation of my abilities, but more as
what a caffiene binge throughout the night can produce.
'''
import sys, os, random, string
DEBUG = True
keywords = ['for', 'while', 'if', 'write', 'writeln', 'break', 'random']
controlStatements = ['for', 'while', 'if', 'break']
hasArguments = ['write', 'writeln', 'random']
variableDeclarations = []
for x in range(8, 13+1):
variableDeclarations.append(list(string.ascii_lowercase)[x])
comparisons = ["=", "==", "<=", ">=", ">", "<", '!=']
operators = ['%']
variables = [{}]
stack = variables[-1]
def debug(output):
if DEBUG:
print('"%s"'%output)
def throwError(error, line, lineNumber):
print(error)
print('Line %s : "%s"'%(lineNumber, line))
sys.exit()
def increaseStack():
global stack, variables
#debug("Stack before increase: %s"%variables)
variables.append({})
#debug("Stack after change: %s"%variables)
stack = variables[-1]
#debug("Last entry on stack: %s"%stack)
def decreaseStack():
global stack, variables
#debug("Stack before decrease: %s"%variables)
variables.pop()
#debug("Stack after change: %s"%variables)
stack = variables[-1]
#debug("Last entry on stack: %s"%stack)
def addOutput(outputCode, outputFile, newLine):
with open(outputFile, 'a') as file:
if newLine:
code = " " * 4 * 2
code += " " * ((len(variables) - 2) * 4)
code += outputCode
file.write(code)
else:
file.write(outputCode)
def interpret(line, number, outputFile, newLine=False):
if 'int' in str(type(line)):
return line
if line.isnumeric():
return line
line = line.replace("\n", "")
if len(line.strip()) < 1:
return 0
items = line.strip()
items = items.split(" ")
if items[0].startswith("#"):
return 0
if items[0] == 'break':
#Decrease stack
addOutput("}\n", outputFile, newLine)
decreaseStack()
return 0
if newLine:
spaces = 0
for x in line:
if x == " ":
spaces += 1
else:
break
if spaces % 4 != 0:
throwError("Improper indenting, expected %s"%(len(stack) * 4), line, number)
tabs = spaces / 4
if tabs != len(variables) - 1:
throwError("Improper indenting", line, number)
word = items[0].split("(")
if word[0] not in keywords:
if len(word[0]) == 1:
if word[0] not in variableDeclarations:
throwError("Invalid variable declaration", line, number)
else:
if len(items) > 1:
character = items[0]
declared = False
if character in stack:
declared = True
if not declared:
for x in range(1, len(variables)):
if len(variables[-x]) > 0:
for y in variables[-x]:
if character in y:
declared = True
break
if items[1] not in comparisons:
if items[1] not in operators:
throwError("Unknown operator", line, number)
else:
return "%s %s %s"%\
(interpret(items[0],number,outputFile),\
items[1],\
interpret(items[2],number,outputFile))\
if items[1] == "=":
if items[2] != 'array':
toInterpret = " ".join(str(x) for x in items[2:len(items)])
#debug("To interpret: %s"%toInterpret)
#debug("Interpreted: %s"%interpret(toInterpret, line, outputFile))
code = "%s %s %s;\n"%(items[0], items[1], interpret(toInterpret, line, outputFile))
if not declared:
stack[items[0]] = interpret(toInterpret, line, outputFile)
code = "int " + code
addOutput(code, outputFile, newLine)
else:
if declared:
throwError("Array already declared", line, number)
code = "int[] %s %s new int[%s];\n"%(character, items[1], interpret(items[3], number, outputFile))
stack[items[0]] = []
addOutput(code, outputFile, newLine)
return items[0]
else:
var = items[0]
character = var[0]
if len(var) in [1,4] and character in variableDeclarations:
declared = False
if character in stack:
declared = True
if not declared:
for x in range(0, len(variables)):
if len(variables[-x]) > 0:
#debug(variables[-x])
#debug(variables[-x].keys())
for y in variables[-x].keys():
if character == y:
declared = True
break
if len(var) == 1:
toInterpret = " ".join(str(x) for x in items[2:len(items)])
outputCode = "%s %s %s;\n"%(items[0], items[1], interpret(toInterpret, number, outputFile))
if not declared:
outputCode = "int " + outputCode
stack[var] = interpret(toInterpret, number, outputFile)
addOutput(outputCode, outputFile, newLine)
return 0
if len(var) == 4:
index = interpret(var[2], number, outputFile)
if len(items) == 3:
toInterpret = " ".join(str(x) for x in items[2:len(items)])
outputCode = ""
if not declared:
throwError("Declare arrays before use", line, number)
else:
outputCode = "%s[%s-1] = %s;\n"%(character, index, interpret(toInterpret, number, outputFile))
addOutput(outputCode, outputFile, outputFile)
return 0
else:
if not declared:
throwError("%s was not declared"%var, line, number)
#debug("Should be returned here")
return "%s[%s-1]"%(var[0],index)
#Is a keyword
else:
if items[0] == 'for':
if len(items) != 5:
throwError("Improper usage of %s"%items[0], line, lineNumber)
increaseStack()
for x in [2,3,4]:
items[x] = interpret(items[x], number, outputFile)
stack[items[1]] = items[2]
addOutput("for(int %s = %s; %s <= %s; %s += %s)\n"%\
(items[1], items[2], items[1], items[3], items[1], items[4]), outputFile, newLine)
addOutput("{\n", outputFile, True)
if items[0] == 'while':
#debug("while loop")
if len(items) != 4:
throwError("Improper usage of %s"%items[0], line, lineNumber)
increaseStack()
for x in [1,3]:
items[x] = interpret(items[x], number, outputFile)
if items[2] not in comparisons:
throwError("Invalid comparison: %s"%(items[2], line, number))
code = "while(%s %s %s)"%(items[1], items[2], items[3])
addOutput(code+"\n", outputFile, newLine)
addOutput("{\n", outputFile, True)
if items[0] == 'if':
if len(items) != 4:
throwError("Improper usage of %s"%items[0], line, lineNumber)
increaseStack()
for x in [1,3]:
items[x] = interpret(items[x], number, outputFile)
if items[2] not in comparisons:
throwError("Invalid comparison: %s"%(items[2], line, number))
code = "if(%s %s %s)\n"%(items[1], items[2], items[3])
addOutput(code, outputFile, newLine)
addOutput("{\n", outputFile, True)
if word[0] == 'writeln':
if word[1] != ")":
throwError("writeln() takes no parameters", line, number, outputFile)
addOutput("System.out.println();\n", outputFile, newLine)
if word[0] == 'write':
parts = line.strip().replace("\n","")
parts = parts.replace("write(", "")
parts = list(parts)
parts = parts[0:len(parts)-1]
output = "".join(str(x) for x in parts)
parts = output.split(",")
for x in range(0, len(parts)):
if not parts[x].strip().startswith('"'):
parts[x] = "Integer.toString(%s)"%interpret(parts[x].strip(), number, outputFile)
output = "+".join(str(x) for x in parts)
output = "System.out.print(%s);\n"%output
addOutput(output, outputFile, newLine)
if word[0] == 'random':
if "(" in word[1]:
throwError("Currently do not support nested functions", line, number)
output = word[1].split(",")
if len(output) != 2:
throwError("Unsupported number of arguments", line, number)
output[-1] = output[-1].replace(")", "")
for x in range(0, len(output)):
output[x] = interpret(output[x], number, outputFile)
code = "rng.nextInt(%s) + %s"%(int(output[1])-int(output[0]), output[0])
if not newLine:
return code
else:
addOutput(code+ ";", outputFile, False)
for arg in sys.argv:
if arg != sys.argv[0]:
if arg.endswith(".4z"):
debug("Calling interpreter")
if os.path.exists(sys.argv[1]):
output = sys.argv[1].replace(".4z", ".java")
if not os.path.exists(output):
file = open(output, 'w+')
file.close()
else:
print("Overwriting %s..."%output)
with open(output, 'w') as file:
file.write("import java.util.*;\n")
file.write("\n\n")
file.write("class %s\n"%sys.argv[1].replace(".4z", ""))
file.write("\n")
file.write("{\n")
file.write(" public static void main(String[] args)\n")
file.write(" {\n")
file.write(" Random rng = new Random();\n")
with open(sys.argv[1], 'r') as program:
lineNumber = 1
for line in program:
interpret(line, lineNumber, output, True)
lineNumber += 1
print("Program closed")
with open(output, 'a') as file:
file.write("\n }\n")
file.write("}")
else:
throwError("%s was not found on the system"%script, "Parameters", sys.argv)
else:
print("%s is not a valiid FORTRANZ script"%arg)
5 8 5 7 2 8 7 4 1 2
+ 6 + 5 + 1 + 7 + 2 + 7 + 5 + 1 + 3 + 6
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
5 1 5 6 4 5 8 4 6 7
+ 8 + 5 + 5 + 3 + 1 + 2 + 3 + 5 + 3 + 2
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
8 3 1 7 4 6 4 2 4 7
+ 2 + 8 + 7 + 4 + 4 + 1 + 6 + 5 + 1 + 3
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
3 2 1 7 8 4 4 3 3 4
+ 3 + 5 + 7 + 2 + 2 + 1 + 1 + 7 + 8 + 4
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
4 8 4 5 5 6 8 5 4 6
+ 8 + 4 + 6 + 5 + 2 + 5 + 5 + 3 + 6 + 7
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
2 4 2 3 2 6 3 3 1 8
+ 8 + 3 + 1 + 6 + 3 + 1 + 4 + 6 + 5 + 1
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
2 6 5 4 2 7 7 1 3 1
+ 5 + 7 + 4 + 2 + 8 + 1 + 5 + 2 + 8 + 6
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
1 1 7 5 3 3 8 5 1 4
+ 7 + 3 + 3 + 3 + 5 + 2 + 5 + 2 + 8 + 2
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
1 4 3 3 6 8 1 6 3 7
+ 4 + 8 + 5 + 3 + 3 + 4 + 1 + 1 + 5 + 7
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
4 5 2 8 7 5 5 2 6 6
+ 2 + 3 + 2 + 8 + 3 + 4 + 4 + 3 + 6 + 8
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
27 14 28 10 27 21 6 26 16 25
- 2 - 5 - 6 - 8 - 8 - 8 - 5 - 5 - 5 - 5
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
25 7 27 19 15 20 20 7 23 27
- 7 - 7 - 2 - 3 - 7 - 3 - 2 - 8 - 3 - 8
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
9 14 13 20 9 20 22 5 19 15
- 8 - 4 - 7 - 4 - 8 - 3 - 6 - 6 - 2 - 6
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
21 5 23 22 27 13 22 25 5 23
- 6 - 7 - 6 - 4 - 8 - 8 - 2 - 3 - 7 - 4
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
23 14 13 7 13 26 12 16 9 16
- 5 - 8 - 3 - 3 - 4 - 8 - 7 - 6 - 3 - 8
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
16 8 22 13 5 27 26 7 7 25
- 5 - 5 - 3 - 6 - 5 - 4 - 8 - 2 - 2 - 5
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
10 13 12 7 11 22 28 13 10 9
- 8 - 4 - 5 - 7 - 2 - 8 - 5 - 2 - 7 - 6
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
25 15 5 16 21 15 25 8 14 5
- 5 - 5 - 4 - 2 - 1 - 3 - 1 - 2 - 2 - 4
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
25 14 10 18 23 19 23 29 12 22
- 8 - 3 - 5 - 4 - 2 - 7 - 3 - 3 - 3 - 2
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
20 24 15 19 15 23 8 23 21 16
- 3 - 8 - 8 - 4 - 2 - 4 - 8 - 3 - 4 - 7
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
7 5 8 6 1 8 1 8 8 5
* 8 * 2 * 4 * 8 * 1 * 7 * 1 * 5 * 5 * 5
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
6 6 6 4 5 3 7 6 6 5
* 8 * 5 * 6 * 2 * 8 * 4 * 6 * 2 * 4 * 6
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
3 6 4 8 4 5 1 4 5 5
* 3 * 5 * 2 * 8 * 8 * 5 * 5 * 5 * 3 * 8
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
1 4 3 5 4 6 6 8 3 3
* 5 * 2 * 2 * 3 * 4 * 6 * 7 * 7 * 8 * 2
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
4 5 1 6 7 4 4 5 8 3
* 7 * 2 * 4 * 7 * 2 * 7 * 5 * 4 * 2 * 7
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
5 5 5 5 2 6 1 2 3 5
* 2 * 1 * 5 * 6 * 1 * 7 * 7 * 3 * 3 * 3
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
4 2 7 8 3 2 3 8 4 5
* 8 * 7 * 7 * 5 * 5 * 7 * 5 * 1 * 6 * 5
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
2 3 6 7 5 3 3 2 7 8
* 3 * 3 * 8 * 2 * 5 * 3 * 4 * 3 * 3 * 5
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
8 4 3 4 5 7 5 8 1 3
* 5 * 6 * 2 * 1 * 2 * 5 * 1 * 2 * 3 * 4
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
4 3 2 2 3 3 4 1 1 7
* 5 * 1 * 7 * 3 * 4 * 6 * 8 * 8 * 4 * 2
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
6) 6 5) 5 4)16 2)26 4)28 4)24 5)20 5)15 2)20 7)21
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
7)21 5)15 7)28 4) 8 7)28 2)10 3) 6 4)28 6)12 8)24
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
4)20 7)14 8) 8 2)24 8)24 7)14 2)12 7)21 5)10 3)21
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
4)12 8)24 2) 8 2)26 6)24 3)27 6)12 5)10 4) 8 6)24
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
6)24 6)12 6)12 5)25 2)22 2) 8 6)24 7)28 6)24 8)16
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
6)24 5)25 8)16 5)10 7)21 7)21 4)24 4)20 2)26 5)15
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
7)28 2)14 8)16 4)16 6)12 5)20 8)24 4)20 4) 8 4)28
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
8)16 3)21 2)26 5)15 8)16 3)27 4)28 3) 9 3)12 2)26
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
6)18 5)15 5)10 8)16 4)20 8)16 6)24 8)24 4)24 2)18
___ ___ ___ ___ ___ ___ ___ ___ ___ ___
4)12 7)21 3)15 5)15 7)14 5)15 6)12 2)18 2)20 2) 8
E:\Users\Aurora\Documents\School\CS415\interpreter>
# addition
#row
for i 1 10 1
#column
for j 1 10 1
m = random(1,9)
write(" ", m)
write(" ")
break
writeln()
for j 1 10 1
m = random(1,9)
write("+ ", m)
write(" ")
break
writeln()
for j 1 10 1
write("___")
write(" ")
break
writeln()
writeln()
writeln()
break
writeln()
# subtraction
#row
n = array 10
for i 1 10 1
#column
for j 1 10 1
n[i] = random(5,30)
if n[i] < 10
write(" ", n[i])
break
if n[i] > 9
write(" ", n[i])
break
write(" ")
break
writeln()
for j 1 10 1
m = random(2,9)
while n[i] < m
m = random(1,9)
break
write("- ", m)
write(" ")
break
writeln()
for j 1 10 1
write("___")
write(" ")
break
writeln()
writeln()
writeln()
break
writeln()
for i 1 10 1
#column
for j 1 10 1
m = random(1,9)
write(" ", m)
write(" ")
break
writeln()
for j 1 10 1
m = random(1,9)
write("* ", m)
write(" ")
break
writeln()
for j 1 10 1
write("___")
write(" ")
break
writeln()
writeln()
writeln()
break
writeln()
for i 1 10 1
#column
for j 1 10 1
write(" ___")
write(" ")
break
writeln()
for j 1 10 1
k = random(2,9)
write(k, ")")
m = random(5,30)
l = m % k
while l != 0
m = random(5,30)
if m <= k
m = random(5,30)
break
l = m % k
break
if m > 9
write(m)
write(" ")
break
if m < 10
write(" ",m, " ")
break
break
writeln()
writeln()
writeln()
writeln()
break
writeln()
import java.util.*;
class tables
{
public static void main(String[] args)
{
Random rng = new Random();
for(int i = 1; i <= 10; i += 1)
{
for(int j = 1; j <= 10; j += 1)
{
int m = rng.nextInt(8) + 1;
System.out.print(" "+Integer.toString(m));
System.out.print(" ");
}
System.out.println();
for(int j = 1; j <= 10; j += 1)
{
int m = rng.nextInt(8) + 1;
System.out.print("+ "+Integer.toString(m));
System.out.print(" ");
}
System.out.println();
for(int j = 1; j <= 10; j += 1)
{
System.out.print("___");
System.out.print(" ");
}
System.out.println();
System.out.println();
System.out.println();
}
System.out.println();
int[] n = new int[10];
for(int i = 1; i <= 10; i += 1)
{
for(int j = 1; j <= 10; j += 1)
{
n[i-1] = rng.nextInt(25) + 5;
if(n[i-1] < 10)
{
System.out.print(" "+Integer.toString(n[i-1]));
}
if(n[i-1] > 9)
{
System.out.print(" "+Integer.toString(n[i-1]));
}
System.out.print(" ");
}
System.out.println();
for(int j = 1; j <= 10; j += 1)
{
int m = rng.nextInt(7) + 2;
while(n[i-1] < m)
{
m = rng.nextInt(8) + 1;
}
System.out.print("- "+Integer.toString(m));
System.out.print(" ");
}
System.out.println();
for(int j = 1; j <= 10; j += 1)
{
System.out.print("___");
System.out.print(" ");
}
System.out.println();
System.out.println();
System.out.println();
}
System.out.println();
for(int i = 1; i <= 10; i += 1)
{
for(int j = 1; j <= 10; j += 1)
{
int m = rng.nextInt(8) + 1;
System.out.print(" "+Integer.toString(m));
System.out.print(" ");
}
System.out.println();
for(int j = 1; j <= 10; j += 1)
{
int m = rng.nextInt(8) + 1;
System.out.print("* "+Integer.toString(m));
System.out.print(" ");
}
System.out.println();
for(int j = 1; j <= 10; j += 1)
{
System.out.print("___");
System.out.print(" ");
}
System.out.println();
System.out.println();
System.out.println();
}
System.out.println();
for(int i = 1; i <= 10; i += 1)
{
for(int j = 1; j <= 10; j += 1)
{
System.out.print(" ___");
System.out.print(" ");
}
System.out.println();
for(int j = 1; j <= 10; j += 1)
{
int k = rng.nextInt(7) + 2;
System.out.print(Integer.toString(k)+ ")");
int m = rng.nextInt(25) + 5;
int l = m % k;
while(l != 0)
{
m = rng.nextInt(25) + 5;
if(m <= k)
{
m = rng.nextInt(25) + 5;
}
l = m % k;
}
if(m > 9)
{
System.out.print(Integer.toString(m));
System.out.print(" ");
}
if(m < 10)
{
System.out.print(" "+Integer.toString(m)+ " ");
}
}
System.out.println();
System.out.println();
System.out.println();
System.out.println();
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment