Exemplo n.º 1
0
 /// <summary>
 /// Moves the current scope back to its parent scope
 /// </summary>
 public static void ParentScope()
 {
     CurrentScope = CurrentScope.ParentScope;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Setup database with default types and operators
        /// </summary>
        public static void Setup()
        {
            GlobalScope.Id = "_";
            GlobalScope.ParentScope = null;

            CurrentScope = GlobalScope;

            //Setup default types
            Types = new List<TypeInfo>();
            Types.Add(new TypeInfo() { Name = "INT" });
            Types.Add(new TypeInfo() { Name = "CHAR" });
            Types.Add(new TypeInfo() { Name = "FLOAT" });
            Types.Add(new TypeInfo() { Name = "BOOL" });
            Types.Add(new TypeInfo() { Name = "DOUBLE" });
            Types.Add(new TypeInfo() { Name = "VOID" });
            Types.Add(new TypeInfo() { Name = "STRING" });

            //Operators
            Operators = new List<OperatorInfo>();
            Operators.Add(new OperatorInfo("+", false, GetType("DOUBLE"), GetType("DOUBLE"), GetType("DOUBLE"), 4));
            Operators.Add(new OperatorInfo("-", false, GetType("DOUBLE"), GetType("DOUBLE"), GetType("DOUBLE"), 4));
            Operators.Add(new OperatorInfo("*", false, GetType("DOUBLE"), GetType("DOUBLE"), GetType("DOUBLE"), 5));
            Operators.Add(new OperatorInfo("/", false, GetType("DOUBLE"), GetType("DOUBLE"), GetType("DOUBLE"), 4));
            Operators.Add(new OperatorInfo("<", false, GetType("DOUBLE"), GetType("DOUBLE"), GetType("BOOL"), 3));
            Operators.Add(new OperatorInfo(">", false, GetType("DOUBLE"), GetType("DOUBLE"), GetType("BOOL"), 3));
            Operators.Add(new OperatorInfo("<=", false, GetType("DOUBLE"), GetType("DOUBLE"), GetType("BOOL"), 3));
            Operators.Add(new OperatorInfo(">=", false, GetType("DOUBLE"), GetType("DOUBLE"), GetType("BOOL"), 3));
            Operators.Add(new OperatorInfo("!=", false, GetType("DOUBLE"), GetType("DOUBLE"), GetType("BOOL"), 3));
            Operators.Add(new OperatorInfo("==", false, GetType("BOOL"), GetType("BOOL"), GetType("BOOL"), 3));
            Operators.Add(new OperatorInfo("==", false, GetType("DOUBLE"), GetType("DOUBLE"), GetType("BOOL"), 3));
            Operators.Add(new OperatorInfo("&&", false, GetType("BOOL"), GetType("BOOL"), GetType("BOOL"), 2));
            Operators.Add(new OperatorInfo("||", false, GetType("BOOL"), GetType("BOOL"), GetType("BOOL"), 2));
            Operators.Add(new OperatorInfo("!", true, GetType("VOID"), GetType("BOOL"), GetType("BOOL"), -1));
            Operators.Add(new OperatorInfo("-", true, GetType("VOID"), GetType("DOUBLE"), GetType("DOUBLE"), -1));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates a new scope
 /// </summary>
 public static void NewScope()
 {
     Scope scope = new Scope();
     scope.ParentScope = CurrentScope;
     scope.Id = scope.ParentScope.Id + "_";
     CurrentScope.ChildScope.Add(scope);
     CurrentScope = scope;
 }
Exemplo n.º 4
0
        /// <summary>
        /// Given a variable name and scope, will return the type of that variable
        /// </summary>
        public static Int32 GetVarType(String name, Scope scope)
        {
            if (scope == null)
                scope = CurrentScope;

            foreach (var variable in scope.Variables)
            {
                if (variable.Name.Equals(name))
                    return variable.Type;
            }

            if (scope.ParentScope != null)
                return GetVarType(name, scope.ParentScope);

            return -1;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Given a variable name and scope, will return the unique ID of that variable
        /// </summary>
        public static String GetVarId(String name, Scope scope)
        {
            if (scope == null)
                scope = CurrentScope;

            foreach (var variable in scope.Variables)
            {
                if (variable.Name.Equals(name))
                    return variable.Id;
            }

            if (scope.ParentScope != null)
                return GetVarId(name, scope.ParentScope);

            return String.Empty;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Given a variable name and a scope, it will check if the variable exists in the scope
        /// or any of the scope's parents
        /// </summary>
        public static Boolean CheckVarInScope(String Name, Scope scope)
        {
            foreach (var variable in scope.Variables)
            {
                if (variable.Name.Equals(Name))
                    return true;
            }

            //Check parent scopes
            if (scope.ParentScope != null)
                return CheckVarInScope(Name, scope.ParentScope);

            return false;
        }
Exemplo n.º 7
0
 /// <summary>
 /// Adds a variable to the variable list
 /// </summary>
 public static void AddVariable(VariableInfo var, Scope scope)
 {
     if (scope != null)
         var.Id = scope.Id + var.Name;
     else
     {
         scope = CurrentScope;
         var.Id = var.Name;
     }
     scope.Variables.Add(var);
 }