Worked on parsing code and filling out Command structure.
1.1 --- a/Command.cpp Mon Jun 29 18:10:54 2009 -0400
1.2 +++ b/Command.cpp Tue Jun 30 19:23:54 2009 -0400
1.3 @@ -1,5 +1,55 @@
1.4 #include "Command.h"
1.5 +#include <stdio.h>
1.6 +Command::Command()
1.7 +{
1.8 + filename = 0; // null
1.9 + data = 0; // null
1.10 + dataCount = 0;
1.11 +}
1.12
1.13 +Command::~Command()
1.14 +{
1.15 + free(filename);
1.16 +}
1.17 +
1.18 +void Command::print()
1.19 +{
1.20 + printf("------\n");
1.21 + printf("Printing command info:\n");
1.22 + printf("------\n");
1.23 +
1.24 + printf("Mode: %d\n", mode);
1.25 +
1.26 + printf("Range: ");
1.27 + if(range == true)
1.28 + printf("Yes\n");
1.29 + else
1.30 + printf("No\n");
1.31 +
1.32 + printf("Start: %#x\n", start);
1.33 + if(range == true)
1.34 + printf("End: %#x\n", end);
1.35 +
1.36 + printf("Filename: %s\n", filename);
1.37 +
1.38 + printf("Data count: %d\n", dataCount);
1.39 + for(int i = 0; i < dataCount; i++)
1.40 + printf("Data: %#x\n", (int)data[i]);
1.41 +
1.42 + printf("------\n");
1.43 + /*
1.44 + MODE_ENUM mode;
1.45 + int start;
1.46 +
1.47 + bool range;
1.48 + int end;
1.49 +
1.50 + char* filename;
1.51 +
1.52 + int* data;
1.53 + int dataCount;
1.54 +*/
1.55 +}
1.56 void processCommand(Command* command)
1.57 {
1.58
2.1 --- a/Command.h Mon Jun 29 18:10:54 2009 -0400
2.2 +++ b/Command.h Tue Jun 30 19:23:54 2009 -0400
2.3 @@ -1,19 +1,35 @@
2.4 +#pragma once
2.5 +
2.6 +#ifndef COMMANDHEADER
2.7 +#define COMMANDHEADER
2.8 +
2.9 enum MODE_ENUM {PRINT=1, XOR, OR, AND, NOT};
2.10
2.11 -struct commandStruct {
2.12 +class Command {
2.13
2.14 - MODE_ENUM mode;
2.15 - int start;
2.16 +public:
2.17
2.18 - bool range;
2.19 - int end;
2.20 + Command();
2.21 +
2.22 + MODE_ENUM mode;
2.23 + int start;
2.24
2.25 - char* filename;
2.26 + bool range;
2.27 + int end;
2.28
2.29 - int* data;
2.30 + char* filename;
2.31 +
2.32 + int* data;
2.33 + int dataCount;
2.34 +
2.35 + void print();
2.36 +
2.37
2.38 };
2.39
2.40 -typedef struct commandStruct Command;
2.41 +static Command* theCommand;
2.42 +
2.43
2.44 void processCommand(Command*);
2.45 +
2.46 +#endif
2.47 \ No newline at end of file
3.1 --- a/CommandParser.l Mon Jun 29 18:10:54 2009 -0400
3.2 +++ b/CommandParser.l Tue Jun 30 19:23:54 2009 -0400
3.3 @@ -10,46 +10,65 @@
3.4 printf("Whitespace\n");
3.5 }
3.6
3.7 -"-p" {
3.8 +-p {
3.9 printf("Print mode\n");
3.10 return PRINTMODE;
3.11 }
3.12
3.13 -"-x" {
3.14 +-x {
3.15 printf("Xor mode\n");
3.16 return XORMODE;
3.17 }
3.18
3.19 -"-n" {
3.20 +-n {
3.21 printf("Not mode\n");
3.22 return NOTMODE;
3.23 }
3.24
3.25 -"-a" {
3.26 +-a {
3.27 printf("And mode\n");
3.28 return ANDMODE;
3.29 }
3.30
3.31 -"-o" {
3.32 +
3.33 +
3.34 +-o {
3.35 printf("Or mode\n");
3.36 return ORMODE;
3.37 }
3.38
3.39 -"-r" {
3.40 +-r {
3.41 printf("Range mode\n");
3.42 return RANGEFLAG;
3.43 }
3.44
3.45 +0x[0-9a-fA-F]+ {
3.46 + printf("Hex number\n");
3.47 + int retVal = sscanf(yytext, "%x",&(yylval.int_val));
3.48 + if(retVal != 1)
3.49 + return;
3.50 + return NUMBER;
3.51 +}
3.52 +
3.53 [0-9]+ {
3.54 printf("Number\n");
3.55 + yylval.int_val = atoi(yytext);
3.56 return NUMBER;
3.57 }
3.58
3.59 +['"].*+['"] {
3.60 + printf("Argument\n");
3.61 + yylval.string_val = strdup(yytext);
3.62 + return ARGUMENT;
3.63 +}
3.64 +
3.65 [^ \t\n]+ {
3.66 printf("Alpha\n");
3.67 + yylval.string_val = strdup(yytext);
3.68 return ARGUMENT;
3.69 }
3.70
3.71 +
3.72 %%
3.73 void configBuffer(char* arguments)
3.74 {
4.1 --- a/CommandParser.y Mon Jun 29 18:10:54 2009 -0400
4.2 +++ b/CommandParser.y Tue Jun 30 19:23:54 2009 -0400
4.3 @@ -27,7 +27,6 @@
4.4
4.5 int rangeStart, rangeEnd;
4.6
4.7 -Command* theCommand;
4.8 %}
4.9
4.10
4.11 @@ -43,37 +42,72 @@
4.12 ;
4.13
4.14 filename:
4.15 - ARGUMENT
4.16 + ARGUMENT {
4.17 + theCommand->filename = strdup($1);
4.18 + }
4.19 ;
4.20
4.21 data:
4.22 - NUMBER
4.23 + NUMBER {
4.24 + if(theCommand->data == NULL)
4.25 + {
4.26 + // null, so allocate some space
4.27 + theCommand->data = (int*)malloc(sizeof(int));
4.28 + }
4.29 + }
4.30 |
4.31 - data NUMBER
4.32 + data NUMBER {
4.33 + if(theCommand->data == NULL)
4.34 + {
4.35 + // null, so allocate some space
4.36 + theCommand->data = (int*)malloc(sizeof(int));
4.37 + }
4.38 + else
4.39 + {
4.40 + // we've already got some space, so increase it
4.41 + theCommand->data = (int*)realloc(theCommand->data, ++theCommand->dataCount);
4.42 + }
4.43 + }
4.44 +
4.45 ;
4.46
4.47 mode:
4.48 - PRINTMODE
4.49 + PRINTMODE {
4.50 + theCommand->mode = PRINT;
4.51 + }
4.52 |
4.53 - XORMODE
4.54 + XORMODE {
4.55 + theCommand->mode = XOR;
4.56 + }
4.57 |
4.58 - ORMODE
4.59 + ORMODE {
4.60 + theCommand->mode = OR;
4.61 + }
4.62 |
4.63 - ANDMODE
4.64 + ANDMODE {
4.65 + theCommand->mode = AND;
4.66 + }
4.67 |
4.68 - NOTMODE
4.69 + NOTMODE {
4.70 + theCommand->mode = NOT;
4.71 + }
4.72 ;
4.73
4.74 destination:
4.75 range
4.76 |
4.77 - NUMBER
4.78 + NUMBER {
4.79 + theCommand->range = false;
4.80 + theCommand->start = $1;
4.81 + theCommand->end = -1;
4.82 + }
4.83 ;
4.84
4.85 range:
4.86 - RANGEFLAG NUMBER NUMBER
4.87 - {
4.88 - printf("Range of: %d to %d\n", $2, $3);
4.89 + RANGEFLAG NUMBER NUMBER {
4.90 + theCommand->range = true;
4.91 + theCommand->start = $2;
4.92 + theCommand->end = $3;
4.93 }
4.94 ;
4.95
4.96 @@ -146,10 +180,15 @@
4.97 return 0;
4.98 }
4.99
4.100 + // initialise our global
4.101 + theCommand = new Command();
4.102 +
4.103 // process our arguments
4.104 parseArguments(argc, argv);
4.105
4.106 // process the command
4.107 processCommand(theCommand);
4.108 +
4.109 + theCommand->print();
4.110 return 0;
4.111 }
5.1 --- a/makefile Mon Jun 29 18:10:54 2009 -0400
5.2 +++ b/makefile Tue Jun 30 19:23:54 2009 -0400
5.3 @@ -4,18 +4,18 @@
5.4 all: executable
5.5
5.6 executable: CommandParser.tab.o CommandParser.yy.o Command.o
5.7 - g++ -o hexciting CommandParser.yy.o CommandParser.tab.o Command.o -lfl
5.8 + g++ -g -o hexciting CommandParser.yy.o CommandParser.tab.o Command.o -lfl
5.9
5.10 CommandParser.yy.o: CommandParser.l
5.11 flex -o CommandParser.yy.c CommandParser.l
5.12 - gcc -c CommandParser.yy.c
5.13 + gcc -g -c CommandParser.yy.c
5.14
5.15 CommandParser.tab.o: CommandParser.y
5.16 bison -d CommandParser.y
5.17 - g++ -c CommandParser.tab.c
5.18 + g++ -g -c CommandParser.tab.c
5.19
5.20 Command.o: Command.cpp
5.21 - g++ -c Command.cpp
5.22 + g++ -g -c Command.cpp
5.23
5.24 clean:
5.25 rm -f CommandParser.tab.* CommandParser.yy.* hexciting *.o