Skip to content

Commit

Permalink
Replace table iterations with new for-loop syntax
Browse files Browse the repository at this point in the history
Didn't even know this existed!
  • Loading branch information
sdt committed Mar 28, 2015
1 parent 79f8ce3 commit f01c268
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 31 deletions.
16 changes: 7 additions & 9 deletions cpp/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,16 @@ static malValuePtr readAtom(Tokeniser& tokeniser)
{ "~@", "splice-unquote" },
{ "~", "unquote" },
};
const ReaderMacro* macroTableEnd = macroTable + ARRAY_SIZE(macroTable);

struct Constant {
const char* token;
malValuePtr value;
};
Constant constTable[] = {
Constant constantTable[] = {
{ "false", mal::falseValue() },
{ "nil", mal::nilValue() },
{ "true", mal::trueValue() },
};
const Constant* constTableEnd = constTable + ARRAY_SIZE(constTable);

String token = tokeniser.next();
if (token[0] == '"') {
Expand All @@ -200,14 +198,14 @@ static malValuePtr readAtom(Tokeniser& tokeniser)
// Note that meta and value switch places
return mal::list(mal::symbol("with-meta"), value, meta);
}
for (Constant* it = constTable; it != constTableEnd; ++it) {
if (token == it->token) {
return it->value;
for (auto &constant : constantTable) {
if (token == constant.token) {
return constant.value;
}
}
for (ReaderMacro *it = macroTable; it < macroTableEnd; ++it) {
if (token == it->token) {
return processMacro(tokeniser, it->symbol);
for (auto &macro : macroTable) {
if (token == macro.token) {
return processMacro(tokeniser, macro.symbol);
}
}
if (std::regex_match(token, intRegex)) {
Expand Down
2 changes: 0 additions & 2 deletions cpp/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <exception>
#include <map>

#define ARRAY_SIZE(a) (sizeof(a)/(sizeof(*(a))))

class malEmptyInputException : public std::exception { };

class malValue : public RefCounted {
Expand Down
4 changes: 2 additions & 2 deletions cpp/step4_if_fn_do.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ static const char* malFunctionTable[] = {
};

static void installFunctions(malEnvPtr env) {
for (int i = 0; i < ARRAY_SIZE(malFunctionTable); i++) {
rep(malFunctionTable[i], env);
for (auto &function : malFunctionTable) {
rep(function, env);
}
}

Expand Down
4 changes: 2 additions & 2 deletions cpp/step5_tco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ static const char* malFunctionTable[] = {
};

static void installFunctions(malEnvPtr env) {
for (int i = 0; i < ARRAY_SIZE(malFunctionTable); i++) {
rep(malFunctionTable[i], env);
for (auto &function : malFunctionTable) {
rep(function, env);
}
}

Expand Down
4 changes: 2 additions & 2 deletions cpp/step6_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ static const char* malFunctionTable[] = {
};

static void installFunctions(malEnvPtr env) {
for (int i = 0; i < ARRAY_SIZE(malFunctionTable); i++) {
rep(malFunctionTable[i], env);
for (auto &function : malFunctionTable) {
rep(function, env);
}
}

Expand Down
4 changes: 2 additions & 2 deletions cpp/step7_quote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ static const char* malFunctionTable[] = {
};

static void installFunctions(malEnvPtr env) {
for (int i = 0; i < ARRAY_SIZE(malFunctionTable); i++) {
rep(malFunctionTable[i], env);
for (auto &function : malFunctionTable) {
rep(function, env);
}
}

Expand Down
8 changes: 4 additions & 4 deletions cpp/step8_macros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ static const char* macroTable[] = {

static void installMacros(malEnvPtr env)
{
for (int i = 0; i < ARRAY_SIZE(macroTable); i++) {
rep(macroTable[i], env);
for (auto &macro : macroTable) {
rep(macro, env);
}
}

Expand All @@ -298,8 +298,8 @@ static const char* malFunctionTable[] = {
};

static void installFunctions(malEnvPtr env) {
for (int i = 0; i < ARRAY_SIZE(malFunctionTable); i++) {
rep(malFunctionTable[i], env);
for (auto &function : malFunctionTable) {
rep(function, env);
}
}

Expand Down
8 changes: 4 additions & 4 deletions cpp/step9_try.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ static const char* macroTable[] = {

static void installMacros(malEnvPtr env)
{
for (int i = 0; i < ARRAY_SIZE(macroTable); i++) {
rep(macroTable[i], env);
for (auto &macro : macroTable) {
rep(macro, env);
}
}

Expand All @@ -341,8 +341,8 @@ static const char* malFunctionTable[] = {
};

static void installFunctions(malEnvPtr env) {
for (int i = 0; i < ARRAY_SIZE(malFunctionTable); i++) {
rep(malFunctionTable[i], env);
for (auto &function : malFunctionTable) {
rep(function, env);
}
}

Expand Down
8 changes: 4 additions & 4 deletions cpp/stepA_mal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ static const char* macroTable[] = {

static void installMacros(malEnvPtr env)
{
for (int i = 0; i < ARRAY_SIZE(macroTable); i++) {
rep(macroTable[i], env);
for (auto &macro : macroTable) {
rep(macro, env);
}
}

Expand Down Expand Up @@ -352,7 +352,7 @@ static const char* malFunctionTable[] = {
};

static void installFunctions(malEnvPtr env) {
for (int i = 0; i < ARRAY_SIZE(malFunctionTable); i++) {
rep(malFunctionTable[i], env);
for (auto &function : malFunctionTable) {
rep(function, env);
}
}

0 comments on commit f01c268

Please sign in to comment.