Skip to content

Commit

Permalink
Merge pull request #1 from Adeimantius/Simple-Writing-Lex
Browse files Browse the repository at this point in the history
Accept the simple version of the lexical analysis into master
  • Loading branch information
Adeimantius committed Mar 19, 2015
2 parents c22a2e4 + ae284fe commit 0f7210d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 59 deletions.
102 changes: 47 additions & 55 deletions Build1/zmachine/zmachine/Lex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public class Lex
List<uint> dictionaryIndex = new List<uint>();
int[] wordStartIndex;

int mp = 0; // Memory Pointer
uint mp = 0; // Memory Pointer

public Lex(Memory mem)
{
memory = mem;
dictionaryAddress = memory.getByte(Memory.ADDR_DICT);
dictionaryAddress = memory.getWord(Memory.ADDR_DICT);
}

public void read(int textBufferAddress, uint parseBufferAddress)
{
int maxInputLength = memory.getByte((uint)textBufferAddress) - 1; // byte 0 of the text-buffer should initially contain the maximum number of letters which can be typed, minus 1
int maxWordLength = memory.getByte((uint)parseBufferAddress);

mp = parseBufferAddress + 2;
String input = Console.ReadLine(); // Get initial input from console terminal

if (input.Length > maxInputLength)
Expand All @@ -39,25 +39,33 @@ public void read(int textBufferAddress, uint parseBufferAddress)

writeToBuffer(input, textBufferAddress); // stored in bytes 1 onward, with a zero terminator (but without any other terminator, such as a carriage return code

buildDict(); // Build Dictionary into class variable
String[] wordArray = parseString(input); // Separate string by spaces and build list of word indices
int[] wordBuffer = new int[maxWordLength];

for (int i = 0; i < wordArray.Length; i++)
if (maxWordLength > 0) // Check to see if lexical analysis is called for
{
wordBuffer[i] = compare(wordArray[i]);
}
// Record dictionary addresses after comparing words
buildDict(); // Build Dictionary into class variable
String[] wordArray = parseString(input); // Separate string by spaces and build list of word indices
uint[] matchedWords = new uint[maxWordLength];

for (int i = 0; i < wordArray.Length; i++)
{
matchedWords[i] = compare(wordArray[i]); // Stores the dictionary address of matched words (or 0 if no match)
// Debug.WriteLine("Byte address of matched word: " + matchedWords[i]);
}
// Record dictionary addresses after comparing words

memory.setByte(parseBufferAddress + 1, (byte)(wordBuffer.Length)); // Write number of parsed words

for (int i = 0; i < wordArray.Length; i++)
{
int wordLength = wordStartIndex[i] > 0 ? wordStartIndex[i + 1] - wordStartIndex[i] : input.Length - wordStartIndex[i];
memory.setWord((uint)(parseBufferAddress + 2 + (4 * i)), (byte)wordBuffer[i]); // Address in dictionary
memory.setByte((uint)(parseBufferAddress + 4 + (4 * i)), (byte)wordLength); // # of letters in parsed word (either from dictionary or 0)
memory.setByte((uint)(parseBufferAddress + 5 + (4 * i)), (byte)wordStartIndex[i]); // Corresponding word position in text buffer
memory.setByte(mp - 1, (byte)(wordArray.Length)); // Write number of parsed words

for (int i = 0; i < wordArray.Length; i++)
{
if (4 * i < maxWordLength)
{
int wordLength = wordStartIndex[i] > 0 ? wordStartIndex[i + 1] - wordStartIndex[i] : input.Length - wordStartIndex[i];
memory.setWord((uint)(mp), (ushort)matchedWords[i]); // Address in dictionary of matches (either from dictionary or 0)
memory.setByte((uint)(mp + 2), (byte)wordLength); // # of letters in parsed word
memory.setByte((uint)(mp + 3), (byte)wordStartIndex[i]); // Corresponding word position in text buffer
}
mp += 4;
}
}

}
Expand All @@ -75,22 +83,27 @@ public void buildDict()
separators.Add(memory.getByte(i + 1)); // Find 'n' different word separators and add to list
}

for (uint i = entryAddress; i < entryAddress + dictionaryLength * 2; i += entryLength)
for (uint i = entryAddress; i < entryAddress + dictionaryLength * entryLength; i += entryLength)
{
dictionaryIndex.Add(i); // Record dictionary entry address
Memory.StringAndReadLength dictEntry = memory.getZSCII(i, 4);
Debug.WriteLine(dictEntry.str);
// Debug.WriteLine(dictEntry.str);
dictionary.Add(dictEntry.str); // Find 'n' different dictionary entries and add words to list
}
}

