Skip to content

Commit

Permalink
parser: prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
dvshapkin committed Aug 20, 2021
1 parent 5eedcd9 commit 6f653b9
Show file tree
Hide file tree
Showing 8 changed files with 1,785 additions and 7 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ project(mython)

set(CMAKE_CXX_STANDARD 17)

add_executable(mython main.cpp
add_executable(mython
main.cpp
lexer.h lexer.cpp
runtime.h runtime.cpp
statement.h statement.cpp
parse.h parse.cpp
tests/test_runner_p.h
tests/lexer_test_open.cpp
tests/runtime_test.cpp)
tests/runtime_test.cpp
tests/statement_test.cpp
tests/parse_test.cpp)
115 changes: 110 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,139 @@
#include "lexer.h"
#include "parse.h"
#include "runtime.h"
#include "statement.h"
#include "tests/test_runner_p.h"

#include <iostream>

using namespace std;

namespace parse {
void RunOpenLexerTests(TestRunner& tr);
}
} // namespace parse

namespace ast {
void RunUnitTests(TestRunner& tr);
}
namespace runtime {
void RunObjectHolderTests(TestRunner& tr);
void RunObjectsTests(TestRunner& tr);
}
} // namespace runtime

void TestParseProgram(TestRunner& tr);

namespace {

void RunMythonProgram(istream& input, ostream& output) {
parse::Lexer lexer(input);
auto program = ParseProgram(lexer);

runtime::SimpleContext context{output};
runtime::Closure closure;
program->Execute(closure, context);
}

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;
//-------------------------------------- Lexer"
parse::RunOpenLexerTests(tr);
//-------------------------------------- Runtime"
runtime::RunObjectHolderTests(tr);
runtime::RunObjectsTests(tr);
ast::RunUnitTests(tr);
TestParseProgram(tr);

RUN_TEST(tr, TestSimplePrints);
RUN_TEST(tr, TestAssignments);
RUN_TEST(tr, TestArithmetics);
RUN_TEST(tr, TestVariablesArePointers);
}

} // namespace

int main() {
try {
TestAll();

RunMythonProgram(cin, cout);
} catch (const std::exception& e) {
std::cerr << e.what();
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
Expand Down
Loading

0 comments on commit 6f653b9

Please sign in to comment.