Sha256: 64359ad4cc3974c288c3b799c53d50de58e8581e47cd6e7314dceb480fa91ace

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

module SimpleSem
  grammar Arithmetic
    rule expression
      comparative / additive
    end

    rule comparative
      operand_1:additive space operator:comparison_op space operand_2:additive <BinaryOperation>
    end

    rule comparison_op
      '>=' {
        def apply(a, b)
          a >= b
        end
      }
      /
      '<=' {
        def apply(a, b)
          a <= b
        end
      }
      /
      '>' {
        def apply(a, b)
          a > b
        end
      }
      /
      '<' {
        def apply(a, b)
          a < b
        end
      }
      /
      '!=' {
        def apply(a, b)
          a != b
        end
      }
      /
      '=' {
        def apply(a, b)
          a == b
        end
      }
    end

    rule additive
      operand_1:multitive space operator:additive_op space operand_2:additive <BinaryOperation>
      /
      multitive
    end

    rule additive_op
      '+' {
        def apply(a, b)
          a + b
        end
      }
      /
      '-' {
        def apply(a, b)
          a - b
        end
      }
    end

    rule multitive
      operand_1:primary space operator:multitive_op space operand_2:multitive <BinaryOperation>
      /
      primary
    end

    rule multitive_op
      '*' {
        def apply(a, b)
          a * b
        end
      }
      /
      '/' {
        def apply(a, b)
          a / b
        end
      }
    end

    rule number
      ('-'? [1-9] [0-9]* / '0') {
        def eval(env={})
          text_value.to_i
        end
      }
    end

    rule space
      ' '*
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
robolson-simplesem-0.1.4 lib/simplesem/arithmetic.treetop
simplesem-0.1.4 lib/simplesem/arithmetic.treetop