Skip to content

Commit e370df0

Browse files
committedNov 8, 2017
working version with dictionary passing
1 parent 1f1957f commit e370df0

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed
 

‎network.py

+51-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from calc import *
22

3-
############################## Assignment functions ######################
3+
############################## General functions #########################
44
def eval_expr(expr, dic):
55
"""
66
Function recieves an expression, checks if it's a binexpr, if so,
@@ -18,6 +18,19 @@ def eval_expr(expr, dic):
1818
else:
1919
return expr
2020

21+
def parter(stmnts, dic):
22+
for stmnt in stmnts:
23+
if isassignment(stmnt):
24+
optional = assigner(stmnt, dic)
25+
elif isinput(stmnt):
26+
optional = inputter(stmnt, dic)
27+
elif isoutput(stmnt):
28+
outputter(stmnt, dic)
29+
elif isrepetition(stmnt):
30+
while_conditioner(stmnt, dic)
31+
elif isselection(stmnt):
32+
if_conditioner(stmnt, dic)
33+
############################## Assignment functions ######################
2134
def binary_calc(binexpr, dic):
2235
"""
2336
Function recieves a binexpr and first evaluates the operator then calls
@@ -34,11 +47,44 @@ def binary_calc(binexpr, dic):
3447
return eval_expr(binary_left(binexpr), dic) * eval_expr(binary_right(binexpr), dic)
3548

3649
# fråga carl om bättre sätt att föra vidare dictionaryn
37-
###########################
3850

51+
def assigner(stmnt, dic):
52+
dic[stmnt[1]] = eval_expr(stmnt[2], dic)
53+
return dic
54+
############################## Input functions ###########################
55+
def inputter(stmnt, dic):
56+
dic[stmnt[1]] = eval(input(("Enter value for %s: ") % stmnt[1]))
57+
return dic
58+
############################## Output functions ##########################
59+
def outputter(stmnt, dic):
60+
if stmnt[1] in dic:
61+
print (dic[stmnt[1]])
62+
else:
63+
print (stmnt[1])
64+
############################## Repetition functions #######################
65+
def while_conditioner(stmnt, dic):
66+
if condition_operator(stmnt[1]) == '<':
67+
while eval_expr(stmnt[1][0], dic) < eval_expr(stmnt[1][2], dic):
68+
parter(stmnt[2:], dic)
69+
elif condition_operator(stmnt[1]) == '>':
70+
while eval_expr(stmnt[1][0], dic) > eval_expr(stmnt[1][2], dic):
71+
parter(stmnt[2:], dic)
72+
elif condition_operator(stmnt[1]) == '=':
73+
while eval_expr(stmnt[1][0], dic) == eval_expr(stmnt[1][2], dic):
74+
parter(stmnt[2:], dic)
75+
############################## Selection functions ########################
76+
def if_conditioner(stmnt, dic):
77+
if condition_operator(stmnt[1]) == '<':
78+
if eval_expr(stmnt[1][0], dic) < eval_expr(stmnt[1][2], dic):
79+
parter(stmnt[2:], dic)
80+
elif condition_operator(stmnt[1]) == '>':
81+
if eval_expr(stmnt[1][0], dic) > eval_expr(stmnt[1][2], dic):
82+
parter(stmnt[2:], dic)
83+
elif condition_operator(stmnt[1]) == '=':
84+
if eval_expr(stmnt[1][0], dic) == eval_expr(stmnt[1][2], dic):
85+
parter(stmnt[2:], dic)
86+
############################## Final function
3987
def eval_program(calc_prog, optional = {}):
4088
if isprogram(calc_prog) and isstatements(program_statements(calc_prog)): #check if calc prog is a valid prog
41-
for stmnt in program_statements(calc_prog):
42-
if isassignment(stmnt):
43-
optional[stmnt[1]] = eval_expr(stmnt[2], optional)
89+
parter(program_statements(calc_prog), optional)
4490
return optional

0 commit comments

Comments
 (0)