basic_math_parser 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74a5a0a134172ccb5a65eaf340034fa2eb0911603542e9f27bf709b43a657a7e
4
- data.tar.gz: 5801d35deb16a2e7ae6372bcffb2b19834d409c77ed283bf680cd049c346f005
3
+ metadata.gz: 52924e0bcd022abe3ba617a25b7e6789e69ee8e6a4ca992180754cd5cbb7e80d
4
+ data.tar.gz: 8d53ec2780e0e632d56b0356b65e9d819615f41c71e1dab89de2293a0f4b1d32
5
5
  SHA512:
6
- metadata.gz: 27f55234f0f0ad6c7bf69be36a0b3ab1679a8eb6a41ab2c9372900eb074cc408ad1086ce82cf3e0ee0452235cceb9a052c991b845acda9e6993e51076159c7b5
7
- data.tar.gz: e784e921b92538617f99e64653a60d474da41ef99ef40abbe3cd01d24f0ceba9de045e102281fdbe902ad5191bccf6290dfa5f24be7275c6978a9f0d19757b88
6
+ metadata.gz: f7100eaf05f324d855200f3d3e46b1ee5a58d3f2e9bfd96642857736da4b221d0df7e3e55c6ae76d39f9784f301c25febd932883defe31529dc780cfab68aee6
7
+ data.tar.gz: 28089b44db08dbf543ef470beb34f26f56d978bdc3878b0fe659cc0cf40f7993c0602e27378504eeff738dc3f375bc9b2d7792d258ab35d020665f3f13d2e636
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # BasicMathParser
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/basic_math_parser`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ BasicMathParser is an simple expression parser bulit based on the postfix expression parsing.
6
4
 
7
5
  ## Installation
8
6
 
@@ -14,9 +12,12 @@ If bundler is not being used to manage dependencies, install the gem by executin
14
12
 
15
13
  $ gem install basic_math_parser
16
14
 
17
- ## Usage
15
+ ## Example
16
+
17
+ To parse an expression:
18
18
 
19
- TODO: Write usage instructions here
19
+ BasicMathParser.evaluate("3+5")
20
+ => 8.0
20
21
 
21
22
  ## Development
22
23
 
@@ -22,7 +22,7 @@ class PostfixEvaluator
22
22
  right_num = @numbers.pop
23
23
  left_num = @numbers.pop
24
24
 
25
- raise BasicMathParser::ParseError.for('Invalid postfix expression', right: right_num, left: left_num ) unless right_num && left_num
25
+ raise BasicMathParser::ParseError.for('Invalid postfix expression', info: { right: right_num, left: left_num } ) unless right_num && left_num
26
26
 
27
27
  result = evaluate(left_num.value, right_num.value, token)
28
28
  @numbers << Token.new(:number, result)
@@ -3,7 +3,8 @@ require_relative 'token'
3
3
  class Tokenizer
4
4
  RULES = {
5
5
  /\d+\.\d+|\d+|\.\d+/ => :number,
6
- /[\/+\-*^%\()]/ => :op
6
+ /[\/+\-*^%\()]/ => :op,
7
+ /\d+[a-zA-z]+|[a-zA-z]+\d+|[a-zA-z]+/ => :variable
7
8
  }
8
9
 
9
10
  def initialize
@@ -27,6 +28,9 @@ class Tokenizer
27
28
 
28
29
  def find_token(regex, type)
29
30
  token = @buffer.scan(regex)
31
+
32
+ raise BasicMathParser::ParseError.for('variable are not unsupported', info: {variable: token} ) if token && type == :variable
33
+
30
34
  @tokens << Token.new(type, token) if token
31
35
  end
32
36
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BasicMathParser
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  require 'strscan'
3
+ require 'timeout'
3
4
 
4
5
  require_relative "basic_math_parser/version"
5
6
  require_relative "basic_math_parser/postfix_evaluator"
@@ -8,14 +9,16 @@ require_relative "basic_math_parser/tokenizer"
8
9
 
9
10
  module BasicMathParser
10
11
  def self.evaluate(expression)
11
- # Parse input string
12
- lexer = Tokenizer.new
13
- tokens = lexer.parse(expression)
12
+ status = Timeout::timeout(5) {
13
+ # Parse input string
14
+ lexer = Tokenizer.new
15
+ tokens = lexer.parse(expression)
14
16
 
15
- # Convert to postfix notation
16
- postfix_exp = Parser.new(tokens).run
17
+ # Convert to postfix notation
18
+ postfix_exp = Parser.new(tokens).run
17
19
 
18
- # Evaluate
19
- PostfixEvaluator.new.run(postfix_exp.output)
20
+ # Evaluate
21
+ PostfixEvaluator.new.run(postfix_exp.output)
22
+ }
20
23
  end
21
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: basic_math_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - srkrishna412
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-15 00:00:00.000000000 Z
11
+ date: 2022-06-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Evalute a basic arithmetic expression
14
14
  email: