Last active
January 2, 2016 07:19
-
-
Save raph-amiard/8269012 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with Ada.Strings.Hash; | |
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; | |
with Ada.Containers.Indefinite_Hashed_Maps; | |
with Ada.Text_IO; use Ada.Text_IO; | |
procedure Eval is | |
package String_Maps is new Ada.Containers.Indefinite_Hashed_Maps | |
(String, Integer, Hash => Ada.Strings.Hash, Equivalent_Keys => "="); | |
function "+" (S : String) return Unbounded_String renames To_Unbounded_String; | |
type Expr_Kind is (Num, Add, Mul, Var); | |
type Expr (Kind : Expr_Kind) is record | |
case Kind is | |
when Num => Val : Integer; | |
when Add | Mul => L, R : not null access Expr; | |
when Var => Name : Unbounded_String; | |
end case; | |
end record; | |
function "-" (E : Expr) return not null access Expr is (E'Unrestricted_Access); | |
function Eval (Env : String_Maps.Map; E : Expr) return Integer is | |
(case E.Kind is | |
when Num => E.Val, | |
when Add => Eval (Env, E.L.all) + Eval (Env, E.R.all), | |
when Mul => Eval (Env, E.L.all) * Eval (Env, E.R.all), | |
when Var => Env.Element (To_String (E.Name))); | |
Env : String_Maps.Map; | |
E : Expr := (Add, -(Var, +"a"), -(Mul, -(Num, 2), -(Var, +"b"))); | |
begin | |
Env.Include ("a", 3); | |
Env.Include ("b", 4); | |
Env.Include ("c", 5); | |
Put_Line ("EVAL RESULT IS : " & Eval (Env, E)'Img); | |
end Eval; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment