Skip to content

Commit

Permalink
changed translator output rpn list to QList class
Browse files Browse the repository at this point in the history
updated CMakeLists.txt to support using Qt;
also required changing the list element pointer in the done stack to a
a rpn item pointer (doesn't need access to list), the list element
pointer in the command stack to an index (needs to know where in the
list to insert tokens), and the list element pointer array in the rpn
item structure to a rpn item pointer array (doesn't need acces to list)
  • Loading branch information
thunder422 committed Oct 27, 2012
1 parent da0014b commit 34cbd66
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 102 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Expand Up @@ -41,11 +41,16 @@
# 2012-10-24 detect the CMAKE_BUILD_TYPE environment variable and set the
# cmake variable if not already set, then report the build type
# 2012-10-25 added configuration of new memtest script on non-Windows builds
# 2012-10-26 added changed for Qt libraries

cmake_minimum_required(VERSION 2.8)

project(ibcp)

# find Qt package
find_package(Qt4 REQUIRED QtCore)
include(${QT_USE_FILE})

# check GCC versionn
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
OUTPUT_VARIABLE GCC_VERSION
Expand Down Expand Up @@ -163,6 +168,7 @@ add_executable(ibcp
${ibcp_INCS}
${ibcp_SRCS}
)
target_link_libraries(ibcp ${QT_LIBRARIES})

# make linker link in static libgcc.a and libstrc++.a
if (GCC_VERSION VERSION_GREATER 4.4)
Expand Down
25 changes: 15 additions & 10 deletions ibcp.h
Expand Up @@ -292,10 +292,16 @@
//
// 2012-10-25 added new function Token::isNull() to properly check for the
// null token (not all token types have a valid code)
// 2012-10-27 converted translator output rpn list from List class to QList
// also required changes to done_stack (element list pointer to
// rpn item pointer), cmd_stack (element list pointer to index
// into output rpn list), and RpnItem.operands[] (element list
// pointer array to RpnItem pointer array)

#ifndef IBCP_H
#define IBCP_H

#include <QList>
#include "string.h"
#include "list.h"
#include "stack.h" // 2010-04-02: added for Stack
Expand Down Expand Up @@ -849,7 +855,7 @@ struct RpnItem;
struct CmdItem {
Token *token; // pointer to command token
int flag; // 2010-06-01: generic flag for command use
List<RpnItem *>::Element *element; // 2011-03-01: pointer for command user
int index; // index into output list for command use
};

// 2010-06-13: added token pointer argument to command handlers
Expand Down Expand Up @@ -1104,10 +1110,9 @@ class Parser {
struct RpnItem {
Token *token; // pointer to token
int noperands; // number of operands
List<RpnItem *>::Element **operand; // array operand pointers
RpnItem **operand; // array of operand pointers

RpnItem(Token *_token, int _noperands = 0,
List<RpnItem *>::Element **_operand = NULL)
RpnItem(Token *_token, int _noperands = 0, RpnItem **_operand = NULL)
{
token = _token;
noperands = _noperands;
Expand All @@ -1124,7 +1129,7 @@ struct RpnItem {
}

// 2010-05-22: function to set operands without allocating a new array
void set(int _noperands, List<RpnItem *>::Element **_operand)
void set(int _noperands, RpnItem **_operand)
{
noperands = _noperands;
operand = _operand;
Expand All @@ -1136,7 +1141,7 @@ struct RpnItem {
// 2010-05-15: changed output list and done stack from Token* to RpnItem*
class Translator {
Table *table; // pointer to the table object
List<RpnItem *> *output; // pointer to RPN list output
QList<RpnItem *> *output; // pointer to RPN list output
// 2010-05-28: changed hold_stack and done_stack from List to Stack
// 2011-01-13: changed hold stack to include first token pointer
struct HoldStackItem {
Expand All @@ -1146,7 +1151,7 @@ class Translator {
Stack<HoldStackItem> hold_stack;// operator/function holding stack
// 2011-01-15: changed done stack to include first and last token pointers
struct DoneStackItem {
List<RpnItem *>::Element *element; // RPN item element pointer
RpnItem *rpnItem; // pointer to RPN item
Token *first; // operator token's first operand pointer
Token *last; // operator token's last operand pointer
};
Expand Down Expand Up @@ -1186,7 +1191,7 @@ class Translator {
void start(bool _exprmode = false)
{
exprmode = _exprmode; // 2010-06-06: save flag
output = new List<RpnItem *>;
output = new QList<RpnItem *>;
state = Initial_State;
// 2010-04-11: initialize mode to command
// 2010-04-16: start in expression mode for testing
Expand All @@ -1200,9 +1205,9 @@ class Translator {
}
// 2010-04-04: made argument a reference so different value can be returned
TokenStatus add_token(Token *&token);
List<RpnItem *> *get_result(void) // only call when add_token returns Done
QList<RpnItem *> *get_result(void) // only call when add_token returns Done
{
List<RpnItem *> *list = output;
QList<RpnItem *> *list = output;
output = NULL;
return list;
}
Expand Down
61 changes: 33 additions & 28 deletions test_ibcp.cpp
Expand Up @@ -189,6 +189,8 @@
//
// 2012-10-24 deleted the rpnlist object after deleting all the list members
// (to fix a memory leak)
// 2012-10-27 changed translator output from List class to QList
// created separate print_output() so can be used for debugging

#include <stdio.h>
#include <stdlib.h>
Expand All @@ -198,6 +200,7 @@ void parse_input(Parser &parser, Table *table, const char *testinput);
void translate_input(Translator &translator, Parser &parser, Table *table,
const char *testinput, bool exprmode = false);
bool print_token(Token *token, Table *table, bool tab);
void print_output(const char *header, QList<RpnItem *> output, Table *table);
bool print_small_token(Token *token, Table *table);
void print_error(Token *token, const char *error);
void print_token_leaks(Table *table, const char *testinput);
Expand Down Expand Up @@ -422,39 +425,17 @@ void translate_input(Translator &translator, Parser &parser, Table *table,
if (status == Done_TokenStatus)
{
// 2010-05-15: change rpn_list from Token pointers
List<RpnItem *> *rpn_list = translator.get_result();
List<RpnItem *>::Element *element;
printf("Output: ");
QList<RpnItem *> *rpnList = translator.get_result();
// 2010-05-15: added separate print loop so operands can also be printed
// 2011-01-29: modified for updated List class functions
for (element = rpn_list->first(); element != NULL;
rpn_list->next(element))
{
print_small_token(element->value->token, table);
//print_token(token, table);
if (element->value->noperands > 0)
{
char separator = '[';
for (int i = 0; i < element->value->noperands; i++)
{
printf("%c", separator);
print_small_token(element->value->operand[i]->value->token,
table);
separator = ',';
}
printf("]");
}
printf(" ");
}
print_output("Output", *rpnList, table);
// 2010-03-21: corrected to handle an empty RPN list
// 2011-01-29: rewrote to remove last item instead of first item
RpnItem *rpn_item;
while (rpn_list->pop(&rpn_item))
while (!rpnList->isEmpty())
{
delete rpn_item;
delete rpnList->takeLast();
}
// 2012-10-24: fix memory leak
delete rpn_list;
delete rpnList;
}
else // error occurred, output it
{
Expand All @@ -475,8 +456,8 @@ void translate_input(Translator &translator, Parser &parser, Table *table,
translator.delete_open_paren(token);
}
translator.clean_up();
printf("\n"); // FIXME not needed, here to match current results
}
printf("\n");
}


Expand Down Expand Up @@ -601,6 +582,30 @@ bool print_token(Token *token, Table *table, bool tab)
}


// 2012-10-27: print entire output rpn list
void print_output(const char *header, QList<RpnItem *> rpnList, Table *table)
{
printf("%s: ", header);
foreach (RpnItem *rpnItem, rpnList)
{
print_small_token(rpnItem->token, table);
if (rpnItem->noperands > 0)
{
char separator = '[';
for (int i = 0; i < rpnItem->noperands; i++)
{
printf("%c", separator);
print_small_token(rpnItem->operand[i]->token, table);
separator = ',';
}
printf("]");
}
printf(" ");
}
printf("\n");
}


// 2010-03-20: reimplemented print_token for small output
bool print_small_token(Token *token, Table *table)
{
Expand Down

0 comments on commit 34cbd66

Please sign in to comment.