Skip to content

Commit

Permalink
O.o eeetz alieeeve!
Browse files Browse the repository at this point in the history
Can't believe it was that easy!
  • Loading branch information
JoshCheek committed Apr 15, 2015
1 parent 99cd546 commit 2a05379
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
4 changes: 2 additions & 2 deletions bin/ddir
Expand Up @@ -36,7 +36,7 @@ parse_opts = {
c.downcase
.upcase
.chars.join '.'
# .map (c.downcase)
.map (c.downcase)
# .map (c*2)
# .map.with_index (c.upcase, i*2)
# .each.with_object "" ((char.downcase, index/2), str)
Expand All @@ -45,7 +45,7 @@ parse_opts = {
# else
# str << "nothin"
.chars.each_slice 2 (c1, c2) c1 + c2
# .chars.each_slice 2 (c1 + c2)
.chars.each_slice 2 (c1 + c2)
BODY
}

Expand Down
14 changes: 13 additions & 1 deletion lib/ddir/ast.rb
Expand Up @@ -7,8 +7,9 @@ class Context
attr_reader :depth

def initialize
@depth = 0
@depth = 0
@expressions_stack = []
@locals_stack = [[]]
end

def push_expressions
Expand Down Expand Up @@ -41,6 +42,17 @@ def update_depth(new_depth)
@depth = new_depth
@expressions_stack.pop while depth < current_expressions.depth
end

def declare_local(local)
@locals_stack.last << local
local
end

def record_locals
@locals_stack.push []
yield
@locals_stack.pop
end
end


Expand Down
9 changes: 6 additions & 3 deletions lib/ddir/ddir.treetop
Expand Up @@ -216,9 +216,10 @@ grammar DayDreamInRuby
}
/
expression:expression {
# doesn't work yet
def param_asts(context)
[expression.to_ast(context)]
context.record_locals { expression.to_ast context }
.uniq(&:name)
.map { |local| Ddir::Ast::OrdinalParam.new name: local.name }
end
def expression_asts(context)
[expression.to_ast(context)]
Expand Down Expand Up @@ -270,7 +271,9 @@ grammar DayDreamInRuby
rule local_variable
identifier {
def to_ast(context)
Ddir::Ast::LocalVariable.new name: text_value.intern
local = Ddir::Ast::LocalVariable.new name: text_value.intern
context.declare_local local
local
end
}
end
Expand Down
9 changes: 6 additions & 3 deletions lib/ddir/ddir.treetop.generated.rb
Expand Up @@ -1093,9 +1093,10 @@ def expression
end

module Param9
# doesn't work yet
def param_asts(context)
[expression.to_ast(context)]
context.record_locals { expression.to_ast context }
.uniq(&:name)
.map { |local| Ddir::Ast::OrdinalParam.new name: local.name }
end
def expression_asts(context)
[expression.to_ast(context)]
Expand Down Expand Up @@ -1544,7 +1545,9 @@ def _nt_variable

module LocalVariable0
def to_ast(context)
Ddir::Ast::LocalVariable.new name: text_value.intern
local = Ddir::Ast::LocalVariable.new name: text_value.intern
context.declare_local local
local
end
end

Expand Down
22 changes: 14 additions & 8 deletions spec/ddir_spec.rb
Expand Up @@ -218,25 +218,31 @@ class << self

context 'code in the argument lists' do
it 'identifies the locals by order of access and turns them into ordinals' do
pending
parses! '@.m (a.next, a + b)', block: {
parses! '@.m (a.next, 1 + b)', block: {
params: [
{ type: :ordinal_param, name: :a },
{ type: :ordinal_param, name: :b },
],
body: [
{ type: :send_message,
name: :next,
target: { type: :local_variable, name: :a },
{ type: :send_message,
name: :next,
receiver: { type: :local_variable, name: :a },
},
{ type: :send_message,
name: :next,
target: { type: :local_variable, name: :b },
{ type: :binary_expression,
operator: :+,
left_child: { type: :integer, value: 1 },
right_child: { type: :local_variable, name: :b },
},
],
}
end

it 'doesn\'t get all confused by the same var being used multiple times', t2:true do
parses! '@.m (a+a)', block: {
params: [{type: :ordinal_param, name: :a }]
}
end

it 'ignores locals in the surrounding environment'
it 'moves the code down into the body'
it 'accepts mixes of code args and other style args'
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -43,6 +43,7 @@ def recursive_assert(ast, expectations)
actual.zip(expected) { |actual_at_index, expected_at_index|
recursive_assert actual_at_index, expected_at_index
}
expect(actual.count).to eq expected.count
end
else
expect(actual).to eq expected
Expand Down

0 comments on commit 2a05379

Please sign in to comment.