]> git.mdlowis.com Git - proto/sclpl-rb.git/commitdiff
Added support for float literals
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 8 Jul 2020 18:36:50 +0000 (14:36 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 8 Jul 2020 18:36:50 +0000 (14:36 -0400)
lib/dyn.rb
spec/type_checker_spec.rb

index bb9791a83ed0d794c967772de165159bb9ca4c06..dbeab155722c43031c5de294f7267aea98bacc06 100755 (executable)
@@ -96,7 +96,7 @@ class Lexer
   BRACES = /[\(\)\[\]\{\}\.]/
   OPERATORS = /[:,<>*\/=+\-\$?!]+/
   INTEGER = /[0-9]+/
-  FLOATING = /[0-9]+(\.[0-9]+)?/
+  FLOATING = /[0-9]+\.[0-9]+/
   STRING = /"(\\"|[^"])*"/
   ID_TYPES = {
     "true"  => :bool,
@@ -134,8 +134,8 @@ class Lexer
       type = :eof
       if @data.scan(IDENT)
         type = get_id_type(@data.matched)
-#      elsif @data.scan(FLOATING)
-#        type = :float
+      elsif @data.scan(FLOATING)
+        type = :float
       elsif @data.scan(INTEGER)
         type = :int
       elsif @data.scan(STRING)
@@ -217,6 +217,7 @@ class Parser
     :if     => { prefix: :if_expr,    infix: nil,         level: :none    },
     :ident  => { prefix: :variable,   infix: nil,         level: :none    },
     :bool   => { prefix: :constant,   infix: nil,         level: :none    },
+    :float  => { prefix: :constant,   infix: nil,         level: :none    },
     :int    => { prefix: :constant,   infix: nil,         level: :none    },
     :string => { prefix: :constant,   infix: nil,         level: :none    },
     :char   => { prefix: :constant,   infix: nil,         level: :none    },
@@ -333,7 +334,9 @@ class Parser
 
   def constant()
     if (@prev.type == :int)
-      Val.new(location(), :int, @prev.text.to_f)
+      Val.new(location(), :int, @prev.text.to_i)
+    elsif (@prev.type == :float)
+      Val.new(location(), :float, @prev.text.to_f)
     elsif (@prev.type == :string)
       Val.new(location(), :string, @prev.text)
     elsif (@prev.type == :bool)
index e40152c8b3b27eaeceee6b89956ffbbc0e46d772..9d11abb5b35b5d2b064544e12243426657348623 100644 (file)
@@ -7,13 +7,19 @@ describe TypeChecker do
     it "will recognize 'true' as a bool" do
       expect(parse_and_check("true")).to eq :bool
     end
+
     it "will recognize 'false' as a bool" do
       expect(parse_and_check("false")).to eq :bool
     end
+
     it "will recognize '123' as an int" do
       expect(parse_and_check("123")).to eq :int
     end
-    it "will recognize '123.0' as a float"
+
+    it "will recognize '123.0' as a float" do
+      expect(parse_and_check("123.0")).to eq :float
+    end
+
     it "will recognize '\"abc\"' as a string" do
       expect(parse_and_check("\"abc\"")).to eq :string
     end