Main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include "Code.h"
  4. #include "Compiler.h"
  5. #include "Values.h"
  6. #define POP_VALUE(name) \
  7. Value name; \
  8. if(popValue(&name)) \
  9. return true
  10. static bool iAdd() {
  11. POP_VALUE(a);
  12. POP_VALUE(b);
  13. return pushValue(INT_VALUE(a.intValue + b.intValue));
  14. }
  15. static bool iPushConstantString() {
  16. return pushValue(CSTRING_VALUE(codeReadConstantString()));
  17. }
  18. static bool iPushInt() {
  19. return pushValue(INT_VALUE(codeReadI64()));
  20. }
  21. static bool iPrint() {
  22. POP_VALUE(a);
  23. switch(a.type) {
  24. case INT64: printf("%ld", a.intValue); break;
  25. case CONSTANT_STRING: printf("%s", a.constantStringValue); break;
  26. }
  27. return false;
  28. }
  29. static bool iPrintNewline() {
  30. putchar('\n');
  31. return false;
  32. }
  33. static bool execute(Instruction command) {
  34. switch(command) {
  35. case ADD: return iAdd();
  36. case PUSH_CONSTANT_STRING: return iPushConstantString();
  37. case PUSH_INT64: return iPushInt();
  38. case PRINT: return iPrint();
  39. case PRINT_NEWLINE: return iPrintNewline();
  40. case STOP: return true;
  41. }
  42. return false;
  43. }
  44. int main(int argCount, const char** args) {
  45. if(argCount < 2) {
  46. return 0;
  47. }
  48. const Error* e = compileFile(args[1]);
  49. if(hasError(e)) {
  50. puts(e->text);
  51. return 0;
  52. }
  53. // if(strcmp(args[1], "./test/Print.basic") == 0) {
  54. // (void)pushInstruction(PUSH_CONSTANT_STRING);
  55. // (void)pushConstantString("Hi");
  56. // (void)pushInstruction(PRINT);
  57. // (void)pushInstruction(PRINT_NEWLINE);
  58. // (void)pushInstruction(PUSH_INT);
  59. // (void)pushI64(6);
  60. // (void)pushInstruction(PRINT);
  61. // (void)pushInstruction(PRINT_NEWLINE);
  62. // (void)pushInstruction(PUSH_CONSTANT_STRING);
  63. // (void)pushConstantString("Hi there ");
  64. // (void)pushInstruction(PRINT);
  65. // (void)pushInstruction(PUSH_INT);
  66. // (void)pushI64(8);
  67. // (void)pushInstruction(PRINT);
  68. // (void)pushInstruction(PUSH_CONSTANT_STRING);
  69. // (void)pushConstantString(" great");
  70. // (void)pushInstruction(PRINT);
  71. // (void)pushInstruction(PRINT_NEWLINE);
  72. //} else if(strcmp(args[1], "./test/Add.basic") == 0) {
  73. // (void)pushInstruction(PUSH_INT);
  74. // (void)pushI64(1);
  75. // (void)pushInstruction(PUSH_INT);
  76. // (void)pushI64(20);
  77. // (void)pushInstruction(ADD);
  78. // (void)pushInstruction(PRINT);
  79. // (void)pushInstruction(PRINT_NEWLINE);
  80. //}
  81. while(!execute(codeReadInstruction())) {}
  82. // char line[256];
  83. // while(true) {
  84. // fgets(line, sizeof(line), stdin);
  85. // if(strcmp(line, "quit\n") == 0) {
  86. // break;
  87. // }
  88. // puts(line);
  89. // }
  90. // puts("quit");
  91. return 0;
  92. }