summaryrefslogtreecommitdiff
path: root/src/tools/parser.cpp
blob: a70dfa44d335cebe4ddfe984af44fd1860d8f96d (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <bu/parser.h>
#include <bu/lexer.h>
#include <bu/file.h>
#include <bu/sio.h>
#include <bu/queuebuf.h>
#include <stdlib.h>

using namespace Bu;

enum Tok
{
	tokNumber,
	tokPlus,
	tokMinus,
	tokDivide,
	tokMultiply,
	tokOpenParen,
	tokCloseParen,
	tokCompute,
	tokEndOfInput=-1
};

Bu::Formatter &operator<<( Bu::Formatter &f, Tok e )
{
	switch( e )
	{
		case tokNumber: return f << "tokNumber";
		case tokPlus: return f << "tokPlus";
		case tokMinus: return f << "tokMinus";
		case tokDivide: return f << "tokDivide";
		case tokMultiply: return f << "tokMultiply";
		case tokOpenParen: return f << "tokOpenParen";
		case tokCloseParen: return f << "tokCloseParen";
		case tokCompute: return f << "tokCompute";
		case tokEndOfInput: return f << "tokEndOfInput";
	}

	return f << "***error***";
}

class MathLexer : public Lexer
{
public:
	MathLexer( Bu::Stream &rSrc ) :
		rSrc( rSrc )
	{
	}

	virtual ~MathLexer()
	{
	}

	enum TokenTypes
	{
		tokStuff
	};

	virtual Token *nextToken()
	{
		for(;;)
		{
			if( qbIn.getSize() == 0 )
			{
				char buf[4096];
				qbIn.write( buf, rSrc.read( buf, 4096 ) );

				if( rSrc.isEos() && qbIn.getSize() == 0 )
					return new Token( tokEndOfInput );
			}

			char b;
			qbIn.peek( &b, 1 );
			switch( b )
			{
				case '+':
					qbIn.seek( 1 );
					return new Token( tokPlus );

				case '-':
					qbIn.seek( 1 );
					return new Token( tokMinus );

				case '/':
					qbIn.seek( 1 );
					return new Token( tokDivide );

				case '*':
					qbIn.seek( 1 );
					return new Token( tokMultiply );

				case ' ':
				case '\t':
				case '\n':
					qbIn.seek( 1 );
					break;

				case '=':
					qbIn.seek( 1 );
					return new Token( tokCompute );

				case '(':
					qbIn.seek( 1 );
					return new Token( tokOpenParen );

				case ')':
					qbIn.seek( 1 );
					return new Token( tokCloseParen );

				case '.':
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
					{
						Bu::FString sTmp;
						sTmp += b;
						qbIn.seek( 1 );
						for(;;)
						{
							qbIn.peek( &b, 1 );
							if( b != '.' && (b < '0' || b > '9') )
							{
								sio << "!! Convert '" << sTmp << "' to "
									<< strtod( sTmp.getStr(), NULL ) << sio.nl;
								return new Token(
									tokNumber, strtod( sTmp.getStr(), NULL )
									);
							}
							qbIn.seek( 1 );
							sTmp += b;
						}
					}
					break;

				default:
					throw Bu::ExceptionBase("Unexpected character '%c'.", b );
			}
		}
	}

private:
	Bu::Stream &rSrc;
	QueueBuf qbIn;
};

int main( int argc, char *argv[] )
{
	File fIn( argv[1], File::Read );

	Parser p;

	p.pushLexer( new MathLexer( fIn ) );

	p.parse();

	return 0;
}