Beispiel #1
0
static void pr_dec(FILE *out, A_dec v, int d) {
	indent(out, d);
	switch (v->kind) {
	case A_functionDec:
		fprintf(out, "functionDec(%s,\n", S_name(v->u.function.func));
		pr_tyfieldList(out, v->u.function.tyfieldList, d + 1);fprintf(out, ",\n");
		fprintf(out, "%s,\n", S_name(v->u.function.type));
		pr_exp(out, v->u.function.exp, d + 1); fprintf(out, ")");
		break;
	case A_varDec:
		fprintf(out, "varDec(%s,\n", S_name(v->u.var.id));
		if (v->u.var.type) {
			indent(out, d + 1); fprintf(out, "%s,\n", S_name(v->u.var.type));
		}
		pr_exp(out, v->u.var.exp, d + 1); fprintf(out, ",\n");
		indent(out, d + 1); //fprintf(out, "%s", v->u.var.escape ? "TRUE)" : "FALSE)");
		break;
	case A_typeDec:
		fprintf(out, "typeDec(\n");
		fprintf(out, "%s,\n", S_name(v->u.type.type));
		pr_ty(out, v->u.type.ty, d + 1); fprintf(out, ")");
		break;
	default:
		assert(0);
	}
}
Beispiel #2
0
/* type-id { id = exp {, id = exp}} or type-id {}
 * the field names and types of the record exp must match
 * those of the named type, in the order given
 */
struct expty transRecordExp(S_table venv, S_table tenv, A_exp e)
{
  Ty_ty recordType = S_look(tenv, e->u.record.typ);
  if (!recordType) {
    EM_error(e->pos, "undefined record type %s",
        S_name(e->u.record.typ));
    return ExpTy(NULL, NULL);
  }

  A_efieldList efl = e->u.record.fields;
  Ty_fieldList tfl = recordType->u.record;

  while (efl && tfl) {
    if (efl->head->name != tfl->head->name) {
      EM_error(e->pos, "field name error %s", S_name(efl->head->name));
    }
    if (transExp(venv, tenv, efl->head->exp).ty != tfl->head->ty) {
      EM_error(e->pos, "field type error %s", S_name(efl->head->name));
    }

    efl = efl->tail;
    tfl = tfl->tail;
  }
  return ExpTy(NULL, recordType);
}
void ABSYN_PrintVarDec(A_dec dec)
{
	/*******************************/
	/* Print my serial node number */
	/*******************************/
	fprintf(fl,"v%d ",dec->PrintMyNodeSerialNumber);

	if (dec->u.var.init == NULL)
	{
		/********************************************/
		/* VariableDeclaration ---> VAR ID COLON ID */
		/********************************************/
		fprintf(fl,"[label = \"%s%s:%s\"];\n","Variable\nDeclaration\n",S_name(dec->u.var.var),S_name(dec->u.var.typ));
		return;
	}

	if (dec->u.var.typ == NULL)
	{
		/**********************************************/
		/* VariableDeclaration ---> VAR ID ASSIGN exp */
		/**********************************************/
		fprintf(fl,"[label = \"%s(%s)\"];\n","Variable\nDeclaration\n",S_name(dec->u.var.var));
		//fprintf(fl,"v%d -> v%d;\n",fieldList->PrintMyNodeSerialNumber,fieldList->head->PrintMyNodeSerialNumber);
		ABSYN_PrintTreeRecursively(dec->u.var.init);
		return;
	}

	/*******************************************************/
	/* VariableDeclaration ---> VAR ID COLON ID ASSIGN exp */
	/*******************************************************/
	fprintf(fl,"[label = \"%s(%s:%s)\"];\n","Variable\nDeclaration\n\n",S_name(dec->u.var.var),S_name(dec->u.var.typ));
	//fprintf(fl,"v%d -> v%d;\n",fieldList->PrintMyNodeSerialNumber,fieldList->head->PrintMyNodeSerialNumber);
	ABSYN_PrintTreeRecursively(dec->u.var.init);
	return;
}
Beispiel #4
0
//check function call exp
static struct expty transExpCall( S_table venv, S_table tenv, A_exp e)
{
  //make sure e is funcall, just in case
  assertKind( e->pos, e->kind, A_callExp , "function call" );

