1 module ast; 2 3 private import asttypes; 4 private import errors; 5 private import std.string; 6 private import std.stdio; 7 8 enum AstExpressionType 9 { 10 SIMPLE, 11 FUNCTION, 12 IF, 13 FOR, 14 WHILE 15 } 16 17 interface AstExpression { 18 AstExpressionType getExpressionType(); 19 } 20 21 class Ast : AstType { 22 public: 23 AstType left, right; 24 string operator; 25 26 this() { 27 left = new LedNullToken(); 28 operator = ""; 29 right = new LedNullToken(); 30 } 31 32 LedAstType getAstType() { 33 return LedAstType.EXPRESSION; 34 } 35 36 string getString() { 37 string ret = "{\"operator\" : \""; 38 ret ~= operator; 39 ret ~= "\", \"left\" : " ~ left.getString() ~ ","; 40 ret ~= "\"right\" : " ~ right.getString() ~ "}"; 41 return ret; 42 } 43 44 AstExpressionType getExpressionType() { 45 return AstExpressionType.SIMPLE; 46 } 47 } 48 49 class FunctionAst : AstType { 50 public: 51 string functionName; 52 string[] params; 53 AstType[] bodyStatements; 54 55 this(string name, string[] params) { 56 this.functionName = name; 57 this.params = params; 58 } 59 60 LedAstType getAstType() { 61 return LedAstType.EXPRESSION; 62 } 63 64 string getString() { 65 return "\"<function>" ~ functionName ~ "\""; 66 } 67 68 AstExpressionType getExpressionType() { 69 return AstExpressionType.FUNCTION; 70 } 71 } 72 73 class IfAst : AstType { 74 public: 75 AstType condition; 76 AstType[] successClause; 77 AstType[] failureClause; 78 79 this(AstType booleval, AstType[] sc, AstType[] fc) { 80 this.condition = booleval; 81 this.successClause = sc; 82 this.failureClause = fc; 83 } 84 85 LedAstType getAstType() { 86 return LedAstType.EXPRESSION; 87 } 88 89 string getString() { 90 return "\"<if>conditional\""; 91 } 92 93 AstExpressionType getExpressionType() { 94 return AstExpressionType.IF; 95 } 96 } 97 98 class WhileAst : AstType { 99 public: 100 AstType condition; 101 AstType[] bodyStatements; 102 103 this(AstType booleval, AstType[] bodyStatements) { 104 this.condition = booleval; 105 this.bodyStatements = bodyStatements; 106 } 107 108 LedAstType getAstType() { 109 return LedAstType.EXPRESSION; 110 } 111 112 string getString() { 113 return "\"<while>loop\""; 114 } 115 116 AstExpressionType getExpressionType() { 117 return AstExpressionType.WHILE; 118 } 119 } 120 121 class ForAst : AstType { 122 public: 123 AstType iteratorStatement; 124 AstType[] bodyStatements; 125 126 this(AstType istmt, AstType[] bodyStatements) { 127 this.iteratorStatement = istmt; 128 this.bodyStatements = bodyStatements; 129 } 130 131 LedAstType getAstType() { 132 return LedAstType.EXPRESSION; 133 } 134 135 string getString() { 136 return "\"<for>loop\""; 137 } 138 139 AstExpressionType getExpressionType() { 140 return AstExpressionType.FOR; 141 } 142 }