private void assignValue(string variableName, ReturnValue valueToAssign)
        {
            Assert.IsNotNull(m_currentScope);

            //Console.WriteLine("Current scope: " + m_currentScope.getName());

            Symbol symbol = m_currentScope.resolve(variableName);
            if (symbol == null) {
                throw new InvalidOperationException("Can't resolve variable with name " + variableName);
            }

            ReturnValueType variableType = symbol.getReturnValueType();

            switch(variableType)
            {
                case ReturnValueType.FLOAT:
                    float floatValue = valueToAssign.FloatValue;
                    ReturnValue floatVal = new ReturnValue(floatValue);
                    m_currentMemorySpace.setValue(variableName, floatVal);
                    break;

                case ReturnValueType.STRING:
                    string stringValue = valueToAssign.StringValue;
                    ReturnValue stringVal = new ReturnValue(stringValue);
                    m_currentMemorySpace.setValue(variableName, stringVal);
                    break;

                default:
                    throw new InvalidOperationException("Can't assign to a variable of type + " + variableType);
            }
        }
Exemple #2
0
 private ReturnValue sqrt(ReturnValue[] parameters)
 {
     ReturnValue parameter0 = parameters[0];
     if (parameter0.getReturnValueType() == ReturnValueType.NUMBER)
     {
         return new ReturnValue((float)(Math.Sqrt(parameter0.NumberValue)));
     }
     else
     {
         m_sprakRunner.getRuntimeErrorHandler().errorOccured(new Error("Can't use sqrt on something that's not a number", Error.ErrorType.SYNTAX, 0, 0));
         return new ReturnValue(0.0f);
     }
 }
Exemple #3
0
 public void ChangeGlobalVariableInitValue(string pName, ReturnValue pReturnValue)
 {
     bool foundVariable = false;
     AST statementListTree = m_ast.getChild(0);
     AST globalVarDefs = statementListTree.getChild(0);
     if(globalVarDefs.getTokenString() != "<GLOBAL_VARIABLE_DEFINITIONS_LIST>") { throw new Exception("Wrong node, " + globalVarDefs.getTokenString()); }
     if(globalVarDefs.getChildren() != null && globalVarDefs.getChildren().Count > 0) {
         foreach(AST defAndAssignmentNode in globalVarDefs.getChildren()) {
             AST_Assignment assigmentNode = (AST_Assignment)defAndAssignmentNode.getChild(1);
             if(assigmentNode.VariableName == pName) {
                 defAndAssignmentNode.removeChild(1);
                 defAndAssignmentNode.addChild(CreateAssignmentTreeFromInitValue(pName, pReturnValue));
                 foundVariable = true;
                 break;
             }
         }
     }
     if(!foundVariable) {
         throw new Exception("Couldn't find and change the variable " + pName);
     }
 }
Exemple #4
0
 public ReturnValue(ReturnValueType type, object pData)
 {
     m_returnType = type;
     switch (m_returnType)
     {
         case ReturnValueType.STRING: m_stringValue = (string)pData; break;
         case ReturnValueType.NUMBER: m_numberValue = pData.GetType() == typeof(float) ? (float)pData : Convert.ToSingle(pData); break;
         case ReturnValueType.BOOL: m_boolValue = (bool)pData; break;
         case ReturnValueType.ARRAY:
         {
             m_arrayValue = new SortedDictionary<int, ReturnValue>();
             int i = 0;
             foreach(object element in (pData as IEnumerable))
             {
                 Type t = element.GetType();
                 ReturnValue rv = new ReturnValue(SystemTypeToReturnValueType(t), element);
                 m_arrayValue.Add(i++, rv);
             }
         }
         break;
         default:
             throw new Exception("Boxing error");
     }
 }
 private ReturnValue number(AST tree)
 {
     ReturnValue returnValue = new ReturnValue((float)Convert.ToDouble(tree.getTokenString()));
     return returnValue;
 }
Exemple #6
0
 private static ReturnValue API_type(ReturnValue[] args)
 {
     return new ReturnValue(args[0].getReturnValueType().ToString());
 }
Exemple #7
0
 private static ReturnValue API_toNumber(ReturnValue[] args)
 {
     return new ReturnValue(args[0].NumberValue);
 }
Exemple #8
0
 private static ReturnValue API_toArray(ReturnValue[] args)
 {
     return new ReturnValue(args[0].ArrayValue);
 }
Exemple #9
0
        private static ReturnValue API_range(ReturnValue[] args)
        {
            int start = (int)args[0].NumberValue;
            int end = (int)args[1].NumberValue;

            SortedDictionary<int, ReturnValue> array = new SortedDictionary<int, ReturnValue>();

            int step = 0;
            if(start < end) {
                step = 1;
                end++;
            } else {
                step = -1;
                end--;
            }
            int index = 0;
            for(int nr = start; nr != end; nr += step) {
                //Console.WriteLine("nr: " + nr);
                array[index] = new ReturnValue((float)nr);
                index++;
            }
            return new ReturnValue(array);
        }