  //find function type in tenv
  E_entry funentry = (E_entry)S_look( venv, e->u.call.func );
  //may be not a function name or can't find at all
  if( funentry == NULL)
    EM_error( e->pos , S_name( e->u.call.func ) , "not defined" );
  if( funentry->kind != E_funEntry )
    EM_error( e->pos , S_name( e->u.call.func ) , "is not function name" );
  //traverse all arguments 
  Ty_tyList params = funentry->fun.formalTys;
  A_expList args = e->u.call.args;
  for( ; args!=NULL && params!=NULL ; 
       args = args->tail, params = params->tail )
    {
      Ty_ty param = params->head;
      //evaluate argument exp first
      A_exp arg = args->head;
      struct expty exp = transExp( venv, tenv, arg );
      assertKind( arg->pos, exp.ty->kind , param->kind ,
		  tyKindName(param->kind) );
    }
  //if argument number satisfy argument list in function
  if( args != NULL )
    EM_error( e->pos, "Too much arguments" );
  else if( params != NULL )
    EM_error( e->pos, "Expect more arguments" );
  return _expTy( NULL, funentry->fun.resultTy );
}
static void pr_dec(FILE *out, A_dec v, int d) {
  indent(out, d);
  switch (v->kind) {
    case A_functionDec:
      fprintf(out, "functionDec(\n"); 
      pr_fundecList(out, v->u.function, d+1);
      fprintf(out, ")");
      break;
    case A_varDec:
      fprintf(out, "varDec(%s,\n", S_name(v->u.var.var));
      if (v->u.var.typ) {
        indent(out, d+1);
        fprintf(out, "%s,\n", S_name(v->u.var.typ)); 
      }
      pr_exp(out, v->u.var.init, d+1);
      fprintf(out, ",\n");
      indent(out, d+1);
      fprintf(out, "%s", v->u.var.escape ? "TRUE)" : "FALSE)");
      break;
    case A_typeDec:
      fprintf(out, "typeDec(\n"); 
      pr_nametyList(out, v->u.type, d+1);
      fprintf(out, ")");
      break;
    default:
      assert(0); 
  } 
}
static void pr_field(FILE *out, A_field v, int d) {
  indent(out, d);
  fprintf(out, "field(%s,\n", S_name(v->name));
  indent(out, d+1);
  fprintf(out, "%s,\n", S_name(v->typ));
  indent(out, d+1);
  fprintf(out, "%s", v->escape ? "TRUE)" : "FALSE)");
}
static void pr_fundec(FILE *out, A_fundec v, int d) {
 indent(out, d);
 fprintf(out, "fundec(%s,\n", S_name(v->name));
 pr_fieldList(out, v->params, d+1); fprintf(out, ",\n");
 if (v->result) {
   indent(out, d+1); fprintf(out, "%s,\n", S_name(v->result));
 }
 pr_exp(out, v->body, d+1); fprintf(out, ")");
}
Beispiel #8
0
static struct expty transVar(Tr_level level, Tr_exp breakk, S_table venv, S_table tenv, A_var v) {
	if (!v) {return expTy(Tr_noExp(), Ty_Void());}
    /*!those several var is ugly*/
	E_enventry x;
	struct expty et,et2;
	Ty_fieldList fl;
	Tr_exp trans;

