Skip to content

Commit

Permalink
Merge pull request #2263 from herwinw/prism_1_0_0
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m authored Oct 12, 2024
2 parents 0b7b298 + a6d65f4 commit f0b5bd6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 40 deletions.
2 changes: 1 addition & 1 deletion ext/prism
Submodule prism updated 1261 files
2 changes: 1 addition & 1 deletion lib/natalie/compiler/arity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(args, is_proc:)
).compact
when ::Prism::NumberedParametersNode
@args = args.maximum.times.map do |i|
Prism::RequiredParameterNode.new(nil, nil, :"_#{i + 1}", args.location)
Prism::RequiredParameterNode.new(nil, nil, args.location, 0, :"_#{i + 1}")
end
else
raise "expected args node, but got: #{args.inspect}"
Expand Down
23 changes: 11 additions & 12 deletions lib/natalie/compiler/macro_expander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ def macro_eval(expr:, current_path:, locals:, **)
node = Prism::StringNode.new(
nil,
nil,
node.location,
0,
node.opening_loc,
node.opening_loc,
node.closing_loc,
node.parts.map(&:unescaped).join,
node.location,
)
end
if node.type == :string_node
Expand All @@ -182,11 +183,11 @@ def macro_eval(expr:, current_path:, locals:, **)
end

def macro_nat_ignore_require(expr:, current_path:) # rubocop:disable Lint/UnusedMethodArgument
false_node # Script has not been loaded
Prism.false_node(location: nil) # Script has not been loaded
end

def macro_nat_ignore_require_relative(expr:, current_path:) # rubocop:disable Lint/UnusedMethodArgument
false_node # Script has not been loaded
Prism.false_node(location: nil) # Script has not been loaded
end

def macro_include_str!(expr:, current_path:, **)
Expand Down Expand Up @@ -216,7 +217,7 @@ def macro_update_load_path(expr:, current_path:, depth:, **)
end

path_to_add = VM.compile_and_run(
::Prism::StatementsNode.new(nil, expr.arguments&.arguments, location(expr)),
::Prism::StatementsNode.new(nil, nil, location(expr), 0, expr.arguments&.arguments),
path: current_path
)

Expand Down Expand Up @@ -264,7 +265,7 @@ def load_file(path, require_once:, location:)

def load_cpp_file(path, require_once:, location:)
name = File.split(path).last.split('.').first
return false_node if @compiler_context[:required_cpp_files][path]
return Prism.false_node(location: nil) if @compiler_context[:required_cpp_files][path]
@compiler_context[:required_cpp_files][path] = name
cpp_source = File.read(path)
init_function = "Value init_#{name}(Env *env, Value self)"
Expand All @@ -274,6 +275,9 @@ def load_cpp_file(path, require_once:, location:)
end
::Prism::StatementsNode.new(
nil,
nil,
location,
0,
[
Prism.call_node(
receiver: nil,
Expand All @@ -284,8 +288,7 @@ def load_cpp_file(path, require_once:, location:)
location: location
),
::Prism.true_node(location: location)
],
location
]
)
end

Expand All @@ -306,12 +309,8 @@ def drop_load_error(message, location:)
drop_error(:LoadError, message, print_warning: @log_load_error, location: location)
end

def false_node
::Prism::FalseNode.new(nil, nil)
end

def nothing(expr)
::Prism::StatementsNode.new(nil, [], location(expr))
::Prism::StatementsNode.new(nil, nil, location(expr), 0, [])
end

def location(expr)
Expand Down
14 changes: 7 additions & 7 deletions lib/natalie/compiler/pass1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -796,10 +796,10 @@ def transform_case_node(node, used:)
end
end

instructions << if node.consequent.nil?
instructions << if node.else_clause.nil?
PushNilInstruction.new
else
transform_expression(node.consequent, used: true)
transform_expression(node.else_clause, used: true)
end

