From: Michael D. Lowis Date: Wed, 8 Jul 2020 18:36:50 +0000 (-0400) Subject: Added support for float literals X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=522c7ad2f0ecd5c967507e51f06aa57f8983752a;p=proto%2Fsclpl-rb.git Added support for float literals --- diff --git a/lib/dyn.rb b/lib/dyn.rb index bb9791a..dbeab15 100755 --- a/lib/dyn.rb +++ b/lib/dyn.rb @@ -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) diff --git a/spec/type_checker_spec.rb b/spec/type_checker_spec.rb index e40152c..9d11abb 100644 --- a/spec/type_checker_spec.rb +++ b/spec/type_checker_spec.rb @@ -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