Beispiel #1
0
        int IExpression.evaluate(SymTable <string, int> symTable)
        {
            int firstRes  = op1.evaluate(symTable);
            int secondRes = op2.evaluate(symTable);

            switch (op)
            {
            case '+':
                return(firstRes + secondRes);

            case '-':
                return(firstRes - secondRes);

            case '*':
                return(firstRes * secondRes);

            case '/':
                if (secondRes == 0)
                {
                    throw new DivideByZeroException("Division by 0!\n");
                }

                return(firstRes / secondRes);

            default:
                throw new Exception("Invalid Operator!\n");
            }
        }
Beispiel #2
0
        ProgramState IStmt.execute(ProgramState programState)
        {
            FileTable fileTable             = programState.getFileTable();
            SymTable <string, int> symTable = programState.getSymTable();

            bool found = false;

            foreach (Tuple <string, TextReader> t in fileTable.Values)
            {
                if (t.Item1.Equals(fileName))
                {
                    found = true;
                    break;
                }
            }

            if (found)
            {
                throw new Exception("File already open!\n");
            }
            if (symTable.ContainsKey(var_file_id))
            {
                throw new Exception("File id already exists!\n");
            }

            TextReader textReader = File.OpenText(fileName);
            int        id         = fileTable.Add(new Tuple <string, TextReader>(fileName, textReader));

            symTable.Add(var_file_id, id);
            return(null);
        }
 public void Init()
 {
     Program.TestMode = true;
     CST         = Program.BuildCST("kode.giraph");
     AST         = Program.BuildAST(CST);
     SymbolTable = Program.BuildSymbolTable(AST as StartNode);
 }
Beispiel #4
0
        int IExpression.evaluate(SymTable <string, int> symTable)
        {
            int firstRes  = op1.evaluate(symTable);
            int secondRes = op2.evaluate(symTable);

            switch (op)
            {
            case "<":
                return(firstRes < secondRes ? 1 : 0);

            case "<=":
                return(firstRes <= secondRes ? 1 : 0);

            case "==":
                return(firstRes == secondRes ? 1 : 0);

            case "!=":
                return(firstRes != secondRes ? 1 : 0);

            case ">=":
                return(firstRes >= secondRes ? 1 : 0);

            case ">":
                return(firstRes > secondRes ? 1 : 0);

            default:
                throw new Exception("Invalid Operator!\n");
            }
        }
Beispiel #5
0
        public Compilation(string sourcePath, string outputPath, string fileName)
        {
            string text = System.IO.File.ReadAllText(sourcePath); // test available in relative directory: sourcePath = "...\SAL\Antlr\Test_Parser_src\Test_Parser\Tests"

            p4Lexer           lexer  = new p4Lexer(new AntlrInputStream(text));
            CommonTokenStream stream = new CommonTokenStream(lexer);

            p4Parser   parser = new p4Parser(stream);
            IParseTree tree   = parser.s();

            ASTNode AST = new ConcreteP4Visitor().Visit(tree);
            //AST?.PrintTrees(0);

            SymTable s = new SymTable(AST);

            s.PrintErrors();
            TypeCheckVisitor t = new TypeCheckVisitor(AST);

            t.Errors.ForEach(s => Console.WriteLine(s.Message));

            CodeEmitter CodeGenerator = new CodeEmitter();

            CodeGenerator.GenerateCode(outputPath, fileName, AST);
            //test available in relative directory:
            //           outputPath = "...\SAL\Antlr\Test_Parser_src\Test_Parser\Tests"
            //           fileName = "Tests"
        }
Beispiel #6
0
 public override string ToString()
 {
     return("\tProgram State\nExecution Stack\n" + ExeStack.ToString() +
            "\n\nTable of Symbols: " + SymTable.ToString() +
            "\n\nOutput: " + Output.ToString() +
            "\n\nFile Table: " + FileTable.ToString() +
            "\n\n\n\n");
 }
 public void Init()
 {
     Program.TestMode = true;
     CST         = Program.BuildCST("kode_TypeChecker.giraph");
     AST         = Program.BuildAST(CST);
     SymbolTable = Program.BuildSymbolTable(AST as StartNode);
     Program.TypeCheck(SymbolTable, AST as StartNode);
     errorlist = SymbolTable.getTypeCheckErrorList();
 }
        public CILFunction DeclareFunction(SourceInfo si, string returnType, int returnPointerDepth, string name,
                                           List <CILVariableDecl> @params, bool isVarArgs)
        {
            var retType = SymTable.LookupType(returnType);
            var func    = new CILFunction(si, name, retType, returnPointerDepth, @params, isVarArgs);

            SymTable.DeclareFunction(func);
            return(func);
        }
Beispiel #9
0
 public ProgramState(IStmt statement)
 {
     _exeStack = new ExeStack <IStmt>();
     _exeStack.Push(statement);
     _symTable  = new SymTable <string, int>();
     _output    = new Output <int>();
     _fileTable = new FileTable();
     _program   = statement;
 }
Beispiel #10
0
 public ProgramState(ExeStack <IStmt> exeStack, Output <int> output, SymTable <string, int> symTable,
                     FileTable fileTable, IStmt program)
 {
     _exeStack  = exeStack;
     _output    = output;
     _symTable  = symTable;
     _fileTable = fileTable;
     _program   = program;
 }
