tyill / interpreter Goto Github PK
View Code? Open in Web Editor NEWSimple and fast tiny interpreter. Only one header and source file.
License: MIT License
Simple and fast tiny interpreter. Only one header and source file.
License: MIT License
Hi there,
I found I can support negative numbers like:
-> summ(-1,24-23);
-> OK: 0
-> summ(-1,24-12);
-> OK: 11
->
by adding if (leftOpd == "") leftOpd = "0";
to the "-" operator, and dot / minus support to isNumber()
bool isNumber(const std::string &s)
{
for (auto c : s)
{
if ( (!std::isdigit(c)) && (c != '.') && (c != '-'))
{
return false;
}
}
return !s.empty();
}
ir->addOperator(
"-", [](string &leftOpd, string &rightOpd) -> string {
if (leftOpd == "") leftOpd = "0"; // negative number, such as "-1" so we deduct right from 0
if (isNumber(leftOpd) && isNumber(rightOpd))
return to_string(stoi(leftOpd) - stoi(rightOpd));
else
return "0";
},
1);
are there any side effects?
Also I've had some initial success with supporting floats like 1.22
, is there already support I am missing?
Cheers,
Hip
I must be doing something fundamentaly wrong here...
Interpreter *pIr = this;
// help
addFunction("help", [pIr](const vector<string> &args) -> string
{
std::string result = "Available Functions:\n";
for (auto & a : pIr->allEntities()){
if (a.type == Interpreter::EntityType::FUNCTION) {
result + a.name;
result + "\n";
}
}
return result;
});
returns ""
I want to list the functions defined in Interpreter... I have several, but help();
always returns empty.
Hi Tyill,
I came back to ask a question, and can see you've done a bunch of work since I was last here!
I wanted to ask about a way to user modify the language, if possible, so simplify to BASICish or perhaps more of a shell thing...
Thanks to your introspection, I can
store(preset(1.2.3));
for example.
But how to interpret what the user might input like store preset 1.2.3
where it is known:
store
is a Function, and
preset
is a Function, also the argument to store
, and
1.2.3
is the argument to preset
... so no (()) required?
Hi Tyill,
I am wondering, not being fluent, can there be multiple instances of Interpreter?
My situation is I have a vector, of class t_user
which keeps track of a individual connected users....
A single connection [t_user] has a Programmer
that contains an instance of Interpreter
:
[pseudocode]
class Programmer {
public:
Interpreter cmdline;
};
class t_user {
public:
std::string name = "root";
Programmer programmer; // each user has own programmer / interpreter / environment
};
std::vector<t_user> Users;
void main(void){
t_user tmp_user;
tmp_user.name = "user1"
Users.push_back(tmp_user); // add a t_user to Users
// Users[0] we just pushed
std::string input = "$a = summ(1, 2);"
Users[0].programmer.cmdline.cmd(input); // <------- complains here
}
Compiler throws a fit...
In file included from /usr/include/x86_64-linux-gnu/c++/9/bits/c++allocator.h:33,
from /usr/include/c++/9/bits/allocator.h:46,
from /usr/include/c++/9/deque:64,
from /home/hip/dev/interpreter/user.cpp:10:
/usr/include/c++/9/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = t_user; _Args = {const t_user&}; _Tp = t_user]’:
/usr/include/c++/9/bits/alloc_traits.h:482:2: required from ‘static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = t_user; _Args = {const t_user&}; _Tp = t_user; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<t_user>]’
/usr/include/c++/9/bits/stl_vector.h:1189:30: required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = t_user; _Alloc = std::allocator<t_user>; std::vector<_Tp, _Alloc>::value_type = t_user]’
//home/hip/dev/interpreter/user.cpp:27:30: required from here
/usr/include/c++/9/ext/new_allocator.h:145:20: error: use of deleted function ‘t_user::t_user(const t_user&)’
145 | noexcept(noexcept(::new((void *)__p)
| ^~~~~~~~~~~~~~~~~~
146 | _Up(std::forward<_Args>(__args)...)))
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It's very much probably not your fault, I am playing with multi-user interpreters where users have own instances - but would introduce some globals for them to share also.
I just wondering multiple instances are possible or if my stupid fault?
Is there a mechanism or suggestion how that that the operator can know it's parent function?
eg: lettuce(1->24);
ir->addOperator( // a selection
"->", [](string &leftOpd, string &rightOpd) -> string {
if (isNumber(leftOpd) && isNumber(rightOpd)){
uint a = stoi(leftOpd);
uint b = stoi(rightOpd);
for (int cnt = a; cnt <= b; cnt++){
printf("select %s %d \n\r", ___parent_function_name___, cnt);
}
}
return ""; // return nothing - ignore?
},
0);
would output...
select lettuce 1
select lettuce 2
select lettuce 3
...
select lettuce 22
select lettuce 23
select lettuce 24
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.