-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.hpp
108 lines (90 loc) · 4.35 KB
/
parser.hpp
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
#pragma once
#include <list>
#include <map>
#include <string>
#include <vector>
#include "script.hpp"
namespace Interpreter {
class Parser : public CRefCountedThreadSafe<Parser> {
protected:
scoped_refptr<Script> mScript;
std::list<std::string*> mStringHolder;
std::string mScanningString;
bool mLogInstruction;
public:
Parser() : mScript(NULL), mLogInstruction(0), mStringHolder(), mScanningString() {}
~Parser() { Finish(); }
void Start(std::string name) { mScript = new Script(name); }
scoped_refptr<Script> Finish() {
scoped_refptr<Script> ret = mScript;
mScript = NULL;
mScanningString.clear();
std::list<std::string*>::iterator iter = mStringHolder.begin();
while (iter != mStringHolder.end()) {
delete (*iter);
iter++;
}
mStringHolder.clear();
return ret;
}
void SetLogInstruction(bool log) { mLogInstruction = log; }
//string parser helper function
void StartScanningString() { mScanningString.clear(); }
void AppendToScanningString(char ch) { mScanningString += ch; }
void AppendToScanningString(const char* text) { mScanningString += text; }
const char* FinishScanningString() { return CreateString(mScanningString.c_str()); }
const char* CreateString(const char* text) {
//TODO: use string pools
std::string* obj = new std::string(text);
mStringHolder.push_back(obj);
return obj->c_str();
}
//null instruction do nothing
Instruction* NULLObject();
//instruction list
Instruction* CreateList(const std::string& typeName, Instruction* element);
Instruction* AddToList(Instruction* list, Instruction* element);
//var declaration read & write
Instruction* VarDeclarationExpresion(const std::string& name, Instruction* value);
Instruction* VarUpdateExpression(const std::string& name, Instruction* value, int opcode);
Instruction* VarReadExpresion(const std::string& name);
//const values
Instruction* CreateConst(const std::string& value);
Instruction* CreateConst(long value);
Instruction* CreateConst(double value);
//function define & call function
Instruction* CreateFunction(const std::string& name, Instruction* formalParameters,
Instruction* body);
Instruction* CreateFunctionCall(const std::string& name, Instruction* actualParameters);
//arithmetic operation +-*/% ,> >= < <= == !=
Instruction* CreateArithmeticOperation(Instruction* first, Instruction* second, int opcode);
Instruction* CreateMinus(Instruction* value);
//condition expresion such as if else if
Instruction* CreateConditionExpresion(Instruction* condition, Instruction* action);
//if(...){} else if(...){} else{} statement
Instruction* CreateIFStatement(Instruction* one, Instruction* tow, Instruction* three);
//return statement
Instruction* CreateReturnStatement(Instruction* value);
//for statement and break statement
Instruction* CreateForStatement(Instruction* init, Instruction* condition, Instruction* op,
Instruction* body);
Instruction* CreateBreakStatement();
Instruction* CreateContinueStatement();
Instruction* CreateMapItem(Instruction* key, Instruction* value);
Instruction* CreateMap(Instruction* list);
Instruction* CreateArray(Instruction* list);
Instruction* VarReadAtExpression(const std::string& name, Instruction* where);
Instruction* VarReadAtExpression(const std::string& name, const std::string& where);
Instruction* VarReadAtExpression(Instruction* obj, Instruction* where);
Instruction* VarReadAtExpression(Instruction* obj, const std::string& where);
Instruction* VarUpdateAtExression(const std::string& name, Instruction* where,
Instruction* value);
Instruction* VarSlice(const std::string& name, Instruction* from, Instruction* to);
Instruction* CreateForInStatement(const std::string& key, const std::string& val,
Instruction* obj, Instruction* body);
Instruction* CreateSwitchCaseStatement(Instruction* value, Instruction* cases,
Instruction* defbranch);
//parser ending
void SetEntryPoint(Instruction* value) { mScript->EntryPoint = value; }
};
}; // namespace Interpreter