// Parses the variable assignation public int Parse(string[] parts, int start) { // Variable is not declared if (!Reserved.dEx.Exists(p => p.desigName == parts[start])) { throw new Exception("Desig '" + parts[start] + "' is not declared!"); } // The second part is not proper assigning operator else if (!desigOps.Contains(parts[start + 1])) { throw new Exception("Desig assignation has improper assigning operator '" + parts[start + 1] + "'!"); } // Get expression else { // Save operator op = parts[start + 1]; // Save desig desig = Reserved.dEx.Find(d => d.desigName == parts[start]); // Identify type IntoAST.IdentifyResponse ir = IntoAST.Identify("desig expression", context, parts, start + 2); // Check type if (ir.IChild is IChildDesigAssignation icda) { // Move to next start = ir.next; exp = icda; } else { throw new Exception("Invalid desig subexpression '" + ir.IChild + "'"); } // Return index after expression return(start); } }
// Parses into variable public int Parse(string[] parts, int start) { // Ends with ; if (parts[start + 1] != ";") { throw new Exception("Subexpression ends with " + parts[start] + " not ';'!"); } DesigDeclaration desDec = Reserved.dEx.Find(d => d.desigName == parts[start]); // Checks if desig name if (desDec == null) { throw new Exception(parts[start] + " is not a desig name!"); } else { // Is a desig name dd = desDec; // Return position after return(start + 2); } }
// Identifies type given and parses into type. expContext is context that it will run on public static IdentifyResponse Identify(string context, string expContext, string[] parts, int start) { // If it is a reserved keyword if (Reserved.reservedKeywords.Contains(parts[start])) { /* Main File */ // If declaring desig if (context == "mainfile" && parts[start] == "desig") { DesigDeclaration dd = new DesigDeclaration(); return(new IdentifyResponse(dd, expContext, dd.Parse(parts, start))); } // If declaring variable else if (context == "mainfile" && parts[start] == "var") { VariableDeclaration vd = new VariableDeclaration(); return(new IdentifyResponse(vd, expContext, vd.Parse(parts, start))); } // If declaring function else if (context == "mainfile" && parts[start] == "func") { FunctionDeclaration fd = new FunctionDeclaration(); return(new IdentifyResponse(fd, expContext, fd.Parse(parts, start))); } // If declaring ruling else if (context == "mainfile" && parts[start] == "rule") { RulingResponse rr = Ruling.IdentifyRuling(parts, start); return(new IdentifyResponse(rr.icmf, expContext, rr.start)); } /* Function */ // If it is a function call else if (context == "function" && parts[start] == "call") { FuncCall fc = new FuncCall(); fc.context = expContext; return(new IdentifyResponse(fc, expContext, fc.Parse(parts, start))); } // If it is a context change else if (context == "function" && parts[start] == "context") { ContextChange cc = new ContextChange(); cc.context = expContext; return(new IdentifyResponse(cc, expContext, cc.Parse(parts, start))); } // If it is a specified command else if (context == "function" && parts[start] == "run") { RunCommand rc = new RunCommand(); rc.context = expContext; return(new IdentifyResponse(rc, expContext, rc.Parse(parts, start))); } /* Variable Assignation */ // If it is a literal else if (context == "variable expression" && parts[start] == "lit") { Literal lit = new Literal(); return(new IdentifyResponse(lit, expContext, lit.Parse(parts, start))); } /* Desig Assignation */ // If it is a literal else if (context == "desig expression" && parts[start] == "lit") { Literal lit = new Literal(); return(new IdentifyResponse(lit, expContext, lit.Parse(parts, start))); } } // Else if it is a Var assignation else if (context == "function" && Reserved.vEx.Exists(vd => vd.varName == parts[start])) { VariableAssignation va = new VariableAssignation(); va.context = expContext; return(new IdentifyResponse(va, expContext, va.Parse(parts, start))); } // Else if it is a Desig assignation else if (context == "function" && Reserved.dEx.Exists(dd => dd.desigName == parts[start])) { DesigAssignation da = new DesigAssignation(); da.context = expContext; return(new IdentifyResponse(da, expContext, da.Parse(parts, start))); } // If it is a variable else if (context == "variable expression" && Reserved.vEx.Exists(v => v.varName == parts[start])) { VarSubExpression vse = new VarSubExpression(); return(new IdentifyResponse(vse, expContext, vse.Parse(parts, start))); } // If it is a variable else if (context == "desig expression" && Reserved.vEx.Exists(v => v.varName == parts[start])) { VarSubExpression vse = new VarSubExpression(); return(new IdentifyResponse(vse, expContext, vse.Parse(parts, start))); } // If it is a desig else if (context == "desig expression" && Reserved.dEx.Exists(d => d.desigName == parts[start])) { DesigSubExpression dse = new DesigSubExpression(); return(new IdentifyResponse(dse, expContext, dse.Parse(parts, start))); } // Nonsensical answer throw new Exception("Inrecognized expression '" + parts[start] + "' in context '" + context + "'"); }