Class: Loxxy::BackEnd::LoxFunction
- Inherits:
-
Object
- Object
- Loxxy::BackEnd::LoxFunction
- Defined in:
- lib/loxxy/back_end/lox_function.rb
Overview
rubocop: disable Style/AccessorGrouping Representation of a Lox function. It is a named slot that can be associated with a value at the time.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#closure ⇒ Object
readonly
Returns the value of attribute closure.
-
#name ⇒ String
readonly
The name of the function (if any).
-
#parameters ⇒ Array<>
readonly
The parameters.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Instance Method Summary collapse
-
#! ⇒ Datatype::False
Logical negation.
- #accept(_visitor) ⇒ Object
- #arity ⇒ Object
- #call(engine, aVisitor) ⇒ Object
-
#initialize(aName, parameterList, aBody, anEngine) ⇒ LoxFunction
constructor
Create a function with given name.
-
#to_str ⇒ Object
Text representation of a Lox function.
Constructor Details
#initialize(aName, parameterList, aBody, anEngine) ⇒ LoxFunction
Create a function with given name
22 23 24 25 26 27 28 29 |
# File 'lib/loxxy/back_end/lox_function.rb', line 22 def initialize(aName, parameterList, aBody, anEngine) @name = aName.dup @parameters = parameterList @body = aBody.kind_of?(Ast::LoxNoopExpr) ? aBody : aBody.subnodes[0] @stack = anEngine.stack @closure = anEngine.symbol_table.current_env anEngine.symbol_table.current_env. = true end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
16 17 18 |
# File 'lib/loxxy/back_end/lox_function.rb', line 16 def body @body end |
#closure ⇒ Object (readonly)
Returns the value of attribute closure.
18 19 20 |
# File 'lib/loxxy/back_end/lox_function.rb', line 18 def closure @closure end |
#name ⇒ String (readonly)
Returns The name of the function (if any).
12 13 14 |
# File 'lib/loxxy/back_end/lox_function.rb', line 12 def name @name end |
#parameters ⇒ Array<> (readonly)
Returns the parameters.
15 16 17 |
# File 'lib/loxxy/back_end/lox_function.rb', line 15 def parameters @parameters end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
17 18 19 |
# File 'lib/loxxy/back_end/lox_function.rb', line 17 def stack @stack end |
Instance Method Details
#! ⇒ Datatype::False
Logical negation. As a function is a truthy thing, its negation is thus false.
60 61 62 |
# File 'lib/loxxy/back_end/lox_function.rb', line 60 def ! Datatype::False.instance end |
#accept(_visitor) ⇒ Object
35 36 37 |
# File 'lib/loxxy/back_end/lox_function.rb', line 35 def accept(_visitor) stack.push self end |
#arity ⇒ Object
31 32 33 |
# File 'lib/loxxy/back_end/lox_function.rb', line 31 def arity parameters ? parameters.size : 0 end |
#call(engine, aVisitor) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/loxxy/back_end/lox_function.rb', line 39 def call(engine, aVisitor) # new_env = Environment.new(engine.symbol_table.current_env) new_env = Environment.new(closure) engine.symbol_table.enter_environment(new_env) parameters&.each do |param_name| local = Variable.new(param_name, stack.pop) engine.symbol_table.insert(local) end catch(:return) do (body.nil? || body.kind_of?(Ast::LoxNoopExpr)) ? Datatype::Nil.instance : body.accept(aVisitor) throw(:return) end engine.symbol_table.leave_environment end |
#to_str ⇒ Object
Text representation of a Lox function
65 66 67 |
# File 'lib/loxxy/back_end/lox_function.rb', line 65 def to_str "<fn #{name}>" end |