Beispiel #1
0
Datei: env.c Projekt: berkus/moto
void
moto_emitCFunctions(MotoEnv *env, StringBuffer *out) {
    Enumeration* e;

    buf_puts(out, "/* BEGIN GENERATED FUNCTION DEFINITIONS */\n\n");
    e = htab_getKeys(env->fdefs);
    while (enum_hasNext(e)) {
        MotoFunction *f = (MotoFunction *)enum_next(e);
        buf_puts(out, mfn_cprototype(f));
        buf_puts(out, "{\n");
        buf_puts(out,(char*)htab_get(env->fdefs,f));
        buf_puts(out, "}\n\n");
    }
    buf_puts(out, "/* END GENERATED FUNCTION DEFINITIONS */\n\n");

    buf_puts(out, "/* BEGIN GENERATED ANONYMOUS FUNCTION DEFINITIONS */\n\n");
    e = htab_getKeys(env->adefs);
    while (enum_hasNext(e)) {
        MotoFunction *f = (MotoFunction *)enum_next(e);
        buf_puts(out, mfn_cprototype(f));
        buf_puts(out, "{\n");
        buf_puts(out,(char*)htab_get(env->adefs,f));
        buf_puts(out, "}\n\n");
    }
    buf_puts(out, "/* END GENERATED ANONYMOUS FUNCTION DEFINITIONS */\n\n");
}
Beispiel #2
0
bool layout<_t>::resize(_t w, _t h, bool scaled, int layout_direction) {
	// resize only affected to dest_
	int ldx = __d_dx(layout_direction); 
	int ldy = __d_dy(layout_direction); 

	clip_type *pc; 

	_t lw = _rect.width(); 
	_t lh = _rect.height(); 

	if (!scaled) {
		// offset will be taken even in relative coordinate system. 
		// ignore !ldx && !ldy for efficiency
		if (ldx || ldy) {
			_t dx = w - lw; 
			_t dy = h - lh; 
			dx = dx * (ldx + 1) / 2; 
			dy = dy * (ldy + 1) / 2; 

			enum_reset(); 
			while (pc = enum_next()) {
				pc->dest.offset(dx, dy); 
			}
		}
	} else if (w != lw || h != lh) {
		// scale mode don't use layout_direction, instead, 
		// it calculate the scale base point in _acs. 
		_t bx = 0; 
		_t by = 0; 
		real_type kw = (real_type)w / (real_type)lw; 
		real_type kh = (real_type)h / (real_type)lh; 
		if (_acs) {
			bx = _rect._x0; 
			by = _rect._y0; 
		}
		enum_reset(); 
		while (pc = enum_next()) {
			_t sw = pc->dest.width(); 
			_t sh = pc->dest.height(); 
			pc->dest.base_width(bx, (_t)(sw * kw)); 
			pc->dest.base_height(by, (_t)(sh * kh)); 
		}
	}
	_rect.width(w); 
	_rect.height(h); 
	return true; 
}
Beispiel #3
0
bool layout<_t>::item(const int i, layout<_t>::clipitem_type *pclipitem) {
	if (i < 0 || i >= count()) return false; 
	enum_reset(); 
	clipitem_type it; 
	while (enum_next(&it) && i--); 
	*pclipitem = it; 
	return true; 
}
Beispiel #4
0
Datei: env.c Projekt: berkus/moto
void
moto_emitCImplicitConstructors(MotoEnv *env, StringBuffer *out) {
    Enumeration* e;
    buf_puts(out, "/* BEGIN GENERATED IMPLICIT CONSTRUCTORS */\n\n");

    /* Foreach MotoClassDefinition */

    e = stab_getValues(env->cdefs);
    while(enum_hasNext(e)) {
        MotoClassDefinition* mcd = (MotoClassDefinition*)enum_next(e);
        Enumeration* ve;

        /* Output the implicit constructor prototype for this MotoClassDefinition */
        buf_printf(out, "static %s* _%s_%s___(){\n",mcd->classn,mcd->classn,mcd->classn);

        /* Allocate the space for the type being constructed and set ÔthisÕ */
        buf_printf(out, "	  %s* this = (%s*)mman_track(rtime_getMPool(),emalloc(sizeof(%s)));\n",
                   mcd->classn,mcd->classn,mcd->classn);

        /* Foreach var in the MotoClassDefinition */
        ve = vec_elements(mcd->memberVarNames);
        while(enum_hasNext(ve)) {
            char* varn = (char*)enum_next(ve);

            MotoVar* mv = moto_createVar(env,varn,mcd_getMemberType(mcd,varn),0,'\0',NULL);

            buf_printf(out,"	 this->%s=%s;\n",
                       mv->n,moto_defaultValForCType(mv->vs)
                      );

            moto_freeVar(env,mv);

        }
        enum_free(ve);

        /* Output the mcd->code */
        buf_puts(out,mcd->code);

        /* Output 'return this\n}\n\nÕ */
        buf_puts(out,"	  return this;\n}\n\n");
    }
    enum_free(e);

    buf_puts(out, "/* END GENERATED IMPLICIT CONSTRUCTORS */\n\n");
}
Beispiel #5
0
bool layout<_t>::offset(_t dx, _t dy) {
	clip_type *pc; 
	enum_reset(); 
	while (pc = enum_next()) {
		pc->dest.offset(dx, dy); 
	}
	_rect.offset(dx, dy); 
	return true; 
}
Beispiel #6
0
/**
 * Executes the function f on each remaining element of the enumeration.
 */
