1 | #include <vector>
|
---|
2 | #include <algorithm>
|
---|
3 |
|
---|
4 | #include "Processor.h"
|
---|
5 | #include "SynTree/Declaration.h"
|
---|
6 |
|
---|
7 | namespace Designators {
|
---|
8 | Matcher::Matcher( const std::list< DeclarationWithType * > &decls ) {
|
---|
9 | int cnt = 0;
|
---|
10 | for ( std::list< DeclarationWithType * >::const_iterator i = decls.begin();
|
---|
11 | i != decls.end(); i++, cnt++ ) {
|
---|
12 | std::string newName = (*i)->get_name();
|
---|
13 | if ( table.find( newName ) == table.end() ) {
|
---|
14 | table.insert( std::pair<std::string, int>(newName, cnt) );
|
---|
15 | order.push_back( newName );
|
---|
16 | declarations.push_back( *i );
|
---|
17 | alternatives.push_back( 0 );
|
---|
18 | }
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | template< class InputIterator >
|
---|
23 | bool Matcher::add(InputIterator begin, InputIterator end, ResolvExpr::Alternative &alt ) {
|
---|
24 | while ( begin != end ) {
|
---|
25 | if ( table.find( *begin ) != table.end() )
|
---|
26 | alternatives[ table[ *begin ] ] = new ResolvExpr::Alternative(alt);
|
---|
27 | else
|
---|
28 | return false;
|
---|
29 | begin++;
|
---|
30 | }
|
---|
31 | return true;
|
---|
32 | }
|
---|
33 |
|
---|
34 | template< class InputIterator, class OutputIterator >
|
---|
35 | bool Matcher::slice(InputIterator begin, InputIterator end, OutputIterator out ) {
|
---|
36 | while ( begin != end )
|
---|
37 | if ( table.find( *begin ) != table.end() )
|
---|
38 | *out++ = declarations [ table[ *begin++ ] ];
|
---|
39 | else
|
---|
40 | return false; // throw 0;
|
---|
41 | return true;
|
---|
42 | }
|
---|
43 |
|
---|
44 | template< class OutputIterator >
|
---|
45 | bool Matcher::get_reorderedCall( OutputIterator out ) {
|
---|
46 | // fill call with defaults, if need be
|
---|
47 | for (Matcher::iterator o = begin(); o != end(); o++ )
|
---|
48 | if ( alternatives[ table[ *o ] ] == 0 )
|
---|
49 | return false;
|
---|
50 | else
|
---|
51 | out++ = *alternatives[table[ *o ]];
|
---|
52 | return true;
|
---|
53 | }
|
---|
54 |
|
---|
55 | bool fixDesignations( ResolvExpr::AlternativeFinder &finder, Expression *designation ) {
|
---|
56 | // Distribute `designation' over alternatives contained in `finder'
|
---|
57 | if ( ! designation) return false;
|
---|
58 | else
|
---|
59 | for ( ResolvExpr::AlternativeFinder::iterator alt = finder.begin(); alt != finder.end(); alt++ )
|
---|
60 | alt->expr->set_argName( designation );
|
---|
61 | return true;
|
---|
62 | }
|
---|
63 |
|
---|
64 | template < class OutputIterator >
|
---|
65 | bool extractNames( Expression *expr, OutputIterator out, Matcher matcher ) {
|
---|
66 | Expression *designator = expr->get_argName();
|
---|
67 | if ( designator == 0 ) return false;
|
---|
68 |
|
---|
69 | if ( NameExpr *ndes = dynamic_cast<NameExpr *>(designator) )
|
---|
70 | out++ = ndes->get_name();
|
---|
71 | else if ( TupleExpr *tdes = dynamic_cast<TupleExpr *>(designator) ) {
|
---|
72 | std::cerr << "Tuple designation" << std::endl;
|
---|
73 | // ObjectDecl *decl = extractTupleV(matcher, tdes); // xxx
|
---|
74 | // transform?
|
---|
75 | for ( std::list< Expression * >::iterator n = tdes->get_exprs().begin();
|
---|
76 | n != tdes->get_exprs().end(); n++ ) {
|
---|
77 |
|
---|
78 | if ( NameExpr *name = dynamic_cast<NameExpr *>(*n) )
|
---|
79 | out++ = name->get_name();
|
---|
80 | else
|
---|
81 | // flatten nested Tuples
|
---|
82 | throw SemanticError( "Invalid tuple designation." );
|
---|
83 | }
|
---|
84 | }
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 |
|
---|
88 | std::string extractName( Expression *expr ) /* throw NoNameExtraction */ {
|
---|
89 | if ( NameExpr *name = dynamic_cast< NameExpr *>(expr) )
|
---|
90 | return name->get_name();
|
---|
91 | else /* if () */
|
---|
92 | throw 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | DeclarationWithType *gensym( DeclarationWithType *, std::string prefix ) {
|
---|
96 | return 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | ObjectDecl *extractTupleV( Matcher matcher, TupleExpr *nmTuple ) {
|
---|
100 | // extract a subtuple of the function `fun' argument list, corresponding to the tuple of names requested by
|
---|
101 | // `nmTuple'.
|
---|
102 | std::list< Expression * > &exprs = nmTuple->get_exprs();
|
---|
103 | std::cerr << "In extractTupleV, the tuple has " << exprs.size() << " components." << std::endl;
|
---|
104 | std::list< std::string > names;
|
---|
105 | std::transform( exprs.begin(), exprs.end(), back_inserter(names), extractName );
|
---|
106 | std::list< DeclarationWithType * > decls;
|
---|
107 | matcher.slice( names.begin(), names.end(), back_inserter(decls) );
|
---|
108 | //std::for_each( decls.begin(), decls.end(), gensym );
|
---|
109 | std::cerr << "Returning declaration with " << decls.size() << " components." << std::endl;
|
---|
110 |
|
---|
111 | return 0;//new ObjectDecl()
|
---|
112 | }
|
---|
113 |
|
---|
114 | void check_alternative( FunctionType *fun, ResolvExpr::AltList &args ) {
|
---|
115 | using namespace ResolvExpr;
|
---|
116 |
|
---|
117 | Matcher matcher( fun->get_parameters() );
|
---|
118 | for ( AltList::iterator a = args.begin(); a != args.end(); a++ ) {
|
---|
119 | std::list< std::string > actNames;
|
---|
120 | if ( ! extractNames( a->expr, back_inserter(actNames), matcher ) ) {
|
---|
121 | return; // not a designated call, leave alternative alone
|
---|
122 | } else {
|
---|
123 | // see if there's a match
|
---|
124 | matcher.add( actNames.begin(), actNames.end(), *a );
|
---|
125 | }
|
---|
126 | }
|
---|
127 | //AltList newArgs;
|
---|
128 | args.clear();
|
---|
129 | matcher.get_reorderedCall( back_inserter(args) );
|
---|
130 |
|
---|
131 | return;
|
---|
132 | }
|
---|
133 | #if 0
|
---|
134 | void pruneAlternatives( Expression *expr, ResolvExpr::AlternativeFinder &finder ) {
|
---|
135 | if ( expr->get_argName() != 0 ) {
|
---|
136 | // Looking at designated expression
|
---|
137 | using namespace ResolvExpr;
|
---|
138 | AltList &alternatives = finder.get_alternatives();
|
---|
139 | std::cerr << "Now printing alternatives: " << std::endl;
|
---|
140 | for ( AltList::iterator a = alternatives.begin(); a != alternatives.end(); a++ )
|
---|
141 | a->expr->print( std::cerr );
|
---|
142 | //std::cerr << "Looking for constructions of length no more than: " << tdes->get_exprs().size() << "." << std::endl;
|
---|
143 | }
|
---|
144 | return;
|
---|
145 | }
|
---|
146 | #endif // 0
|
---|
147 | } // namespaces Designators
|
---|