Skip to content

Commit

Permalink
Hacky things
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Oct 25, 2022
1 parent 6f6ef25 commit b708117
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 21 deletions.
3 changes: 2 additions & 1 deletion bench.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
puts RUBY_DESCRIPTION

a = Example::Page.new.call
Example::Page.compile
# Example::Page.compile
# Example::LayoutComponent.compile
b = Example::Page.new.call

raise unless a == b
Expand Down
36 changes: 19 additions & 17 deletions fixtures/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@
module Example
class Page < Phlex::View
def template
h1 { "Hi" }
render LayoutComponent.new do
h1 { "Hi" }

table id: "test", class: "a b c d e f g" do
tr do
td id: "test", class: "a b c d e f g" do
span { "Hi" }
end
table id: "test", class: "a b c d e f g" do
tr do
td id: "test", class: "a b c d e f g" do
span { "Hi" }
end

td id: "test", class: "a b c d e f g" do
span { "Hi" }
end
td id: "test", class: "a b c d e f g" do
span { "Hi" }
end

td id: "test", class: "a b c d e f g" do
span { "Hi" }
end
td id: "test", class: "a b c d e f g" do
span { "Hi" }
end

td id: "test", class: "a b c d e f g" do
span { "Hi" }
end
td id: "test", class: "a b c d e f g" do
span { "Hi" }
end

td id: "test", class: "a b c d e f g" do
span { "Hi" }
td id: "test", class: "a b c d e f g" do
span { "Hi" }
end
end
end
end
Expand Down
24 changes: 21 additions & 3 deletions lib/phlex/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def initialize(view)
@view = view
end

attr_accessor :scope

def inspect
"#{self.class.name} for #{@view.name} view class"
end
Expand All @@ -26,9 +28,25 @@ def redefined?(method_name)
end

def redefine(method, line:)
print method
print "\n\n"
@view.class_eval(method, file, line)
string = the_scope + method + (";end" * scope.size)

print string
puts
puts


eval(string)

# Module.class_eval(string, file, line)
end

def the_scope
scope.map do |scope|
case scope
in SyntaxTree::ModuleDeclaration then "module #{scope.constant.constant.value};"
in SyntaxTree::ClassDeclaration then "class #{scope.constant.constant.value};"
end
end.join
end

def line
Expand Down
16 changes: 16 additions & 0 deletions lib/phlex/compiler/visitors/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@

module Phlex::Compiler::Visitors
class File < Base
def initialize(compiler)
@scope = []
super
end

visit_method def visit_class(node)
@scope.push node

if node.location.start_line == @compiler.line
@compiler.scope = @scope
View.new(@compiler).visit_all(node.child_nodes)
else
super
end

@scope.pop
end

visit_method def visit_module(node)
@scope.push(node)
super
@scope.pop
end
end
end
7 changes: 7 additions & 0 deletions lib/phlex/compiler/visitors/stable_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ def visit_brace_block(node)
def visit_do_block(node)
nil
end

def visit_method_add_block(node)
node = Phlex::Compiler::Nodes::MethodAddBlock.new(node)
if node.method_call.name == :render
visit(node.content)
end
end
end
2 changes: 2 additions & 0 deletions lib/phlex/compiler/visitors/view_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def optimized_something?
if optimizer.call
@optimized_something = true
end

super
end

visit_method def visit_vcall(node)
Expand Down

0 comments on commit b708117

Please sign in to comment.