public override GeneralSymbol Evaluate() { switch (_operator) { case Operator.Plus: if (_expression.Evaluate().Type != Enums.Type.Number) { throw new Exception("Incorrect type"); } return(_expression.Evaluate()); case Operator.Minus: if (_expression.Evaluate().Type != Enums.Type.Number) { throw new Exception("Incorrect type"); } GeneralSymbol variableSymbolMinus = _expression.Evaluate(); variableSymbolMinus.Minus(); return(variableSymbolMinus); case Operator.Not: if (_expression.Evaluate().Type != Enums.Type.Bool) { throw new Exception("! cannot be applied to type " + _expression.Evaluate().Type); } GeneralSymbol variableSymbolNot = _expression.Evaluate(); variableSymbolNot.Not(); return(variableSymbolNot); } return(null); }
public StringExpression(string value) { _variableSymbol = new GeneralSymbol { Type = Enums.Type.String, StringValue = value }; }
public NumericalExpression(double value) { _variableSymbol = new GeneralSymbol { Type = Enums.Type.Number, NumberValue = value }; }
public BoolExpression(bool value) { _variableSymbol = new GeneralSymbol { Type = Enums.Type.Bool, BoolValue = value }; }
public VariableDeclerationStatement(Enums.Type type, string variableName, GeneralSymbol symbol) { _variableSymbol = symbol; _variableName = variableName; if (_variableSymbol.Type != type) { throw new Exception("Cannot declare type " + _variableSymbol.Type.ToString() + " as " + type); } }
public bool AssignVariable(string variableName, GeneralSymbol variableSymbol) { if (!_variableTable.ContainsKey(variableName)) { throw new Exception("Variable does not exist in current context!"); } _variableTable[variableName] = variableSymbol; return(true); }
public bool AddToVariableTable(string variableName, GeneralSymbol variableSymbol) { if (_variableTable.ContainsKey(variableName)) { throw new Exception("Variable already exists!"); } _variableTable.Add(variableName, variableSymbol); return(true); }
public override bool Execute() { if (_variableSymbol == null) { _variableSymbol = _value.Evaluate(); } GlobalSymbolTable.table.AddToVariableTable(_variableName, _variableSymbol); return(true); }
public override bool Execute() { _variableSymbol = _expression.Evaluate(); if (GlobalSymbolTable.table.GetVariableSymbol(_variableName).Type != _variableSymbol.Type) { throw new Exception("Cannot assign type " + _variableSymbol.Type + " to type " + GlobalSymbolTable.table.GetVariableSymbol(_variableName).Type); } GlobalSymbolTable.table.AssignVariable(_variableName, _variableSymbol); return(true); }
public override bool Execute() { _variableSymbol = _expression.Evaluate(); switch (_variableSymbol.Type) { case Enums.Type.String: Console.WriteLine(_variableSymbol.StringValue); return(true); case Enums.Type.Bool: Console.WriteLine(_variableSymbol.BoolValue); return(true); case Enums.Type.Number: Console.WriteLine(_variableSymbol.NumberValue); return(true); default: return(false); } }
public override GeneralSymbol Evaluate() { if (_expression1.Evaluate().Type != _expression2.Evaluate().Type) { throw new Exception("Type mismatch!"); } GeneralSymbol variableSymbol = new GeneralSymbol(); switch (_operator) { case Operator.Plus: if (_expression1.Evaluate().Type == Type.Number) { variableSymbol.Type = Type.Number; variableSymbol.NumberValue = _expression1.Evaluate().NumberValue + _expression2.Evaluate().NumberValue; } else if (_expression1.Evaluate().Type == Type.String) { variableSymbol.Type = Type.String; variableSymbol.StringValue = _expression1.Evaluate().StringValue + _expression2.Evaluate().StringValue; } else { throw new Exception("Incorrect type"); } return(variableSymbol); case Operator.Minus: if (_expression1.Evaluate().Type == Type.Number) { variableSymbol.Type = Type.Number; variableSymbol.NumberValue = _expression1.Evaluate().NumberValue - _expression2.Evaluate().NumberValue; } else { throw new Exception("Incorrect type"); } return(variableSymbol); case Operator.Multiply: if (_expression1.Evaluate().Type == Type.Number) { variableSymbol.Type = Type.Number; variableSymbol.NumberValue = _expression1.Evaluate().NumberValue *_expression2.Evaluate().NumberValue; } else { throw new Exception("Incorrect type"); } return(variableSymbol); case Operator.Divide: if (_expression1.Evaluate().Type == Type.Number) { variableSymbol.Type = Type.Number; variableSymbol.NumberValue = _expression1.Evaluate().NumberValue / _expression2.Evaluate().NumberValue; } else { throw new Exception("Incorrect type"); } return(variableSymbol); case Operator.And: if (_expression1.Evaluate().Type == Type.Bool) { variableSymbol.Type = Type.Bool; variableSymbol.BoolValue = _expression1.Evaluate().BoolValue&& _expression2.Evaluate().BoolValue; } else { throw new Exception("Incorrect type"); } return(variableSymbol); case Operator.Or: if (_expression1.Evaluate().Type == Type.Bool) { variableSymbol.Type = Type.Bool; variableSymbol.BoolValue = _expression1.Evaluate().BoolValue || _expression2.Evaluate().BoolValue; } else { throw new Exception("Incorrect type"); } return(variableSymbol); } return(null); }
public void Get_Test() { var testString = "Amanhã. Tempo...\n\n\nNão! Porquê?"; TextReader reader = new StringReader(testString); var endOfFileSymbType = "eof"; var charSymbolReader = new CharSymbolReader <string>(reader, "desconhecido", "final"); charSymbolReader.RegisterCharRangeType('a', 'z', "palavra"); charSymbolReader.RegisterCharRangeType('A', 'Z', "palavra"); charSymbolReader.RegisterCharType('ã', "palavra"); charSymbolReader.RegisterCharType('ê', "palavra"); charSymbolReader.RegisterCharType('.', "pontuação"); charSymbolReader.RegisterCharType('!', "pontuação"); charSymbolReader.RegisterCharType('?', "pontuação"); charSymbolReader.RegisterCharType('\n', "brancos"); charSymbolReader.RegisterCharType(' ', "brancos"); var target = new SimpleTextSymbolReader <string>( charSymbolReader, endOfFileSymbType, StringComparer.InvariantCultureIgnoreCase); // Todos os símbolos que se espera encontrar na expressão de acordo com a configuração. var expectedSymbols = new GeneralSymbol <string, string>[] { new GeneralSymbol <string, string>() { SymbolValue = "Amanhã", SymbolType = "palavra" }, new GeneralSymbol <string, string>() { SymbolValue = ".", SymbolType = "pontuação" }, new GeneralSymbol <string, string>() { SymbolValue = " ", SymbolType = "brancos" }, new GeneralSymbol <string, string>() { SymbolValue = "Tempo", SymbolType = "palavra" }, new GeneralSymbol <string, string>() { SymbolValue = "...", SymbolType = "pontuação" }, new GeneralSymbol <string, string>() { SymbolValue = "\n\n\n", SymbolType = "brancos" }, new GeneralSymbol <string, string>() { SymbolValue = "Não", SymbolType = "palavra" }, new GeneralSymbol <string, string>() { SymbolValue = "!", SymbolType = "pontuação" }, new GeneralSymbol <string, string>() { SymbolValue = " ", SymbolType = "brancos" }, new GeneralSymbol <string, string>() { SymbolValue = "Porquê", SymbolType = "palavra" }, new GeneralSymbol <string, string>() { SymbolValue = "?", SymbolType = "pontuação" } }; for (int i = 0; i < expectedSymbols.Length; ++i) { var expected = expectedSymbols[i]; var actual = target.Get(); Assert.AreEqual(expected.SymbolValue, actual.SymbolValue); Assert.AreEqual(expected.SymbolType, actual.SymbolType); } // Verifica se não existem mais símbolos a serem lidos. Assert.IsTrue(target.IsAtEOF()); }