Skip to content

Commit

Permalink
implemented string constant encoding and dictionary
Browse files Browse the repository at this point in the history
added constant string information class for holding constant values
added constant string dictionary class with desctructor for clean up
added constant string encode and operand text functions
added function pointers to constant string code table entry
added constant string dictionary to program unit class

added another statement (with embedded double quote string) to encoder
test #1, updated expected results for new statement and string constant
operand text output
  • Loading branch information
thunder422 committed Oct 6, 2013
1 parent babfb75 commit 8a85668
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 5 deletions.
30 changes: 30 additions & 0 deletions basic/basic.cpp
Expand Up @@ -65,6 +65,36 @@ QString constNumOperandText(ProgramUnit *programUnit, quint16 operand)
}


ConstStrInfo::ConstStrInfo(void)
{
// default constructor needed for QVector
}

ConstStrInfo::ConstStrInfo(Token *token)
{
m_value = new QString(token->string());
}

ConstStrDictionary::~ConstStrDictionary(void)
{
for (int i = 0; i < m_info.count(); i++)
{
delete m_info.at(i).value();
}
}


quint16 constStrEncode(ProgramUnit *programUnit, Token *token)
{
return programUnit->constStrDictionary()->add(token);
}

QString constStrOperandText(ProgramUnit *programUnit, quint16 operand)
{
return programUnit->constStrDictionary()->string(operand);
}


// VARIABLE FUNCTIONS

quint16 varDblEncode(ProgramUnit *programUnit, Token *token)
Expand Down
24 changes: 24 additions & 0 deletions basic/basic.h
Expand Up @@ -57,6 +57,28 @@ class ConstNumInfo
};


class ConstStrInfo
{
QString *m_value;

public:
ConstStrInfo(void);
ConstStrInfo(Token *token);

QString *value(void) const
{
return m_value;
}
};


class ConstStrDictionary : public InfoDictionary<ConstStrInfo>
{
public:
~ConstStrDictionary(void);
};


