Class: MVinl::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/mvinl/context.rb

Overview

Lexer and parser shared context

Constant Summary collapse

RESERVED =
I[def]
CONSTANTS =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



13
14
15
# File 'lib/mvinl/context.rb', line 13

def initialize
  reset
end

Instance Attribute Details

#functionsObject

Returns the value of attribute functions.



8
9
10
# File 'lib/mvinl/context.rb', line 8

def functions
  @functions
end

#stateObject

Returns the value of attribute state.



8
9
10
# File 'lib/mvinl/context.rb', line 8

def state
  @state
end

#variablesObject

Returns the value of attribute variables.



8
9
10
# File 'lib/mvinl/context.rb', line 8

def variables
  @variables
end

Instance Method Details

#define_constant(name, value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/mvinl/context.rb', line 34

def define_constant(name, value)
  if RESERVED.include? name
    nil
  elsif CONSTANTS[name]
    false
  else
    @state[:in_var] = false
    CONSTANTS[name] = value
  end
end

#define_function(name, args, body) ⇒ Object



30
31
32
# File 'lib/mvinl/context.rb', line 30

def define_function(name, args, body)
  @functions[name] = { args: args, body: body }
end

#define_variable(name, value) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/mvinl/context.rb', line 45

def define_variable(name, value)
  if RESERVED.include? name
    nil
  else
    @state[:in_var] = false
    @variables[name] = value
  end
end

#resetObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mvinl/context.rb', line 17

def reset
  @variables = {}
  @functions = {}
  @state = {
    in_prop: false,
    in_var: false,
    in_keyword_arg: false,
    keyword_arg_depth: 0,
    lines: 0,
    depth: 0
  }
end