summaryrefslogtreecommitdiff
path: root/src/pim/Parser.h
blob: 19a2835e91a3401abdc69800ea22a03a7c5a08a5 (plain)
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
#pragma once

#include <string>
#include <cstring>
#include <sstream>
#include "Scanner.h"
#include "Generator.h"
#include "Token.h"
namespace pim
{
	namespace compiler
	{
		class ParserExpectException: public std::exception
		{
			char * error;
		public:
			ParserExpectException(Token token, int expectingSymbol) {
				error = strdup(std::string("Expecting " + Token::SymbolNames[expectingSymbol] + " got " + token.Source).c_str());
			}
			ParserExpectException(Token token, std::string expectingString) {
				error = strdup(std::string("Expecting " + expectingString + " got " + token.Source).c_str());
			}
			const char * what() const throw()
			{
				return error;
			}
			~ParserExpectException() throw() {};
		};
		class Parser
		{
			std::stringstream & source;
			Generator * generator;
			Scanner * scanner;
			Token token;
			Token lastToken;
			std::string breakLabel;
			std::string continueLabel;
			std::stack<Token> previousTokens;

			void program();
			void functionList();
			void function();
			void functionCall();
			void block();
			void argumentList();
			void argument();
			void declarationList();
			void declaration();
			void identifierList();
			void statementList();
			void statement();
			void neighbourStatement();
			void ifStatement();
			void condition(std::string jumpLabel);
			void assigmentStatement();
			void particleAction();
			void killStatement();
			void getStatement();
			void createStatement();
			void transformStatement();
			void expressionList();

			void expression();
			void term();
			void factor();
			void variableValue();

			Token forward();
			bool accept(int symbol);
			bool look(int symbol);
			void back();
			void expect(int symbol);
		public:
			Parser(std::stringstream & source_);

			std::vector<unsigned char> Compile();
		};
	}
}