Class: Udb::Eqn

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/udb/eqn.rb

Overview

parses the equation format from ‘eqntott` / `espresso` and converts it to a LogicNode

Defined Under Namespace

Classes: EmptyEqnParen, EqnAnd, EqnName, EqnNot, EqnOne, EqnOr, EqnParen, EqnTop, EqnZero

Constant Summary collapse

EQN_GRAMMAR =
<<~GRAMMAR
  # Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
  # SPDX-License-Identifier: BSD-3-Clause-Clear

  # grammar for the EQNTOTT format

  grammar Eqn
    rule eqn
      expression space* ';' space* <Udb::Eqn::EqnTop>
    end

    rule name
      [a-zA-Z_] [a-zA-Z0-9.]* <Udb::Eqn::EqnName>
    end

    rule zero
      'ZERO' / '0' <Udb::Eqn::EqnZero>
    end

    rule one
      'ONE' / '1' <Udb::Eqn::EqnOne>
    end

    rule paren
      '(' space* ')' <Udb::Eqn::EmptyEqnParen>
      /
      '(' space* conjunction space* ')' <Udb::Eqn::EqnParen>
    end

    rule not
      '!' space* name <Udb::Eqn::EqnNot>
    end

    rule unary_expression
      paren / not / zero / one / name
    end

    rule conjunction
      first:unary_expression r:(space* '&' space* unary_expression)+ <Udb::Eqn::EqnAnd>
      /
      unary_expression
    end

    rule disjunction
      first:conjunction r:(space* '|' space* conjunction)+ <Udb::Eqn::EqnOr>
      /
      conjunction
    end

    rule expression
      (space* disjunction space*)
      {
        def to_logic_tree(term_map)
          disjunction.to_logic_tree(term_map)
        end
      }
    end

    rule space
      [ \n]
    end
  end
GRAMMAR

Instance Method Summary collapse

Constructor Details

#initialize(eqn)

Parameters:

  • eqn (String)


184
185
186
187
# File 'lib/udb/eqn.rb', line 184

def initialize(eqn)
  @eqn = eqn
  @parser = T.unsafe(EqnParser).new
end

Instance Method Details

#to_logic_tree(term_map) ⇒ LogicNode

Parameters:

Returns:



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/udb/eqn.rb', line 190

def to_logic_tree(term_map)
  m = @parser.parse(@eqn)
  if m.nil?
    puts "start"
    pp @eqn
    puts "end"
    raise "Error parsing eqn: #{@parser.failure_reason}"
  end

  raise "unexpected" unless m.is_a?(EqnTop)

  m.to_logic_tree(term_map)
end