Skip to content

Commit

Permalink
Basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
timmyjose committed May 20, 2020
1 parent acb062f commit 8c0fff0
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
**/*.bexch
**/*__*.adb
**/*__*.ads
**/*.*~
**/*.*~
**/bin/*
**/*.swp
27 changes: 27 additions & 0 deletions eval.gpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
project Eval is
type Mode_Type is ("debug", "release");
Mode: Mode_Type := external ("mode", "release");

for Languages use ("Ada");
for Source_Dirs use ("src");
for Object_Dir use "obj/" & Mode;
for Exec_Dir use "bin/" & Mode;
for Main use ("src/eval_main.adb");

package Compiler is
case Mode is
when "debug" =>
for Switches ("Ada")
use ("-g", "-gnatwa");
when "release" =>
for Switches ("Ada")
use ("-O2", "-gnatwa");
end case;
end Compiler;

package Binder is
end Binder;

package Linker is
end Linker;
end Eval;
41 changes: 41 additions & 0 deletions src/eval.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
with Ada.Text_IO;

package body Eval is
procedure Show (E : in Expr) is
begin
case E.Kind is
when Add =>
Ada.Text_IO.Put ("Add(");
when Sub =>
Ada.Text_IO.Put ("Sub(");
when Mul =>
Ada.Text_IO.Put ("Mul(");
when Div =>
Ada.Text_IO.Put ("Div(");
when Val =>
Ada.Text_IO.Put (Image (E.Val));
return;
end case;

Show (E.Left.all);
Ada.Text_IO.Put (", ");
Show (E.Right.all);
Ada.Text_IO.Put (")");
end Show;

function Eval (E : in Expr) return T is
begin
case E.Kind is
when Val =>
return E.Val;
when Add =>
return Eval (E.Left.all) + Eval (E.Right.all);
when Sub =>
return Eval (E.Left.all) - Eval (E.Right.all);
when Mul =>
return Eval (E.Left.all) * Eval (E.Right.all);
when Div =>
return Eval (E.Left.all) / Eval (E.Right.all);
end case;
end Eval;
end Eval;
26 changes: 26 additions & 0 deletions src/eval.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
generic
type T is private;
with function Image (Item : T) return String;
with function "=" (First_Item, Second_Item : T) return Boolean;
with function "+" (First_Item, Second_Item : T) return T;
with function "-" (First_Item, Second_Item : T) return T;
with function "*" (First_Item, Second_Item : T) return T;
with function "/" (First_Item, Second_Item : T) return T;
package Eval is
type Expr;
type Expr_Access is access Expr;

type Expr_Type is (Add, Sub, Mul, Div, Val);

type Expr (Kind : Expr_Type) is record
case Kind is
when Val =>
Val : T;
when others =>
Left, Right : Expr_Access;
end case;
end record;

procedure Show (E : in Expr);
function Eval (E : in Expr) return T;
end Eval;
63 changes: 63 additions & 0 deletions src/eval_main.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
with Ada.Text_IO;
with Ada.Float_Text_IO;

with Eval;

procedure Eval_Main is
package Eval_Integer is new Eval (T => Integer, Image => Integer'Image,
"=" => "=", "+" => "+", "-" => "-", "*" => "*", "/" => "/");

package Eval_Float is new Eval (T => Float, Image => Float'Image,
"=" => "=", "+" => "+", "-" => "-", "*" => "*", "/" => "/");

-- 2 + 3 * 4
E1 : Eval_Integer.Expr :=
(Kind => Eval_Integer.Add,
Left => new Eval_Integer.Expr'(Kind => Eval_Integer.Val, Val => 2),
Right =>
new Eval_Integer.Expr'
(Kind => Eval_Integer.Mul,
Left => new Eval_Integer.Expr'(Kind => Eval_Integer.Val, Val => 3),
Right =>
new Eval_Integer.Expr'(Kind => Eval_Integer.Val, Val => 4)));

-- 10 / 2 - 3
E2 : Eval_Integer.Expr :=
(Kind => Eval_Integer.Sub,
Left =>
new Eval_Integer.Expr'
(Kind => Eval_Integer.Div,
Left => new Eval_Integer.Expr'(Kind => Eval_Integer.Val, Val => 10),
Right =>
new Eval_Integer.Expr'(Kind => Eval_Integer.Val, Val => 2)),
Right => new Eval_Integer.Expr'(Kind => Eval_Integer.Val, Val => 3));

-- 2.3 * 4.5 + 1.2
E3 : Eval_Float.Expr :=
(Kind => Eval_Float.Add,
Left =>
new Eval_Float.Expr'
(Kind => Eval_Float.Mul,
Left => new Eval_Float.Expr'(Kind => Eval_Float.Val, Val => 2.3),
Right => new Eval_Float.Expr'(Kind => Eval_Float.Val, Val => 4.5)),
Right => new Eval_Float.Expr'(Kind => Eval_Float.Val, Val => 1.2));
begin
Ada.Text_IO.Put ("The expression E1 is : ");
Eval_Integer.Show (E1);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put
("And its value is " & Integer'Image (Eval_Integer.Eval (E1)));
Ada.Text_IO.New_Line;

Ada.Text_IO.Put ("The expression E2 is : ");
Eval_Integer.Show (E2);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put
("And its value is " & Integer'Image (Eval_Integer.Eval (E2)));

Ada.Text_IO.Put ("The expression E3 is : ");
Eval_Float.Show (E3);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put ("And its value is ");
Ada.Float_Text_IO.Put (Eval_Float.Eval (E3), Aft => 3, Exp => 0);
end Eval_Main;

0 comments on commit 8c0fff0

Please sign in to comment.