Class: SFRP::Mono::MatchExp

Inherits:
Exp
  • Object
show all
Defined in:
lib/sfrp/mono/expression.rb

Defined Under Namespace

Classes: Case

Instance Attribute Summary

Attributes inherited from Exp

#type_str

Instance Method Summary collapse

Methods inherited from Exp

#==

Constructor Details

#initialize(type_str, left_exp, cases, id = nil) ⇒ MatchExp

Returns a new instance of MatchExp.



14
15
16
17
18
19
# File 'lib/sfrp/mono/expression.rb', line 14

def initialize(type_str, left_exp, cases, id = nil)
  @type_str = type_str
  @left_exp = left_exp
  @cases = cases
  @id = id
end

Instance Method Details

#check_completeness(set) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/sfrp/mono/expression.rb', line 42

def check_completeness(set)
  set.type(@left_exp.type_str).all_pattern_examples(set).each do |exam|
    unless @cases.any? { |c| c.pattern.accept?(exam) }
      raise IncompleteMatchExpError.new
    end
  end
end

#compObject



21
22
23
# File 'lib/sfrp/mono/expression.rb', line 21

def comp
  [@type_str, @left_exp, @cases]
end

#memory(set) ⇒ Object



50
51
52
53
# File 'lib/sfrp/mono/expression.rb', line 50

def memory(set)
  m = @cases.map { |c| c.exp.memory(set) }.reduce { |a, b| a.or(b) }
  @left_exp.memory(set).and(m)
end

#to_low(set, env) ⇒ Object

Note that the expression this returns is not wrapped by ().



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sfrp/mono/expression.rb', line 26

def to_low(set, env)
  check_completeness(set)
  tmp_var_str = env.new_var(@left_exp.type_str)
  left_let_exp = "#{tmp_var_str} = #{@left_exp.to_low(set, env)}"
  case_exp = L.if_chain_exp do |i|
    @cases.each do |c|
      cond_exps = c.pattern.low_cond_exps(set, tmp_var_str)
      let_exps = c.pattern.low_let_exps(set, tmp_var_str, env)
      exp = (let_exps + [c.exp.to_low(set, env)]).join(', ')
      i.finish(exp) if cond_exps.empty?
      i.append_case(cond_exps.join(' && '), exp)
    end
  end
  "#{left_let_exp}, #{case_exp}"
end