1 module vayne.ast.node; 2 3 4 import std.traits; 5 6 import vayne.ast.visitor; 7 import vayne.source.token; 8 9 10 mixin template VisitorInterface() { 11 override void accept(IVisitor visitor) { 12 visitor.visit(this); 13 } 14 } 15 16 17 class Node : IVisitorNode { 18 this(Args...)(Token tok, Args args) { 19 this.tok = tok; 20 21 static if (args.length) { 22 static if ((args.length == 1) && isArray!(Args[0])) { 23 children = args[0]; 24 } else { 25 children.reserve(args.length); 26 foreach (arg; args) 27 children ~= arg; 28 } 29 } 30 } 31 32 override void accept(IVisitor visitor) { 33 assert(false, "unsupported"); 34 } 35 36 Token tok; 37 Node[] children; 38 } 39 40 41 class Expression : Node { 42 this(Token tok, Node expr, bool parens = false) { 43 super(tok, expr); 44 45 parenthesis = parens; 46 } 47 48 mixin VisitorInterface; 49 50 bool parenthesis; 51 } 52 53 54 class Constant : Node { 55 this(Token tok) { 56 super(tok); 57 } 58 59 mixin VisitorInterface; 60 } 61 62 63 class Identifier : Node { 64 this(Token tok) { 65 super(tok); 66 } 67 68 mixin VisitorInterface; 69 } 70 71 72 class UnaryOp : Node { 73 this(Token tok, Node expr) { 74 super(tok, expr); 75 } 76 77 mixin VisitorInterface; 78 } 79 80 81 class PrefixOp : Node { 82 this(Token tok, Node expr) { 83 super(tok, expr); 84 } 85 86 mixin VisitorInterface; 87 } 88 89 90 class SuffixOp : Node { 91 this(Token tok, Node expr) { 92 super(tok, expr); 93 } 94 95 mixin VisitorInterface; 96 } 97 98 99 class BinaryOp : Node { 100 this(Token tok, Node lexpr, Node rexpr) { 101 super(tok, lexpr, rexpr); 102 } 103 104 mixin VisitorInterface; 105 } 106 107 108 class ConditionalExpression : Node { 109 this(Token tok, Node condition, Node trueCase, Node falseCase) { 110 super(tok, condition, trueCase, falseCase); 111 } 112 113 mixin VisitorInterface; 114 } 115 116 117 class IndexOp : Node { 118 this(Token tok, Node expr, Node index) { 119 super(tok, expr, index); 120 } 121 122 mixin VisitorInterface; 123 } 124 125 126 class SliceOp : Node { 127 this(Token tok, Node expr, Node start, Node end) { 128 super(tok, expr, start, end); 129 } 130 131 mixin VisitorInterface; 132 } 133 134 135 class DispatchOp : Node { 136 this(Token tok, Node expr, Token target) { 137 super(tok, expr); 138 139 this.target = target; 140 } 141 142 mixin VisitorInterface; 143 144 Token target; 145 } 146 147 148 class FunctionCall : Node { 149 this(Token tok, Node call, Node[] args) { 150 super(tok, call ~ args); 151 } 152 153 mixin VisitorInterface; 154 } 155 156 157 class Output : Node { 158 this(Token tok, Node expr) { 159 super(tok, expr); 160 } 161 162 mixin VisitorInterface; 163 } 164 165 166 class StatementBlock : Node { 167 this(Token tok, Node[] children = null) { 168 super(tok, children); 169 } 170 171 mixin VisitorInterface; 172 } 173 174 175 class WithStatement : Node { 176 this(Token tok, Node[] exprs, Node body_) { 177 super(tok, exprs ~ body_); 178 } 179 180 mixin VisitorInterface; 181 } 182 183 184 class WithExpression : Node { 185 this(Token tok, Node expr, Token name) { 186 super(tok, expr); 187 188 this.name = name; 189 } 190 191 mixin VisitorInterface; 192 193 Token name; 194 } 195 196 197 class IfStatement : Node { 198 this(Token tok, Node expr, Node trueCase, Node elseCase) { 199 super(tok, expr, trueCase, elseCase); 200 } 201 202 mixin VisitorInterface; 203 } 204 205 206 class LoopStatement : Node { 207 this(Token tok, Token key, Token value, Node obj, Node end, Node body_) { 208 super(tok, obj, end, body_); 209 210 this.key = key; 211 this.value = value; 212 } 213 214 mixin VisitorInterface; 215 216 Token key; 217 Token value; 218 } 219 220 221 class Module : Node { 222 this(Token tok, Node[] children) { 223 super(tok, children); 224 } 225 226 mixin VisitorInterface; 227 } 228 229 230 auto create(T, Args...)(Args args) { 231 return new T(args); 232 }