00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <assert.h>
00039 #include <stdint.h>
00040 #include <stdlib.h>
00041 #include <dirent.h>
00042 #include <fstream>
00043 #include <iostream>
00044 #include <string>
00045 #include <sstream>
00046
00047 #include <libsherpa/UExcept.hxx>
00048
00049 #include "AST.hxx"
00050 #include "Type.hxx"
00051 #include "inter-pass.hxx"
00052
00053 using namespace boost;
00054 using namespace sherpa;
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 static unsigned long ltmpCounter = 0;
00101
00102 static shared_ptr<AST>
00103 LetWrap(shared_ptr<AST> ast)
00104 {
00105 std::stringstream ss;
00106 ss << "_ltmp" << ltmpCounter++;
00107
00108 shared_ptr<AST> let = AST::make(at_let, ast->loc);
00109 shared_ptr<AST> letBindings = AST::make(at_letbindings, ast->loc);
00110 shared_ptr<AST> binding = AST::make(at_letbinding, ast->loc);
00111 shared_ptr<AST> idPattern = AST::make(at_identPattern, ast->loc);
00112 shared_ptr<AST> id = AST::make(at_ident, ast->loc);
00113 shared_ptr<AST> useid = AST::make(at_ident, ast->loc);
00114
00115 let->addChild(letBindings);
00116 let->addChild(useid);
00117 let->addChild(AST::make(at_constraints));
00118
00119 letBindings->addChild(binding);
00120
00121 binding->addChild(idPattern);
00122 binding->addChild(ast);
00123
00124 idPattern->addChild(id);
00125
00126 useid->s = id->s = ss.str();
00127 useid->flags |= ID_IS_GENSYM;
00128 id->flags |= ID_IS_GENSYM;
00129 useid->identType = id->identType = id_value;
00130 assert(!ast->scheme);
00131
00132 useid->symType = id->symType = ast->symType;
00133 useid->symbolDef = id;
00134 useid->fqn.ident = id->fqn.ident = id->s;
00135
00136 return let;
00137 }
00138
00139
00140
00141
00142 void
00143 MakeFrame(shared_ptr<AST> ast, shared_ptr<AST> frameBindings)
00144 {
00145 switch(ast->astType) {
00146 case at_let:
00147 ast->astType = at_begin;
00148 break;
00149
00150 case at_letbinding:
00151 {
00152 assert(ast->child(0)->astType = at_identPattern);
00153
00154
00155
00156 frameBindings->addChild(ast->child(0));
00157
00158
00159 ast->astType = at_setbang;
00160 ast->symType = ast->child(0)->symType;
00161 }
00162 default:
00163 break;
00164 }
00165
00166
00167 for (size_t i = 0; i < ast->children.size(); i++) {
00168 MakeFrame(ast->child(i), frameBindings);
00169 }
00170 }
00171
00172
00173 void
00174 LetInsert(shared_ptr<AST> ast, bool skip = false)
00175 {
00176 switch(ast->astType) {
00177 case at_apply:
00178 case at_struct_apply:
00179 case at_object_apply:
00180 case at_ucon_apply:
00181 {
00182
00183
00184
00185 for (size_t c = 0; c < ast->children.size(); c++) {
00186 shared_ptr<AST> child = ast->child(c);
00187 LetInsert(child);
00188 }
00189
00190 if (!skip)
00191 ast = LetWrap(ast);
00192
00193 break;
00194 }
00195
00196 case at_letbinding:
00197 {
00198 LetInsert(ast->child(1),
00199 (ast->child(0)->astType == at_identPattern));
00200 break;
00201 }
00202 default:
00203 break;
00204 }
00205
00206 for (size_t i = 0; i < ast->children.size(); i++) {
00207 LetInsert(ast->child(i));
00208 }
00209 }