1 | #include <jenga/include/smoothie/LexicalScoping.h>
|
---|
2 | #include <jenga/include/smoothie/SmoothieException.h>
|
---|
3 | #include <jenga/include/smoothie/Variable.h>
|
---|
4 | #include <jenga/include/smoothie/Procedure.h>
|
---|
5 |
|
---|
6 |
|
---|
7 | CScope::CScope( int level, int addr, SCOPE_TYPE TypeOfStatement ){
|
---|
8 | this->level = level;
|
---|
9 | this->StartAddress = addr;
|
---|
10 | this->TypeOfStatement = TypeOfStatement;
|
---|
11 |
|
---|
12 | pBreakSchedule = (DWORD *)malloc( 1 );
|
---|
13 | nBreakSchedule = 0;
|
---|
14 | }
|
---|
15 | CScope::~CScope(){
|
---|
16 | free( pBreakSchedule );
|
---|
17 | }
|
---|
18 |
|
---|
19 | int CScope::GetStartAddress(){
|
---|
20 | return StartAddress;
|
---|
21 | }
|
---|
22 | SCOPE_TYPE CScope::GetTypeOfStatement(){
|
---|
23 | return TypeOfStatement;
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 | CScope *CLexicalScopes::SearchScope( SCOPE_TYPE TypeOfStatement ){
|
---|
30 | for( int i = level; i>=0; i-- ){
|
---|
31 | if( ppScopes[i]->GetTypeOfStatement() == TypeOfStatement ){
|
---|
32 | return ppScopes[i];
|
---|
33 | }
|
---|
34 | }
|
---|
35 | return NULL;
|
---|
36 | }
|
---|
37 |
|
---|
38 | CLexicalScopes::CLexicalScopes(){
|
---|
39 | ppScopes = (CScope **)malloc( 1 );
|
---|
40 | level=0;
|
---|
41 | }
|
---|
42 | CLexicalScopes::~CLexicalScopes(){
|
---|
43 | free( ppScopes );
|
---|
44 | }
|
---|
45 | void CLexicalScopes::Init(int addr){
|
---|
46 | // TODO: エラーチェック
|
---|
47 |
|
---|
48 | level = -1;
|
---|
49 | Start( addr, SCOPE_TYPE_BASE );
|
---|
50 | }
|
---|
51 | void CLexicalScopes::Start( int addr, SCOPE_TYPE TypeOfStatement ){
|
---|
52 | level++;
|
---|
53 | ppScopes = (CScope **)realloc( ppScopes, ( level + 1 ) * sizeof( CScope * ) );
|
---|
54 | ppScopes[level] = CreateScope( level, addr, TypeOfStatement );
|
---|
55 | }
|
---|
56 | void CLexicalScopes::End(){
|
---|
57 | if( level <= 0 ){
|
---|
58 | throw SmoothieException();
|
---|
59 | return;
|
---|
60 | }
|
---|
61 |
|
---|
62 | //デストラクタを呼ぶ
|
---|
63 | CallDestructorsOfScopeEnd();
|
---|
64 |
|
---|
65 | Variables &vars = UserProc::IsGlobalAreaCompiling()?
|
---|
66 | globalVars :
|
---|
67 | UserProc::CompilingUserProc().localVars;
|
---|
68 |
|
---|
69 | //使用済みローカル変数の生存チェックを外す
|
---|
70 | BOOST_FOREACH( Variable *pVar, vars ){
|
---|
71 | if(pVar->bLiving&&pVar->ScopeLevel==level){
|
---|
72 | pVar->bLiving=0;
|
---|
73 | extern int obp;
|
---|
74 | pVar->ScopeEndAddress=obp;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | //スコープ抜け出しスケジュール
|
---|
80 | ppScopes[level]->RunScheduleOfBreak();
|
---|
81 |
|
---|
82 |
|
---|
83 | //スコープレベルを下げる
|
---|
84 | delete ppScopes[level];
|
---|
85 | level--;
|
---|
86 | }
|
---|
87 |
|
---|
88 | int CLexicalScopes::GetNowLevel(){
|
---|
89 | return level;
|
---|
90 | }
|
---|
91 | void CLexicalScopes::SetNowLevel( int level ){
|
---|
92 | this->level = level;
|
---|
93 | }
|
---|
94 | int CLexicalScopes::GetStartAddress(){
|
---|
95 | return ppScopes[level]->GetStartAddress();
|
---|
96 | }
|
---|