-1

I'm asking this question mostly because I'm curious.

Mostly for fun, I'm trying to develop a basic interpreter & shell in C++. Now, I already developed a basic shell - I can CD, dir, and whatnot, and I'm asking this mostly in order to learn. :)

As a part of the interpreter, I want to save user-declared variables. I.e: param x = 'siosidf'; or param x = 5;.

I also would like to be able to preform pure calculations, i.e 72*32 or 23 + 82 * 2 should output 2304 or 187, recpectivley.

Note that I also want to be able to apply operators on variables, like so:

param x = 72;

x = x + 23;

print x;

Needs to output 95.

My question has two parts:

First, how would you (doesn't have to be actual code, but if you do it, please do it in C++ :) ) implement the variable saving system? (Please also explain why, since I'm doing it in order to learn. :) )

Second, how would you tell apart different calculations/declarations/calls and preform them in all their different forms effectively? (Spaces, such as 72*83 vs. 72 * 83.)

Biffen
  • 6,249
  • 6
  • 28
  • 36
A. Abramov
  • 1,823
  • 17
  • 45
  • 5
    You might want to find a good book or course on writing interpreters. – chris Feb 06 '15 at 20:18
  • 1
    This is a neat project, but this question is not a StackOverflow FAQ-type question :( – Mooing Duck Feb 06 '15 at 20:19
  • @MooingDuck why so? It really is about programming & concepts, I think... If it doesn't fit in here, where should i ask it? – A. Abramov Feb 06 '15 at 20:21
  • Regarding the first question, if only there was some to *map* something like a string to a value... ;) – Some programmer dude Feb 06 '15 at 20:21
  • Probably belongs on [programmers.stackexchange.com](https://programmers.stackexchange.com/tour) – glenn jackman Feb 06 '15 at 20:24
  • Might like to look at [this](https://code.google.com/p/picoc/) – dwn Feb 06 '15 at 20:25
  • 1
    @A.Abramov _"why so?"_ Because it's much too broad for possible solutions. You can use e.g. a `std::map` to keep variable values stored, but a lot of better, more generic solutions will be available. – πάντα ῥεῖ Feb 06 '15 at 20:26
  • 1
    @glennjackman - This looks like more of an implementation issue and would be better answered here on StackOverflow. –  Feb 06 '15 at 20:27
  • @dwn I`m trying to take a look at the source, but for some reason it throws me a 404. Do you have any idea where could i take a look at it? Thank you :) – A. Abramov Feb 06 '15 at 20:28
  • @A.Abramov: What you're looking for is a parser. (The people suggesting map are not fully grasping the problem I think). The simplest to understand is a [Recursive Decent Parser](http://en.wikipedia.org/wiki/Recursive_descent_parser) – Mooing Duck Feb 06 '15 at 20:36
  • @A.Abramov https://picoc.googlecode.com/files/picoc-2.1.tar.bz2 – dwn Feb 06 '15 at 20:48

1 Answers1

3
  1. Use a map.
  2. Use a parser. Boost.Spirit would be a good choice.
StenSoft
  • 9,369
  • 25
  • 30
  • I was writing a lengthy answer, but this sums it up. Boost spirit is easy to use and will do the job, but to be old-school you should consider flex and bison as well. Anyway, this answer from @StenSoft is correct. – Robert Jørgensgaard Engdahl Feb 06 '15 at 20:36
  • Both sound like good ideas; However, this project is not for a product; i'd like to implement the parser myself. I'll mark this as the answer in a hour if nobody else answers. Thank you :) – A. Abramov Feb 06 '15 at 20:36
  • @A.Abramov if you really want to implement the parser yourself you need this: [The dragon book](http://www.amazon.com/Compilers-Principles-Techniques-Tools-Edition/dp/0321486811) It's the bible of compilers. But be warned: it very advanced. Not suited for beginners or intermediates. Although you need just some of the chapters in it. Mainly those around LL and/or LR Parsers. – bolov Feb 06 '15 at 20:41
  • If you want to implement it yourself, consider checking [this answer](http://stackoverflow.com/a/3085141/4538344). – StenSoft Feb 06 '15 at 20:43