// translate functions
TokenStatus inputTranslate(Translator &translator, Token *commandToken,
Token *&token);
Expand All @@ -69,6 +91,7 @@ TokenStatus printTranslate(Translator &translator, Token *commandToken,
// encode functions
quint16 remEncode(ProgramUnit *programUnit, Token *token);
quint16 constNumEncode(ProgramUnit *programUnit, Token *token);
quint16 constStrEncode(ProgramUnit *programUnit, Token *token);
quint16 varDblEncode(ProgramUnit *programUnit, Token *token);
quint16 varIntEncode(ProgramUnit *programUnit, Token *token);
quint16 varStrEncode(ProgramUnit *programUnit, Token *token);
Expand All @@ -77,6 +100,7 @@ quint16 varStrEncode(ProgramUnit *programUnit, Token *token);
// operand text functions
QString remOperandText(ProgramUnit *programUnit, quint16 operand);
QString constNumOperandText(ProgramUnit *programUnit, quint16 operand);
QString constStrOperandText(ProgramUnit *programUnit, quint16 operand);
QString varDblOperandText(ProgramUnit *programUnit, quint16 operand);
QString varIntOperandText(ProgramUnit *programUnit, quint16 operand);
QString varStrOperandText(ProgramUnit *programUnit, quint16 operand);
Expand Down
2 changes: 2 additions & 0 deletions programmodel.cpp
Expand Up @@ -84,6 +84,7 @@ ProgramUnit::ProgramUnit(Table &table): m_table(table)
{
m_remDictionary = new Dictionary;
m_constNumDictionary = new InfoDictionary<ConstNumInfo>;
m_constStrDictionary = new ConstStrDictionary;

m_varDblDictionary = new Dictionary;
m_varIntDictionary = new Dictionary;
Expand All @@ -95,6 +96,7 @@ ProgramUnit::~ProgramUnit(void)
{
delete m_remDictionary;
delete m_constNumDictionary;
delete m_constStrDictionary;

delete m_varDblDictionary;
delete m_varIntDictionary;
Expand Down
5 changes: 5 additions & 0 deletions programmodel.h
Expand Up @@ -81,6 +81,7 @@ class ProgramUnit
// pointers to the global program dictionaries
Dictionary *m_remDictionary;
InfoDictionary<ConstNumInfo> *m_constNumDictionary;
ConstStrDictionary *m_constStrDictionary;

// pointers to the local unit dictionaries
Dictionary *m_varDblDictionary;
Expand All @@ -100,6 +101,10 @@ class ProgramUnit
{
return m_constNumDictionary;
}
InfoDictionary<ConstStrInfo> *constStrDictionary(void) const
{
return m_constStrDictionary;
}

Dictionary *varDblDictionary(void) const
{
Expand Down
3 changes: 2 additions & 1 deletion table.cpp
Expand Up @@ -1238,7 +1238,8 @@ static TableEntry tableEntries[] =
{ // ConstStr_Code
Constant_TokenType, OneWord_Multiple,
NULL, "ConstStr", NULL,
HasOperand_Flag, 2, String_DataType, &Str_ExprInfo
HasOperand_Flag, 2, String_DataType, &Str_ExprInfo,
NULL, constStrEncode, constStrOperandText
},
{ // Var_Code
NoParen_TokenType, OneWord_Multiple,
Expand Down
1 change: 1 addition & 0 deletions test/encoder01.dat
Expand Up @@ -22,3 +22,4 @@ rem reuse previous double constant as double
D%=C%+5
'same constant but as string; should be separate string constant
PRINT "Test",C%,"5"
PRINT "Quote""Test"
11 changes: 7 additions & 4 deletions test/encoder01.txt
Expand Up @@ -15,7 +15,7 @@ Input: PRINT B%
Output: 0:VarInt 1:|0:B%| 2:PrintInt 3:PRINT

Input: C$="Test"
Output: 0:VarRefStr 1:|0:C$| 2:ConstStr 3:|0:--| 4:Assign$
Output: 0:VarRefStr 1:|0:C$| 2:ConstStr 3:|0:Test| 4:Assign$

Input: PRINT C$' not much to see here
Output: 0:VarStr 1:|0:C$| 2:PrintStr 3:PRINT 4:' 5:|2: not much to see here|
Expand All @@ -24,7 +24,7 @@ Input:
Output:

Input: INPUT PROMPT "Enter:";D%:LET E=SQR(D%):PRINT D%,E' comment
Output: 0:ConstStr 1:|0:--| 2:InputParseInt 3:InputBeginStr 4:VarRefInt 5:|1:D%| 6:InputAssignInt 7:PROMPT':' 8:VarRef 9:|1:E| 10:VarInt 11:|1:D%| 12:CvtDbl 13:SQR( 14:Assign'LET:' 15:VarInt 16:|1:D%| 17:PrintInt 18:, 19:Var 20:|1:E| 21:PrintDbl 22:PRINT 23:' 24:|1: comment|
Output: 0:ConstStr 1:|1:Enter:| 2:InputParseInt 3:InputBeginStr 4:VarRefInt 5:|1:D%| 6:InputAssignInt 7:PROMPT':' 8:VarRef 9:|1:E| 10:VarInt 11:|1:D%| 12:CvtDbl 13:SQR( 14:Assign'LET:' 15:VarInt 16:|1:D%| 17:PrintInt 18:, 19:Var 20:|1:E| 21:PrintDbl 22:PRINT 23:' 24:|1: comment|

Input:
Output:
Expand All @@ -36,7 +36,7 @@ Input: PRINT A,B,C
Output: 0:Var 1:|0:A| 2:PrintDbl 3:, 4:Var 5:|2:B| 6:PrintDbl 7:, 8:Var 9:|3:C| 10:PrintDbl 11:PRINT

Input: A$="Test"+CHR$(B%+48)
Output: 0:VarRefStr 1:|1:A$| 2:ConstStr 3:|0:--| 4:VarInt 5:|0:B%| 6:ConstInt 7:|2:48| 8:+% 9:CHR$( 10:+$ 11:Assign$
Output: 0:VarRefStr 1:|1:A$| 2:ConstStr 3:|0:Test| 4:VarInt 5:|0:B%| 6:ConstInt 7:|2:48| 8:+% 9:CHR$( 10:+$ 11:Assign$

Input: PRINT B%;A$
Output: 0:VarInt 1:|0:B%| 2:PrintInt 3:VarStr 4:|1:A$| 5:PrintStr 6:PRINT
Expand All @@ -60,5 +60,8 @@ Input: 'same constant but as string; should be separate string constant
Output: 0:' 1:|5:same constant but as string; should be separate string constant|

Input: PRINT "Test",C%,"5"
Output: 0:ConstStr 1:|0:--| 2:PrintStr 3:, 4:VarInt 5:|2:C%| 6:PrintInt 7:, 8:ConstStr 9:|0:--| 10:PrintStr 11:PRINT
Output: 0:ConstStr 1:|0:Test| 2:PrintStr 3:, 4:VarInt 5:|2:C%| 6:PrintInt 7:, 8:ConstStr 9:|2:5| 10:PrintStr 11:PRINT

Input: PRINT "Quote""Test"
Output: 0:ConstStr 1:|3:Quote"Test| 2:PrintStr 3:PRINT

0 comments on commit 8a85668

Please sign in to comment.