Exemplo n.º 1
0
		public FloatingConstant (ISyntaxNode parent, ref string Input)
			: base (parent)
		{
			Pattern regExPattern =
				"^\\s*" +
				new Group("def",
					new Group("signess", "[+-]") +
					"?" + 
					new Group("nums",
						new Group("pre_comma", "\\d+") +
						"(\\." +
						new Group("post_comma", "\\d+") +
						")?([eE]" +
						new Group("exp", "-?\\d+") +
						")?"));

			System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(regExPattern);
			System.Text.RegularExpressions.Match match = regEx.Match(Input);

			if (!match.Success)
				throw new ParseException();
			//if (match.Index != 0)
			//	throw new ParseException();
			Input = Input.Remove(0, match.Index + match.Length);
			
			string value = match.Groups["nums"].Value;
			try
			{
				Value = Convert.ToDouble(value);

				I_Type = AtomicTypeSpecifier.Double(this);
			} 
			catch (OverflowException)
			{
				throw new SyntaxException("syntax error: value \"" + value + "\" is too large for a floating value.");
			}
		}
Exemplo n.º 2
0
 public IConstantValue(ISyntaxNode parent)
     : base(parent)
 {
 }
Exemplo n.º 3
0
        public static IRightValue Parse(ISyntaxNode parent, ref string Input)
        {
            string temp = Input;

            System.Text.RegularExpressions.Regex endRegEx      = new System.Text.RegularExpressions.Regex("^\\s*$");
            System.Text.RegularExpressions.Regex bracketsRegEx = new System.Text.RegularExpressions.Regex("^\\s*\\)\\s*");
            System.Text.RegularExpressions.Regex commaRegEx    = new System.Text.RegularExpressions.Regex("^\\s*,\\s*");

            IRightValue highestNode = null;

            while ((!endRegEx.IsMatch(Input)) && (!bracketsRegEx.IsMatch(Input)) && (!commaRegEx.IsMatch(Input)))
            {
                IntegerConstant iconst = TryParse <IntegerConstant>(parent, ref Input);
                if (iconst != null)
                {
                    if (highestNode != null)                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Invalid rvalue before integer constant.");
                    }
                    highestNode = iconst;
                    continue;
                }

                FloatingConstant fconst = TryParse <FloatingConstant>(parent, ref Input);
                if (fconst != null)
                {
                    if (highestNode != null)                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Invalid rvalue before floating constant.");
                    }
                    highestNode = fconst;
                    continue;
                }

                FunctionCall fcall = TryParse <FunctionCall>(parent, ref Input);
                if (fcall != null)
                {
                    if (highestNode != null)                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Invalid rvalue before function call.");
                    }
                    highestNode = fcall;
                    continue;
                }


                //string tmp = Input;
                IBinaryOperator binop = IBinaryOperator.Parse(parent, ref Input, highestNode);
                if (binop != null)
                {
                    //	Input = tmp;

                    if (highestNode == null)                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Missing first operand for binary operator.");
                    }
                    highestNode = binop;
                    continue;
                }

                IUnaryOperator unop = IUnaryOperator.Parse(parent, ref Input, highestNode);
                if (unop != null)
                {
                    if ((unop.Position == OperatorPosition.Postfix) && (highestNode == null))                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Missing first operand for unary operator.");
                    }
                    highestNode = unop;
                    continue;
                }

                Brackets backets = TryParse <Brackets>(parent, ref Input);
                if (backets != null)
                {
                    if (highestNode != null)                     // Function calls can only be the first one.
                    {
                        throw new SyntaxException("Syntax error: Invalid rvalue before brackets.");
                    }
                    highestNode = backets;
                    continue;
                }

                //	InfixOperator iopp = TryParse<InfixOperator>(ref Input, delegate(ref string i) { return new InfixOperator(parent, ref i, highestNode); });
                //	if (iopp != null)
                //		highestNode = fcall;

                // Well, if nothing got parsed, then it's a invalid expression
                throw new SyntaxException("Syntax error: Invalid token \"" + Input + "\"");
            }

            if ((highestNode is IOperator) &&
                ((highestNode as IOperator).SecondaryOperand is IOperator) &&
                (highestNode.Priority < (highestNode as IOperator).SecondaryOperand.Priority))
            {
                IOperator higher = (highestNode as IOperator);
                IOperator lower  = (IOperator)higher.SecondaryOperand;

                higher.SecondaryOperand = lower.PrimaryOperand;
                lower.PrimaryOperand    = higher;
                higher = lower;

                highestNode = higher;
            }


            return(highestNode);
        }
