v8-test-2.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // { dg-do run }
  2. #include <stdlib.h>
  3. #include <string>
  4. class Literal;
  5. class CallRuntime;
  6. class AstNode {
  7. public:
  8. enum Type {
  9. kLiteral, kCallRuntime,
  10. kInvalid = -1
  11. };
  12. AstNode() { }
  13. virtual ~AstNode() { }
  14. virtual Type node_type() const = 0;
  15. bool
  16. IsLiteral() { return node_type() == AstNode::kLiteral; }
  17. Literal *
  18. AsLiteral() { return IsLiteral() ? reinterpret_cast<Literal*>(this)
  19. : NULL; }
  20. bool
  21. IsCallRuntime() { return node_type() == AstNode::kCallRuntime; }
  22. CallRuntime *
  23. AsCallRuntime() { return IsCallRuntime() ? reinterpret_cast<CallRuntime*>(this)
  24. : NULL; }
  25. };
  26. class Expression: public AstNode {
  27. public:
  28. private:
  29. int id_;
  30. };
  31. class Literal: public Expression {
  32. public:
  33. virtual AstNode::Type node_type() const { return AstNode::kLiteral; }
  34. private:
  35. std::string ToString();
  36. };
  37. class CallRuntime: public Expression {
  38. public:
  39. virtual AstNode::Type node_type() const { return AstNode::kCallRuntime; }
  40. private:
  41. std::string name_;
  42. };
  43. Expression *
  44. ExpressionCheck (bool *ok)
  45. {
  46. if (*ok == true)
  47. return new CallRuntime();
  48. else
  49. return new Literal ();
  50. return NULL;
  51. }
  52. Expression *
  53. GetExpression (bool *ok)
  54. {
  55. Expression *expression = ExpressionCheck (ok);
  56. Expression *return_expr = NULL;
  57. if (expression != NULL && expression->AsLiteral() != NULL)
  58. return_expr = new Literal();
  59. else if (expression != NULL && expression->AsCallRuntime() != NULL)
  60. return_expr = expression;
  61. return return_expr;
  62. }
  63. int
  64. main (int argc, char **argv)
  65. {
  66. bool a_bool = true;
  67. AstNode *node = GetExpression (&a_bool);
  68. return 0;
  69. }