![]() | ![]() | ![]() | Análise Semântica |
Análise Semântica:
Mapeamento de Ids para Tipos e Localização:
function f(a:int, b:int, c:int) =
(print_int(a+c);
let var j := a+b
var a := "hello"
in print(a); print_int(j);
end;
print_int(b)
)
Primeiro, notar que sigma1 + sigma2 != sigma2 + sigma1:
Algumas linguagens permitem ambientes múltiplos:
package M;
class E { static int a = 5; }
class N {
static int b = 10;
static int a = E.a + b;
}
class D { static int d = E.a + N.a; }
O ambiente de N pode usar variáveis de M e de D!
Tiger tem dois espaços de nomes:
struct Ty_ty_
{ enum{Ty_record, Ty_nil, Ty_int, Ty_string,
Ty_array, Ty_name, Ty_void} kind;
union {Ty_fieldList record;
Ty_ty array;
struct {S_symbol sym; Ty_ty;} name;
} u; }
Ty_ty Ty_Nil(void); Ty_ty Ty_Int(void);
Ty_ty Ty_String(void); Ty_ty Ty_Void(void);
Ty_ty Ty_Record(Ty_fieldlist fields);
Ty_ty Ty_Array(Ty_ty ty);
Ty_ty Ty_Name(S_symbol sym, Ty_ty ty);
struct Ty_tyList_ {Ty_ty head;Ty_tyList tail;}
Ty_tyList Ty_tyList(Ty_ty head,Ty_tyList tail);
struct Ty_field_ {S_symbol name; Ty_ty ty;}
Ty_field Ty_Field(S_symbol name; Ty_ty ty);
struct Ty_fieldList_ {Ty_field head;
Ty_fieldList tail;}
Ty_fieldList Ty_FieldList(Ty_field head;
Ty_fieldList tail);
Em Tiger temos ambientes para tipos e valores:
let type a = int
var a : a = 5
var b : a = a
in b + a
end
Verifica o tipo dos elementos que aparecem na árvore sintáctica:
case A_opExp;
A_oper oper = a->u.op.oper;
struct expty left = transexp(venv,
tenv,a->u.op.left);
struct expty right = transexp(venv,
tenv,a->u.op.right);
if (oper == A_plusOp) {
if (left.ty->kind != Ty_int)
Em_error(a->u.op.left->pos,
"integer required");
if (right.ty->kind != Ty_int)
Em_error(a->u.op.right->pos,
"integer required");
return(expTy(NULL,Ty_Int));
}
function f(a: ta, b: tb,) : rt = body
A_fundec f = d->u.function->head;
Ty_ty resultTy = tylook(tenv,f->result,
f->pos);
Ty_tyList formalTys = makeFormalTyList(
tenv,f->params);
S_enter(venv,f->name,E_FunEntry(
formalTys,resultTy));
S_beginScope(venv);
{A_fieldList l=f->params;
Ty_tyList t = formalTys;
for(;l;l=l->tail,t=t->tail)
S_enter(venv,l->head->name,
E_VarEntry(t->head));
}
S_endScope(venv);
O que fazer com tipos ou funções que se chamam mútuamente?
type list = {first: int, rest: list}
{first: int, rest: list}
![]() | ![]() | ![]() | Análise Semântica |