Пример #1
0
        protected override JavaScriptObject Execute(SourcePosition pos, Scope scope, JavaScriptObject thisObject)
        {
            JavaScriptString str = scope.GetVariable("string", pos).ToJavaScriptString();
            Console.Write(str.ToString());

            return null;
        }
Пример #2
0
 /**
  * @param left  Left operand
  * @param right Right operand
  */
 protected BinaryOpNode(SourcePosition pos, String oper, Node left, Node right)
     : base(pos)
 {
     this.oper = oper;
     this.left = left;
     this.right = right;
 }
 public TooFewArgumentsException(
         String functionName,
         int noArgsRequired,
         int noArgs,
         SourcePosition pos)
     : base(functionName + " expects at least " + noArgsRequired
         + " arguments but got " + noArgs, pos)
 {
 }
Пример #4
0
 protected override JavaScriptObject Execute(SourcePosition pos, Scope scope, JavaScriptObject thisObject)
 {
     JavaScriptArray array = (JavaScriptArray)thisObject;
     FunctionArguments args = (FunctionArguments)scope.GetVariable("arguments", pos);
     foreach (JavaScriptObject obj in args)
     {
         array.Add(obj);
     }
     return array;
 }
Пример #5
0
        public JavaScriptObject GetVariable(string functionName, SourcePosition sourcePosition)
        {
            if (variablesAndFunctions.ContainsKey(functionName))
            {
                return variablesAndFunctions[functionName];
            }
            else if (parentscope != null)
            {
                return parentscope.GetVariable(functionName, sourcePosition);
            }

            throw new UnsetVariableException(functionName, sourcePosition);
        }
Пример #6
0
        public Function GetFunction(string functionName, SourcePosition sourcePosition)
        {
            if (this.HasFunction(functionName))
            {
                return (Function)variablesAndFunctions[functionName];
            }
            else if (parentscope != null)
            {
                return parentscope.GetFunction(functionName, sourcePosition);
            }

            throw new UnsetFunctionException(functionName, sourcePosition);
        }
 public UnsetPropertyException(string propertyName, SourcePosition pos)
     : base(propertyName + " is not set", pos)
 {
 }
 public UnsetFunctionException(String functionName, SourcePosition pos)
     : base("Function '" + functionName + "' does not exist", pos)
 {
 }
Пример #9
0
 private Token MatchStringLiteral(char quote)
 {
     SourcePosition pos = new SourcePosition(lineNo, columnNo);
     Match(quote);
     StringBuilder sb = new StringBuilder();
     int character = LookAhead(1);
     while (character != quote && character != END_OF_FILE)
     {
         sb.Append((char)character);
         character = Next();
     }
     Match(quote);
     return new Token(pos, TokenType.STRING_LITERAL, sb.ToString());
 }
Пример #10
0
 private Token MatchLineComment()
 {
     SourcePosition pos = new SourcePosition(lineNo, columnNo);
     Match("//");
     StringBuilder sb = new StringBuilder();
     int character = LookAhead(1);
     while (character != '\r' && character != '\n' && character != END_OF_FILE)
     {
         sb.Append((char)character);
         character = Next();
     }
     return new Token(pos, TokenType.COMMENT, sb.ToString());
 }
Пример #11
0
 private Token MatchBlockComment()
 {
     SourcePosition pos = new SourcePosition(lineNo, columnNo);
     Match("/*");
     StringBuilder sb = new StringBuilder();
     int character = LookAhead(1);
     while (true)
     {
         if (character == END_OF_FILE)
         {
             throw new LexerException("Expecting */ but found end of file", lineNo, columnNo);
         }
         if (LookAhead(1) == '*' && LookAhead(2) == '/')
         {
             break;
         }
         sb.Append((char)character);
         character = Next();
     }
     Match("*/");
     return new Token(pos, TokenType.COMMENT, sb.ToString());
 }
Пример #12
0
 public InterpreterException(String message, SourcePosition position)
     : base(message + " on line " + position.GetLineNumber() +
            " at column " + position.GetColumnNumber())
 {
 }
 public UnsetVariableException(String variableName, SourcePosition pos)
     : base("'" + variableName + "' is not set", pos)
 {
 }
 public UnsetFunctionException(String functionName, SourcePosition pos)
     : base("Function '" + functionName + "' does not exist", pos)
 {
 }
