-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImpSyntaxParser.cpp
146 lines (120 loc) · 2.89 KB
/
ImpSyntaxParser.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* SyntaxParser.cpp
*
* Created on: 15.04.2013
* Author: urandon
*/
#include "ImpSyntaxParser.h"
#include <cstdlib>
#include <unistd.h>
#include <stdio.h>
template<typename T, size_t N>
inline size_t LengthOf(T (&x)[N]){
return N;
}
const char * ImpSyntaxParser::atom_essence[] = {
"[", "]", "(", ")", "{", "}",
":=", ":", ";", ",",
"def",
"if", "then", "else", "goto",
"buy", "sell", "prod", "build", "endturn",
"print",
"&", "|", "!",
"<", ">", "=",
"+", "-",
"/", "*", "%"
};
const int ImpSyntaxParser::PREDEFINED_ATOMS = LengthOf(ImpSyntaxParser::atom_essence);
/* ImpSyntaxParser::atom */
ImpSyntaxParser::atom::atom(enum atom_type mtype, int mid, int mline):
type(mtype), id(mid), line(mline) { } ;
ImpSyntaxParser::atom::atom(const lexem & lex):
id(lex.id), line(lex.line)
{
switch(lex.type){
case LexicalParser::NUMERIC :
type = NUMERIC;
break;
case LexicalParser::STRING :
type = STRING;
break;
case LexicalParser::VARIABLE :
type = VARIABLE;
break;
case LexicalParser::FUNCTION :
type = FUNCTION;
break;
case LexicalParser::LABEL :
type = LABEL;
break;
default:
type = (enum atom_type) id;
}
}
ImpSyntaxParser::atom::atom(const ImpSyntaxParser::atom & mAtom):
type(mAtom.type), id(mAtom.id), line(mAtom.line) { }
/* SyntaxParser::code_t */
ImpSyntaxParser::code_t::code_t():size(0),allocated(DEFAULT_SIZE){
data = (atom **) malloc(DEFAULT_SIZE * sizeof(char));
}
ImpSyntaxParser::code_t::~code_t(){
for(int i = 0; i < size; ++i){
delete data[i];
}
free(data);
}
void ImpSyntaxParser::code_t::push(atom * mAtom){
int newsize = size + 1;
if(newsize > allocated){
allocated *= 2;
data = (atom **) realloc(data, allocated);
}
data[size] = mAtom;
size = newsize;
}
/* Costructor & Destructor */
ImpSyntaxParser::ImpSyntaxParser(int fd):lo(0),hi(0) {
const int buffsize = 8;
char buffer[buffsize];
int length;
lexem * lex;
atom * mAtom;
do{
length = read(fd, buffer, buffsize);
if(length == 0){
if((lex = lexical.push(int(EOF))) != NULL){
mAtom = new atom(*lex);
delete lex;
code.push(mAtom);
}
} else {
for(int i = 0; i < length; ++i){
if((lex = lexical.push(int(buffer[i]))) != NULL){
mAtom = new atom(*lex);
delete lex;
code.push(mAtom);
}
}
}
} while(length != 0);
}
ImpSyntaxParser::~ImpSyntaxParser() { }
ImpSyntaxParser::atom * ImpSyntaxParser::current() { return code.data[lo]; }
ImpSyntaxParser::atom * ImpSyntaxParser::next() { return code.data[++lo]; }
ImpSyntaxParser::atom * ImpSyntaxParser::prev() { return code.data[--lo]; }
/* language-sensitive recursive descent methods */
/*
void SyntaxParser::Program() {
if(current()->type == DEFINITION){
Definition();
if(next()->type == DOT_N_COMMA){
Code();
} else {
throw UnexpectedLexemError(current()->line, int(current()->type), int(DOT_N_COMMA));
}
} else {
Code();
}
}
void SyntaxParser::Definition() {
} */