Compiler.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "Compiler.h"
  5. #include "Operation.h"
  6. #include "Tokenizer.h"
  7. #define MAX_BYTES (1024 * 1024)
  8. static const char* UNEXPECTED_TOKEN = "unexpected token";
  9. static const char* FULL_BUFFER = "the compiler buffer is too small";
  10. static const char* error = NULL;
  11. static unsigned char byteCode[MAX_BYTES];
  12. static int writeIndex = 0;
  13. static bool cAddBytes(const void* data, int length) {
  14. if(writeIndex + length > MAX_BYTES) {
  15. error = FULL_BUFFER;
  16. return false;
  17. }
  18. memcpy(byteCode + writeIndex, data, length);
  19. writeIndex += length;
  20. return true;
  21. }
  22. static bool cAddOperation(Operation token) {
  23. unsigned char c = token;
  24. return cAddBytes(&c, 1);
  25. }
  26. static bool cConsumeToken(Token t) {
  27. if(tReadToken() == t) {
  28. return true;
  29. }
  30. error = UNEXPECTED_TOKEN;
  31. return false;
  32. }
  33. static bool cConsumeTokenIf(Token t) {
  34. if(tPeekToken() == t) {
  35. tReadToken();
  36. return true;
  37. }
  38. return false;
  39. }
  40. static bool cConstant() {
  41. if(cConsumeToken(T_INT)) {
  42. int value;
  43. return tReadInt(&value) && cAddOperation(OP_PUSH_INT) && cAddBytes(&value, sizeof(int));
  44. }
  45. return false;
  46. }
  47. static bool cAdd() {
  48. if(!cConstant()) {
  49. return false;
  50. }
  51. while(cConsumeTokenIf(T_ADD)) {
  52. if(!cConstant() || !cAddOperation(OP_ADD)) {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. static bool cPrint() {
  59. return cAdd() && cConsumeToken(T_SEMICOLON) && cAddOperation(OP_PRINT);
  60. }
  61. static bool cLine() {
  62. Token t = tReadToken();
  63. if(t == T_END) {
  64. return false;
  65. } else if(t == T_PRINT) {
  66. return cPrint();
  67. }
  68. error = UNEXPECTED_TOKEN;
  69. return false;
  70. }
  71. unsigned char* cCompile(int* codeLength) {
  72. while(cLine()) {
  73. }
  74. if(error != NULL) {
  75. return NULL;
  76. }
  77. unsigned char* bytes = malloc(writeIndex);
  78. memcpy(bytes, byteCode, writeIndex);
  79. *codeLength = writeIndex;
  80. return bytes;
  81. }
  82. const char* cGetError() {
  83. return error;
  84. }