	switch (v->kind) {
	case A_simpleVar:/* var id (a)*/
		x = S_look(venv, v->u.simple); 
		trans = Tr_noExp();
		if (x && x->kind == E_varEntry) {
			trans = Tr_simpleVar(x->u.var.access, level);
			return expTy(trans, actual_ty(x->u.var.ty));
		} else {
			EM_error(v->pos, "undefined var %s", S_name(v->u.simple));
			return expTy(trans, Ty_Int());
		}
		break;
	case A_fieldVar:/* var record (a.b)*/
		et = transVar(level, breakk, venv, tenv, v->u.field.var);
		trans = Tr_noExp();
		if (et.ty->kind != Ty_record) {
			EM_error(v->pos, "not a record type");
		} else {
			int i = 0;
			for (fl = et.ty->u.record; fl; fl = fl->tail, i++) { /*fl is Ty_fieldList*/
				if (fl->head->name == v->u.field.sym) {
					trans = Tr_fieldVar(et.exp, i);
					return expTy(trans, actual_ty(fl->head->ty));
				}
			}
			EM_error(v->pos, "no such field in record %s", S_name(v->u.field.sym));
		}
		return expTy(trans, Ty_Int());
		break;
	case A_subscriptVar: /*var array (a[b])*/ 
		et  = transVar(level, breakk, venv, tenv, v->u.subscript.var);
		trans = Tr_noExp();
		if (et.ty->kind != Ty_array) {
			EM_error(v->pos, "not a array type");
		} else {
			et2 = transExp(level, breakk, venv, tenv, v->u.subscript.exp);
			if (et2.ty->kind != Ty_int) {
				EM_error(v->pos, "int required");
			} else {
				trans = Tr_subscriptVar(et.exp, et2.exp);
				return expTy(trans, actual_ty(et.ty->u.array));
			}
		}
		return expTy(trans, Ty_Int());
	default:
		assert(0);
	}
}
Beispiel #9
0
Ty_tyList makeFormalTyList(S_table tenv, A_fieldList params) {
	A_fieldList ptr = params;
	Ty_tyList ret, ptr2;
	Ty_ty ty;

	ret = NULL; ptr2 = NULL;
	while(ptr != NULL) {
		ty = S_look(tenv, ptr->head->typ);
		if(ty != NULL) {
			if(ret == NULL) {
				ret = Ty_TyList(ty, NULL);
				ptr2 = ret;
			}
			else {
				ptr2->tail = Ty_TyList(ty, NULL);
				ptr2 = ptr2->tail;
			}
		}
		else {
			EM_error(ptr->head->pos, "undefined type %s", S_name(ptr->head->typ));
			exit(1);
		}
		ptr = ptr->tail;
	}
	return ret;
}
Beispiel #10
0
struct expty transExp_recordExp(Tr_level level, S_table venv, S_table tenv, A_exp a, Temp_label breakk) {
  Ty_ty typ = actual_ty(S_look(tenv, a->u.record.typ));
  A_pos pos = a->pos;

  // check record type
  if (!typ || typ->kind != Ty_record) {
    EM_error(pos, "record type '%s' mismatched", S_name(a->u.record.typ));
    return expTy(NULL, Ty_Void());
  }

  // check exp field type
  A_efieldList efields = a->u.record.fields;
  Ty_fieldList fields = typ->u.record;
  Tr_expList expl_h = Tr_ExpList(NULL, NULL), expl_p = expl_h;
  int size = 0;
  while (efields && fields) {
    struct expty efield = transExp(level, venv, tenv, efields->head->exp, breakk);
    pos = efields->head->exp->pos;
    if (!has_same_ty(fields->head->ty, efield.ty)) {
      EM_error(pos, "record field type mismatched: '%s' and '%s'",
               type_msg(fields->head->ty), type_msg(efield.ty));
      break;
    }
    efields = efields->tail;
    fields = fields->tail;
    expl_p->tail = Tr_ExpList(efield.exp, NULL);
    expl_p = expl_p->tail;
    size += 1;
  }
  if (efields || fields) EM_error(pos, "record type mismatched");

