tibasic.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2011 Matthew Iselin
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef _TIBASIC_H
  17. #define _TIBASIC_H
  18. #include <string>
  19. #ifdef _MSC_VER
  20. #define PACKED
  21. #else
  22. #define PACKED __attribute__((packed))
  23. #endif
  24. /// Stores a token to be written and the size of that token.
  25. typedef struct
  26. {
  27. unsigned short token;
  28. size_t sz;
  29. } token_t;
  30. /// Log severities
  31. enum LogSeverity
  32. {
  33. Error,
  34. Info,
  35. Debug
  36. };
  37. void stripExtension(const char *in, char *out, size_t len);
  38. void initialiseTokens();
  39. size_t getLongestToken();
  40. bool lookupToken(std::string in, token_t &ret);
  41. bool lookupToken(unsigned short in, std::string &out);
  42. inline const char *severityToString(LogSeverity s)
  43. {
  44. switch(s)
  45. {
  46. case Error:
  47. return "Error";
  48. break;
  49. case Info:
  50. return "Info";
  51. break;
  52. case Debug:
  53. return "Debug";
  54. break;
  55. default:
  56. return "!";
  57. break;
  58. };
  59. }
  60. /// Log function
  61. void log(LogSeverity, const char *);
  62. /// Compilation class.
  63. class Compiler
  64. {
  65. public:
  66. Compiler() {}
  67. virtual ~Compiler() {}
  68. inline void verbosity(bool is)
  69. {
  70. m_bVerbose = is;
  71. }
  72. bool compile(std::string inFile, std::string outFile);
  73. bool decompile(std::string inFile, std::string outFile);
  74. private:
  75. /// Whether to be verbose when compiling.
  76. bool m_bVerbose;
  77. /// Perform a checksum over a region of data.
  78. size_t sumBytes(const char *data, size_t len);
  79. unsigned short doChecksum(size_t sum);
  80. #ifdef _MSC_VER
  81. #pragma pack(push, 1)
  82. #endif
  83. /// 8xp file header
  84. struct ProgramHeader
  85. {
  86. char sig[8];
  87. char extsig[3];
  88. char comment[42];
  89. unsigned short datalen;
  90. } PACKED;
  91. /// Variable entry
  92. struct VariableEntry
  93. {
  94. unsigned short start;
  95. unsigned short length1;
  96. unsigned char type;
  97. char name[8];
  98. char ver;
  99. char flags;
  100. unsigned short length2;
  101. } PACKED;
  102. #ifdef _MSC_VER
  103. #pragma pack(pop)
  104. #endif
  105. };
  106. #endif