Esempio n. 1
0
        private void CheckFunction(ASTNode node)
        {
            string         name  = node.GetName();
            List <ASTNode> args  = node.GetChildren();
            Table          scope = node.GetScope();

            while (scope != null)
            {
                Symbol reference = scope.lookup(name);
                if (reference != null) //The identifier exists in the current scope
                {
                    if (args.Count == reference.GetParameters().Count)
                    {
                        for (int i = 0; i < args.Count; i++)
                        {
                            if (!(args[i].GetType() == reference.GetParameters()[i]))
                            {
                                Swift.error("The type of the parameter you supplied when calling \"" + name + "\" at the line " + args[i].GetContext().GetLine().ToString() + ", column " + args[i].GetContext().GetPos().ToString() + " is not the same type as required by the function", 1);
                            }
                        }
                        break;
                    }
                    else
                    {
                        Swift.error("The number of parameters you supplied when calling \"" + name + "\" at the line " + node.GetContext().GetLine().ToString() + ", column " + node.GetContext().GetPos().ToString() + " does not match the required number of parameters as defined in the function", 1);
                    }
                }
                scope = scope.GetReference();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Parses a statement
        /// </summary>
        /// <param name="token"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private ASTNode EatStatement(List <Token> tokensIn = null, List <LineContext> contextIn = null)
        {
            List <Token>       tmpTokens;
            List <LineContext> tmpContext;

            if (tokensIn == null)
            {
                tmpTokens  = tokens;
                tmpContext = context;
            }
            else
            {
                tmpTokens  = tokensIn;
                tmpContext = contextIn;
            }
            switch (tmpTokens[0].type)
            {
            case Global.DataType.IDENTIFIER:
                if (tmpTokens[1].type == Global.DataType.OPEN_ROUND_BRACKET)
                {
                    return(EatFunctionCall(tmpTokens, tmpContext));
                }
                else
                {
                    return(EatExpression(tmpTokens, tmpContext));
                }

            case Global.DataType.STRING:
                node = new ASTNode(Global.ASTType.STRING, tmpContext[0]);
                node.SetName(tmpTokens[0].value);
                return(node);

            default:
                Swift.error("Syntax error: \"" + tmpTokens[0].value + "\" at line " + tmpContext[0].GetLine().ToString() + ", colomn " + tmpContext[0].GetPos().ToString() + " could not be identified", 1);
                return(null);
            }
        }
        public Tuple <List <Token>, List <LineContext> > GetTokens(string[] input)
        {
            List <Token>       tokens  = new List <Token>();
            List <LineContext> context = new List <LineContext>();
            Match match;
            int   lineX;     //The character at the line
            int   lineY = 1; //The nth line as seen from the top

            foreach (string line_raw in input)
            {
                lineX = 1;
                string line = line_raw;
                while (line != "")
                {
                    string[] tmp = EatWhitespace(line);
                    line   = tmp[0];
                    lineX += int.Parse(tmp[1]);


                    //Comments


                    //Single line
                    match = Regex.Match(line, regexComment);
                    if (match.Success)
                    {
                        break;
                    }
                    ;


                    //Literals


                    //Integer
                    match = Regex.Match(line, regexInt);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.INT, match.Groups[0].Value));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;

                    //String
                    match = Regex.Match(line, regexString);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.STRING, match.Groups[0].Value));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;


                    //Keywords


                    //Let
                    match = Regex.Match(line, regexLet);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.LET));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;

                    //Var
                    match = Regex.Match(line, regexVar);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.VAR));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;


                    //Punctuation


                    //Open Round Bracket
                    match = Regex.Match(line, regexOpenRoundBracket);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.OPEN_ROUND_BRACKET));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;

                    //Close Round Bracket
                    match = Regex.Match(line, regexCloseRoundBracket);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.CLOSE_ROUND_BRACKET));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;

                    //Open Square Bracket
                    match = Regex.Match(line, regexOpenSquareBracket);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.OPEN_SQUARE_BRACKET));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;

                    //Close Square Bracket
                    match = Regex.Match(line, regexCloseSquareBracket);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.CLOSE_SQUARE_BRACKET));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;

                    //Open Braces
                    match = Regex.Match(line, regexOpenBraces);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.OPEN_BRACE));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;

                    //Close Braces
                    match = Regex.Match(line, regexCloseBraces);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.CLOSE_BRACE));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;


                    //Operator


                    //Operator
                    match = Regex.Match(line, regexOperator);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.OPERATOR, match.Groups[0].Value));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;


                    //Identifiers
                    match = Regex.Match(line, regexIdentity);
                    if (match.Success)
                    {
                        tokens.Add(new Token(Global.DataType.IDENTIFIER, match.Groups[0].Value));
                        context.Add(new LineContext(lineX, lineY));
                        line = line.Substring(match.Length); lineX += match.Length; continue;
                    }
                    ;

                    Swift.error("Syntax error: \"" + Regex.Match(line, "/^[^\\s]+").Value + "\" could not be identified", 1);
                }
                lineY++;
            }
            return(Tuple.Create(tokens, context));
        }