  expl_p = expl_h->tail;
  free(expl_h);
  return expTy(Tr_recordExp(expl_p, size), S_look(tenv, a->u.record.typ));
}
Beispiel #11
0
Ty_ty transTy(Tr_level level, S_table tenv, A_ty a) {
  switch (a->kind) {
    case A_nameTy: {
      return Ty_Name(a->u.name, S_look(tenv, a->u.name));
    }
    case A_recordTy: {
      Ty_fieldList h = Ty_FieldList(NULL, NULL), p = h, fields;
      for (A_fieldList efields = a->u.record; efields;
           efields = efields->tail) {
        Ty_ty typ = S_look(tenv, efields->head->typ);
        if (!typ) {
          EM_error(efields->head->pos, "type '%s' undefined",
                   S_name(efields->head->name));
          typ = Ty_Int();
        }
        p->tail = Ty_FieldList(Ty_Field(efields->head->name, typ), NULL);
        p = p->tail;
      }
      fields = h->tail;
      free(h);

      return Ty_Record(fields);
    }
    case A_arrayTy: {
      return Ty_Array(S_look(tenv, a->u.array));
    }
  }
}
Beispiel #12
0
static struct expty transVar(Tr_level level, S_table venv, S_table tenv, A_var v) {
	E_enventry x;
	struct expty et,et2;
	Ty_fieldList fl;

	switch (v->kind) {
	case A_simpleVar:/* var id (a)*/
		x = S_look(venv, v->u.simple);
		if (x && x->kind == E_varEntry) {
			return expTy(NULL, actual_ty(x->u.var.ty));
		} else {
			EM_error(v->pos, "undefined var %s", S_name(v->u.simple));
			return expTy(NULL, Ty_Int());
		}
		break;
	case A_fieldVar:/* var record (a.b)*/
		et = transVar(level, venv, tenv, v->u.field.var);
		if (et.ty->kind != Ty_record) {
			EM_error(v->pos, "not a record type");
			return expTy(NULL, Ty_Record(NULL));
		} else {
			for (fl = et.ty->u.record; fl; fl = fl->tail) {
				if (fl->head->name == v->u.field.sym) {
					return expTy(NULL, actual_ty(fl->head->ty));
				}
			}
			EM_error(v->pos, "no such field in record %s", S_name(v->u.field.sym));
		}
		return expTy(NULL, Ty_Record(NULL));
		break;
	case A_subscriptVar:/*var array (a[b])*/ 
		et  = transVar(level, venv, tenv, v->u.subscript.var);
		if (et.ty->kind != Ty_array) {
			EM_error(v->pos, "not a array type");
		} else {
			et2 = transExp(level, venv, tenv, v->u.subscript.exp);
			if (et2.ty->kind != Ty_int) {
				EM_error(v->pos, "int required");
			} else {
				return expTy(NULL, actual_ty(et.ty->u.array));
			}
		}
		return expTy(NULL, Ty_Array(NULL));
	default:
		assert(0);
	}
}
Beispiel #13
0
/* This will infinite loop on mutually recursive types */
void Ty_print(Ty_ty t)
{
  if (t == NULL) printf("null");
  else { printf("%s", str_ty[t->kind]);
         if (t->kind == Ty_name) {
	   printf(", %s", S_name(t->u.name.sym)); }
       }
}
Beispiel #14
0
static void pr_ty(FILE *out, A_ty v, int d) {
	indent(out, d);
	switch (v->kind) {
	case A_idTy:
		fprintf(out, "idTy(%s)", S_name(v->u.id));
		break;
	case A_tyfieldTy:
		fprintf(out, "tyfieldTy(\n");
		pr_tyfieldList(out, v->u.tyfield, d + 1); fprintf(out, ")");
		break;
	case A_arrayTy:
		fprintf(out, "arrayTy(%s)", S_name(v->u.array));
		break;
	default:
		assert(0);
	}
}
void S_print(S_table t) {
    S_binder b = t->top;
    while(b != NULL) {
        printf("%s->", S_name(b->sym));
        b = b->prevtop;
    }
    printf("\n");
}
Beispiel #16
0
struct expty transVar (S_table venv, S_table tenv, A_var v)
{
  switch (v->kind) {
    case A_simpleVar: {
        E_enventry x = S_look (venv, v->u.simple);

        if (x && x->kind == E_varEntry) {
          return expTy (NULL, actual_ty (x->u.var.ty));
        } else {
          EM_error (v->pos, "undefined variable %s", S_name (v->u.simple));
          return expTy (NULL, Ty_Int());
        }
      }

    case A_fieldVar: {
        struct expty var = transVar (venv, tenv, v->u.field.var);

        Ty_fieldList fList = var.ty->u.record;

        while (fList && fList->head->name != v->u.field.sym) {
          fList = fList->tail;
        }

        if (!fList) {
          EM_error (v->pos, "undefined subName %s", S_name (v->u.field.sym));
          return expTy (NULL, Ty_Int());
        } else {
          return expTy (NULL, actual_ty (fList->head->ty));
        }
      }

    case A_subscriptVar: {
        struct expty var = transVar (venv, tenv, v->u.subscript.var);
        struct expty exp = transExp (venv, tenv, v->u.subscript.exp);

        if (exp.ty->kind != Ty_int) {
          EM_error (v->pos, "subscript should be int");
          return expTy (NULL, Ty_Int());
        } else {
          return expTy (NULL, actual_ty (var.ty->u.array));
        }
      }
  }

