File: intcomp.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (401 lines) | stat: -rw-r--r-- 10,094 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
   FALCON - The Falcon Programming Language.
   FILE: intcomp.cpp

   Complete encapsulation of an incremental interactive compiler.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Sun, 17 Aug 2008 11:10:24 +0200

   -------------------------------------------------------------------
   (C) Copyright 2008: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

#include <falcon/intcomp.h>
#include <falcon/rosstream.h>
#include <falcon/gencode.h>
#include <falcon/runtime.h>
#include <falcon/modloader.h>
#include <falcon/src_lexer.h>
#include <falcon/transcoding.h>
#include "core_module/core_module.h"

namespace Falcon
{

InteractiveCompiler::InteractiveCompiler( ModuleLoader *l, VMachine *vm ):
   Compiler(),
   m_lmodule( 0 ),
   m_loader( l ),
   m_interactive( true )
{
   m_vm = vm;
   m_vm->incref();

   m_modVersion = 0x010000;
   m_language = "en_EN";

   m_module = new Module;
   m_module->name( "falcon.intcomp" );
   m_module->engineVersion( FALCON_VERSION_NUM );

   // link this as main and private.
   m_lmodule = m_vm->link( m_module, true, true );

   // as the module is currently empty, we expect the link to work.
   fassert( m_lmodule );

   // set incremental mode for the lexer.
   m_tempLine = 1;

   init();
}

InteractiveCompiler::~InteractiveCompiler()
{
   m_module->decref();
   m_vm->decref();
}

InteractiveCompiler::t_ret_type InteractiveCompiler::compileNext( const String &input )
{
   // we split the thing for performance
   // and use stack based object to protect against exception raisal
   if( input.manipulator()->charSize() == 1 )
   {
      ROStringStream ss( input );
      return compileNext( &ss );
   }
   else {
      String temp;
      TranscodeString(input, "utf-16", temp );
      ROStringStream ss( temp );
      TranscoderUTF16 tr( &ss, false );
      return compileNext( &tr );
   }
}

InteractiveCompiler::t_ret_type InteractiveCompiler::compileAll( const String &input )
{
   t_ret_type ret = e_nothing;

   // we split the thing for performance
   // and use stack based object to protect against exception raisal
   if( input.manipulator()->charSize() == 1 )
   {
      ROStringStream ss( input );
      while ( ! ss.eof() )
      {
         // on error, we'll throw.
         ret = compileNext( &ss );
      }
   }
   else {
      String temp;
      TranscodeString(input, "utf-16", temp );
      ROStringStream ss( temp );
      TranscoderUTF16 tr( &ss, false );
      while ( ! tr.eof() )
      {
         // on error, we'll throw.
         ret = compileNext( &tr );
      }
   }

   return ret;
}

InteractiveCompiler::t_ret_type InteractiveCompiler::compileNext( Stream *input )
{
   // ensure we're talking the same language...
   m_stream = input;

   // reset if last code was executed.
   m_contextSet.clear();
   m_context.clear();
   m_loops.clear();
   m_functions.clear();
   m_func_ctx.clear();

   delete m_root;
   m_root = new SourceTree;


   pushContextSet( &m_root->statements() );

   // and an empty list for the local function undefined values
   while( ! m_statementVals.empty() )
   {
      delete (List*) m_statementVals.back();
      m_statementVals.popBack();
   }

   List* l = new List;
   m_statementVals.pushBack( l );

   // reset compilation
   m_enumId = 0;
   m_staticPrefix = 0;
   m_closureContexts = 0;

   // anyhow reset error count
   m_errors = 0;

   // reset the lexer
   m_lexer->reset();
   m_lexer->line( m_tempLine );
   m_lexer->input( m_stream );
   m_lexer->incremental( m_interactive );


   // prepare to unroll changes in the module
   uint32 modSymSize = m_module->symbols().size();

   // parse
   flc_src_parse( this );

   // TODO: move this directly where changed...
   m_module->language( m_language );
   m_module->version( (uint32) m_modVersion );

   // some errors during compilation?
   if( m_errors != 0 )
   {
      // but do not reset errors, the owner may want to know.
      Error *err = m_rootError;
      m_rootError = 0;
      m_module->rollBackSymbols( modSymSize );
      throw err;
   }

   if ( m_lexer->hasOpenContexts() )
   {
      m_module->rollBackSymbols( modSymSize );
      return e_incomplete;
   }

   // If the context is not empty, then we have a partial data.
   // more is needed
   if ( !m_context.empty() || m_contextSet.size() != 1 ) {
      m_module->rollBackSymbols( modSymSize );
      return e_more;
   }

   // now we have to decide what to do with the beast.
   // if it's a directive, we already handled it.
   m_tempLine += m_lexer->previousLine();
   t_ret_type ret = e_nothing;

   // empty?
   if ( m_root->classes().empty() && m_root->functions().empty() && m_root->statements().empty() )
   {
      m_module->rollBackSymbols( modSymSize );
      return e_nothing;
   }

   // if we have some statements, we may need to change the first expression
   // into a return, if we are in interactive mode.
   if( ! m_root->statements().empty() )
   {
      // was it an expression?
      if( m_interactive && m_root->statements().back()->type() == Statement::t_autoexp )
      {
         // wrap it around a return, so A is not nilled.
         StmtAutoexpr *ae = static_cast<StmtAutoexpr *>( m_root->statements().pop_back() );
         m_root->statements().push_back( new StmtReturn( 1, ae->value()->clone() ) );

         // what kind of expression is it? -- if it's a call, we're interested
         if( ae->value()->isExpr() && ae->value()->asExpr()->type() == Expression::t_funcall )
            ret = e_call;
         else
            ret = e_expression;

         // we don't need the expression anymore.
         delete ae;
      }
      else
         ret = e_statement;
   }

   // generate the module
   GenCode gencode( module() );
   gencode.generate( m_root );


   m_lmodule->globals().resize( module()->symbolTable().size() + 1 );

   while ( ! m_root->classes().empty() )
   {
      StmtClass *cls = static_cast<StmtClass *>( m_root->classes().front() );

      // in case of classes, we have to link the constructor,
      // the class itself and eventually the singleton.
      StmtFunction *init = cls->ctorFunction();
      if ( init != 0 )
      {
         ListElement *from_iter = cls->symbol()->getClassDef()->inheritance().begin();
         while( from_iter != 0 )
         {
            const InheritDef *def = (const InheritDef *) from_iter->data();
            const Symbol *parent = def->base();
            // it's just an import
            m_vm->linkSymbol( parent, m_lmodule );
            from_iter = from_iter->next();
         }

         m_vm->linkCompleteSymbol( init->symbol(), m_lmodule );
      }

      m_vm->linkCompleteSymbol( cls->symbol(), m_lmodule );

      Symbol *singleton = cls->singleton();
      if ( singleton != 0 )
      {
         m_vm->linkCompleteSymbol( singleton, m_lmodule );
      }

      if ( ret == e_nothing )
         ret = e_decl;
      delete m_root->classes().pop_front();
   }

   // if it's a function
   while ( ! m_root->functions().empty() )
   {
      StmtFunction *func = static_cast<StmtFunction *>( m_root->functions().front() );
      m_vm->linkCompleteSymbol( func->symbol(), m_lmodule );

      if ( ret == e_nothing )
         ret = e_decl;
      delete m_root->functions().pop_front();
   }

   // now, the symbol table must be traversed.
   MapIterator iter = m_module->symbolTable().map().begin();
   bool success = true;
   while( iter.hasCurrent() )
   {
      Symbol *sym = *(Symbol **) iter.currentValue();
      // try to link undefined symbols.

      if ( sym->isUndefined() )
      {
         try
         {
            m_vm->linkSymbol( sym, m_lmodule );
         }
         catch( Error *e )
         {
            // but continue to expose other errors as well.
            success = false;

            // prevent searching again
            sym->setGlobal();
            raiseError( e );
         }
      }

      // next symbol
      iter.next();
   }

   // re-link failed?
   if ( ! success )
   {
      Error *e = m_rootError;
      m_rootError = 0;
      m_module->rollBackSymbols( modSymSize );
      throw e;
   }

   // finally, link main
   if ( ! m_root->statements().empty() )
   {
      Symbol* msym = module()->findGlobalSymbol("__main__" );
      vm()->linkCompleteSymbol( msym, m_lmodule );
   }

   // launch the vm.
   if ( ret == e_statement || ret == e_call || ret == e_expression )
   {
      try {
         Item* i_main = m_vm->mainModule()->findModuleItem("__main__");
         if( i_main == 0 )
         {
            throw new CodeError( ErrorParam( e_undef_sym, __LINE__ )
                  .origin( e_orig_compiler )
                  .extra( "__main__ (check modules link order/mode)" ) );
         }

         m_vm->reset();
         m_vm->callItem( *i_main, 0 );
      }
      catch( VMEventQuit & )
      {
         ret = e_terminated;
      }
   }

   return ret;
}

void InteractiveCompiler::addNamespace( const String &nspace, const String &alias,
   bool full, bool filename )
{
   loadNow( nspace, filename, true );

   // create also the standard namespace.
   if ( m_errors == 0 )
   {
      Compiler::addNamespace( nspace, alias, full, filename );
   }
}

void InteractiveCompiler::addLoad( const String &name, bool isFilename )
{
   loadNow( name, isFilename, false );

   if ( m_errors == 0 )
   {
      Compiler::addLoad( name, isFilename );
   }
}

void InteractiveCompiler::loadNow( const String &name, bool isFilename, bool bPrivate )
{
   Module *mod;
   if ( isFilename )
      mod = m_loader->loadFile( name );
   else
      mod = m_loader->loadName( name, m_module->name() );

   if ( mod == 0 )
   {
      // we had an error.
      m_errors++;
      return;
   }

   // perform a complete linking
   Runtime rt( m_loader, m_vm );
   rt.hasMainModule( false );
   try
   {
      rt.addModule( mod, bPrivate );
      m_vm->link( &rt );
   }
   catch( Error* )
   {
      m_errors++;
      mod->decref();
      throw;
   }
   mod->decref();

}

}

/* end of intcomp.cpp */