Exemple #10
0
 private static ReturnValue API_count(ReturnValue[] args)
 {
     SortedDictionary<int, ReturnValue> array = args[0].ArrayValue;
     return new ReturnValue((float)array.Count);
 }
Exemple #11
0
 private ReturnValue f(ReturnValue[] parameters)
 {
     return new ReturnValue();
 }
 private void returnStatement(AST tree)
 {
     ReturnValue returnValue = new ReturnValue();
     if (tree.getChildren().Count > 0)
     {
         returnValue = execute(tree.getChild(0));
     }
     if(returnValue != null) {
     #if WRITE_DEBUG_INFO
         Console.Write("Return value was: ");
         printReturnValue(returnValue);
     #endif
         throw returnValue;
     }
 }
 private ReturnValue quotedString(AST tree)
 {
     ReturnValue returnValue = new ReturnValue(tree.getTokenString());
     return returnValue;
 }
 private void printReturnValue(ReturnValue returnValue)
 {
     if (returnValue.getType() == ReturnValueType.FLOAT) {
         Console.WriteLine(returnValue.FloatValue);
     }
     else if (returnValue.getType() == ReturnValueType.STRING) {
         Console.WriteLine(returnValue.StringValue);
     }
     else {
         Console.WriteLine("NULL");
     }
 }
        private ReturnValue operation(AST tree)
        {
            ReturnValue returnValue = null;

            float lhs = execute(tree.getChild(0)).FloatValue;
            float rhs = execute(tree.getChild(1)).FloatValue;

            if(tree.getTokenString() == "+") {
                returnValue = new ReturnValue(lhs + rhs);
            }
            else if(tree.getTokenString() == "-") {
                returnValue = new ReturnValue(lhs - rhs);
            }
            else if(tree.getTokenString() == "*") {
                returnValue = new ReturnValue(lhs * rhs);
            }
            else if(tree.getTokenString() == "/") {
                returnValue = new ReturnValue(lhs / rhs);
            }
            else if(tree.getTokenString() == "<") {
                float v = lhs < rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if(tree.getTokenString() == ">") {
                float v = lhs > rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if(tree.getTokenString() == "<=") {
                float v = lhs <= rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if(tree.getTokenString() == ">=") {
                float v = lhs >= rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if(tree.getTokenString() == "==") {
                float v = lhs == rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if(tree.getTokenString() == "!=") {
                float v = lhs != rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if(tree.getTokenString() == "&&") {
                float v = ((lhs != 0 ? true : false) && (rhs != 0 ? true : false)) ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if(tree.getTokenString() == "||") {
                float v = ((lhs != 0 ? true : false) || (rhs != 0 ? true : false)) ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else {
                throw new NotImplementedException("Operator " + tree.getTokenString() + " isn't implemented yet!");
            }

            return returnValue;
        }
Exemple #16
0
 // Used for the getInput() function
 public void SwapStackTopValueTo(ReturnValue pValue)
 {
     m_interpreter.SwapStackTopValueTo(pValue);
 }
Exemple #17
0
 private static ReturnValue API_allocate(ReturnValue[] args)
 {
     int size = (int)args[0].NumberValue;
     SortedDictionary<int, ReturnValue> array = new SortedDictionary<int, ReturnValue>();
     for(int i  = 0; i < size; i++) {
         array.Add(i, new ReturnValue(ReturnValueType.NUMBER));
     }
     return new ReturnValue(array);
 }
Exemple #18
0
 public VariableDefinition(string pVariableName, ReturnValue pInitValue)
 {
     variableName = pVariableName;
     initValue = pInitValue;
 }
Exemple #19
0
 private static ReturnValue API_createArrayOfArrayIndexes(ReturnValue[] args)
 {
     SortedDictionary<int, ReturnValue> originalArray = args[0].ArrayValue;
     SortedDictionary<int, ReturnValue> newArray = new SortedDictionary<int, ReturnValue>();
     int i = 0;
     foreach(int index in originalArray.Keys) {
         newArray.Add(i, new ReturnValue((float)index));
         i++;
     }
     return new ReturnValue(newArray);
 }
Exemple #20
0
 public void PackUnpackInt()
 {
     ReturnValue rv = new ReturnValue(ReturnValueType.NUMBER, 32);
     Assert.AreEqual(32.0f, (float)rv.Unpack(), 0.0001f);
     Assert.AreEqual(32, Convert.ToInt32(rv.Unpack()));
 }
Exemple #21
0
 private static ReturnValue API_removeElement(ReturnValue[] args)
 {
     SortedDictionary<int, ReturnValue> array = args[0].ArrayValue;
     int index = (int)args[1].NumberValue;
     array.Remove(index);
     return new ReturnValue();
 }
Exemple #22
0
        public void setValue(string name, ReturnValue val)
        {
            Debug.Assert(name != null);
            Debug.Assert(val != null);

            if(m_valuesForStrings.ContainsKey(name)) {
                //Console.WriteLine("Setting the value with name " + name + " and type " + val.getReturnValueType() + " to " + val + " in " + getName());
                m_valuesForStrings[name] = val;
            } else {
                //Console.WriteLine("Setting a new value with name " + name + " and type " + val.getReturnValueType() + " to " + val + " in " + getName());
                m_valuesForStrings.Add(name, val);
            }
        }
Exemple #23
0
 private static ReturnValue API_toBool(ReturnValue[] args)
 {
     return new ReturnValue(args[0].BoolValue);
 }
Exemple #24
0
 private static ReturnValue print(ReturnValue[] parameters)
 {
     ReturnValue parameter0 = parameters[0];
     m_output.Add(parameter0.ToString());
     return new ReturnValue(); // void
 }
Exemple #25
0
 private static ReturnValue API_toString(ReturnValue[] args)
 {
     return new ReturnValue(args[0].StringValue);
 }
Exemple #26
0
 public TokenWithValue(TokenType tokenType, string tokenString, int lineNr, int linePosition, ReturnValue pValue)
     : base(tokenType, tokenString, lineNr, linePosition)
 {
     m_value = pValue;
 }
Exemple #27
0
 private AST CreateAssignmentTreeFromInitValue(string pVariableName, ReturnValue pInitValue)
 {
     Token.TokenType tokenType;
     switch(pInitValue.getReturnValueType()) {
         case ReturnValueType.BOOL:
             tokenType = Token.TokenType.BOOLEAN_VALUE;
         break;
         case ReturnValueType.STRING:
             tokenType = Token.TokenType.QUOTED_STRING;
         break;
         case ReturnValueType.NUMBER:
             tokenType = Token.TokenType.NUMBER;
         break;
         case ReturnValueType.ARRAY:
             tokenType = Token.TokenType.ARRAY;
         break;
         case ReturnValueType.VOID:
             throw new Error("Can't assign void to variable");
         default:
             throw new Exception("Forgot to implement support for a type?");
     }
     Token initValueToken = new TokenWithValue(tokenType, pInitValue.ToString(), pInitValue);
     AST assignmentTree = new AST_Assignment(new Token(Token.TokenType.ASSIGNMENT, "="), pVariableName);
     assignmentTree.addChild(initValueToken);
     return assignmentTree;
 }
Exemple #28
0
 public TokenWithValue(TokenType pTokenType, string pTokenString, ReturnValue pValue)
     : base(pTokenType, pTokenString)
 {
     m_value = pValue;
 }
Exemple #29
0
 public void setValue(string name, ReturnValue val)
 {
     Scope scope = resolveToScope(name);
     Debug.Assert(scope != null, "scope is null, trying to set " + name + " from scope " + m_name);
     scope.m_memorySpaces.Peek().setValue(name, val);
 }
        private ReturnValue functionCall(AST tree)
        {
            ReturnValue returnValue = null;

            if (m_externalFunctionCreator.externalFunctions.ContainsKey(tree.getTokenString()))
            {
                ExternalFunctionCreator.OnFunctionCall functionCall = m_externalFunctionCreator.externalFunctions[tree.getTokenString()];
                if (functionCall != null)
                {
                    ReturnValue[] parameters = new ReturnValue[tree.getChildren().Count];
                    int i = 0;
                    foreach (AST parameter in tree.getChildren())
                    {
                        parameters[i] = execute(parameter);
                        i++;
                    }
                    returnValue = functionCall(parameters);
                }
                else
                {
                    throw new Error("Can't find external function " + tree.getTokenString(), Error.ErrorType.UNDEFINED, tree.getToken().LineNr, tree.getToken().LinePosition);
                }
            }
            else
            {
                // Call user defined function
                string functionName = tree.getTokenString();
                AST functionTree = getFunctionTreeNode(functionName);
                Assert.IsNotNull(functionTree);

                // Create list of parameter values
                List<ReturnValue> parameterValues = new List<ReturnValue>();
                List<AST> functionCallChildNodes = tree.getChildren();
                if (functionCallChildNodes != null)
                {
                    foreach(AST parameter in tree.getChildren())
                    {
                        ReturnValue val = execute(parameter);
                        parameterValues.Add(val);
                    }
                }

                returnValue = function(functionTree, parameterValues);
            }

            return returnValue;
        }