Class: Code

Inherits:
Object show all
Defined in:
lib/code.rb,
lib/code/node.rb,
lib/code/type.rb,
lib/code/error.rb,
lib/code/object.rb,
lib/code/parser.rb,
lib/code/node/if.rb,
lib/code/type/or.rb,
lib/code/node/not.rb,
lib/code/type/sig.rb,
lib/code/node/call.rb,
lib/code/node/code.rb,
lib/code/node/list.rb,
lib/code/parser/if.rb,
lib/code/type/hash.rb,
lib/code/node/splat.rb,
lib/code/node/while.rb,
lib/code/type/maybe.rb,
lib/code/node/base_2.rb,
lib/code/node/base_8.rb,
lib/code/node/number.rb,
lib/code/node/string.rb,
lib/code/object/code.rb,
lib/code/object/date.rb,
lib/code/object/json.rb,
lib/code/object/list.rb,
lib/code/object/time.rb,
lib/code/parser/call.rb,
lib/code/parser/code.rb,
lib/code/parser/list.rb,
lib/code/parser/name.rb,
lib/code/type/repeat.rb,
lib/code/node/base_10.rb,
lib/code/node/base_16.rb,
lib/code/node/boolean.rb,
lib/code/node/decimal.rb,
lib/code/node/nothing.rb,
lib/code/node/ternary.rb,
lib/code/object/class.rb,
lib/code/object/range.rb,
lib/code/parser/class.rb,
lib/code/parser/equal.rb,
lib/code/parser/group.rb,
lib/code/parser/power.rb,
lib/code/parser/range.rb,
lib/code/parser/shift.rb,
lib/code/parser/splat.rb,
lib/code/parser/while.rb,
lib/code/node/function.rb,
lib/code/node/negation.rb,
lib/code/object/global.rb,
lib/code/object/string.rb,
lib/code/parser/number.rb,
lib/code/parser/rescue.rb,
lib/code/parser/string.rb,
lib/code/node/statement.rb,
lib/code/object/boolean.rb,
lib/code/object/context.rb,
lib/code/object/decimal.rb,
lib/code/object/integer.rb,
lib/code/object/nothing.rb,
lib/code/parser/boolean.rb,
lib/code/parser/greater.rb,
lib/code/parser/nothing.rb,
lib/code/parser/ternary.rb,
lib/code/node/dictionary.rb,
lib/code/object/duration.rb,
lib/code/object/function.rb,
lib/code/parser/addition.rb,
lib/code/parser/equality.rb,
lib/code/parser/function.rb,
lib/code/parser/negation.rb,
lib/code/node/unary_minus.rb,
lib/code/object/parameter.rb,
lib/code/parser/statement.rb,
lib/code/object/dictionary.rb,
lib/code/parser/bitwise_or.rb,
lib/code/parser/dictionary.rb,
lib/code/parser/or_keyword.rb,
lib/code/parser/whitespace.rb,
lib/code/node/call_argument.rb,
lib/code/parser/bitwise_and.rb,
lib/code/parser/if_modifier.rb,
lib/code/parser/not_keyword.rb,
lib/code/parser/or_operator.rb,
lib/code/parser/unary_minus.rb,
lib/code/node/left_operation.rb,
lib/code/node/square_bracket.rb,
lib/code/parser/and_operator.rb,
lib/code/parser/chained_call.rb,
lib/code/node/right_operation.rb,
lib/code/parser/left_operation.rb,
lib/code/parser/multiplication.rb,
lib/code/parser/square_bracket.rb,
lib/code/object/identifier_list.rb,
lib/code/parser/right_operation.rb,
lib/code/node/function_parameter.rb

Defined Under Namespace

Classes: Error, Node, Object, Parser, Type

Constant Summary collapse

GLOBALS =
%i[output error context object].freeze
DEFAULT_TIMEOUT =
0
Version =
Gem::Version.new("0.14.0")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output: StringIO.new, error: StringIO.new, timeout: DEFAULT_TIMEOUT) ⇒ Code

Returns a new instance of Code.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/code.rb', line 7

def initialize(
  input,
  output: StringIO.new,
  error: StringIO.new,
  timeout: DEFAULT_TIMEOUT
)
  @input = input
  @output = output
  @error = error
  @timeout = timeout || DEFAULT_TIMEOUT
  @context = Object::Context.new
end

Class Method Details

.evaluate(input, output: StringIO.new, error: StringIO.new, timeout: DEFAULT_TIMEOUT) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/code.rb', line 24

def self.evaluate(
  input,
  output: StringIO.new,
  error: StringIO.new,
  timeout: DEFAULT_TIMEOUT
)
  new(input, output:, error:, timeout:).evaluate
end

.parse(input, timeout: DEFAULT_TIMEOUT) ⇒ Object



20
21
22
# File 'lib/code.rb', line 20

def self.parse(input, timeout: DEFAULT_TIMEOUT)
  Timeout.timeout(timeout) { Parser.parse(input).to_raw }
end

Instance Method Details

#evaluateObject



33
34
35
36
37
38
# File 'lib/code.rb', line 33

def evaluate
  Timeout.timeout(timeout) do
    parsed = Code.parse(input)
    Node::Code.new(parsed).evaluate(context:, output:, error:)
  end
end