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
00039
00040
00041
00042 #include <assert.h>
00043 #include <stdint.h>
00044 #include <stdlib.h>
00045 #include <dirent.h>
00046 #include <fstream>
00047 #include <iostream>
00048 #include <sstream>
00049 #include <string>
00050
00051 #include <libsherpa/UExcept.hxx>
00052
00053 #include "AST.hxx"
00054 #include "inter-pass.hxx"
00055
00056 using namespace boost;
00057 using namespace sherpa;
00058
00082 static shared_ptr<AST>
00083 beginSimp(shared_ptr<AST> ast, std::ostream& errStream, bool &errFree)
00084 {
00085 for (size_t c = 0; c < ast->children.size(); c++)
00086 ast->child(c) = beginSimp(ast->child(c), errStream, errFree);
00087
00088 if (ast->astType == at_begin) {
00089
00090
00091 for (size_t c = 0; c < ast->children.size(); c++) {
00092 if (ast->child(c)->astType == at_define ||
00093 ast->child(c)->astType == at_recdef) {
00094 bool rec = (ast->child(c)->astType == at_recdef);
00095 shared_ptr<AST> def = ast->child(c);
00096
00097
00098
00099
00100
00101 if (def->child(0)->child(0)->astType == at_usesel) {
00102 errStream << def->loc << ": "
00103 << "Hygienically aliased names cannot be defined locally."
00104 << std::endl;
00105 errFree = false;
00106 }
00107
00108 shared_ptr<AST> letBinding =
00109 AST::make(at_letbinding,
00110 def->loc, def->child(0), def->child(1));
00111 if (rec)
00112 letBinding->flags |= LB_REC_BIND;
00113
00114
00115 def->child(0)->child(0)->flags &= ~ID_IS_GLOBAL;
00116 shared_ptr<AST> body = AST::make(at_begin, ast->child(c)->loc);
00117
00118 for (size_t bc = c+1; bc < ast->children.size(); bc++)
00119 body->addChild(ast->child(bc));
00120
00121 if (body->children.size() == 0) {
00122 errStream << def->loc << ": "
00123 << "definition not permitted as the "
00124 << "last expression in a sequece."
00125 << std::endl;
00126 errFree = false;
00127 }
00128
00129
00130
00131
00132 std::vector<shared_ptr<AST> > newChildren;
00133 for (size_t i=0; i <= c; i++)
00134 newChildren.push_back(ast->child(i));
00135 ast->children = newChildren;
00136
00137
00138 shared_ptr<AST> theLetRec =
00139 AST::make((rec ? at_letrec : at_let), def->loc,
00140 AST::make(at_letbindings, def->loc, letBinding),
00141 body, def->child(2));
00142 ast->child(c) = beginSimp(theLetRec, errStream, errFree);
00143
00144
00145 break;
00146 }
00147 }
00148 }
00149
00150 if (ast->astType == at_begin && ast->children.size() == 1)
00151 return ast->child(0);
00152
00153 return ast;
00154 }
00155
00156 bool
00157 UocInfo::fe_beginSimp(std::ostream& errStream,
00158 bool init, unsigned long flags)
00159 {
00160 DEBUG(BEG_SIMP) if (isSourceUoc())
00161 PrettyPrint(errStream);
00162
00163 DEBUG(BEG_SIMP) std::cerr << "fe_beginSimp" << std::endl;
00164 bool errFree = true;
00165 uocAst = beginSimp(uocAst, errStream, errFree);
00166
00167 DEBUG(BEG_SIMP) if (isSourceUoc())
00168 PrettyPrint(errStream);
00169
00170 return true;
00171 }