Skip to content

Commit

Permalink
partial update
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioAriasC committed Apr 5, 2024
1 parent 4644690 commit 04cfa9d
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
5 changes: 4 additions & 1 deletion .ameba.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ Lint/NotNil:
Enabled: false

Lint/DebugCalls:
Enabled: false
Enabled: false

Documentation/DocumentationAdmonition:
Enabled: false
2 changes: 1 addition & 1 deletion src/code.cr
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ module Code

private def read_u8(ins : MBytes) : UInt8
int = read(ins, 0)
if (int < 0)
if int < 0
raise "error reading byte"
else
return int.to_u8
Expand Down
16 changes: 8 additions & 8 deletions src/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ module Compilers
getter scope_index : Int32 = 0

def initialize(@constants = [] of MObject, @symbol_table = SymbolTable.new)
Objects::BUILTINS.each_with_index do |t, i|
@symbol_table.define_builtin(i, t[0]) # name
Objects::BUILTINS.each_with_index do |builtin, i|
@symbol_table.define_builtin(i, builtin[0]) # name
end
end

Expand Down Expand Up @@ -108,7 +108,7 @@ module Compilers
compile(node.condition?.not_nil!)
jump_not_truthy_pos = emit(Opcode::OpJumpNotTruthy, 9999)
compile(node.consequence?.not_nil!)
if is_last_instruction_pop?
if last_instruction_pop?
remove_last_pop
end
jump_pos = emit(Opcode::OpJump, 9999)
Expand All @@ -118,7 +118,7 @@ module Compilers
emit(Opcode::OpNull)
else
compile(node.alternative?.not_nil!)
if is_last_instruction_pop?
if last_instruction_pop?
remove_last_pop
end
end
Expand Down Expand Up @@ -157,15 +157,15 @@ module Compilers
emit(Opcode::OpIndex)
when FunctionLiteral
enter_scope
if (!node.name.empty?)
if !node.name.empty?
@symbol_table.define_function_name(node.name)
end
parameters = node.parameters?
if !parameters.nil?
parameters.each { |parameter| @symbol_table.define(parameter.value) }
end
compile(node.body?.not_nil!)
if is_last_instruction_pop?
if last_instruction_pop?
replace_last_pop_with_return
end
if !last_instruction_is?(Opcode::OpReturnValue)
Expand All @@ -174,7 +174,7 @@ module Compilers
free_symbols = @symbol_table.free_symbols
num_locals = @symbol_table.num_definitions
instructions = leave_scope
free_symbols.each { |s| load_symbol(s) }
free_symbols.each { |free| load_symbol(free) }

compiled_fn = Objects::MCompiledFunction.new(instructions: instructions, num_locals: num_locals, num_parameters: node.parameters?.not_nil!.size)
emit(Opcode::OpClosure, add_constant(compiled_fn), free_symbols.size)
Expand Down Expand Up @@ -274,7 +274,7 @@ module Compilers
return @constants.size - 1
end

private def is_last_instruction_pop? : Bool
private def last_instruction_pop? : Bool
return last_instruction_is?(Opcode::OpPop)
end

Expand Down
12 changes: 6 additions & 6 deletions src/evaluator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ module Evaluator

private def eval_if_expression(if_expression : IfExpression, env : Environment) : MObject?
return eval(if_expression.condition?, env).if_not_error do |condition|
if condition.is_truthy?
if condition.truthy?
eval(if_expression.consequence?, env)
elsif !if_expression.alternative?.nil?
eval(if_expression.alternative?, env)
Expand Down Expand Up @@ -379,7 +379,7 @@ end

module Objects
abstract class MObject
def is_truthy? : Bool
def truthy? : Bool
case self
when Evaluator::NULL
return false
Expand All @@ -403,9 +403,9 @@ struct Nil
return self
end

def is_truthy : Bool
return true
end
# def is_truthy : Bool
# return true
# end

def is_error? : Bool
false
Expand All @@ -415,7 +415,7 @@ struct Nil
"nil"
end

def is_truthy? : Bool
def truthy? : Bool
false
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/monyet.cr
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ module Monyet
end
end

option_parser = OptionParser.parse do |parser|
OptionParser.parse do |parser|
parser.on "-v", "--version", "Show version" do
p VERSION
exit
Expand Down
6 changes: 3 additions & 3 deletions src/objects.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module Objects
yield self
end

def is_truthy? : Bool
def truthy? : Bool
true
end
end
Expand Down Expand Up @@ -155,7 +155,7 @@ module Objects
return "null"
end

def is_truthy? : Bool
def truthy? : Bool
false
end
end
Expand All @@ -181,7 +181,7 @@ module Objects
return HashKey.new(hash_type, (@value ? 1 : 0).to_u64)
end

def is_truthy? : Bool
def truthy? : Bool
value
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ module Parsers
next_token
value = parse_expression(Precedence::Lowest)
pairs[key.not_nil!] = value.not_nil!
if (!peek_token_is?(RBRACE) && !expect_peek?(COMMA))
if !peek_token_is?(RBRACE) && !expect_peek?(COMMA)
return nil
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/vm.cr
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module Vm
pos = Code.read_int(ins, ip + 1)
current_frame.ip &+= 2
condition = pop
if !condition.is_truthy?
if !condition.truthy?
current_frame.ip = pos - 1
end
when Opcode::OpNull
Expand Down

0 comments on commit 04cfa9d

Please sign in to comment.