-
Notifications
You must be signed in to change notification settings - Fork 0
/
mython.cpp
130 lines (100 loc) · 2.23 KB
/
mython.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "object.h"
#include "object_holder.h"
#include "statement.h"
#include "test_runner.h"
#include "lexer.h"
#include "parse.h"
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void TestAll();
void RunMythonProgram(istream& input, ostream& output) {
Ast::Print::SetOutputStream(output);
Parse::Lexer lexer(input);
auto program = ParseProgram(lexer);
Runtime::Closure closure;
program->Execute(closure);
}
int main() {
TestAll();
RunMythonProgram(cin, cout);
return 0;
}
void TestSimplePrints() {
istringstream input(R"(
print 57
print 10, 24, -8
print 'hello'
print "world"
print True, False
print
print None
)");
ostringstream output;
RunMythonProgram(input, output);
ASSERT_EQUAL(output.str(), "57\n10 24 -8\nhello\nworld\nTrue False\n\nNone\n");
}
void TestAssignments() {
istringstream input(R"(
x = 57
print x
x = 'C++ black belt'
print x
y = False
x = y
print x
x = None
print x, y
)");
ostringstream output;
RunMythonProgram(input, output);
ASSERT_EQUAL(output.str(), "57\nC++ black belt\nFalse\nNone False\n");
}
void TestArithmetics() {
istringstream input(
"print 1+2+3+4+5, 1*2*3*4*5, 1-2-3-4-5, 36/4/3, 2*5+10/2"
);
ostringstream output;
RunMythonProgram(input, output);
ASSERT_EQUAL(output.str(), "15 120 -13 3 15\n");
}
void TestVariablesArePointers() {
istringstream input(R"(
class Counter:
def __init__():
self.value = 0
def add():
self.value = self.value + 1
class Dummy:
def do_add(counter):
counter.add()
x = Counter()
y = x
x.add()
y.add()
print x.value
d = Dummy()
d.do_add(x)
print y.value
)");
ostringstream output;
RunMythonProgram(input, output);
ASSERT_EQUAL(output.str(), "2\n3\n");
}
void TestAll() {
TestRunner tr;
Runtime::RunObjectHolderTests(tr);
Runtime::RunObjectsTests(tr);
Ast::RunUnitTests(tr);
Parse::RunLexerTests(tr);
TestParseProgram(tr);
RUN_TEST(tr, TestSimplePrints);
RUN_TEST(tr, TestAssignments);
RUN_TEST(tr, TestArithmetics);
RUN_TEST(tr, TestVariablesArePointers);
}