Exemplo n.º 4
0
 public IRightValue(ISyntaxNode parent)
     : base(parent)
 {
 }
Exemplo n.º 5
0
 public static AtomicTypeSpecifier Double(ISyntaxNode parent)
 {
     return(new AtomicTypeSpecifier(parent, "Double", false));
 }
Exemplo n.º 6
0
 public static AtomicTypeSpecifier Float(ISyntaxNode parent)
 {
     return(new AtomicTypeSpecifier(parent, "Float", false));
 }
Exemplo n.º 7
0
 public abstract ITypeSpecifier Clone(ISyntaxNode parent);
Exemplo n.º 8
0
 public VariableDeclaration(ISyntaxNode parent, ref string Input)
     : this(parent, ref Input, true)
 {
 }
Exemplo n.º 9
0
 public static AtomicTypeSpecifier UChar(ISyntaxNode parent)
 {
     return(new AtomicTypeSpecifier(parent, "Char", true));
 }
Exemplo n.º 10
0
 public override ITypeSpecifier Clone(ISyntaxNode parent)
 {
     return(new AtomicTypeSpecifier(parent, this.TypeName, this.Unsigned));
 }
Exemplo n.º 11
0
 /*
  * Static "Enum" members
  */
 public static AtomicTypeSpecifier Void(ISyntaxNode parent)
 {
     return(new AtomicTypeSpecifier(parent, "Void", false));
 }
Exemplo n.º 12
0
 public AtomicTypeSpecifier(ISyntaxNode parent, String type, bool unsigned)
     : base(parent)
 {
     this.TypeName = type.ToLower();
     Unsigned      = unsigned;
 }
Exemplo n.º 13
0
 public ISyntaxNode(ISyntaxNode parent)
 {
     Parent = parent;
 }
Exemplo n.º 14
0
        public IntegerConstant(ISyntaxNode parent, ref string Input)
            : base(parent)
        {
            Pattern regExPattern =
                "^\\s*" +
                new Group("def",
                          new Group("signess", "[+-]") +
                          "?" +
                          new Group("base", "0[xbo]") +
                          "?" +
                          new Group("nums", "\\d+"));

            System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(regExPattern);
            System.Text.RegularExpressions.Match match = regEx.Match(Input);

            if (!match.Success)
            {
                throw new ParseException();
            }
            //if (match.Index != 0)
            //	throw new ParseException();
            Input = Input.Remove(0, match.Index + match.Length);

            string value   = match.Groups["nums"].Value;
            int    numBase = 10;

            switch (match.Groups["base"].Value)
            {
            case "":
                numBase = 10;
                break;

            case "0x":
            case "0X":
                numBase = 16;
                break;

            case "0o":
            case "oO":
                numBase = 8;
                break;

            case "0b":
            case "0B":
                numBase = 2;
                break;
            }

            try
            {
                Value = Convert.ToInt32(value, numBase);
                if (match.Groups["signess"].Value == "-")
                {
                    Value = (Int32)Value * -1;
                }
                I_Type = AtomicTypeSpecifier.Int(this);
            }
            catch (OverflowException)
            {
                try
                {
                    Value = Convert.ToInt64(value, numBase);
                    if (match.Groups["signess"].Value == "-")
                    {
                        Value = (Int64)Value * -1;
                    }

                    I_Type = AtomicTypeSpecifier.Long(this);
                }
                catch (OverflowException)
                {
                    try
                    {
                        Value = Convert.ToUInt64(value);

                        if (match.Groups["signess"].Value == "-")
                        {
                            throw new SyntaxException("syntax error: value \"" + value + "\" is too large for a signed integer value.");
                        }

                        I_Type = AtomicTypeSpecifier.ULong(this);
                    }
                    catch (OverflowException)
                    {
                        throw new SyntaxException("syntax error: value \"" + value + "\" is too large for a integer value.");
                    }
                }
            }
        }
Exemplo n.º 15
0
 public static AtomicTypeSpecifier UInt(ISyntaxNode parent)
 {
     return(new AtomicTypeSpecifier(parent, "Int", true));
 }
