New answers tagged

0

Most people don't know that these function declarations int foo(); int bar() { } do not declare a prototype for foo respectively bar. In consequence, this is a legal implementation of foo: int foo(int a) { } And this is a likely illegal call to bar, however the compiler can't warn: bar(42); To provide a prototype, add void inside the parenthesis to ...


5

+1 on specifying argument code: str and return type -> str. I implemented a class seen below which has the advantage of seeing chars and their corresponding functions at a glance. Adding new chars if need be is incredibly easy. self.actions = { '>': self.greater_than, '<': self.less_than, '+': self....


2

Naming Functions do things, so the name for a function should normally be a verb. In your case, the function should be something like bf_interpret, not ...interpreter. I'd also at least consider making bf a namespace, so the call in main would be something like: bf::interpret(s); Prefix operators Lacking a reason to do otherwise, I'd prefer to use ...


5

Bug The only issue I see is that the data array is never expanded. As a result any value of ptr that is not zero will cause undefined behavior. What you want to do is give a fixed but reasonable size for the data. std::size_t const maxDataSize = 10000; ... std::vector<int> values; values.resize(maxDataSize); Then make sure when you access ...


8

All of your if char == '>': ptr += 1 and similar checks should use elif after the first check. By using if for all of the checks, you're forcing them all to run, even once you've found a match. This is wasteful because the checks are necessarily exclusive of each other. Once one check is true, none of the other checks can be. For example, you should have: ...


1

Expanding on Edward's point with "const": It's good practice to properly qualify all constant data, not so much for optimization (see http://www.gotw.ca/gotw/081.htm) as for displaying the author's intent, and for preventing errors. const is infectious, so you'll have to change all function signatures and pointer declarations operating on the constant ...


41

This is a pretty reasonable start on a simple interpreter. Edward's suggestions are all good; a few additional suggestions: interpret("+++++++++++++[->.... Please break up that long line. C allows you to break up literal strings "like " "this." void goToLoopEnd(char** ip) { ... void goToLoopStart(char** ip) { ... If you wrote these instead as char *...


25

Here are some things that may help you improve your program. In all, it seems to be nice, straightforward code that does what it needs to do. Good start! Use only required #includes The code has #include <stdbool.h> but doesn't use booleans. It also appears that nothing from <stdlib.h> is used either. Only include files that are actually ...


2

Use X-Macro to construct parallel enum and array As the number of items increases, so does the effort required to keep the parallel constructions in sync. enum Combinator { POP, DUP, SWAP , FLIP, ID, QUOTE , UNQUOTE, UNKNOWN }; static const char Str_Combs[][8] = { "pop", "dup", "swap" , "flip", "id", "quote" , "unquote" }; Instead,...


Top 50 recent answers are included