60 Commits

Author SHA1 Message Date
Vinícius Garcia
6d18f60bb2 Update CI badge to reflect the new CI status 2025-06-09 10:15:01 -03:00
TOGO Li
7e6d2983e6 fix missing namespace in README.md examples and add space after include in functions.h 2023-08-11 06:43:56 +00:00
TOGO Li
e5a2252883 fix missing namespace in README.md examples and add space after include in functions.h 2023-08-11 06:39:17 +00:00
Vinícius Garcia
d4e1383590 Update the list of built-in operators on README 2022-11-06 10:01:32 -03:00
Vinícius Garcia
42959ec138 Minor improvement on README file 2022-05-10 08:49:42 -03:00
Vinícius Garcia
5ddfd90779 Update README.md 2021-05-05 14:29:02 -03:00
Vinícius Garcia
a93f0165f3 Adding a instruction on how to run tests as suggested on #83 2021-05-05 14:28:16 -03:00
Vinícius Garcia
a32511d632 Update README.md 2020-08-14 11:29:26 -03:00
Vinícius Garcia
85673cee67 Update README.md 2019-05-14 12:27:04 -03:00
Vinícius Garcia
ffe3b13eca Update README.md 2018-04-11 12:44:52 -03:00
Vinícius Garcia
f4980dd940 Update README.md 2017-05-01 13:41:36 -03:00
Vinícius Garcia
36ef0463f6 Update README.md 2017-04-19 14:26:32 -03:00
Vinícius Garcia
e422b1a77c Update README.md 2017-04-19 14:17:35 -03:00
Vinícius Garcia
5c1891558e Update README.md 2017-04-19 14:15:38 -03:00
Vinícius Garcia
3fe02bed64 Update README.md 2017-04-19 14:12:11 -03:00
Vinícius Garcia
e8a4a38997 Update README.md 2017-04-19 14:01:16 -03:00
Vinícius Garcia
a69330cf8c Update README.md 2017-04-19 13:56:46 -03:00
Vinícius Garcia
9944390e60 Update README.md 2017-04-19 13:56:01 -03:00
Vinícius Garcia
c311e6ea8f Update README.md 2017-04-19 13:52:45 -03:00
Vinícius Garcia
f791a7c411 Update README.md 2017-04-19 13:51:07 -03:00
Vinícius Garcia
e612e0bd8c Update README.md 2017-04-19 13:49:47 -03:00
Vinícius Garcia
f068454682 Update README.md 2017-04-19 13:44:41 -03:00
Vinícius Garcia
26042632dc Fix Travis and License markdown code 2017-04-19 13:21:02 -03:00
Vinícius Garcia
5f2633abe8 Fix broken JSpy link #47 2017-01-17 15:35:09 -03:00
Vinícius Garcia
1a4049f4cb Update Travis Links 2016-10-26 10:19:48 -03:00
Vinícius Garcia
3fadca0495 Add a reference to our Wiki 2016-10-26 10:07:49 -03:00
Vinícius Garcia
2c6cee494e Update title to CParser 2016-10-25 19:51:29 -03:00
Vinícius Garcia
9bdba2aa4c Decouple operations (#30)
* Replace all `scope->find(name)` calls for `scope[name]`

These calls were on the built-in functions declarations, and
were used to recover the function arguments.

This change was made because the find() function was not
the correct tool for the job; The find() function looks
on the entire prototype chain of the scope for the variable,
and this is not needed on this case since the function arguments
will always be on the most local scope of the function.

This refactor makes the code more readable and sightly more
efficient.

* Rename `argsList` to `args_t`

* Convert Function::name from a literal string to a getter function

* Simplify CppFunction constructor

This commit adds a new constructor to CppFunction that expects
a `const args_t` as argument.

This way it is possible to define a new function without having
to explicitly give how many arguments it receives, i.e.:

  CppFunction(&my_func, {"arg1", "arg2", "arg3"}, "my_func");

instead of:

  CppFunction(&my_func, 3, {"arg1", "arg2", "arg3"}, "my_func");

* Add new constructor to undefined_operation

* Major refactor: decouple operations from calculator class

Before this commit every new operation would be hard coded
inside the static calculator::calculate() function.

Now it is not required anymore. To add a new operation do as follow:

```C++
struct my_operation : public Operation {
  // This describes the operands expected by this operation:
  const opID_t getMask() { return build_mask(INT, REAL); }

  // This describes the operation behavior:
  TokenBase* exec(TokenBase* left, std::string op, TokenBase* right) {
    if (/* not satisfy condition */) {
      // Ignore this operation and keep looking for a match:
      return 0;
    }

    if (/* erro condition*/) {
      throw my_error("...");
    }

    // if its all ok:
    TokenBase* result = some_code(left, right);

    // Consume both Tokens:
    delete left;
    delete right;

    // Return:
    return result;
  }
// Don't forget to declare and static instance:
} my_operation;

struct my_Startup {
  my_Startup() {
    // Register the operation on the default opMap:
    opMap_t& opMap = calculator::default_opMap();
    opMap[","].push_back(&my_operation);
    opMap[ANY_OP].push_back(&my_other_operation);
  }
} my_Startup;
```

The default operations are on the operations.cpp file.
To declare your own default operations, just make sure
to not link your code to `operations.o`, and add your
own description of your operations wherever you want.

In future work:

* Make an optional Operation function that works with
  packTokens, so the final user will not need to worry about
  memory management.

* Make it possible to overwrite the default_opMap by extending
  the calculator class, creating any number of different
  calculators as required.

* Add `release` recipe to Makefile

The `make release` will output 2 `.o` files:

- core-shunting-yard.o
- operations.o

The `core` bin contain an working calculator with
no defined operations except for asignment and function call,
that are still hard-coded on the system.

The `operations` bin contain all the other default operations.

If a user wants to define adhoc operations, include only
the `core` binary.

* Add tests for declaring adhoc operations

* Simplify Operation definition

Now there are 2 Operation classes: `BaseOperation` which is the
old class with a new name and the `Operation` that inherits from
the former.

The `exec()` pure virtual function from BaseOperation has the following
signature: TokenBase* exec(TokenBase*, std::string, TokenBase*);

The `exec()` pure virtual function from Operation has the following
signature: packToken exec(packToken, std::string, packToken);

This makes the usage of the Operation class easier since
hiding from the user the required memory management.

Advanced users can declare new operations using the `BaseOperation`
class. Working with TokenBase* is more efficient since
it does not require to call the packToken constructor and
destructor several times.

* Make possible to define adhoc opPrecedence.

Now it is possible to define your own operator precedence map
the same way it is done for operations.

The definition of the operator precedence was now moved to the
file `operations.h` and is initialized just before the operations.

If the user want to define its own operations and precedence map,
he just need to compile his code without the file `operations.o`.
After that he can define his own adhoc behavior in a file on his
project.

It is also possible to overwrite opPrecedence on a child calculator
class. The user just need to overwrite the function "opPrecedence()".

* Update README with new features.
2016-10-04 13:37:35 -04:00
Brandon Amos
72da211394 Update README.md 2016-09-19 14:54:00 -04:00
Vinícius Garcia
743010af73 Update README.md (#29)
* Remove ambiguity from TokenList::operator[]

* Update README.md with all new features
2016-09-16 16:26:50 -04:00
Vinícius Garcia
a61a241241 Almost a complete programming language core (#24)
* Fix bug of treating characters `'` and `"` as operators

* Fix print() so it can print anything not only strings

* Remove `print()` tests so stdout is not polluted

* Make the expression `pow(2,+2) and pow(2,-2) work

* Add literals: None, True and False, with capital letters

* Replace `^` operator for `**`

* Improve the reference resolving mechanism

* Fix Travis build

* Major refactor: Add prototypical inheritance

This commit makes the default Map class compatible
with prototypical inheritance.

The old TokenMap_t was replaced by the class TokenMap.

Since all TokenMaps now work as a hierarchy of maps,
the Scope class was also replaced by the the TokenMap class.

The old Scope::default_global()
was replaced by TokenMap::default_global()

Since now all maps have a parent map, a static Map was created
to occupy the top of the hierarchy: TokenMap::base_map()

By default any map inherits from TokenMap::default_global().

If you want an empty TokenMap, use instead the Object class.

* Make it possible for evaluation to return a reference

Before this commit all references were resolved before
being returned, and there was no way to get to them.

Now the calculator::eval() function has an optional
argument: `keep_refs`, which is set by default to false.

If set to true this function will return the result
of its evaluation without resolving its reference.

It can be used to parse names like: a.b.c['d'],
and allow the library user to add or edit information
of this map.

* Fix Travis build

* Prepare to add more complex types

* Refactor RefToken to work with complex types

* Add `this` to functions local scope

* Add support to type specific functions, e.g.: 'str'.len()

Before this commit it was not possible to link functions
to objects other than MAPs, so it would not be possible
to add functions such as:

- `'str'.len()` for the STR type.
- or `(1,2,3).join(',')` for the TUPLE type.

Now it is possible. This feature is possible because of 2
new structures:

1. Now we have a static std::map<tokType, Object> that
   maps a Token type to an Object which is a packToken map.
2. The `this` attribute that was added on the last commit.

Now everytime the calculator::calculate function finds
any type of Token (except MAP) followed by a '.' operator
and a string `str`.
It will search on the tokType map to see if there is a
function named `str` for this type.

* Major commit: Add reference counting to Map instances

This commit add a reference counting system for automatically
deleting maps that are not used anymore. This will allow our
users to declare their own maps and use them with no risk
or memory leaking.

This commit adds the template pack<T> that wraps any type T.
The pack<TokenMap>() instance was typedef'd as packMap.

A packMap can me initialized with no arguments and it will
manage a pointer to a TokenMap*. If you copy a packMap
the reference counter is increase, if you delete a packMap
it is decreased. If it gets to 0, the original TokenMap* is
also deleted.

Secondary changes:

- The TokenMap constructor with no arguments now sets its parent
  attribute to TokenMap::base_map instead of TokenMap::default_global.

It means that a TokenMap() will no have access by default to variables
declared on TokenMap::default_global. But instead will be really empty.

- The Object class that inherited from TokenMap was changed:

  * Now it sets TokenMap::default_global as it parent by default.
  * Now it is named `class GlobalScope` which is a more consistent name.

So if you want to declare a Map with access to the global scope
now you should use the `GlobalScope class`, if you just want
an empty map you should use the `TokenMap class`.

If you want to insert a map into a TokenMap or GlobalScope,
you should declare it as a packMap(), e.g.:

  GlobalScope map;
  map["sub_map"] = packMap()

But it would also work if you did like this:

  GlobalScope map;
  TokenMap sub_map;
  map["sub_map"] = packMap(&sub_map);

* Make packToken::asMap and ::asString to return by reference

* Fix bug on assignment operation on maps

* Add support for `closures` just like JavaScript.

This change required that all the references to `TokenMap*` on
the calculator class were replaced by `packMap`.

This was an important change because now the local scope of any
function defined within a calculator expression will keep existing
for as long as there is a reference of it.

This allow this language to support de definition of `closures` by
our users, just like JavaScript does.

* Fix Travis Build

* Add the LIST type and the packList class

Now we finally have lists! They are objects like maps and
are passed by reference, implying they also need memory management.

To solve this the same template: `pack<T>` class is used to manage
memory with a reference counting system.

----

Please note that the reference counting system we use has a problem:
It does not get rid of cycles, e.g.: a[0] = b; b[0] = a;

We need to plan a way to check for existence of unreachable
memory from time to time.

(Note that this problem also exist with maps)

----

Lists also have 3 functions associated with them that are
tested and work:

- list.push(item)
  * add an item to the end of the list.
- list.pop() or list.pop(idx)
  * remove an item from the end of the list
  * or remove an item whose index is idx.
- list.len()
  * return the current length of the list.

They are all tested and work.

* Small refactor: Move TokenMap and TokenList to objects.{cpp,h}

* Fix Travis Build

* Add multiple arguments support for functions

Now every function will receive at least 2 arguments:

- "this", but thats old news
- "arglist" that contain all the extra args that did not
  match the normal required arguments of the function.

This is different from the "arguments" Tuple of Javascript in
that it won't contain all the arguments, but instead only
the one that were not named by the function argument names.

It is simillar to the *largs arguments of Python.

In future we might change this behavior to work even more like
Python, i.e. only pass this argument if the user require it
with an argument named "*largs" or something with an asterisk
on the beginning.

Also:

- Add the sum() built-in function to test this feature.
- Improve the print() built-in function with this feature.

* Fix bug with parentesis expressions: list.push((1,2))

* Add built-in map and list constructors

* Add the Iterator abstract class

This class requires the implementation of:

- packToken* next()
- void reset()

Thus allowing any subclass to be iterable.

It should also be used as return type for generator
functions in future.

Currently the TokenMap class and the TokenList class inherit from
the Iterator class.

The Tuple class should also inherit from it in future.

* Make packMap and packList inherit from Iterator class

* Revert "Make packMap and packList inherit from Iterator class"

Reverting this last commit since it made things
more complicated, less efficient and didn't really had
the effect I wanted.

This reverts commit 8a005fb4990c70c4909c543191d6282bacd01b7b.
2016-07-30 16:57:06 -04:00
Vinícius Garcia
e4ebe719cd Some refactoring and bug fixing (#22)
* Remove redundant calculator::compile(const char*, OppMap_t)

* Add support for calculator to work as a slave parser

As a slave parser I mean it will parse until a specified delimiter
and report back the pointer of where it has stopped.

This way another class can the calculator constructor to parse
or evaluate a sequence of expressions.

* Add static calculator::str() implementation for help debug

* Add an arguments validation check at calculator::toRPN()

* Minor refactor: Move calculator::scope_empty to Scope::empty

* Set Function string argument to `const char*`

* Update files to satisfy Travis CI rules

* Move Function::default_functions to Scope::default_global

The TokenMap_t Function::default_functions was used as base
map for all Scope instances, and represented the global scope.

It is more coherent to keep it with the name default_global
and defined as a static member of Scope class.

Now any other class that needs to add something to the global
scope can just use Scope::default_global.

* Make packToken::str(TokenBase*) accept const argument

* Fix memory leak problem on Reference Token management

* Prevent bug called "static initialization order fiasco"

This bug is better explained here:

 - https://isocpp.org/wiki/faq/ctors#static-init-order

Short version:

It is caused when the compiler initializes static objects in
a random order. And one of these objects depend on the initialization
of another one.

In our project the singleton class Function::Startup depends
on the TokenMap_t Scope::default_global.

It might cause no bug if we are lucky with the init order the
compiler chooses. But might cause big problems otherwise.

This commit fix this problem so we don't need to depend on lucky.

* Optimize calculator::toRPN loop

* Update README.md features section

* Update quote escaping for string literals

Now the user can type \\n and it will be converted to \n.

It also works for \\t, but nothing happens with other characters
like: \\f or \\o

Also add more tests for the string section.

* Fix bug on string escaping

* Add RAII_TokenQueue_t class to facilitate rpn management

Since C++ does not allow a finally block it is hard to
be sure something will be dealloc'd even when exceptions are
thrown.

The function calculator::calculate(char* expr, ...)
needed to have a catch for each possible exception type thrown
by its internal function calls (where a single finally
block would have done the job)

The only alternative encouraged by C++ is the RAII idiom
so the RAII_TokenQueue_t was built.

This new class should be used with care, but it has reduced the
number of explicit calls to cleanRPN which is good.

* Add valgrind check (make check) to Makefile

* Fix () operator so m.a(arg) works

* Fix `print()` so that it will call `print(None)`

* Add string representations to types FUNC and TUPLE
2016-07-18 12:11:02 -04:00
Brandon Amos
fc18d4bd22 Update example in README. 2016-06-27 16:22:28 -04:00
Brandon Amos
922dc7f10f Remove future work from README. 2016-06-27 15:13:15 -04:00
Brandon Amos
501a83b129 Correct link. 2016-06-27 15:12:05 -04:00
Brandon Amos
8c214ea43b Formatting. 2016-06-27 15:11:37 -04:00
Brandon Amos
bb5c41cbd7 Formatting. 2016-06-27 15:11:23 -04:00
Brandon Amos
ec5051f250 Move acknowledgements and authors from the code to the README. 2016-06-27 15:11:01 -04:00
Brandon Amos
4481a234a2 README: Link to @awsteiner's o2scl. 2016-03-22 15:57:26 -04:00
Vinícius Garcia
3f1adf454b Update README.md 2016-02-29 05:44:42 -03:00
Brandon Amos
19b56e6629 Add Travis badge to the README. 2016-02-26 17:57:37 -05:00
Vinícius Garcia
9750682c9c Modify calculate() for lazy variable evaluation
Now it is possible to precompile an expression without
binding all its variable values. And bind them only
when the expression will be evaluated.

This way different evaluations of a single expression
may have different sets of variables.

This method optimizes the case when you need to re-evaluate
a single equation several times in different contexts.
2016-02-26 01:24:57 -03:00
Brandon Amos
0faa25ea56 Remove biicode and CI. 2016-02-09 11:39:15 -05:00
Brandon Amos
813a6b24c2 Move badge to the top of the README. 2015-10-18 13:13:25 -04:00
mariadeanton
a606f18161 added CI files 2015-02-16 15:01:24 +01:00
mariadeanton
8196b7424f added unobtrusive changes to work with biicode 2015-02-09 15:32:43 +01:00
Brandon Amos
1aff34f526 Add link to biicode. 2014-12-30 11:16:03 -05:00
Brandon Amos
97255a665c Typo. 2014-12-19 11:21:37 -05:00
Brandon Amos
12c1536af0 Fix code blocks. 2014-09-13 18:45:04 -04:00
Brandon Amos
d64d6cd4c9 Update README. 2014-09-13 18:44:21 -04:00