Guest User

Untitled

a guest
Jun 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. diff --git src/interpret.c src/interpret.c
  2. index 5a6cf07..bed664f 100644
  3. --- src/interpret.c
  4. +++ src/interpret.c
  5. @@ -36,6 +36,7 @@ struct InterState
  6. Expression *localThis; // value of 'this', or NULL if none
  7. bool awaitingLvalueReturn; // Support for ref return values:
  8. // Any return to this function should return an lvalue.
  9. + Loc loc;
  10. InterState();
  11. };
  12.  
  13. @@ -56,6 +57,74 @@ ArrayLiteralExp *createBlockDuplicatedArrayLiteral(Type *type, Expression *elem,
  14. Expression * resolveReferences(Expression *e, Expression *thisval, bool *isReference = NULL);
  15. Expression *getVarExp(Loc loc, InterState *istate, Declaration *d);
  16.  
  17. +
  18. +namespace rsinfu
  19. +{
  20. + Expression* interpret_writeln(InterState* istate, Expressions* arguments)
  21. + {
  22. + Expression* result = EXP_VOID_INTERPRET;
  23. +
  24. + if (arguments)
  25. + {
  26. + size_t const dim = arguments->dim;
  27. +
  28. + for (size_t i = 0; i < dim; ++i)
  29. + {
  30. + if (i > 0)
  31. + printf(", ");
  32. +
  33. + Expression* arg = static_cast<Expression*>(arguments->data[i]);
  34. + Expression* value = arg->interpret(istate);
  35. +
  36. + if (!value || value == EXP_CANT_INTERPRET)
  37. + {
  38. + result = EXP_CANT_INTERPRET;
  39. + break;
  40. + }
  41. + printf("%s", value->toChars());
  42. + }
  43. + }
  44. + putchar('\n');
  45. + return result;
  46. + }
  47. +
  48. + Expression* dump_variables(Dsymbols const* vars)
  49. + {
  50. + if (vars)
  51. + {
  52. + size_t const nvars = vars->dim;
  53. +
  54. + for (size_t i = 0; i < nvars; ++i)
  55. + {
  56. + VarDeclaration* var = static_cast<VarDeclaration*>(vars->data[i]);
  57. + printf("\t%s %s = %s\n", var->type->toChars(),
  58. + var->ident->toChars(),
  59. + var->value->toChars());
  60. + }
  61. + }
  62. + return EXP_VOID_INTERPRET;
  63. + }
  64. +
  65. + Expression* dump_istate(InterState const* istate)
  66. + {
  67. + if (istate)
  68. + {
  69. + printf("@ %s(%u)\n", istate->loc.filename,
  70. + istate->loc.linnum);
  71. +
  72. + if (FuncDeclaration* fd = istate->fd)
  73. + {
  74. + printf("# %s %s\n", fd->type->toChars(),
  75. + fd->ident->toChars());
  76. + dump_variables(fd->parameters);
  77. + }
  78. + dump_variables(&istate->vars);
  79. + puts("--------------------");
  80. + }
  81. + return EXP_VOID_INTERPRET;
  82. + }
  83. +}
  84. +
  85. /*************************************
  86. * Attempt to interpret a function given the arguments.
  87. * Input:
  88. @@ -90,6 +159,13 @@ Expression *FuncDeclaration::interpret(InterState *istate, Expressions *argument
  89. if (cantInterpret || semanticRun == PASSsemantic3)
  90. return NULL;
  91.  
  92. +#if 1
  93. + if (strcmp(ident->string, "writeln") == 0)
  94. + return rsinfu::interpret_writeln(istate, arguments);
  95. + if (strcmp(ident->string, "dump_istate") == 0)
  96. + return rsinfu::dump_istate(istate);
  97. +#endif
  98. +
  99. if (!fbody)
  100. { cantInterpret = 1;
  101. error("cannot be interpreted at compile time,"
  102. @@ -307,7 +383,30 @@ Expression *FuncDeclaration::interpret(InterState *istate, Expressions *argument
  103. { if (istate->start != this) \
  104. return NULL; \
  105. istate->start = NULL; \
  106. - }
  107. + } \
  108. + rsinfu::location_keeper location_keeper_(this, istate);
  109. +
  110. +namespace rsinfu
  111. +{
  112. + struct location_keeper
  113. + {
  114. + location_keeper(Statement const* stmt, InterState* istate)
  115. + : oldloc_(istate ? istate->loc : 0),
  116. + istate_(istate)
  117. + {
  118. + if (istate_) istate_->loc = stmt->loc;
  119. + }
  120. +
  121. + ~location_keeper()
  122. + {
  123. + if (istate_) istate_->loc = oldloc_;
  124. + }
  125. +
  126. + private:
  127. + Loc oldloc_;
  128. + InterState* istate_;
  129. + };
  130. +}
  131.  
  132. /***********************************
  133. * Interpret the statement.
  134. @@ -2830,12 +2929,12 @@ Expression *CallExp::interpret(InterState *istate)
  135. }
  136. else
  137. {
  138. - if (!fd->fbody)
  139. - {
  140. - error("%s cannot be interpreted at compile time,"
  141. - " because it has no available source code", fd->toChars());
  142. - return EXP_CANT_INTERPRET;
  143. - }
  144. +// if (!fd->fbody)
  145. +// {
  146. +// error("%s cannot be interpreted at compile time,"
  147. +// " because it has no available source code", fd->toChars());
  148. +// return EXP_CANT_INTERPRET;
  149. +// }
  150. Expression *eresult = fd->interpret(istate, arguments);
  151. if (eresult)
  152. e = eresult;
Add Comment
Please, Sign In to add comment