Coder Social home page Coder Social logo

project-eutopia / keisan Goto Github PK

View Code? Open in Web Editor NEW
72.0 4.0 10.0 930 KB

A Ruby-based expression parser, evaluator, and programming language

License: MIT License

Ruby 99.97% Shell 0.03%
ruby calculator equation formula symbolic-math programming-language math

keisan's People

Contributors

project-eutopia avatar shioyama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

keisan's Issues

Date & Time Support

We need to manipulate date & time. We also need some basic date & time function like NOW, TODAY. They can be added if date & time data type is supported.

Set operation

Hi! Thank you for your amazing library for computations.

I would like to ask if it is possible to include some basic set operation for Array (like in basic Ruby).

In particular:

Difference: [1,2,3] - [2,3,4] = [1]
Intersection: [1,2,3] & [2,3,4] = [2,3]
Union: [1,2,3] | [2,3,4] = [1,2,3,4]

In the meanwhile I will implement them with "define_function" method.

Derivative of `ExpressionFunction`

Instances of ExpressionFunction contain the original AST expression that defined the function, so it should be possible to take derivatives of them. This is unlike ProcFunction which loses the mathematical structure behind the Proc instance.

Tokenizer issue related to comment character in strings

Hello! I ran into another issue with the tokenizer in (related to #94) regards to strings whereby having a hash character (which marks the remainder of the line as a comment) in a string literal will cause tokenization to fail, as the closing quote at the end of the line is regarded as part of the comment.

Version: 0.8.3

Executing the following demonstrates the issue:

Keisan::Calculator.new.ast('"#1"')
# raises Keisan::Exceptions::TokenizingError (Tokenizing error, no closing quote ')

Keisan::Calculator.new.ast("'#1'")
# raises Keisan::Exceptions::TokenizingError (Tokenizing error, no closing quote ")

Expected behavior: comment sequences should be treated as part of the string literal, only being ignored if encountered outside a string literal. I'm guessing the expression syntax is supposed to be much like actual Ruby, and I know that string literals like '#' definitely treat the hash as part of the literal and not a comment sequence.

Add more Array methods

Currently can only get values inside of arrays or get the size of an array. Could add support for splices, or even functional math operations on arrays.

Parsing error: escaped quotes in string under a group

Hello, I've run into another issue with escaped quotes in strings. Parsing works great with escaped strings, but if the string literal is enclosed in a group, the tokenizer fails:

# Simple string
Keisan::Calculator.new.ast('"a"')
# => #<Keisan::AST::String:0x00007fdf97128bc0 @content="a">

# With escaped double quote
Keisan::Calculator.new.ast('"\"a"')
# => #<Keisan::AST::String:0x00007fdf9b8d8c68 @content="\"a">

# Escaped double quote in a group
Keisan::Calculator.new.ast('("\"a")')
# Raises Keisan::Exceptions::TokenizingError (Tokenizing error, no closing quote ")

It occurs for any form of group, and either quote style—single or double.

Edit: I should add a more practical example along with an example of some of the more wacky behavior:

# Calling a function with a string arg that contains an escaped quote
Keisan::Calculator.new.ast('my_fn("\"a")')
# Raises Keisan::Exceptions::TokenizingError (Tokenizing error, no closing quote ")

# The expression gets modified in a strange way if your escaped quotes are actually closed, e.g.
Keisan::Calculator.new.ast('("\"a\"")').to_s
# => "(\"\"*a)*\"\""
# The above line is output from Rails console, so it's an `inspect`ed string. The actual value of the string it spits out is:
# (""*a)*""
# but it should be
# "\"a\""

Currently using Keisan version 0.8.4

Tokenizer issue: grouping symbols in strings

Gem version: 0.8.0

Hello, I came across an issue involving grouping symbols in string literals throwing off the tokenizer. It looks like the grouping regex is not ignoring grouping symbols inside of string literals. This isn't a problem if the string literal itself uses matched groups (see second example below), but if you put an unpaired grouping symbol anywhere in the string it causes issues.

Running the following in an interactive console demonstrates the issue:

Keisan::Calculator.new.ast("f('a')")
# => #<Keisan::AST::Function ...>

Keisan::Calculator.new.ast("f('a()')")
# => #<Keisan::AST::Function ...>

Keisan::Calculator.new.ast("f('a]')")
# raises Keisan::Exceptions::TokenizingError (Unexpected token: "(")

# Another error example
Keisan::Calculator.new.ast("f('a)')")
# raises Keisan::Exceptions::TokenizingError (Unexpected token: "'")

Add more string methods

Not much support for strings except for basic comparison and concatenation. Add support for things like splicing and searching too. See Ruby String methods for ideas.

Remove `activesupport` dependency

Required changes:

  • Implement Array.split
  • Implement Array.wrap
  • Implement Object.present? and Object.blank?
  • Implement String.underscore
  • Change try to safe navigation operator &.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.