instructions << [EndInstruction.new(:if)] * node.conditions.length
Expand Down Expand Up @@ -1231,7 +1231,7 @@ def transform_defn_args(node, used:, for_block: false, check_args: true, local_o
).compact
when Prism::NumberedParametersNode
node.maximum.times.map do |i|
Prism::RequiredParameterNode.new(nil, nil, :"_#{i + 1}", node.location)
Prism::RequiredParameterNode.new(nil, nil, node.location, 0, :"_#{i + 1}")
end
else
[node]
Expand Down Expand Up @@ -1475,7 +1475,7 @@ def transform_hash_node(node, used:)

def transform_if_node(node, used:)
true_body = node.statements || Prism.nil_node(location: node.location)
false_body = node.consequent || Prism.nil_node(location: node.location)
false_body = node.subsequent || Prism.nil_node(location: node.location)
true_instructions = transform_expression(true_body, used: true)
false_instructions = transform_expression(false_body, used: true)
instructions = [
Expand Down Expand Up @@ -2353,8 +2353,8 @@ def transform_rescue_node(node, used:)
)

instructions << ElseInstruction.new(:if)
if node.consequent
instructions += transform_expression(node.consequent, used: true)
if node.subsequent
instructions += transform_expression(node.subsequent, used: true)
else
instructions += [
PushSelfInstruction.new,
Expand Down Expand Up @@ -2572,7 +2572,7 @@ def transform_undef_node(node, used:)

def transform_unless_node(node, used:)
true_body = node.statements || Prism.nil_node(location: node.location)
false_body = node.consequent || Prism.nil_node(location: node.location)
false_body = node.else_clause || Prism.nil_node(location: node.location)
true_instructions = transform_expression(true_body, used: true)
false_instructions = transform_expression(false_body, used: true)
instructions = [
Expand Down
28 changes: 9 additions & 19 deletions lib/natalie/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,43 @@
module Prism
# Create an ArrayNode with the optionally given elements and location.
def self.array_node(location:, elements: [])
ArrayNode.new(nil, 0, elements, nil, nil, location)
ArrayNode.new(nil, nil, location, 0, elements, nil, nil)
end

# Create a CallNode with the optionally given values.
def self.call_node(receiver:, name:, location:, arguments: [], block: nil, flags: 0)
arguments = ArgumentsNode.new(nil, 0, arguments, location)
CallNode.new(nil, flags, receiver, nil, name, nil, nil, arguments, nil, block, location)
end

# Create a ClassVariableWriteNode with the optionally given values.
def self.class_variable_write_node(name:, location:, value: nil)
ClassVariableWriteNode.new(nil, name, nil, value, nil, location)
arguments = ArgumentsNode.new(nil, nil, location, 0, arguments)
CallNode.new(nil, nil, location, flags, receiver, nil, name, nil, nil, arguments, nil, block)
end

# Create a ConstantReadNode with the optionally given values.
def self.constant_read_node(name:, location:)
ConstantReadNode.new(nil, name, location)
ConstantReadNode.new(nil, nil, location, 0, name)
end

# Create a FalseNode with the optionally given location.
def self.false_node(location:)
FalseNode.new(nil, location)
end

# Create an LocalVariableWriteNode with the optionally given location.
def self.local_variable_write_node(name:, value:, location:)
LocalVariableWriteNode.new(nil, name, 0, nil, value, nil, location)
FalseNode.new(nil, nil, location, 0)
end

# Create a NilNode with the optionally given location.
def self.nil_node(location:)
NilNode.new(nil, location)
NilNode.new(nil, nil, location, 0)
end

# Create an OrNode with the optionally given left, right, and location.
def self.or_node(location:, left: nil, right: nil)
OrNode.new(nil, left, right, nil, location)
OrNode.new(nil, nil, location, 0, left, right, nil)
end

# Create a StringNode with the optionally given location.
def self.string_node(unescaped:, location:)
StringNode.new(nil, 0, nil, nil, nil, unescaped, location)
StringNode.new(nil, nil, location, 0, nil, nil, nil, unescaped)
end

# Create a TrueNode with the optionally given location.
def self.true_node(location:)
TrueNode.new(nil, location)
TrueNode.new(nil, nil, location, 0)
end
end

Expand Down

0 comments on commit f0b5bd6

Please sign in to comment.