You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/semant.ml
+94-27Lines changed: 94 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
openAst
2
2
openSast
3
3
openUtilities
4
+
openInfer
4
5
5
6
(* Semant takes an Abstract Syntax Tree and returns a Syntactically Checked AST with partial type inferrence,
6
7
syntax checking, and other features. expr objects are converted to sexpr, and stmt objects are converted
@@ -24,10 +25,34 @@ let needs_cast t1 t2 =
24
25
This currently is quite restrictive and does not permit automatic type casting like in Python.
25
26
This may be changed in the future. The commented-out line would allow that feature *)
26
27
27
-
letbinopt1t2op=
28
+
(* Helper to check if a type is a type variable *)
29
+
letis_tvar=function
30
+
|TVar_ -> true
31
+
|_ -> false
32
+
33
+
(* Check if an operation is a comparison that returns Bool *)
34
+
letis_comparison=function
35
+
|Eq|Neq|Less|Leq|Greater|Geq -> true
36
+
|_ -> false
37
+
38
+
(* Check if an operation is a logical operation that returns Bool *)
39
+
letis_logical=function
40
+
|And|Or -> true
41
+
|_ -> false
42
+
43
+
letbinopt1t2op=
28
44
let except = (Failure ("STypeError: unsupported operand type(s) for binary "^ binop_to_string op ^": '"^ type_to_string t1 ^"' and '"^ type_to_string t2 ^"'")) in
29
45
match (t1, t2) with
30
-
| (Dyn, Dyn) | (Dyn, _) | (_, Dyn) -> Dyn
46
+
(* If either operand is Dyn, result is Dyn (or Bool for comparisons) *)
47
+
| (Dyn, Dyn) | (Dyn, _) | (_, Dyn) ->
48
+
if is_comparison op || is_logical op thenBoolelseDyn
49
+
(* Handle type variables - infer from the known operand *)
50
+
| (TVar a, TVar b) when a = b -> (* Same TVar *)
51
+
if is_comparison op || is_logical op thenBoolelseTVar a
52
+
| (TVar _, TVar _) -> Dyn(* Different TVars -> Dyn *)
53
+
| (TVar_, t) | (t, TVar _) ->
54
+
if is_comparison op || is_logical op thenBool
55
+
else t (* Infer result type from the known operand *)
31
56
|_ -> let same = t1 = t2 in (match op with
32
57
|Add|Sub|Mul|Expwhen same && t1 =Int -> Int
33
58
|Add|Sub|Mul|Div|Expwhen same && t1 =Float -> Float
@@ -165,27 +190,50 @@ and exp the_state = function
165
190
let (map'', _, _, _) = assign map' (Dyn, (SCall (e, (List.rev exprout), transforms), Dyn), data) name in(* add the function itself to the namespace *)
|_ -> raise (Failure ("SCriticalFailure: unexpected type encountered internally in Call evaluation"))) (* can be expanded to allow classes in the future *)
191
239
@@ -260,7 +308,7 @@ stack is a TypeMap containing the function call stack.
260
308
TODO distinguish between outer and inner scope return statements to stop evaluating when definitely
0 commit comments