Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
Add DFGOperator.evaluate method
Browse files Browse the repository at this point in the history
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
Burgos committed Jan 31, 2019
1 parent e7ec848 commit c4704d1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
10 changes: 10 additions & 0 deletions spec/dfgoperator_spec.cr
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
40 changes: 38 additions & 2 deletions src/dfgoperator.cr
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

0 comments on commit c4704d1

Please sign in to comment.