Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / src / main.cpp
blob: a10e4d1e5b71bbe8d201e770d2369cfa0e197670 (plain) (blame)
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
#include "Lexer.h"
#include "Parser.h"
#include "ASTPrinter.h"
#include "ASTInterpreter.h"
#include <iostream>
#include <cstring>

void RunParseCalc()
{
	IPLVector<Token> tokens;
	//Tokenize("var a = 0;  var b = a + 123;a+b;", tokens);
	Tokenize("var a = 5; if(a == 5){a++;}", tokens);

	// TODO make actual test :D
	auto expr = Parse(tokens);
	//ASTPrinter p(std::cout);
	//expr->Accept(p);
	ASTInterpreter i;
    std::cout << "Running" << std::endl;
	auto result = i.Run(expr.get());
    std::cout << "Stack after run, top to bottom:" << std::endl;
    while (!result.empty()) {
        std::cout << result.back() << std::endl;
        result.pop_back();
    }
	std::cout << "S = " << i.ModifyVariable("a") << std::endl;
}

class InterpreterPrinter : public ASTInterpreter::Printer
{
public:
	virtual void PrintVariable(const char* name, double value) override
	{
		std::cout << name << ": " << value << std::endl;
	}
};

void InteractiveInterpreter()
{
	char command[1000];
	ASTInterpreter i;
	InterpreterPrinter p;
	do
	{
		std::cin.getline(command, 1000);
		if (std::strcmp(command, "exit") == 0)
		{
			break;
		}
		if (std::strcmp(command, "print") == 0)
		{
			i.Print(p);
			continue;
		}
		IPLVector<Token> tokens;
		Tokenize(command, tokens);
		auto result = i.Run(Parse(tokens).get());
		while (!result.empty()) {
			std::cout << result.back() << std::endl;
			result.pop_back();
		}
	} while (true);
}

int main()
{
	//RunParseCalc();
	InteractiveInterpreter();

#if defined(_WIN32)
	std::system("pause");
#endif
	return 0;
}