  assert (0);
}
Beispiel #17
0
static void pr_assignfield(FILE *out, A_assignfield v, int d) {
	indent(out, d);
	if (v) {
		fprintf(out, "assignfield(%s,\n", S_name(v->id));
		pr_exp(out, v->exp, d + 1); fprintf(out, ")");
	}
	else fprintf(out, "assignfield()");
}
Beispiel #18
0
struct expty transVar(S_table venv, S_table tenv, A_var v) {
	switch (v->kind) {
		case A_simpleVar: {
			E_enventry x = S_look(venv, v->u.simple);
			if (x && x->kind == E_varEntry) {
				return expTy(NULL, actual_ty(x->u.var.ty));
			}
			EM_error(v->pos, (string)"undeclared variable '%s'", S_name(v->u.simple));
			return expTy(NULL, Ty_Int());
		}
		case A_fieldVar: {
			struct expty res = transVar(venv, tenv, v->u.field.var);
			Ty_ty ty = res.ty;
			if (ty->kind == Ty_record) {
				Ty_fieldList tfl = ty->u.record;
				for (tfl = ty->u.record; tfl; tfl = tfl->tail) {
					if (tfl->head->name == v->u.field.sym) {
						return expTy(NULL, actual_ty(tfl->head->ty));
					}
				}
				EM_error(v->pos, "field '%s' not in record type", S_name(v->u.field.sym));
				return expTy(NULL, Ty_Int());
			}
			EM_error(v->pos, "variable not record");
			return expTy(NULL, Ty_Int());
		}
		case A_subscriptVar: {
			struct expty res = transVar(venv, tenv, v->u.subscript.var);
			struct expty index = transExp(venv, tenv, v->u.subscript.exp);
			Ty_ty ty = res.ty;
			if (ty->kind != Ty_array) {
				EM_error(v->pos, "variable not array");
				return expTy(NULL, Ty_Int());
			}
			if (index.ty->kind != Ty_int) {
				EM_error(v->pos, "array index error");
				return expTy(NULL, Ty_Int());
			}
			return expTy(NULL, actual_ty(ty->u.array));
		}
		default: {
			assert(0);
		}
	}
}
Beispiel #19
0
/* translate A_ty in AST to real type representation
 */
