Class: Code

Inherits:
Object
  • Object
show all
Defined in:
lib/code.rb,
lib/code/node.rb,
lib/code/ruby.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/date.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/number.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/argument.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/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/object/ruby_function.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, Ruby, Type

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Code.



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

def initialize(
  input,
  output: StringIO.new,
  error: StringIO.new,
  timeout: DEFAULT_TIMEOUT,
  ruby: {}
)
  @input = input
  @parsed = Timeout.timeout(timeout) { ::Code::Parser.parse(input).to_raw }
  @output = output
  @error = error
  @timeout = timeout || DEFAULT_TIMEOUT
  @ruby = ::Code::Ruby.to_code(ruby || {}).code_to_context
end

Class Method Details

.evaluate(input, context = EMPTY_STRING, output: StringIO.new, error: StringIO.new, timeout: DEFAULT_TIMEOUT, ruby: {}) ⇒ Object



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

def self.evaluate(
  input,
  context = EMPTY_STRING,
  output: StringIO.new,
  error: StringIO.new,
  timeout: DEFAULT_TIMEOUT,
  ruby: {}
)
  new(input, output:, error:, timeout:, ruby:).evaluate(context)
end

Instance Method Details

#evaluate(context = EMPTY_STRING) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/code.rb', line 34

def evaluate(context = EMPTY_STRING)
  Timeout.timeout(timeout) do
    context =
      if context == EMPTY_STRING
        Object::Context.new
      else
        Code.evaluate(
          context,
          timeout:,
          output:,
          error:,
          ruby:
        ).code_to_context
      end

    context = ruby.merge(context)

    Node::Code.new(parsed).evaluate(context:, output:, error:)
  end
end