public void AddDuplicatedLabelTest() { var table = new SymbolsTable(); table.Add("Sym", 4); Assert.Throws <SymbolsException>(() => table.Add("Sym", 5)); }
public void AddAndGetSymbolTest() { var table = new SymbolsTable(); table.Add("Sym"); Assert.Equal(16, table["Sym"]); }
public void AddAndGetLabelTest() { var table = new SymbolsTable(); table.Add("Sym", 5); Assert.Equal(5, table["Sym"]); }
public void GetUndeclaredAndAddTest() { var table = new SymbolsTable(); Assert.Equal(16, table.Get("Sym1")); table.Add("Sym1", 5); Assert.Equal(5, table.Get("Sym1")); }
private void CreateCodesTabel() { var unicode = 'а'; for (var i = 0; i < 32; i++) { var code = Convert.ToString(i, 2); var codeLength = code.Length; if (codeLength < 5) { for (var j = 0; j < 5 - codeLength; j++) { code = "0" + code; } } SymbolsTable.Add((char)(unicode + i), code); CodesTable.Add(code, (char)(unicode + i)); } }
public void Analysis(string inputText) { inputText = deleteReturnCorret(inputText); int lineCount = 1; int spaceCount = 0; for (int i = 0; i < inputText.Length;) { while (i < inputText.Length && inputText[i] == ' ' && inputText[i] == '\t') { spaceCount++; if (inputText[i] == '\t') { spaceCount += 3; } i++; } if (Char.IsLetter(inputText[i]) || inputText[i] == '_') { string temp = ""; while (i < inputText.Length && !symbols.ContainsKey(inputText[i])) { temp += inputText[i]; ++i; } if (words.ContainsKey(temp)) { TokensTable.Add(new Token(Token.TokenType.WORD, temp, lineCount, spaceCount)); } else { SymbolsTable.Add(new Symbol(temp)); TokensTable.Add(new Token(Token.TokenType.ID, (SymbolsTable.Count - 1).ToString(), lineCount, spaceCount)); } } else if (Char.IsDigit(inputText[i])) { string temp = ""; while (i < inputText.Length && !symbols.ContainsKey(inputText[i])) { temp += inputText[i]; ++i; } TokensTable.Add(new Token(Token.TokenType.CONST, temp, lineCount, spaceCount)); if (temp.Contains('.')) { TokensTable[TokensTable.Count - 1].ConstType = Symbol.SymType.FLOAT; } } else if (symbols.ContainsKey(inputText[i])) { if (symbols[inputText[i]] == Symbols.QMARK || symbols[inputText[i]] == Symbols.QQMARK) { i++; string temp = ""; while (i < inputText.Length && symbols[inputText[i]] != Symbols.QMARK && symbols[inputText[i]] != Symbols.QQMARK) { temp += inputText[i]; i++; } TokensTable.Add(new Token(Token.TokenType.CONST, temp, lineCount, spaceCount, Symbol.SymType.STRING)); i++; spaceCount = 0; continue; } if (symbols[inputText[i]] == Symbols.SRET) { lineCount++; } TokensTable.Add(new Token(Token.TokenType.SYMBOL, inputText[i].ToString(), lineCount, spaceCount)); i++; } else { ErrorsTable.Add(new ErrorMessage($"Unexpected symbol: {inputText[i]}", lineCount)); return; } spaceCount = 0; } }