Exemple #1
0
void constdef(){
    symbol_t t, id;
    if(!isType(sym)){
        msg(ERR, "missing a type name", line);
		ERROR_STATUS = 1;
    }
    t = copySym(sym);
    do{
        nextSym();
        if(sym->type!=ID){
            msg(ERR, "missing a identifier after a type name", line);
			ERROR_STATUS = 1;
        }
        id = copySym(sym);
        nextSym();
        if(sym->type!=ASN){
            msg(ERR, "constant need a value", line);
			ERROR_STATUS = 1;
        }
        nextSym();
        readLiteral();
        if(!isLiteral(sym)){
            msg(ERR, "missing a literal in constant definition", line);
			ERROR_STATUS = 1;
        }
        createconst(t, id, sym);
        mfree(t); mfree(id);
        nextSym();
    }while(sym->type==COMMA);
}
Exemple #2
0
void printfcall(){
    ident_t tmp, id;
    if(sym->type!=LPAREN){
        msg(ERR, "missing \'(\'", line);
		ERROR_STATUS = 1;
    }
    nextSym();
    if(sym->type!=STRLIT){
        tmp = expression(0);
        gen(OUT, tmp, 0, 0);
    }else{
        readLiteral();
        id = getLiteral(global, sym);
        gen(OUT, id, 0, 0);
        nextSym();
        if(sym->type==COMMA){
            nextSym();
            tmp = expression(0);
            gen(OUT, tmp, 0, 0);
        }
    }
    if(sym->type!=RPAREN){
        msg(ERR, "missing \')\'", line);
		ERROR_STATUS = 1;
    }
    nextSym();
}
Exemple #3
0
static Flags readFlags(FILE *f) {
  int i;
  int count = readInt(f);
  Flags flags = (Flags) malloc(sizeof(struct _Flags)+count*sizeof(struct _Flag));
  
  flags->count = count;
  for (i = 0; i < count; i++) {
    flags->values[i].name  = readCId(f);
    flags->values[i].value = readLiteral(f);
  }

  return flags;
}
Exemple #4
0
void jumpentry(ident_t expr, ident_t nextlab){
    ident_t lit;
    readLiteral();
    if(!isLiteral(sym)){
        // error occur;
    }
    if(sym->type!=INTEGER && sym->type!=CHARLIT){
        // error occur;
    }
    lit = getLiteral(global, sym);
    gen(CMP, expr, lit, 0);
    gen(JNE, nextlab, 0, 0);
    nextSym();
    if(sym->type!=COLON){
        // error occur;
    }
    nextSym();
    statement();
}
Exemple #5
0
ident_t factor(){
    ident_t ret = 0;
    symbol_t id;
    if(sym->type==ID){
        id = copySym(sym);
        nextSym();
        if(sym->type!=LPAREN){
            if(context)
                ret = findTable(context->local, (char*)(id->value));
            if(!ret)
                ret = findTable(global, (char*)(id->value));
            if(!ret){
                msg(ERR, "undefined identifier", line);
				ERROR_STATUS = 1;
            }
            return ret;
        }
        nextSym();
        if((ret = funccall(id))==0){
            msg(ERR, "no value return from function", line);
			ERROR_STATUS = 1;
        }
        mfree(id);
    }else if(sym->type==LPAREN){
        nextSym();
        ret = expression(tn);
        if(sym->type!=RPAREN){
            msg(ERR, "missing \')\'", line);
			ERROR_STATUS = 1;
        }
        nextSym();
    }else if(isLiteral(sym) || isAddOperator(sym)){
        readLiteral();
        ret = getLiteral(global, sym);
        nextSym();
    }
    return ret;
}
Exemple #6
0
static Patt readPatt(FILE *f) {
  int tag = readTag(f);

  switch (tag) {
  case TAG_PAPP:
    {
       CId fun = readCId(f);
       
       int i;
       int count = readInt(f);
       PattApp p = (PattApp) malloc(sizeof(struct _PattApp)+count*sizeof(Patt));
  
       p->_.tag      = tag;
       p->fun        = fun;
       p->args.count = count;
       for (i = 0; i < count; i++) {
         p->args.pats[i] = readPatt(f);
       }
       
       return ((Patt) p);
    }
  case TAG_PVAR:
    {
        PattVar p = (PattVar) malloc(sizeof(struct _PattVar));
        p->_.tag = tag;
        p->var   = readCId(f);
        return ((Patt) p);
    }
  case TAG_PAT:
    {
        PattAt p = (PattAt) malloc(sizeof(struct _PattAt));
        p->_.tag = tag;
        p->var   = readCId(f);
        p->pat   = readPatt(f);
        return ((Patt) p);
    }
  case TAG_PWILD:
    {
        PattWild p = (PattWild) malloc(sizeof(struct _PattWild));
        p->_.tag = tag;
        return ((Patt) p);
    }
  case TAG_PLIT:
    {
        PattLit p = (PattLit) malloc(sizeof(struct _PattLit));
        p->_.tag = tag;
        p->lit   = readLiteral(f);
        return ((Patt) p);
    }
  case TAG_PIMP:
    {
        PattImplArg p = (PattImplArg) malloc(sizeof(struct _PattImplArg));
        p->_.tag = tag;
        p->pat   = readPatt(f);
        return ((Patt) p);
    }
  case TAG_PTILDE:
    {
        PattTilde p = (PattTilde) malloc(sizeof(struct _PattTilde));
        p->_.tag = tag;
        p->e     = readExpr(f);
        return ((Patt) p);
    }
  default:
    __pgf_panic("Unknown pattern tag");
  }  
}
Exemple #7
0
static Expr readExpr(FILE *f) {
  int tag = readTag(f);
  
  switch (tag) {
  case TAG_ABS:
    {
        ExprAbs e = (ExprAbs) malloc(sizeof(struct _ExprAbs));
        e->_.tag = tag;
        e->bt    = readTag(f);
        e->var   = readCId(f);
        e->body  = readExpr(f);
        return ((Expr) e);
    }
  case TAG_APP:
    {
        ExprApp e = (ExprApp) malloc(sizeof(struct _ExprApp));
        e->_.tag = tag;
        e->left  = readExpr(f);
        e->right = readExpr(f);
        return ((Expr) e);
    }
  case TAG_LIT:
    {
        ExprLit e = (ExprLit) malloc(sizeof(struct _ExprLit));
        e->_.tag = tag;
        e->lit   = readLiteral(f);
        return ((Expr) e);
    }
  case TAG_MET:
    {
        ExprMeta e = (ExprMeta) malloc(sizeof(struct _ExprMeta));
        e->_.tag = tag;
        e->id    = readInt(f);
        return ((Expr) e);
    }
  case TAG_FUN:
    {
        ExprFun e = (ExprFun) malloc(sizeof(struct _ExprFun));
        e->_.tag = tag;
        e->fun   = readCId(f);
        return ((Expr) e);
    }
  case TAG_VAR:
    {
        ExprVar e = (ExprVar) malloc(sizeof(struct _ExprVar));
        e->_.tag = tag;
        e->index = readInt(f);
        return ((Expr) e);
    }
  case TAG_TYP:
    {
        ExprTyped e = (ExprTyped) malloc(sizeof(struct _ExprTyped));
        e->_.tag = tag;
        e->e     = readExpr(f);
        e->ty    = readType(f);
        return ((Expr) e);
    }
  case TAG_IMP:
    {
        ExprImplArg e = (ExprImplArg) malloc(sizeof(struct _ExprImplArg));
        e->_.tag = tag;
        e->e     = readExpr(f);
        return ((Expr) e);
    }
  default:
    __pgf_panic("Unknown expression tag");
  }
}