Exemplo n.º 16
0
        public FunctionCall(ISyntaxNode parent, ref string Input)
            : base(parent)
        {
            Pattern regExPattern =
                "^\\s*" +
                new Group("def",
                          new Group("identifier", Provider.identifier) +
                          "\\s*\\(" +
                          new Group("params", "[a-zA-Z_0-9*\\+/!&|%()=,\\s]*") +
                          "\\)");

            System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(regExPattern);
            System.Text.RegularExpressions.Match match = regEx.Match(Input);

            if (!match.Success)
            {
                throw new ParseException();
            }
            //if (match.Index != 0)
            //	throw new ParseException();
            Input = Input.Remove(0, match.Index + match.Length);           // Also removes all starting spaces etc...

            Identifier = match.Groups["identifier"].Value;

            //System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("\\s*,\\s*" + new Group("", ""));
            if (!parent.IsFunctionDeclared(Identifier))
            {
                throw new SyntaxException("Syntax error: Call of undeclared function \"" + Identifier + "\".");
            }

            String             param      = match.Groups["params"].Value;
            List <IRightValue> parameters = new List <IRightValue>();

            System.Text.RegularExpressions.Regex endRegEx   = new System.Text.RegularExpressions.Regex("^\\s*$");
            System.Text.RegularExpressions.Regex commaRegEx = new System.Text.RegularExpressions.Regex("^\\s*,\\s*");

            while (!endRegEx.IsMatch(param))
            {
                IRightValue val = IRightValue.Parse(this, ref param);
                if (val == null)
                {
                    throw new SyntaxException("syntax error: Can't parse rvalue at function call.");
                }

                parameters.Add(val);

                if (endRegEx.IsMatch(param))
                {
                    break;
                }

                System.Text.RegularExpressions.Match comma = commaRegEx.Match(param);
                if (!comma.Success)
                {
                    throw new SyntaxException("syntax error: Function arguments must be separated by a comma.");
                }

                param = param.Remove(0, comma.Index + comma.Length);                 // Also removes all starting spaces etc...
            }

            this.Parameters = parameters.ToArray();;
        }
Exemplo n.º 17
0
 public static AtomicTypeSpecifier ULong(ISyntaxNode parent)
 {
     return(new AtomicTypeSpecifier(parent, "Long", true));
 }
Exemplo n.º 18
0
        public VariableDeclaration(ISyntaxNode parent, ref string Input, bool allowAssignment)
            : base(parent)
        {
            //	string regExPattern = "(?<def>(?<signdness>signed|unsigned)?\\s*(?<type>(void|char|short|int|long|float|double))(?<pointer>[\\s\\*]+)(?<identifier>[a-zA-Z_][a-zA-Z_0-9]*)\\s*(=\\s*(?<assignment>.*))?);(?<rest>.*)";

            Pattern regExPattern =
                "^\\s*" +
                new Group("def",
                          "(" +
                          new Group("signedness", "signed|unsigned") +
                          "\\s+" +
                          ")?" +
                          new Group("type", Provider.type) +
                          new Group("pointer", "[\\s\\*]+") +
                          new Group("identifier", Provider.identifier) +
                          "\\s*(" +
                          "=\\s*" +
                          new Group("assignment", ".*") +
                          ")?") +
                ";";

            System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(regExPattern);
            System.Text.RegularExpressions.Match match = regEx.Match(Input);

            if (!match.Success)
            {
                throw new ParseException();
            }
            //	if (match.Index != 0)
            //		throw new ParseException();
            Input = Input.Remove(0, match.Index + match.Length);             // Also removes all starting spaces etc...

            // Load signedness
            if (match.Groups["signedness"].Success)
            {
                this.Signedness = (Signedness)Enum.Parse(typeof(Signedness), match.Groups["signedness"].Value, true);
            }
            else
            {
                this.Signedness = this.DefaultSignedness;
            }

            // Load type
            Type = ITypeSpecifier.Parse(this, match.Groups["type"].Value);
            if (Type == null)
            {
                throw new SyntaxException("Error parsing variable: Expected type, got \"" + match.Groups["type"].Value + "\".");
            }

            // Load identifier
            Identifier = match.Groups["identifier"].Value;
            if ((match.Groups["identifier"].Success) && ((Identifier == null) || (Identifier == "")))
            {
                throw new SyntaxException("Error parsing variable: Expected identifier, got \"" + match.Groups["identifier"].Value + "\".");
            }

            // And last but not least possible assignments
            if (allowAssignment)
            {
                Assignment = match.Groups["assignment"].Value;
                if ((match.Groups["assignment"].Success) && ((Assignment == null) || (Assignment == "")))
                {
                    throw new SyntaxException("Error parsing variable: Expected assignment, got \"" + match.Groups["assignment"].Value + "\".");
                }
            }
            else if (match.Groups["assignment"].Success)
            {
                throw new SyntaxException("Error parsing variable: Assignment is not allowed here.");
            }
        }
Exemplo n.º 19
0
 protected ITypeSpecifier(ISyntaxNode parent)
     : base(parent)
 {
 }