Ty_ty transTy(S_table tenv, S_symbol ty_name, A_ty t)
{
  switch(t->kind) {
    case A_nameTy: {
      Ty_ty ty = S_look(tenv, t->u.name);
      if (ty)
        return Ty_Name(ty);
      else
        return NULL;
    }
    case A_recordTy: {
      A_fieldList a_record = NULL;
      A_field a_field = NULL;
      Ty_fieldList t_record = NULL;
      Ty_fieldList saved_t_record = NULL;
      Ty_ty ty = NULL;

      /* we only allow record field type to refer itself or already defined
       * type: type record = { id : record, id : other_defined_type }
       */
      for (a_record = t->u.record; a_record; a_record = a_record->tail) {
        a_field = a_record->head;
        ty = S_look(tenv, a_field->typ);
        if (ty_name == a_field->typ || ty) {
          if (t_record) {
            t_record->tail = Ty_FieldList(Ty_Field(a_field->name, ty), NULL);
            t_record = t_record->tail;
          } else {
            t_record = Ty_FieldList(Ty_Field(a_field->name, ty), NULL);
            saved_t_record = t_record;
          }
        } else {
          EM_error(a_field->pos, "undefined type %s", S_name(a_field->typ));
          return NULL;
        }
      }
      
      // fill the record's self-recursive reference
      Ty_ty new_record = Ty_Record(saved_t_record);
      Ty_fieldList tfl = NULL;
      for (tfl = t_record; tfl; tfl = tfl->tail) {
        if (!tfl->head->ty) {
          tfl->head->ty = new_record;
        }
      }
      return new_record;
    }
    case A_arrayTy: {
      Ty_ty ty = S_look(tenv, t->u.array);
      if (ty)
        return Ty_Array(ty);
      else
        return NULL;
    }
  }
}
Beispiel #20
0
struct expty transVar(S_table venv, S_table tenv, A_var v)
{

  switch( v->kind )
    {
    case A_simpleVar:
      {
	string varName = S_name( v->u.simple );
	E_entry var = (E_entry) S_look( venv , v->u.simple );
	if( var == NULL )
	  EM_error( v->pos , "can't find" , varName);
	if( var->kind != E_varEntry )
	  EM_error( v->pos , varName , " is not variable " );
	return _expTy( NULL , var->var );
      }
    case A_fieldVar:
      {
	struct expty var =  transVar( venv , tenv , v->u.field.var);
	Ty_ty varType = var.ty;
	string fieldName = S_name( v->u.field.sym );
	if( varType->kind != Ty_record )
	  EM_error( v->pos , " is not record " );
	Ty_fieldList fields = varType->u.record;
	for( ; fields != NULL ; fields = fields->tail )
	  {
	    Ty_field field = fields->head ;
	    if( field->name == v->u.field.sym )
	      return _expTy( NULL , field->ty );
	  }
	EM_error( v->pos , "can't find field " , S_name( v->u.field.sym ) );
      }
    case A_subscriptVar:
      {
	struct expty var = transVar( venv , tenv , v->u.subscript.var );
	struct expty subs = transExp( venv , tenv , v->u.subscript.exp );
	if( subs.ty->kind != Ty_int )
	  EM_error( v->pos , "array subs should be int" );
	return var;
      }
    default :
      assert(0);
    }
}
void ABSYN_PrintTypeDec(A_dec dec)
{
	switch (dec->u.type->ty->kind) {
	case (A_nameTy):
		
		fprintf(fl,"[label = \"%s%s = %s\"];\n","Type\nDeclaration\n\n",S_name(dec->u.type->name),S_name(dec->u.type->ty->u.name));
		break;

	case (A_arrayTy):
		
		fprintf(fl,"[label = \"%s%s = %s\"];\n","Type\nDeclaration\n\n",S_name(dec->u.type->name),S_name(dec->u.type->ty->u.array));
		break;

	case (A_recordTy):
		
		fprintf(fl,"[label = \"%s%s = \"];\n","Type\nDeclaration\n\n",S_name(dec->u.type->name));
		ABSYN_PrintFieldsDecRecursively(dec->u.type->ty->u.record);
		break;
	}
}
Beispiel #22
0
struct expty transVar_simpleVar(Tr_level level, S_table venv, S_table tenv, A_var v, Temp_label breakk) {
  E_enventry x = S_look(venv, v->u.simple);

  if (!x || x->kind != E_varEntry) {
    EM_error(v->pos, "undeclared variable '%s'", S_name(v->u.simple));
    return expTy(NULL, Ty_Int());
  }

