summaryrefslogtreecommitdiff
path: root/Jennisys/Jennisys/TypeChecker.fs
blob: bc696f438e8ab49e336afd62bbae25d7f24f53e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module TypeChecker

open Ast
open AstUtils
open Printer
open System.Collections.Generic

let GetClass name decls =
  match decls |> List.tryFind (function Class(n,_,_) when n = name -> true | _ -> false) with
  | Some(cl) -> cl
  | None -> Class(name,[],[])

let GetModel name decls =
  match decls |> List.tryFind (function Model(n,_,_,_,_) when n = name -> true | _ -> false) with
  | Some(m) -> m
  | None -> Model(name,[],[],[],BoolLiteral(true))

let GetCode name decls =
  match decls |> List.tryFind (function Code(n,_) when n = name -> true | _ -> false) with
  | Some(c) -> c
  | None -> Code(name,[])

let IsUserType prog tpo =
  match tpo with
  | Some(tp) ->
      let tpname = match tp with
                   | NamedType(tname,_) -> tname
                   | InstantiatedType(tname, _) -> tname
                   | _ -> ""
      match prog with
      | Program(components) -> components |> List.filter (function Component(Class(name,_,_),_,_) when name = tpname -> true
                                                                   | _                                               -> false) |> List.isEmpty |> not
  | None -> false

let TypeCheck prog =
  match prog with
  | SProgram(decls) ->
      let componentNames = decls |> List.choose (function Class(name,_,_) -> Some(name) | _ -> None)
      let clist = componentNames |> List.map (fun name -> Component(GetClass name decls, GetModel name decls, GetCode name decls))
      Some(Program(clist))

// TODO: implement this
let rec InferType prog thisComp expr = 
  let __FindVar comp fldName = 
    let var = FindVar comp fldName |> Utils.ExtractOption
    match var with
    | Var(_, tyOpt) -> 
        let c = FindComponentForType prog (Utils.ExtractOption tyOpt) |> Utils.ExtractOption
        Some(c)
  try 
    match expr with
    | ObjLiteral("this") -> Some(thisComp)
    | ObjLiteral("null") -> None
    | IdLiteral(id) -> __FindVar thisComp id
    | Dot(discr, fldName) -> 
        match InferType prog thisComp discr with
        | Some(comp) -> __FindVar comp fldName
        | None -> None
    | _ -> None
  with 
  | ex -> None