Пример #15
0
        private Node Statement()
        {
            // | (ID LPAREN) => functionCall  END_STATEMENT
            // | VARIABLE! ASSIGN! expression END_STATEMENT
            // | RETURN expression END_STATEMENT
            // | IF | WHILE | FOR_EACH
            TokenType type = LookAhead(1);

            if (type == TokenType.VARIABLE && LookAhead(2) == TokenType.LPAREN)
            {
                Node funcCall = FunctionCall();
                Match(TokenType.END_STATEMENT);
                return(funcCall);
            }
            else if (type == TokenType.VARIABLE &&

                     ((LookAhead(2) == TokenType.PLUS && LookAhead(3) == TokenType.PLUS) ||    // i++;

                      (LookAhead(2) == TokenType.MINUS) && LookAhead(3) == TokenType.MINUS) || // i--;

                     ((LookAhead(2) == TokenType.PLUS || LookAhead(2) == TokenType.MINUS) &&   // i+=n; or i-=n;
                      LookAhead(3) == TokenType.EQUAL))
            {
                Node selfExpression = Expression();
                Match(TokenType.END_STATEMENT);
                return(selfExpression);
            }
            else if (type == TokenType.VARIABLE || type == TokenType.VAR)
            {
                return(VariableDeclaration());
            }
            else if (type == TokenType.RETURN)
            {
                SourcePosition pos        = Match(TokenType.RETURN).Position;
                Node           expression = Expression();
                Match(TokenType.END_STATEMENT);
                return(new ReturnNode(pos, expression));
            }
            else if (type == TokenType.IF)
            {
                return(If());
            }
            else if (type == TokenType.WHILE)
            {
                return(While());
            }
            else if (type == TokenType.FOR)
            {
                return(For());
            }
            else if (type == TokenType.FUNCTION)
            {
                return(FunctionDeclaration());
            }
            else if (type == TokenType.THIS)
            {
                return(This());
            }
            else if (type == TokenType.TRY)
            {
                return(TryCatch());
            }
            else
            {
                // We only get here if there is token from the lexer
                // that is not handled by parser yet.
                throw new ParserException("Unknown token type " + type);
            }
        }
Пример #16
0
 public InvalidTypeException(String message, SourcePosition position)
     : base(message, position)
 {
 }
 public TypeMismatchException(SourcePosition pos, Type expect, Type actual)
     : base("Type mismatch - Excepted type '" + ToString(expect) + "' but got type '" + ToString(actual) + "'", pos)
 {
 }
Пример #18
0
 private Token CreateToken(TokenType type, char c)
 {
     SourcePosition pos = new SourcePosition(lineNo, columnNo);
     Match(c);
     return new Token(pos, type, "" + c);
 }
Пример #19
0
 public InvalidTypeException(String message, SourcePosition position)
     : base(message, position)
 {
 }
Пример #20
0
 public UnsetPropertyException(string propertyName, SourcePosition pos)
     : base(propertyName + " is not set", pos)
 {
 }
Пример #21
0
 public ArrayNode(SourcePosition pos, List<Node> elements)
     : base(pos)
 {
     this.elements = elements;
 }
Пример #22
0
        /**
         * An identifier is either a keyword, function, or variable
         *
         * @return Token
         */
        private Token MatchIdentifier()
        {
            SourcePosition pos       = new SourcePosition(lineNo, columnNo);
            StringBuilder  sb        = new StringBuilder();
            char           character = LookAhead(1);

            while ((character >= 'a' && character <= 'z') ||
                   (character >= 'A' && character <= 'Z') ||
                   (character >= '0' && character <= '9') ||
                   character == '_')
            {
                sb.Append((char)character);
                character = Next();
            }
            String word = sb.ToString();

            if (word.Equals("true"))
            {
                return(new Token(pos, TokenType.TRUE, word));
            }
            else if (word.Equals("false"))
            {
                return(new Token(pos, TokenType.FALSE, word));
            }
            else if (word.Equals("if"))
            {
                return(new Token(pos, TokenType.IF, word));
            }
            else if (word.Equals("else"))
            {
                return(new Token(pos, TokenType.ELSE, word));
            }
            else if (word.Equals("while"))
            {
                return(new Token(pos, TokenType.WHILE, word));
            }
            else if (word.Equals("for"))
            {
                return(new Token(pos, TokenType.FOR, word));
            }
            else if (word.Equals("as"))
            {
                return(new Token(pos, TokenType.IN, word));
            }
            else if (word.Equals("function"))
            {
                return(new Token(pos, TokenType.FUNCTION, word));
            }
            else if (word.Equals("var"))
            {
                return(new Token(pos, TokenType.VAR, word));
            }
            else if (word.Equals("return"))
            {
                return(new Token(pos, TokenType.RETURN, word));
            }
            else if (word.Equals("new"))
            {
                return(new Token(pos, TokenType.NEW, word));
            }
            else if (word.Equals("this"))
            {
                return(new Token(pos, TokenType.THIS, word));
            }
            //else if (word.Equals("prototype"))
            //{
            //    return new Token(pos, TokenType.PROTOTYPE, word);
            //}
            else if (word.Equals("try"))
            {
                return(new Token(pos, TokenType.TRY, word));
            }
            else if (word.Equals("catch"))
            {
                return(new Token(pos, TokenType.CATCH, word));
            }
            else if (word.Equals("finally"))
            {
                return(new Token(pos, TokenType.FINALLY, word));
            }
            else
            {
                return(new Token(pos, TokenType.VARIABLE, word));
            }
        }