  return expTy(Tr_simpleVar(x->u.var.access, level),
               actual_ty(x->u.var.ty));
}
static void pr_efield(FILE *out, A_efield v, int d) {
  indent(out, d);
  if (v) {
    fprintf(out, "efield(%s,\n", S_name(v->name));
    pr_exp(out, v->exp, d+1);
    fprintf(out, ")");
  }
  else {
    fprintf(out, "efield()");
  }
}
Beispiel #24
0
/* Print A_var types. Indent d spaces. */
static void pr_var(FILE *out, A_var v, int d) {
	indent(out, d);
	switch (v->kind) {
	case A_simpleVar:
		fprintf(out, "simpleVar(%s)", S_name(v->u.simple));
		break;
	case A_fieldVar:
		fprintf(out, "%s\n", "fieldVar(");
		pr_var(out, v->u.field.var, d + 1); fprintf(out, "%s\n", ",");
		indent(out, d + 1); fprintf(out, "%s)", S_name(v->u.field.id));
		break;
	case A_subscriptVar:
		fprintf(out, "%s\n", "subscriptVar(");
		pr_var(out, v->u.subscript.var, d + 1); fprintf(out, "%s\n", ",");
		pr_exp(out, v->u.subscript.exp, d + 1); fprintf(out, "%s", ")");
		break;
	default:
		assert(0);
	}
}
Beispiel #25
0
static int ret_TableMatch(A_var newvar){
			Var_VarList next;
			next=Saved_CT;
			//printf("Constant matching: \n");
			while(next!=NULL){
				if(!(strcmp(S_name(newvar->u.simple),S_name(next->head->v))))
				{
					//printf("const matched %s", S_name(newvar->u.simple));
					current_Const = next->head->c;
					return 1;
				}
				else
				{
					//printf("Not matched\n");
				next=next->tail;
				}
			}
			//printf("\n");
			return 0;
}
Beispiel #26
0
struct expty transVar(S_table venv, S_table tenv, A_var v)
{
  switch(v->kind) {
    case A_simpleVar: {
      E_enventry x = S_look(venv, v->u.simple);
      if (x && x->kind == E_varEntry)
        return ExpTy(NULL, x->u.var.ty);
      else {
        EM_error(v->pos, "undefined variable %s",
            S_name(v->u.simple));
        return ExpTy(NULL, NULL);
      }
    }

    case A_fieldVar: {
      struct expty et = transVar(venv, tenv, v->u.field.var);
      if (et.ty && et.ty->kind == Ty_record) {
        Ty_fieldList fl = et.ty->u.record;
        for(; fl; fl = fl->tail) {
          if (fl->head->name == v->u.field.sym) {
            return ExpTy(NULL, fl->head->ty);
          }
        }
      }
      // can NOT get the v's name, use field name instaed
      EM_error(v->pos, "undefined variable %s",
          S_name(v->u.field.sym));
      return ExpTy(NULL, NULL);
    }

    case A_subscriptVar: {
      struct expty et = transVar(venv, tenv, v->u.field.var);
      if (et.ty && et.ty->kind == Ty_array) {
        return ExpTy(NULL, et.ty->u.array);
      }
      // can NOT get v's name
      EM_error(v->pos, "undefined variable");
      return ExpTy(NULL, NULL);
    }
  }
}
Beispiel #27
0
/* translate variable declaration
 * var id:type-id := exp
 */
