Small imperative language translator

Hi, I am writing an interpreter of C-like, a statically typed language in Haskell. I want to do type checking before executing the code, but I have some problems with it. First of all, below are some type definitions from my abstract syntax:

newtype Ident = Ident String deriving (Eq,Ord,Show)
data Exp = {-- some other value constructors --} | EFuncWithParams Ident [Exp]
data Type = TInt | TDouble | {-- some other value constructors --} | TFunction [Exp]
type TCM a = ErrorT String (Reader Env) a

TCM is designed to report errors and transfer media, for example:

typeof (EVar v) = do
env <- ask
case M.lookup v env of
    Nothing -> throwError $ "undefined variable" ++ v ++ "\n"
    Just t - > return t

Now I want to check the type of expressions, so I have a function that performs checks:

typeof Exp :: Exp -> TCM Type

It is defined for all cases, but one:

typeof (EFuncWithParams f l)

. , f ( , , , ) , , f , . , haskell , . :)

EDIT: , , , , EFuncWithParams Ident [Exp] - (, , ), f (2 + 3, a, b [0]), TFunction [Exp]. - :

data Function_def =
   Func Type_specifier Declarator Compound_stm
   deriving (Eq,Ord,Show)

Declarator:

data Declarator =  FuncDec Ident Parameter_declarations

- _

, , , - , , . , :

typeof_stm :: Stm -> TCM Type -- Function_def is a statement

, , , , (, typeof_stm), (, typeof). , , , , .

+3
3

, . TFunction [Exp], TFunction [Type] Type ( ).

:

case ... of ...
  EFuncWithParams ident args -> do
     t <- typeof (EVar ident)
     ts <- mapM typeof args
     case t of
         TFunction ps r -> if ts == ps
                               then return r
                               else throwError $ "parameter types do not match"
         _ -> throwError $ "called id " ++ ident ++ " which is not a function"

, , , , , , , . . , , ( ).

+3

Haskell, OCaml ++, , , - , .

, , -

FunCall ident, exp list

, , :

  • ident
  • ( , . )
  • , typeof (exp1), , TCM Type

. OCaml ( Haskell) - :

match exp with
    | FunCall ident, (param list) ->
      (* get fundecl from ident *)
      (* call check_params list_of_parameters, list_of_type_of_parameters *)
      (* if check params return true then type check value of the function is the return value *)


let check_params list decl_list =
  match list, decl_list with
    | [], [] -> true
    | p :: rp, d :: rd -> typeof(p) = d && check_params rp rd
    | _ -> false
+1
EFuncWithParams Ident [Exp]

, , , , . ,

EFuncWithparams { inType, outType :: Type
                , funcInput :: Ident
                , funcBody :: [Expr] }

, , :

  • funcInput inType .
  • funcBody
  • , outType.

, , inType, outType.

0

All Articles