Class: Halunke::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/halunke/interpreter.rb

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInterpreter

Returns a new instance of Interpreter.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/halunke/interpreter.rb', line 9

def initialize
  @parser = Parser.new
  @root_context = Context.new

  @root_context["Class"] = Halunke::Runtime::HClass
  @root_context["Function"] = Halunke::Runtime::HFunction
  @root_context["Number"] = Halunke::Runtime::HNumber
  @root_context["String"] = Halunke::Runtime::HString
  @root_context["Array"] = Halunke::Runtime::HArray
  @root_context["Dictionary"] = Halunke::Runtime::HDictionary
  @root_context["UnassignedBareword"] = Halunke::Runtime::HUnassignedBareword
  @root_context["Regexp"] = Halunke::Runtime::HRegexp
  @root_context["stdio"] = Halunke::Runtime::HStdio.create_instance
  @root_context["web"] = Halunke::Runtime::HWeb.create_instance

  preludes.each do |prelude|
    self.eval(prelude)
  end
end

Instance Attribute Details

#root_contextObject (readonly)

Returns the value of attribute root_context.



7
8
9
# File 'lib/halunke/interpreter.rb', line 7

def root_context
  @root_context
end

Instance Method Details

#eval(str, error_mode: :raise, exit_on_error: false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/halunke/interpreter.rb', line 29

def eval(str, error_mode: :raise, exit_on_error: false)
  nodes = @parser.parse(str)
  result = nodes.eval(root_context)
  result.nil? ? nil : result.inspect(root_context)
rescue HError => err
  raise err if error_mode == :raise

  puts err.source_code_position.reveal(str, error_mode)
  puts err.message
  exit(1) if exit_on_error

  nil
end

#preludesObject



43
44
45
46
47
48
# File 'lib/halunke/interpreter.rb', line 43

def preludes
  [
    Pathname.new(__dir__).join("runtime", "true.hal").read,
    Pathname.new(__dir__).join("runtime", "false.hal").read
  ]
end