Beispiel #1
0
    /*
     * Parses TYPEDEF commands (First token, then delegates rest to specific TYPEDEF implementations).
     * Populates typedef table.
     */
    public static TYPEDEF Parse(string code, Dictionary <string, TYPEDEF> typedefTable)
    {
        if (String.IsNullOrEmpty(code))
        {
            throw new ArgumentNullException("code");
        }
        if (null == typedefTable)
        {
            throw new ArgumentNullException("typedefTable");
        }

        string[] tokens = code.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

        if (tokens.Length < 3)
        {
            throw new ArgumentException("TYPEDEF statements must consist of at least three tokens (TYPEDEF <NAME> <TYPE>), received: " + code);
        }
        if (tokens[0].ToUpper() != "TYPEDEF")
        {
            throw new ArgumentException("TYPEDEFs must start with keyword TYPEDEF, but received: " + tokens[0]);
        }

        TYPEDEF parsedType = null;
        string  Type       = tokens[2].ToUpper();

        if (Type == "BASIC")
        {
            parsedType = new BASIC(tokens, typedefTable);
        }
        else if (Type == "PRODUCT")
        {
            parsedType = new PRODUCT(tokens, typedefTable);
        }
        else if (Type == "SUM")
        {
            parsedType = new SUM(tokens, typedefTable);
        }
        else if (Type == "SEQUENCE")
        {
            parsedType = new SEQUENCE(tokens, typedefTable);
        }
        else if (Type == "FUNCTION")
        {
            parsedType = new FUNCTION(tokens, typedefTable);
        }
        else if (Type == "SUBTYPE")
        {
            parsedType = new SUBTYPE(tokens, typedefTable);
        }
        else
        {
            throw new SyntacticException("Not a valid TYPEDEF: " + code);
        }

        return(parsedType);
    }
Beispiel #2
0
    public override string Eval(List <Expression> expList, Dictionary <string, TYPEDEF> typedefTable, Dictionary <string, VARDECL> symbolTable)
    {
        if (expList.Count != 2)
        {
            throw new SemanticException("CONS Operator requires one expression (CONS <exp1> <exp2> ).");
        }

        string seqname1 = expList[0].Eval(typedefTable, symbolTable);
        string seqname2 = expList[1].Eval(typedefTable, symbolTable);

        if (!typedefTable.ContainsKey(seqname1))
        {
            throw new SemanticException("There is no SEQUENCE type named " + seqname1);
        }
        if (!typedefTable.ContainsKey(seqname2))
        {
            throw new SemanticException("There is no SEQUENCE type named " + seqname2);
        }

        TYPEDEF seqdef1 = typedefTable[seqname1];
        TYPEDEF seqdef2 = typedefTable[seqname2];

        if (seqdef1.Type != "SEQUENCE")
        {
            throw new SemanticException("CONS operator is only allowed on two SEQUENCEs");
        }
        if (seqdef2.Type != "SEQUENCE")
        {
            throw new SemanticException("CONS operator is only allowed on two SEQUENCEs");
        }

        if ((seqdef1 as SEQUENCE).AliasedType != (seqdef2 as SEQUENCE).AliasedType)
        {
            throw new SemanticException("Type Error: Can not CONS two different types together.");
        }

        return((seqdef1 as SEQUENCE).Name);
    }
Beispiel #3
0
    public override string Eval(List <Expression> expList, Dictionary <string, TYPEDEF> typedefTable, Dictionary <string, VARDECL> symbolTable)
    {
        if (expList.Count < 1)
        {
            throw new SemanticException("NIL Operator requires one expression (NIL <exp1> <exp2> ).");
        }

        string seqname1 = expList[0].Variable;

        if (!typedefTable.ContainsKey(seqname1))
        {
            throw new SemanticException("There is no SEQUENCE type named " + seqname1);
        }

        TYPEDEF seqdef1 = typedefTable[seqname1];

        if (seqdef1.Type != "SEQUENCE")
        {
            throw new SemanticException("NIL operator is only allowed on two SEQUENCEs");
        }

        return((seqdef1 as SEQUENCE).Name);
    }
Beispiel #4
0
    public override string Eval(Dictionary <string, TYPEDEF> typedefTable, Dictionary <string, VARDECL> symbolTable)
    {
        TYPEDEF type = typedefTable[symbolTable[(expList[0] as VariableExpression).Variable].Type];

        return(type.Eval(expList.GetRange(1, expList.Count - 1), typedefTable, symbolTable));
    }
Beispiel #5
0
    public static void Main(string[] args)
    {
        if (args.Length < 1 || !File.Exists(args[0]))
        {
            Console.WriteLine("tail.exe file.tail");
            System.Environment.Exit(0);
        }


        Dictionary <string, TYPEDEF> typedefTable = new Dictionary <string, TYPEDEF> ();
        Dictionary <string, VARDECL> symbolTable  = new Dictionary <string, VARDECL> ();


        string[] lines = File.ReadAllLines(args[0]);

        // Organize the TAIL contructs into lists.
        // Then process the lists.
        // I assume that if correct syntax is used and that
        // only TAIL expressions, typedefs, vardecls or empty lines
        // can exist in a tail file.
        int lineCount = 0;

        foreach (string line in lines)
        {
            try
            {
                if (line.ToUpper().StartsWith("TYPEDEF"))
                {
                    TYPEDEF.Parse(line, typedefTable);
                }
                else if (line.ToUpper().StartsWith("VARDECL"))
                {
                    new VARDECL(line, typedefTable, symbolTable);
                }
                else if (!String.IsNullOrEmpty(line))
                {
                    ExpressionEval.Eval(line, typedefTable, symbolTable);
                }
            }
            catch (SemanticException semex)
            {
                Console.WriteLine("Semantic Exception on line " + lineCount + " : " + semex.Message);
            }
            catch (SyntacticException synex)
            {
                Console.WriteLine("Syntax Exceptionon line " + lineCount + " : " + synex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Internal Exception: " + ex.Message);
            }

            lineCount++;
        }

        /*
         * foreach (string str in typedefTable.Keys)
         * {
         *  Console.WriteLine("Type: " + str);
         * }
         *
         * Console.WriteLine("");
         *
         * foreach (string str in symbolTable.Keys)
         * {
         *  Console.WriteLine("Var: " + str);
         * }*/
    }