void enum_each(Enumeration *e,Function *f) {
	while(enum_hasNext(e)){
		void* o = enum_next(e);
		
		f->flags & F_INTERPRETED ? 
			ifunc_vcall(f,"O", o) : 
			((void(*)(Function *f,void*))f->fn)(f,o) ;
	
	}
}
Beispiel #7
0
Datei: env.c Projekt: berkus/moto
void
moto_emitCStructures(MotoEnv *env, StringBuffer *out) {
    Enumeration* e;
    buf_puts(out, "/* BEGIN GENERATED STRUCTURES */\n\n");

    /* Foreach MotoClassDefinition */

    e = stab_getValues(env->cdefs);
    while(enum_hasNext(e)) {
        MotoClassDefinition* mcd = (MotoClassDefinition*)enum_next(e);
        Enumeration* ve;

        /* Output Ôtypedef struct _<typename> {Ô */
        buf_printf(out, "typedef struct _%s {\n",mcd->classn);

        /* Foreach var in the MotoClassDefinition */
        ve = vec_elements(mcd->memberVarNames);
        while(enum_hasNext(ve)) {
            char* varn = (char*)enum_next(ve);

            MotoVar* mv = moto_createVar(env,varn,mcd_getMemberType(mcd,varn),0,'\0',NULL);

            /* if the var is another MotoClassDefinition */
            /* output Ôstruct _<var typename> *Õ */
            /* else */
            /* output <typename> */
            /* output varname */
            /* output Ô;\nÕ */

            buf_printf(out,"	 %s %s;\n", moto_valToCType(mv->vs), mv->n);

            moto_freeVar(env,mv);
        }
        enum_free(ve);

        /* output Ô} <typename>;Õ */
        buf_printf(out, "} %s;\n\n",mcd->classn);
    }
    enum_free(e);

    buf_puts(out, "/* END GENERATED STRUCTURES */\n\n");
}
Beispiel #8
0
layout<_t>::clip_type *layout<_t>::item(const int i) {
	if (i < 0 || i >= count()) return 0; 
	// rebuild the index table
	__index_pclip.resize(count()); 
	enum_reset(); 
	clip_type *pc; 
	int _i = 0; 
	while (pc = enum_next()) {
		__index_pclip[_i++] = pc; 
	}
	
	return __index_pclip[i]; 
}
Beispiel #9
0
Datei: mfn.c Projekt: berkus/moto
void mfn_dump(MFN *p) {
   Enumeration *e;
   printf("MFN--------------------\n");   
   printf("rtype: %s\n", p->rtype);   
   printf("fname: %s\n", p->fname);   
   printf("argc: %d\n", vec_size(p->argv));   
   printf("argv: \n");   
   e = vec_elements(p->argv);
   while(enum_hasNext(e)) {
      FNArg *arg = (FNArg *)enum_next(e);
      printf("\t%s %s\n", arg->type, arg->name);   
   }
   enum_free(e);
   printf("--------------------------------\n");   
}
Beispiel #10
0
Datei: env.c Projekt: berkus/moto
void
moto_emitCPrototypes(MotoEnv *env, StringBuffer *out) {
    Enumeration* e;

    buf_puts(out, "/* BEGIN GENERATED FUNCTION PROTOTYPES */\n\n");
    e = htab_getKeys(env->fdefs);
    while (enum_hasNext(e)) {
        MotoFunction *f = (MotoFunction*)enum_next(e);

        buf_puts(out, mfn_cprototype(f));
        buf_puts(out, ";\n");
    }
    buf_puts(out, "/* END GENERATED FUNCTION PROTOTYPES */\n\n");

    buf_puts(out, "/* BEGIN GENERATED ANONYMOUS FUNCTION PROTOTYPES */\n\n");
    e = htab_getKeys(env->adefs);
    while (enum_hasNext(e)) {
        MotoFunction *f = (MotoFunction*)enum_next(e);

        buf_puts(out, mfn_cprototype(f));
        buf_puts(out, ";\n");
    }
    buf_puts(out, "/* END GENERATED ANONYMOUS FUNCTION PROTOTYPES */\n\n");
}
Beispiel #11
0
Datei: env.c Projekt: berkus/moto
int
moto_use(MotoEnv *env, char *usename) {
    char *libpath;

    libpath = mxdl_find(usename);
    if (libpath == NULL) {
        mman_track(env->mpool, libpath);
        return -1;
    }

    if(!sset_contains(env->uses,usename) ) {
        int i;
        Enumeration* e;
        MotoExtension* mx = mxdl_load(usename);
        sset_add(env->uses,moto_strdup(env,usename)) ;

        /* Track the MotoExtension in the mpool */
        mman_trackf(env->mpool,mx,(void(*)(void *))mext_free);

        /* Collect all the new includes required by this extension */
        for (i = 0; i < mx->includeCount; i++)
            sset_add(env->includes,mx->includes[i]);

        e = mext_getFunctions(mx);
        while(enum_hasNext(e)) {
            MotoFunction* mfn = enum_next(e);

            /* Track the MotoFunction in the mpool */
            mman_trackf(env->mpool,mfn,(void(*)(void *))mfn_free);

            /* Add the function to the ftable */
            ftab_add(env->ftable, mfn->motoname, mfn);

            /* Define a new type if need be */
            if (mfn->deftype != NULL) {
                if (mttab_get(env->types, mfn->deftype,0) == NULL) {
                    mttab_add(env->types, mfn->deftype,'\1');
                }
            }
        }
        enum_free(e);

    }

    free(libpath);

    return 0;
}
Beispiel #12
0
Datei: mfn.c Projekt: berkus/moto
void mfn_free(MFN *p) {
   /* free FNArg structs */
   Enumeration *e = vec_elements(p->argv);
   while(enum_hasNext(e)) {
      FNArg *arg = (FNArg *)enum_next(e);
      free(arg);
   }
   enum_free(e);
   /* free internally allocated memory */
   vec_free(p->argv);

   free(p->cname);
   free(p);
   /*
   log_debug(__FILE__, "Freed motofunction: 0x%x\n", p);
   */
}
Beispiel #13
0
Datei: env.c Projekt: berkus/moto
void
moto_emitCGlobals(MotoEnv *env, StringBuffer *out) {
    Enumeration* e;
    buf_puts(out, "/* BEGIN GENERATED GLOBAL DECLARATIONS */\n\n");

    e = stab_getKeys(env->globals);
    while (enum_hasNext(e)) {
        char* n = (char*)enum_next(e);
        MotoVar* mv = stab_get(env->globals,n);

        buf_printf(out,"static %s _G_%s=%s;\n", moto_valToCType(mv->vs), n,moto_defaultValForCType(mv->vs));

    }
    enum_free(e);

    buf_puts(out, "/* END GENERATED GLOBAL DECLARATIONS */\n\n");
}
Beispiel #14
0
Datei: env.c Projekt: berkus/moto
void
moto_emitCImplicitConstructorPrototypes(MotoEnv *env, StringBuffer *out) {
    Enumeration* e;
    buf_puts(out, "/* BEGIN GENERATED IMPLICIT CONSTRUCTOR PROTOTYPES */\n\n");

    /* Foreach MotoClassDefinition */

    e = stab_getValues(env->cdefs);
    while(enum_hasNext(e)) {
        MotoClassDefinition* mcd = (MotoClassDefinition*)enum_next(e);

        /* output an implicit constructor prototype */
        buf_printf(out, "static %s* _%s_%s___();\n",
                   mcd->classn,mcd->classn,mcd->classn);
    }
    enum_free(e);

    buf_puts(out, "/* END GENERATED IMPLICIT CONSTRUCTOR PROTOTYPES */\n\n");
}
Beispiel #15
0
char *hset_toString(HashSet *p) {
	char *result = NULL;
	StringBuffer *buf = buf_createDefault();   
   Enumeration *e;  
   int i = 0;
   
   for(e = hset_elements(p); enum_hasNext(e); ) { 
      void *curkey = (void *)enum_next(e);
		if (i > 0) {
      	buf_puts(buf, ", ");
		}
      buf_putc(buf, '{');
      buf_puts(buf, curkey);
      buf_putc(buf, '}');
   	i++;
   }
   result = buf_toString(buf);
   free(e);
   
   return result;
}
Beispiel #16
0
void 
tnfa_free(TNFA* nfa){
   Enumeration* e;

   for(e=ihtab_getValues(nfa->states);enum_hasNext(e);){
      TFATrans* fat,*next;
   
      for(fat=enum_next(e);fat!=NULL;fat=next){
         next = fat->next;
         free(fat);
      }
   }
   enum_free(e);
   
   ihtab_free(nfa->states);
	itoi_free(nfa->inputOrder);
	iset_free(nfa->minimized);
	
	free(nfa->cStates);
	free(nfa->cInputOrder);
   free(nfa);
}
Beispiel #17
0
Datei: pp.c Projekt: berkus/moto
void motopp_freeEnv(MotoPP *ppenv) {
   Enumeration *e;
   
   log_debug(__FILE__, ">>> motopp_freeEnv\n");
  
   buf_free(ppenv->out);
   buf_free(ppenv->err);
   buf_free(ppenv->argbuf);
   istack_free(ppenv->dirstack);
   stack_free(ppenv->frames);   

   /* free vals */
   e = hset_elements(ppenv->vallist);
   while (enum_hasNext(e)) {
      MotoPPVal *val = enum_next(e);
      if (shared_check(val->sval)) {
         free(val->sval);
         hset_remove(ppenv->ptrs, val->sval);
      }
      free(val);
   }
   enum_free(e);
   hset_free(ppenv->vallist);
   
   /* free macros */
   while (stack_size(ppenv->macrostack) > 0) {
      SymbolTable *macros = stack_pop(ppenv->macrostack);
      e = stab_getKeys(macros);
      while (enum_hasNext(e)) {
         char *name = (char *)enum_next(e);
         MotoMacro *m = (MotoMacro *)stab_get(macros, name);
         motopp_freeMacro(m);
      }
      stab_free(macros);
      enum_free(e);
   }
   
   /* free all remaining sys pointers */
   e = hset_elements(ppenv->sysptrs);
   while (enum_hasNext(e)) {
      void *ptr = enum_next(e);
      if (ptr) {
         sys_free(ptr);         
      }
   }
   enum_free(e);
      
   /* free all remaining pointers */
   e = hset_elements(ppenv->ptrs);
   while (enum_hasNext(e)) {
      void *ptr = enum_next(e);
      if (shared_check(ptr)) {
         free(ptr);         
      }
   }
   enum_free(e);
      
   /* free remaining pooled memory */
   mpool_free(ppenv->mpool);

   /* free remainder of env struct */
   hset_free(ppenv->sysptrs);
   hset_free(ppenv->ptrs);
   stack_free(ppenv->macrostack);   
   
   free(ppenv);

   log_debug(__FILE__, "<<< motopp_freeEnv\n");

}
Beispiel #18
0
Datei: env.c Projekt: berkus/moto
void
moto_emitCHeader(MotoEnv *env, StringBuffer *out) {
    Enumeration *e;
    char bts[MAX_DATE_LEN];
    char gts[MAX_DATE_LEN];
    int maxlen;
    char *execname = path_alloc(&maxlen);
    char *filename = env->filename;
    int i;
    for (i = 0; i < maxlen; i++) {
        if (filename[i] == '\0' || filename + i == strrchr(filename,'.')) {
            execname[i] = '\0';
            break;
        } else if ( !isalpha(filename[i]) && !isdigit(filename[i]) ) {
            execname[i] = '_';
        } else {
            execname[i] = filename[i];
        }
    }
    moto_buildTimeStamp(bts);
    moto_curTimeStamp(gts);
    buf_puts(out, "/***************** DO NOT EDIT ****************\n");
    buf_puts(out, " * FILE GENERATED BY THE MOTO COMPILER\n");
    buf_puts(out, " * Moto Build: ");
    buf_puts(out, bts);
    buf_puts(out, "\n");
    buf_puts(out, " * Generated:	");
    buf_puts(out, gts);
    buf_puts(out, "\n");
    buf_puts(out, " * Input File: ");
    buf_puts(out, env->filename);
    buf_puts(out, "\n");
    buf_puts(out, " **********************************************/\n\n");
    e = (sset_elements(env->includes));
    while (enum_hasNext(e)) {
        char *incl = (char *)enum_next(e);
        buf_puts(out, "#include ");
        buf_puts(out, incl);
        buf_puts(out, "\n");
    }
    enum_free(e);
    buf_puts(out, "\n");
    buf_puts(out, "#ifdef SHARED_MALLOC\n");
    buf_puts(out, "#	define STD_FREE_FN shared_free\n");
    buf_puts(out, "#else\n");
    buf_puts(out, "#	define STD_FREE_FN free\n");
    buf_puts(out, "#endif\n");
    buf_puts(out, "\n");
    buf_puts(out, "#ifdef HAVE_FN_NAME_H\n");
    buf_puts(out, "#include \"mod_fn.h\"\n");
    buf_puts(out, "#endif\n");
    buf_puts(out, "#ifndef __MAIN__\n");
    buf_puts(out, "#	 define __MAIN__ main_");
    buf_puts(out, execname);
    buf_puts(out, "\n");
    buf_puts(out, "#endif\n");
    buf_puts(out, "#define __EXEC__ exec_");
    buf_puts(out, execname);
    buf_puts(out, "\n");
    buf_puts(out, buf_data(env->constantPool));
    buf_puts(out, "\n\n");

    buf_puts(out, "enum {\n");
    buf_puts(out, "NOOP_ACTION = 0,\n");
    buf_puts(out, "BREAK_ACTION = 1,\n");
    buf_puts(out, "CONTINUE_ACTION = 2,\n");
    buf_puts(out, "RETURN_ACTION = 3\n");
    buf_puts(out, "};\n");
    buf_puts(out, "static int _MOTO_ACTION = 0;\n");

    moto_emitCStructures(env,out);

    moto_emitCImplicitConstructorPrototypes(env,out);
    moto_emitCPrototypes(env,out);

    buf_puts(out, "StringBuffer *out;\n"); /* FIXME: big hack to get output buffer to functions */

    /* FIXME : The following hacks all exist because non-gcc compilers don't like inline array instantiation */
    buf_puts(out, "int _MDS_[20];\n\n"); /* FIXME: big hack to allow for array instantiation */
    buf_puts(out, "int _PARGI_[20];\n\n"); /* FIXME: big hack to allow for partially applied functions */
    buf_puts(out, "FuncTypeKind _PARGT_[20];\n\n"); /* FIXME: big hack to allow for partially applied functions */

    moto_emitCGlobals(env,out);
    moto_emitCImplicitConstructors(env,out);
    moto_emitCFunctions(env,out);

    buf_puts(out, "void __EXEC__");
    buf_puts(out, "(StringBuffer *outputBuffer) {\n\n");
    buf_puts(out, "	/* BEGIN GENERATED CODE */\n\n");
    free(execname);
}
Beispiel #19
0
Datei: env.c Projekt: berkus/moto
void
moto_clearEnv(MotoEnv *env) {
    Enumeration *e;
    int size;
    //hset_free(env->ptrs);
    //mpool_free(env->mpool);

    /* Clear out the globals */
    e = stab_getKeys(env->globals);
    while (enum_hasNext(e)) {
        char* n = (char*)enum_next(e);
        MotoVar* var = stab_get(env->globals,n);

        if(env->mode != COMPILER_MODE) {
            moto_freeVal(env,var->vs);
        } else {
            opool_release(env->valpool,var->vs);
        }
        free(var);
    }
    enum_free(e);
    stab_clear(env->globals);

    /* Clear out the frames */
    size = vec_size(env->frames);
    while (--size >= 0) {
        MotoFrame *frame = (MotoFrame *)vec_get(env->frames, size);
        stack_free(frame->opstack);
        stab_free(frame->symtab);
        buf_free(frame->out);
        free(frame);
    }
    vec_clear(env->frames);

    e = stack_elements(env->scope);
    while (enum_hasNext(e)) {
        free(enum_next(e));
    }
    enum_free(e);

    stack_clear(env->scope);
    buf_clear(env->out);
    buf_clear(env->err);

    // stab_free(env->types);
    // ftab_free(env->ftable);
    // stab_free(env->rxcache);
    // stack_clear(env->callstack);
    sset_clear(env->errs);
    // sset_clear(env->uses);

    if (env->mode == COMPILER_MODE) {
        sset_free(env->includes);
        htab_free(env->fdefs);
        htab_free(env->adefs);
        buf_free(env->constantPool);
        buf_free(env->fcodebuffer);
        istack_free(env->scopeIDStack);
    }
    env->frameindex = -1;

    /* error stuff */
    env->meta.filename = env->filename;
    env->meta.caller = NULL;
    env->meta.macroname = NULL;
    env->meta.lineno = 1;

    env->errflag = 0;
    //free(env);
}
Beispiel #20
0
Datei: env.c Projekt: berkus/moto
void
moto_freeEnv(MotoEnv *env) {
    Enumeration *e;

    /* Free any outstanding frames */
    while(vec_size(env->frames) > 0) {
        moto_freeFrame(env);
    }

    /* Free all the globals */
    e = stab_getKeys(env->globals);
    while (enum_hasNext(e)) {
        char* n = (char*)enum_next(e);
        MotoVar* var = stab_get(env->globals,n);

        if(env->mode != COMPILER_MODE) {
            moto_freeVal(env,var->vs);
        } else {
            opool_release(env->valpool,var->vs);
        }
        free(var);
    }
    enum_free(e);
    stab_free(env->globals);

    /* free all cached regular expressions */
    e = stab_getKeys(env->rxcache);
    while (enum_hasNext(e)) {
        char *rx = (char *)enum_next(e);
        MDFA *mdfa = (MDFA *)stab_get(env->rxcache, rx);
        if (mdfa != NULL) {
            mdfa_free(mdfa);
        }
    }
    enum_free(e);

    /* free all remaining pointers */
    e = hset_elements(env->ptrs);
    while (enum_hasNext(e)) {
        void *ptr = enum_next(e);
        if (shared_check(ptr)) {
            free(ptr);
        }
    }
    enum_free(e);

    /* free all errors */
    e = sset_elements(env->errs);
    while (enum_hasNext(e)) {
        void *ptr = enum_next(e);
        if (shared_check(ptr)) {
            free(ptr);
        }
    }
    enum_free(e);

    /* free all scopes */
    e = stack_elements(env->scope);
    while (enum_hasNext(e)) {
        free(enum_next(e));
    }
    enum_free(e);

    /* free all cells */
    moto_freeTreeCells(env);

    /* free all class defs */
    stab_free(env->cdefs);

    /* free remainder of env struct */
    hset_free(env->ptrs);
    buf_free(env->out);
    buf_free(env->err);
    stab_free(env->types);
    vec_free(env->frames);

    ftab_free(env->ftable);

    /* Free all the stuff that got put in the mpool ... this includes
    	MotoFunctions and MotoClassDefinitions */
    mpool_free(env->mpool);

    stack_free(env->scope);
    stab_free(env->rxcache);
    //stack_free(env->callstack);
    sset_free(env->errs);
    sset_free(env->uses);
    sset_free(env->includes);
    buf_free(env->fcodebuffer);
    istack_free(env->scopeIDStack);

    htab_free(env->fdefs);
    htab_free(env->adefs);
    buf_free(env->constantPool);
    moto_freeTree(env->tree);

    opool_free(env->valpool);
    opool_free(env->bufpool);
    opool_free(env->stkpool);

    e = stab_getKeys(env->fcache);
    while (enum_hasNext(e))
        free((char *)enum_next(e));
    enum_free(e);
    stab_free(env->fcache);

    free(env);
}
Beispiel #21
0
Datei: env.c Projekt: berkus/moto
void
moto_freeFrame(MotoEnv *env) {
    MotoFrame *frame = env->frame;
    MotoFrame *pframe = NULL;
    StringBuffer *pout = NULL;
    Enumeration *e;

    /* Append declarations if in compiler mode and not in a function frame*/
    if (env->mode == COMPILER_MODE && frame->type != FN_FRAME) {

        if (frame->type == MAIN_FRAME)
            pout = env->out;
        else if(frame->type == SUB_FRAME)
            pout = ((MotoFrame *)vec_get(env->frames, env->frameindex - 1))->out;

        e = stab_getKeys(frame->symtab);
        while (enum_hasNext(e)) {
            int indent = env->frameindex+1;
            char* n = (char*)enum_next(e);
            MotoVar* mv = (MotoVar*)stab_get(frame->symtab,n);

            while (--indent >= 0)
                buf_puts(pout, "	 ");

            buf_printf(pout,"%s %s=%s;\n", moto_valToCType(mv->vs), n,moto_defaultValForCType(mv->vs));

        }
        enum_free(e);

        buf_puts(pout, "\n");

        if (frame->type == MAIN_FRAME)  /* FIXME : big hack to get out buffer set for functions */
            buf_printf(pout,"out = outputBuffer;\n");
    }

    switch (frame->type) {
    case MAIN_FRAME:
        /* append buffer from frame to env main buffer */
        pout = env->out;
        buf_cat(pout, frame->out);
        break;
    case FN_FRAME:
        if (env->mode != COMPILER_MODE) {
            /* append buffer from frame current function buffer */
            pframe = (MotoFrame *)vec_get(env->frames, env->frameindex - 1);
            buf_cat(pframe->out, frame->out);
        } else {
            /* append buffer from frame current function buffer */
            buf_cat(env->fcodebuffer, frame->out);
        }
        break;
    case SUB_FRAME:
    default:
        /* append buffer from frame to buffer in parent frame */
        pframe = (MotoFrame *)vec_get(env->frames, env->frameindex - 1);
        buf_cat(pframe->out, frame->out);
        break;
    }

    /* Release the frame's output buffer */
    opool_release(env->bufpool,frame->out);

    /* Free frame structure */
    opool_release(env->stkpool,frame->opstack);

    /* Free vars and associated vals */
    e = stab_getKeys(frame->symtab);
    while (enum_hasNext(e)) {
        char* n = (char*)enum_next(e);
        MotoVar* var = (MotoVar*)stab_get(frame->symtab,n);

        /* Do not free vars that are class member vars */


        if(env->mode != COMPILER_MODE) {
            moto_freeVal(env,var->vs);
        } else {
            opool_release(env->valpool,var->vs);
        }
        free(var);

    }
    enum_free(e);

    stab_free(frame->symtab);

    /* set new frame */

    vec_removeAt(env->frames, env->frameindex);
    env->frameindex = env->frameindex - 1;
    if(env->frameindex < 0)
        env->frame = NULL;
    else
        env->frame = vec_get(env->frames, env->frameindex);

    free(frame);

}
Beispiel #22
0
Datei: pp.c Projekt: berkus/moto
void motopp_freeFrame(MotoPP *ppenv) {
   MotoPPFrame *frame;
   Enumeration *e;
   
   if (stack_size(ppenv->frames) <= 1) {
      frame = stack_pop(ppenv->frames);
      buf_cat(ppenv->out, frame->out);
      ppenv->frame = NULL;
   }
   else {
      MotoPPVal *val;
      MotoPPFrame *pframe;
      SymbolTable *macros;
      MotoMacro *m;
      char *catstr;
      int catstrlen;
      frame = stack_pop(ppenv->frames);
      pframe = stack_peek(ppenv->frames);
      
      switch (frame->type) {
         case MACRO_FRAME:
            m = frame->macro;
            val = motopp_createVal(ppenv);
            if (ppenv->flags & MACRO_DEBUG_FLAG) {
               buf_printf(frame->out, LINE_FMT_2, pframe->filename, pframe->lineno);
            }
            val->sval = buf_toString(frame->out);
            stack_push(pframe->opstack, val);
                                                                           
            macros = stack_pop(ppenv->macrostack);
            e = stab_getKeys(macros);
            while (enum_hasNext(e)) {
               char *name = (char *)enum_next(e);
               MotoMacro *m = (MotoMacro *)stab_get(macros, name);
               motopp_freeMacro(m);
            }
            stab_free(macros);
            enum_free(e);
            break;
         case INCLUDE_FRAME:
            catstr = buf_data(frame->out);
            catstrlen = strlen(catstr);            
            if (catstr[catstrlen - 1] == '\n') {
               catstr[catstrlen - 1] = '\0';
            }
            buf_puts(pframe->out, catstr);
            if (ppenv->flags & MACRO_DEBUG_FLAG) {
               buf_printf(pframe->out, LINE_FMT_2, pframe->filename, pframe->lineno);
            }
            break;
         default:
            buf_cat(pframe->out, frame->out);
            break;
      }
      
      /* reset pplineno */
      pplineno = pframe->lineno;

      /* reset frame */
      ppenv->frame = pframe;   
   } 

   if (frame->yybuf != NULL) {
      if (shared_check(frame->yybuf)) {
         motopp_freeYYBuffer(frame->yybuf);
      }
   }
   if (frame->yybufmem != NULL) {
      free(frame->yybufmem);
   }
   buf_free(frame->out);
   stack_free(frame->opstack);
   free(frame->filename);
   free(frame->relative_root);
   free(frame);
   
}