Class: Expressir::Express::Builders::FunctionDeclBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/expressir/express/builders/function_decl_builder.rb

Overview

Builds function declaration nodes.

Instance Method Summary collapse

Instance Method Details

#call(ast_data) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/expressir/express/builders/function_decl_builder.rb', line 8

def call(ast_data)
  head = ast_data[:function_head]
  algorithm_head = ast_data[:algorithm_head]
  stmts = ast_data[:stmt]

  id = Builder.build_optional(head[:function_id]) if head
  parameters = build_parameters(head)
  return_type = Builder.build_optional(head[:parameter_type]) if head

  declarations = []
  if algorithm_head.is_a?(Hash)
    declarations = Builder.build_children(algorithm_head[:declaration])
  end

  types = declarations.grep(Expressir::Model::Declarations::Type)
  entities = declarations.grep(Expressir::Model::Declarations::Entity)
  subtype_constraints = declarations.grep(Expressir::Model::Declarations::SubtypeConstraint)
  functions = declarations.grep(Expressir::Model::Declarations::Function)
  procedures = declarations.grep(Expressir::Model::Declarations::Procedure)
  constants = if algorithm_head.is_a?(Hash) && algorithm_head[:constant_decl]
                build_constant_decl(algorithm_head[:constant_decl])
              else
                []
              end
  variables = if algorithm_head.is_a?(Hash) && algorithm_head[:local_decl]
                build_local_decl(algorithm_head[:local_decl])
              else
                []
              end
  statements = Builder.build_children(stmts)

  Expressir::Model::Declarations::Function.new(
    id: id,
    parameters: parameters,
    return_type: return_type,
    types: types,
    entities: entities,
    subtype_constraints: subtype_constraints,
    functions: functions,
    procedures: procedures,
    constants: constants,
    variables: variables,
    statements: statements.compact,
  )
end