diff options
-rw-r--r-- | tools/preproc/asm_file.cpp | 81 | ||||
-rw-r--r-- | tools/preproc/asm_file.h | 2 | ||||
-rw-r--r-- | tools/preproc/preproc.cpp | 37 | ||||
-rw-r--r-- | tools/preproc/string_parser.cpp | 2 |
4 files changed, 108 insertions, 14 deletions
diff --git a/tools/preproc/asm_file.cpp b/tools/preproc/asm_file.cpp index b843d640b..eba486e66 100644 --- a/tools/preproc/asm_file.cpp +++ b/tools/preproc/asm_file.cpp @@ -191,6 +191,8 @@ Directive AsmFile::GetDirective() return Directive::Include; else if (CheckForDirective(".string")) return Directive::String; + else if (CheckForDirective(".braille")) + return Directive::Braille; else return Directive::Unknown; } @@ -285,6 +287,85 @@ int AsmFile::ReadString(unsigned char* s) return length; } +int AsmFile::ReadBraille(unsigned char* s) +{ + static std::map<char, unsigned char> encoding = + { + { 'A', 0x01 }, + { 'B', 0x05 }, + { 'C', 0x03 }, + { 'D', 0x0B }, + { 'E', 0x09 }, + { 'F', 0x07 }, + { 'G', 0x0F }, + { 'H', 0x0D }, + { 'I', 0x06 }, + { 'J', 0x0E }, + { 'K', 0x11 }, + { 'L', 0x15 }, + { 'M', 0x13 }, + { 'N', 0x1B }, + { 'O', 0x19 }, + { 'P', 0x17 }, + { 'Q', 0x1F }, + { 'R', 0x1D }, + { 'S', 0x16 }, + { 'T', 0x1E }, + { 'U', 0x31 }, + { 'V', 0x35 }, + { 'W', 0x2E }, + { 'X', 0x33 }, + { 'Y', 0x3B }, + { 'Z', 0x39 }, + { ' ', 0x00 }, + { ',', 0x04 }, + { '.', 0x2C }, + { '$', 0xFF }, + }; + + SkipWhitespace(); + + int length = 0; + + if (m_buffer[m_pos] != '"') + RaiseError("expected braille string literal"); + + m_pos++; + + while (m_buffer[m_pos] != '"') + { + if (length == kMaxStringLength) + RaiseError("mapped string longer than %d bytes", kMaxStringLength); + + if (m_buffer[m_pos] == '\\' && m_buffer[m_pos + 1] == 'n') + { + s[length++] = 0xFE; + m_pos += 2; + } + else + { + char c = m_buffer[m_pos]; + + if (encoding.count(c) == 0) + { + if (IsAsciiPrintable(c)) + RaiseError("character '%c' not valid in braille string", m_buffer[m_pos]); + else + RaiseError("character '\\x%02X' not valid in braille string", m_buffer[m_pos]); + } + + s[length++] = encoding[c]; + m_pos++; + } + } + + m_pos++; // Go past the right quote. + + ExpectEmptyRestOfLine(); + + return length; +} + // If we're at a comma, consumes it. // Returns whether a comma was found. bool AsmFile::ConsumeComma() diff --git a/tools/preproc/asm_file.h b/tools/preproc/asm_file.h index 335dbab4b..c0aba878d 100644 --- a/tools/preproc/asm_file.h +++ b/tools/preproc/asm_file.h @@ -30,6 +30,7 @@ enum class Directive { Include, String, + Braille, Unknown }; @@ -43,6 +44,7 @@ public: Directive GetDirective(); std::string ReadPath(); int ReadString(unsigned char* s); + int ReadBraille(unsigned char* s); bool IsAtEnd(); void OutputLine(); void OutputLocation(); diff --git a/tools/preproc/preproc.cpp b/tools/preproc/preproc.cpp index 1dd6808c3..38ae762ea 100644 --- a/tools/preproc/preproc.cpp +++ b/tools/preproc/preproc.cpp @@ -27,6 +27,22 @@ Charmap* g_charmap; +void PrintAsmBytes(unsigned char *s, int length) +{ + if (length > 0) + { + printf("\t.byte "); + for (int i = 0; i < length; i++) + { + printf("0x%02X", s[i]); + + if (i < length - 1) + printf(", "); + } + putchar('\n'); + } +} + void PreprocAsmFile(std::string filename) { std::stack<AsmFile> stack; @@ -57,19 +73,14 @@ void PreprocAsmFile(std::string filename) { unsigned char s[kMaxStringLength]; int length = stack.top().ReadString(s); - - if (length > 0) - { - printf("\t.byte "); - for (int i = 0; i < length; i++) - { - printf("0x%02X", s[i]); - - if (i < length - 1) - printf(", "); - } - putchar('\n'); - } + PrintAsmBytes(s, length); + break; + } + case Directive::Braille: + { + unsigned char s[kMaxStringLength]; + int length = stack.top().ReadBraille(s); + PrintAsmBytes(s, length); break; } case Directive::Unknown: diff --git a/tools/preproc/string_parser.cpp b/tools/preproc/string_parser.cpp index b383f2f4f..dd5196a44 100644 --- a/tools/preproc/string_parser.cpp +++ b/tools/preproc/string_parser.cpp @@ -186,7 +186,7 @@ int StringParser::ParseString(long srcPos, unsigned char* dest, int& destLength) for (const char& c : sequence) { if (destLength == kMaxStringLength) - RaiseError("mapped string longer than %d bytes", destLength); + RaiseError("mapped string longer than %d bytes", kMaxStringLength); dest[destLength++] = c; } |