Ada: change types.smart_pointer to Mal_Handle
[jackhill/mal.git] / ada / evaluation.adb
CommitLineData
9a6f4925
CM
1with Ada.Text_IO;
2with Envs;
3with Smart_Pointers;
4
5package body Evaluation is
6
7
8 -- primitive functions on Smart_Pointer,
9 function "+" is new Types.Op ("+", "+");
10 function "-" is new Types.Op ("-", "-");
11 function "*" is new Types.Op ("*", "*");
12 function "/" is new Types.Op ("/", "/");
13
14
fbad73cb
CM
15 function Apply (Func : Types.Mal_Handle; Args : Types.List_Mal_Type)
16 return Types.Mal_Handle is
9a6f4925
CM
17 use Types;
18 begin
19--Ada.Text_IO.Put_Line ("Applying " & To_String (Deref (Func).all) & " to " & Args.To_String);
20 case Deref (Func).Sym_Type is
21 when Sym =>
22 declare
23 Sym_P : Types.Sym_Ptr;
24 begin
25 Sym_P := Types.Deref_Sym (Func);
26 case Sym_P.all.Symbol is
27 when '+' => return Reduce ("+"'Access, Args);
28 when '-' => return Reduce ("-"'Access, Args);
29 when '*' => return Reduce ("*"'Access, Args);
30 when '/' => return Reduce ("/"'Access, Args);
31 when others => null;
32 end case;
33 end;
066c5345 34 when Error => return Func;
9a6f4925
CM
35 when others => null;
36 end case;
37 return Smart_Pointers.Null_Smart_Pointer;
38 end Apply;
39
40
41 function Eval_Ast
fbad73cb
CM
42 (Ast : Types.Mal_Handle)
43 return Types.Mal_Handle is
9a6f4925
CM
44 use Types;
45 begin
46 case Deref (Ast).Sym_Type is
47 when Sym =>
066c5345
CM
48 declare
49 Sym : Mal_String (1..1) := Deref_Sym (Ast).Symbol & "";
50 begin
51 return Envs.Get (Sym);
52 exception
53 when Envs.Not_Found =>
54 return New_Error_Mal_Type ("'" & Sym & "' not found");
55 end;
56 when Atom =>
57 declare
58 Sym : Mal_String := Deref_Atom (Ast).Get_Atom;
59 begin
60 return Envs.Get (Sym);
61 exception
62 when Envs.Not_Found =>
63 return New_Error_Mal_Type ("'" & Sym & "' not found");
64 end;
9a6f4925
CM
65 when List =>
66 return Map (Eval'Access, Deref_List (Ast).all);
67 when others =>
68 return Ast;
69 end case;
70 end Eval_Ast;
71
fbad73cb
CM
72 function Eval (Param : Types.Mal_Handle)
73 return Types.Mal_Handle is
9a6f4925
CM
74 use Types;
75 begin
76--Ada.Text_IO.Put_Line ("Evaling " & Deref (Param).To_String);
77 if Deref (Param).Sym_Type = List and then
78 Deref_List (Param).all.Get_List_Type = List_List then
79 declare
80 Evaled_List : Types.List_Mal_Type;
fbad73cb 81 Func : Types.Mal_Handle;
9a6f4925
CM
82 Args : Types.List_Mal_Type;
83 begin
84 Evaled_List := Deref_List (Eval_Ast (Param)).all;
85 Func := Types.Car (Evaled_List);
86 Args := Types.Cdr (Evaled_List);
87 return Apply (Func, Args);
88 end;
89 else
90 return Eval_Ast (Param);
91 end if;
92 end Eval;
93
94
95end Evaluation;