-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.cpp
More file actions
77 lines (67 loc) · 2.56 KB
/
util.cpp
File metadata and controls
77 lines (67 loc) · 2.56 KB
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
#include "lex.h"
#include "parser.h"
#include "token_types.h"
#include <vector>
using namespace std;
//##############################################################################
//## gets one scope from the point you told it to
std::vector<token> parser::
GetScope(vector<token> Tokens, int Start, int &End, int Open, int Close){
int Depth=0;
vector<token> ScopeTokens;
if( Start+2 >= Tokens.size() || Tokens[Start+1].Type == TOK_CLOSE_BRACE ) return ScopeTokens;
for(int i=Start; i<Tokens.size(); ++i){
if( Tokens[i].Type == Open ){
//if( Tokens[0].Type == TOK_FUNCTION_CALL && !Depth ){ ++i; }
if( Depth ) ScopeTokens.push_back(Tokens[i]);
Depth++;
}
else if( Tokens[i].Type == Close ){
Depth--;
if( Depth ){ ScopeTokens.push_back(Tokens[i]); }
else{
End = i;
return ScopeTokens;
}
}
else if( Depth ){
ScopeTokens.push_back(Tokens[i]);
}
}
End=Tokens.size()-1;
return ScopeTokens;
}
//##############################################################################
//## separates tokens into statements
vector< vector<token> > parser::
GetStatements( vector<token> Tokens, int Div, bool AllowBraceTerm, bool AddTail ){
vector< vector<token> > Statements;
int Begin = 0;
int Depth = 0;
for( int i=0; i<Tokens.size(); ++i ){
if( Tokens[i].Type == TOK_OPEN_BRACE || Tokens[i].Type == TOK_OPEN_PARENTHESIS
|| Tokens[i].Type == TOK_OPEN_BRACKET )
{
++Depth;
}
else if( Tokens[i].Type == TOK_CLOSE_BRACE || Tokens[i].Type == TOK_CLOSE_PARENTHESIS
|| Tokens[i].Type == TOK_CLOSE_BRACKET )
{
--Depth;
if( AllowBraceTerm && !Depth && Tokens[i].Type == TOK_CLOSE_BRACE ){
Statements.push_back( vector<token>(Tokens.begin()+Begin,Tokens.begin()+i+1) );
Begin = i+1;
}
}
else if( Tokens[i].Type == Div ){
if( !Depth ){
Statements.push_back( vector<token>(Tokens.begin()+Begin,Tokens.begin()+i) );
Begin = i+1;
}
}
}
if( AddTail && Begin <= Tokens.size() ){
Statements.push_back( vector<token>(Tokens.begin()+Begin,Tokens.end()) );
}
return Statements;
}