Class: XfOOrth::Console

Inherits:
Object show all
Includes:
ReadPoint
Defined in:
lib/fOOrth/compiler/source/console.rb

Overview

The console class enables the use of the command line console as a source for fOOrth commands and source code. The readline facility is used to enable editing and command history and retrieval.

Instance Attribute Summary

Attributes included from ReadPoint

#read_buffer

Instance Method Summary collapse

Methods included from ReadPoint

#eoln?, #read, #reset_read_point

Constructor Details

#initializeConsole

Initialize a new console command source.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fOOrth/compiler/source/console.rb', line 13

def initialize
  @peek_buffer = nil

  reset_read_point

  auto_src = lambda { SymbolMap.forward_map.keys.sort  }

  @edit = MiniReadline::Readline.new(history: true,
                                     auto_complete: true,
                                     auto_source: MiniReadline::ArraySource,
                                     array_src: auto_src,
                                     eoi_detect: true)
end

Instance Method Details

#closeObject Also known as: flush

Close the console source.



28
29
30
31
# File 'lib/fOOrth/compiler/source/console.rb', line 28

def close
  @peek_buffer = nil
  reset_read_point
end

#eof?Boolean

Has the scanning of the text reached the end of input?
Returns

  • Always returns false.

Returns:

  • (Boolean)


56
57
58
# File 'lib/fOOrth/compiler/source/console.rb', line 56

def eof?
  false
end

#fine_nameObject

The console has no file name to return.



84
85
86
# File 'lib/fOOrth/compiler/source/console.rb', line 84

def fine_name
  nil
end

#getObject

Get the next character of command text from the user.
Returns

  • The next character of user input as a string.



38
39
40
41
42
43
44
# File 'lib/fOOrth/compiler/source/console.rb', line 38

def get
  @peek_buffer || read do
    @edit.readline(prompt: prompt).rstrip
  end
ensure
  @peek_buffer = nil
end

#peekObject

Peek ahead by one character.
Returns:

  • A peek at next character or nil if none are available.



49
50
51
# File 'lib/fOOrth/compiler/source/console.rb', line 49

def peek
  @peek_buffer ||= get unless eoln?
end

#promptObject

Build the command prompt for the user based on the state of the virtual machine.
Returns

  • A prompt string.


Endemic Code Smells

  • :reek:FeatureEnvy



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fOOrth/compiler/source/console.rb', line 66

def prompt
  vm = Thread.current[:vm]
  puts

  if vm.show_stack
    vm.data_stack.to_foorth_s(vm)
    puts vm.pop
  end

  '>' * vm.context.depth + '"' * vm.quotes + '(' * vm.parens
end

#source_nameObject

What is the source of this text?



79
80
81
# File 'lib/fOOrth/compiler/source/console.rb', line 79

def source_name
  "The console."
end