Пример #23
0
 private Token CreateToken(TokenType type, String str)
 {
     SourcePosition pos = new SourcePosition(lineNo, columnNo);
     Match(str);
     return new Token(pos, type, str);
 }
Пример #24
0
 public ParserException(String message, SourcePosition position)
     : base(message, position)
 {
 }
Пример #25
0
 /**
  * An identifier is either a keyword, function, or variable
  *
  * @return Token
  */
 private Token MatchIdentifier()
 {
     SourcePosition pos = new SourcePosition(lineNo, columnNo);
     StringBuilder sb = new StringBuilder();
     char character = LookAhead(1);
     while ((character >= 'a' && character <= 'z') ||
             (character >= 'A' && character <= 'Z') ||
             (character >= '0' && character <= '9') ||
             character == '_')
     {
         sb.Append((char)character);
         character = Next();
     }
     String word = sb.ToString();
     if (word.Equals("true"))
     {
         return new Token(pos, TokenType.TRUE, word);
     }
     else if (word.Equals("false"))
     {
         return new Token(pos, TokenType.FALSE, word);
     }
     else if (word.Equals("if"))
     {
         return new Token(pos, TokenType.IF, word);
     }
     else if (word.Equals("else"))
     {
         return new Token(pos, TokenType.ELSE, word);
     }
     else if (word.Equals("while"))
     {
         return new Token(pos, TokenType.WHILE, word);
     }
     else if (word.Equals("for"))
     {
         return new Token(pos, TokenType.FOR, word);
     }
     else if (word.Equals("as"))
     {
         return new Token(pos, TokenType.IN, word);
     }
     else if (word.Equals("function"))
     {
         return new Token(pos, TokenType.FUNCTION, word);
     }
     else if (word.Equals("var"))
     {
         return new Token(pos, TokenType.VAR, word);
     }
     else if (word.Equals("return"))
     {
         return new Token(pos, TokenType.RETURN, word);
     }
     else if (word.Equals("new"))
     {
         return new Token(pos, TokenType.NEW, word);
     }
     else if (word.Equals("this"))
     {
         return new Token(pos, TokenType.THIS, word);
     }
     //else if (word.Equals("prototype"))
     //{
     //    return new Token(pos, TokenType.PROTOTYPE, word);
     //}
     else if (word.Equals("try"))
     {
         return new Token(pos, TokenType.TRY, word);
     }
     else if (word.Equals("catch"))
     {
         return new Token(pos, TokenType.CATCH, word);
     }
     else if (word.Equals("finally"))
     {
         return new Token(pos, TokenType.FINALLY, word);
     }
     else
     {
         return new Token(pos, TokenType.VARIABLE, word);
     }
 }
Пример #26
0
 protected override JavaScriptObject Execute(SourcePosition pos, Scope scope, JavaScriptObject thisObject)
 {
     JavaScriptInteger integer = scope.GetVariable("number", pos).ToInteger();
     return new JavaScriptInteger(integer.Value * integer.Value);
 }
Пример #27
0
 private Token MatchNumber()
 {
     SourcePosition pos = new SourcePosition(lineNo, columnNo);
     StringBuilder sb = new StringBuilder();
     bool isDecimal = false;
     int character = LookAhead(1);
     while ((character >= '0' && character <= '9') || character == '.')
     {
         if (isDecimal && character == '.')
         {
             throw new LexerException("Unexcepted '.' character", lineNo, columnNo);
         }
         else if (character == '.')
         {
             isDecimal = true;
         }
         sb.Append((char)character);
         character = Next();
     }
     if (isDecimal)
     {
         return new Token(pos, TokenType.FLOAT, sb.ToString());
     }
     else
     {
         return new Token(pos, TokenType.INTEGER, sb.ToString());
     }
 }
Пример #28
0
 public InterpreterException(String message, SourcePosition position)
     : base(message + " on line " + position.GetLineNumber() +
     " at column " + position.GetColumnNumber())
 {
 }
Пример #29
0
 protected override JavaScriptObject Execute(SourcePosition pos, Scope scope, JavaScriptObject thisObject)
 {
     JavaScriptString str = scope.GetVariable("string", pos).ToJavaScriptString();
     return new JavaScriptInteger(str.ToString().Length);
 }
Пример #30
0
 public Token(SourcePosition position, TokenType type, String text)
 {
     this.position = position;
     this.type = type;
     this.text = text;
 }