예제 #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;
 }