2021-12-07 16:09:17 +01:00
|
|
|
# frozen_string_literal: true
|
2021-11-10 11:01:26 +01:00
|
|
|
|
2021-11-23 13:10:03 +01:00
|
|
|
module Rpl
|
2021-12-07 15:50:58 +01:00
|
|
|
module Lang
|
2021-12-07 16:09:17 +01:00
|
|
|
class Dictionary
|
2021-12-08 12:46:57 +01:00
|
|
|
attr_reader :vars
|
|
|
|
|
2021-12-07 16:09:17 +01:00
|
|
|
def initialize
|
|
|
|
@parser = Parser.new
|
|
|
|
@words = {}
|
2021-12-08 12:46:57 +01:00
|
|
|
@vars = {}
|
2021-12-07 16:09:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def add( name, implementation )
|
|
|
|
@words[ name ] = implementation
|
|
|
|
end
|
|
|
|
|
2021-12-08 12:46:57 +01:00
|
|
|
def add_var( name, implementation )
|
|
|
|
@vars[ name ] = implementation
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_var( name )
|
|
|
|
@vars.delete( name )
|
2021-12-07 16:09:17 +01:00
|
|
|
end
|
|
|
|
|
2021-12-08 12:46:57 +01:00
|
|
|
def remove_all_vars
|
|
|
|
@vars = {}
|
|
|
|
end
|
|
|
|
|
2021-12-08 16:38:46 +01:00
|
|
|
def lookup( name )
|
2021-12-08 12:46:57 +01:00
|
|
|
word = @words[ name ]
|
|
|
|
word ||= @vars[ name ]
|
|
|
|
|
|
|
|
word
|
|
|
|
end
|
2021-11-10 11:01:26 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|