Skip to content

Commit

Permalink
Support Rational literal
Browse files Browse the repository at this point in the history
  • Loading branch information
mame committed Aug 23, 2024
1 parent 2d9f766 commit f18c568
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/typeprof/core/ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ def self.create_node(raw_node, lenv, use_result = true)
when :false_node then FalseNode.new(raw_node, lenv)
when :integer_node then IntegerNode.new(raw_node, lenv)
when :float_node then FloatNode.new(raw_node, lenv)
when :rational_node then RationalNode.new(raw_node, lenv)
when :imaginary_node then ComplexNode.new(raw_node, lenv)
when :symbol_node then SymbolNode.new(raw_node, lenv)
when :interpolated_symbol_node then InterpolatedSymbolNode.new(raw_node, lenv)
when :string_node then StringNode.new(raw_node, lenv, raw_node.content)
Expand All @@ -210,7 +212,6 @@ def self.create_node(raw_node, lenv, use_result = true)
when :hash_node then HashNode.new(raw_node, lenv, false)
when :keyword_hash_node then HashNode.new(raw_node, lenv, true)
when :lambda_node then LambdaNode.new(raw_node, lenv)
when :imaginary_node then ImaginaryNode.new(raw_node, lenv)

# misc
when :defined_node then DefinedNode.new(raw_node, lenv)
Expand Down
22 changes: 16 additions & 6 deletions lib/typeprof/core/ast/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ def initialize(raw_node, lenv)
def install0(genv) = Source.new(genv.float_type)
end

class RationalNode < LiteralNode
def initialize(raw_node, lenv)
super(raw_node, lenv, raw_node.slice.to_r)
end

def install0(genv) = Source.new(genv.rational_type)
end

class ComplexNode < LiteralNode
def initialize(raw_node, lenv)
super(raw_node, lenv, raw_node.slice.to_c)
end

def install0(genv) = Source.new(genv.complex_type)
end

class SymbolNode < LiteralNode
def initialize(raw_node, lenv)
super(raw_node, lenv, raw_node.value.to_sym)
Expand Down Expand Up @@ -299,11 +315,5 @@ def install0(genv)
Source.new(genv.proc_type)
end
end

class ImaginaryNode < Node
def install0(genv)
Source.new(genv.complex_type)
end
end
end
end
8 changes: 5 additions & 3 deletions lib/typeprof/core/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ def initialize
@str_type = Type::Instance.new(self, resolve_cpath([:String]), [])
@int_type = Type::Instance.new(self, resolve_cpath([:Integer]), [])
@float_type = Type::Instance.new(self, resolve_cpath([:Float]), [])
@rational_type = Type::Instance.new(self, resolve_cpath([:Rational]), [])
@complex_type = Type::Instance.new(self, resolve_cpath([:Complex]), [])
@proc_type = Type::Instance.new(self, resolve_cpath([:Proc]), [])
@symbol_type = Type::Instance.new(self, resolve_cpath([:Symbol]), [])
@set_type = Type::Instance.new(self, resolve_cpath([:Set]), [])
@regexp_type = Type::Instance.new(self, resolve_cpath([:Regexp]), [])
@complex_type = Type::Instance.new(self, resolve_cpath([:Complex]), [])

@run_count = 0
end
Expand All @@ -43,8 +44,9 @@ def initialize

attr_reader :mod_object, :mod_ary, :mod_hash, :mod_range, :mod_str
attr_reader :cls_type, :mod_type
attr_reader :obj_type, :nil_type, :true_type, :false_type, :str_type, :int_type, :float_type
attr_reader :proc_type, :symbol_type, :set_type, :regexp_type, :complex_type
attr_reader :obj_type, :nil_type, :true_type, :false_type, :str_type
attr_reader :int_type, :float_type, :rational_type, :complex_type
attr_reader :proc_type, :symbol_type, :set_type, :regexp_type

def gen_ary_type(elem_vtx)
Type::Instance.new(self, @mod_ary, [elem_vtx])
Expand Down
4 changes: 2 additions & 2 deletions scenario/misc/complex.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## update
def foo
def check
1i
end

## assert
class Object
def foo: -> Complex
def check: -> Complex
end
9 changes: 9 additions & 0 deletions scenario/misc/rational.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## update
def check
1r
end

## assert
class Object
def check: -> Rational
end

0 comments on commit f18c568

Please sign in to comment.