-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramToEvaluate.py
More file actions
55 lines (43 loc) · 2.12 KB
/
programToEvaluate.py
File metadata and controls
55 lines (43 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from expressions.effectType import EffectType
from expressions.expression import Expression
from expressions.pointer import Pointer
from securityLevel import SecurityLevel
from statements.statement import Statement
class ProgramToEvaluate:
statement: Statement = None
variables = {}
@staticmethod
def evaluate():
try:
ProgramToEvaluate.statement.evaluate()
print("Program is safe!")
except Exception as e:
print("Error!\n{}".format(e))
#ProgramToEvaluate.statement.evaluate()
@staticmethod
def startAst(programText: str):
from customAst import CustomAST
ProgramToEvaluate.statement = CustomAST.getAstStatement(programText, 1)
@staticmethod
def loadImportedVariables(importedVariablesText: str):
importedVariablesSplitted = importedVariablesText.split('\n')
for importedVariable in importedVariablesSplitted:
securityLevel = importedVariable.split(":")[1].split(",")[0]
type = importedVariable.split(":")[1].split(",",1)[1]
ProgramToEvaluate.variables[importedVariable.split(":")[0]] = EffectType(securityLevel, type)
@staticmethod
def addVariable(variable: Expression, securityLevel: str, effectTypeOfValueAssignedToVariable: EffectType):
if not isinstance(variable, Pointer):
raise Exception("Variable in ref({},{}) needs to be of type ref".format(variable.toString(), securityLevel))
if variable.name in ProgramToEvaluate.variables.keys():
raise Exception("Cannot declare variable that it is already declared")
ProgramToEvaluate.variables[variable.name] = EffectType(securityLevel, "ref;{}".format(effectTypeOfValueAssignedToVariable.toInputSchemaString()))
@staticmethod
def removeVariable(variable: Expression):
if not isinstance(variable, Pointer):
raise Exception("Variable ro be removed needs to be of type ref")
if variable.name not in ProgramToEvaluate.variables.keys():
raise Exception("Cannot remove variable {} because it does not exist in this scope".format(variable.name))
ProgramToEvaluate.variables.pop(variable.name)
def toString():
return ProgramToEvaluate.statement.toString()