Lexem Constant(ref int row, string substring) { float tempFloat; if (float.TryParse(substring.Replace('.', ','), out tempFloat)) { int tempInt; int index; if (int.TryParse(substring, out tempInt)) { index = AddConstant(tempInt, "int"); } else { index = AddConstant(tempFloat, "float"); } LexemList.Add(new Lexem(row, substring, 38, indexConst: index)); } else { throw new Exception($"Oops, I can`t understand constant {substring} in {row} row"); } return(null); }
Lexem MinusOrLexem(ref int row, string substring) { LexemList.Add(new Lexem(row, "-", 22)); substring = substring.Remove(0, 1); return(null); }
protected virtual void AnalysExpression(LexemList lexems) { for (int i = 0; i < lexems.Count; i++) { var lexem = lexems[i]; if (lexem is LexemBracket) { i = this.SkipBrackets(lexems, i); } else if (lexem is LexemSwitch && lexem.ToString() == "?") { var sw = lexem as LexemSwitch; var simetric = sw.Simetric; if (simetric == null) throw new FormatException("Switch syntax should have simetric Else path"); int index = lexems.FindPosition(simetric); if (index <= i) throw new FormatException("Switch syntax balance is incorrect"); var condition = lexems.Range(0, i); var then = lexems.Range(i + 1, index - i - 1); var other = lexems.Range(index + 1, lexems.Count - index - 1); this.OnSwitch(sw, condition, then, other); return; } } this.AnalysLogical(lexems); }
protected override void AnalysExpression(LexemList lexems) { var first = lexems == null ? null : lexems.FirstOrDefault() as LexemOperator; if (first != null && first.OperatorType == LexemOperator.OperationType.Arithmetic && first.Text == "-") lexems.Insert(0, 0); base.AnalysExpression(lexems); }
private void outputLexem() { nextLexem.value = currentLexemString.ToString(); LexemList.Add(nextLexem); currentLexemString.Clear(); currentState = State.Input; return; }
protected override void Analys(LexemList lexems) { if (lexems.Count > 2 && lexems[0] is LexemVariable && lexems[1] is LexemOperator && (lexems[1] as LexemOperator).Text == "=") { _variable = lexems[0] as LexemVariable; if (String.IsNullOrEmpty(_variable.Name)) throw new FormatException("Assignable Variable Name is empty"); this.AnalysExpression(lexems.Range(2, lexems.Count - 2)); } else this.AnalysExpression(lexems); }
protected virtual void AnalysElement(LexemList lexems) { if (lexems == null || lexems.Count <= 0) throw new FormatException("Analys Element is empty"); if (lexems.Count == 1) { this.OnElement(lexems[0]); return; } if (lexems[0] is LexemBracket && lexems.FindPosition((lexems[0] as LexemBracket).Simetric) == lexems.Count - 1) { this.OnElement(lexems); return; } this.Scan(lexems, LexemOperator.OperationType.Code, new string[] { "->", "." }, this.Error); }
Lexem ReservedLexem(ref int row, string substring) { if (substring == Environment.NewLine) { LexemList.Add(new Lexem(row, "ΒΆ", 3)); row++; } else { int code = Check.IsReservedLexem(substring); if (code != -1) { LexemList.Add(new Lexem(row, substring, code)); } else { throw new Exception($"Oops, I can`t understand constant '{substring}' in {row} row"); } } return(null); }
Lexem Identifier(ref int row, string substring) { Idnt tempIdnt = IdentifierList.Find(x => x.Name == substring); if (tempIdnt != null) { LexemList.Add(new Lexem(row, tempIdnt.Name, 34, indexIdnt: tempIdnt.Index)); return(null); } else { if (LexemList.Count == 0) { throw new Exception("Identifier can`t be first"); } Lexem temp = LexemList.Last <Lexem>(); if (temp.Code == 30) { IdentifierList.Add(new Idnt(substring, IdentifierList.Count, "int")); } else if (temp.Code == 31) { IdentifierList.Add(new Idnt(substring, IdentifierList.Count, "float")); } else if (temp.Code == 0) { IdentifierList.Add(new Idnt(substring, IdentifierList.Count, "program")); } else { throw new Exception($"Opps,I see a problem.Maybe you forgot about type 'int', 'float'or 'program' type in row {row} near '{substring}'"); } } LexemList.Add(new Lexem(row, substring, 34, indexIdnt: IdentifierList.Count - 1)); return(null); }
protected virtual void AnalysComparison(LexemList lexems) { this.Scan(lexems, LexemOperator.OperationType.Comparison, null, this.AnalysArithmetic); }
protected int SkipBrackets(LexemList lexems, int index, bool forward = true) { var bracket = lexems[index] as LexemBracket; if (bracket == null) return index; var result = index; if (bracket.Simetric != null) result = lexems.FindPosition(bracket.Simetric); if (forward && result <= index || !forward && result >= index) throw new FormatException($"Brackets balance is incorrect: {lexems}"); return result; }
protected virtual void AnalysArithmeticMultiplication(LexemList lexems) { this.Scan(lexems, LexemOperator.OperationType.Arithmetic, new string[] { "*", "/" }, this.AnalysElement); }
protected void Scan(LexemList lexems, LexemOperator.OperationType type, string[] operators, Action<LexemList> action, bool parseFromLeftToRight = false) { //ΠΎΠ±ΡΡΠ½ΡΠΉ ΡΠ°Π·Π±ΠΎΡ Π²ΡΡΠ°ΠΆΠ΅Π½ΠΈΡ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΠ΄ΡΠΈ ΡΠΏΡΠ°Π²Π° Π½Π°Π»Π΅Π²ΠΎ (<-) //Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ, ΠΏΠΎΠΏΡΠΎΠ±ΡΠΉΡΠ΅ ΠΏΠΎΡΡΡΠΎΠΈΡΡ ΠΏΠΎΠ»ΠΈΠ· Π΄Π»Ρ Π²ΡΡΠ°ΠΆΠ΅Π½ΠΈΡ: a-b+c ΠΈΠ»ΠΈ a/b*c //ΡΠΎΠ»ΡΠΊΠΎ ΡΠ°Π·Π±ΠΈΡΠ°Ρ ΠΎΠ΄Π½ΠΎΡΡΠΎΠ²Π½Π΅Π²ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠΏΡΠ°Π²Π° Π½Π°Π»Π΅Π²ΠΎ ΠΏΠΎΠ»ΡΡΠΈΡΡΡ ΠΊΠΎΡΡΠ΅ΠΊΡΠ½ΡΠΉ ΠΏΠΎΠ»ΠΈΠ·!!! for (int i = parseFromLeftToRight ? 0 : lexems.Count - 1; 0 <= i && i < lexems.Count; i += (parseFromLeftToRight ? 1 : -1)) { var lexem = lexems[i]; if (lexem is LexemBracket) { i = this.SkipBrackets(lexems, i, parseFromLeftToRight); } else if (lexem is LexemOperator) { var op = lexem as LexemOperator; if (op.OperatorType == type && (operators == null || operators.Contains(op.Text))) { this.OnOperator(op, lexems.Range(0, i), lexems.Range(i + 1, lexems.Count - i - 1)); return; } } } if (action != null) action(lexems); }
protected void AnalysArithmetic(LexemList lexems) { this.AnalysArithmeticAddition(lexems); }
private void ParserLine(List <LexicalUnit> lexUnitInRowList) { int tempInt; float tempFloat; foreach (var unit in lexUnitInRowList) { //if we have this lexem in reserved lexem tempInt = Check.IsReservedLexem(unit.Substring); if (tempInt >= 0) { LexemList.Add(new Lexem(unit.Row, unit.Substring, tempInt)); continue; } //if this lexem is a constant if (float.TryParse(unit.Substring.Replace('.', ','), out tempFloat)) { if (int.TryParse(unit.Substring, out tempInt)) { var const_ = ConstantList.Find(x => x._Const == tempInt); if (const_ != null) { LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: const_.Index)); } else { ConstantList.Add(new Const(tempInt, ConstantList.Count, "int")); LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: ConstantList.Count - 1)); } } else { var const_ = ConstantList.Find(x => x._Const == tempInt); if (const_ != null) { LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: const_.Index)); } else { ConstantList.Add(new Const(tempInt, ConstantList.Count, "float")); LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: ConstantList.Count - 1)); } } continue; } //if this lexem is a idnt; Idnt tempIdnt = IdentifierList.Find(x => x.Name == unit.Substring); if (tempIdnt != null) { LexemList.Add(new Lexem(unit.Row, tempIdnt.Name, 35, indexIdnt: tempIdnt.Index)); } else { if (lexUnitInRowList.Exists(x => (x.Substring.Contains("float"))) && (lexUnitInRowList.Exists(x => x.Substring.Contains("=")))) { IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "float")); LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1)); } else if (lexUnitInRowList.Exists(x => (x.Substring.Contains("int"))) && (lexUnitInRowList.Exists(x => x.Substring.Contains("=")))) { IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "int")); LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1)); } else if ((lexUnitInRowList.Exists(x => x.Substring.Contains("program")))) { IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "program")); LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1)); } else { throw new Exception($"not defined variable {unit.Substring} "); } } } }
protected abstract void OnSwitch(LexemSwitch lexem, LexemList condition, LexemList then, LexemList other);
protected void AnalysLogical(LexemList lexems) { this.AnalysLogicalOr(lexems); }
protected virtual void AnalysLogicalAnd(LexemList lexems) { this.Scan(lexems, LexemOperator.OperationType.Logical, new string[] { "&&" }, this.AnalysComparison); }
public static LexemPoliz Create(LexemList lexems) { var result = new LexemPoliz(); result.Analys(lexems); return result; }
protected override void AnalysExpression(LexemList lexems) { if (lexems != null && lexems.Count > 0 && lexems[0].ToString() == "-") lexems.Insert(0, 0); base.AnalysExpression(lexems); }
protected LexemPoliz(LexemList lexems) { _lexems = lexems; }
protected override void OnSwitch(LexemSwitch lexem, LexemList condition, LexemList then, LexemList other) { this.AnalysLogical(condition); _lexems.Add(lexem); this.AnalysExpression(then); _lexems.Add(lexem.Simetric); this.AnalysExpression(other); lexem.End = _lexems[_lexems.Count - 1]; }
protected LexemPoliz() { _lexems = new LexemList(); }
protected override void OnOperator(LexemOperator op, LexemList left, LexemList right) { switch (op.OperatorType) { case LexemOperator.OperationType.Logical: { if (op.Text == "||") { this.AnalysLogicalOr(left); this.AnalysLogicalAnd(right); _lexems.Add(op); } else if (op.Text == "&&") { this.AnalysLogicalAnd(left); this.AnalysComparison(right); _lexems.Add(op); } return; } case LexemOperator.OperationType.Comparison: { this.AnalysArithmetic(left); this.AnalysArithmetic(right); _lexems.Add(op); return; } case LexemOperator.OperationType.Arithmetic: { if (op.Text == "+" || op.Text == "-") { this.AnalysArithmeticAddition(left); this.AnalysArithmeticMultiplication(right); _lexems.Add(op); } else if (op.Text == "*" || op.Text == "/") { this.AnalysArithmeticMultiplication(left); this.AnalysElement(right); _lexems.Add(op); } return; } case LexemOperator.OperationType.Code: { if (op.Text == "->" || op.Text == ".") { this.AnalysElement(left); this.AnalysElement(right); _lexems.Add(op); } return; } } }
protected override void OnElement(LexemList lexems) { this.AnalysExpression(lexems.Range(1, lexems.Count - 2)); }
protected override void OnElement(LexemList lexems) { _lexems.Add(lexems[0]); this.AnalysExpression(lexems.Range(1, lexems.Count - 2)); _lexems.Add(lexems[lexems.Count - 1]); }
protected void Error(LexemList lexems) { throw new FormatException($"Expression syntax not recognized: {lexems}"); }
protected virtual void Analys(LexemList lexems) { this.AnalysExpression(lexems); }
protected abstract void OnElement(LexemList lexems);
protected virtual void AnalysLogicalOr(LexemList lexems) { this.Scan(lexems, LexemOperator.OperationType.Logical, new string[] { "||" }, this.AnalysLogicalAnd); }
protected abstract void OnOperator(LexemOperator op, LexemList left, LexemList right);