Last active
December 16, 2015 08:59
-
-
Save sudiptamondal/5409601 to your computer and use it in GitHub Desktop.
Scope: Many programming languages implement scope for variables – and they are context sensitive. For example a global scope variable will be over-ridden using local variable definition. You need to implement simple interpreter which tells us current value of a variable, if its not defined then value should be zero, Input will start with ‘[‘ and…
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
puts "Enter your statements separated by a carriage return, The matching braces will terminate the loop" | |
variables = Hash.new(Hash.new) | |
opening_brackets = 0 | |
closing_brackets = 0 | |
while true | |
variable = nil | |
level = nil | |
value = nil | |
input = gets.chomp | |
if input.split.join.empty? | |
puts "enter your statement" | |
else | |
if '[' == input | |
opening_brackets += 1 | |
next | |
end | |
if ']' == input | |
if opening_brackets < (closing_brackets+1) | |
puts "error in input" | |
break | |
else | |
closing_brackets += 1 | |
level_to_delete = opening_brackets - closing_brackets + 1 | |
variables.each do |h,k| | |
k.delete(level_to_delete) | |
end | |
if opening_brackets == closing_brackets | |
puts "loop complete" | |
break | |
else | |
next | |
end | |
end | |
end | |
input = input.split.join(' ') | |
case input.split(' ')[0] | |
when 'print' | |
if variables.has_key? input.split(' ')[1] | |
value_found = false | |
(opening_brackets - closing_brackets).downto 1 do |temp| | |
unless variables[input.split(' ')[1]][temp].nil? | |
puts variables[input.split(' ')[1]][temp] | |
value_found = true | |
break | |
else | |
next | |
end | |
end | |
puts 0 unless value_found | |
else | |
puts 0 | |
end | |
else | |
level = opening_brackets - closing_brackets | |
if variables.has_key? input.split(' ')[0] | |
if input.split(' ')[1].to_i.to_s == input.split(' ')[1] | |
variables["#{input.split(' ')[0]}"].merge! level => (input.split(' ')[1] || "0") | |
else | |
(opening_brackets - closing_brackets).downto 1 do |temp| | |
value = variables[input.split(' ')[1]][temp] | |
if value.nil? | |
next | |
else | |
variables["#{input.split(' ')[0]}"].merge! level => value | |
break | |
end | |
end | |
end | |
else | |
value_found = false | |
if input.split(' ')[1].to_i.to_s == input.split(' ')[1] | |
variables.merge! "#{input.split(' ')[0]}" => {level => (input.split(' ')[1] || "0")} | |
else | |
(opening_brackets - closing_brackets).downto 1 do |temp| | |
value = variables[input.split(' ')[1]][temp] | |
if value.nil? | |
next | |
else | |
value_found = true | |
variables.merge! "#{input.split(' ')[0]}" => { level => value } | |
break | |
end | |
end | |
(variables["#{input.split(' ')[0]}"].merge! level => value ) unless value_found | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment