Esta página mostra as diferenças entre as duas revisões da página.
| Próxima revisão | Revisão anterior | ||
|
ocaml [2006/03/22 10:31] marcus Criação deste novo documento. |
ocaml [2010/01/18 15:45] (Actual) |
||
|---|---|---|---|
| Linha 43: | Linha 43: | ||
| </html> | </html> | ||
| + | |||
| + | ====== Material ====== | ||
| + | |||
| + | ===== Sintaxe abstrata do IMP ===== | ||
| + | <code ocaml> | ||
| + | (* categoria sintatica Num *) | ||
| + | type num = int;; | ||
| + | |||
| + | (* categoria sintatica Ident *) | ||
| + | type ident = string;; | ||
| + | |||
| + | (* categoria sintatica Bool: usamos bool *) | ||
| + | |||
| + | (* categoria sintatica AExpr *) | ||
| + | type aExpr = Num of num | Ident of ident | ||
| + | | Sum of aExpr * aExpr | ||
| + | | Diff of aExpr * aExpr | ||
| + | | Prod of aExpr * aExpr;; | ||
| + | |||
| + | (* categoria sintatica BExpr *) | ||
| + | type bExpr = Bool of bool | ||
| + | | Eq of aExpr * aExpr | Lt of aExpr * aExpr | ||
| + | | Not of bExpr | ||
| + | | And of bExpr * bExpr | ||
| + | | Or of bExpr * bExpr;; | ||
| + | |||
| + | (* categoria sintatica Com *) | ||
| + | type com = Skip | ||
| + | | Assign of ident * aExpr | ||
| + | | Seq of com * com | ||
| + | | Cond of bExpr * com * com | ||
| + | | While of bExpr * com | ||
| + | ;; | ||
| + | </code> | ||
| + | |||
| + | ===== Estados ===== | ||
| + | <code ocaml> | ||
| + | (* o estado: uma função das identificadores para números inteiros *) | ||
| + | type sigma = ident -> num;; | ||
| + | |||
| + | (* o estado inicial *) | ||
| + | let empty : sigma = function x -> 0;; | ||
| + | |||
| + | (* um estado modificado em um ponto: sigma[l -> n] *) | ||
| + | let mapsto f l n a = if a = l then n else f a;; | ||
| + | </code> | ||
| + | |||
| + | ===== Avaliação de expressões aritméticas ===== | ||
| + | <code ocaml> | ||
| + | (* avaliação das expressões aritméticas *) | ||
| + | let rec eval_aExpr (a,sigma) = | ||
| + | match a with | ||
| + | Num n -> n | ||
| + | | Ident x -> sigma x | ||
| + | | Sum (a1,a2) -> (eval_aExpr (a1,sigma))+(eval_aExpr (a2,sigma)) | ||
| + | | Diff (a1,a2) -> (eval_aExpr (a1,sigma))-(eval_aExpr (a2,sigma)) | ||
| + | | Prod (a1,a2) -> (eval_aExpr (a1,sigma))*(eval_aExpr (a2,sigma)) | ||
| + | ;; | ||
| + | </code> | ||