public static IListable BuildListTernary(List <Token> tokens) { IBoolable condition = BoolableBuilder.Build(GetTernaryCondition(tokens)); if (condition.IsNull()) { return(null); } IListable confirmationCase = ListableBuilder.Build(GetTernaryConfirmation(tokens)); if (confirmationCase.IsNull()) { return(null); } IListable negationCase = ListableBuilder.Build(GetTernaryNegation(tokens)); if (negationCase.IsNull()) { return(null); } return(new ListTernary(condition, confirmationCase, negationCase)); }
private static INumerable BuildCount(List <Token> tokens) { List <Token> laterTokens = tokens.Skip(1).ToList(); if (laterTokens.Count == 0) { throw new SyntaxErrorException("ERROR! Expression 'count' do not contain all necessary information."); } if (TokenGroups.ContainsTokenOutsideBrackets(laterTokens, TokenType.Inside)) { return(BuildCountInside(laterTokens)); } else { IListable ilist = ListableBuilder.Build(laterTokens); if (ilist.IsNull()) { return(null); } else { return(new Count(ilist)); } } }
public static IStringable BuildLis(string name, List <Argument> args) { if (args.Count != 1) { throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 list argument."); } IListable ilis = ListableBuilder.Build(args[0].tokens); if (ilis.IsNull()) { throw new SyntaxErrorException("ERROR! Argument of function " + name + " cannot be read as list."); } if (name.Equals("commonbeginning")) { return(new FuncCommonbeginning(ilis)); } else if (name.Equals("commonending")) { return(new FuncCommonending(ilis)); } else if (name.Equals("concatenate") || name.Equals("concatenated")) { return(new FuncConcatenate(ilis)); } throw new SyntaxErrorException("ERROR! Function " + name + " not identified."); }
public static IBoolable BuildIn(List <Token> tokens) { int index = tokens.TakeWhile(x => !x.GetTokenType().Equals(TokenType.In)).Count(); if (index == 0 || index == tokens.Count - 1) { return(null); } List <Token> leftTokens = tokens.GetRange(0, index); List <Token> rightTokens = tokens.GetRange(index + 1, tokens.Count - index - 1); IStringable istr = StringableBuilder.Build(leftTokens); if (istr.IsNull()) { return(null); } IListable ilis = ListableBuilder.Build(rightTokens); if (ilis.IsNull()) { return(null); } return(new In(istr, ilis)); }
public static IBoolable BuildLisStr(string name, List <Argument> args) { if (args.Count != 2) { throw new SyntaxErrorException("ERROR! Function " + name + " has to have 2 arguments: one list and one text."); } IListable ilis = ListableBuilder.Build(args[0].tokens); IStringable istr = StringableBuilder.Build(args[1].tokens); if (ilis.IsNull()) { throw new SyntaxErrorException("ERROR! First argument of function " + name + " cannot be read as list."); } if (istr.IsNull()) { throw new SyntaxErrorException("ERROR! Second argument of function " + name + " cannot be read as text."); } if (name.Equals("contain") || name.Equals("contains")) { return(new FuncContain(ilis, istr)); } throw new SyntaxErrorException("ERROR! Function " + name + " not identified."); }
public static ICommand BuildCreateUsual(List <Token> tokens, bool forced, bool directory) { if (tokens.Count == 0) { if (directory) { return(new CreateDirectory(new StringVariableRefer("this"), forced)); } else { return(new CreateFile(new StringVariableRefer("this"), forced)); } } IListable ilist = ListableBuilder.Build(tokens); if (ilist.IsNull()) { throw new SyntaxErrorException("ERROR! There are is something wrong with " + (directory ? "directory" : "file") + " creation command syntax."); } if (directory) { return(new CreateDirectory(ilist, forced)); } else { return(new CreateFile(ilist, forced)); } }
public static ICommand Build(List <Token> tokens, bool forced) { TokenType type = tokens[0].GetTokenType(); tokens.RemoveAt(0); if (tokens.Where(t => t.GetTokenType().Equals(TokenType.To)).Count() > 1) { throw new SyntaxErrorException("ERROR! In " + GetName(type) + " command keyword 'to' occurs too many times."); } List <Token> part1 = new List <Token>(); List <Token> part2 = new List <Token>(); bool pastTo = false; foreach (Token tok in tokens) { if (tok.GetTokenType().Equals(TokenType.To)) { pastTo = true; } else { if (pastTo) { part2.Add(tok); } else { part1.Add(tok); } } } if (part2.Count == 0) { throw new SyntaxErrorException("ERROR! Command " + GetName(type) + " is too short and do not contain all necessary information."); } IStringable expression2 = StringableBuilder.Build(part2); if (expression2.IsNull()) { throw new SyntaxErrorException("ERROR! Second part of command " + GetName(type) + " cannot be read as text."); } if (part1.Count == 0) { return(BuildSimple(type, expression2, forced)); } else { IListable expression1 = ListableBuilder.Build(part1); if (expression1.IsNull()) { throw new SyntaxErrorException("ERROR! First part of command " + GetName(type) + " cannot be read as list."); } return(BuildComplex(type, expression1, expression2, forced)); } }
private static INumerable BuildCountInside(List <Token> tokens) { int index = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.Inside); if (index == tokens.Count - 1) { throw new SyntaxErrorException("ERROR! Expression 'count inside' do not contain information about referent location."); } if (index == 0) { throw new SyntaxErrorException("ERROR! Expression 'count inside' do not contain information about referent list of elements."); } IListable ilist = ListableBuilder.Build(tokens.Take(index).ToList()); if (ilist.IsNull()) { return(null); } IStringable istr = StringableBuilder.Build(tokens.Skip(index + 1).ToList()); if (istr.IsNull()) { return(null); } return(new CountInside(ilist, istr)); }
public static ICommand Build(List <Token> tokens, bool forced) { TokenType type = tokens[0].GetTokenType(); tokens.RemoveAt(0); int toIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.To); int asIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.As); if (asIndex < toIndex) { return(null); } if (toIndex == asIndex - 1) { throw new SyntaxErrorException("ERROR! Command " + GetName(type) + " do not have definition of destination directory."); } if (asIndex == tokens.Count - 1) { throw new SyntaxErrorException("ERROR! Command " + GetName(type) + " do not have definition of new name for file/directory."); } List <Token> listTokens = tokens.Take(toIndex).ToList(); List <Token> destinationTokens = tokens.GetRange(toIndex + 1, asIndex - toIndex - 1); List <Token> nameTokens = tokens.Skip(asIndex + 1).ToList(); IStringable destination = StringableBuilder.Build(destinationTokens); if (destination.IsNull()) { throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of destination directory cannot be read as text."); } IStringable name = StringableBuilder.Build(nameTokens); if (name.IsNull()) { throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of new name for file/directory cannot be read as text."); } if (listTokens.Count == 0) { return(BuildSimple(type, destination, name, forced)); } else { IListable list = ListableBuilder.Build(listTokens); if (list.IsNull()) { throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of list of files and directories is not correct."); } return(BuildComplex(type, list, destination, name, forced)); } }
public static ISubcommand BuildWith(List <Token> tokens, bool negated) { string name = negated ? "without" : "with"; IListable ilis = ListableBuilder.Build(tokens); if (ilis.IsNull()) { throw new SyntaxErrorException("ERROR! In list declaration there is something wrong with expression: " + name + "."); } else { return(new With(ilis, negated)); } }
public static ICommand Build(List <Token> tokens) { if (tokens.Count == 0) { return(new Print(new StringVariableRefer("this"))); } IListable ilist = ListableBuilder.Build(tokens); if (ilist.IsNull()) { throw new SyntaxErrorException("ERROR! There are is something wrong with print command."); } else { return(new Print(ilist)); } }
private static IBoolable BuildComparison(List <Token> tokens) { int index = tokens.TakeWhile(x => !TokenGroups.IsComparingSign(x.GetTokenType())).Count(); if (index == 0 || index == tokens.Count - 1) { return(null); } ComparisonType type = GetComparingToken(tokens); List <Token> leftTokens = tokens.GetRange(0, index); List <Token> rightTokens = tokens.GetRange(index + 1, tokens.Count - index - 1); IListable leftL = ListableBuilder.Build(leftTokens); IListable rightL = ListableBuilder.Build(rightTokens); if (leftL.IsNull() || rightL.IsNull()) { return(null); } if (leftL is INumerable && rightL is INumerable) { return(new NumericComparison(leftL as INumerable, rightL as INumerable, type)); } if (leftL is ITimeable && rightL is ITimeable) { return(new TimeComparison(leftL as ITimeable, rightL as ITimeable, type)); } if (leftL is IStringable && rightL is IStringable) { return(new Uroboros.syntax.expressions.bools.comparisons.StringComparison(leftL as IStringable, rightL as IStringable, type)); } if (leftL is IListable && rightL is IListable) { return(new ListComparison(leftL as IListable, rightL as IListable, type)); } return(null); }
public static IBoolable BuildLis(string name, List <Argument> args) { if (args.Count != 1) { throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 list argument."); } IListable ilis = ListableBuilder.Build(args[0].tokens); if (ilis.IsNull()) { throw new SyntaxErrorException("ERROR! Argument of function " + name + " cannot be read as list."); } if (name.Equals("emptylist") || name.Equals("listisempty")) { return(new FuncEmptylist(ilis)); } throw new SyntaxErrorException("ERROR! Function " + name + " not identified."); }
public static ICommand Build(List <Token> tokens) { TokenType type = tokens[0].GetTokenType(); tokens.RemoveAt(0); if (tokens.Count == 0) { return(BuildSimple(type)); } IListable ilist = ListableBuilder.Build(tokens); if (ilist.IsNull()) { throw new SyntaxErrorException("ERROR! There are is something wrong with elements declaration in " + GetName(type) + " command."); } else { return(BuildComplex(type, ilist)); } }
public static INumerable BuildLis(string name, List <Argument> args) { if (args.Count != 1) { throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 list argument."); } IListable ilis = ListableBuilder.Build(args[0].tokens); if (ilis.IsNull()) { throw new SyntaxErrorException("ERROR! Argument of function " + name + " cannot be read as list."); } else if (name.Equals("lengthofshortest")) { return(new FuncLengthofshortest(ilis)); } else if (name.Equals("lengthoflongest")) { return(new FuncLengthoflongest(ilis)); } throw new SyntaxErrorException("ERROR! Function " + name + " not identified."); }
public static ICommand Build(List <Token> tokens) { TokenType type = tokens[0].GetTokenType(); tokens.RemoveAt(0); if (!tokens.Any(t => t.GetTokenType().Equals(TokenType.To))) { throw new SyntaxErrorException("ERROR! Command 'add' do not contain keyword 'to'."); } if (tokens.Where(t => t.GetTokenType().Equals(TokenType.To)).Count() > 1) { throw new SyntaxErrorException("ERROR! In command 'add' keyword 'to' occurs too many times."); } List <Token> part1 = new List <Token>(); List <Token> part2 = new List <Token>(); bool pastTo = false; foreach (Token tok in tokens) { if (tok.GetTokenType().Equals(TokenType.To)) { pastTo = true; } else { if (pastTo) { part2.Add(tok); } else { part1.Add(tok); } } } if (part2.Count == 0) { throw new SyntaxErrorException("ERROR! Command 'add' do not contain definition for target variable."); } if (part2.Count > 2 || !part2[0].GetTokenType().Equals(TokenType.Variable)) { throw new SyntaxErrorException("ERROR! Target variable in command 'add' cannot be read."); } string name = part2[0].GetContent(); if (!InterVariables.GetInstance().ContainsChangable(name, InterVarType.List)) { throw new SyntaxErrorException("ERROR! In command 'add' variable " + name + " do not exist or cannot be read as list."); } // add this if (part1.Count == 0) { return(new AddString(name, new StringVariableRefer("this") as IStringable)); } IListable ilist = ListableBuilder.Build(part1); if (ilist.IsNull()) { throw new SyntaxErrorException("ERROR! In command 'add' definition for elements to add cannot be read as list."); } // turn variable to list if it was string if (InterVariables.GetInstance().ContainsChangable(name, InterVarType.String)) { InterVariables.GetInstance().TurnToList(name); } if (ilist is IStringable) { return(new AddString(name, ilist as IStringable)); } else { return(new AddList(name, ilist)); } }
public static List <ICommand> Build(List <Token> tokens) { List <Token> currentTokens = new List <Token>(); List <ICommand> commands = new List <ICommand>(); int waitingArrowsEndings = 0; int arrowDepth = 0; foreach (Token tok in tokens) { if (tok.GetTokenType().Equals(TokenType.Semicolon)) { if (currentTokens.Count > 0) { commands.Add(SingleCommandFactory.Build(currentTokens)); currentTokens.Clear(); } if (arrowDepth == 0 && waitingArrowsEndings > 0) { waitingArrowsEndings.Times(() => commands.Add(new BracketOff())); waitingArrowsEndings = 0; } } else if (tok.GetTokenType().Equals(TokenType.CurlyBracketOff)) { if (currentTokens.Count > 0) { commands.Add(SingleCommandFactory.Build(currentTokens)); currentTokens.Clear(); } commands.Add(new BracketOff()); InterVariables.GetInstance().BracketsDown(); if (waitingArrowsEndings > 0) { arrowDepth--; } if (arrowDepth == 0 && waitingArrowsEndings > 0) { waitingArrowsEndings.Times(() => commands.Add(new BracketOff())); waitingArrowsEndings = 0; } } else if (tok.GetTokenType().Equals(TokenType.CurlyBracketOn)) { if (currentTokens.Count == 0) { commands.Add(new EmptyOpenning()); } else { TokenType first = currentTokens.First().GetTokenType(); if (first.Equals(TokenType.If)) { currentTokens.RemoveAt(0); if (currentTokens.Count == 0) { throw new SyntaxErrorException("ERROR! Expression 'if' is empty."); } IBoolable iboo = BoolableBuilder.Build(currentTokens); if (iboo.IsNull()) { throw new SyntaxErrorException("ERROR! There is something wrong with expression 'if'."); } commands.Add(new IfOpenning(iboo, commands.Count)); currentTokens.Clear(); } else if (first.Equals(TokenType.Else)) { currentTokens.RemoveAt(0); if (currentTokens.Count == 0) { commands.Add(new ElseOpenning()); } else { throw new SyntaxErrorException("ERROR! Expression 'else' contains not necessary code."); } } else if (first.Equals(TokenType.While)) { currentTokens.RemoveAt(0); if (currentTokens.Count == 0) { throw new SyntaxErrorException("ERROR! Expression 'while' is empty."); } IBoolable iboo = BoolableBuilder.Build(currentTokens); if (iboo.IsNull()) { throw new SyntaxErrorException("ERROR! There is something wrong with expression 'while'."); } commands.Add(new WhileOpenning(iboo, commands.Count)); currentTokens.Clear(); } else if (first.Equals(TokenType.Inside)) { currentTokens.RemoveAt(0); if (currentTokens.Count == 0) { throw new SyntaxErrorException("ERROR! Expression 'inside' is empty."); } IListable ilist = ListableBuilder.Build(currentTokens); if (ilist.IsNull()) { throw new SyntaxErrorException("ERROR! There is something wrong with expression 'inside'."); } commands.Add(new InsideOpenning(ilist, commands.Count)); currentTokens.Clear(); } else { IListable ilist = ListableBuilder.Build(currentTokens); if (ilist.IsNull()) { throw new SyntaxErrorException("ERROR! There is something wrong with List Loop / Numeric Loop."); } if (ilist is INumerable) { commands.Add(new NumericLoopOpenning(ilist as INumerable, commands.Count)); } else { commands.Add(new ListLoopOpenning(ilist, commands.Count)); } currentTokens.Clear(); } } InterVariables.GetInstance().BracketsUp(); if (waitingArrowsEndings > 0) { arrowDepth++; } } else if (tok.GetTokenType().Equals(TokenType.BigArrow)) { if (currentTokens.Count == 0) { throw new SyntaxErrorException("ERROR! Left side of Big Arrow Function is empty."); } else { IListable ilist = ListableBuilder.Build(currentTokens); if (ilist.IsNull()) { throw new SyntaxErrorException("ERROR! There is something wrong with List Loop / Numeric Loop."); } if (ilist is INumerable) { commands.Add(new NumericLoopOpenning(ilist as INumerable, commands.Count)); } else { commands.Add(new ListLoopOpenning(ilist, commands.Count)); } currentTokens.Clear(); waitingArrowsEndings++; } } else { currentTokens.Add(tok); } } if (currentTokens.Count > 0) { commands.Add(SingleCommandFactory.Build(currentTokens)); } if (waitingArrowsEndings > 0) { waitingArrowsEndings.Times(() => commands.Add(new BracketOff())); } return(commands); }
private static IListable BuildSmallArrowFunction(List <Token> tokens) { bool unique = false; List <Token> leftSide = new List <Token>(); List <Token> rightSide = new List <Token>(); bool pastTo = false; foreach (Token tok in tokens) { if (tok.GetTokenType().Equals(TokenType.SmallArrow)) { pastTo = true; } else { if (pastTo) { rightSide.Add(tok); } else { leftSide.Add(tok); } } } if (leftSide.Count == 0) { throw new SyntaxErrorException("ERROR! Left side of Small Arrow Function is empty."); } if (rightSide.Count == 0) { throw new SyntaxErrorException("ERROR! Right side of Small Arrow Function is empty."); } if (rightSide.First().GetTokenType().Equals(TokenType.Unique)) { unique = true; rightSide.RemoveAt(0); if (rightSide.Count == 0) { throw new SyntaxErrorException("ERROR! Right side of Small Arrow Function contains only one word: unique."); } } IListable ilist = ListableBuilder.Build(leftSide); if (ilist.IsNull()) { return(null); } IStringable istr = StringableBuilder.Build(rightSide); if (istr.IsNull()) { return(null); } return(new SmallArrow(ilist, istr, unique)); }
public static IListable Build(List <Token> tokens) { // try to build Stringable IStringable ist = StringableBuilder.Build(tokens); if (!ist.IsNull()) { return(ist as IListable); } // remove first and last bracket if it is there while (tokens[0].GetTokenType().Equals(TokenType.BracketOn) && tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.BracketOff) && !Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal)) { List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList(); tokensCopy.RemoveAt(tokens.Count - 1); tokensCopy.RemoveAt(0); tokens = tokensCopy; } // try to build 'empty list' if (tokens.Count == 2 && tokens[0].GetTokenType().Equals(TokenType.Variable) && tokens[1].GetTokenType().Equals(TokenType.Variable) && tokens[0].GetContent().ToLower().Equals("empty") && tokens[1].GetContent().ToLower().Equals("list")) { return(new EmptyList()); } // try to build small arrow function if (tokens.Where(t => t.GetTokenType().Equals(TokenType.SmallArrow)).Count() == 1) { IListable smallArrow = BuildSmallArrowFunction(tokens); if (!smallArrow.IsNull()) { return(smallArrow); } } string str = tokens[0].GetContent(); // try to build list variable reference - just one word and it is a name if (tokens.Count == 1 && tokens[0].GetTokenType().Equals(TokenType.Variable) && InterVariables.GetInstance().Contains(str, InterVarType.List)) { return(new ListVariableRefer(str)); } // try to build list expression if (InterVariables.GetInstance().Contains(str, InterVarType.List)) { IListable listEx = BuildListExpression(tokens, str); if (!listEx.IsNull()) { return(listEx); } } // try to build list ternary if (TernaryBuilder.IsPossibleTernary(tokens)) { IListable ilist = TernaryBuilder.BuildListTernary(tokens); if (!ilist.IsNull()) { return(ilist); } } // try to build listed lists/strings: many Listables/Stringables divided by commas if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Comma)) { IListable listed = BuildListed(tokens); if (!listed.IsNull()) { return(listed); } } throw new SyntaxErrorException("ERROR! Unknown error in code syntax."); }
private static IListable BuildListed(List <Token> tokens) { List <Token> currentTokens = new List <Token>(); List <IListable> elements = new List <IListable>(); int level = 0; for (int i = 0; i < tokens.Count; i++) { if (tokens[i].GetTokenType().Equals(TokenType.BracketOn)) { level++; } if (tokens[i].GetTokenType().Equals(TokenType.BracketOff)) { level--; } if (tokens[i].GetTokenType().Equals(TokenType.Comma) && level == 0) { if (currentTokens.Count > 0) { IListable ilist = ListableBuilder.Build(currentTokens); currentTokens.Clear(); if (ilist.IsNull()) { return(null); } else { elements.Add(ilist); } } } else { currentTokens.Add(tokens[i]); } } if (currentTokens.Count > 0) { IListable ilist = ListableBuilder.Build(currentTokens); if (ilist.IsNull()) { return(null); } else { elements.Add(ilist); } } if (elements.Count == 0) { return(null); } if (elements.All(e => e is IStringable)) { if (elements.All(e => e is StringConstant)) { return(new ListConstant(elements.Select(e => e.ToString()).ToList())); } else { return(new ListedStringables(elements.Select(e => e as IStringable).ToList())); } } else { if (elements.All(e => e is StringConstant || e is ListConstant)) { return(new ListConstant(elements.Select(e => e.ToString()).ToList())); } else { return(new ListedListables(elements)); } } }
public static ICommand Build(List <Token> tokens) { string name = tokens[0].GetContent(); tokens.RemoveAt(0); tokens.RemoveAt(0); if (tokens.Count == 0) { throw new SyntaxErrorException("ERROR! Variable " + name + " has no assigned value."); } if (name.Contains('.')) { return(BuildWithPoint(tokens, name)); } if (!InterVariables.GetInstance().Contains(name)) { IListable value = ListableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! There are is something wrong with assigning value to variable " + name + "."); } if (value is IBoolable) { InterVariables.GetInstance().Add(name, InterVarType.Bool); return(new BoolDeclaration(name, (IBoolable)value)); } else if (value is INumerable) { InterVariables.GetInstance().Add(name, InterVarType.Number); return(new NumericDeclaration(name, (INumerable)value)); } else if (value is ITimeable) { InterVariables.GetInstance().Add(name, InterVarType.Time); return(new TimeDeclaration(name, (ITimeable)value)); } else if (value is IStringable) { InterVariables.GetInstance().Add(name, InterVarType.String); return(new StringDeclaration(name, (IStringable)value)); } else { InterVariables.GetInstance().Add(name, InterVarType.List); return(new ListDeclaration(name, value)); } } else { InterVar ivar = InterVariables.GetInstance().GetVar(name); if (ivar.IsBool()) { IBoolable value = BoolableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be logical."); } return(new BoolDeclaration(name, value)); } else { if (ivar.IsNumber()) { INumerable value = NumerableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be numeric."); } return(new NumericDeclaration(name, value)); } else if (ivar.IsTime()) { ITimeable value = TimeableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be time."); } return(new TimeDeclaration(name, value)); } else if (ivar.IsString()) { IStringable value = StringableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be text."); } return(new StringDeclaration(name, value)); } else { IListable value = ListableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be list."); } return(new ListDeclaration(name, value)); } } } }