Esempio n. 1
0
        public static Node ParseFactor()
        {
            //Console.WriteLine("Parsing Factor");

            //Factor rules
            //1st -> num
            //1st -> +
            //1st -> -
            //1st -> (
            //+ -> factor
            //- -> factor
            //( -> expression
            //expression -> )
            //factor acts as num

            Node root;

            switch (tokens.actual.type)
            {
            case "INT":
                root       = new IntVal();
                root.value = tokens.actual.value;
                tokens.SelectNext();
                return(root);

            case "IDENTIFIER":
                root       = new Identifier((string)tokens.actual.value);
                root.value = tokens.actual.value;
                tokens.SelectNext();
                return(root);

            case "PLUS":
            case "MINUS":
                root       = new UnOp();
                root.value = tokens.actual.type == "PLUS" ? '+': '-';
                tokens.SelectNext();
                root.children[0] = ParseFactor();
                return(root);

            case "POPEN":
                tokens.SelectNext();
                root = ParseExpression();
                if (tokens.actual.type != "PCLOSE")
                {
                    throw new SystemException($"Missing closing parentesis (position {tokens.position}) [Line: {CurrentLine}]");
                }
                tokens.SelectNext();
                return(root);

            case "INPUT":
                root       = new UnOp();
                root.value = "input";
                tokens.SelectNext();
                return(root);
            }

            //End of term reached, but exiting was not allowed
            throw new SystemException($"Invalid expression format. Expression end was unexpected(position {tokens.position}) [Line: {CurrentLine}]");
        }
Esempio n. 2
0
        public static Node ParseFactor()
        {
            //Console.WriteLine("Parsing Factor");

            //Factor rules
            //1st -> num
            //1st -> +
            //1st -> -
            //1st -> not
            //1st -> (
            //+ -> factor
            //- -> factor
            //not -> factor
            //( -> expression
            //expression -> )
            //identifier acts as num
            //True | False acts as num

            Node root;

            switch (tokens.actual.type)
            {
            case "INT":
                root       = new IntVal();
                root.value = tokens.actual.value;
                tokens.SelectNext();
                return(root);

            case "IDENTIFIER":
                root       = new Identifier((string)tokens.actual.value);
                root.value = tokens.actual.value;
                tokens.SelectNext();

                //check if this is a function
                if (tokens.actual.type == "POPEN")
                {
                    FuncCall call = new FuncCall();
                    call.value = root.value;
                    tokens.SelectNext();

                    if (tokens.actual.type != "PCLOSE")
                    {
                        do
                        {
                            if (tokens.actual.type == "COMMA")
                            {
                                tokens.SelectNext();
                            }
                            call.Add(ParseRelExpression());
                        } while(tokens.actual.type == "COMMA");
                    }
                    Expect("PCLOSE", true);
                    return(call);
                }

                return(root);

            case "TRUE":
            case "FALSE":
                root       = new UnOp();
                root.value = tokens.actual.type == "TRUE" ? "true": "false";
                tokens.SelectNext();
                return(root);

            case "PLUS":
            case "MINUS":
                root       = new UnOp();
                root.value = tokens.actual.type == "PLUS" ? '+': '-';
                tokens.SelectNext();
                root.children[0] = ParseFactor();
                return(root);

            case "NOT":
                root       = new UnOp();
                root.value = "not";
                tokens.SelectNext();
                root.children[0] = ParseFactor();
                return(root);

            case "POPEN":
                tokens.SelectNext();
                root = ParseRelExpression();
                if (tokens.actual.type != "PCLOSE")
                {
                    throw new SystemException($"Missing closing parentesis (position {tokens.position}) [Line: {CurrentLine}]");
                }
                tokens.SelectNext();
                return(root);

            case "INPUT":
                root       = new UnOp();
                root.value = "input";
                tokens.SelectNext();
                return(root);
            }

            //End of term reached, but exiting was not allowed
            throw new SystemException($"Invalid expression format. Expression end was unexpected(position {tokens.position}) [Line: {CurrentLine}]");
        }