private static Logic[] splitPackagesAndStatements(Logic[] logicOrder, int lineNumber, Scope currentScope) { List <Logic> newOrder = new List <Logic> (); for (int i = 0; i < logicOrder.Length; i++) { if (logicOrder [i].currentType == WordTypes.functionCall && SpecialWordParser.isKeyWord((logicOrder [i] as FunctionCall).name)) { Package temp = new Package("(" + (logicOrder [i] as FunctionCall).parameter + ")", lineNumber); temp.logicOrder = WordsToLogicParser.determineLogicFromWords(new string[] { temp.word }, lineNumber, currentScope); if ((logicOrder [i] as FunctionCall).name == "if") { newOrder.Add(new IfStatement()); newOrder.Add(temp); } else if ((logicOrder [i] as FunctionCall).name == "while") { newOrder.Add(new WhileLoop()); newOrder.Add(temp); } } else { newOrder.Add(logicOrder [i]); } } return(newOrder.ToArray()); }
static void setScopeStarterTargets(Scope currentScope) { int currentChildScope = 0; int linkCounter = 0; for (int i = 0; i < currentScope.codeLines.Count; i++) { CodeLine line = currentScope.codeLines [i]; if (SpecialWordParser.isValidScopeStarter(line.logicOrder, line.lineNumber)) { if (currentChildScope > currentScope.childScopes.Count - 1) { ErrorMessage.sendErrorMessage(line.lineNumber, ErrorType.System, SystemFailureErrorType.scopeParsingMalfunction.ToString(), null); } (line.logicOrder [0] as ScopeStarter).setTargetScope(currentScope.childScopes [currentChildScope]); currentChildScope++; // Links else/elif if (line.logicOrder [0] is ComparisonScope) { if (i == 0 && line.logicOrder [0] is IfStatement == false) { ErrorMessage.sendErrorMessage(line.lineNumber, ErrorType.ElseStatements, ElseErrorType.missingIfBeforeElse.ToString(), null); } if (line.logicOrder [0] is IfStatement == false) { linkElseStatement(line.logicOrder [0], currentScope.codeLines [i - 1].logicOrder [0], currentScope, linkCounter, line.lineNumber); } linkCounter++; } else { linkCounter = 0; } } } foreach (Scope tempScope in currentScope.childScopes) { setScopeStarterTargets(tempScope); } }
public static void checkScopeStarters(List <CodeLine> programLines, Scope mainScope) { int expectedIndent = 0; bool expectedHigherIndent = true; bool isFirst = true; for (int i = 0; i < programLines.Count; i++) { if (expectedHigherIndent) { if (isFirst) { if (programLines [i].indentLevel != 0) { ErrorMessage.sendErrorMessage(programLines [i].lineNumber, ErrorType.Indentation, IndentationErrorType.firstLineIndentError.ToString(), null); } isFirst = false; } else { if (programLines [i].indentLevel != expectedIndent) { ErrorMessage.sendErrorMessage(programLines [i].lineNumber, ErrorType.Indentation, IndentationErrorType.indentationError.ToString(), null); } } } if (SpecialWordParser.isValidScopeStarter(programLines [i].logicOrder, programLines [i].lineNumber)) { expectedIndent = programLines [i].indentLevel + 1; expectedHigherIndent = true; if (i == programLines.Count - 1) { ErrorMessage.sendErrorMessage(programLines [i].lineNumber, ErrorType.Indentation, IndentationErrorType.expectingBodyAfterScopeStarter.ToString(), null); } } else { expectedHigherIndent = false; } } setScopeStarterTargets(mainScope); }
// Could be optimized so that we save left, right and operators for next comparision so we do not need to parse every loop instance! public static Logic parseDefStatement(Logic[] logicOrder, int lineNumber, Scope currentScope) { FunctionCall theFuncCall = logicOrder [1] as FunctionCall; if (SpecialWordParser.isKeyWord(theFuncCall.name)) { ErrorMessage.sendErrorMessage(lineNumber, string.Format("\"{0}\" är ett Python keyword, du kan därför inte döpa en funktion till det", theFuncCall.name)); } int paraAmount = (FunctionParser.getParameterAmount(theFuncCall.parameter, lineNumber, currentScope)); UserFunction theFunc = new UserFunction(theFuncCall.name, (logicOrder [0] as ScopeStarter).getTargetScope(), paraAmount); currentScope.scopeFunctions.addFunction(theFunc); theFunc.inputParameterNames = FunctionParser.getParameterNames(theFuncCall.parameter, lineNumber, currentScope); return(new DefStatement()); }
private static Logic getCurrentLogic(string word, int lineNumber, Scope currentScope) { if (word == ",") { return(new CommaSign()); } if (word == ">") { return(new GreaterThenSign()); } if (word == "<") { return(new LessThenSign()); } if (word == ":") { return(new IndentOperator()); } if (word == "!") { return(new XorOperator(word)); } if (word == "not") { return(new NotOperator(word)); } if (word == "or") { return(new OrOperator(word)); } if (word == "and") { return(new AndOperator(word)); } if (isBooleanValue(word) == 1) { return(new BooleanValue(true)); } if (isBooleanValue(word) == 0) { return(new BooleanValue(false)); } if (isNumber(word)) { return(new NumberValue(word)); } if (isString(word)) { return(new TextValue(word)); } if (SpecialWordParser.isKeyWord(word)) { SpecialWordParser.checkIfUnsupportedKeyword(word, lineNumber); WordTypes specialType = SpecialWordParser.getSpecialType(word, lineNumber); if (specialType == WordTypes.forLoop) { return(new ForLoop()); } if (specialType == WordTypes.whileLoop) { return(new WhileLoop()); } if (specialType == WordTypes.ifOperator) { return(new IfStatement()); } if (specialType == WordTypes.elifOperator) { return(new ElifStatement()); } if (specialType == WordTypes.elseOperator) { return(new ElseStatement()); } if (specialType == WordTypes.defStatement) { return(new DefStatement()); } if (specialType == WordTypes.returnStatement) { return(new ReturnStatement()); } if (specialType == WordTypes.breakStatement) { return(new BreakStatement()); } if (specialType == WordTypes.continueStatement) { return(new ContinueStatment()); } } if (startsWithDigitOrWeirdChar(word) == false) { if (endsWithParantes(word)) { Logic temp = FunctionParser.parseIntoFunctionCall(word, lineNumber, currentScope); if (temp != null) { return(temp); } } else { if (containsButNotStartWithDigitWeirdChar(word) == false) { return(checkVariable(word, currentScope.scopeVariables)); } } } if (word.Length == 1) { if (word [0] == '=') { return(new EqualSign()); } if (isMathOperator(word[0])) { return(new MathOperator(word)); } } if (isPackage(word)) { return(new Package(word, lineNumber)); } return(new UnknownLogic(lineNumber)); }