void transVarDec(S_table venv, S_table tenv, A_dec d)
{
  struct expty e = transExp(venv, tenv, d->u.var.init);
  if (d->u.var.typ) {
    /* 1. check the type constraint if any
     * 2. init value of type Ty_Nil must be constrainted 
     * by a Ty_Record type
     */
    Ty_ty expected_ty = S_look(tenv, d->u.var.typ);
    if (!expected_ty) {
      EM_error(d->pos, "undefined type %s", S_name(d->u.var.typ));
      return;
    }

    if ((expected_ty->kind != Ty_record && e.ty == Ty_Nil())
        || expected_ty != e.ty) {
      EM_error(d->pos, "inconsistent var type %s",
          S_name(d->u.var.var));
      return;
    }
  }

  S_enter(venv, d->u.var.var, E_VarEntry(e.ty));
}
void ABSYN_PrintFieldsDecRecursively(A_fieldList fieldList)
{
	/*******************************/
	/* Print my serial node number */
	/*******************************/
	fprintf(fl,"v%d ",fieldList->PrintMyNodeSerialNumber);

	/*****************/
	/* Print content */
	/*****************/
	fprintf(fl,"[label = \"%s\"];\n",fieldList->PrintTheKindOfTreeIAm);

	/*********************/
	/* Print first field */
	/*********************/
	fprintf(fl,"v%d -> v%d;\n",fieldList->PrintMyNodeSerialNumber,fieldList->head->PrintMyNodeSerialNumber);

	/*********************/
	/* Print first field */
	/*********************/
	fprintf(fl,"v%d ",fieldList->head->PrintMyNodeSerialNumber);

	/*****************/
	/* Print content */
	/*****************/
	fprintf(fl,"[label = \"%s:%s\"];\n",S_name(fieldList->head->name),S_name(fieldList->head->typ));

	/********************************/
	/* Print the rest of the fields */
	/********************************/
	if (fieldList->tail != NULL)
	{
		fprintf(fl,"v%d -> v%d;\n",fieldList->PrintMyNodeSerialNumber,fieldList->tail->PrintMyNodeSerialNumber);
		ABSYN_PrintFieldsDecRecursively(fieldList->tail);
	}
}
Beispiel #29
0
	static void ret_dec(  A_dec *v, int d) {
	 //indent(  d);
	 switch ((*v)->kind) {
	 case A_functionDec:
	   //fprintf(  "functionDec(\n"); 
	   ret_fundecList(  &(*v)->u.function, d+1); //fprintf(  ")");
	   break;
	 case A_varDec:
	   //fprintf(  "varDec(%s,\n", S_name((*v)->u.var.var));
	   //printf(  "varDec(%s,\n", S_name((*v)->u.var.var));
	   if ((*v)->u.var.typ) {
	     //indent(  d+1); //fprintf(  "%s,\n", S_name((*v)->u.var.typ)); 
	   }
	    if(((*v)->u.var.init)->kind == A_intExp)
		{
		 printf("Simple declaration found :%s with constant on the right :  \n",S_name((*v)->u.var.var));
		  Var_var vin=checked_malloc(sizeof(*vin));
		 vin->v = (*v)->u.var.var;
		 vin->c = ((*v)->u.var.init)->u.intt;
		 
		 if(Saved_CT==NULL){
			 Saved_CT=Const_tableEnter(vin,NULL);
			 printf("First Entry into table!\n");
			 }
		else{
			
			if(Const_tableCheck(vin,&Saved_CT)==1)
			{
			Saved_CT=Const_tableEnter(vin,Saved_CT);
			 printf("Entered into table!\n");
			 printf("Saved_CT:\n");
			 Const_tablePrintExt(Saved_CT);
			}
			}
		}
	 	retScan_exp(  &(*v)->u.var.init, d+1); //fprintf(  ",\n");
	   //indent(  d+1); //fprintf(  "%s", (*v)->u.var.escape ? "TRUE)" : "FALSE)");
	   break;
	 case A_typeDec:
	   //fprintf(  "typeDec(\n"); 
	   ret_nametyList(  &(*v)->u.type, d+1); //fprintf(  ")");
	   break;
	 default:
	   assert(0); 
	 } 
	}
Beispiel #30
0
struct expty transVar_fieldVar(Tr_level level, S_table venv, S_table tenv, A_var v, Temp_label breakk) {
  struct expty var = transVar(level, venv, tenv, v->u.field.var, breakk);

  if (var.ty->kind != Ty_record)
    EM_error(v->pos, "invalid types '%s' for record field", type_msg(var.ty));

  int index = 0;
  Ty_fieldList fields;
  for (fields = var.ty->u.record; fields != NULL; fields = fields->tail, index += 1)
    if (fields->head->name == v->u.field.sym) break;
  if (!fields) {
    EM_error(v->pos, "field '%s' not defined in record type", S_name(v->u.field.sym));
    return expTy(NULL, Ty_Int());
  }
  return expTy(Tr_fieldVar(var.exp, index, level),
               actual_ty(fields->head->ty));
}