1 /* 2 * ParseTree.cs 3 * 4 * This source file is part of the FoundationDB open source project 5 * 6 * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 using System; 22 using System.Collections.Generic; 23 using System.Linq; 24 using System.Text; 25 using System.Text.RegularExpressions; 26 27 namespace actorcompiler 28 { 29 class VarDeclaration 30 { 31 public string type; 32 public string name; 33 public string initializer; 34 public bool initializerConstructorSyntax; 35 }; 36 37 abstract class Statement 38 { 39 public int FirstSourceLine; containsWait()40 public virtual bool containsWait() 41 { 42 return false; 43 } 44 }; 45 class PlainOldCodeStatement : Statement 46 { 47 public string code; ToString()48 public override string ToString() 49 { 50 return code; 51 } 52 }; 53 class StateDeclarationStatement : Statement 54 { 55 public VarDeclaration decl; ToString()56 public override string ToString() 57 { 58 if (decl.initializerConstructorSyntax) 59 return string.Format("State {0} {1}({2});", decl.type, decl.name, decl.initializer); 60 else 61 return string.Format("State {0} {1} = {2};", decl.type, decl.name, decl.initializer); 62 } 63 }; 64 class WhileStatement : Statement 65 { 66 public string expression; 67 public Statement body; containsWait()68 public override bool containsWait() 69 { 70 return body.containsWait(); 71 } 72 }; 73 class ForStatement : Statement 74 { 75 public string initExpression = ""; 76 public string condExpression = ""; 77 public string nextExpression = ""; 78 public Statement body; containsWait()79 public override bool containsWait() 80 { 81 return body.containsWait(); 82 } 83 }; 84 class RangeForStatement : Statement 85 { 86 public string rangeExpression; 87 public string rangeDecl; 88 public Statement body; containsWait()89 public override bool containsWait() 90 { 91 return body.containsWait(); 92 } 93 }; 94 class LoopStatement : Statement 95 { 96 public Statement body; ToString()97 public override string ToString() 98 { 99 return "Loop " + body.ToString(); 100 } containsWait()101 public override bool containsWait() 102 { 103 return body.containsWait(); 104 } 105 }; 106 class BreakStatement : Statement 107 { 108 }; 109 class ContinueStatement : Statement 110 { 111 }; 112 class IfStatement : Statement 113 { 114 public string expression; 115 public Statement ifBody; 116 public Statement elseBody; // might be null containsWait()117 public override bool containsWait() 118 { 119 return ifBody.containsWait() || (elseBody != null && elseBody.containsWait()); 120 } 121 }; 122 class ReturnStatement : Statement 123 { 124 public string expression; ToString()125 public override string ToString() 126 { 127 return "Return " + expression; 128 } 129 }; 130 class WaitStatement : Statement 131 { 132 public VarDeclaration result; 133 public string futureExpression; 134 public bool resultIsState; 135 public bool isWaitNext; ToString()136 public override string ToString() 137 { 138 return string.Format("Wait {0} {1} <- {2} ({3})", result.type, result.name, futureExpression, resultIsState ? "state" : "local"); 139 } containsWait()140 public override bool containsWait() 141 { 142 return true; 143 } 144 }; 145 class ChooseStatement : Statement 146 { 147 public Statement body; ToString()148 public override string ToString() 149 { 150 return "Choose " + body.ToString(); 151 } containsWait()152 public override bool containsWait() 153 { 154 return body.containsWait(); 155 } 156 }; 157 class WhenStatement : Statement 158 { 159 public WaitStatement wait; 160 public Statement body; ToString()161 public override string ToString() 162 { 163 return string.Format("When ({0}) {1}", wait, body); 164 } containsWait()165 public override bool containsWait() 166 { 167 return true; 168 } 169 }; 170 class TryStatement : Statement 171 { 172 public struct Catch 173 { 174 public string expression; 175 public Statement body; 176 public int FirstSourceLine; 177 }; 178 179 public Statement tryBody; 180 public List<Catch> catches; containsWait()181 public override bool containsWait() 182 { 183 if (tryBody.containsWait()) 184 return true; 185 foreach (Catch c in catches) 186 if (c.body.containsWait()) 187 return true; 188 return false; 189 } 190 }; 191 class ThrowStatement : Statement 192 { 193 public string expression; 194 }; 195 196 class CodeBlock : Statement 197 { 198 public Statement[] statements; ToString()199 public override string ToString() 200 { 201 return string.Join("\n", 202 new string[] { "CodeBlock" } 203 .Concat(statements.Select(s => s.ToString())) 204 .Concat(new string[] { "EndCodeBlock" }) 205 .ToArray()); 206 } containsWait()207 public override bool containsWait() 208 { 209 foreach (Statement s in statements) 210 if (s.containsWait()) 211 return true; 212 return false; 213 } 214 }; 215 216 217 class Declaration 218 { 219 public string type; 220 public string name; 221 public string comment; 222 }; 223 224 class Actor 225 { 226 public string returnType; 227 public string name; 228 public VarDeclaration[] parameters; 229 public VarDeclaration[] templateFormals; //< null if not a template 230 public CodeBlock body; 231 public int SourceLine; 232 public bool isStatic = false; 233 public bool isUncancellable = false; 234 public string testCaseParameters = null; 235 public string nameSpace = null; 236 public bool isForwardDeclaration = false; 237 public bool isTestCase = false; 238 }; 239 240 class Descr 241 { 242 public string name; 243 public string superClassList; 244 public List<Declaration> body; 245 }; 246 }; 247