~ubuntu-branches/ubuntu/feisty/faust/feisty

« back to all changes in this revision

Viewing changes to compiler/parser/sourcereader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mario Lang
  • Date: 2006-10-09 11:05:30 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 edgy)
  • Revision ID: james.westby@ubuntu.com-20061009110530-zvktdvpq5zewdxso
Tags: 0.9.8-1
* New upstream release.
* Upgrade Standards-Version to 3.7.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        sourcereader : Faust source file reader
 
3
        
 
4
        This component is in charge of mapping filenames to 
 
5
        the list of faust definitions they contain.
 
6
        
 
7
*/
 
8
#include <iostream>
 
9
 
 
10
#include "sourcereader.hh"
 
11
#include "enrobage.hh"
 
12
using namespace std;
 
13
 
 
14
/****************************************************************
 
15
                                                Parser variables
 
16
*****************************************************************/
 
17
 
 
18
 
 
19
int yyparse();
 
20
 
 
21
extern int              yyerr;
 
22
extern int              yydebug;
 
23
extern FILE*    yyin;
 
24
extern int              yylineno;
 
25
extern const char * yyfilename;
 
26
 
 
27
extern Tree     gResult;
 
28
extern Tree     gResult2;
 
29
 
 
30
 
 
31
/**
 
32
 * Parse a single faust source file. returns the list of
 
33
 * definitions it contains.
 
34
 * 
 
35
 * @param fname the name of the file to parse
 
36
 * @return the list of definitions it contains
 
37
 */
 
38
 
 
39
Tree SourceReader::parse(string fname)
 
40
{
 
41
        yyerr = 0;
 
42
        
 
43
        yyfilename = fname.c_str();
 
44
        yyin = fopensearch(yyfilename);
 
45
        if (yyin == NULL) {
 
46
                fprintf(stderr, "ERROR : Unable to open file  %s \n", yyfilename); 
 
47
                exit(1);
 
48
        }
 
49
        
 
50
        yylineno = 1;
 
51
        int r = yyparse();
 
52
        if (r) { 
 
53
                fprintf(stderr, "Parse error : code = %d \n", r); 
 
54
        }
 
55
        if (yyerr > 0) {
 
56
                //fprintf(stderr, "Erreur de parsing 2, count = %d \n", yyerr); 
 
57
                exit(1);
 
58
        }
 
59
 
 
60
        return gResult;
 
61
}
 
62
 
 
63
 
 
64
/**
 
65
 * Check if a file as been read and is in the "cache"
 
66
 * 
 
67
 * @param fname the name of the file to check
 
68
 * @return true if the file is in the cache
 
69
 */
 
70
 
 
71
bool SourceReader::cached(string fname)
 
72
{
 
73
        return fFileCache.find(fname) != fFileCache.end();
 
74
}
 
75
 
 
76
 
 
77
/**
 
78
 * Return the list of definitions file contains. Cache the result.
 
79
 * 
 
80
 * @param fname the name of the file to check
 
81
 * @return the list of definitions it contains
 
82
 */
 
83
 
 
84
Tree SourceReader::getlist(string fname)
 
85
{
 
86
        if (!cached(fname)) {
 
87
                fFileCache[fname] = parse(fname);
 
88
        }
 
89
        return fFileCache[fname];
 
90
}
 
91
 
 
92
 
 
93
/**
 
94
 * Return the list of definitions where all imports have been expanded.
 
95
 * 
 
96
 * @param ldef the list of definitions to expand
 
97
 * @return the expanded list of definitions
 
98
 */
 
99
 
 
100
Tree SourceReader::expandlist(Tree ldef)
 
101
{
 
102
        set<string> visited;
 
103
        return expandrec(ldef, visited, nil);   
 
104
}
 
105
 
 
106
Tree SourceReader::expandrec(Tree ldef, set<string>& visited, Tree lresult)
 
107
{
 
108
        for (;!isNil(ldef); ldef = tl(ldef)) {
 
109
                Tree d = hd(ldef); 
 
110
                Tree fname;
 
111
                if (isNil(d)) {
 
112
                        // skill null definitions produced by declarations
 
113
                } else if (isImportFile(d,fname)) {
 
114
                        string f = tree2str(fname);
 
115
                        //cerr << "import(" << f << ")" << endl;
 
116
                        
 
117
                        //string f = tree2str(fname);
 
118
                        if (visited.find(f) == visited.end()) {
 
119
                                visited.insert(f);
 
120
                                //Tree l = getlist(f);
 
121
                                lresult = expandrec(getlist(f), visited, lresult);
 
122
                        }
 
123
                        
 
124
                } else {
 
125
                        lresult = cons(d, lresult);
 
126
                }
 
127
        }
 
128
        return lresult;
 
129
}
 
130
                                
 
131