Skip to content

Commit

Permalink
Merge 44d753a into af39d52
Browse files Browse the repository at this point in the history
  • Loading branch information
project-eutopia committed Feb 1, 2018
2 parents af39d52 + 44d753a commit 528f814
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 22 deletions.
2 changes: 0 additions & 2 deletions keisan.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = ">= 2.0.0"

spec.add_dependency "activesupport", ">= 4.2.2"

spec.add_development_dependency "coveralls"
spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 10.0"
Expand Down
4 changes: 1 addition & 3 deletions lib/keisan.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require "active_support"
require "active_support/core_ext"

require "keisan/version"
require "keisan/exceptions"
require "keisan/util"

require "keisan/ast/node"
require "keisan/ast/cell"
Expand Down
4 changes: 2 additions & 2 deletions lib/keisan/ast/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def initialize(string: nil, parser: nil, components: nil)
elsif !parser.nil?
@components = parser.components
else
@components = Array.wrap(components)
@components = Array(components)
end

@lines = @components.split {|component|
@lines = Util.array_split(@components) {|component|
component.is_a?(Parsing::LineSeparator)
}.reject(&:empty?)

Expand Down
2 changes: 1 addition & 1 deletion lib/keisan/ast/operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def initialize(children = [], parsing_operators = [])
raise Exceptions::ASTError.new("Mismatch of children and operators")
end

children = Array.wrap(children)
children = Array(children)
super(children)

@parsing_operators = parsing_operators
Expand Down
2 changes: 1 addition & 1 deletion lib/keisan/ast/parent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Parent < Node
attr_reader :children

def initialize(children = [])
children = Array.wrap(children).map do |child|
children = Array(children).map do |child|
child.is_a?(Cell) ? child : child.to_node
end
raise Exceptions::InternalError.new unless children.is_a?(Array)
Expand Down
2 changes: 1 addition & 1 deletion lib/keisan/ast/unary_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Keisan
module AST
class UnaryOperator < Operator
def initialize(children = [])
children = Array.wrap(children)
children = Array(children)
super(children)
if children.count != 1
raise Exceptions::ASTError.new("Unary operator takes has a single child")
Expand Down
8 changes: 4 additions & 4 deletions lib/keisan/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class Context

def initialize(parent: nil, random: nil, allow_recursive: false, shadowed: [])
@parent = parent
@function_registry = Functions::Registry.new(parent: @parent.try(:function_registry))
@variable_registry = Variables::Registry.new(parent: @parent.try(:variable_registry), shadowed: shadowed)
@function_registry = Functions::Registry.new(parent: @parent&.function_registry)
@variable_registry = Variables::Registry.new(parent: @parent&.variable_registry, shadowed: shadowed)
@random = random
@allow_recursive = allow_recursive
end
Expand Down Expand Up @@ -47,7 +47,7 @@ def spawn_child(definitions: {}, shadowed: [], transient: nil)

def transient_definitions
return {} unless @transient
parent_definitions = @parent.present? ? @parent.transient_definitions : {}
parent_definitions = @parent.nil? ? {} : @parent.transient_definitions
parent_definitions.merge(
@variable_registry.locals
).merge(
Expand Down Expand Up @@ -100,7 +100,7 @@ def register_function!(name, function, local: false)
end

def random
@random || @parent.try(:random) || Random.new
@random || @parent&.random || Random.new
end

protected
Expand Down
8 changes: 4 additions & 4 deletions lib/keisan/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def multi_line?
end

def parse_multi_line!
line_parsers = @tokens.split {|token| token.is_a?(Tokens::LineSeparator)}.map {|tokens| self.class.new(tokens: tokens)}
line_parsers = Util.array_split(@tokens) {|token| token.is_a?(Tokens::LineSeparator)}.map {|tokens| self.class.new(tokens: tokens)}
@components = []
line_parsers.each.with_index do |line_parser, i|
@components += line_parser.components
Expand All @@ -52,7 +52,7 @@ def parse_multi_line!
def parse_keyword!
keyword = tokens.first.string
arguments = if tokens[1].is_a?(Tokens::Group)
tokens[1].sub_tokens.split {|token| token.is_a?(Tokens::Comma)}.map {|argument_tokens|
Util.array_split(tokens[1].sub_tokens) {|token| token.is_a?(Tokens::Comma)}.map {|argument_tokens|
Parsing::Argument.new(argument_tokens)
}
else
Expand Down Expand Up @@ -212,7 +212,7 @@ def add_group_element_components!(token)
@components << Parsing::List.new(arguments_from_group(token))
when :curly
if token.sub_tokens.any? {|token| token.is_a?(Tokens::Colon)}
@components << Parsing::Hash.new(token.sub_tokens.split {|token| token.is_a?(Tokens::Comma)})
@components << Parsing::Hash.new(Util.array_split(token.sub_tokens) {|token| token.is_a?(Tokens::Comma)})
else
@components << Parsing::CurlyGroup.new(token.sub_tokens)
end
Expand Down Expand Up @@ -280,7 +280,7 @@ def arguments_from_group(token)
if token.sub_tokens.empty?
[]
else
token.sub_tokens.split {|sub_token| sub_token.is_a?(Tokens::Comma)}.map do |sub_tokens|
Util.array_split(token.sub_tokens) {|sub_token| sub_token.is_a?(Tokens::Comma)}.map do |sub_tokens|
Parsing::Argument.new(sub_tokens)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/keisan/parsing/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Function < Element

def initialize(name, arguments)
@name = name
@arguments = Array.wrap(arguments)
@arguments = Array(arguments)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/keisan/parsing/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ class Hash < Element
attr_reader :key_value_pairs

def initialize(key_value_pairs)
@key_value_pairs = Array.wrap(key_value_pairs).map {|key_value_pair|
@key_value_pairs = Array(key_value_pairs).map {|key_value_pair|
validate_and_extract_key_value_pair(key_value_pair)
}
end

private

def validate_and_extract_key_value_pair(key_value_pair)
key, value = key_value_pair.split {|token| token.is_a?(Tokens::Colon)}
key, value = Util.array_split(key_value_pair) {|token| token.is_a?(Tokens::Colon)}
raise Exceptions::ParseError.new("Invalid hash") unless key.size == 1 && value.size >= 1

key = key.first
Expand Down
2 changes: 1 addition & 1 deletion lib/keisan/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def type
end

def self.type
@type ||= self.to_s.split("::").last.underscore.to_sym
@type ||= Util.underscore(self.to_s.split("::").last).to_sym
end

def regex
Expand Down
19 changes: 19 additions & 0 deletions lib/keisan/util.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Keisan
class Util
def self.underscore(str)
str.to_s.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').downcase
end

def self.array_split(array, &block)
array.inject([[]]) do |results, element|
if yield(element)
results << []
else
results.last << element
end

results
end
end
end
end

0 comments on commit 528f814

Please sign in to comment.