public int compare(String word, int dictionaryFlag = 0)
public uint compare(String word, int dictionaryFlag = 0)
{
if (word.Length > 6)
word = word.Remove(6);
// search dictionary for comparisons
for (int i = 0; i < dictionary.Count; i++)
{
if (dictionary[i] == word)
return memory.getByte((uint)dictionaryIndex[i]);
if (dictionary[i] == word)
{
// Console.WriteLine("Matched word: " + word + " at dictionary entry: " + memory.getByte((uint)dictionaryIndex[i]) + " // " + dictionary[i] );
return dictionaryIndex[i];
}
}
Console.WriteLine("Could not identify keyword:" + word);
return 0;
Expand Down Expand Up @@ -121,42 +134,21 @@ public String[] parseString(String input)
// Store string (in ZSCII) at address in byte 1 onward with a zero terminator.
public void writeToBuffer(String input, int address)
{
int[] c = new int[3]; // Store three zchars across 16 bits (two bytes). May have to make a dynamic list and pad it once every 3 reads.
int i = 0;
mp = 1;

bool stringComplete = false;

while (stringComplete != true)
int i;
for(i = 0 ; i < input.Length; ++i)
{
ushort word = 0;

for (int j = 0; j < 3; j++)
{
if (i + j > input.Length - 1)
{
c[j] = 5; // Pad word with 5's.
stringComplete = true;
}
else // Write next char from input into 3-char array
{
c[j] = convertZSCIIToZchar(input[i + j]);
}
word += (ushort)(c[j] << 10 - (5 * j)); // Write 3 zchars into word
}

memory.setWord((uint)(address + mp), word); // Write word to memory
Console.WriteLine("Converted ZSCII string: " + memory.getZSCII((uint)(address + mp), 2).str);
i += 3;
mp += 2;


char ch = input[i];
// Supposed to convert these to zscii here...

memory.setByte((uint)(address + 1 + i), (byte)ch);
}
memory.setWord((uint)(address + mp), 0); // Write empty word to terminate after read is complete.

// Write next char from input into 3-char array (unimplemented)

memory.setWord((uint)(address + 1 + i), 0); // Write empty word to terminate after read is complete.
// Console.WriteLine("Converted ZSCII string: " + memory.getZSCII((uint)(address + 1), 0).str);
}

public int convertZSCIIToZchar(char letter)
public int convertZSCIIToZchar(char letter) // (unimplemented)
{
String[] zalphabets = { "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", " \n0123456789.,!?_#'\"/\\-:()" };
// Convert into Zchar from ZSCII char
Expand Down
8 changes: 4 additions & 4 deletions Build1/zmachine/zmachine/OpcodeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public class op_not : OpcodeHandler_1OP
public class op_print_addr : OpcodeHandler_1OP
{
public override String name() { return "op_print_addr"; }
public override void run(Machine machine,ushort v1) { Console.WriteLine(machine.memory.getZSCII(v1, 0)); }
public override void run(Machine machine,ushort v1) { Console.WriteLine(machine.memory.getZSCII(v1, 0).str); }
}
public class op_remove_obj : OpcodeHandler_1OP
{
Expand Down Expand Up @@ -336,7 +336,7 @@ public override void run(Machine machine, ushort v1)
public class op_print_paddr : OpcodeHandler_1OP
{
public override String name() { return "op_print_paddr"; }
public override void run(Machine machine, ushort v1) { Console.Write(machine.memory.getZSCII((uint)v1 * 2, 0)); }
public override void run(Machine machine, ushort v1) { Console.Write(machine.memory.getZSCII((uint)v1 * 2, 0).str); }
}
public class op_load : OpcodeHandler_1OP
{
Expand All @@ -362,7 +362,7 @@ public override void run(Machine machine)
{
// Debug.WriteLine("Getting string at " + machine.pc);
Memory.StringAndReadLength str = machine.memory.getZSCII(machine.pc, 0);
Console.WriteLine(str.str);
Console.Write(str.str);
machine.pc += (uint)str.bytesRead;
// Debug.WriteLine("New pc location: " + machine.pc);

Expand All @@ -373,7 +373,7 @@ public class op_print_ret : OpcodeHandler_0OP
public override String name() { return "op_print_ret"; }
public override void run(Machine machine)
{
Console.WriteLine(machine.memory.getZSCII(machine.pc_getWord(), 0));
Console.WriteLine(machine.memory.getZSCII(machine.pc_getWord(), 0).str);
machine.popRoutineData(1);
}
}
Expand Down

0 comments on commit 0f7210d

Please sign in to comment.