-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from project-eutopia/hash
Add support for Hashes (associative arrays)
- Loading branch information
Showing
17 changed files
with
276 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module Keisan | ||
module AST | ||
class Hash < Node | ||
def initialize(key_value_pairs) | ||
@hash = ::Hash[key_value_pairs] | ||
stringify_and_cellify! | ||
end | ||
|
||
def [](key) | ||
key = key.to_node | ||
return nil unless key.is_a?(AST::String) | ||
|
||
if val = @hash[key.value] | ||
val | ||
else | ||
Cell.new(Null.new).tap do |cell| | ||
@hash[key.value] = cell | ||
end | ||
end | ||
end | ||
|
||
def evaluate(context = nil) | ||
context ||= Context.new | ||
stringify_and_cellify! | ||
|
||
@hash = ::Hash[ | ||
@hash.map do |key, val| | ||
[key, Cell.new(val.evaluate(context))] | ||
end | ||
] | ||
|
||
self | ||
end | ||
|
||
def simplify(context = nil) | ||
evaluate(context) | ||
end | ||
|
||
def value(context = nil) | ||
context ||= Context.new | ||
evaluate(context) | ||
|
||
::Hash[ | ||
@hash.map {|key, val| | ||
raise Exceptions::InvalidExpression.new("Keisan::AST::Hash#value must have all keys evaluate to strings") unless key.is_a?(::String) | ||
[key, val.value(context)] | ||
} | ||
] | ||
end | ||
|
||
def to_s | ||
"{#{@hash.map {|k,v| "'#{k}': #{v}"}.join(', ')}}" | ||
end | ||
|
||
private | ||
|
||
def stringify_and_cellify! | ||
@hash = ::Hash[ | ||
@hash.map do |key, val| | ||
key = key.value if key.is_a?(AST::String) | ||
val = Cell.new(val) unless val.is_a?(Cell) | ||
[key, val] | ||
end | ||
] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Keisan | ||
module Parsing | ||
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| | ||
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)} | ||
raise Exceptions::ParseError.new("Invalid hash") unless key.size == 1 && value.size >= 1 | ||
|
||
key = key.first | ||
raise Exceptions::ParseError.new("Invalid hash (keys must be strings)") unless key.is_a?(Tokens::String) | ||
[Parsing::String.new(key.value), Parsing::RoundGroup.new(value)] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Keisan | ||
module Tokens | ||
class Colon < Token | ||
REGEX = /(\:)/ | ||
|
||
def self.regex | ||
REGEX | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.