|
// SimpexTerm.cs:
//
// Copyright (C) 2007 by Alex Nek
// alexnek@russinger.com
// simpleparser.russinger.com
//
// This file is part of ExpressionParser classes
//
// ExpressionParser classes is free software:
// you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
//////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using SimpleParser;
namespace ExpressionsParser
{
/// <summary>
/// Simple exression term class
/// </summary>
class CSimpexTerm : CParseObject
{
#region Data Members
/// <summary>
/// Parsed Factor
/// </summary>
CSimpexFactor m_Factor = null;
/// <summary>
/// Value node
/// </summary>
CExpressionNode m_ValueNode = null;
#endregion
/// <summary>
/// Consructor
/// </summary>
public CSimpexTerm ()
{
}
/// <summary>
/// Parse lexical item
/// </summary>
/// <param name="Executor">Data Storage. We store parsed data here</param>
/// <param name="Parser">Data Parser. We parse input stream here</param>
/// <param name="Token">Next Token. We always have the next Token after parsing the current item</param>
public override void Parse(CExecutor Executor, CExpressionParser Parser, ref CToken Token)
{
m_Factor = new CSimpexFactor();
//Parse first factor
m_Factor.Parse(Executor, Parser, ref Token);
m_ValueNode = m_Factor.Node;
// we could have one or more * or / operators
while ((Token.TokenType == (int)CSimpexToken.ETokenType.Multiplay) || (Token.TokenType == (int)CSimpexToken.ETokenType.Divide))
{
CExpressionNode OperationNode = null;
//if we have allowed operator create and parse its
if (Token.TokenType == (int)CSimpexToken.ETokenType.Multiplay)
{
OperationNode = new COperationNode(COperationNode.EOPERATION.Multiplay);
//skip *
Parser.GetNextToken(ref Token);
}
else if (Token.TokenType == (int)CSimpexToken.ETokenType.Divide)
{
OperationNode = new COperationNode(COperationNode.EOPERATION.Divide);
//skip /
Parser.GetNextToken(ref Token);
}
if (OperationNode != null)
{
CSimpexFactor Factor = new CSimpexFactor();
//Parse second factor after operator
Factor.Parse(Executor, Parser, ref Token);
OperationNode.AddLeft(m_ValueNode);
OperationNode.AddRight(Factor.Node);
m_ValueNode = OperationNode;
}
}
}
#region Properties
/// <summary>
/// Current node
/// </summary>
public CExpressionNode Node
{
get
{
return m_ValueNode;
}
}
#endregion
}
}
#region Syntax
/*
'?' - repeated null or more time , '*' - repeated one or more times
--------------------------------------------------------------------
term ::= factor (multiplication_operator factor)?
multiplication_operator ::= '*' | '/'
factor ::= NUMBER | STRING | VARIABLE_NAME
| function_designator
| '(' expression ')' | ! factor
*/
#endregion
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I started my way with Fortran and going via Pascal, Delphi, C and C++. I'm falling in love with C# now.