Skip to content

Commit

Permalink
finish ameba checks
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioAriasC committed Apr 5, 2024
1 parent 04cfa9d commit c13c7a2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
6 changes: 3 additions & 3 deletions spec/evaluator_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ describe "Evaluator" do
it "function object" do
input = "fn(x) { x + 2; };"
evaluated = test_eval(input)
check_type(MFunction, evaluated) do |fn|
parameters = fn.parameters?.not_nil!
check_type(MFunction, evaluated) do |function|
parameters = function.parameters?.not_nil!
parameters.size.should eq(1)
parameters[0].to_s.should eq("x")
fn.body?.not_nil!.to_s.should eq("(x + 2)")
function.body?.not_nil!.to_s.should eq("(x + 2)")
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/benchmarks.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fibonacci(#{size});)
end

private def step(x : Int64) : Int64
if (x < 2)
if x < 2
return x
else
step(x - 1) + step(x - 2)
Expand Down
20 changes: 8 additions & 12 deletions src/evaluator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "./objects"
require "./ast"

macro error_ret(e)
if {{e}}.is_error?
if {{e}}.error?
return {{e}}
end
end
Expand Down Expand Up @@ -106,7 +106,7 @@ module Evaluator
when CallExpression
return eval(node.function?, env).if_not_error do |function|
args = eval_expressions(node.arguments?, env)
if args.size == 1 && args[0].is_error?
if args.size == 1 && args[0].error?
return args[0]
else
apply_function(function, args)
Expand Down Expand Up @@ -142,7 +142,7 @@ module Evaluator
return eval_hash_literal(node, env)
when ArrayLiteral
elements = eval_expressions(node.elements?, env)
if elements.size == 1 && elements[0].is_error?
if elements.size == 1 && elements[0].error?
return elements[0]
else
MArray.new(elements)
Expand Down Expand Up @@ -262,7 +262,7 @@ module Evaluator
private def eval_expressions(arguments : Array(Expression?)?, env : Environment) : Array(MObject?)
return arguments.not_nil!.map do |argument|
evaluated = eval(argument, env)
if evaluated.is_error?
if evaluated.error?
return [evaluated]
end
evaluated
Expand Down Expand Up @@ -298,7 +298,7 @@ module Evaluator
i = index.value
max = elements.size - 1

if (i < 0 || i > max)
if i < 0 || i > max
return NULL
end

Expand Down Expand Up @@ -392,22 +392,18 @@ module Objects
end
end

def is_error? : Bool
def error? : Bool
self.is_a?(MError)
end
end
end

struct Nil
def if_not_error(&body : Objects::MObject -> Objects::MObject?) : Objects::MObject?
def if_not_error(& : Objects::MObject -> Objects::MObject?) : Objects::MObject?
return self
end

# def is_truthy : Bool
# return true
# end

def is_error? : Bool
def error? : Bool
false
end

Expand Down
12 changes: 6 additions & 6 deletions src/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Lexers
end
end

private def read_value(&predicate : Char -> Bool) : String
private def read_value(& : Char -> Bool) : String
current_position = @position
while yield @ch
read_char
Expand All @@ -40,7 +40,7 @@ module Lexers
end

private def read_identifier : String
read_value(&.is_identifier?)
read_value(&.identifier?)
end

private def read_string : String
Expand All @@ -59,7 +59,7 @@ module Lexers
end

private def ends_with_equal(one_char : TokenType, two_chars : TokenType, duplicate_chars = true)
if (peak_char == '=')
if peak_char == '='
current_char = @ch
read_char
value = if duplicate_chars
Expand All @@ -74,7 +74,7 @@ module Lexers
end

private def skip_whitespace
while WHITE_SPACES.any? { |wp| wp == @ch }
while WHITE_SPACES.any? { |white_space| white_space == @ch }
read_char
end
end
Expand Down Expand Up @@ -123,7 +123,7 @@ module Lexers
r = Token.new(EOF, "")
else
case
when @ch.is_identifier?
when @ch.identifier?
identifier = read_identifier
return Token.new(identifier.lookup_ident, identifier)
when @ch.number?
Expand All @@ -139,7 +139,7 @@ module Lexers
end

struct Char
def is_identifier? : Bool
def identifier? : Bool
self.letter? || self == '_'
end
end

0 comments on commit c13c7a2

Please sign in to comment.