Skip to content

Commit ca245ae

Browse files
committedNov 6, 2017
main part of labb6
1 parent 2d65ff6 commit ca245ae

File tree

2 files changed

+229
-0
lines changed

2 files changed

+229
-0
lines changed
 

‎calc.py

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# ----------------------------------------------------------------------------
2+
# Grammar for the Calc language
3+
# ----------------------------------------------------------------------------
4+
5+
"""
6+
7+
PROGRAM ::= [ 'calc', STATEMENTS ]
8+
9+
STATEMENTS ::= STATEMENT
10+
| STATEMENT, STATEMENTS
11+
12+
STATEMENT ::= ASSIGNMENT
13+
| REPETITION
14+
| SELECTION
15+
| INPUT
16+
| OUTPUT
17+
18+
ASSIGNMENT ::= [ 'set', VARIABLE, EXPRESSION ]
19+
20+
REPETITION ::= [ 'while', CONDITION, STATEMENTS ]
21+
22+
SELECTION ::= [ 'if', CONDITION, STATEMENT ]
23+
| [ 'if', CONDITION, STATEMENT, STATEMENT ]
24+
25+
INPUT ::= [ 'read', VARIABLE ]
26+
27+
OUTPUT ::= [ 'print', VARIABLE ]
28+
29+
EXPRESSION ::= CONSTANT
30+
| VARIABLE
31+
| BINARYEXPR
32+
33+
BINARYEXPR ::= [ EXPRESSION, BINARYOPER, EXPRESSION ]
34+
35+
CONDITION ::= [ EXPRESSION, CONDOPER, EXPRESSION ]
36+
37+
BINARYOPER ::= '+' | '-' | '*' | '/'
38+
39+
CONDOPER ::= '<' | '>' | '='
40+
41+
VARIABEL ::= a Python string
42+
43+
KONSTANT ::= a Python number
44+
45+
"""
46+
47+
# ----------------------------------------------------------------------------
48+
# Primitive functions for the Calc language constructs
49+
# ----------------------------------------------------------------------------
50+
51+
# ----- PROGRAM -----
52+
53+
def isprogram(p):
54+
return isinstance(p, list) and len(p) > 1 and p[0] == 'calc'
55+
56+
def program_statements(p):
57+
return p[1:]
58+
59+
# ----- STATEMENTS -----
60+
61+
def isstatements(p):
62+
isstmnt = lambda s: isassignment(s) or isrepetition(s) or isselection(s) \
63+
or isoutput(s) or isinput(s)
64+
return isinstance(p, list) and p and all(map(isstmnt, p))
65+
66+
def first_statement(p):
67+
return p[0]
68+
69+
def rest_statements(p):
70+
return p[1:]
71+
72+
def empty_statements(p):
73+
return not p
74+
75+
# ----- STATEMENT -----
76+
77+
# No functions for statements in general. Instead, see the differenct
78+
# types of statements: assignments, repetitions, selections, input
79+
# and output.
80+
81+
# ----- ASSIGNMENT -----
82+
83+
def isassignment(p):
84+
return isinstance(p, list) and len(p) == 3 and p[0] == 'set'
85+
86+
def assignment_variable(p):
87+
return p[1]
88+
89+
def assignment_expression(p):
90+
return p[2]
91+
92+
# ----- REPETITION -----
93+
94+
def isrepetition(p):
95+
return isinstance(p, list) and len(p) > 2 and p[0] == 'while'
96+
97+
def repetition_condition(p):
98+
return p[1]
99+
100+
def repetition_statements(p):
101+
return p[2:]
102+
103+
# ----- SELECTION -----
104+
105+
def isselection(p):
106+
return isinstance(p, list) and (3 <= len(p) <= 4) and p[0] == 'if'
107+
108+
def selection_condition(p):
109+
return p[1]
110+
111+
def selection_true(p):
112+
return p[2]
113+
114+
def hasfalse(p):
115+
return len(p) == 4
116+
117+
def selection_false(p):
118+
return p[3]
119+
120+
# ----- INPUT -----
121+
122+
def isinput(p):
123+
return isinstance(p, list) and len(p) == 2 and p[0] == 'read'
124+
125+
def input_variable(p):
126+
return p[1]
127+
128+
# ----- OUTPUT -----
129+
130+
def isoutput(p):
131+
return isinstance(p, list) and len(p) == 2 and p[0] == 'print'
132+
133+
def output_variable(p):
134+
return p[1]
135+
136+
# ----- EXPRESSION -----
137+
138+
# No functions for expressions in general. Instead, see the differenct
139+
# types of expressions: constants, variables and binary expressions.
140+
141+
# ----- BINARYEXPR -----
142+
143+
def isbinary(p):
144+
return isinstance(p, list) and len(p) == 3 and isbinaryoper(p[1])
145+
146+
def binary_operator(p):
147+
return p[1]
148+
149+
def binary_left(p):
150+
return p[0]
151+
152+
def binary_right(p):
153+
return p[2]
154+
155+
# ----- CONDITION -----
156+
157+
def iscondition(p):
158+
return isinstance(p, list) and len(p) == 3 and iscondoper(p[1])
159+
160+
def condition_operator(p):
161+
return p[1]
162+
163+
def condition_left(p):
164+
return p[0]
165+
166+
def condition_right(p):
167+
return p[2]
168+
169+
# ----- BINARYOPER -----
170+
171+
def isbinaryoper(p):
172+
return p in ['+', '-', '*', '/']
173+
174+
# ----- CONDOPER -----
175+
176+
def iscondoper(p):
177+
return p in ['<', '>', '=']
178+
179+
# ----- VARIABLE -----
180+
181+
def isvariable(p):
182+
return isinstance(p, str) and p != ""
183+
184+
# ----- CONSTANT -----
185+
186+
def isconstant(p):
187+
return isinstance(p, int) or isinstance(p, float)

‎network.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from calc import *
2+
3+
def poop(p, dic):
4+
if not p:
5+
return dic
6+
elif isassignment(p):
7+
dic[eval_expr(first_statement(p)), poop(rest_statements(p))
8+
9+
10+
11+
############################## Assignment functions ######################
12+
def assigner(assg_stmnt):
13+
14+
15+
def eval_expr(expr):
16+
if isbinary(expr):
17+
return binary_calc(expr)
18+
elif isconstant(expr) or isvariable(expr):
19+
return expr
20+
21+
def binary_calc(binexpr):
22+
if binary_operator(binexpr) == '+':
23+
return eval_expr(binary_left(binexpr)) + eval_expr(binary_right(binexpr))
24+
elif binary_operator(binexpr) == '-':
25+
return eval_expr(binary_left(binexpr)) - eval_expr(binary_right(binexpr))
26+
elif binary_operator(binexpr) == '/':
27+
return eval_expr(binary_left(binexpr)) / eval_expr(binary_right(binexpr))
28+
elif binary_operator(binexpr) == '*':
29+
return eval_expr(binary_left(binexpr)) * eval_expr(binary_right(binexpr))
30+
31+
###########################
32+
33+
def eval_program(calc_prog, optional = {}):
34+
if isprogram(calc_prog): #check if calc prog is a valid prog
35+
if isstatements(program_statements(calc_prog)):
36+
if isassignment(first_statement(program_statements(calc_prog))):
37+
optional[first_statement(program_statements(calc_prog))[1]] = eval_expr(first_statement(program_statements(calc_prog))[2])
38+
return optional
39+
40+
41+
42+

0 commit comments

Comments
 (0)
Please sign in to comment.