Beispiel #11
0
 public override string ToString()
 {
     return
         ("ExeStack:\n" + ExeStack.ToString() +
          "SymLink:\n" + SymTable.ToString() +
          "FileTable:\n" + FileTable.ToString() +
          "Heap:\n" + Heap.ToString() +
          "Output:\n" + Output);
 }
        public CILFunction CreateFunction(SourceInfo si, string returnType, int returnPointerDepth, string name,
                                          List <CILVariableDecl> @params)
        {
            var retType = SymTable.LookupType(returnType);
            var func    = new CILFunction(si, name, retType, returnPointerDepth, @params, false);

            AddFunction(func);
            return(func);
        }
Beispiel #13
0
 int IExpression.evaluate(SymTable <string, int> symTable)
 {
     if (symTable.ContainsKey(name))
     {
         return(symTable[name]);
     }
     else
     {
         throw new Exception("Non-existent variable!\n");
     }
 }
Beispiel #14
0
        public OILNode findSymbol(String ident)
        {
            OILNode node  = null;
            int     i     = symbolStack.Count - 1;
            bool    found = false;

            while (!found && (i >= 0))
            {
                SymTable symtbl = symbolStack[i--];
                node  = symtbl.findSym(ident);
                found = (node != null);
            }
            return(node);
        }
Beispiel #15
0
        ProgramState IStmt.execute(ProgramState programState)
        {
            SymTable <string, int> symTable = programState.getSymTable();

            if (symTable.ContainsKey(varName))
            {
                symTable[varName] = exp.evaluate(symTable);
            }
            else
            {
                symTable.Add(varName, exp.evaluate(symTable));
            }

            return(null);
        }
Beispiel #16
0
        ProgramState IStmt.execute(ProgramState programState)
        {
            FileTable fileTable             = programState.getFileTable();
            SymTable <string, int> symTable = programState.getSymTable();

            int TextReader_id = exp_file_id.evaluate(symTable);

            if (!fileTable.ContainsKey(TextReader_id))
            {
                throw new Exception("Invalid file id!\n");
            }
            TextReader textReader = fileTable[TextReader_id].Item2;

            textReader.Close();

            fileTable.Remove(TextReader_id);
            return(null);
        }
        public static void CreateAST(bool isShowAST = false, bool isShowSymTable = false)
        {
            GetNextToken();
            CheckupClosedToken(Tokens.Token.K_NAMESPACE);

            headAST = ParseMainArea(Area.NAMESPACE);
            if (headAST != null)
            {
                if (isShowAST)
                {
                    headAST.Print("");
                }

                SymTable.CreateSymTable(headAST);
                if (isShowSymTable)
                {
                    SymTable.PrintSymTable();
                }

                SemAnalyzer.StartSemAnalyzer(headAST, SymTable.symTabls);
            }
        }
Beispiel #18
0
        ProgramState IStmt.execute(ProgramState programState)
        {
            FileTable fileTable             = programState.getFileTable();
            SymTable <string, int> symTable = programState.getSymTable();

            int id = exp_file_id.evaluate(symTable);

            if (!fileTable.ContainsKey(id))
            {
                throw new Exception("Invalid text reader id!\n");
            }
            TextReader textReader = fileTable[id].Item2;

            string line = textReader.ReadLine();
            int    value;

            if (line == null)
            {
                value = 0;
            }
            else
            {
                value = int.Parse(line);
            }

            if (symTable.ContainsKey(varName))
            {
                symTable[varName] = value;
            }
            else
            {
                symTable.Add(varName, value);
            }

            return(null);
        }
Beispiel #19
0
 public AstTypeCheckerVisitor(SymTable symbolTable)
 {
     _symbolTable = symbolTable;
 }
Beispiel #20
0
 int IExpression.evaluate(SymTable <string, int> symTable)
 {
     return(value);
 }
Beispiel #21
0
        //---------------------------------------------------------------------

        public Dynamo()
        {
            symTable  = new SymTable(this);
            codeGen   = new CodeGen(this);
            assembler = new Assembler(this);
        }
Beispiel #22
0
        public void enterScope()
        {
            SymTable symtbl = new SymTable();

            symbolStack.Add(symtbl);
        }
Beispiel #23
0
 public SymbolTable()
 {
     symbolStack = new List <SymTable>();
     global      = new SymTable();
     symbolStack.Add(global);
 }
 public void DeclareLocalVariable(CILVariableDecl variable)
 {
     SymTable.DeclareVar(variable);
 }
 public void PopScope()
 {
     SymTable.Pop();
 }
 public void PushScope()
 {
     SymTable.Push();
 }
 public void AddGlobal(CILVariableDecl global)
 {
     SymTable.DeclareGlobalVar(global);
     _globals.Add(global.Name, global);
 }
 public void AddStruct(CILStruct @struct)
 {
     SymTable.DeclareStruct(@struct);
     _structs.Add(@struct.Name, @struct);
 }
 public void AddFunction(CILFunction function)
 {
     SymTable.DeclareFunction(function);
     _functions.Add(function.Name, function);
 }