Class: TypedRb::Language

Inherits:
Object show all
Includes:
Model, Types
Defined in:
lib/typed/language.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#unification_resultObject (readonly)

Returns the value of attribute unification_result.



9
10
11
# File 'lib/typed/language.rb', line 9

def unification_result
  @unification_result
end

Instance Method Details

#all_errors(top_level_errors, errors) ⇒ Object



162
163
164
# File 'lib/typed/language.rb', line 162

def all_errors(top_level_errors, errors)
  (top_level_errors||[]) + ((errors||{}).values.reduce(&:concat) || [])
end

#check(expr) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/typed/language.rb', line 11

def check(expr)
  restore_prelude
  $TYPECHECK = true
  eval(expr, TOPLEVEL_BINDING)
  $TYPECHECK = false
  TypedRb.log(binding, :debug, 'Normalize top level')
  ::BasicObject::TypeRegistry.normalize_types!
  TypingContext.clear(:top_level)
  check_result = check_type(parse(expr))
  ::BasicObject::TypeRegistry.check_super_type_annotations
  @unification_result = run_unification
  check_result
end

#check_errors(total_errors) ⇒ Object



166
167
168
# File 'lib/typed/language.rb', line 166

def check_errors(total_errors)
  raise total_errors.first if total_errors.count > 0
end

#check_file(path, raise_errors = false) ⇒ Object



170
171
172
# File 'lib/typed/language.rb', line 170

def check_file(path, raise_errors = false)
  check_files([path], raise_errors)
end

#check_files(files, raise_errors = false) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/typed/language.rb', line 71

def check_files(files, raise_errors = false)
  ::BasicObject::TypeRegistry.clear
  $TYPECHECK = true
  prelude_path = File.join(File.dirname(__FILE__), 'prelude.rb')
  load prelude_path
  Kernel.reset_dependencies
  Kernel.with_dependency_tracking do
    files.each { |file| load file if file != prelude_path }
  end
  ordered_files = Kernel.computed_dependencies.select do |file|
    files.include?(file)
  end
  ordered_files += files.select { |file| !ordered_files.include?(file) }
  $TYPECHECK = false
  TypedRb.log(binding, :debug, 'Normalize top level')
  ::BasicObject::TypeRegistry.normalize_types!
  TypingContext.clear(:top_level)
  check_result = nil
  errors = {}
  errors_accum = {}
  ordered_files.each do |file|
    $TYPECHECK_FILE = file
    expr = File.open(file, 'r').read
    begin
      check_result = check_type(parse(expr))
      print '.'.green
    rescue TypedRb::Types::UncomparableTypes, TypedRb::TypeCheckError => e
      print 'E'.red
      hash = e.to_s.hash
      unless errors_accum[hash]
        errors_accum[hash] = true
        errors_for_file = errors[file] || []
        errors_for_file << e
        errors[file] = errors_for_file
      end
    end
  end

  top_level_errors = []
  begin
    ::BasicObject::TypeRegistry.check_super_type_annotations
    @unification_result = run_unification
  rescue TypedRb::Types::UncomparableTypes, TypedRb::TypeCheckError => e
    hash = e.to_s.hash
    unless errors_accum[hash]
      errors_accum[hash] = true
      print 'E'.red
      top_level_errors << e
    end
  end
  puts "\n"
  total_errors = all_errors(top_level_errors, errors)
  dynamic_warnings = TypedRb.dynamic_warnings
  if total_errors.count > 0
    puts "\nErrors:"
    report_errors(top_level_errors, errors, {})
  end
  if dynamic_warnings.count > 0
    puts "\nWarnings:"
    report_errors([], {}, dynamic_warnings)
  end
  puts "\nProcessed #{files.size} files, #{total_errors.count} errors, #{dynamic_warnings.count} warnings\n"
  check_errors(total_errors) if raise_errors
  check_result
end

#check_type(expr) ⇒ Object



180
181
182
# File 'lib/typed/language.rb', line 180

def check_type(expr)
  expr.check_type(TypingContext.top_level)
end

#gen_bin_preludeObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/typed/language.rb', line 25

def gen_bin_prelude
  File.open(File.join(File.dirname(__FILE__), 'prelude_registry.bin'), 'w') do |f|
    f.write(Marshal.dump(::BasicObject::TypeRegistry.send(:registry)))
  end
  File.open(File.join(File.dirname(__FILE__), 'prelude_generic_registry.bin'), 'w') do |f|
    f.write(Marshal.dump(::BasicObject::TypeRegistry.send(:generic_types_registry)))
  end
  File.open(File.join(File.dirname(__FILE__), 'prelude_existential_registry.bin'), 'w') do |f|
    f.write(Marshal.dump(::BasicObject::TypeRegistry.send(:existential_types_registry)))
  end
end

#load_bin_preludeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/typed/language.rb', line 37

def load_bin_prelude
  old_value = $TYPECHECK
  $TYPECHECK = false
  require_relative('./prelude')
  $TYPECHECK = old_value
  ::BasicObject::TypeRegistry.clear
  File.open(File.join(File.dirname(__FILE__), 'prelude_registry.bin'), 'r') do |f|
    ::BasicObject::TypeRegistry.registry =  Marshal.load(f.read)
  end
  File.open(File.join(File.dirname(__FILE__), 'prelude_generic_registry.bin'), 'r') do |f|
    ::BasicObject::TypeRegistry.generic_types_registry = Marshal.load(f.read)
  end
  File.open(File.join(File.dirname(__FILE__), 'prelude_existential_registry.bin'), 'r') do |f|
    ::BasicObject::TypeRegistry.existential_types_registry = Marshal.load(f.read)
  end
  ::BasicObject::TypeRegistry.clear_parsing_registries
  true
rescue
  false
end

#parse(expr) ⇒ Object



174
175
176
177
178
# File 'lib/typed/language.rb', line 174

def parse(expr)
  Model::GenSym.reset
  parser = AstParser.new
  parser.parse(expr)
end

#report_errors(top_level_errors, errors, warnings) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/typed/language.rb', line 137

def report_errors(top_level_errors, errors, warnings)
  files = (errors.keys||[]) + (warnings.keys||[])
  files.each do |file|
    errors_for_file = errors[file] || []
    warnings_for_file = warnings[file] || []
    errors_for_file.each do |error|
      puts "\n"
      puts error.message.red
    end
    warnings_accum= {}
    warnings_for_file.each do |warning|
      hash = warning.to_s.hash
      unless warnings_accum[hash]
        warnings_accum[hash] = true
        puts "\n"
        puts warning.message.yellow
      end
    end
  end
  top_level_errors.each do |error|
    puts "\n"
    puts error.message.red
  end
end

#restore_preludeObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/typed/language.rb', line 58

def restore_prelude
  unless load_bin_prelude
    ::BasicObject::TypeRegistry.clear
    $TYPECHECK = true
    load File.join(File.dirname(__FILE__), 'prelude.rb')
    $TYPECHECK = false
    ::BasicObject::TypeRegistry.normalize_types!
    gen_bin_prelude
    TypingContext.clear(:top_level)
    ::BasicObject::TypeRegistry.clear_parsing_registries
  end
end

#run_unificationObject



184
185
186
187
188
# File 'lib/typed/language.rb', line 184

def run_unification
  constraints = Types::TypingContext.all_constraints
  unif = Types::Polymorphism::Unification.new(constraints)
  unif.run(true)
end

#type_variablesObject



190
191
192
# File 'lib/typed/language.rb', line 190

def type_variables
  TypingContext.all_variables
end