This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds the evaluate method to operators. Operators are objects that encapsulate the mathematical operation which appear as a result of parsing. During the transformation stage, these operations are evaluated calling the `evaluate` method.
- Loading branch information
Showing
2 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "spec" | ||
require "../src/dfg.cr" | ||
require "../src/dfgoperator.cr" | ||
|
||
describe Isekai do | ||
op : DFGOperator = OperatorAdd.new() | ||
op.evaluate(1, 2).should eq 3 | ||
op = OperatorMul.new() | ||
op.evaluate(1, 2).should eq 2 | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,76 @@ | ||
# Operators on DFGExpressions. Wraps the operation in a type-safe manner. | ||
class DFGOperator | ||
abstract class DFGOperator | ||
abstract def evaluate (left : T, right : T) forall T | ||
end | ||
|
||
class LeftShiftOp < DFGOperator | ||
def initialize (@bitwidth : Int32) | ||
end | ||
|
||
def evaluate (left : T, right : T) forall T | ||
return left << right | ||
end | ||
end | ||
|
||
class RightShiftOp < DFGOperator | ||
def initialize (@bitwidth : Int32) | ||
end | ||
|
||
def evaluate (left : T, right : T) forall T | ||
return left >> right | ||
end | ||
end | ||
|
||
class LogicalAndOp < DFGOperator | ||
def evaluate (left : T, right : T) forall T | ||
return left && right | ||
end | ||
end | ||
|
||
class OperatorAdd < DFGOperator | ||
def evaluate (left : T, right : T) forall T | ||
return left + right | ||
end | ||
end | ||
|
||
class OperatorSub < DFGOperator | ||
def evaluate (left : T, right : T) forall T | ||
return left - right | ||
end | ||
end | ||
|
||
class OperatorMul < DFGOperator | ||
def evaluate (left : T, right : T) forall T | ||
return left * right | ||
end | ||
end | ||
|
||
class OperatorDiv < DFGOperator | ||
def evaluate (left : T, right : T) forall T | ||
return left / right | ||
end | ||
end | ||
|
||
class OperatorMod < DFGOperator | ||
def evaluate (left : T, right : T) forall T | ||
return left % right | ||
end | ||
end | ||
|
||
class OperatorXor < DFGOperator | ||
def evaluate (left : T, right : T) forall T | ||
return left ^ right | ||
end | ||
end | ||
|
||
class OperatorBor < DFGOperator | ||
def evaluate (left : T, right : T) forall T | ||
return left | right | ||
end | ||
end | ||
|
||
class OperatorBAnd < DFGOperator | ||
end | ||
def evaluate (left : T, right : T) forall T | ||
return left & right | ||
end | ||
end |